Skip to content

Commit

Permalink
feat: Add support for UUID function
Browse files Browse the repository at this point in the history
  • Loading branch information
VaggelisD committed Sep 9, 2024
1 parent c7c6b47 commit a7b610c
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions sqlglot/dialects/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,7 @@ class Generator(generator.Generator):
exp.Unhex: rename_func("FROM_HEX"),
exp.UnixDate: rename_func("UNIX_DATE"),
exp.UnixToTime: _unix_to_time_sql,
exp.Uuid: lambda *_: "GENERATE_UUID()",
exp.Values: _derived_table_values_to_unnest,
exp.VariancePop: rename_func("VAR_POP"),
}
Expand Down
1 change: 1 addition & 0 deletions sqlglot/dialects/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@ class Generator(generator.Generator):
exp.TsOrDsAdd: _date_add_sql("+"),
exp.TsOrDsDiff: _date_diff_sql,
exp.UnixToTime: lambda self, e: self.func("TO_TIMESTAMP", e.this),
exp.Uuid: lambda *_: "GEN_RANDOM_UUID()",
exp.TimeToUnix: lambda self, e: self.func(
"DATE_PART", exp.Literal.string("epoch"), e.this
),
Expand Down
1 change: 1 addition & 0 deletions sqlglot/dialects/snowflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,7 @@ class Generator(generator.Generator):
"TRY_TO_DATE" if e.args.get("safe") else "TO_DATE", e.this, self.format_time(e)
),
exp.UnixToTime: rename_func("TO_TIMESTAMP"),
exp.Uuid: rename_func("UUID_STRING"),
exp.VarMap: lambda self, e: var_map_sql(self, e, "OBJECT_CONSTRUCT"),
exp.WeekOfYear: rename_func("WEEKOFYEAR"),
exp.Xor: rename_func("BOOLXOR"),
Expand Down
6 changes: 6 additions & 0 deletions sqlglot/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6217,6 +6217,12 @@ class UnixToTimeStr(Func):
pass


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

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


class TimestampFromParts(Func):
_sql_names = ["TIMESTAMP_FROM_PARTS", "TIMESTAMPFROMPARTS"]
arg_types = {
Expand Down
1 change: 1 addition & 0 deletions sqlglot/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ class Generator(metaclass=_Generator):
exp.TransientProperty: lambda *_: "TRANSIENT",
exp.Union: lambda self, e: self.set_operations(e),
exp.UnloggedProperty: lambda *_: "UNLOGGED",
exp.Uuid: lambda *_: "UUID()",
exp.UppercaseColumnConstraint: lambda *_: "UPPERCASE",
exp.VarMap: lambda self, e: self.func("MAP", e.args["keys"], e.args["values"]),
exp.ViewAttributeProperty: lambda self, e: f"WITH {self.sql(e, 'this')}",
Expand Down
29 changes: 29 additions & 0 deletions tests/dialects/test_dialect.py
Original file line number Diff line number Diff line change
Expand Up @@ -2853,3 +2853,32 @@ def test_trim(self):
"bigquery": "RTRIM('Hello World', 'd')",
},
)

def test_uuid(self):
self.validate_all(
"UUID()",
read={
"hive": "UUID()",
"spark2": "UUID()",
"spark": "UUID()",
"databricks": "UUID()",
"duckdb": "UUID()",
"presto": "UUID()",
"trino": "UUID()",
"postgres": "GEN_RANDOM_UUID()",
"bigquery": "GENERATE_UUID()",
"snowflake": "UUID_STRING()",
},
write={
"hive": "UUID()",
"spark2": "UUID()",
"spark": "UUID()",
"databricks": "UUID()",
"duckdb": "UUID()",
"presto": "UUID()",
"trino": "UUID()",
"postgres": "GEN_RANDOM_UUID()",
"bigquery": "GENERATE_UUID()",
"snowflake": "UUID_STRING()",
},
)
22 changes: 22 additions & 0 deletions tests/dialects/test_snowflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,28 @@ def test_snowflake(self):
},
)

self.validate_identity(
"SELECT UUID_STRING(), UUID_STRING('fe971b24-9572-4005-b22f-351e9c09274d', 'foo')"
)

self.validate_all(
"UUID_STRING('fe971b24-9572-4005-b22f-351e9c09274d', 'foo')",
read={
"snowflake": "UUID_STRING('fe971b24-9572-4005-b22f-351e9c09274d', 'foo')",
},
write={
"hive": "UUID()",
"spark2": "UUID()",
"spark": "UUID()",
"databricks": "UUID()",
"duckdb": "UUID()",
"presto": "UUID()",
"trino": "UUID()",
"postgres": "GEN_RANDOM_UUID()",
"bigquery": "GENERATE_UUID()",
},
)

def test_null_treatment(self):
self.validate_all(
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",
Expand Down

0 comments on commit a7b610c

Please sign in to comment.