Skip to content

Commit

Permalink
fix: No module named 'pandas.core.arrays.arrow.dtype'
Browse files Browse the repository at this point in the history
introduced by pandas 2.1 and fixed in duckdb 0.9.0

see duckdb/duckdb#8735
  • Loading branch information
tekumara committed Dec 27, 2023
1 parent bf6f7e1 commit 6ed1d2a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 3 deletions.
1 change: 1 addition & 0 deletions fakesnow/fakes.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ def _execute(
.transform(transforms.values_columns)
.transform(transforms.to_date)
.transform(transforms.to_decimal)
.transform(transforms.to_timestamp)
.transform(transforms.object_construct)
.transform(transforms.timestamp_ntz_ns)
.transform(transforms.float_to_double)
Expand Down
14 changes: 14 additions & 0 deletions fakesnow/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,20 @@ def to_decimal(expression: exp.Expression) -> exp.Expression:
return expression


def to_timestamp(expression: exp.Expression) -> exp.Expression:
"""Convert to_timestamp(seconds) to timestamp without timezone (ie: TIMESTAMP_NTZ).
See https://docs.snowflake.com/en/sql-reference/functions/to_timestamp
"""

if isinstance(expression, exp.UnixToTime):
return exp.Cast(
this=expression,
to=exp.DataType(this=exp.DataType.Type.TIMESTAMP, nested=False, prefix=False),
)
return expression


def timestamp_ntz_ns(expression: exp.Expression) -> exp.Expression:
"""Convert timestamp_ntz(9) to timestamp_ntz.
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ classifiers = ["License :: OSI Approved :: MIT License"]
keywords = ["snowflake", "snowflakedb", "fake", "local", "mock", "testing"]
requires-python = ">=3.9"
dependencies = [
"duckdb~=0.8.0",
"duckdb~=0.9.2",
"pyarrow",
"snowflake-connector-python",
"sqlglot~=19.5.1",
Expand Down
7 changes: 5 additions & 2 deletions tests/test_fakes.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,11 +606,14 @@ def test_tags_noop(cur: snowflake.connector.cursor.SnowflakeCursor):
cur.execute("ALTER TABLE table1 MODIFY COLUMN name1 SET TAG foo='bar'")


def test_timestamp(cur: snowflake.connector.cursor.SnowflakeCursor):
def test_to_timestamp(cur: snowflake.connector.cursor.SnowflakeCursor):
# snowflake returns naive timestamps (ie: no timezone)
cur.execute("SELECT to_timestamp(0)")
# NB: duckdb~=0.9 returns a datetime with utc timezone
assert cur.fetchall() == [(datetime.datetime(1970, 1, 1, 0, 0),)]

cur.execute("SELECT to_timestamp('2013-04-05 01:02:03')")
assert cur.fetchall() == [(datetime.datetime(2013, 4, 5, 1, 2, 3),)]


def test_timestamp_to_date(cur: snowflake.connector.cursor.SnowflakeCursor):
cur.execute("SELECT to_date(to_timestamp(0)), to_date(cast(to_timestamp(0) as timestamp(9)))")
Expand Down
8 changes: 8 additions & 0 deletions tests/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
timestamp_ntz_ns,
to_date,
to_decimal,
to_timestamp,
upper_case_unquoted_identifiers,
values_columns,
)
Expand Down Expand Up @@ -198,6 +199,13 @@ def test_to_decimal() -> None:
)


def test_to_timestamp() -> None:
assert (
sqlglot.parse_one("SELECT to_timestamp(0)", read="snowflake").transform(to_timestamp).sql(dialect="duckdb")
== "SELECT CAST(TO_TIMESTAMP(0) AS TIMESTAMP)"
)


def test_use() -> None:
assert (
sqlglot.parse_one("use database marts").transform(set_schema, current_database=None).sql()
Expand Down

0 comments on commit 6ed1d2a

Please sign in to comment.