Skip to content

Commit

Permalink
feat(snowflake): Transpile BQ's TIMESTAMP() function
Browse files Browse the repository at this point in the history
  • Loading branch information
VaggelisD committed Oct 29, 2024
1 parent efd9b4e commit 25f7df8
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 10 deletions.
5 changes: 2 additions & 3 deletions sqlglot/dialects/dialect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1210,9 +1210,8 @@ def no_timestamp_sql(self: Generator, expression: exp.Timestamp) -> str:
return self.sql(exp.cast(expression.this, target_type))
if zone.name.lower() in TIMEZONES:
return self.sql(
exp.AtTimeZone(
this=exp.cast(expression.this, exp.DataType.Type.TIMESTAMP),
zone=zone,
exp.ConvertTimezone(
source_tz=zone, target_tz=exp.Literal.string("UTC"), timestamp=expression.this
)
)
return self.func("TIMESTAMP", expression.this, zone)
Expand Down
1 change: 1 addition & 0 deletions sqlglot/dialects/presto.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ class Generator(generator.Generator):
exp.DataType.Type.STRUCT: "ROW",
exp.DataType.Type.TEXT: "VARCHAR",
exp.DataType.Type.TIMESTAMPTZ: "TIMESTAMP",
exp.DataType.Type.TIMESTAMPNTZ: "TIMESTAMP",
exp.DataType.Type.TIMETZ: "TIME",
}

Expand Down
2 changes: 2 additions & 0 deletions sqlglot/dialects/snowflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
var_map_sql,
map_date_part,
no_safe_divide_sql,
no_timestamp_sql,
)
from sqlglot.helper import flatten, is_float, is_int, seq_get
from sqlglot.tokens import TokenType
Expand Down Expand Up @@ -840,6 +841,7 @@ class Generator(generator.Generator):
),
exp.Stuff: rename_func("INSERT"),
exp.TimeAdd: date_delta_sql("TIMEADD"),
exp.Timestamp: no_timestamp_sql,
exp.TimestampDiff: lambda self, e: self.func(
"TIMESTAMPDIFF", e.unit, e.expression, e.this
),
Expand Down
9 changes: 9 additions & 0 deletions tests/dialects/test_bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,9 +371,18 @@ def test_bigquery(self):
write={
"bigquery": "TIMESTAMP(x)",
"duckdb": "CAST(x AS TIMESTAMPTZ)",
"snowflake": "CAST(x AS TIMESTAMPTZ)",
"presto": "CAST(x AS TIMESTAMP WITH TIME ZONE)",
},
)
self.validate_all(
"SELECT TIMESTAMP('2008-12-25 15:30:00', 'America/Los_Angeles')",
write={
"bigquery": "SELECT TIMESTAMP('2008-12-25 15:30:00', 'America/Los_Angeles')",
"duckdb": "SELECT CAST('2008-12-25 15:30:00' AS TIMESTAMP) AT TIME ZONE 'America/Los_Angeles' AT TIME ZONE 'UTC'",
"snowflake": "SELECT CONVERT_TIMEZONE('America/Los_Angeles', 'UTC', '2008-12-25 15:30:00')",
},
)
self.validate_all(
"SELECT SUM(x IGNORE NULLS) AS x",
read={
Expand Down
7 changes: 0 additions & 7 deletions tests/dialects/test_presto.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,13 +414,6 @@ def test_time(self):
"CAST(x AS TIMESTAMP)",
read={"mysql": "TIMESTAMP(x)"},
)
self.validate_all(
"TIMESTAMP(x, 'America/Los_Angeles')",
write={
"duckdb": "CAST(x AS TIMESTAMP) AT TIME ZONE 'America/Los_Angeles'",
"presto": "AT_TIMEZONE(CAST(x AS TIMESTAMP), 'America/Los_Angeles')",
},
)
# this case isn't really correct, but it's a fall back for mysql's version
self.validate_all(
"TIMESTAMP(x, '12:00:00')",
Expand Down

0 comments on commit 25f7df8

Please sign in to comment.