Skip to content

Fix constant IN sets dropping NULL for Variant keys under transform_null_in - #130261

Open
groeneai wants to merge 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/variant-null-constant-in-set-transform-null-in
Open

groeneai wants to merge 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/variant-null-constant-in-set-transform-null-in

Conversation

@groeneai

@groeneai groeneai commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

Changelog category (leave one):

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

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fixed IN with a constant list, array or tuple under transform_null_in = 1 when the left side is a Variant (or a multi-column key with a Variant element): a NULL member of the set was dropped, so WHERE v IN (NULL, 1) skipped the rows where v is NULL and NOT IN returned them. Constant sets now match NULL like a subquery set and a Nullable column already did.

Description

Related: #123745 (lists this case as an open risk)
Related: #119183

On 26.3 and 26.10, SELECT id FROM t WHERE v IN (NULL, 1) SETTINGS transform_null_in = 1 on a Variant(UInt64, String) column drops the rows where v is NULL, and NOT IN keeps them, while the same set written as a subquery matches them.

createBlockFromCollection keeps a member that converted to NULL only when the key type isNullable(). A Variant stores NULL in its discriminator and is not Nullable, so the member was dropped. The single-key and multi-key checks now use canContainNull, the predicate convertColumnToTypeOrNull already applies, so a NULL enters the set only from a real NULL member. transform_null_in = 0 is unchanged. The Nullable(Tuple) key cases are handled on master since #119183; the test keeps them.

Validation: the new test covers single and multi-column keys, Nullable(Tuple) keys, and Memory and MergeTree tables (with a set skip index on and off), next to the subquery results. 20 of its 40 lines are wrong on master, and reverting either changed line turns its own lines red. It passes 50/50 with random settings; the existing IN and transform_null_in tests pass unchanged.

Not changed here: with transform_null_in = 0 a multi-column key holding a Variant NULL still differs ((v, 1) IN ((NULL, 1)) gives 0, the subquery 1).


Workflow [PR]
Sync PR [sync-upstream/pr/130261]

…ull_in

With transform_null_in = 1 a NULL is an ordinary matchable value of IN.
createBlockFromCollection and getSetElementsForConstantValue kept a
constant member that converted to NULL only when the key type (or tuple
element type) was isNullable(). A Variant stores NULL in its
discriminator and is not Nullable, and neither is a LowCardinality(Nullable)
element of a Nullable(Tuple) key, so the NULL member was silently dropped:
`v IN (NULL, 1)` gave 0 for a Variant NULL (NOT IN gave 1), while the same
set written as a subquery, and a Nullable NULL, matched.

Use canContainNull at the four sites (single key, multi-column key,
Nullable(Tuple) element loop, the all-NULL tuple-literal reading of
`Nullable(Tuple) IN (NULL, NULL)`). It is the predicate
convertColumnToTypeOrNull already applies: it yields a NULL column only for
a NULL source into a type that can hold NULL and skips the member
otherwise, so a NULL can enter the set only from a genuine NULL member.
The transform_null_in = 0 arm is unchanged.

Related: ClickHouse#123745

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

groeneai commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 2 findings; 1 full pass).

# Sev Finding Verdict Evidence / action
1 ⚠️ The test does not prove the set skip index is selected, so B mt_in would stay green if index selection declined DISAGREE The lines assert set membership: reverting the fix turns all 7 literal-list B lines red, index or not. The index does prune for this predicate on master (2/4 granules), and index selection is not changed here.
2 💡 Comments at SetUtils.cpp:648-650 and :809-810 still say "nullable" AGREE Pre-existing lines outside the diff, left as is because #119183 rewrites the adjacent lines.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-self-slot-58:20261109-051000

@groeneai

groeneai commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes: CAST(NULL, 'Variant(UInt64, String)') IN (NULL, 1) SETTINGS transform_null_in = 1 gives 0 on every run on master (1 for the same set as a subquery).
b Root cause explained? The constant-set builder in SetUtils.cpp keeps a member converted to NULL only if the key type isNullable(); Variant (and an unstripped LowCardinality(Nullable) tuple element) can hold NULL without being Nullable, so the NULL member was dropped.
c Fix matches root cause? Yes: the four checks use canContainNull, the predicate the converter beneath already applies; no probe-side guard, no setting pin.
d Test intent preserved / new tests added? New SQL test, 40 labelled lines: 27 fail on master, 13 controls.
e Both directions demonstrated? Yes: fails on master and on the same tree with the four lines reverted, passes with the fix; reverting each line alone turns its own lines red. 50/50 with random settings.
f Fix is general across code paths? Yes: every site with this predicate (single key, multi-column key, Nullable(Tuple) elements, all-NULL tuple literal); the native-number fast paths never see a Variant key.
g Fix generalizes across inputs (params/datatypes/wrappers)? Yes: scalar/tuple/array literals, nullIn/notNullIn, materialized LHS, Memory and MergeTree with a set skip index on/off, multi-column keys, Nullable(Tuple) with Variant, mixed and LowCardinality(Nullable) fields; Nullable and non-nullable controls.
h Backward compatible? (maintainer-approved exception only) Yes: only transform_null_in = 1 with a NULL member and a key that can hold NULL changes, from a wrong answer to the subquery's answer.
i Invariants and contracts preserved? Yes: set block structure unchanged; a NULL row enters only from a real NULL member; the transform_null_in = 0 path is unchanged.

Session id: cron:clickhouse-impl-self-slot-57:20261109-044300

@groeneai

groeneai commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator Author

cc @yakov-olkhovskiy @CurtizJ, could you review this and add the can be tested label so CI runs? The constant-set builder in SetUtils.cpp kept a NULL member only when the key type isNullable(), so under transform_null_in = 1 a Variant key (or a LowCardinality(Nullable) tuple element) lost it and v IN (NULL, 1) disagreed with the subquery form; the four checks now use canContainNull.

@clickhouse-gh

clickhouse-gh Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [a380958]

Summary: ✅


AI Review

Summary

The PR preserves genuine NULL members in constant IN sets for Variant keys under transform_null_in = 1, including multi-column keys. The two checks now use the same null-capability predicate as the converter. No actionable correctness or safety findings. The new stateless regression passed in Fast test on the reviewed head, a3809581fa7c7b91cc8af3018ba99c39238386f3; no local tests were run.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default comp-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). can be tested Allows running workflows for external contributors labels Nov 9, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing a3809581f with master d31a6eff9 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

7 translation units recompiled, 5 s compile time in total, 7 of them have a recent master baseline.

Job report

@groeneai

groeneai commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - b2563e9

Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
AST fuzzer (amd_debug, targeted) / Logical error: Inconsistent KeyCondition behavior (STID: 5182-57a3) not from this PR: SELECT count() ... WHERE key_nullable IN (NULL) on a Nullable(UInt32) sorting key. For a Nullable key canContainNull() equals isNullable(), so this diff does not change that set. The same query shape failed on 6 other PRs in the last 30 days #121089 (mine, open)
AST fuzzer (amd_debug, targeted, old_compatibility) / Logical error: Inconsistent KeyCondition behavior (STID: 5182-728c) same query and mechanism as the row above #121089 (mine, open)
PR praktika rollup of the rows above #121089 (the owner above)

The other CIDB FAIL rows on this head are Config Workflow / Pre Hooks and Finish Workflow / Post Hooks of the run before the can be tested label.

Session id: cron:our-pr-ci-monitor:20261109-103003

…ll-constant-in-set-transform-null-in

# Conflicts:
#	src/Analyzer/SetUtils.cpp

This branch has not been deployed

No deployments
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 comp-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant