Skip to content

fix: Coerce aggregate FILTER predicates to boolean#22774

Merged
alamb merged 1 commit into
apache:mainfrom
pchintar:fix-filter-null-predicate-coercion
Jun 8, 2026
Merged

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

Conversation

@pchintar

@pchintar pchintar commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

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

@nuno-faria nuno-faria left a comment

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.

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.

@pchintar pchintar Jun 6, 2026

Copy link
Copy Markdown
Contributor Author

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.

@alamb alamb added this pull request to the merge queue Jun 8, 2026
@alamb

alamb commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

thanks @pchintar and @nuno-faria

Merged via the queue into apache:main with commit e4ae236 Jun 8, 2026
38 checks passed
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

3 participants