Skip to content

Commit 633cdb9

Browse files
committed
temporay changes
1 parent 24ae113 commit 633cdb9

File tree

3 files changed

+77
-6
lines changed

3 files changed

+77
-6
lines changed

bigframes/core/compile/sqlglot/compiler.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import dataclasses
1717
import functools
1818
import typing
19+
import itertools
1920

2021
from google.cloud import bigquery
2122
import sqlglot.expressions as sge
@@ -219,6 +220,37 @@ def compile_filter(
219220
condition = scalar_compiler.compile_scalar_expression(node.predicate)
220221
return child.filter(condition)
221222

223+
@_compile_node.register
224+
def compile_window(self, node: nodes.WindowOpNode, child: ir.SQLGlotIR) -> ir.SQLGlotIR:
225+
column_references: tuple[sge.Expression, ...] = tuple(
226+
scalar_compiler.compile_scalar_expression(expression.DerefOp(column))
227+
for column in expression.column_references
228+
)
229+
230+
# TODO: can_directly_window = not any(map(lambda x: is_window(x), used_exprs))
231+
# used_exprs = map(
232+
# scalar_compiler.compile_scalar_expression,
233+
# map(
234+
# expression.DerefOp,
235+
# itertools.chain(
236+
# node.expression.column_references, node.window_spec.all_referenced_columns
237+
# ),
238+
# ),
239+
# )
240+
# can_directly_window = False
241+
242+
window_spec = node.window_spec
243+
if node.expression.op.order_independent and window_spec.is_unbounded:
244+
# notably percentile_cont does not support ordering clause
245+
window_spec = window_spec.without_order()
246+
247+
return child.window(
248+
column_references = column_references,
249+
window_spec = node.window_spec,
250+
output_column = scalar_compiler.compile_scalar_expression(node.output_name),
251+
skip_nulls = node.expression.op.skips_nulls and not node.never_skip_nulls
252+
)
253+
222254
@_compile_node.register
223255
def compile_join(
224256
self, node: nodes.JoinNode, left: ir.SQLGlotIR, right: ir.SQLGlotIR

bigframes/core/compile/sqlglot/sqlglot_ir.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,9 @@
2525
import sqlglot.expressions as sge
2626

2727
from bigframes import dtypes
28-
from bigframes.core import guid, utils
28+
from bigframes.core import guid, utils, window_spec, local_data, schema
2929
from bigframes.core.compile.sqlglot.expressions import typed_expr
3030
import bigframes.core.compile.sqlglot.sqlglot_types as sgt
31-
import bigframes.core.local_data as local_data
32-
import bigframes.core.schema as bf_schema
3331

3432
# shapely.wkt.dumps was moved to shapely.io.to_wkt in 2.0.
3533
try:
@@ -68,7 +66,7 @@ def sql(self) -> str:
6866
def from_pyarrow(
6967
cls,
7068
pa_table: pa.Table,
71-
schema: bf_schema.ArraySchema,
69+
schema: schema.ArraySchema,
7270
uid_gen: guid.SequentialUIDGenerator,
7371
) -> SQLGlotIR:
7472
"""Builds SQLGlot expression from a pyarrow table.
@@ -293,13 +291,28 @@ def filter(
293291
expr=new_expr.where(condition, append=False), uid_gen=self.uid_gen
294292
)
295293

294+
def window(
295+
self,
296+
column_references: tuple[sge.Expression, ...],
297+
window_spec: window_spec.WindowSpec,
298+
output_name: sge.Expression,
299+
skip_nulls: bool,
300+
) -> SQLGlotIR:
301+
new_expr = _select_to_cte(
302+
self.expr,
303+
sge.to_identifier(
304+
next(self.uid_gen.get_uid_stream("bfcte_")), quoted=self.quoted
305+
),
306+
)
307+
308+
return self
309+
296310
def join(
297311
self,
298312
right: SQLGlotIR,
299313
join_type: typing.Literal["inner", "outer", "left", "right", "cross"],
300314
conditions: tuple[tuple[typed_expr.TypedExpr, typed_expr.TypedExpr], ...],
301-
*,
302-
joins_nulls: bool = True,
315+
joins_nulls: bool,
303316
) -> SQLGlotIR:
304317
"""Joins the current query with another SQLGlotIR instance."""
305318
left_cte_name = sge.to_identifier(
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import pytest
16+
17+
import bigframes.pandas as bpd
18+
19+
pytest.importorskip("pytest_snapshot")
20+
21+
22+
def test_compile_window(scalar_types_df: bpd.DataFrame, snapshot):
23+
24+
bf_df = scalar_types_df[["int64_col"]].sort_index()
25+
result = bf_df.diff()
26+
snapshot.assert_match(result.sql, "out.sql")

0 commit comments

Comments
 (0)