Skip to content

Commit 2544ee5

Browse files
committed
select some columns
1 parent 801da07 commit 2544ee5

File tree

5 files changed

+17
-32
lines changed

5 files changed

+17
-32
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff 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)

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

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
def 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 = (
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
WITH `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
)
1211
SELECT
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`
1613
FROM `bfcte_1`
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
WITH `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
)
1211
SELECT
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`
1613
FROM `bfcte_1`

tests/unit/core/compile/sqlglot/aggregations/test_unary_compiler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)