Skip to content

Commit

Permalink
fix(rewrite): avoid accumulating context state during rewriting (#9814)
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud authored Aug 11, 2024
1 parent 36f5162 commit 9165255
Show file tree
Hide file tree
Showing 19 changed files with 100 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SELECT
ntile(2) OVER (ORDER BY RAND() ASC) - 1 AS `new_col`
FROM `test` AS `t0`
LIMIT 10
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SELECT
ntile(2) OVER (ORDER BY randCanonical() ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) - 1 AS "new_col"
FROM "test" AS "t0"
LIMIT 10
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SELECT
NTILE(2) OVER (ORDER BY RANDOM() ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) - 1 AS "new_col"
FROM "test" AS "t0"
LIMIT 10
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
SELECT
NTILE(2) OVER (ORDER BY RANDOM() ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) - 1 AS "new_col"
FROM (
SELECT
"test"
FROM (VALUES
(1),
(2),
(3),
(4),
(5)) AS "test"("test")
) AS "t0"
LIMIT 10
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SELECT
NTILE(2) OVER (ORDER BY RANDOM() ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) - 1 AS "new_col"
FROM "test" AS "t0"
LIMIT 10
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SELECT
NTILE(2) OVER (ORDER BY RANDOM() ASC) - 1 AS "new_col"
FROM "test" AS "t0"
LIMIT 10
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
SELECT
NTILE(2) OVER (ORDER BY RAND() ASC NULLS LAST) - 1 AS `new_col`
FROM (
SELECT
`test`
FROM (VALUES
(1),
(2),
(3),
(4),
(5)) AS `test`(`test`)
) AS `t0`
LIMIT 10
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SELECT
NTILE(2) OVER (ORDER BY RAND(UTC_TO_UNIX_MICROS(UTC_TIMESTAMP())) ASC) - 1 AS `new_col`
FROM `test` AS `t0`
LIMIT 10
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SELECT
TOP 10
NTILE(2) OVER (ORDER BY RAND() ASC) - 1 AS [new_col]
FROM [test] AS [t0]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SELECT
NTILE(2) OVER (ORDER BY RAND() ASC) - 1 AS `new_col`
FROM `test` AS `t0`
LIMIT 10
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SELECT
NTILE(2) OVER (ORDER BY DBMS_RANDOM.VALUE() ASC) - 1 AS "new_col"
FROM "test" "t0"
FETCH FIRST 10 ROWS ONLY
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SELECT
NTILE(2) OVER (ORDER BY RANDOM() ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) - 1 AS "new_col"
FROM "test" AS "t0"
LIMIT 10
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SELECT
NTILE(2) OVER (ORDER BY RAND() ASC NULLS LAST) - 1 AS `new_col`
FROM `test` AS `t0`
LIMIT 10
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SELECT
NTILE(2) OVER (ORDER BY RANDOM() ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) - 1 AS "new_col"
FROM "test" AS "t0"
LIMIT 10
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SELECT
NTILE(2) OVER (ORDER BY UNIFORM(TO_DOUBLE(0.0), TO_DOUBLE(1.0), RANDOM()) ASC) - 1 AS "new_col"
FROM "test" AS "t0"
LIMIT 10
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
SELECT
NTILE(2) OVER (ORDER BY 0.5 + (
CAST(RANDOM() AS REAL) / -1.8446744073709552e+19
) ASC NULLS LAST ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) - 1 AS "new_col"
FROM "test" AS "t0"
LIMIT 10
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SELECT
NTILE(2) OVER (ORDER BY RAND() ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) - 1 AS "new_col"
FROM "test" AS "t0"
LIMIT 10
11 changes: 11 additions & 0 deletions ibis/backends/tests/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,14 @@ def test_mixed_qualified_and_unqualified_predicates(backend_name, snapshot):
r"\bQUALIFY\b", result, flags=re.MULTILINE | re.IGNORECASE
)
snapshot.assert_match(result, "out.sql")


@pytest.mark.parametrize("backend_name", _get_backends_to_test())
@pytest.mark.notimpl(
["dask", "pandas", "polars"], raises=ValueError, reason="not a SQL backend"
)
def test_rewrite_context(snapshot, backend_name):
table = ibis.memtable({"test": [1, 2, 3, 4, 5]}, name="test")
expr = table.select(new_col=ibis.ntile(2).over(order_by=ibis.random())).limit(10)
result = ibis.to_sql(expr, dialect=backend_name)
snapshot.assert_match(result, "out.sql")
2 changes: 1 addition & 1 deletion ibis/common/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,9 @@ def _coerce_replacer(obj: ReplacerLike, context: Optional[dict] = None) -> Repla
"""
if isinstance(obj, Pattern):
ctx = context or {}

def fn(node, _, **kwargs):
ctx = context or {}
# need to first reconstruct the node from the possible rewritten
# children, so we can match on the new node containing the rewritten
# child arguments, this way we can propagate the rewritten nodes
Expand Down

0 comments on commit 9165255

Please sign in to comment.