Skip to content

Fix logical error in arrayUnion and arraySymmetricDifference with a Nullable and a Dynamic argument - #118966

Merged
alexey-milovidov merged 7 commits into
ClickHouse:masterfrom
groeneai:fix-arrayunion-dynamic-nullable-column-unwrap
Sep 12, 2026
Merged

alexey-milovidov merged 7 commits into
ClickHouse:masterfrom
groeneai:fix-arrayunion-dynamic-nullable-column-unwrap

Conversation

@groeneai

@groeneai groeneai commented Sep 9, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #118961
Related: #118485

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

Not required: the defect exists only on master, introduced by the unreleased #118485, so no released version is affected.

Description

SELECT arrayUnion([1, NULL], ['a'::Dynamic]) fails with Columns are assumed to be of identical types, but they are different in Nullable, and aborts debug and sanitizer builds. arraySymmetricDifference behaves the same, and table columns fail identically, not only constants.

prepareArrays builds an overflow mask by re-comparing the cast element column against the original with notEquals, and it unwraps ColumnNullable from the initial column only inside the if that tests the cast one. With a Dynamic common element type the cast column is a ColumnDynamic: Dynamic cannot sit inside Nullable, and getLeastSupertype's Dynamic rule precedes its Nullable rule. So that inner unwrap never runs and the initial column keeps its wrapper, while #118485 strips Nullable from both declared types, leaving callFunctionNotEquals with UInt8 for a still-ColumnNullable column. The Dynamic adaptor substitutes its own operand's per-variant column, so the declared types match and notEquals enters executeGenericIdenticalTypes, whose paranoid isNullable() check this argument list violates.

The fix moves that unwrap out of the cast-column if, so both operands are unwrapped whenever either is. initial_column is read nowhere after the guard block, so the move is observable only where it fails today: with no cast both sides unwrap to the same pointer and the guard is skipped as before. Before #118485 the element type stayed Nullable(T) and isInteger(Nullable(T)) is false, so the branch was unreachable in every release: master only.

Validated both directions: four query shapes and the test fail before and pass after, returning the pre-#118485 output [NULL,1,'a']; 50 randomized runs and five neighbour tests pass. arrayIntersect and the Variant spelling stop at an earlier NO_COMMON_TYPE, so they are not carriers. Residual left alone: the mask is all-zero whenever the cast target is Dynamic, which predates this defect.

CI provenance: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=117973&sha=fea6f43a91efed27ff56e9425b728dfda0498c74&name_0=PR&name_1=Stress%20test%20%28arm_tsan%29


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

Version info

  • Merged into: 26.9.1.1272 (included in 26.9 and later)

… cast one

prepareArrays builds an overflow mask for integer-like element types by
re-comparing the cast element column against the original with notEquals, so
values truncated by the narrowing cast cannot match. It unwrapped ColumnNullable
from the initial element column only inside the `if` that tests the cast column,
so the two operands could disagree about nullability.

With a Dynamic common element type they do. Dynamic cannot sit inside Nullable
(DataTypeDynamic::canBeInsideNullable) and getLeastSupertype's Dynamic rule
precedes its Nullable rule, so castColumns produces a ColumnDynamic for the cast
side while the initial side keeps its ColumnNullable wrapper. Since the element
types handed to the comparison are stripped of Nullable, callFunctionNotEquals
is given UInt8 for a column that is still a ColumnNullable. The Dynamic adaptor
substitutes its own operand's per-variant column and forwards the other
untouched, the declared types become equal, and notEquals takes the types_equal
branch into executeGenericIdenticalTypes, whose paranoid isNullable() check
fires: SELECT arrayUnion([1, NULL], ['a'::Dynamic]) raises a logical error and
aborts builds that treat it as fatal.

Move the initial-column unwrap out of the cast-column `if` so both operands are
unwrapped whenever either is. initial_column is read nowhere after the guard
block, so the move is observable only in the shape that fails today: when the
cast column is a ColumnNullable the behaviour is byte-identical, and when
neither column is nullable it is a no-op. The pointer comparison that gates the
mask is unaffected, because without a cast both sides unwrap to the same object
and with a cast they differ either way. The same class was fixed at these three
lines before, in f8ebd75.

arrayUnion and arraySymmetricDifference share prepareArrays and both failed;
arrayIntersect resolves its cast target through getMostSubtype, which has no
Dynamic handling, so it rejects the mixed spelling with NO_COMMON_TYPE earlier.
The defect is not confined to analyzer constant folding either: the same call
over table columns failed identically.

Master only. Previously the element type stayed Nullable(T) and
isInteger(Nullable(T)) is false, so this branch was unreachable for a Nullable
element type in every release.

Co-Authored-By: Claude Opus 5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-ci-own-pr PR origin: CI on a groeneai PR exposed a separate defect labels Sep 9, 2026
@groeneai

groeneai commented Sep 9, 2026

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: no findings in the code; two notes disclosed

An independent model re-reviewed this diff cold and returned no findings. My own cold pass raised five,
all in the PR description rather than the source, and I fixed those before publishing.

💡 The moved guard tests initial_column->isNullable() and then reference-casts to ColumnNullable,
where the sibling unwrap just above uses the pointer form. isNullable() is also overridden by
ColumnSparse, ColumnReplicated and ColumnConst, but none of them can be a ColumnArray's data at
that line: ColumnConst is unwrapped earlier, a replicated argument cannot pass the two ColumnArray
casts that precede it, and Array is not sparse-serializable. I kept the two-line move instead of
rewriting the predicate, since the hazard has no reachable input. Happy to switch to the pointer form
if you prefer it.

💡 Whenever the cast target is Dynamic the overflow mask is all-zero by construction, so computing it
is wasted work. That predates this defect and I left it alone.

@clickhouse-gh

clickhouse-gh Bot commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [be32a70]

Summary: ✅


AI Review

Summary

This PR fixes the arrayUnion / arraySymmetricDifference LOGICAL_ERROR introduced by #118485 when an Array(Nullable(...)) argument is compared against an Array(Dynamic) while building the overflow mask. I reviewed the current diff, the full src/Functions/array/arrayIntersect.cpp implementation, and all prior discussion on the PR, and I did not find a remaining correctness or coverage issue that warrants an inline review comment.

Missing context / blind spots
  • ⚠️ This checkout did not have a runnable clickhouse binary available, so I could not rerun the reproducer locally. Confidence comes from tracing the current code paths and from the focused stateless coverage added in this PR.
Final Verdict

✅ No new findings.

LLVM Coverage Report

Measured on commit be32a70.

Metric Baseline Current Δ
Lines 89.00% 89.00% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 81.30% 81.30% +0.00%

Changed lines: Changed C/C++ lines covered: 5/5 (100.00%) · Uncovered code

Full report · Diff report

@groeneai

groeneai commented Sep 9, 2026

Copy link
Copy Markdown
Collaborator Author

cc @Avogar @Ergus, could you review this? prepareArrays unwrapped ColumnNullable from the initial element column only inside the if that tests the cast column, so with a Dynamic common element type (which cannot sit inside Nullable) the overflow-mask notEquals received a still-ColumnNullable column declared as bare UInt8, and its identical-types check raises a logical error. The fix moves that unwrap out of the if; master only, since the branch was unreachable for Nullable element types before #118485.

@clickhouse-gh clickhouse-gh Bot added pr-not-for-changelog This PR should not be mentioned in the changelog comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... labels Sep 9, 2026
@PedroTadim PedroTadim self-assigned this Sep 9, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing be32a709f with master a61a24c6a (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, 11 s compile time in total, 7 of them have a recent master baseline.

Job report

Requested in review. The overflow-mask guard has four separate element type
predicates (isInteger, isDate, isDateTime, isDateTime64), so each one now has a
case, and the cases are spread over both functions that can reach the guard with
a Dynamic cast target. Every added statement aborts on master and passes here:

  - Date, DateTime and DateTime64 element types, one guard predicate each
  - a LowCardinality(Nullable(UInt8)) element type
  - non-constant arguments, which run executeImpl rather than the analyzer's
    constant folding
  - a value present in both arguments, so the result also pins that the overflow
    mask stays aligned with the element column it was built from
  - an empty array, paired with a non-empty row in the same column: on its own
    the cast side is a zero-row ColumnDynamic with no variants, the comparison is
    never dispatched, and the case would pass with or without the fix

Results are sorted because the element order of an Array(Dynamic) result is
variant dependent and the order is not the property under test.

arrayIntersect cannot reach this shape: it resolves its cast target through
getMostSubtype, which rejects a number next to Dynamic with NO_COMMON_TYPE.

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

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - a22f0d5

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.

Check / test Reason Owner / fixing PR
Integration tests (amd_asan_ubsan, db disk, old analyzer, 1/6) / Timeout job-level session timeout (session-timeout occurred during test execution), zero failing tests; 197 rows over 163 pull requests and 14 true-master runs in 30 days #118384 (mine, open) and #118933 (external, open)
Stress test (amd_msan) / Input nodes size mismatch in dag (STID 4752-5ad8) LOGICAL_ERROR abort at JoinExpressionActions:138 reached through chooseJoinOrder, on a Nullable(Nothing) join key; 11 rows over 11 unrelated pull requests, 0 on master, in 30 days, so not caused by this PR (nothing here touches the join planner) #119106 (mine, open)
Stress test (arm_debug) / Inconsistent KeyCondition behavior (STID 5182-6d6b) debug-only key-analysis canary abort at MergeTreeDataSelectExecutor.cpp:2550 (markRangesFromPKRange); 4 rows over 4 pull requests, 0 on master, in 30 days, and no index or key-analysis code is touched here a fix task is created (investigating at full effort, fixing-PR link to follow here)

Mergeable Check and PR are the praktika aggregators reporting the three rows above, not separate
failures. CH Inc sync is green at this head. 148 of 174 check-runs passed, 21 were skipped, and the
workflow finished at 2026-09-10 02:38:06Z.

Session id: cron:our-pr-ci-monitor:20260910-030114

@PedroTadim
PedroTadim added this pull request to the merge queue Sep 10, 2026
@PedroTadim

Copy link
Copy Markdown
Member

@alexey-milovidov the PR is on the queue, why you merged master into it?

@alexey-milovidov
alexey-milovidov removed this pull request from the merge queue due to a manual request Sep 10, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member

@PedroTadim, it had a failed check.

@ClickHouse ClickHouse deleted a comment from CLAassistant Sep 12, 2026
Merged via the queue into ClickHouse:master with commit e3d6e2b Sep 12, 2026
175 of 345 checks passed
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 12, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

Fixing PR for the Inconsistent KeyCondition behavior row on my CI finish ledger here (STID 5182-6d6b, Stress test (arm_debug), 2026-09-09): #121089.

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-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... groeneai-origin-ci-own-pr PR origin: CI on a groeneai PR exposed a separate defect pr-not-for-changelog This PR should not be mentioned in the changelog 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.

arrayUnion(Nullable, Dynamic) LOGICAL_ERROR: element type stripped, column not

5 participants