Skip to content

Commit

Permalink
fix(sql): remove constants in order_by calls during select merging (#…
Browse files Browse the repository at this point in the history
…10475)

Remove literals in `ORDER BY` when sorting tables. Fixes #10428.
  • Loading branch information
cpcloud authored Dec 4, 2024
1 parent 5d0558a commit 6e693b7
Show file tree
Hide file tree
Showing 20 changed files with 139 additions and 1 deletion.
4 changes: 3 additions & 1 deletion ibis/backends/sql/rewrites.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,9 @@ def merge_select_select(_, **kwargs):
selections=selections,
predicates=unique_predicates,
qualified=unique_qualified,
sort_keys=unique_sort_keys,
sort_keys=tuple(
key for key in unique_sort_keys if not isinstance(key.expr, ops.Literal)
),
distinct=distinct,
)
return result if complexity(result) <= complexity(_) else _
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SELECT
`t0`.`a`,
9 AS `i`,
'foo' AS `s`
FROM `test` AS `t0`
ORDER BY
`t0`.`a` ASC NULLS LAST
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SELECT
"t0"."a" AS "a",
9 AS "i",
'foo' AS "s"
FROM "test" AS "t0"
ORDER BY
"t0"."a" ASC
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SELECT
`t0`.`a`,
9 AS `i`,
'foo' AS `s`
FROM `test` AS `t0`
ORDER BY
`t0`.`a` ASC NULLS LAST
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SELECT
"t0"."a",
9 AS "i",
'foo' AS "s"
FROM "test" AS "t0"
ORDER BY
"t0"."a" ASC
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SELECT
"t0"."a",
9 AS "i",
'foo' AS "s"
FROM "test" AS "t0"
ORDER BY
"t0"."a" ASC
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SELECT
"t0"."a",
9 AS "i",
'foo' AS "s"
FROM "test" AS "t0"
ORDER BY
"t0"."a" ASC
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SELECT
"t0"."a",
9 AS "i",
'foo' AS "s"
FROM "test" AS "t0"
ORDER BY
"t0"."a" ASC
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SELECT
`t0`.`a`,
9 AS `i`,
'foo' AS `s`
FROM `test` AS `t0`
ORDER BY
`t0`.`a` ASC NULLS LAST
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SELECT
`t0`.`a`,
9 AS `i`,
'foo' AS `s`
FROM `test` AS `t0`
ORDER BY
`t0`.`a` ASC
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SELECT
[t0].[a],
9 AS [i],
'foo' AS [s]
FROM [test] AS [t0]
ORDER BY
CASE WHEN [t0].[a] IS NULL THEN 1 ELSE 0 END, [t0].[a] ASC
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SELECT
`t0`.`a`,
9 AS `i`,
'foo' AS `s`
FROM `test` AS `t0`
ORDER BY
CASE WHEN `t0`.`a` IS NULL THEN 1 ELSE 0 END, `t0`.`a` ASC
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SELECT
"t0"."a",
9 AS "i",
'foo' AS "s"
FROM "test" "t0"
ORDER BY
"t0"."a" ASC
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SELECT
"t0"."a",
9 AS "i",
'foo' AS "s"
FROM "test" AS "t0"
ORDER BY
"t0"."a" ASC
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SELECT
`t0`.`a`,
9 AS `i`,
'foo' AS `s`
FROM `test` AS `t0`
ORDER BY
`t0`.`a` ASC NULLS LAST
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SELECT
"t0"."a",
9 AS "i",
'foo' AS "s"
FROM "test" AS "t0"
ORDER BY
"t0"."a" ASC
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SELECT
"t0"."a",
9 AS "i",
'foo' AS "s"
FROM "test" AS "t0"
ORDER BY
"t0"."a" ASC
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SELECT
"t0"."a",
9 AS "i",
'foo' AS "s"
FROM "test" AS "t0"
ORDER BY
"t0"."a" ASC NULLS LAST
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SELECT
"t0"."a",
9 AS "i",
'foo' AS "s"
FROM "test" AS "t0"
ORDER BY
"t0"."a" ASC
10 changes: 10 additions & 0 deletions ibis/backends/tests/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,13 @@ def test_sample(backend_name, snapshot, subquery):
row = ibis.to_sql(t.sample(0.5, method="row"), dialect=backend_name)
snapshot.assert_match(block, "block.sql")
snapshot.assert_match(row, "row.sql")


@pytest.mark.parametrize("backend_name", _get_backends_to_test())
@pytest.mark.notimpl(["polars"], raises=ValueError, reason="not a SQL backend")
def test_order_by_no_deference_literals(backend_name, snapshot):
t = ibis.table({"a": "int"}, name="test")
s = t.select("a", i=ibis.literal(9), s=ibis.literal("foo"))
o = s.order_by("a", "i", "s")
sql = ibis.to_sql(o, dialect=backend_name)
snapshot.assert_match(sql, "out.sql")

0 comments on commit 6e693b7

Please sign in to comment.