Skip to content

Commit

Permalink
test(backend): add temporal literal tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud authored and gforsyth committed Dec 12, 2023
1 parent 18af402 commit 0e896c6
Show file tree
Hide file tree
Showing 29 changed files with 121 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SELECT
CAST('2023-04-07' AS DATE) AS `datetime_date_2023_ 4_ 7`
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SELECT
CAST('04:05:06.230136' AS TIME) AS `datetime_time_4_ 5_ 6_ 230136`
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SELECT
CAST('2023-04-07 04:05:06.230136' AS DATETIME) AS `datetime_datetime_2023_ 4_ 7_ 4_ 5_ 6_ 230136`
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SELECT
toDate('2023-04-07') AS "datetime.date(2023, 4, 7)"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SELECT
toDateTime64('2023-04-07T04:05:06.230136', 6) AS "datetime.datetime(2023, 4, 7, 4, 5, 6, 230136)"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SELECT
'2023-04-07' AS `datetime.date(2023, 4, 7)`
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SELECT
'2023-04-07T04:05:06.230136' AS `datetime.datetime(2023, 4, 7, 4, 5, 6, 230136)`
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SELECT
DATE_FROM_PARTS(2023, 4, 7) AS "datetime.date(2023, 4, 7)"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SELECT
TIME_FROM_PARTS(4, 5, 6, 230136000) AS "datetime.time(4, 5, 6, 230136)"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SELECT
TIMESTAMP_FROM_PARTS(2023, 4, 7, 4, 5, 6, 230136000) AS "datetime.datetime(2023, 4, 7, 4, 5, 6, 230136)"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SELECT
'2023-04-07' AS "datetime.date(2023, 4, 7)"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SELECT
'04:05:06.230136' AS "datetime.time(4, 5, 6, 230136)"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SELECT
'2023-04-07 04:05:06.230136' AS "datetime.datetime(2023, 4, 7, 4, 5, 6, 230136)"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SELECT
FROM_ISO8601_DATE('2023-04-07') AS "datetime.date(2023, 4, 7)"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SELECT
CAST('04:05:06.230136' AS TIME) AS "datetime.time(4, 5, 6, 230136)"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SELECT
CAST(FROM_ISO8601_TIMESTAMP('2023-04-07T04:05:06.230136') AS TIMESTAMP) AS "datetime.datetime(2023, 4, 7, 4, 5, 6, 230136)"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SELECT
CAST('04:05:06' AS TIME) AS `datetime_time_4_ 5_ 6`
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SELECT
TIME_FROM_PARTS(4, 5, 6, 0) AS "datetime.time(4, 5, 6)"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SELECT
'04:05:06.000000' AS "datetime.time(4, 5, 6)"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SELECT
CAST('04:05:06' AS TIME) AS "datetime.time(4, 5, 6)"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SELECT
CAST('04:05:06.234567' AS TIME) AS `datetime_time_4_ 5_ 6_ 234567`
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SELECT
TIME_FROM_PARTS(4, 5, 6, 234567000) AS "datetime.time(4, 5, 6, 234567)"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SELECT
'04:05:06.234567' AS "datetime.time(4, 5, 6, 234567)"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SELECT
CAST('04:05:06.234567' AS TIME) AS "datetime.time(4, 5, 6, 234567)"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SELECT
CAST('04:05:06.230136' AS TIME) AS `datetime_time_4_ 5_ 6_ 230136`
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SELECT
MAKE_TIME(4, 5, 6.230136) AS "datetime.time(4, 5, 6, 230136)"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SELECT
TIME_FROM_PARTS(4, 5, 6, 230136000) AS "datetime.time(4, 5, 6, 230136)"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SELECT
'04:05:06.230136' AS "datetime.time(4, 5, 6, 230136)"
80 changes: 65 additions & 15 deletions ibis/backends/tests/test_temporal.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import ibis
import ibis.common.exceptions as com
import ibis.expr.datatypes as dt
from ibis.backends.base import _get_backend_names
from ibis.backends.pandas.execution.temporal import day_name
from ibis.common.annotations import ValidationError

Expand Down Expand Up @@ -176,11 +177,6 @@ def test_timestamp_extract(backend, alltypes, df, attr):
raises=sa.exc.CompileError,
reason='No literal value renderer is available for literal value "datetime.datetime(2015, 9, 1, 14, 48, 5, 359000)" with datatype DATETIME',
),
pytest.mark.notimpl(
["oracle"],
raises=sa.exc.DatabaseError,
reason="ORA-30076: invalid extract field for extract source",
),
],
),
param(
Expand All @@ -193,11 +189,6 @@ def test_timestamp_extract(backend, alltypes, df, attr):
raises=sa.exc.CompileError,
reason='No literal value renderer is available for literal value "datetime.datetime(2015, 9, 1, 14, 48, 5, 359000)" with datatype DATETIME',
),
pytest.mark.notimpl(
["oracle"],
raises=sa.exc.DatabaseError,
reason="ORA-30076: invalid extract field for extract source",
),
],
),
param(
Expand All @@ -210,11 +201,6 @@ def test_timestamp_extract(backend, alltypes, df, attr):
raises=sa.exc.CompileError,
reason='No literal value renderer is available for literal value "datetime.datetime(2015, 9, 1, 14, 48, 5, 359000)" with datatype DATETIME',
),
pytest.mark.notimpl(
["oracle"],
raises=sa.exc.DatabaseError,
reason="ORA-30076: invalid extract field for extract source",
),
],
),
param(
Expand Down Expand Up @@ -2950,3 +2936,67 @@ def test_timestamp_bucket_offset(backend, offset_mins):
td = pd.Timedelta(minutes=offset_mins)
sol = ((ts.execute() - td).dt.floor("300s") + td).astype("datetime64[ns]")
backend.assert_series_equal(res, sol)


_NO_SQLGLOT_DIALECT = {"pandas", "dask", "druid", "flink", "datafusion", "polars"}
no_sqlglot_dialect = sorted(
param(backend, marks=pytest.mark.xfail) for backend in _NO_SQLGLOT_DIALECT
)


@pytest.mark.parametrize(
"value",
[
param(datetime.date(2023, 4, 7), id="date"),
param(datetime.datetime(2023, 4, 7, 4, 5, 6, 230136), id="timestamp"),
],
)
@pytest.mark.parametrize(
"dialect",
[
*sorted(_get_backend_names() - {"pyspark", *_NO_SQLGLOT_DIALECT}),
*no_sqlglot_dialect,
],
)
def test_temporal_literals(value, dialect, snapshot):
expr = ibis.literal(value)
sql = ibis.to_sql(expr, dialect=dialect)
snapshot.assert_match(sql, "out.sql")


@pytest.mark.parametrize(
"dialect",
[
*sorted(
_get_backend_names()
- {"pyspark", "impala", "clickhouse", "oracle", *_NO_SQLGLOT_DIALECT}
),
*no_sqlglot_dialect,
*[
param(
"impala",
marks=pytest.mark.xfail(
raises=NotImplementedError, reason="no time type support"
),
),
param(
"clickhouse",
marks=pytest.mark.xfail(
raises=NotImplementedError, reason="no time type support"
),
),
param(
"oracle",
marks=pytest.mark.xfail(
raises=NotImplementedError, reason="no time type support"
),
),
],
],
)
@pytest.mark.parametrize("micros", [0, 234567])
def test_time_literals(dialect, snapshot, micros):
value = datetime.time(4, 5, 6, microsecond=micros)
expr = ibis.literal(value)
sql = ibis.to_sql(expr, dialect=dialect)
snapshot.assert_match(sql, "out.sql")

0 comments on commit 0e896c6

Please sign in to comment.