Fix aggregate_functions_null_for_empty for non-Nullable result types#99839
Merged
alexey-milovidov merged 2 commits intomasterfrom Mar 18, 2026
Merged
Conversation
…Nullable types Some aggregate functions (e.g., `groupArray`, `sumMap`) return types like Array or Map that cannot be inside Nullable. The `aggregate_functions_null_for_empty` setting was unconditionally wrapping all result types in Nullable via the OrNull combinator, causing an exception. Now the OrNull combinator checks `canBeInsideNullable` before wrapping, and skips the Nullable wrapper for incompatible types, returning the default value (e.g., empty array) instead. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Contributor
|
Workflow [PR], commit [778f908] Summary: ✅ AI ReviewSummaryThis PR fixes Missing context
ClickHouse Rules
Final Verdict
|
…hrowing The `OrNull` combinator now skips `Nullable` wrapping for types that cannot be inside `Nullable`, so `topKOrNullState` no longer throws `ILLEGAL_TYPE_OF_ARGUMENT`. https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=99839&sha=7b20176882cb107649b50cc440adc8898e64a3c0&name_0=PR&name_1=Fast%20test Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Member
Author
alexey-milovidov
left a comment
There was a problem hiding this comment.
Good change, good test.
qoega
approved these changes
Mar 18, 2026
Contributor
LLVM Coverage Report
PR changed lines: PR changed-lines coverage: 90.00% (18/20, 0 noise lines excluded) |
alexey-milovidov
added a commit
that referenced
this pull request
Mar 22, 2026
The `transform` function casts WHEN values to the expression type. When WHEN values are Nullable (e.g. `CAST(NULL AS Nullable(Int32))`) but the expression is non-Nullable, the cast fails with "Cannot convert NULL value to non-Nullable type". This became visible after #99839 made `aggregate_functions_null_for_empty` produce Nullable result types for more aggregates, causing SQLLogic tests to hit this path. Fix by wrapping the entire `transform` attempt (array construction, build, and execute) in a try-catch that falls through to the `multiIf` path on type-related errors. The `multiIf` path handles Nullable WHEN values correctly via `caseWhenEquals`. https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=100293&sha=f067c3bec9fe4a7a07e810a27c9587ea9f777bfb&name_0=PR&name_1=SQLLogic%20test Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
alexey-milovidov
added a commit
that referenced
this pull request
Mar 22, 2026
The `transform` function casts WHEN values to the expression type. When WHEN values are Nullable (e.g. `CAST(NULL AS Nullable(Int32))`) but the expression is non-Nullable, the cast fails with "Cannot convert NULL value to non-Nullable type". This became visible after #99839 made `aggregate_functions_null_for_empty` produce Nullable result types for more aggregates, causing SQLLogic tests to hit this path. Fix by wrapping the entire `transform` attempt (array construction, build, and execute) in a try-catch that falls through to the `multiIf` path on type-related errors. The `multiIf` path handles Nullable WHEN values correctly via `caseWhenEquals`. https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=100293&sha=f067c3bec9fe4a7a07e810a27c9587ea9f777bfb&name_0=PR&name_1=SQLLogic%20test Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
alexey-milovidov
added a commit
that referenced
this pull request
Mar 24, 2026
The `transform` function casts WHEN values to the expression type. When WHEN values are Nullable (e.g. `CAST(NULL AS Nullable(Int32))`) but the expression is non-Nullable, the cast fails with "Cannot convert NULL value to non-Nullable type". This became visible after #99839 made `aggregate_functions_null_for_empty` produce Nullable result types for more aggregates, causing SQLLogic tests to hit this path. Fix by wrapping the entire `transform` attempt (array construction, build, and execute) in a try-catch that falls through to the `multiIf` path on type-related errors. The `multiIf` path handles Nullable WHEN values correctly via `caseWhenEquals`. https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=100293&sha=f067c3bec9fe4a7a07e810a27c9587ea9f777bfb&name_0=PR&name_1=SQLLogic%20test Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Desel72
pushed a commit
to Desel72/ClickHouse
that referenced
this pull request
Mar 30, 2026
The `transform` function casts WHEN values to the expression type. When WHEN values are Nullable (e.g. `CAST(NULL AS Nullable(Int32))`) but the expression is non-Nullable, the cast fails with "Cannot convert NULL value to non-Nullable type". This became visible after ClickHouse#99839 made `aggregate_functions_null_for_empty` produce Nullable result types for more aggregates, causing SQLLogic tests to hit this path. Fix by wrapping the entire `transform` attempt (array construction, build, and execute) in a try-catch that falls through to the `multiIf` path on type-related errors. The `multiIf` path handles Nullable WHEN values correctly via `caseWhenEquals`. https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=100293&sha=f067c3bec9fe4a7a07e810a27c9587ea9f777bfb&name_0=PR&name_1=SQLLogic%20test
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.
Closes #38738. Closes #38786.
Some aggregate functions (e.g.,
groupArray,sumMap) return types likeArrayorMapthat cannot be insideNullable. Theaggregate_functions_null_for_emptysetting was unconditionally wrapping all result types inNullablevia theOrNullcombinator, causing an exception like:Now the
OrNullcombinator checkscanBeInsideNullablebefore wrapping, and skips theNullablewrapper for incompatible types, returning the default value (e.g., empty array) instead.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix
aggregate_functions_null_for_emptysetting to work with aggregate functions returning non-Nullable types such asArrayorMap(e.g.,groupArray,sumMap).Documentation entry for user-facing changes