sql-parser icon indicating copy to clipboard operation
sql-parser copied to clipboard

Parsing failure CREATE TABLE with order in INDEX

Open Gared opened this issue 5 years ago • 1 comments

Hey this parser is very cool and searched forever until I found it :-) I just have a problem parsing my scheme definition when I have a CREATE TABLE query with an INDEX in it:

CREATE TABLE test (
  user_id INT,
  INDEX `test` (user_id ASC),
)

This gets parsed as the following definition:

object(PhpMyAdmin\SqlParser\Components\CreateDefinition)#2102 (6) {
  ["name"]=>
  NULL
  ["isConstraint"]=>
  NULL
  ["type"]=>
  NULL
  ["key"]=>
  object(PhpMyAdmin\SqlParser\Components\Key)#2103 (4) {
    ["name"]=>
    string(4) "test"
    ["columns"]=>
    array(1) {
      [0]=>
      array(1) {
        ["name"]=>
        string(3) "ASC"
      }
    }
    ["type"]=>
    string(5) "INDEX"
    ["options"]=>
    object(PhpMyAdmin\SqlParser\Components\OptionsArray)#2104 (1) {
      ["options"]=>
      array(0) {
      }
    }
  }
  ["references"]=>
  NULL
  ["options"]=>
  NULL
}

The problem is in $object->key->columns I have an array with "ASC" in it, but this should be "user_id" (the name of the column - not the order). If I remove the order ("ASC") the parsing works as expected.

Tested it with the latest version (5.3.1)

Gared avatar Jul 29 '20 16:07 Gared

Thank you for the report and feedback @Gared ! If you want to attempt a fix please use QA branch.

williamdes avatar Jul 29 '20 16:07 williamdes

{
    "query": "CREATE TABLE test (\n  user_id INT,\n  INDEX `test` (user_id ASC)\n)",
    "lexer": {
        "@type": "PhpMyAdmin\\SqlParser\\Lexer",
        "str": "CREATE TABLE test (\n  user_id INT,\n  INDEX `test` (user_id ASC)\n)",
        "len": 65,
        "last": 65,
        "list": {
            "@type": "PhpMyAdmin\\SqlParser\\TokensList",
            "tokens": [
            // removed the long list, not relevant here
            ],
            "count": 25,
            "idx": 25
        },
        "delimiter": ";",
        "delimiterLen": 1,
        "strict": false,
        "errors": []
    },
    "parser": {
        "@type": "PhpMyAdmin\\SqlParser\\Parser",
        "list": {
            "@type": "@1"
        },
        "statements": [
            {
                "@type": "PhpMyAdmin\\SqlParser\\Statements\\CreateStatement",
                "name": {
                    "@type": "PhpMyAdmin\\SqlParser\\Components\\Expression",
                    "database": null,
                    "table": "test",
                    "column": null,
                    "expr": "test",
                    "alias": null,
                    "function": null,
                    "subquery": null
                },
                "entityOptions": {
                    "@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray",
                    "options": []
                },
                "fields": [
                    {
                        "@type": "PhpMyAdmin\\SqlParser\\Components\\CreateDefinition",
                        "name": "user_id",
                        "isConstraint": null,
                        "type": {
                            "@type": "PhpMyAdmin\\SqlParser\\Components\\DataType",
                            "name": "INT",
                            "parameters": [],
                            "options": {
                                "@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray",
                                "options": []
                            }
                        },
                        "key": null,
                        "references": null,
                        "options": {
                            "@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray",
                            "options": []
                        }
                    },
                    {
                        "@type": "PhpMyAdmin\\SqlParser\\Components\\CreateDefinition",
                        "name": null,
                        "isConstraint": null,
                        "type": null,
                        "key": {
                            "@type": "PhpMyAdmin\\SqlParser\\Components\\Key",
                            "name": "test",
                            "columns": [
                                {
                                    // name is detected
                                    "name": "user_id",
                                   // order seems to be understood
                                    "order": "ASC"
                                }
                            ],
                            "type": "INDEX",
                            "expr": null,
                            "options": {
                                "@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray",
                                "options": []
                            }
                        },
                        "references": null,
                        "options": null
                    }
                ],
                "with": null,
                "select": null,
                "like": null,
                "partitionBy": null,
                "partitionsNum": null,
                "subpartitionBy": null,
                "subpartitionsNum": null,
                "partitions": null,
                "table": null,
                "return": null,
                "parameters": null,
                "body": [],
                "options": {
                    "@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray",
                    "options": {
                        "6": "TABLE"
                    }
                },
                "first": 0,
                "last": 24
            }
        ],
        "brackets": 0,
        "strict": false,
        "errors": []
    },
    "errors": {
        "lexer": [],
        "parser": []
    }
}

williamdes avatar Apr 21 '23 17:04 williamdes

Duplicate of #296

williamdes avatar Apr 21 '23 17:04 williamdes

I think this was fixed in #350

williamdes avatar Apr 21 '23 17:04 williamdes