Skip to content

Commit

Permalink
fix(postgres): render dates, times, timestamps and none literals corr…
Browse files Browse the repository at this point in the history
…ectly
  • Loading branch information
cpcloud authored and gforsyth committed Dec 12, 2023
1 parent 5d8866a commit a3c1c07
Show file tree
Hide file tree
Showing 67 changed files with 51 additions and 55 deletions.
14 changes: 14 additions & 0 deletions ibis/backends/postgres/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,10 @@ def _literal(t, op):
dtype = op.dtype
value = op.value

if value is None:
return (
sa.null() if dtype.is_null() else sa.cast(sa.null(), t.get_sqla_type(dtype))
)
if dtype.is_interval():
return sa.literal_column(f"INTERVAL '{value} {dtype.resolution}'")
elif dtype.is_geospatial():
Expand All @@ -405,6 +409,16 @@ def _literal(t, op):
return pg.array(value)
elif dtype.is_map():
return pg.hstore(list(value.keys()), list(value.values()))
elif dtype.is_time():
return sa.func.make_time(
value.hour, value.minute, value.second + value.microsecond / 1e6
)
elif dtype.is_date():
return sa.func.make_date(value.year, value.month, value.day)
elif dtype.is_timestamp():
if (tz := dtype.timezone) is not None:
return sa.func.to_timestamp(value.timestamp()).op("AT TIME ZONE")(tz)
return sa.cast(sa.literal(value.isoformat()), sa.TIMESTAMP())
else:
return sa.literal(value)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SELECT
CAST('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,3 @@
SELECT
TO_DATE('2023-04-07', 'FXYYYY-MM-DD') AS "datetime.date(2023, 4, 7)"
FROM DUAL
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SELECT
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)"
FROM DUAL
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SELECT
MAKE_DATE(2023, 4, 7) AS "datetime.date(2023, 4, 7)"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SELECT
CAST('2023-04-07T04:05:06.230136' AS TIMESTAMP) AS "datetime.datetime(2023, 4, 7, 4, 5, 6, 230136)"

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SELECT
MAKE_TIME(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
MAKE_TIME(4, 5, 6.234567) AS "datetime.time(4, 5, 6, 234567)"

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

4 changes: 2 additions & 2 deletions ibis/backends/tests/test_temporal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2958,7 +2958,7 @@ def test_timestamp_bucket_offset(backend, offset_mins):
*no_sqlglot_dialect,
],
)
def test_temporal_literals(value, dialect, snapshot):
def test_temporal_literal_sql(value, dialect, snapshot):
expr = ibis.literal(value)
sql = ibis.to_sql(expr, dialect=dialect)
snapshot.assert_match(sql, "out.sql")
Expand Down Expand Up @@ -2995,7 +2995,7 @@ def test_temporal_literals(value, dialect, snapshot):
],
)
@pytest.mark.parametrize("micros", [0, 234567])
def test_time_literals(dialect, snapshot, micros):
def test_time_literal_sql(dialect, snapshot, micros):
value = datetime.time(4, 5, 6, microsecond=micros)
expr = ibis.literal(value)
sql = ibis.to_sql(expr, dialect=dialect)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ FROM (
COUNT(*) AS count_order
FROM main.lineitem AS t1
WHERE
t1.l_shipdate <= CAST('1998-09-02' AS DATE)
t1.l_shipdate <= MAKE_DATE(1998, 9, 2)
GROUP BY
1,
2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ WITH t0 AS (
ON t4.l_orderkey = t3.o_orderkey
WHERE
t2.c_mktsegment = 'BUILDING'
AND t3.o_orderdate < CAST('1995-03-15' AS DATE)
AND t4.l_shipdate > CAST('1995-03-15' AS DATE)
AND t3.o_orderdate < MAKE_DATE(1995, 3, 15)
AND t4.l_shipdate > MAKE_DATE(1995, 3, 15)
GROUP BY
1,
2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ WHERE
t1.l_orderkey = t0.o_orderkey AND t1.l_commitdate < t1.l_receiptdate
)
)
AND t0.o_orderdate >= CAST('1993-07-01' AS DATE)
AND t0.o_orderdate < CAST('1993-10-01' AS DATE)
AND t0.o_orderdate >= MAKE_DATE(1993, 7, 1)
AND t0.o_orderdate < MAKE_DATE(1993, 10, 1)
GROUP BY
1
ORDER BY
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ FROM (
ON t5.n_regionkey = t6.r_regionkey
WHERE
t6.r_name = 'ASIA'
AND t2.o_orderdate >= CAST('1994-01-01' AS DATE)
AND t2.o_orderdate < CAST('1995-01-01' AS DATE)
AND t2.o_orderdate >= MAKE_DATE(1994, 1, 1)
AND t2.o_orderdate < MAKE_DATE(1995, 1, 1)
GROUP BY
1
) AS t0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ SELECT
SUM(t0.l_extendedprice * t0.l_discount) AS revenue
FROM main.lineitem AS t0
WHERE
t0.l_shipdate >= CAST('1994-01-01' AS DATE)
AND t0.l_shipdate < CAST('1995-01-01' AS DATE)
t0.l_shipdate >= MAKE_DATE(1994, 1, 1)
AND t0.l_shipdate < MAKE_DATE(1995, 1, 1)
AND t0.l_discount BETWEEN CAST(0.05 AS REAL(53)) AND CAST(0.07 AS REAL(53))
AND t0.l_quantity < CAST(24 AS TINYINT)
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ FROM (
OR t0.cust_nation = 'GERMANY'
AND t0.supp_nation = 'FRANCE'
)
AND t0.l_shipdate BETWEEN CAST('1995-01-01' AS DATE) AND CAST('1996-12-31' AS DATE)
AND t0.l_shipdate BETWEEN MAKE_DATE(1995, 1, 1) AND MAKE_DATE(1996, 12, 31)
GROUP BY
1,
2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ WITH t0 AS (
FROM t0
WHERE
t0.r_name = 'AMERICA'
AND t0.o_orderdate BETWEEN CAST('1995-01-01' AS DATE) AND CAST('1996-12-31' AS DATE)
AND t0.o_orderdate BETWEEN MAKE_DATE(1995, 1, 1) AND MAKE_DATE(1996, 12, 31)
AND t0.p_type = 'ECONOMY ANODIZED STEEL'
), t2 AS (
SELECT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ WITH t0 AS (
JOIN main.nation AS t5
ON t2.c_nationkey = t5.n_nationkey
WHERE
t3.o_orderdate >= CAST('1993-10-01' AS DATE)
AND t3.o_orderdate < CAST('1994-01-01' AS DATE)
t3.o_orderdate >= MAKE_DATE(1993, 10, 1)
AND t3.o_orderdate < MAKE_DATE(1994, 1, 1)
AND t4.l_returnflag = 'R'
GROUP BY
1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ FROM (
t2.l_shipmode IN ('MAIL', 'SHIP')
AND t2.l_commitdate < t2.l_receiptdate
AND t2.l_shipdate < t2.l_commitdate
AND t2.l_receiptdate >= CAST('1994-01-01' AS DATE)
AND t2.l_receiptdate < CAST('1995-01-01' AS DATE)
AND t2.l_receiptdate >= MAKE_DATE(1994, 1, 1)
AND t2.l_receiptdate < MAKE_DATE(1995, 1, 1)
GROUP BY
1
) AS t0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,4 @@ FROM main.lineitem AS t0
JOIN main.part AS t1
ON t0.l_partkey = t1.p_partkey
WHERE
t0.l_shipdate >= CAST('1995-09-01' AS DATE)
AND t0.l_shipdate < CAST('1995-10-01' AS DATE)
t0.l_shipdate >= MAKE_DATE(1995, 9, 1) AND t0.l_shipdate < MAKE_DATE(1995, 10, 1)
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ WITH t0 AS (
)) AS total_revenue
FROM main.lineitem AS t3
WHERE
t3.l_shipdate >= CAST('1996-01-01' AS DATE)
AND t3.l_shipdate < CAST('1996-04-01' AS DATE)
t3.l_shipdate >= MAKE_DATE(1996, 1, 1) AND t3.l_shipdate < MAKE_DATE(1996, 4, 1)
GROUP BY
1
), t1 AS (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ WITH t0 AS (
WHERE
t6.l_partkey = t5.ps_partkey
AND t6.l_suppkey = t5.ps_suppkey
AND t6.l_shipdate >= CAST('1994-01-01' AS DATE)
AND t6.l_shipdate < CAST('1995-01-01' AS DATE)
AND t6.l_shipdate >= MAKE_DATE(1994, 1, 1)
AND t6.l_shipdate < MAKE_DATE(1995, 1, 1)
) * CAST(0.5 AS REAL(53))
) AS t4
)
Expand Down

0 comments on commit a3c1c07

Please sign in to comment.