Skip to content

Commit 8a5a96e

Browse files
dont extract union all children as ctes
1 parent e6043dd commit 8a5a96e

File tree

3 files changed

+50
-61
lines changed
  • bigframes/core/rewrite
  • tests/unit/core/compile/sqlglot/snapshots/test_compile_concat

3 files changed

+50
-61
lines changed

bigframes/core/rewrite/as_sql.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from __future__ import annotations
1515

1616
import dataclasses
17+
import itertools
1718
from typing import Optional, Sequence, Union
1819

1920
from bigframes.core import (
@@ -240,11 +241,23 @@ def _insert_cte_markers(root: nodes.BigFrameNode) -> nodes.BigFrameNode:
240241
wrapped_nodes = set(
241242
node.child for node in root.unique_nodes() if isinstance(node, nodes.CteNode)
242243
)
244+
# don't wrap child nodes of ConcatNode
245+
union_child_nodes = set(
246+
itertools.chain.from_iterable(
247+
node.child_nodes
248+
for node in root.unique_nodes()
249+
if isinstance(node, nodes.ConcatNode)
250+
)
251+
)
243252

244253
def maybe_insert_cte_marker(node: nodes.BigFrameNode) -> nodes.BigFrameNode:
245254
if node == root:
246255
return node
247-
if isinstance(node, _LOGICAL_NODE_TYPES_TO_WRAP) and node not in wrapped_nodes:
256+
if (
257+
isinstance(node, _LOGICAL_NODE_TYPES_TO_WRAP)
258+
and node not in wrapped_nodes
259+
and node not in union_child_nodes
260+
):
248261
wrapped_nodes.add(node)
249262
return nodes.CteNode(node)
250263
return node

tests/unit/core/compile/sqlglot/snapshots/test_compile_concat/test_compile_concat/out.sql

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,6 @@ WITH `bfcte_0` AS (
66
`string_col` AS `bfcol_6`
77
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types` AS `bft_0`
88
), `bfcte_1` AS (
9-
SELECT
10-
`bfcol_3` AS `bfcol_11`,
11-
`bfcol_4` AS `bfcol_12`,
12-
`bfcol_5` AS `bfcol_13`,
13-
`bfcol_6` AS `bfcol_14`,
14-
1 AS `bfcol_15`,
15-
ROW_NUMBER() OVER () - 1 AS `bfcol_16`
16-
FROM `bfcte_0`
17-
), `bfcte_2` AS (
18-
SELECT
19-
`bfcol_3` AS `bfcol_17`,
20-
`bfcol_4` AS `bfcol_18`,
21-
`bfcol_5` AS `bfcol_19`,
22-
`bfcol_6` AS `bfcol_20`,
23-
0 AS `bfcol_21`,
24-
ROW_NUMBER() OVER () - 1 AS `bfcol_22`
25-
FROM `bfcte_0`
26-
), `bfcte_3` AS (
279
SELECT
2810
`bfcol_17` AS `bfcol_23`,
2911
`bfcol_18` AS `bfcol_24`,
@@ -34,14 +16,24 @@ WITH `bfcte_0` AS (
3416
FROM (
3517
(
3618
SELECT
37-
*
38-
FROM `bfcte_2`
19+
`bfcol_3` AS `bfcol_17`,
20+
`bfcol_4` AS `bfcol_18`,
21+
`bfcol_5` AS `bfcol_19`,
22+
`bfcol_6` AS `bfcol_20`,
23+
0 AS `bfcol_21`,
24+
ROW_NUMBER() OVER () - 1 AS `bfcol_22`
25+
FROM `bfcte_0`
3926
)
4027
UNION ALL
4128
(
4229
SELECT
43-
*
44-
FROM `bfcte_1`
30+
`bfcol_3` AS `bfcol_11`,
31+
`bfcol_4` AS `bfcol_12`,
32+
`bfcol_5` AS `bfcol_13`,
33+
`bfcol_6` AS `bfcol_14`,
34+
1 AS `bfcol_15`,
35+
ROW_NUMBER() OVER () - 1 AS `bfcol_16`
36+
FROM `bfcte_0`
4537
)
4638
)
4739
)
@@ -50,7 +42,7 @@ SELECT
5042
`bfcol_24` AS `rowindex_1`,
5143
`bfcol_25` AS `int64_col`,
5244
`bfcol_26` AS `string_col`
53-
FROM `bfcte_3`
45+
FROM `bfcte_1`
5446
ORDER BY
5547
`bfcol_27` ASC NULLS LAST,
5648
`bfcol_28` ASC NULLS LAST

tests/unit/core/compile/sqlglot/snapshots/test_compile_concat/test_compile_concat_filter_sorted/out.sql

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,6 @@ WITH `bfcte_0` AS (
1111
`int64_col` AS `bfcol_6`
1212
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types` AS `bft_0`
1313
), `bfcte_2` AS (
14-
SELECT
15-
`bfcol_7` AS `bfcol_25`,
16-
`bfcol_8` AS `bfcol_26`,
17-
3 AS `bfcol_27`,
18-
ROW_NUMBER() OVER () - 1 AS `bfcol_28`
19-
FROM `bfcte_0`
20-
), `bfcte_3` AS (
21-
SELECT
22-
`bfcol_7` AS `bfcol_29`,
23-
`bfcol_8` AS `bfcol_30`,
24-
1 AS `bfcol_31`,
25-
ROW_NUMBER() OVER () - 1 AS `bfcol_32`
26-
FROM `bfcte_0`
27-
), `bfcte_4` AS (
28-
SELECT
29-
`bfcol_5` AS `bfcol_17`,
30-
`bfcol_6` AS `bfcol_18`,
31-
2 AS `bfcol_19`,
32-
ROW_NUMBER() OVER (ORDER BY `bfcol_6` ASC NULLS LAST) - 1 AS `bfcol_20`
33-
FROM `bfcte_1`
34-
), `bfcte_5` AS (
35-
SELECT
36-
`bfcol_5` AS `bfcol_21`,
37-
`bfcol_6` AS `bfcol_22`,
38-
0 AS `bfcol_23`,
39-
ROW_NUMBER() OVER (ORDER BY `bfcol_6` ASC NULLS LAST) - 1 AS `bfcol_24`
40-
FROM `bfcte_1`
41-
), `bfcte_6` AS (
4214
SELECT
4315
`bfcol_21` AS `bfcol_33`,
4416
`bfcol_22` AS `bfcol_34`,
@@ -47,33 +19,45 @@ WITH `bfcte_0` AS (
4719
FROM (
4820
(
4921
SELECT
50-
*
51-
FROM `bfcte_5`
22+
`bfcol_5` AS `bfcol_21`,
23+
`bfcol_6` AS `bfcol_22`,
24+
0 AS `bfcol_23`,
25+
ROW_NUMBER() OVER (ORDER BY `bfcol_6` ASC NULLS LAST) - 1 AS `bfcol_24`
26+
FROM `bfcte_1`
5227
)
5328
UNION ALL
5429
(
5530
SELECT
56-
*
57-
FROM `bfcte_3`
31+
`bfcol_7` AS `bfcol_29`,
32+
`bfcol_8` AS `bfcol_30`,
33+
1 AS `bfcol_31`,
34+
ROW_NUMBER() OVER () - 1 AS `bfcol_32`
35+
FROM `bfcte_0`
5836
)
5937
UNION ALL
6038
(
6139
SELECT
62-
*
63-
FROM `bfcte_4`
40+
`bfcol_5` AS `bfcol_17`,
41+
`bfcol_6` AS `bfcol_18`,
42+
2 AS `bfcol_19`,
43+
ROW_NUMBER() OVER (ORDER BY `bfcol_6` ASC NULLS LAST) - 1 AS `bfcol_20`
44+
FROM `bfcte_1`
6445
)
6546
UNION ALL
6647
(
6748
SELECT
68-
*
69-
FROM `bfcte_2`
49+
`bfcol_7` AS `bfcol_25`,
50+
`bfcol_8` AS `bfcol_26`,
51+
3 AS `bfcol_27`,
52+
ROW_NUMBER() OVER () - 1 AS `bfcol_28`
53+
FROM `bfcte_0`
7054
)
7155
)
7256
)
7357
SELECT
7458
`bfcol_33` AS `float64_col`,
7559
`bfcol_34` AS `int64_col`
76-
FROM `bfcte_6`
60+
FROM `bfcte_2`
7761
ORDER BY
7862
`bfcol_35` ASC NULLS LAST,
7963
`bfcol_36` ASC NULLS LAST

0 commit comments

Comments
 (0)