Repository navigation
Fix logical error in tuple-vs-string comparison with nested Nullable elements (STID 3344-4a3c) - #104171
Conversation
…elements
When type-inferring a comparison like `Tuple(Tuple(Nullable(String))) <= String`,
the loop in `FunctionComparison::getReturnTypeImpl` only recurses for the both-
tuples case. For `tuple <= string` (and the symmetric form), it inspects only
the immediate element type, so a `Tuple(Nullable(...))` outer element is not
detected as nullable-producing. The result type is inferred as plain `UInt8`,
but the runtime — which parses the string as a tuple value and compares
element-wise — produces `Nullable(UInt8)`. The mismatch then trips the analyzer's
constant-folding type-match assertion at `src/Analyzer/Resolve/resolveFunction.cpp`,
manifesting as STID 3344-4a3c:
Logical error: Unexpected return type from lessOrEquals.
Expected UInt8. Got Const(Nullable(UInt8))
Fix: when one side is `String`/`FixedString`, recursively call `func->build`
with `(element_type, string_side_type)` to compute the per-element comparison
type. This mirrors the runtime semantics and correctly propagates `Nullable`
(as well as `Dynamic`/`Variant`/`Nothing`) through arbitrarily-nested tuples.
The fix is symmetric for both `tuple <= string` and `string <= tuple`.
Minimal reproducer (crashes the unfixed server):
SELECT tuple(tuple(toNullable('x'))) <= '((NULL))';
Same root-cause class as #1d24a248c32f (tuple comparison Nothing-type) and the
existing constant-folding fix in #c4b3d5221acf (Variant + LowCardinality NULL).
Source PR where the assertion was first caught:
ClickHouse#100300
Regression test: 04206_tuple_string_compare_nested_nullable_inference
Co-Authored-By: Claude Opus 4.7 <[email protected]>
Pre-PR validation gateSession:
|
|
cc @rschu1ze @KochetovNicolai — could you review this? It fixes a long-standing AST-fuzzer LOGICAL_ERROR (STID 3344-4a3c) where comparing a nested-Nullable tuple with a String literal ( |
|
Workflow [PR], commit [09244e1] Summary: ✅ AI ReviewSummaryThis PR fixes a real type-inference mismatch in ClickHouse Rules
Final VerdictStatus: ✅ Approve |
LLVM Coverage Report
Changed lines: 100.00% (8/8) · Uncovered code |
Fixes the AST fuzzer logical error tracked as STID 3344-4a3c:
First surfaced on #100300 (
AST fuzzer (amd_debug))and previously hit on #100035 with the same root cause but
a different operator (
equalsinstead oflessOrEquals).Motivation / approach
When comparing a
Tuple(...)with aStringliteral (e.g.tuple(tuple(toNullable('x'))) <= '((NULL))'), the type-inference loop inFunctionComparison::getReturnTypeImplonly recurses into per-elementcomparisons when both sides are tuples. For the
tuple <= stringcase(and the symmetric
string <= tuple), it inspects the immediate element typeonly — so a
Tuple(Nullable(...))outer element is not detected asnullable-producing and the comparison gets inferred as plain
UInt8.At runtime, however, the string is parsed into a tuple value matching the
structure of the tuple side and compared element-wise, which DOES propagate
Nullable. The actual result column isNullable(UInt8). The mismatch tripsthe analyzer's constant-folding type-match assertion at
src/Analyzer/Resolve/resolveFunction.cpp.The fix adds the missing recursion: when one side is
String/FixedString,we recursively call
func->build({element_type, string_side_type})to computethe per-element comparison type. This mirrors the runtime semantics and
correctly propagates
Nullable(as well asDynamic/Variant/Nothing)through arbitrarily-nested tuples. Symmetric for both
tuple <= stringandstring <= tuple.This is the same class of bug fixed in
1d24a248c32f (tuple comparison
Nothing-type) and adjacent to the constant-folding fix in
c4b3d5221acf (Variant +
LowCardinalityNULL).Reproduction / verification
Minimal repro on master HEAD (crashed the server,
LOGICAL_ERRORabort):After the fix:
The original fuzzer query from PR #100300 (the third
INTERSECT DISTINCTbranch, in particular) no longer crashes — it now returns a clean
ILLEGAL_TYPE_OF_ARGUMENTerror rejecting theor(... toInt256 ...)shape,which is the expected behaviour. Same for the PR #100035 query.
A regression test was added at
tests/queries/0_stateless/04206_tuple_string_compare_nested_nullable_inferencecovering:
Tuple(Tuple(Nullable(...))) <= StringTuple(Int, Tuple(Nullable(...), ...)) <= StringPREWHEREString <= Tuple(Tuple(Nullable(...)))(the symmetric path)Tuple(Tuple(Nullable(...))) <= Tuple(Tuple(Nullable(...)))(existingboth-tuples recursion path — must not regress)
Existing tests
04081_comparison_constant_folding_null_variant_lc,03925_tuple_comparison_nothing_type,00250_tuple_comparison,00406_tuples_with_nulls, and01329_compare_tuple_string_constantwereverified to still pass against the patched binary.
CI report where the failure was first caught:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=100300&sha=54d442c48654c5f60c620ecee424947caea7f2bf&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%29
Changelog category (leave one):
Changelog entry (a user-readable short description of the change):
Fix
LOGICAL_ERRORexception (Unexpected return type from <comparison>. ExpectedUInt8. GotConst(Nullable(UInt8))) when comparing a nestedTuple(Tuple(Nullable(...)))(or deeper nesting) with aStringliteral. The comparison's return type is now correctly inferred asNullable(UInt8)`, matching the runtime behaviour.Documentation entry for user-facing changes