Repository navigation
Fix FunctionDynamicAdaptor exception when nested function returns Nothing type - #115982
alexey-milovidov merged 5 commits into
Conversation
…hing type
When a function applied to a Dynamic column returns a result of the bare
Nothing type (e.g. arrayElement on an empty array literal), the adaptor
reached code that assumes the result type is a storable variant.
Nothing is the only empty type that onlyNull() does not report:
DataTypeNothing does not override IDataType::onlyNull(), so it inherits
false, while DataTypeNullable::onlyNull() is true for Nullable(Nothing).
The guard for the empty-result case tested onlyNull() alone at all three
execution branches, so a Nothing nested result slipped past. Then
ColumnDynamic::addNewVariant returns true while the DataTypeVariant
constructor silently drops Nothing, so the following
variant_name_to_discriminator.at("Nothing") threw std::out_of_range
(branch 2, the frame reported by the fuzzer). The other two branches
reach castColumn instead and throw "Variant type should have at least
one nested type".
Add isNothing checks alongside the existing onlyNull checks in all three
execution paths (single variant no NULLs, single variant with NULLs,
multiple variants) so that a Nothing result is treated as NULLs and
defaults. A column of type Nothing cannot hold a non-NULL value, so no
query that succeeds today can enter the new branch.
This restores symmetry with the sibling FunctionVariantAdaptor, where the
identical bug was fixed in 4c570d9 with
the same trigger at the same three sites.
Each of the three sites was verified independently load-bearing: reverting
one isNothing term reproduces that branch's own failure at its own frame,
and restoring it clears it. LowCardinality-wrapped shapes were probed and
are unaffected in both directions, so the guard was not widened for them.
Found by the AST fuzzer:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=104591&sha=6daffee939f67c63ec50c31023f7b996cddbc5b1&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%2C%20old_compatibility%29
Internal second-model reviewAn independent cold review of this diff plus a second-model review pass returned no blockers and no majors. Findings below.
Checks that came back clean:
|
Pre-PR validation gate
100 randomized-settings runs of the new test pass (two independent 50-run sweeps). |
|
Workflow [PR], commit [722dcf9] Summary: ✅
AI ReviewSummaryThis PR fixes Final Verdict
LLVM Coverage ReportMeasured on commit 722dcf9.
Changed lines: Changed C/C++ lines covered: 20/23 (86.96%) · Uncovered code |
|
cc @Ergus @KochetovNicolai, could you review this? |
Build profile diff (arm_release)Commit See the job log for details. |
CI finish ledger - 5ea684fEvery failure below has an owner: a fixing PR (ours or external), or a full-effort fix task
The abort is at Note: a second Session id: cron:our-pr-ci-monitor:20260823-110000 |
|
cc @Avogar easy to review |
| SELECT arrayElement([], d) FROM (SELECT if(number % 2, CAST(number, 'Dynamic'), CAST(NULL, 'Dynamic')) AS d FROM numbers(4)); | ||
|
|
||
| -- Several stored types. | ||
| SELECT arrayElement([], d) FROM (SELECT if(number % 2, CAST(toInt64(number), 'Dynamic'), CAST(toUInt8(number), 'Dynamic')) AS d FROM numbers(4)); |
There was a problem hiding this comment.
no test exercises branch 3 with a mix of Nothing and non-Nothing variant results, the shape where the new nullptr slot has to interleave with a real result column.
There was a problem hiding this comment.
Correct. Every existing branch-3 arm had all variants return Nothing, so variants_results was uniformly empty and a misaligned insert against a real column would have passed. Added your arm in 4347c63.
Measured both directions on a debug build:
- parent commit 5ea684f:
Code: 36 ... Variant type should have at least one nested type - with the fix:
0 7 / 1 \N / 2 7 / 3 \N
It also gives that branch its first coverage of the mixed shape. The fuzzer signature this PR started from only ever hit the second branch.
|
|
||
| -- Several stored types. | ||
| SELECT arrayElement([], d) FROM (SELECT if(number % 2, CAST(toInt64(number), 'Dynamic'), CAST(toUInt8(number), 'Dynamic')) AS d FROM numbers(4)); | ||
|
|
There was a problem hiding this comment.
Suggested:
-- Some variants produce Nothing while others produce a real type.
SELECT number, arrayElement(d, 1)
FROM (SELECT number, if(number % 2, CAST([], 'Dynamic'), CAST([7, 8], 'Dynamic')) AS d FROM numbers(4))
ORDER BY number;
Expected under the fix: 0 7 / 1 \N / 2 7 / 3 \N.
There was a problem hiding this comment.
Used verbatim, and the result matches your prediction exactly. It sits after the arrayElement(d, 1) group in 4347c63, since it needs the same CAST([], 'Dynamic') setup.
| SELECT arrayElement([10, 20], d) FROM (SELECT CAST(2, 'Dynamic') AS d); | ||
|
|
||
| -- An argument that cannot index an array is still rejected. | ||
| SELECT arrayElement([], d) FROM (SELECT CAST('s', 'Dynamic') AS d); -- { serverError ILLEGAL_TYPE_OF_ARGUMENT } |
There was a problem hiding this comment.
the { serverError ILLEGAL_TYPE_OF_ARGUMENT } control silently degrades to a \N row if dynamic_throw_on_type_mismatch is ever 0; pin it inline.
There was a problem hiding this comment.
Pinned inline in 4347c63.
Your premise checks out on the same query: with dynamic_throw_on_type_mismatch = 0 it returns \N instead of throwing, so unpinned that arm can stop asserting rejection entirely. Nothing in CI flips it today (absent from the clickhouse-test randomizer and from tests/config/, and true throughout SettingsChangesHistory), so the pin is protecting against a future default or profile change rather than a live break.
| @@ -0,0 +1,30 @@ | |||
| SET allow_suspicious_types_in_order_by = 1; | |||
There was a problem hiding this comment.
Why we need this if this test has no ORDER BY or GROUP BY
There was a problem hiding this comment.
No reason. Removed in 4347c63.
It came in from the sibling test 03918_variant_adaptor_nothing_type, whose ORDER BY arr is over a Variant column and does need it. Nothing here sorts or groups by a Dynamic or Variant expression, so the setting was dead.
| if (nested_result_type->onlyNull()) | ||
| /// If result is Nullable(Nothing) or Nothing, just return column filled with NULLs/defaults. | ||
| /// Nothing can appear when the function is executed on an empty type (e.g. arrayElement on Array(Nothing)). | ||
| if (nested_result_type->onlyNull() || isNothing(nested_result_type)) |
There was a problem hiding this comment.
onlyNull() || isNothing(...) is hand-written at six sites across two files; a named helper would make the next adaptor correct by construction.
There was a problem hiding this comment.
Agreed. Added isNothingOrNullableNothing() in IDataType.h beside isNullableOrLowCardinalityNullable, and used it at all six adaptor sites, in 4347c63.
One correction to the count while I was at it: the predicate is hand-written at eight sites, not six. FunctionsComparison.h:1270-1271 carries the same pair for tuple elements. I left those two alone, since a header included by every comparison function is unrelated churn inside a bug fix, but they take the same DataTypePtr and would substitute cleanly. Happy to fold them in here or in a separate PR if you prefer either.
…ng, name the guard predicate Branch-3 coverage only had shapes where every variant result is Nothing, so a nullptr slot never had to line up against a real result column. Add an arm for the mixed shape. It fails on the parent commit with "Code: 36 Variant type should have at least one nested type" and returns 0 7 / 1 \N / 2 7 / 3 \N with the fix. Pin dynamic_throw_on_type_mismatch = 1 on the ILLEGAL_TYPE_OF_ARGUMENT arm. Measured: with 0 the same query returns \N instead of throwing, so without the pin that arm can stop asserting rejection. Drop SET allow_suspicious_types_in_order_by = 1. It was inherited from the sibling test 03918, whose ORDER BY is over a Variant column; this test orders by a UInt64 only, so the setting was never needed. Replace the hand-written onlyNull() || isNothing(...) predicate with a named isNothingOrNullableNothing() beside isNullableOrLowCardinalityNullable in IDataType.h, and use it at all six adaptor sites across FunctionDynamicAdaptor.cpp and FunctionVariantAdaptor.cpp, so the next adaptor gets the bare-Nothing half by construction instead of by memory.
CI finish ledger - 4347c63Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task whose
Root cause, measured: #118606 ("Proper partition pruning with dates", merged 13:52:27Z) broke Session id: cron:our-pr-ci-monitor:20260910-190047 |
…fication (fixes 05141_time_series_select_join_settings)
CI finish ledger - 8358af7Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Cause and fix, measured rather than inferred: #117356 ("Unify the spill trigger for hash joins") Rule 3c, not 3d, decided at the FAILING head rather than at the live one: the failing legs' server Session id: cron:our-pr-ci-monitor:20260911-020045 |
CI finish ledger - 2af3ed7Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Not caused by this pull request. Session id: cron:our-pr-ci-monitor:20260911-093124 |
Refresh CI; no conflicts. Co-Authored-By: Claude Opus 5.5 <[email protected]>
No related open issue found. Prior art for the same defect on the sibling class:
Related: 4c570d9
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed an exception when a function applied to a
Dynamiccolumn returns a result of theNothingtype, for examplearrayElementon an empty array literal. Such queries now returnNULLfor every row instead of failing withstd::out_of_rangeorVariant type should have at least one nested type.Description
Found by the AST fuzzer on master (
AST fuzzer (amd_debug, targeted, old_compatibility)), 2 occurrences on unrelated PRs 9 days apart, neither of which touchessrc/Functions/:arrayElement([], json.c))Root cause.
Nothingis the only empty type not reported byIDataType::onlyNull():DataTypeNothingdoes not override it, so it inheritsfalse, whileDataTypeNullable::onlyNull()is true forNullable(Nothing).ExecutableFunctionDynamicAdaptor::executeImplguarded the empty-result case withonlyNull()alone at all three of its execution branches, so a bareNothingnested result slipped past and reached code that assumes a storable variant type.ColumnDynamic::addNewVariantthen returnstruewhileDataTypeVariant's constructor silently dropsNothing, so the followingvariant_name_to_discriminator.at("Nothing")throwsstd::out_of_range(branch 2, the reported frame). The other two branches reachcastColumninstead and throwVariant type should have at least one nested type.Change. Add the missing
isNothinghalf to the existingonlyNull()guard at all three branches, so aNothingresult is treated likeNullable(Nothing). A column of typeNothingcannot hold a non-NULL value, so no currently-succeeding query can enter the new branch. This restores symmetry with the siblingFunctionVariantAdaptor, where the identical bug was fixed in4c570d929744406with the same trigger at the same three sites.Validation. Each of the three sites was verified independently load-bearing: reverting one
isNothingterm reproduces that branch's own failure at its own frame, and restoring it clears it. Queries that already worked are unchanged, and an argument that cannot index an array is still rejected. 100 randomized-settings runs of the new test pass.Workflow [PR]
Sync PR [sync-upstream/pr/115982]
Version info
26.10.1.2023-master(included in26.10and later)