File tree Expand file tree Collapse file tree 5 files changed +17
-32
lines changed
bigframes/core/compile/sqlglot/aggregations
tests/unit/core/compile/sqlglot/aggregations
snapshots/test_unary_compiler Expand file tree Collapse file tree 5 files changed +17
-32
lines changed Original file line number Diff line number Diff line change @@ -53,7 +53,10 @@ def _(
5353 column : typed_expr .TypedExpr ,
5454 window : typing .Optional [window_spec .WindowSpec ] = None ,
5555) -> sge .Expression :
56- return apply_window_if_present (sge .func ("DENSE_RANK" ), window )
56+ # Ranking functions do not support window framing clauses.
57+ return apply_window_if_present (
58+ sge .func ("DENSE_RANK" ), window , include_framing_clauses = False
59+ )
5760
5861
5962@UNARY_OP_REGISTRATION .register (agg_ops .MaxOp )
@@ -121,7 +124,10 @@ def _(
121124 column : typed_expr .TypedExpr ,
122125 window : typing .Optional [window_spec .WindowSpec ] = None ,
123126) -> sge .Expression :
124- return apply_window_if_present (sge .func ("RANK" ), window )
127+ # Ranking functions do not support window framing clauses.
128+ return apply_window_if_present (
129+ sge .func ("RANK" ), window , include_framing_clauses = False
130+ )
125131
126132
127133@UNARY_OP_REGISTRATION .register (agg_ops .SumOp )
Original file line number Diff line number Diff line change 2525def apply_window_if_present (
2626 value : sge .Expression ,
2727 window : typing .Optional [window_spec .WindowSpec ] = None ,
28+ include_framing_clauses : bool = True ,
2829) -> sge .Expression :
2930 if window is None :
3031 return value
@@ -64,23 +65,7 @@ def apply_window_if_present(
6465 if not window .bounds and not order :
6566 return sge .Window (this = value , partition_by = group_by )
6667
67- # Ranking and navigation functions do not support window framing clauses.
68- # See: https://cloud.google.com/bigquery/docs/reference/standard-sql/analytic-function-concepts#window-frame-clause
69- no_frame_functions = {
70- "RANK" ,
71- "DENSE_RANK" ,
72- "NTILE" ,
73- "PERCENT_RANK" ,
74- "CUME_DIST" ,
75- "ROW_NUMBER" ,
76- "LAG" ,
77- "LEAD" ,
78- "FIRST_VALUE" ,
79- "LAST_VALUE" ,
80- "NTH_VALUE" ,
81- }
82- func_name = value .sql_name ().upper () if isinstance (value , sge .Func ) else ""
83- if not window .bounds and func_name in no_frame_functions :
68+ if not window .bounds and not include_framing_clauses :
8469 return sge .Window (this = value , partition_by = group_by , order = order )
8570
8671 kind = (
Original file line number Diff line number Diff line change 11WITH ` bfcte_0` AS (
22 SELECT
3- ` int64_col` AS ` bfcol_0` ,
4- ` rowindex` AS ` bfcol_1`
3+ ` int64_col` AS ` bfcol_0`
54 FROM ` bigframes-dev` .` sqlglot_test` .` scalar_types`
65), ` bfcte_1` AS (
76 SELECT
87 * ,
9- DENSE_RANK() OVER (ORDER BY ` bfcol_0` IS NULL ASC NULLS LAST, ` bfcol_0` ASC NULLS LAST) AS ` bfcol_4 `
8+ DENSE_RANK() OVER (ORDER BY ` bfcol_0` IS NULL ASC NULLS LAST, ` bfcol_0` ASC NULLS LAST) AS ` bfcol_1 `
109 FROM ` bfcte_0`
1110)
1211SELECT
13- ` bfcol_1` AS ` bfuid_col_1` ,
14- ` bfcol_0` AS ` int64_col` ,
15- ` bfcol_4` AS ` agg_int64`
12+ ` bfcol_1` AS ` agg_int64`
1613FROM ` bfcte_1`
Original file line number Diff line number Diff line change 11WITH ` bfcte_0` AS (
22 SELECT
3- ` int64_col` AS ` bfcol_0` ,
4- ` rowindex` AS ` bfcol_1`
3+ ` int64_col` AS ` bfcol_0`
54 FROM ` bigframes-dev` .` sqlglot_test` .` scalar_types`
65), ` bfcte_1` AS (
76 SELECT
87 * ,
9- RANK() OVER (ORDER BY ` bfcol_0` IS NULL ASC NULLS LAST, ` bfcol_0` ASC NULLS LAST) AS ` bfcol_4 `
8+ RANK() OVER (ORDER BY ` bfcol_0` IS NULL ASC NULLS LAST, ` bfcol_0` ASC NULLS LAST) AS ` bfcol_1 `
109 FROM ` bfcte_0`
1110)
1211SELECT
13- ` bfcol_1` AS ` bfuid_col_1` ,
14- ` bfcol_0` AS ` int64_col` ,
15- ` bfcol_4` AS ` agg_int64`
12+ ` bfcol_1` AS ` agg_int64`
1613FROM ` bfcte_1`
Original file line number Diff line number Diff line change @@ -57,7 +57,7 @@ def _apply_unary_window_op(
5757 window_spec = window_spec ,
5858 output_name = identifiers .ColumnId (new_name ),
5959 )
60- result = array_value .ArrayValue (win_node )
60+ result = array_value .ArrayValue (win_node ). select_columns ([ new_name ])
6161
6262 sql = result .session ._executor .to_sql (result , enable_cache = False )
6363 return sql
You can’t perform that action at this time.
0 commit comments