Skip to content

Commit

Permalink
fix timestamp after sqlglot change
Browse files Browse the repository at this point in the history
  • Loading branch information
eakmanrq committed Dec 7, 2024
1 parent 52e3f25 commit 96d72be
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 9 deletions.
22 changes: 22 additions & 0 deletions sqlframe/base/function_alternatives.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,28 @@ def first_always_ignore_nulls(col: ColumnOrName, ignorenulls: t.Optional[bool] =
return first(col)


def to_timestamp_without_time_zone(col: ColumnOrName, format: t.Optional[str] = None) -> Column:
from sqlframe.base.session import _BaseSession

if format is not None:
return Column.invoke_expression_over_column(
col, expression.StrToTime, format=_BaseSession().format_time(format)
)

return Column.ensure_col(col).cast("timestamp without time zone", dialect="postgres")


def to_timestamp_just_timestamp(col: ColumnOrName, format: t.Optional[str] = None) -> Column:
from sqlframe.base.session import _BaseSession

if format is not None:
return Column.invoke_expression_over_column(
col, expression.StrToTime, format=_BaseSession().format_time(format)
)

return Column.ensure_col(col).cast("datetime", dialect="bigquery")


def bitwise_not_from_bitnot(col: ColumnOrName) -> Column:
return Column.invoke_anonymous_function(col, "BITNOT")

Expand Down
2 changes: 1 addition & 1 deletion sqlframe/base/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,7 @@ def to_timestamp(col: ColumnOrName, format: t.Optional[str] = None) -> Column:
col, expression.StrToTime, format=_BaseSession().format_time(format)
)

return Column.ensure_col(col).cast("timestamp")
return Column.ensure_col(col).cast("timestampntz")


@meta()
Expand Down
1 change: 1 addition & 0 deletions sqlframe/bigquery/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
_is_string_using_typeof_string as _is_string,
array_append_using_array_cat as array_append,
endswith_with_underscore as endswith,
to_timestamp_just_timestamp as to_timestamp,
)


Expand Down
1 change: 1 addition & 0 deletions sqlframe/postgres/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,5 @@
endswith_using_like as endswith,
last_day_with_cast as last_day,
regexp_replace_global_option as regexp_replace,
to_timestamp_without_time_zone as to_timestamp,
)
9 changes: 1 addition & 8 deletions tests/integration/engines/test_int_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def test_col(get_session_and_func, input, output):
([1, 2, 3], "array<bigint>"),
(Row(a=1), "struct<a:bigint>"),
(datetime.date(2022, 1, 1), "date"),
(datetime.datetime(2022, 1, 1, 0, 0, 0), "timestamp"),
(datetime.datetime(2022, 1, 1, 0, 0, 0), "timestamptz"),
(datetime.datetime(2022, 1, 1, 0, 0, 0, tzinfo=datetime.timezone.utc), "timestamptz"),
(True, "boolean"),
(bytes("test", "utf-8"), "binary"),
Expand All @@ -193,9 +193,6 @@ def test_typeof(get_session_and_func, get_types, arg, expected):
if isinstance(session, PySparkSession)
else dialect_to_string(session.execution_dialect)
)
if isinstance(session, (SparkSession, PySparkSession)):
if expected == "timestamptz":
expected = "timestamp"
if isinstance(session, DuckDBSession):
if expected == "binary":
pytest.skip("DuckDB doesn't support binary")
Expand All @@ -207,8 +204,6 @@ def test_typeof(get_session_and_func, get_types, arg, expected):
expected = expected.split("<")[0]
if expected == "binary":
pytest.skip("BigQuery doesn't support binary")
if expected == "timestamp":
expected = "datetime"
if isinstance(session, PostgresSession):
if expected.startswith("map"):
pytest.skip("Postgres doesn't support map types")
Expand All @@ -225,8 +220,6 @@ def test_typeof(get_session_and_func, get_types, arg, expected):
expected = "object"
elif expected.startswith("array"):
pytest.skip("Snowflake doesn't handle arrays properly in values clause")
elif expected == "timestamp":
expected = "timestampntz"
result = df.select(typeof("col").alias("test")).first()[0]
assert exp.DataType.build(result, dialect=dialect) == exp.DataType.build(
expected, dialect=dialect
Expand Down

0 comments on commit 96d72be

Please sign in to comment.