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

fix(tsql, postgres)!: Improve UUID support #4718

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sqlglot/dialects/spark.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class Generator(Spark2.Generator):
**Spark2.Generator.TYPE_MAPPING,
exp.DataType.Type.MONEY: "DECIMAL(15, 4)",
exp.DataType.Type.SMALLMONEY: "DECIMAL(6, 4)",
exp.DataType.Type.UNIQUEIDENTIFIER: "STRING",
exp.DataType.Type.UUID: "STRING",
exp.DataType.Type.TIMESTAMPLTZ: "TIMESTAMP_LTZ",
exp.DataType.Type.TIMESTAMPNTZ: "TIMESTAMP_NTZ",
}
Expand Down
4 changes: 3 additions & 1 deletion sqlglot/dialects/tsql.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ class Tokenizer(tokens.Tokenizer):
"TOP": TokenType.TOP,
"TIMESTAMP": TokenType.ROWVERSION,
"TINYINT": TokenType.UTINYINT,
"UNIQUEIDENTIFIER": TokenType.UNIQUEIDENTIFIER,
"UNIQUEIDENTIFIER": TokenType.UUID,
"UPDATE STATISTICS": TokenType.COMMAND,
"XML": TokenType.XML,
}
Expand Down Expand Up @@ -919,6 +919,7 @@ class Generator(generator.Generator):
exp.DataType.Type.SMALLDATETIME: "SMALLDATETIME",
exp.DataType.Type.UTINYINT: "TINYINT",
exp.DataType.Type.VARIANT: "SQL_VARIANT",
exp.DataType.Type.UUID: "UNIQUEIDENTIFIER",
}

TYPE_MAPPING.pop(exp.DataType.Type.NCHAR)
Expand Down Expand Up @@ -974,6 +975,7 @@ class Generator(generator.Generator):
exp.TsOrDsAdd: date_delta_sql("DATEADD", cast=True),
exp.TsOrDsDiff: date_delta_sql("DATEDIFF"),
exp.TimestampTrunc: lambda self, e: self.func("DATETRUNC", e.unit, e.this),
exp.Uuid: lambda *_: "NEWID()",
exp.DateFromParts: rename_func("DATEFROMPARTS"),
}

Expand Down
3 changes: 1 addition & 2 deletions sqlglot/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4476,7 +4476,6 @@ class Type(AutoName):
UMEDIUMINT = auto()
UDECIMAL = auto()
UNION = auto()
UNIQUEIDENTIFIER = auto()
UNKNOWN = auto() # Sentinel value, useful for type annotation
USERDEFINED = "USER-DEFINED"
USMALLINT = auto()
Expand Down Expand Up @@ -6749,7 +6748,7 @@ class UnixSeconds(Func):


class Uuid(Func):
_sql_names = ["UUID", "GEN_RANDOM_UUID", "GENERATE_UUID", "UUID_STRING"]
_sql_names = ["UUID", "GEN_RANDOM_UUID", "GENERATE_UUID", "NEWID", "UUID_STRING"]

arg_types = {"this": False, "name": False}

Expand Down
1 change: 0 additions & 1 deletion sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,6 @@ class Parser(metaclass=_Parser):
TokenType.BIGSERIAL,
TokenType.XML,
TokenType.YEAR,
TokenType.UNIQUEIDENTIFIER,
TokenType.USERDEFINED,
TokenType.MONEY,
TokenType.SMALLMONEY,
Expand Down
1 change: 0 additions & 1 deletion sqlglot/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ class TokenType(AutoName):
BIGSERIAL = auto()
XML = auto()
YEAR = auto()
UNIQUEIDENTIFIER = auto()
USERDEFINED = auto()
MONEY = auto()
SMALLMONEY = auto()
Expand Down
2 changes: 2 additions & 0 deletions tests/dialects/test_dialect.py
Original file line number Diff line number Diff line change
Expand Up @@ -3184,6 +3184,7 @@ def test_uuid(self):
"postgres": "GEN_RANDOM_UUID()",
"bigquery": "GENERATE_UUID()",
"snowflake": "UUID_STRING()",
"tsql": "NEWID()",
},
write={
"hive": "UUID()",
Expand All @@ -3197,6 +3198,7 @@ def test_uuid(self):
"postgres": "GEN_RANDOM_UUID()",
"bigquery": "GENERATE_UUID()",
"snowflake": "UUID_STRING()",
"tsql": "NEWID()",
},
)

Expand Down
3 changes: 2 additions & 1 deletion tests/dialects/test_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -1088,7 +1088,8 @@ def test_ddl(self):
"duckdb": "CREATE TABLE x (a UUID, b BLOB)",
"presto": "CREATE TABLE x (a UUID, b VARBINARY)",
"hive": "CREATE TABLE x (a UUID, b BINARY)",
"spark": "CREATE TABLE x (a UUID, b BINARY)",
"spark": "CREATE TABLE x (a STRING, b BINARY)",
"tsql": "CREATE TABLE x (a UNIQUEIDENTIFIER, b VARBINARY)",
},
)

Expand Down
9 changes: 9 additions & 0 deletions tests/dialects/test_tsql.py
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,15 @@ def test_ddl(self):
"databricks": "CREATE TABLE tbl (id BIGINT NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 10 INCREMENT BY 1) PRIMARY KEY)",
},
)
self.validate_all(
"CREATE TABLE x (a UNIQUEIDENTIFIER, b VARBINARY)",
write={
"duckdb": "CREATE TABLE x (a UUID, b BLOB)",
"presto": "CREATE TABLE x (a UUID, b VARBINARY)",
"spark": "CREATE TABLE x (a STRING, b BINARY)",
"postgres": "CREATE TABLE x (a UUID, b BYTEA)",
},
)
self.validate_all(
"SELECT * INTO foo.bar.baz FROM (SELECT * FROM a.b.c) AS temp",
read={
Expand Down