Skip to content

Commit

Permalink
fix(sql): replace CTEs within CTEs (#8572)
Browse files Browse the repository at this point in the history
Co-authored-by: Phillip Cloud <417981+cpcloud@users.noreply.github.com>
  • Loading branch information
kszucs and cpcloud authored Mar 8, 2024
1 parent 68925f6 commit 182b6a5
Show file tree
Hide file tree
Showing 18 changed files with 391 additions and 564 deletions.
11 changes: 8 additions & 3 deletions ibis/backends/sql/rewrites.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,14 @@ def sqlize(
simplified = sqlized.replace(merge_select_select)

# extract common table expressions while wrapping them in a CTE node
ctes = extract_ctes(simplified)
subs = {cte: CTE(cte) for cte in ctes}
result = simplified.replace(subs)
ctes = frozenset(extract_ctes(simplified))

def wrap(node, _, **kwargs):
new = node.__recreate__(kwargs)
return CTE(new) if node in ctes else new

result = simplified.replace(wrap)
ctes = reversed([cte.parent for cte in result.find(CTE)])

return result, ctes

Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,32 @@
WITH `t6` AS (
WITH `t1` AS (
SELECT
`t5`.`street`,
ROW_NUMBER() OVER (ORDER BY `t5`.`street` ASC) - 1 AS `key`
`t0`.`street`,
ROW_NUMBER() OVER (ORDER BY `t0`.`street` ASC) - 1 AS `key`
FROM `data` AS `t0`
), `t7` AS (
SELECT
`t6`.`street`,
ROW_NUMBER() OVER (ORDER BY `t6`.`street` ASC) - 1 AS `key`
FROM (
SELECT
`t2`.`street`,
`t2`.`key`
FROM (
SELECT
`t0`.`street`,
ROW_NUMBER() OVER (ORDER BY `t0`.`street` ASC) - 1 AS `key`
FROM `data` AS `t0`
) AS `t2`
`t3`.`street`,
`t3`.`key`
FROM `t1` AS `t3`
INNER JOIN (
SELECT
`t1`.`key`
FROM (
SELECT
`t0`.`street`,
ROW_NUMBER() OVER (ORDER BY `t0`.`street` ASC) - 1 AS `key`
FROM `data` AS `t0`
) AS `t1`
) AS `t4`
ON `t2`.`key` = `t4`.`key`
) AS `t5`
), `t1` AS (
SELECT
`t0`.`street`,
ROW_NUMBER() OVER (ORDER BY `t0`.`street` ASC) - 1 AS `key`
FROM `data` AS `t0`
`t2`.`key`
FROM `t1` AS `t2`
) AS `t5`
ON `t3`.`key` = `t5`.`key`
) AS `t6`
)
SELECT
`t8`.`street`,
`t8`.`key`
FROM `t6` AS `t8`
`t9`.`street`,
`t9`.`key`
FROM `t7` AS `t9`
INNER JOIN (
SELECT
`t7`.`key`
FROM `t6` AS `t7`
) AS `t10`
ON `t8`.`key` = `t10`.`key`
`t8`.`key`
FROM `t7` AS `t8`
) AS `t11`
ON `t9`.`key` = `t11`.`key`
Original file line number Diff line number Diff line change
@@ -1,42 +1,32 @@
WITH "t6" AS (
WITH "t1" AS (
SELECT
"t5"."street",
ROW_NUMBER() OVER (ORDER BY "t5"."street" ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) - 1 AS "key"
"t0"."street",
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) - 1 AS "key"
FROM (
SELECT
"t2"."street",
"t2"."key"
FROM (
SELECT
"t0"."street",
ROW_NUMBER() OVER (ORDER BY "t0"."street" ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) - 1 AS "key"
FROM "data" AS "t0"
) AS "t2"
"t3"."street",
"t3"."key"
FROM "t1" AS "t3"
INNER JOIN (
SELECT
"t1"."key"
FROM (
SELECT
"t0"."street",
ROW_NUMBER() OVER (ORDER BY "t0"."street" ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) - 1 AS "key"
FROM "data" AS "t0"
) AS "t1"
) AS "t4"
ON "t2"."key" = "t4"."key"
) AS "t5"
), "t1" AS (
SELECT
"t0"."street",
ROW_NUMBER() OVER (ORDER BY "t0"."street" ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) - 1 AS "key"
FROM "data" AS "t0"
"t2"."key"
FROM "t1" AS "t2"
) AS "t5"
ON "t3"."key" = "t5"."key"
) AS "t6"
)
SELECT
"t8"."street",
"t8"."key"
FROM "t6" AS "t8"
"t9"."street",
"t9"."key"
FROM "t7" AS "t9"
INNER JOIN (
SELECT
"t7"."key"
FROM "t6" AS "t7"
) AS "t10"
ON "t8"."key" = "t10"."key"
"t8"."key"
FROM "t7" AS "t8"
) AS "t11"
ON "t9"."key" = "t11"."key"
Original file line number Diff line number Diff line change
@@ -1,42 +1,32 @@
WITH "t6" AS (
WITH "t1" AS (
SELECT
"t5"."street",
ROW_NUMBER() OVER (ORDER BY "t5"."street" ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) - 1 AS "key"
"t0"."street",
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) - 1 AS "key"
FROM (
SELECT
"t2"."street",
"t2"."key"
FROM (
SELECT
"t0"."street",
ROW_NUMBER() OVER (ORDER BY "t0"."street" ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) - 1 AS "key"
FROM "data" AS "t0"
) AS "t2"
"t3"."street",
"t3"."key"
FROM "t1" AS "t3"
INNER JOIN (
SELECT
"t1"."key"
FROM (
SELECT
"t0"."street",
ROW_NUMBER() OVER (ORDER BY "t0"."street" ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) - 1 AS "key"
FROM "data" AS "t0"
) AS "t1"
) AS "t4"
ON "t2"."key" = "t4"."key"
) AS "t5"
), "t1" AS (
SELECT
"t0"."street",
ROW_NUMBER() OVER (ORDER BY "t0"."street" ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) - 1 AS "key"
FROM "data" AS "t0"
"t2"."key"
FROM "t1" AS "t2"
) AS "t5"
ON "t3"."key" = "t5"."key"
) AS "t6"
)
SELECT
"t8"."street",
"t8"."key"
FROM "t6" AS "t8"
"t9"."street",
"t9"."key"
FROM "t7" AS "t9"
INNER JOIN (
SELECT
"t7"."key"
FROM "t6" AS "t7"
) AS "t10"
ON "t8"."key" = "t10"."key"
"t8"."key"
FROM "t7" AS "t8"
) AS "t11"
ON "t9"."key" = "t11"."key"
Original file line number Diff line number Diff line change
@@ -1,42 +1,32 @@
WITH "t6" AS (
WITH "t1" AS (
SELECT
"t5"."street",
ROW_NUMBER() OVER (ORDER BY "t5"."street" ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) - 1 AS "key"
"t0"."street",
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) - 1 AS "key"
FROM (
SELECT
"t2"."street",
"t2"."key"
FROM (
SELECT
"t0"."street",
ROW_NUMBER() OVER (ORDER BY "t0"."street" ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) - 1 AS "key"
FROM "data" AS "t0"
) AS "t2"
"t3"."street",
"t3"."key"
FROM "t1" AS "t3"
INNER JOIN (
SELECT
"t1"."key"
FROM (
SELECT
"t0"."street",
ROW_NUMBER() OVER (ORDER BY "t0"."street" ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) - 1 AS "key"
FROM "data" AS "t0"
) AS "t1"
) AS "t4"
ON "t2"."key" = "t4"."key"
) AS "t5"
), "t1" AS (
SELECT
"t0"."street",
ROW_NUMBER() OVER (ORDER BY "t0"."street" ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) - 1 AS "key"
FROM "data" AS "t0"
"t2"."key"
FROM "t1" AS "t2"
) AS "t5"
ON "t3"."key" = "t5"."key"
) AS "t6"
)
SELECT
"t8"."street",
"t8"."key"
FROM "t6" AS "t8"
"t9"."street",
"t9"."key"
FROM "t7" AS "t9"
INNER JOIN (
SELECT
"t7"."key"
FROM "t6" AS "t7"
) AS "t10"
ON "t8"."key" = "t10"."key"
"t8"."key"
FROM "t7" AS "t8"
) AS "t11"
ON "t9"."key" = "t11"."key"
Original file line number Diff line number Diff line change
@@ -1,42 +1,32 @@
WITH "t6" AS (
WITH "t1" AS (
SELECT
"t5"."street",
ROW_NUMBER() OVER (ORDER BY "t5"."street" ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) - CAST(1 AS TINYINT) AS "key"
"t0"."street",
ROW_NUMBER() OVER (ORDER BY "t0"."street" ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) - CAST(1 AS TINYINT) 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"
FROM (
SELECT
"t2"."street",
"t2"."key"
FROM (
SELECT
"t0"."street",
ROW_NUMBER() OVER (ORDER BY "t0"."street" ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) - CAST(1 AS TINYINT) AS "key"
FROM "data" AS "t0"
) AS "t2"
"t3"."street",
"t3"."key"
FROM "t1" AS "t3"
INNER JOIN (
SELECT
"t1"."key"
FROM (
SELECT
"t0"."street",
ROW_NUMBER() OVER (ORDER BY "t0"."street" ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) - CAST(1 AS TINYINT) AS "key"
FROM "data" AS "t0"
) AS "t1"
) AS "t4"
ON "t2"."key" = "t4"."key"
) AS "t5"
), "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"
FROM "data" AS "t0"
"t2"."key"
FROM "t1" AS "t2"
) AS "t5"
ON "t3"."key" = "t5"."key"
) AS "t6"
)
SELECT
"t8"."street",
"t8"."key"
FROM "t6" AS "t8"
"t9"."street",
"t9"."key"
FROM "t7" AS "t9"
INNER JOIN (
SELECT
"t7"."key"
FROM "t6" AS "t7"
) AS "t10"
ON "t8"."key" = "t10"."key"
"t8"."key"
FROM "t7" AS "t8"
) AS "t11"
ON "t9"."key" = "t11"."key"
Loading

0 comments on commit 182b6a5

Please sign in to comment.