Skip to content

Commit c53c86f

Browse files
authored
chore: add compile_filter (#1817)
1 parent ef4948b commit c53c86f

File tree

5 files changed

+74
-0
lines changed

5 files changed

+74
-0
lines changed

bigframes/core/compile/sqlglot/compiler.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,13 @@ def compile_projection(
211211
)
212212
return child.project(projected_cols)
213213

214+
@_compile_node.register
215+
def compile_filter(
216+
self, node: nodes.FilterNode, child: ir.SQLGlotIR
217+
) -> ir.SQLGlotIR:
218+
condition = scalar_compiler.compile_scalar_expression(node.predicate)
219+
return child.filter(condition)
220+
214221
@_compile_node.register
215222
def compile_concat(
216223
self, node: nodes.ConcatNode, *children: ir.SQLGlotIR

bigframes/core/compile/sqlglot/scalar_compiler.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,10 @@ def compile_addop(op: ops.AddOp, left: TypedExpr, right: TypedExpr) -> sge.Expre
9999

100100
# Numerical addition
101101
return sge.Add(this=left.expr, expression=right.expr)
102+
103+
104+
def compile_ge(
105+
op: ops.ge_op, left: TypedExpr, right: TypedExpr # type: ignore[valid-type]
106+
) -> sge.Expression:
107+
108+
return sge.GTE(this=left.expr, expression=right.expr)

bigframes/core/compile/sqlglot/sqlglot_ir.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,16 @@ def project(
250250
new_expr = self._encapsulate_as_cte().select(*projected_cols_expr, append=True)
251251
return SQLGlotIR(expr=new_expr, uid_gen=self.uid_gen)
252252

253+
def filter(
254+
self,
255+
condition: sge.Expression,
256+
) -> SQLGlotIR:
257+
"""Filters the query with the given condition."""
258+
new_expr = self._encapsulate_as_cte()
259+
return SQLGlotIR(
260+
expr=new_expr.where(condition, append=False), uid_gen=self.uid_gen
261+
)
262+
253263
def insert(
254264
self,
255265
destination: bigquery.TableReference,
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
WITH `bfcte_0` AS (
2+
SELECT
3+
`int64_col` AS `bfcol_0`,
4+
`rowindex` AS `bfcol_1`
5+
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
6+
), `bfcte_1` AS (
7+
SELECT
8+
*,
9+
`bfcol_1` AS `bfcol_5`,
10+
`bfcol_1` AS `bfcol_6`,
11+
`bfcol_0` AS `bfcol_7`,
12+
`bfcol_1` >= 1 AS `bfcol_8`
13+
FROM `bfcte_0`
14+
), `bfcte_2` AS (
15+
SELECT
16+
*
17+
FROM `bfcte_1`
18+
WHERE
19+
`bfcol_8`
20+
)
21+
SELECT
22+
`bfcol_5` AS `rowindex`,
23+
`bfcol_6` AS `rowindex_1`,
24+
`bfcol_7` AS `int64_col`
25+
FROM `bfcte_2`
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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_filter(scalars_types_df: bpd.DataFrame, snapshot):
23+
bf_df = scalars_types_df[["rowindex", "int64_col"]]
24+
bf_filter = bf_df[bf_df["rowindex"] >= 1]
25+
snapshot.assert_match(bf_filter.sql, "out.sql")

0 commit comments

Comments
 (0)