Skip to content

fix: Coerce aggregate FILTER predicates to boolean#22774

Open
pchintar wants to merge 1 commit into
apache:mainfrom
pchintar:fix-filter-null-predicate-coercion
Open

fix: Coerce aggregate FILTER predicates to boolean#22774
pchintar wants to merge 1 commit into
apache:mainfrom
pchintar:fix-filter-null-predicate-coercion

Conversation

@pchintar
Copy link
Copy Markdown

@pchintar pchintar commented Jun 5, 2026

Which issue does this PR close?

Rationale for this change

Aggregate and window aggregate FILTER clauses currently fail with an internal error when the filter condition is NULL, even though the equivalent boolean-typed expression (e.g. CAST(NULL AS BOOLEAN)) works correctly.

This occurs because FILTER predicates are not being coerced to BOOLEAN during type coercion.

What changes are included in this PR?

  • Coerce aggregate FILTER predicates to BOOLEAN during type coercion.
  • Apply the same coercion to window aggregate FILTER predicates.
  • Add SQL logic tests covering FILTER (WHERE NULL) for both aggregate and window aggregate functions.

Are these changes tested?

Yes.

Added SQL logic tests covering:

  • COUNT(*) FILTER (WHERE NULL)
  • COUNT(1) FILTER (WHERE NULL)
  • SUM(1) FILTER (WHERE NULL)
  • AVG(1) FILTER (WHERE NULL)
  • Window aggregate variants using FILTER (WHERE NULL)

Are there any user-facing changes?

Yes.

Queries using FILTER (WHERE NULL) no longer fail with an internal error and now return the expected results. Also, no changes were made to any public APIs

@github-actions github-actions Bot added optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt) labels Jun 5, 2026
Copy link
Copy Markdown
Contributor

@nuno-faria nuno-faria left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @pchintar, LGTM.

Just a small note: predicates like select count(*) filter (where 1) are also converted to bool. DuckDB follows the same logic, while Postgres returns an error. I'd say this is ok.

Comment on lines +6290 to +6302
# window_aggregate_with_untyped_null_filter
query I
SELECT count(*) FILTER (WHERE NULL) OVER ()
FROM (VALUES (1)) AS t(x)
----
0

query I
SELECT sum(1) FILTER (WHERE NULL) OVER ()
FROM (VALUES (1)) AS t(x)
----
NULL

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these be moved to window.slt? On the other hand there are already other window function tests at aggregate.slt, so I'm not sure what is the criteria.

Copy link
Copy Markdown
Author

@pchintar pchintar Jun 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @nuno-faria , and thanks for checking the predicate behavior against DuckDB/Postgres too.

I put these in aggregate.slt because the failure is in aggregate FILTER handling and there are already related aggregate/window aggregate cases here. So, Keeping the new cases alongside the existing aggregate FILTER coverage felt more consistent to me.

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

Labels

optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Aggregate FILTER fails on NULL predicate

2 participants