Skip to content

Commit dcb629e

Browse files
committed
feat(rewrites): improve how constant sort values are dropped
Before, we only dropped constant sort values if - it was an EXACT Literal. if it was derived, eg `ibis.literal(5) + 1`, then we wouldn't. - if the merge_select_select decided to return the original select instead of merging them (eg because of a window function, unnest, etc), then we wouldn't see this optimization.
1 parent e4e582a commit dcb629e

File tree

11 files changed

+104
-6
lines changed

11 files changed

+104
-6
lines changed

ibis/backends/sql/rewrites.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,15 @@ def filter_to_select(_, **kwargs):
124124
)
125125

126126

127+
def is_constant(value: ops.Value) -> bool:
128+
return not value.relations and not value.find(ops.Impure, filter=ops.Value)
129+
130+
127131
@replace(p.Sort)
128132
def sort_to_select(_, **kwargs):
129133
"""Convert a Sort node to a Select node."""
130-
return Select(_.parent, selections=_.values, sort_keys=_.keys)
134+
non_constant_keys = tuple(key for key in _.keys if not is_constant(key.expr))
135+
return Select(_.parent, selections=_.values, sort_keys=non_constant_keys)
131136

132137

133138
@replace(p.Distinct)
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
SELECT
2-
*
2+
"t0"."f",
3+
"t0"."c"
34
FROM "star1" AS "t0"
45
ORDER BY
56
"t0"."f" ASC
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
SELECT
2+
"t0"."f",
3+
"t0"."c"
4+
FROM "star1" AS "t0"
5+
ORDER BY
6+
"t0"."f" ASC
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
SELECT
2+
"t0"."f",
3+
"t0"."c"
4+
FROM "star1" AS "t0"
5+
ORDER BY
6+
"t0"."f" + 1 ASC
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
SELECT
2+
"t0"."f",
3+
"t0"."c"
4+
FROM "star1" AS "t0"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
SELECT
2+
"t0"."f",
3+
"t0"."c"
4+
FROM "star1" AS "t0"
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
SELECT
22
*
3-
FROM "star1" AS "t0"
3+
FROM (
4+
SELECT
5+
"t1"."f",
6+
"t1"."c"
7+
FROM (
8+
SELECT
9+
*
10+
FROM "star1" AS "t0"
11+
ORDER BY
12+
RANDOM() ASC
13+
) AS "t1"
14+
) AS "t2"
415
ORDER BY
516
RANDOM() ASC
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
SELECT
2+
*
3+
FROM (
4+
SELECT
5+
"t1"."f",
6+
"t1"."c"
7+
FROM (
8+
SELECT
9+
*
10+
FROM "star1" AS "t0"
11+
ORDER BY
12+
RANDOM() ASC,
13+
"t0"."f" ASC
14+
) AS "t1"
15+
) AS "t2"
16+
ORDER BY
17+
RANDOM() ASC,
18+
"t2"."f" ASC
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
SELECT
2+
*
3+
FROM (
4+
SELECT
5+
"t1"."f",
6+
"t1"."c"
7+
FROM (
8+
SELECT
9+
*
10+
FROM "star1" AS "t0"
11+
ORDER BY
12+
RANDOM() ASC
13+
) AS "t1"
14+
) AS "t2"
15+
ORDER BY
16+
RANDOM() ASC
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
SELECT
2+
*
3+
FROM (
4+
SELECT
5+
"t1"."f",
6+
"t1"."c"
7+
FROM (
8+
SELECT
9+
*
10+
FROM "star1" AS "t0"
11+
ORDER BY
12+
RANDOM() + 1 ASC
13+
) AS "t1"
14+
) AS "t2"
15+
ORDER BY
16+
RANDOM() + 1 ASC

0 commit comments

Comments
 (0)