Skip to content

Commit a7b610c

Browse files
committed
feat: Add support for UUID function
1 parent c7c6b47 commit a7b610c

File tree

7 files changed

+61
-0
lines changed

7 files changed

+61
-0
lines changed

sqlglot/dialects/bigquery.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,7 @@ class Generator(generator.Generator):
724724
exp.Unhex: rename_func("FROM_HEX"),
725725
exp.UnixDate: rename_func("UNIX_DATE"),
726726
exp.UnixToTime: _unix_to_time_sql,
727+
exp.Uuid: lambda *_: "GENERATE_UUID()",
727728
exp.Values: _derived_table_values_to_unnest,
728729
exp.VariancePop: rename_func("VAR_POP"),
729730
}

sqlglot/dialects/postgres.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,7 @@ class Generator(generator.Generator):
557557
exp.TsOrDsAdd: _date_add_sql("+"),
558558
exp.TsOrDsDiff: _date_diff_sql,
559559
exp.UnixToTime: lambda self, e: self.func("TO_TIMESTAMP", e.this),
560+
exp.Uuid: lambda *_: "GEN_RANDOM_UUID()",
560561
exp.TimeToUnix: lambda self, e: self.func(
561562
"DATE_PART", exp.Literal.string("epoch"), e.this
562563
),

sqlglot/dialects/snowflake.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,7 @@ class Generator(generator.Generator):
848848
"TRY_TO_DATE" if e.args.get("safe") else "TO_DATE", e.this, self.format_time(e)
849849
),
850850
exp.UnixToTime: rename_func("TO_TIMESTAMP"),
851+
exp.Uuid: rename_func("UUID_STRING"),
851852
exp.VarMap: lambda self, e: var_map_sql(self, e, "OBJECT_CONSTRUCT"),
852853
exp.WeekOfYear: rename_func("WEEKOFYEAR"),
853854
exp.Xor: rename_func("BOOLXOR"),

sqlglot/expressions.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6217,6 +6217,12 @@ class UnixToTimeStr(Func):
62176217
pass
62186218

62196219

6220+
class Uuid(Func):
6221+
_sql_names = ["UUID", "GEN_RANDOM_UUID", "GENERATE_UUID", "UUID_STRING"]
6222+
6223+
arg_types = {"this": False, "name": False}
6224+
6225+
62206226
class TimestampFromParts(Func):
62216227
_sql_names = ["TIMESTAMP_FROM_PARTS", "TIMESTAMPFROMPARTS"]
62226228
arg_types = {

sqlglot/generator.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ class Generator(metaclass=_Generator):
154154
exp.TransientProperty: lambda *_: "TRANSIENT",
155155
exp.Union: lambda self, e: self.set_operations(e),
156156
exp.UnloggedProperty: lambda *_: "UNLOGGED",
157+
exp.Uuid: lambda *_: "UUID()",
157158
exp.UppercaseColumnConstraint: lambda *_: "UPPERCASE",
158159
exp.VarMap: lambda self, e: self.func("MAP", e.args["keys"], e.args["values"]),
159160
exp.ViewAttributeProperty: lambda self, e: f"WITH {self.sql(e, 'this')}",

tests/dialects/test_dialect.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2853,3 +2853,32 @@ def test_trim(self):
28532853
"bigquery": "RTRIM('Hello World', 'd')",
28542854
},
28552855
)
2856+
2857+
def test_uuid(self):
2858+
self.validate_all(
2859+
"UUID()",
2860+
read={
2861+
"hive": "UUID()",
2862+
"spark2": "UUID()",
2863+
"spark": "UUID()",
2864+
"databricks": "UUID()",
2865+
"duckdb": "UUID()",
2866+
"presto": "UUID()",
2867+
"trino": "UUID()",
2868+
"postgres": "GEN_RANDOM_UUID()",
2869+
"bigquery": "GENERATE_UUID()",
2870+
"snowflake": "UUID_STRING()",
2871+
},
2872+
write={
2873+
"hive": "UUID()",
2874+
"spark2": "UUID()",
2875+
"spark": "UUID()",
2876+
"databricks": "UUID()",
2877+
"duckdb": "UUID()",
2878+
"presto": "UUID()",
2879+
"trino": "UUID()",
2880+
"postgres": "GEN_RANDOM_UUID()",
2881+
"bigquery": "GENERATE_UUID()",
2882+
"snowflake": "UUID_STRING()",
2883+
},
2884+
)

tests/dialects/test_snowflake.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,28 @@ def test_snowflake(self):
904904
},
905905
)
906906

907+
self.validate_identity(
908+
"SELECT UUID_STRING(), UUID_STRING('fe971b24-9572-4005-b22f-351e9c09274d', 'foo')"
909+
)
910+
911+
self.validate_all(
912+
"UUID_STRING('fe971b24-9572-4005-b22f-351e9c09274d', 'foo')",
913+
read={
914+
"snowflake": "UUID_STRING('fe971b24-9572-4005-b22f-351e9c09274d', 'foo')",
915+
},
916+
write={
917+
"hive": "UUID()",
918+
"spark2": "UUID()",
919+
"spark": "UUID()",
920+
"databricks": "UUID()",
921+
"duckdb": "UUID()",
922+
"presto": "UUID()",
923+
"trino": "UUID()",
924+
"postgres": "GEN_RANDOM_UUID()",
925+
"bigquery": "GENERATE_UUID()",
926+
},
927+
)
928+
907929
def test_null_treatment(self):
908930
self.validate_all(
909931
r"SELECT FIRST_VALUE(TABLE1.COLUMN1) OVER (PARTITION BY RANDOM_COLUMN1, RANDOM_COLUMN2 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS MY_ALIAS FROM TABLE1",

0 commit comments

Comments
 (0)