Skip to content

Commit

Permalink
Fix(mysql): convert VARCHAR without size to TEXT for DDLs
Browse files Browse the repository at this point in the history
  • Loading branch information
georgesittas committed Sep 9, 2024
1 parent 6294f9e commit cf253dc
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions sqlglot/dialects/doris.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class Parser(MySQL.Parser):

class Generator(MySQL.Generator):
LAST_DAY_SUPPORTS_DATE_PART = False
VARCHAR_REQUIRES_SIZE = False

TYPE_MAPPING = {
**MySQL.Generator.TYPE_MAPPING,
Expand Down
10 changes: 10 additions & 0 deletions sqlglot/dialects/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,7 @@ class Generator(generator.Generator):
PARSE_JSON_NAME: t.Optional[str] = None
PAD_FILL_PATTERN_IS_REQUIRED = True
WRAP_DERIVED_VALUES = False
VARCHAR_REQUIRES_SIZE = True

TRANSFORMS = {
**generator.Generator.TRANSFORMS,
Expand Down Expand Up @@ -1117,10 +1118,19 @@ def extract_sql(self, expression: exp.Extract) -> str:
return super().extract_sql(expression)

def datatype_sql(self, expression: exp.DataType) -> str:
if (
self.VARCHAR_REQUIRES_SIZE
and expression.is_type(exp.DataType.Type.VARCHAR)
and not expression.expressions
):
# `VARCHAR` must always have a size - if it doesn't, we always generate `TEXT`
return "TEXT"

# https://dev.mysql.com/doc/refman/8.0/en/numeric-type-syntax.html
result = super().datatype_sql(expression)
if expression.this in self.UNSIGNED_TYPE_MAPPING:
result = f"{result} UNSIGNED"

return result

def jsonarraycontains_sql(self, expression: exp.JSONArrayContains) -> str:
Expand Down
1 change: 1 addition & 0 deletions sqlglot/dialects/starrocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def _parse_unnest(self, with_alias: bool = True) -> t.Optional[exp.Unnest]:
class Generator(MySQL.Generator):
EXCEPT_INTERSECT_SUPPORT_ALL_CLAUSE = False
JSON_TYPE_REQUIRED_FOR_EXTRACTION = False
VARCHAR_REQUIRES_SIZE = False
PARSE_JSON_NAME: t.Optional[str] = "PARSE_JSON"
WITH_PROPERTIES_PREFIX = "PROPERTIES"

Expand Down
8 changes: 8 additions & 0 deletions tests/dialects/test_mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ def test_ddl(self):
self.validate_identity(
"CREATE OR REPLACE VIEW my_view AS SELECT column1 AS `boo`, column2 AS `foo` FROM my_table WHERE column3 = 'some_value' UNION SELECT q.* FROM fruits_table, JSON_TABLE(Fruits, '$[*]' COLUMNS(id VARCHAR(255) PATH '$.$id', value VARCHAR(255) PATH '$.value')) AS q",
)
self.validate_identity(
"CREATE TABLE t (name VARCHAR)",
"CREATE TABLE t (name TEXT)",
)
self.validate_identity(
"ALTER TABLE t ADD KEY `i` (`c`)",
"ALTER TABLE t ADD INDEX `i` (`c`)",
Expand Down Expand Up @@ -175,6 +179,10 @@ def test_identity(self):
self.validate_identity(
"REPLACE INTO table SELECT id FROM table2 WHERE cnt > 100", check_command_warning=True
)
self.validate_identity(
"CAST(x AS VARCHAR)",
"CAST(x AS CHAR)",
)
self.validate_identity(
"""SELECT * FROM foo WHERE 3 MEMBER OF(info->'$.value')""",
"""SELECT * FROM foo WHERE 3 MEMBER OF(JSON_EXTRACT(info, '$.value'))""",
Expand Down

0 comments on commit cf253dc

Please sign in to comment.