Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues parsing MySQL with UNIQUE constraints #1707

Closed
RothNRK opened this issue May 31, 2023 · 0 comments · Fixed by #1708
Closed

Issues parsing MySQL with UNIQUE constraints #1707

RothNRK opened this issue May 31, 2023 · 0 comments · Fixed by #1708

Comments

@RothNRK
Copy link

RothNRK commented May 31, 2023

After creating a table in mysql, and getting the create sql string with SHOW CREATE TABLE, sqlglot is unable to parse the returned string.

I expect that sqlglot to correctly parse valid MySQL strings.

  1. Create a MySQL table:
CREATE TABLE foo (
  id CHAR(36) DEFAULT (UUID()) UNIQUE,
  created_at TIMESTAMP,
  PRIMARY KEY (id)
)
  1. Get create table sql:
SHOW CREATE TABLE foo

> CREATE TABLE `foo` (
  `id` char(36) NOT NULL DEFAULT (uuid()),
  `created_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

  1. Parse string:
sql = """CREATE TABLE `foo` (
  `id` char(36) NOT NULL DEFAULT (uuid()),
  `created_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci"""

sqlglot.parse_one(sql, "mysql")

>>> ParseError: Expecting ). Line 6, Col: 12.
  T NULL DEFAULT (uuid()),
  `created_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

If you remove the UNIQUE KEY line the string will parse.

sql = """CREATE TABLE `foo` (
  `id` char(36) NOT NULL DEFAULT (uuid()),
  `created_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci"""

sqlglot.parse_one(sql, "mysql")

>>> (CREATE this: 
  (SCHEMA this: 
    (TABLE this: 
      (IDENTIFIER this: foo, quoted: True)), expressions: 
    (COLUMNDEF this: 
      (IDENTIFIER this: id, quoted: True), kind: 
      (DATATYPE this: Type.CHAR, expressions: 
        (DATATYPESIZE this: 
          (LITERAL this: 36, is_string: False)), nested: False, prefix: False), constraints: 
      (COLUMNCONSTRAINT kind: 
        (NOTNULLCOLUMNCONSTRAINT )), 
      (COLUMNCONSTRAINT kind: 
        (DEFAULTCOLUMNCONSTRAINT this: 
          (PAREN this: 
            (ANONYMOUS this: uuid))))), 
    (COLUMNDEF this: 
      (IDENTIFIER this: created_at, quoted: True), kind: 
      (DATATYPE this: Type.TIMESTAMP), constraints: 
      (COLUMNCONSTRAINT kind: 
        (NOTNULLCOLUMNCONSTRAINT allow_null: True)), 
      (COLUMNCONSTRAINT kind: 
        (DEFAULTCOLUMNCONSTRAINT this: 
          (NULL )))), 
    (PRIMARYKEY expressions: 
      (IDENTIFIER this: id, quoted: True))), kind: TABLE, properties: 
  (PROPERTIES expressions: 
    (ENGINEPROPERTY this: 
      (IDENTIFIER this: InnoDB, quoted: False)), 
    (CHARACTERSETPROPERTY this: 
      (VAR this: utf8mb4), default: True), 
    (COLLATEPROPERTY this: 
      (IDENTIFIER this: utf8mb4_0900_ai_ci, quoted: False))))

How can I parse sql string with a UNIQUE key?

Thank you in advance for any help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant