Skip to content

Commit 1109b27

Browse files
committed
feat(mysql): support ZEROFILL column attribute
- Implemented `_parse_types` in `MySQL.Parser` to consume the `ZEROFILL` token and store it in the DataType expression. - Updated `datatype_sql` in `MySQL.Generator` to output `ZEROFILL` when the attribute is present. This fixes parsing errors encountered when processing legacy MySQL schemas containing numeric columns with the zerofill property.
1 parent 67f499d commit 1109b27

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

sqlglot/dialects/mysql.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,14 @@ def _parse_set_item_names(self) -> exp.Expression:
702702

703703
return self.expression(exp.SetItem, this=charset, collate=collate, kind="NAMES")
704704

705+
def _parse_types(self, *args, **kwargs) -> t.Optional[exp.Expression]:
706+
dtype = super()._parse_types(*args, **kwargs)
707+
708+
if dtype and self._match_text_seq("ZEROFILL"):
709+
dtype.set("zerofill", True)
710+
711+
return dtype
712+
705713
def _parse_type(
706714
self, parse_interval: bool = True, fallback_to_identifier: bool = False
707715
) -> t.Optional[exp.Expression]:
@@ -1211,6 +1219,8 @@ def datatype_sql(self, expression: exp.DataType) -> str:
12111219
if expression.this in self.UNSIGNED_TYPE_MAPPING:
12121220
result = f"{result} UNSIGNED"
12131221

1222+
if expression.args.get("zerofill"):
1223+
result = f"{result} ZEROFILL"
12141224
return result
12151225

12161226
def jsonarraycontains_sql(self, expression: exp.JSONArrayContains) -> str:

tests/dialects/test_mysql.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def test_ddl(self):
2626
self.validate_identity("CREATE TABLE foo (a BIGINT, INDEX USING BTREE (b))")
2727
self.validate_identity("CREATE TABLE foo (a BIGINT, FULLTEXT INDEX (b))")
2828
self.validate_identity("CREATE TABLE foo (a BIGINT, SPATIAL INDEX (b))")
29+
self.validate_identity("CREATE TABLE foo (a INT UNSIGNED ZEROFILL)")
2930
self.validate_identity("ALTER TABLE t1 ADD COLUMN x INT, ALGORITHM=INPLACE, LOCK=EXCLUSIVE")
3031
self.validate_identity("ALTER TABLE t ADD INDEX `i` (`c`)")
3132
self.validate_identity("ALTER TABLE t ADD UNIQUE `i` (`c`)")

0 commit comments

Comments
 (0)