Skip to content
This repository was archived by the owner on Apr 1, 2026. It is now read-only.

Commit 46dcf29

Browse files
fix order by, window key issues
1 parent 920ff4a commit 46dcf29

File tree

25 files changed

+94
-73
lines changed
  • bigframes/core
  • tests/unit/core/compile/sqlglot
    • aggregations/snapshots/test_unary_compiler/test_qcut
    • snapshots
      • test_compile_aggregate
      • test_compile_concat
      • test_compile_explode
        • test_compile_explode_dataframe
        • test_compile_explode_series
      • test_compile_geo
      • test_compile_random_sample/test_compile_random_sample
      • test_compile_readlocal
        • test_compile_readlocal_w_json_df
        • test_compile_readlocal_w_lists_df
        • test_compile_readlocal_w_special_values
        • test_compile_readlocal_w_structs_df
        • test_compile_readlocal
      • test_compile_readtable
        • test_compile_readtable_w_limit
        • test_compile_readtable_w_ordering
      • test_compile_window
        • test_compile_window_w_groupby_rolling
        • test_compile_window_w_range_rolling
        • test_compile_window_w_skips_nulls_op
        • test_compile_window_wo_skips_nulls_op

25 files changed

+94
-73
lines changed

bigframes/core/compile/sqlglot/aggregations/windows.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,13 @@ def _get_window_bounds(
193193
def _compile_group_by_key(key: ex.Expression) -> sge.Expression:
194194
expr = expression_compiler.expression_compiler.compile_expression(key)
195195
# The group_by keys has been rewritten by bind_schema_to_node
196-
assert isinstance(key, ex.ResolvedDerefOp)
196+
assert key.is_scalar_expr and key.is_resolved
197197

198198
# Some types need to be converted to another type to enable groupby
199-
if key.dtype == dtypes.FLOAT_DTYPE:
199+
if key.output_type == dtypes.FLOAT_DTYPE:
200200
expr = sge.Cast(this=expr, to="STRING")
201-
elif key.dtype == dtypes.GEO_DTYPE:
201+
elif key.output_type == dtypes.GEO_DTYPE:
202202
expr = sge.func("ST_ASBINARY", expr)
203-
elif key.dtype == dtypes.JSON_DTYPE:
203+
elif key.output_type == dtypes.JSON_DTYPE:
204204
expr = sge.func("TO_JSON_STRING", expr)
205205
return expr

bigframes/core/compile/sqlglot/compiler.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ def _compile_result_node(root: nodes.ResultNode) -> str:
108108
root = typing.cast(nodes.ResultNode, schema_binding.bind_schema_to_tree(root))
109109

110110
sqlglot_ir = compile_node(rewrite.as_sql_nodes(root), uid_gen)
111+
print(sqlglot_ir.sql)
111112
return sqlglot_ir.sql
112113

113114

bigframes/core/rewrite/as_sql.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ def _as_sql_node(node: nodes.BigFrameNode) -> nodes.BigFrameNode:
202202
if node.order_by is not None:
203203
result = _sort(result, node.order_by.all_ordering_columns)
204204
result = _remap_select_cols(
205-
node.child,
205+
result,
206206
[
207207
nodes.AliasedRef(ref, identifiers.ColumnId(name))
208208
for ref, name in node.output_cols
Lines changed: 22 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,35 @@
1-
WITH `bfcte_0` AS (
2-
SELECT
3-
`int64_col`,
4-
`rowindex`
5-
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
6-
), `bfcte_1` AS (
7-
SELECT
8-
*,
9-
NOT `int64_col` IS NULL AS `bfcol_4`
10-
FROM `bfcte_0`
11-
), `bfcte_2` AS (
12-
SELECT
13-
*,
1+
SELECT
2+
`rowindex`,
3+
`int64_col`,
4+
IF(
5+
NOT `int64_col` IS NULL,
146
IF(
157
`int64_col` IS NULL,
168
NULL,
179
CAST(GREATEST(
18-
CEIL(PERCENT_RANK() OVER (PARTITION BY `bfcol_4` ORDER BY `int64_col` ASC) * 4) - 1,
10+
CEIL(
11+
PERCENT_RANK() OVER (PARTITION BY NOT `int64_col` IS NULL ORDER BY `int64_col` ASC) * 4
12+
) - 1,
1913
0
2014
) AS INT64)
21-
) AS `bfcol_5`
22-
FROM `bfcte_1`
23-
), `bfcte_3` AS (
24-
SELECT
25-
*,
26-
IF(`bfcol_4`, `bfcol_5`, NULL) AS `bfcol_6`
27-
FROM `bfcte_2`
28-
), `bfcte_4` AS (
29-
SELECT
30-
*,
31-
NOT `int64_col` IS NULL AS `bfcol_10`
32-
FROM `bfcte_3`
33-
), `bfcte_5` AS (
34-
SELECT
35-
*,
15+
),
16+
NULL
17+
) AS `qcut_w_int`,
18+
IF(
19+
NOT `int64_col` IS NULL,
3620
CASE
37-
WHEN PERCENT_RANK() OVER (PARTITION BY `bfcol_10` ORDER BY `int64_col` ASC) < 0
21+
WHEN PERCENT_RANK() OVER (PARTITION BY NOT `int64_col` IS NULL ORDER BY `int64_col` ASC) < 0
3822
THEN NULL
39-
WHEN PERCENT_RANK() OVER (PARTITION BY `bfcol_10` ORDER BY `int64_col` ASC) <= 0.25
23+
WHEN PERCENT_RANK() OVER (PARTITION BY NOT `int64_col` IS NULL ORDER BY `int64_col` ASC) <= 0.25
4024
THEN 0
41-
WHEN PERCENT_RANK() OVER (PARTITION BY `bfcol_10` ORDER BY `int64_col` ASC) <= 0.5
25+
WHEN PERCENT_RANK() OVER (PARTITION BY NOT `int64_col` IS NULL ORDER BY `int64_col` ASC) <= 0.5
4226
THEN 1
43-
WHEN PERCENT_RANK() OVER (PARTITION BY `bfcol_10` ORDER BY `int64_col` ASC) <= 0.75
27+
WHEN PERCENT_RANK() OVER (PARTITION BY NOT `int64_col` IS NULL ORDER BY `int64_col` ASC) <= 0.75
4428
THEN 2
45-
WHEN PERCENT_RANK() OVER (PARTITION BY `bfcol_10` ORDER BY `int64_col` ASC) <= 1
29+
WHEN PERCENT_RANK() OVER (PARTITION BY NOT `int64_col` IS NULL ORDER BY `int64_col` ASC) <= 1
4630
THEN 3
4731
ELSE NULL
48-
END AS `bfcol_11`
49-
FROM `bfcte_4`
50-
), `bfcte_6` AS (
51-
SELECT
52-
*,
53-
IF(`bfcol_10`, `bfcol_11`, NULL) AS `bfcol_12`
54-
FROM `bfcte_5`
55-
)
56-
SELECT
57-
`rowindex`,
58-
`int64_col`,
59-
`bfcol_6` AS `qcut_w_int`,
60-
`bfcol_12` AS `qcut_w_list`
61-
FROM `bfcte_6`
32+
END,
33+
NULL
34+
) AS `qcut_w_list`
35+
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@ WITH `bfcte_0` AS (
1818
SELECT
1919
`bfcol_3` AS `bool_col`,
2020
`bfcol_6` AS `int64_too`
21-
FROM `bfcte_1`
21+
FROM `bfcte_1`
22+
ORDER BY
23+
`bfcol_3` ASC NULLS LAST

tests/unit/core/compile/sqlglot/snapshots/test_compile_aggregate/test_compile_aggregate_wo_dropna/out.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@ WITH `bfcte_0` AS (
1616
SELECT
1717
`bfcol_3` AS `bool_col`,
1818
`bfcol_6` AS `int64_too`
19-
FROM `bfcte_1`
19+
FROM `bfcte_1`
20+
ORDER BY
21+
`bfcol_3` ASC NULLS LAST

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,7 @@ SELECT
3535
`bfcol_31` AS `rowindex_1`,
3636
`bfcol_32` AS `int64_col`,
3737
`bfcol_33` AS `string_col`
38-
FROM `bfcte_0`
38+
FROM `bfcte_0`
39+
ORDER BY
40+
`bfcol_34` ASC NULLS LAST,
41+
`bfcol_35` ASC NULLS LAST

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,7 @@ WITH `bfcte_0` AS (
4949
SELECT
5050
`bfcol_42` AS `float64_col`,
5151
`bfcol_43` AS `int64_col`
52-
FROM `bfcte_0`
52+
FROM `bfcte_0`
53+
ORDER BY
54+
`bfcol_44` ASC NULLS LAST,
55+
`bfcol_45` ASC NULLS LAST

tests/unit/core/compile/sqlglot/snapshots/test_compile_explode/test_compile_explode_dataframe/out.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@ SELECT
1616
`rowindex` AS `rowindex_1`,
1717
`int_list_col`,
1818
`string_list_col`
19-
FROM `bfcte_1`
19+
FROM `bfcte_1`
20+
ORDER BY
21+
`bfcol_7` ASC NULLS LAST

tests/unit/core/compile/sqlglot/snapshots/test_compile_explode/test_compile_explode_series/out.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@ WITH `bfcte_0` AS (
1313
SELECT
1414
`rowindex`,
1515
`int_list_col`
16-
FROM `bfcte_1`
16+
FROM `bfcte_1`
17+
ORDER BY
18+
`bfcol_4` ASC NULLS LAST

0 commit comments

Comments
 (0)