Skip to content

test: improve tests coverage for the windows compiler #1997

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ def compile(
return UNARY_OP_REGISTRATION[op](op, column, window=window)


@UNARY_OP_REGISTRATION.register(agg_ops.CountOp)
def _(
op: agg_ops.CountOp,
column: typed_expr.TypedExpr,
window: typing.Optional[window_spec.WindowSpec] = None,
) -> sge.Expression:
return apply_window_if_present(sge.func("COUNT", column.expr), window)


@UNARY_OP_REGISTRATION.register(agg_ops.SumOp)
def _(
op: agg_ops.SumOp,
Expand Down
9 changes: 6 additions & 3 deletions bigframes/core/compile/sqlglot/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,9 @@ def compile_window(
this=is_observation_expr, expression=expr
)
is_observation = ir._cast(is_observation_expr, "INT64")
observation_count = windows.apply_window_if_present(
sge.func("SUM", is_observation), window_spec
)
else:
# Operations like count treat even NULLs as valid observations
# for the sake of min_periods notnull is just used to convert
Expand All @@ -344,10 +347,10 @@ def compile_window(
sge.Not(this=sge.Is(this=inputs[0], expression=sge.Null())),
"INT64",
)
observation_count = windows.apply_window_if_present(
sge.func("COUNT", is_observation), window_spec
)

observation_count = windows.apply_window_if_present(
sge.func("SUM", is_observation), window_spec
)
clauses.append(
(
observation_count < sge.convert(window_spec.min_periods),
Expand Down
12 changes: 7 additions & 5 deletions tests/system/small/engines/test_windowing.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,23 @@ def test_engines_with_offsets(
assert_equivalence_execution(result.node, REFERENCE_ENGINE, engine)


@pytest.mark.parametrize("never_skip_nulls", [True, False])
@pytest.mark.parametrize("agg_op", [agg_ops.sum_op, agg_ops.count_op])
def test_engines_with_rows_window(
scalars_array_value: array_value.ArrayValue,
bigquery_client: bigquery.Client,
never_skip_nulls,
agg_op,
):
window = window_spec.WindowSpec(
bounds=window_spec.RowsWindowBounds.from_window_size(3, "left"),
)
window_node = nodes.WindowOpNode(
child=scalars_array_value.node,
expression=expression.UnaryAggregation(
agg_ops.sum_op, expression.deref("int64_too")
),
expression=expression.UnaryAggregation(agg_op, expression.deref("int64_too")),
window_spec=window,
output_name=identifiers.ColumnId("sum_int64"),
never_skip_nulls=False,
output_name=identifiers.ColumnId("agg_int64"),
never_skip_nulls=never_skip_nulls,
skip_reproject_unsafe=False,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,14 @@ WITH `bfcte_0` AS (
SELECT
*,
CASE
WHEN SUM(CAST(NOT `bfcol_0` IS NULL AS INT64)) OVER (
WHEN COUNT(CAST(NOT `bfcol_0` IS NULL AS INT64)) OVER (
ORDER BY `bfcol_1` IS NULL ASC NULLS LAST, `bfcol_1` ASC NULLS LAST
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) < 3
ROWS BETWEEN 4 PRECEDING AND CURRENT ROW
) < 5
THEN NULL
ELSE COALESCE(
SUM(`bfcol_0`) OVER (
ORDER BY `bfcol_1` IS NULL ASC NULLS LAST, `bfcol_1` ASC NULLS LAST
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
),
0
ELSE COUNT(`bfcol_0`) OVER (
ORDER BY `bfcol_1` IS NULL ASC NULLS LAST, `bfcol_1` ASC NULLS LAST
ROWS BETWEEN 4 PRECEDING AND CURRENT ROW
)
END AS `bfcol_4`
FROM `bfcte_0`
Expand Down
17 changes: 10 additions & 7 deletions tests/unit/core/compile/sqlglot/test_compile_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,20 @@
)


def test_compile_window_w_rolling(scalar_types_df: bpd.DataFrame, snapshot):
def test_compile_window_w_skips_nulls_op(scalar_types_df: bpd.DataFrame, snapshot):
bf_df = scalar_types_df[["int64_col"]].sort_index()
# The SumOp's skips_nulls is True
result = bf_df.rolling(window=3).sum()
snapshot.assert_match(result.sql, "out.sql")


def test_compile_window_wo_skips_nulls_op(scalar_types_df: bpd.DataFrame, snapshot):
bf_df = scalar_types_df[["int64_col"]].sort_index()
# The CountOp's skips_nulls is False
result = bf_df.rolling(window=5).count()
snapshot.assert_match(result.sql, "out.sql")


def test_compile_window_w_groupby_rolling(scalar_types_df: bpd.DataFrame, snapshot):
bf_df = scalar_types_df[["bool_col", "int64_col"]].sort_index()
result = (
Expand All @@ -46,13 +54,8 @@ def test_compile_window_w_groupby_rolling(scalar_types_df: bpd.DataFrame, snapsh
snapshot.assert_match(result.sql, "out.sql")


def test_compile_window_w_min_periods(scalar_types_df: bpd.DataFrame, snapshot):
bf_df = scalar_types_df[["int64_col"]].sort_index()
result = bf_df.expanding(min_periods=3).sum()
snapshot.assert_match(result.sql, "out.sql")


def test_compile_window_w_range_rolling(compiler_session, snapshot):
# TODO: use `duration_col` instead.
values = np.arange(20)
pd_df = pd.DataFrame(
{
Expand Down