Skip to content

Commit b12aba9

Browse files
authored
fix(tsql, postgres)!: Improve UUID support (#4718)
* fix(tsql, postgres): Improve UUID support * Move NEWID reference into T-SQL specific dialect
1 parent da52181 commit b12aba9

File tree

8 files changed

+18
-6
lines changed

8 files changed

+18
-6
lines changed

sqlglot/dialects/spark.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ class Generator(Spark2.Generator):
144144
**Spark2.Generator.TYPE_MAPPING,
145145
exp.DataType.Type.MONEY: "DECIMAL(15, 4)",
146146
exp.DataType.Type.SMALLMONEY: "DECIMAL(6, 4)",
147-
exp.DataType.Type.UNIQUEIDENTIFIER: "STRING",
147+
exp.DataType.Type.UUID: "STRING",
148148
exp.DataType.Type.TIMESTAMPLTZ: "TIMESTAMP_LTZ",
149149
exp.DataType.Type.TIMESTAMPNTZ: "TIMESTAMP_NTZ",
150150
}

sqlglot/dialects/tsql.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ class Tokenizer(tokens.Tokenizer):
525525
"TOP": TokenType.TOP,
526526
"TIMESTAMP": TokenType.ROWVERSION,
527527
"TINYINT": TokenType.UTINYINT,
528-
"UNIQUEIDENTIFIER": TokenType.UNIQUEIDENTIFIER,
528+
"UNIQUEIDENTIFIER": TokenType.UUID,
529529
"UPDATE STATISTICS": TokenType.COMMAND,
530530
"XML": TokenType.XML,
531531
}
@@ -579,6 +579,7 @@ class Parser(parser.Parser):
579579
"JSON_VALUE": parser.build_extract_json_with_path(exp.JSONExtractScalar),
580580
"LEN": _build_with_arg_as_text(exp.Length),
581581
"LEFT": _build_with_arg_as_text(exp.Left),
582+
"NEWID": exp.Uuid.from_arg_list,
582583
"RIGHT": _build_with_arg_as_text(exp.Right),
583584
"PARSENAME": _build_parsename,
584585
"REPLICATE": exp.Repeat.from_arg_list,
@@ -919,6 +920,7 @@ class Generator(generator.Generator):
919920
exp.DataType.Type.SMALLDATETIME: "SMALLDATETIME",
920921
exp.DataType.Type.UTINYINT: "TINYINT",
921922
exp.DataType.Type.VARIANT: "SQL_VARIANT",
923+
exp.DataType.Type.UUID: "UNIQUEIDENTIFIER",
922924
}
923925

924926
TYPE_MAPPING.pop(exp.DataType.Type.NCHAR)
@@ -974,6 +976,7 @@ class Generator(generator.Generator):
974976
exp.TsOrDsAdd: date_delta_sql("DATEADD", cast=True),
975977
exp.TsOrDsDiff: date_delta_sql("DATEDIFF"),
976978
exp.TimestampTrunc: lambda self, e: self.func("DATETRUNC", e.unit, e.this),
979+
exp.Uuid: lambda *_: "NEWID()",
977980
exp.DateFromParts: rename_func("DATEFROMPARTS"),
978981
}
979982

sqlglot/expressions.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4476,7 +4476,6 @@ class Type(AutoName):
44764476
UMEDIUMINT = auto()
44774477
UDECIMAL = auto()
44784478
UNION = auto()
4479-
UNIQUEIDENTIFIER = auto()
44804479
UNKNOWN = auto() # Sentinel value, useful for type annotation
44814480
USERDEFINED = "USER-DEFINED"
44824481
USMALLINT = auto()

sqlglot/parser.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,6 @@ class Parser(metaclass=_Parser):
387387
TokenType.BIGSERIAL,
388388
TokenType.XML,
389389
TokenType.YEAR,
390-
TokenType.UNIQUEIDENTIFIER,
391390
TokenType.USERDEFINED,
392391
TokenType.MONEY,
393392
TokenType.SMALLMONEY,

sqlglot/tokens.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,6 @@ class TokenType(AutoName):
192192
BIGSERIAL = auto()
193193
XML = auto()
194194
YEAR = auto()
195-
UNIQUEIDENTIFIER = auto()
196195
USERDEFINED = auto()
197196
MONEY = auto()
198197
SMALLMONEY = auto()

tests/dialects/test_dialect.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3184,6 +3184,7 @@ def test_uuid(self):
31843184
"postgres": "GEN_RANDOM_UUID()",
31853185
"bigquery": "GENERATE_UUID()",
31863186
"snowflake": "UUID_STRING()",
3187+
"tsql": "NEWID()",
31873188
},
31883189
write={
31893190
"hive": "UUID()",
@@ -3197,6 +3198,7 @@ def test_uuid(self):
31973198
"postgres": "GEN_RANDOM_UUID()",
31983199
"bigquery": "GENERATE_UUID()",
31993200
"snowflake": "UUID_STRING()",
3201+
"tsql": "NEWID()",
32003202
},
32013203
)
32023204

tests/dialects/test_postgres.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1088,7 +1088,8 @@ def test_ddl(self):
10881088
"duckdb": "CREATE TABLE x (a UUID, b BLOB)",
10891089
"presto": "CREATE TABLE x (a UUID, b VARBINARY)",
10901090
"hive": "CREATE TABLE x (a UUID, b BINARY)",
1091-
"spark": "CREATE TABLE x (a UUID, b BINARY)",
1091+
"spark": "CREATE TABLE x (a STRING, b BINARY)",
1092+
"tsql": "CREATE TABLE x (a UNIQUEIDENTIFIER, b VARBINARY)",
10921093
},
10931094
)
10941095

tests/dialects/test_tsql.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,15 @@ def test_ddl(self):
928928
"databricks": "CREATE TABLE tbl (id BIGINT NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 10 INCREMENT BY 1) PRIMARY KEY)",
929929
},
930930
)
931+
self.validate_all(
932+
"CREATE TABLE x (a UNIQUEIDENTIFIER, b VARBINARY)",
933+
write={
934+
"duckdb": "CREATE TABLE x (a UUID, b BLOB)",
935+
"presto": "CREATE TABLE x (a UUID, b VARBINARY)",
936+
"spark": "CREATE TABLE x (a STRING, b BINARY)",
937+
"postgres": "CREATE TABLE x (a UUID, b BYTEA)",
938+
},
939+
)
931940
self.validate_all(
932941
"SELECT * INTO foo.bar.baz FROM (SELECT * FROM a.b.c) AS temp",
933942
read={

0 commit comments

Comments
 (0)