Skip to content

[GLUTEN-13831][VL] Honor FILTER clause in window aggregate functions - #13832

Open
WangGuangxin wants to merge 2 commits into
apache:mainfrom
WangGuangxin:fix-window-aggregate-filter
Open

WangGuangxin wants to merge 2 commits into
apache:mainfrom
WangGuangxin:fix-window-aggregate-filter

Conversation

@WangGuangxin

@WangGuangxin WangGuangxin commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

What changes are proposed in this pull request?

Spark 4.2 supports FILTER (WHERE ...) on window aggregate functions (SPARK-55702). In the Velox backend, genWindowFunctionsNode serializes only the aggregate function and its arguments and never passes AggregateExpression.filter on. Velox's WindowNode::Function has no mask either. As a result, a window aggregate such as COUNT(x) FILTER (WHERE p) OVER w runs over every row of the frame, and Gluten returns wrong results without any error.

This PR:

  • Adds the RewriteWindowAggregateFilter rewrite rule. For aggregates that ignore null inputs, it moves the predicate into the arguments: agg(x) FILTER (WHERE p) becomes agg(IF(p, x, NULL)), and the filter is cleared. PullOutPreProject then pulls the IF into the pre-project, so the window stays native.
    • Covered functions: COUNT, SUM, AVG, MIN, MAX, FIRST/LAST with ignoreNulls, the central-moment aggregates, covariance/correlation, and the bitwise aggregates.
    • A row whose predicate is false or null contributes only null inputs. That matches Spark's AggregateProcessor, which leaves the buffer unchanged for such rows.
  • Registers the rule in the Velox rewrite list after RewriteMultiChildrenCount.
  • Makes supportWindowExec reject any window aggregate that still has a filter after the rewrite, for example FIRST/LAST that respect nulls. These windows now fall back to vanilla Spark instead of dropping the predicate.

Minimal reproducer:

WITH src AS (SELECT id, id % 2 AS g FROM range(0, 12, 1, 4))
SELECT id, g, COUNT(id) FILTER (WHERE id % 4 = 0) OVER (PARTITION BY g) FROM src;
-- vanilla: 3 / 0 per group, Gluten before this patch: 6 / 6

Was this patch authored or co-authored using generative AI tooling?

Generated-by: Claude Opus 5.5 (Claude Code)

Related issue: #13831

Velox window functions have no per-function mask, and genWindowFunctionsNode
never serialized AggregateExpression.filter. A window aggregate such as
COUNT(x) FILTER (WHERE p) OVER w therefore ran over every row of the frame.

Add RewriteWindowAggregateFilter, which folds the predicate into the inputs of
null-ignoring aggregates: agg(x) FILTER (WHERE p) => agg(IF(p, x, NULL)). Rows
whose predicate is false or null then contribute only null inputs, matching
Spark's AggregateProcessor. Any FILTER that survives the rewrite now makes the
window fall back to vanilla Spark instead of being dropped silently.
@github-actions github-actions Bot added CORE works for Gluten Core VELOX labels Nov 28, 2026
@WangGuangxin
WangGuangxin force-pushed the fix-window-aggregate-filter branch from 2fe6a18 to 764c0c1 Compare November 28, 2026 11:31
@github-actions

Copy link
Copy Markdown

Run Gluten Clickhouse CI on x86

1 similar comment
@github-actions

Copy link
Copy Markdown

Run Gluten Clickhouse CI on x86

@github-actions

Copy link
Copy Markdown

Run Gluten Clickhouse CI on x86

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CORE works for Gluten Core VELOX

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant