Skip to content

Commit a3c1c07

Browse files
cpcloudgforsyth
authored andcommitted
fix(postgres): render dates, times, timestamps and none literals correctly
1 parent 5d8866a commit a3c1c07

File tree

67 files changed

+51
-55
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+51
-55
lines changed

ibis/backends/postgres/registry.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,10 @@ def _literal(t, op):
396396
dtype = op.dtype
397397
value = op.value
398398

399+
if value is None:
400+
return (
401+
sa.null() if dtype.is_null() else sa.cast(sa.null(), t.get_sqla_type(dtype))
402+
)
399403
if dtype.is_interval():
400404
return sa.literal_column(f"INTERVAL '{value} {dtype.resolution}'")
401405
elif dtype.is_geospatial():
@@ -405,6 +409,16 @@ def _literal(t, op):
405409
return pg.array(value)
406410
elif dtype.is_map():
407411
return pg.hstore(list(value.keys()), list(value.values()))
412+
elif dtype.is_time():
413+
return sa.func.make_time(
414+
value.hour, value.minute, value.second + value.microsecond / 1e6
415+
)
416+
elif dtype.is_date():
417+
return sa.func.make_date(value.year, value.month, value.day)
418+
elif dtype.is_timestamp():
419+
if (tz := dtype.timezone) is not None:
420+
return sa.func.to_timestamp(value.timestamp()).op("AT TIME ZONE")(tz)
421+
return sa.cast(sa.literal(value.isoformat()), sa.TIMESTAMP())
408422
else:
409423
return sa.literal(value)
410424

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
SELECT
2+
CAST('2023-04-07T04:05:06.230136' AS TIMESTAMP) AS "datetime.datetime(2023, 4, 7, 4, 5, 6, 230136)"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SELECT
2+
TO_DATE('2023-04-07', 'FXYYYY-MM-DD') AS "datetime.date(2023, 4, 7)"
3+
FROM DUAL
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SELECT
2+
TO_TIMESTAMP('2023-04-07,04:05:06.230136', 'FXYYYY-MM-DD,HH24:MI:SS.FF6') AS "datetime.datetime(2023, 4, 7, 4, 5, 6, 230136)"
3+
FROM DUAL
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
SELECT
2+
MAKE_DATE(2023, 4, 7) AS "datetime.date(2023, 4, 7)"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
SELECT
2+
CAST('2023-04-07T04:05:06.230136' AS TIMESTAMP) AS "datetime.datetime(2023, 4, 7, 4, 5, 6, 230136)"

ibis/backends/tests/snapshots/test_temporal/test_temporal_literals/bigquery-time/out.sql

Lines changed: 0 additions & 2 deletions
This file was deleted.

ibis/backends/tests/snapshots/test_temporal/test_temporal_literals/duckdb-time/out.sql

Lines changed: 0 additions & 2 deletions
This file was deleted.

ibis/backends/tests/snapshots/test_temporal/test_temporal_literals/duckdb-timestamp/out.sql

Lines changed: 0 additions & 2 deletions
This file was deleted.

ibis/backends/tests/snapshots/test_temporal/test_temporal_literals/mysql-time/out.sql

Lines changed: 0 additions & 2 deletions
This file was deleted.

ibis/backends/tests/snapshots/test_temporal/test_temporal_literals/oracle-date/out.sql

Lines changed: 0 additions & 3 deletions
This file was deleted.

ibis/backends/tests/snapshots/test_temporal/test_temporal_literals/oracle-timestamp/out.sql

Lines changed: 0 additions & 3 deletions
This file was deleted.

ibis/backends/tests/snapshots/test_temporal/test_temporal_literals/snowflake-time/out.sql

Lines changed: 0 additions & 2 deletions
This file was deleted.

ibis/backends/tests/snapshots/test_temporal/test_temporal_literals/sqlite-time/out.sql

Lines changed: 0 additions & 2 deletions
This file was deleted.

ibis/backends/tests/snapshots/test_temporal/test_temporal_literals/trino-time/out.sql

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
SELECT
2+
MAKE_TIME(4, 5, 6.0) AS "datetime.time(4, 5, 6)"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
SELECT
2+
MAKE_TIME(4, 5, 6.234567) AS "datetime.time(4, 5, 6, 234567)"

ibis/backends/tests/snapshots/test_temporal/test_time_literals/bigquery/out.sql

Lines changed: 0 additions & 2 deletions
This file was deleted.

ibis/backends/tests/snapshots/test_temporal/test_time_literals/duckdb/out.sql

Lines changed: 0 additions & 2 deletions
This file was deleted.

ibis/backends/tests/snapshots/test_temporal/test_time_literals/mysql/out.sql

Lines changed: 0 additions & 2 deletions
This file was deleted.

ibis/backends/tests/snapshots/test_temporal/test_time_literals/snowflake/out.sql

Lines changed: 0 additions & 2 deletions
This file was deleted.

ibis/backends/tests/snapshots/test_temporal/test_time_literals/sqlite/out.sql

Lines changed: 0 additions & 2 deletions
This file was deleted.

ibis/backends/tests/snapshots/test_temporal/test_time_literals/trino/out.sql

Lines changed: 0 additions & 2 deletions
This file was deleted.

ibis/backends/tests/test_temporal.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2958,7 +2958,7 @@ def test_timestamp_bucket_offset(backend, offset_mins):
29582958
*no_sqlglot_dialect,
29592959
],
29602960
)
2961-
def test_temporal_literals(value, dialect, snapshot):
2961+
def test_temporal_literal_sql(value, dialect, snapshot):
29622962
expr = ibis.literal(value)
29632963
sql = ibis.to_sql(expr, dialect=dialect)
29642964
snapshot.assert_match(sql, "out.sql")
@@ -2995,7 +2995,7 @@ def test_temporal_literals(value, dialect, snapshot):
29952995
],
29962996
)
29972997
@pytest.mark.parametrize("micros", [0, 234567])
2998-
def test_time_literals(dialect, snapshot, micros):
2998+
def test_time_literal_sql(dialect, snapshot, micros):
29992999
value = datetime.time(4, 5, 6, microsecond=micros)
30003000
expr = ibis.literal(value)
30013001
sql = ibis.to_sql(expr, dialect=dialect)

ibis/backends/tests/tpch/snapshots/test_h01/test_tpc_h01/duckdb/h01.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ FROM (
3131
COUNT(*) AS count_order
3232
FROM main.lineitem AS t1
3333
WHERE
34-
t1.l_shipdate <= CAST('1998-09-02' AS DATE)
34+
t1.l_shipdate <= MAKE_DATE(1998, 9, 2)
3535
GROUP BY
3636
1,
3737
2

ibis/backends/tests/tpch/snapshots/test_h03/test_tpc_h03/duckdb/h03.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ WITH t0 AS (
1313
ON t4.l_orderkey = t3.o_orderkey
1414
WHERE
1515
t2.c_mktsegment = 'BUILDING'
16-
AND t3.o_orderdate < CAST('1995-03-15' AS DATE)
17-
AND t4.l_shipdate > CAST('1995-03-15' AS DATE)
16+
AND t3.o_orderdate < MAKE_DATE(1995, 3, 15)
17+
AND t4.l_shipdate > MAKE_DATE(1995, 3, 15)
1818
GROUP BY
1919
1,
2020
2,

ibis/backends/tests/tpch/snapshots/test_h04/test_tpc_h04/duckdb/h04.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ WHERE
1212
t1.l_orderkey = t0.o_orderkey AND t1.l_commitdate < t1.l_receiptdate
1313
)
1414
)
15-
AND t0.o_orderdate >= CAST('1993-07-01' AS DATE)
16-
AND t0.o_orderdate < CAST('1993-10-01' AS DATE)
15+
AND t0.o_orderdate >= MAKE_DATE(1993, 7, 1)
16+
AND t0.o_orderdate < MAKE_DATE(1993, 10, 1)
1717
GROUP BY
1818
1
1919
ORDER BY

ibis/backends/tests/tpch/snapshots/test_h05/test_tpc_h05/duckdb/h05.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ FROM (
2020
ON t5.n_regionkey = t6.r_regionkey
2121
WHERE
2222
t6.r_name = 'ASIA'
23-
AND t2.o_orderdate >= CAST('1994-01-01' AS DATE)
24-
AND t2.o_orderdate < CAST('1995-01-01' AS DATE)
23+
AND t2.o_orderdate >= MAKE_DATE(1994, 1, 1)
24+
AND t2.o_orderdate < MAKE_DATE(1995, 1, 1)
2525
GROUP BY
2626
1
2727
) AS t0

ibis/backends/tests/tpch/snapshots/test_h06/test_tpc_h06/duckdb/h06.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ SELECT
22
SUM(t0.l_extendedprice * t0.l_discount) AS revenue
33
FROM main.lineitem AS t0
44
WHERE
5-
t0.l_shipdate >= CAST('1994-01-01' AS DATE)
6-
AND t0.l_shipdate < CAST('1995-01-01' AS DATE)
5+
t0.l_shipdate >= MAKE_DATE(1994, 1, 1)
6+
AND t0.l_shipdate < MAKE_DATE(1995, 1, 1)
77
AND t0.l_discount BETWEEN CAST(0.05 AS REAL(53)) AND CAST(0.07 AS REAL(53))
88
AND t0.l_quantity < CAST(24 AS TINYINT)

ibis/backends/tests/tpch/snapshots/test_h07/test_tpc_h07/duckdb/h07.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ FROM (
3939
OR t0.cust_nation = 'GERMANY'
4040
AND t0.supp_nation = 'FRANCE'
4141
)
42-
AND t0.l_shipdate BETWEEN CAST('1995-01-01' AS DATE) AND CAST('1996-12-31' AS DATE)
42+
AND t0.l_shipdate BETWEEN MAKE_DATE(1995, 1, 1) AND MAKE_DATE(1996, 12, 31)
4343
GROUP BY
4444
1,
4545
2,

ibis/backends/tests/tpch/snapshots/test_h08/test_tpc_h08/duckdb/h08.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ WITH t0 AS (
3434
FROM t0
3535
WHERE
3636
t0.r_name = 'AMERICA'
37-
AND t0.o_orderdate BETWEEN CAST('1995-01-01' AS DATE) AND CAST('1996-12-31' AS DATE)
37+
AND t0.o_orderdate BETWEEN MAKE_DATE(1995, 1, 1) AND MAKE_DATE(1996, 12, 31)
3838
AND t0.p_type = 'ECONOMY ANODIZED STEEL'
3939
), t2 AS (
4040
SELECT

ibis/backends/tests/tpch/snapshots/test_h10/test_tpc_h10/duckdb/h10.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ WITH t0 AS (
1818
JOIN main.nation AS t5
1919
ON t2.c_nationkey = t5.n_nationkey
2020
WHERE
21-
t3.o_orderdate >= CAST('1993-10-01' AS DATE)
22-
AND t3.o_orderdate < CAST('1994-01-01' AS DATE)
21+
t3.o_orderdate >= MAKE_DATE(1993, 10, 1)
22+
AND t3.o_orderdate < MAKE_DATE(1994, 1, 1)
2323
AND t4.l_returnflag = 'R'
2424
GROUP BY
2525
1,

ibis/backends/tests/tpch/snapshots/test_h12/test_tpc_h12/duckdb/h12.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ FROM (
3030
t2.l_shipmode IN ('MAIL', 'SHIP')
3131
AND t2.l_commitdate < t2.l_receiptdate
3232
AND t2.l_shipdate < t2.l_commitdate
33-
AND t2.l_receiptdate >= CAST('1994-01-01' AS DATE)
34-
AND t2.l_receiptdate < CAST('1995-01-01' AS DATE)
33+
AND t2.l_receiptdate >= MAKE_DATE(1994, 1, 1)
34+
AND t2.l_receiptdate < MAKE_DATE(1995, 1, 1)
3535
GROUP BY
3636
1
3737
) AS t0

ibis/backends/tests/tpch/snapshots/test_h14/test_tpc_h14/duckdb/h14.sql

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,4 @@ FROM main.lineitem AS t0
1818
JOIN main.part AS t1
1919
ON t0.l_partkey = t1.p_partkey
2020
WHERE
21-
t0.l_shipdate >= CAST('1995-09-01' AS DATE)
22-
AND t0.l_shipdate < CAST('1995-10-01' AS DATE)
21+
t0.l_shipdate >= MAKE_DATE(1995, 9, 1) AND t0.l_shipdate < MAKE_DATE(1995, 10, 1)

ibis/backends/tests/tpch/snapshots/test_h15/test_tpc_h15/duckdb/h15.sql

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ WITH t0 AS (
66
)) AS total_revenue
77
FROM main.lineitem AS t3
88
WHERE
9-
t3.l_shipdate >= CAST('1996-01-01' AS DATE)
10-
AND t3.l_shipdate < CAST('1996-04-01' AS DATE)
9+
t3.l_shipdate >= MAKE_DATE(1996, 1, 1) AND t3.l_shipdate < MAKE_DATE(1996, 4, 1)
1110
GROUP BY
1211
1
1312
), t1 AS (

ibis/backends/tests/tpch/snapshots/test_h20/test_tpc_h20/duckdb/h20.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ WITH t0 AS (
5454
WHERE
5555
t6.l_partkey = t5.ps_partkey
5656
AND t6.l_suppkey = t5.ps_suppkey
57-
AND t6.l_shipdate >= CAST('1994-01-01' AS DATE)
58-
AND t6.l_shipdate < CAST('1995-01-01' AS DATE)
57+
AND t6.l_shipdate >= MAKE_DATE(1994, 1, 1)
58+
AND t6.l_shipdate < MAKE_DATE(1995, 1, 1)
5959
) * CAST(0.5 AS REAL(53))
6060
) AS t4
6161
)

0 commit comments

Comments
 (0)