Skip to content

Commit

Permalink
fix(duckdb): avoid literals casts that might defeat optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud authored and jcrist committed Jul 23, 2024
1 parent feb12f4 commit e4ff1bd
Show file tree
Hide file tree
Showing 72 changed files with 156 additions and 177 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SELECT
ST_DWITHIN("t0"."geom", "t0"."geom", CAST(3.0 AS DOUBLE)) AS "tmp"
ST_DWITHIN("t0"."geom", "t0"."geom", 3.0) AS "tmp"
FROM "t" AS "t0"
29 changes: 19 additions & 10 deletions ibis/backends/sql/compilers/duckdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,17 @@ def visit_ArrayZip(self, op, *, arg):
any_arg_null = sg.or_(*(arr.is_(NULL) for arr in arg))
return self.if_(any_arg_null, NULL, zipped_arrays)

def visit_Array(self, op, *, exprs):
return self.cast(self.f.array(*exprs), op.dtype)

def visit_Map(self, op, *, keys, values):
# workaround for https://github.com/ibis-project/ibis/issues/8632
return self.if_(
sg.or_(keys.is_(NULL), values.is_(NULL)), NULL, self.f.map(keys, values)
sg.or_(keys.is_(NULL), values.is_(NULL)),
NULL,
self.f.map(
self.cast(keys, op.keys.dtype), self.cast(values, op.values.dtype)
),
)

def visit_MapGet(self, op, *, arg, key, default):
Expand Down Expand Up @@ -378,6 +385,8 @@ def visit_NonNullLiteral(self, op, *, value, dtype):
return self.cast(
str(value), to=dt.float32 if dtype.is_decimal() else dtype
)
if dtype.is_floating() or dtype.is_integer():
return sge.convert(value)
return self.cast(value, dtype)
elif dtype.is_time():
return self.f.make_time(
Expand All @@ -401,16 +410,16 @@ def visit_NonNullLiteral(self, op, *, value, dtype):

return self.f[funcname](*args)
elif dtype.is_struct():
return sge.Struct.from_arg_list(
[
sge.PropertyEQ(
this=sg.to_identifier(k, quoted=self.quoted),
expression=self.visit_Literal(
return self.cast(
sge.Struct.from_arg_list(
[
self.visit_Literal(
ops.Literal(v, field_dtype), value=v, dtype=field_dtype
),
)
for field_dtype, (k, v) in zip(dtype.types, value.items())
]
)
for field_dtype, v in zip(dtype.types, value.values())
]
),
op.dtype,
)
else:
return None
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
WITH "t1" AS (
SELECT
"t0"."street",
ROW_NUMBER() OVER (ORDER BY "t0"."street" ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) - CAST(1 AS TINYINT) AS "key"
ROW_NUMBER() OVER (ORDER BY "t0"."street" ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) - 1 AS "key"
FROM "data" AS "t0"
), "t7" AS (
SELECT
"t6"."street",
ROW_NUMBER() OVER (ORDER BY "t6"."street" ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) - CAST(1 AS TINYINT) AS "key"
ROW_NUMBER() OVER (ORDER BY "t6"."street" ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) - 1 AS "key"
FROM (
SELECT
"t3"."street",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ SELECT
*
FROM "t" AS "t0"
WHERE
"t0"."x" > CAST(2 AS TINYINT)
"t0"."x" > 2
) AS "InSubquery(x)"
FROM "t" AS "t0"
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,26 @@ WITH "t5" AS (
SELECT
"t0"."field_of_study",
UNNEST(
[
{'years': '1970-71', 'degrees': "t0"."1970-71"},
{'years': '1975-76', 'degrees': "t0"."1975-76"},
{'years': '1980-81', 'degrees': "t0"."1980-81"},
{'years': '1985-86', 'degrees': "t0"."1985-86"},
{'years': '1990-91', 'degrees': "t0"."1990-91"},
{'years': '1995-96', 'degrees': "t0"."1995-96"},
{'years': '2000-01', 'degrees': "t0"."2000-01"},
{'years': '2005-06', 'degrees': "t0"."2005-06"},
{'years': '2010-11', 'degrees': "t0"."2010-11"},
{'years': '2011-12', 'degrees': "t0"."2011-12"},
{'years': '2012-13', 'degrees': "t0"."2012-13"},
{'years': '2013-14', 'degrees': "t0"."2013-14"},
{'years': '2014-15', 'degrees': "t0"."2014-15"},
{'years': '2015-16', 'degrees': "t0"."2015-16"},
{'years': '2016-17', 'degrees': "t0"."2016-17"},
{'years': '2017-18', 'degrees': "t0"."2017-18"},
{'years': '2018-19', 'degrees': "t0"."2018-19"},
{'years': '2019-20', 'degrees': "t0"."2019-20"}
]
CAST([
ROW('1970-71', "t0"."1970-71"),
ROW('1975-76', "t0"."1975-76"),
ROW('1980-81', "t0"."1980-81"),
ROW('1985-86', "t0"."1985-86"),
ROW('1990-91', "t0"."1990-91"),
ROW('1995-96', "t0"."1995-96"),
ROW('2000-01', "t0"."2000-01"),
ROW('2005-06', "t0"."2005-06"),
ROW('2010-11', "t0"."2010-11"),
ROW('2011-12', "t0"."2011-12"),
ROW('2012-13', "t0"."2012-13"),
ROW('2013-14', "t0"."2013-14"),
ROW('2014-15', "t0"."2014-15"),
ROW('2015-16', "t0"."2015-16"),
ROW('2016-17', "t0"."2016-17"),
ROW('2017-18', "t0"."2017-18"),
ROW('2018-19', "t0"."2018-19"),
ROW('2019-20', "t0"."2019-20")
] AS STRUCT("years" TEXT, "degrees" BIGINT)[])
) AS "__pivoted__"
FROM "humanities" AS "t0"
) AS "t1"
Expand Down Expand Up @@ -73,7 +73,7 @@ FROM (
*
FROM "t5" AS "t6"
WHERE
"t6"."diff" < CAST(0 AS TINYINT)
"t6"."diff" < 0
ORDER BY
"t6"."diff" ASC
LIMIT 10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ SELECT
*
FROM "my_table" AS "t0"
WHERE
"t0"."a" < CAST(100 AS TINYINT)
"t0"."a" < 100
AND "t0"."a" = (
SELECT
MAX("t1"."a") AS "Max(a)"
Expand All @@ -11,7 +11,7 @@ WHERE
*
FROM "my_table" AS "t0"
WHERE
"t0"."a" < CAST(100 AS TINYINT)
"t0"."a" < 100
) AS "t1"
)
AND "t0"."b" = 'a'
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
SELECT
"t0"."a",
"t0"."b" * CAST(2 AS TINYINT) AS "b2"
"t0"."b" * 2 AS "b2"
FROM "my_table" AS "t0"
WHERE
"t0"."a" < CAST(100 AS TINYINT)
"t0"."a" < 100
AND "t0"."a" = (
SELECT
MAX("t1"."a") AS "Max(a)"
FROM (
SELECT
"t0"."a",
"t0"."b" * CAST(2 AS TINYINT) AS "b2"
"t0"."b" * 2 AS "b2"
FROM "my_table" AS "t0"
WHERE
"t0"."a" < CAST(100 AS TINYINT)
"t0"."a" < 100
) AS "t1"
)
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
SELECT
"t0"."a",
"t0"."b" * CAST(2 AS TINYINT) AS "b2"
"t0"."b" * 2 AS "b2"
FROM "my_table" AS "t0"
WHERE
"t0"."a" < CAST(100 AS TINYINT)
"t0"."a" < 100
AND "t0"."a" = (
SELECT
MAX("t1"."a") AS "Max(a)"
FROM (
SELECT
"t0"."a",
"t0"."b" * CAST(2 AS TINYINT) AS "b2"
"t0"."b" * 2 AS "b2"
FROM "my_table" AS "t0"
WHERE
"t0"."a" < CAST(100 AS TINYINT)
"t0"."a" < 100
) AS "t1"
)
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SELECT
"t0"."int_col" + CAST(4 AS TINYINT) AS "Add(int_col, 4)"
"t0"."int_col" + 4 AS "Add(int_col, 4)"
FROM "int_col_table" AS "t0"
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SELECT
"t0"."int_col" + CAST(4 AS TINYINT) AS "foo"
"t0"."int_col" + 4 AS "foo"
FROM "int_col_table" AS "t0"
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ FROM (
*
FROM "functional_alltypes" AS "t0"
WHERE
"t0"."bigint_col" > CAST(0 AS TINYINT)
"t0"."bigint_col" > 0
) AS "t1"
GROUP BY
1
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ FROM (
CAST("t0"."float_col" AS DOUBLE) AS "value"
FROM "functional_alltypes" AS "t0"
WHERE
"t0"."int_col" > CAST(0 AS TINYINT)
"t0"."int_col" > 0
) AS "t1"
EXCEPT
SELECT
Expand All @@ -20,6 +20,6 @@ FROM (
"t0"."double_col" AS "value"
FROM "functional_alltypes" AS "t0"
WHERE
"t0"."int_col" <= CAST(0 AS TINYINT)
"t0"."int_col" <= 0
) AS "t2"
) AS "t3"
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ FROM (
1
) AS "t2"
WHERE
"t2"."Max(a)" = CAST(2 AS TINYINT)
"t2"."Max(a)" = 2
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ FROM (
1
) AS "t1"
WHERE
"t1"."Max(double_col)" = CAST(1 AS TINYINT)
"t1"."Max(double_col)" = 1
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ FROM (
CAST("t0"."float_col" AS DOUBLE) AS "value"
FROM "functional_alltypes" AS "t0"
WHERE
"t0"."int_col" > CAST(0 AS TINYINT)
"t0"."int_col" > 0
) AS "t1"
INTERSECT
SELECT
Expand All @@ -20,6 +20,6 @@ FROM (
"t0"."double_col" AS "value"
FROM "functional_alltypes" AS "t0"
WHERE
"t0"."int_col" <= CAST(0 AS TINYINT)
"t0"."int_col" <= 0
) AS "t2"
) AS "t3"
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ SELECT
*
FROM "functional_alltypes" AS "t0"
WHERE
"t0"."double_col" > CAST(3.14 AS DOUBLE)
"t0"."double_col" > 3.14
AND CONTAINS("t0"."string_col", 'foo')
AND (
(
(
"t0"."int_col" - CAST(1 AS TINYINT)
) = CAST(0 AS TINYINT)
)
OR (
"t0"."float_col" <= CAST(1.34 AS DOUBLE)
"t0"."int_col" - 1
) = 0
) OR (
"t0"."float_col" <= 1.34
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ SELECT
*
FROM "my_table" AS "t0"
WHERE
"t0"."a" < CAST(100 AS TINYINT)
"t0"."a" < 100
AND "t0"."a" = (
SELECT
MAX("t1"."a") AS "Max(a)"
Expand All @@ -11,6 +11,6 @@ WHERE
*
FROM "my_table" AS "t0"
WHERE
"t0"."a" < CAST(100 AS TINYINT)
"t0"."a" < 100
) AS "t1"
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ FROM (
CAST("t0"."float_col" AS DOUBLE) AS "value"
FROM "functional_alltypes" AS "t0"
WHERE
"t0"."int_col" > CAST(0 AS TINYINT)
"t0"."int_col" > 0
) AS "t1"
EXCEPT
SELECT
Expand All @@ -17,5 +17,5 @@ FROM (
"t0"."double_col" AS "value"
FROM "functional_alltypes" AS "t0"
WHERE
"t0"."int_col" <= CAST(0 AS TINYINT)
"t0"."int_col" <= 0
) AS "t2"
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ FROM (
ON "t4"."b" = "t2"."b"
) AS "t5"
WHERE
"t5"."a" < CAST(1.0 AS DOUBLE)
"t5"."a" < 1.0
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ FROM (
CAST("t0"."float_col" AS DOUBLE) AS "value"
FROM "functional_alltypes" AS "t0"
WHERE
"t0"."int_col" > CAST(0 AS TINYINT)
"t0"."int_col" > 0
) AS "t1"
INTERSECT
SELECT
Expand All @@ -17,5 +17,5 @@ FROM (
"t0"."double_col" AS "value"
FROM "functional_alltypes" AS "t0"
WHERE
"t0"."int_col" <= CAST(0 AS TINYINT)
"t0"."int_col" <= 0
) AS "t2"
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ FROM (
CAST("t0"."float_col" AS DOUBLE) AS "value"
FROM "functional_alltypes" AS "t0"
WHERE
"t0"."int_col" > CAST(0 AS TINYINT)
"t0"."int_col" > 0
) AS "t1"
UNION
SELECT
Expand All @@ -17,5 +17,5 @@ FROM (
"t0"."double_col" AS "value"
FROM "functional_alltypes" AS "t0"
WHERE
"t0"."int_col" <= CAST(0 AS TINYINT)
"t0"."int_col" <= 0
) AS "t2"
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ FROM (
CAST("t0"."float_col" AS DOUBLE) AS "value"
FROM "functional_alltypes" AS "t0"
WHERE
"t0"."int_col" > CAST(0 AS TINYINT)
"t0"."int_col" > 0
) AS "t1"
UNION ALL
SELECT
Expand All @@ -20,6 +20,6 @@ FROM (
"t0"."double_col" AS "value"
FROM "functional_alltypes" AS "t0"
WHERE
"t0"."int_col" <= CAST(0 AS TINYINT)
"t0"."int_col" <= 0
) AS "t2"
) AS "t3"
Loading

0 comments on commit e4ff1bd

Please sign in to comment.