fix(snowflake): rewrite COUNT(*) FILTER to COUNT_IF [CLAUDE]#7900
Merged
geooo109 merged 1 commit intoJul 20, 2026
Conversation
filter_sql rewrites aggregate FILTER into a conditional argument by wrapping the aggregate's input in IFF(cond, input, NULL). For COUNT(*) that input is a star, producing COUNT(IFF(cond, *, NULL)) -- a star is not a valid IFF argument, so Snowflake rejects it. COUNT(*) FILTER (WHERE cond) counts qualifying rows, which is exactly Snowflake's native COUNT_IF(cond); emit that instead. Other aggregates and COUNT(expr)/COUNT(DISTINCT ...) are unaffected. This completes #7884, which handled the DISTINCT/ORDER BY argument shapes but not the star. Co-authored-by AI: Claude (Claude Code). [CLAUDE]
geooo109
reviewed
Jul 20, 2026
Comment on lines
+369
to
+370
| self.validate_all( | ||
| "SELECT COUNT(*) FILTER (WHERE b > 0) FROM t", |
Collaborator
There was a problem hiding this comment.
Actual the current approach works
snowflake output: SELECT COUNT(IFF(b > 0, *, NULL)) FROM t
WITH t(b) AS (
SELECT * FROM VALUES (1), (2)
)
SELECT COUNT(IFF(b > 0, *, NULL)) FROM t;
> 2
WITH t(b, c) AS (
SELECT * FROM VALUES (1, 'x'), (-1, 'x'), (5, 'y'), (0, 'y')
)
SELECT COUNT(IFF(b > 0, *, NULL)) FROM t;
* expands to 2 arguments and we get:
> SQL compilation error: error line 4 at position 13 too many arguments for function [IFF(T.B > 0, T.B, T.C, null)] expected 3, got 4
If t contains only 1 column.
geooo109
approved these changes
Jul 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
filter_sqlrewrites an aggregateFILTER (WHERE cond)into a conditionalargument by wrapping the aggregate's input in
IFF(cond, input, NULL). When theaggregate is
COUNT(*), that input is a star, so the output isCOUNT(IFF(cond, *, NULL))— a star isn't a validIFFargument and Snowflakerejects it.
COUNT(*) FILTER (WHERE cond)is just a count of qualifying rows, which isexactly Snowflake's native
COUNT_IF(cond), so emit that instead. Otheraggregates and
COUNT(expr)/COUNT(DISTINCT ...)are untouched.This completes #7884, which handled the
DISTINCT/ORDER BYargument shapesbut not the star case. Added a regression test alongside the existing
FILTER-rewrite cases in
test_duckdb.py;make unitandmake stylepass.This PR was authored by an AI coding agent (Claude Code) running on this account:
the AI found the bug, ran the repro, wrote the test, and wrote this description.
The human account holder reviews every change and is accountable for it. The
verification above is real and re-runnable from the diff. If this isn't the kind
of contribution you want, say so and I'll close it.