Skip to content

Commit 6678ee0

Browse files
burnisonVaggelisD
andauthored
feat(mysql): Add INVISIBLE column constraint for MySQL. (#7510)
* Add `INVISIBLE` column constraint for MySQL. MySQL 8.0.23 introduced invisible columns, declared with the `INVISIBLE` keyword in column definitions. Until now, sqlglot could parse `ALTER TABLE ... ALTER COLUMN ... SET INVISIBLE` (PR #4809) and `ALTER TABLE ... ALTER INDEX ... INVISIBLE` (PR #4800), but the keyword was not recognized in column definitions. This meant `CREATE TABLE t (c INT INVISIBLE)` and `ALTER TABLE t ADD COLUMN c INT INVISIBLE` both failed to parse. This commit adds `InvisibleColumnConstraint`, following the same pattern as `AutoIncrementColumnConstraint` and `ZeroFillColumnConstraint`. The constraint is MySQL-specific and is registered in the MySQL dialect's `CONSTRAINT_PARSERS`. The generator emits the bare `INVISIBLE` keyword via an inline lambda in `TRANSFORMS`. Note that `SHOW CREATE TABLE` emits invisible columns using MySQL's version-comment syntax (`/*!80023 INVISIBLE */`), which the tokenizer treats as a comment. This commit does not address that; it only supports the bare keyword in input DDL. * Update tests/dialects/test_mysql.py --------- Co-authored-by: Vaggelis Danias <daniasevangelos@gmail.com>
1 parent 379c63e commit 6678ee0

4 files changed

Lines changed: 19 additions & 0 deletions

File tree

sqlglot/expressions/constraints.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ class AutoIncrementColumnConstraint(Expression, ColumnConstraintKind):
3333
pass
3434

3535

36+
class InvisibleColumnConstraint(Expression, ColumnConstraintKind):
37+
arg_types = {}
38+
39+
3640
class ZeroFillColumnConstraint(ColumnConstraint):
3741
arg_types = {}
3842

sqlglot/generator.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ class Generator:
234234
exp.ProjectionPolicyColumnConstraint: lambda self, e: (
235235
f"PROJECTION POLICY {self.sql(e, 'this')}"
236236
),
237+
exp.InvisibleColumnConstraint: lambda self, e: "INVISIBLE",
237238
exp.ZeroFillColumnConstraint: lambda self, e: "ZEROFILL",
238239
exp.Put: lambda self, e: self.get_put_sql(e),
239240
exp.RemoteWithConnectionModelProperty: lambda self, e: (

sqlglot/parsers/mysql.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ class MySQLParser(parser.Parser):
247247
"KEY": lambda self: self._parse_index_constraint(),
248248
"SPATIAL": lambda self: self._parse_index_constraint(kind="SPATIAL"),
249249
"ZEROFILL": lambda self: self.expression(exp.ZeroFillColumnConstraint()),
250+
"INVISIBLE": lambda self: self.expression(exp.InvisibleColumnConstraint()),
250251
}
251252

252253
ALTER_PARSERS = {

tests/dialects/test_mysql.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ def test_ddl(self):
2828
self.validate_identity("CREATE TABLE foo (a BIGINT, FULLTEXT INDEX (b))")
2929
self.validate_identity("CREATE TABLE foo (a BIGINT, SPATIAL INDEX (b))")
3030
self.validate_identity("CREATE TABLE foo (a INT UNSIGNED ZEROFILL)")
31+
self.validate_identity("CREATE TABLE foo (a INT INVISIBLE)")
32+
self.validate_identity("ALTER TABLE t ADD COLUMN c INT INVISIBLE")
3133
self.validate_identity("ALTER TABLE t1 ADD COLUMN x INT, ALGORITHM=INPLACE, LOCK=EXCLUSIVE")
3234
self.validate_identity("ALTER TABLE t ADD INDEX `i` (`c`)")
3335
self.validate_identity("ALTER TABLE t ADD UNIQUE `i` (`c`)")
@@ -1680,3 +1682,14 @@ def test_ignore_respect_nulls(self):
16801682
"snowflake": "SELECT LEAD(col1, 1) RESPECT NULLS OVER (ORDER BY col2 NULLS FIRST) FROM table1",
16811683
},
16821684
)
1685+
1686+
def test_invisible_column(self):
1687+
expr = self.parse_one("CREATE TABLE t (c INT INVISIBLE)")
1688+
self.assertIsNotNone(
1689+
expr.find(exp.InvisibleColumnConstraint)
1690+
)
1691+
1692+
expr = self.parse_one("ALTER TABLE t ADD COLUMN c INT INVISIBLE")
1693+
self.assertIsInstance(
1694+
expr.find(exp.InvisibleColumnConstraint)
1695+
)

0 commit comments

Comments
 (0)