Skip to content

Fix logical error in tuple-vs-string comparison with nested Nullable elements (STID 3344-4a3c) - #104171

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/fix-stid-3344-tuple-string-nested-nullable
May 6, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/fix-stid-3344-tuple-string-nested-nullable

Conversation

@groeneai

@groeneai groeneai commented May 6, 2026

Copy link
Copy Markdown
Collaborator

Fixes the AST fuzzer logical error tracked as STID 3344-4a3c:

Logical error: 'Unexpected return type from lessOrEquals.
Expected UInt8. Got Const(Nullable(UInt8))'

First surfaced on #100300 (AST fuzzer (amd_debug))
and previously hit on #100035 with the same root cause but
a different operator (equals instead of lessOrEquals).

Motivation / approach

When comparing a Tuple(...) with a String literal (e.g.
tuple(tuple(toNullable('x'))) <= '((NULL))'), the type-inference loop in
FunctionComparison::getReturnTypeImpl only recurses into per-element
comparisons when both sides are tuples. For the tuple <= string case
(and the symmetric string <= tuple), it inspects the immediate element type
only — so a Tuple(Nullable(...)) outer element is not detected as
nullable-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 is Nullable(UInt8). The mismatch trips
the 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 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. Symmetric for both tuple <= string and
string <= 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 +
LowCardinality NULL).

Reproduction / verification

Minimal repro on master HEAD (crashed the server, LOGICAL_ERROR abort):

SELECT tuple(tuple(toNullable('x'))) <= '((NULL))';

After the fix:

:) SELECT toTypeName(tuple(tuple(toNullable('x'))) <= '((NULL))');
Nullable(UInt8)
:) SELECT tuple(tuple(toNullable('x'))) <= '((NULL))';
\N

The original fuzzer query from PR #100300 (the third INTERSECT DISTINCT
branch, in particular) no longer crashes — it now returns a clean
ILLEGAL_TYPE_OF_ARGUMENT error rejecting the or(... 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_inference
covering:

  • Tuple(Tuple(Nullable(...))) <= String
  • Tuple(Int, Tuple(Nullable(...), ...)) <= String
  • The shape that surfaced the bug under PREWHERE
  • String <= Tuple(Tuple(Nullable(...))) (the symmetric path)
  • Tuple(Tuple(Nullable(...))) <= Tuple(Tuple(Nullable(...))) (existing
    both-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, and 01329_compare_tuple_string_constant were
verified 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):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the change):

Fix LOGICAL_ERROR exception (Unexpected return type from <comparison>. Expected UInt8. Got Const(Nullable(UInt8))) when comparing a nested Tuple(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

  • Documentation is unchanged (internal type-inference correctness fix; no user-facing API change)

…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]>
@groeneai

groeneai commented May 6, 2026

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate

Session: cron:clickhouse-ci-task-worker:20260506-031500

# Question Status
a Deterministic repro? YES — SELECT tuple(tuple(toNullable('x'))) <= '((NULL))'; aborts the unfixed server every time, passes deterministically with the fix.
b Root cause explained? YES — FunctionComparison::getReturnTypeImpl recurses into per-element comparison only when both sides are tuples. For tuple <= string, it inspects only the immediate element type. So a Tuple(Nullable(...)) outer-tuple element is not detected as nullable-producing, the result type is inferred as UInt8, but the runtime parses the string and produces Nullable(UInt8), tripping the constant-folding type-match assertion.
c Fix matches root cause? YES — Adds the missing recursion: when one side is String/FixedString, recursively call func->build({element_type, string_side_type}) to compute element-wise comparison type. Mirrors runtime semantics.
d Test intent preserved? / New tests added? YES — New regression test 04206_tuple_string_compare_nested_nullable_inference covers Tuple(Tuple(Nullable)) <= String, multi-level nesting, the symmetric String <= Tuple(Tuple(Nullable)) path, the PREWHERE-shape that triggered the original CI failure, and a sanity check on the existing both-tuples recursion path. Existing tests 04081, 03925, 00250_tuple_comparison, 00406_tuples_with_nulls, 01329_compare_tuple_string_constant all still pass against the patched binary.
e Both directions demonstrated? YES — Without fix: server LOGICAL_ERROR abort on the minimal repro (verified locally on master HEAD 3312c99db56e). With fix: minimal repro returns \N (Nullable(UInt8)), original PR #100300 fuzzer query returns ILLEGAL_TYPE_OF_ARGUMENT instead of crashing, server stays alive.
f Fix is general, not narrow? YES — Symmetric for both tuple <= string and string <= tuple. Recursion handles arbitrary nesting depth (not just depth-2). Goes through FunctionToOverloadResolverAdaptor::build → getReturnType → useDefaultImplementationForNulls/Nothing so it correctly handles Nullable/Dynamic/Variant/Nothing/LowCardinality(Nullable) via the existing infrastructure. Same pattern as the both-tuples branch.

@groeneai

groeneai commented May 6, 2026

Copy link
Copy Markdown
Collaborator Author

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 (Tuple(Tuple(Nullable(...))) <= String) had its return type incorrectly inferred as plain UInt8 because the per-element recursion in FunctionComparison::getReturnTypeImpl was only enabled in the both-tuples branch, not in the tuple-vs-string branch.

@Algunenano Algunenano added the can be tested Allows running workflows for external contributors label May 6, 2026
@clickhouse-gh

clickhouse-gh Bot commented May 6, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [09244e1]

Summary: ✅


AI Review

Summary

This PR fixes a real type-inference mismatch in FunctionComparison::getReturnTypeImpl for nested Tuple vs String comparisons by adding recursive element-level inference in the tuple-string path. The change is small, symmetric (Tuple <= String and String <= Tuple), and covered by a focused regression test; I did not find correctness, safety, or performance issues that should block merge.

ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ✅
Compilation time ✅
No large/binary files ✅
Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label May 6, 2026
@clickhouse-gh

clickhouse-gh Bot commented May 6, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 91.10% 91.10% +0.00%
Branches 76.70% 76.60% -0.10%

Changed lines: 100.00% (8/8) · Uncovered code

Full report · Diff report

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

@alexey-milovidov alexey-milovidov self-assigned this May 6, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue May 6, 2026
Merged via the queue into ClickHouse:master with commit b0b5f2c May 6, 2026
165 checks passed
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label May 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants