Skip to content

Fix IN with a bare array column on the right argument - #109416

Merged
alexey-milovidov merged 3 commits into
masterfrom
in-array-column-right-argument
Jul 12, 2026
Merged

alexey-milovidov merged 3 commits into
masterfrom
in-array-column-right-argument

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Jul 4, 2026 •

Copy link
Copy Markdown
Member

On the right side of IN, an array is treated as the set of its elements, so x IN [1, 2, 3] and x IN array_returning_function() are already rewritten to has. A bare array column on the right, however, was unconditionally wrapped in tuple(column) and treated as a single set element. As a result:

  • 'system.tables' IN tables silently returned an empty result (the whole array was stringified into one set element and never matched), while has(tables, 'system.tables') matched — the two are expected to be equivalent.
  • For non-stringifiable element types (e.g. Array(UInt8)) it threw ILLEGAL_TYPE_OF_ARGUMENT.

This rewrites x IN arr to has(arr, x) when the right argument is an Array-typed column that is exactly one dimension deeper than the left argument, so that the element type of has matches the left argument (e.g. a scalar and Array(scalar), or Array(T) and Array(Array(T))). Columns of equal depth (e.g. Array(T) IN Array(T)) remain a one-element set, i.e. equality. Every other case falls through to the pre-existing behavior unchanged.

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 bare array column on the right argument silently returning a wrong (always-false) result or throwing an exception, so that x IN arr behaves like has(arr, x).

Version info

  • Merged into: 26.7.1.819 (included in 26.7 and later)

The right side of `IN` treats an array as the set of its elements, so
`x IN [1, 2, 3]` and `x IN array_returning_function()` are already rewritten
to `has`. A bare array *column* on the right, however, was unconditionally
wrapped in `tuple(column)` and treated as a single set element. For a
stringifiable element type this silently produced an always-false result
(e.g. `'system.tables' IN tables` returned nothing while
`has(tables, 'system.tables')` matched); for other element types it threw
`ILLEGAL_TYPE_OF_ARGUMENT`.

Rewrite `x IN arr` to `has(arr, x)` when the column is `Array`-typed and
exactly one dimension deeper than the left argument, so that the element type
of `has` matches the left argument (e.g. a scalar and `Array(scalar)`, or
`Array(T)` and `Array(Array(T))`). Columns of equal depth (e.g. `Array(T) IN
Array(T)`) stay a one-element set, i.e. equality.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jul 4, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [287deae]

Summary: ✅


AI Review

Summary

This PR fixes the analyzer path for x IN arr when arr is a bare Array-typed column, so it now follows the same has(arr, x) semantics already used for array literals and array-returning functions. The final head also adds focused stateless coverage for the intended depth-based rewrite boundaries and the LowCardinality shapes that can actually occur. I did not find any unresolved correctness, compatibility, or evidence gaps in the current diff.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 4, 2026
… the right

The AI review asked to unwrap `LowCardinality` around `Array` in the new
depth checks. Such a type cannot exist: `DataTypeLowCardinality` rejects
`Array` as its dictionary type regardless of
`allow_suspicious_low_cardinality_types` (that setting only relaxes the
separate validation for small fixed-size types), so there is nothing to
unwrap and no code change is needed. Add regression queries for the
LowCardinality shapes that can occur - a `LowCardinality` scalar left
argument (including a suspicious `LowCardinality(UInt8)`) and
`Array(LowCardinality(String))` on the right - plus a guard that creating
`LowCardinality(Array(...))` keeps throwing `ILLEGAL_TYPE_OF_ARGUMENT`.

Co-Authored-By: Claude Fable 5 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

@groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=109416&sha=f5dc138ca3afaa5093238e2fa66daf4d84e8b8ab&name_0=PR&name_1=Stateless%20tests%20%28amd_asan_ubsan%2C%20distributed%20plan%2C%20parallel%29 and provide a fix in a separate PR. If the fix is already in progress, link it here.

01730_distributed_group_by_no_merge_order_by_long failed with MEMORY_LIMIT_EXCEEDED in AggregatingTransform on a remote shard (query with distributed_group_by_no_merge = 2 that expects no memory-limit error), all 15 in-place reruns passed, and CIDB shows it failing on master and many unrelated PRs daily since 2026-07-06. It is not caused by this PR (which only touches the analyzer IN → has rewrite), and I found no open issue or fix PR tracking it.

@groeneai

Copy link
Copy Markdown
Collaborator

This is the known flaky 01730_distributed_group_by_no_merge_order_by_long, not caused by your IN->has change. Fix is already in progress in #109456.

The failing statement is query 2:

select * from remote('127.{2..11}', view(select * from numbers(1e6)))
group by number order by number limit 20
settings distributed_group_by_no_merge=2, max_memory_usage='100Mi';

It is expected to pass (with distributed_group_by_no_merge=2 the remote shards do the ORDER BY + LIMIT cut). It flakes because a remote shard's per-query cap is 100 MiB and the shard-side GROUP BY number FROM numbers(1e6) reserves the aggregation hash table as a single 512 MiB chunk up front:

would use 513.05 MiB (attempt to allocate chunk of 512.00 MiB), maximum: 100.00 MiB: While executing AggregatingTransform

That 512 MiB is a size reservation, not incremental growth, so it either trips the 100 MiB cap immediately or not at all depending on whether the size hint is populated. #109456 fixes it by disabling that reservation for this test. It is under review with @azat.

CIDB, last 7d: same test + same AggregatingTransform MEMORY_LIMIT_EXCEEDED across ~20 unrelated PRs and master, all reruns pass. Report from this PR: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=109416&sha=f5dc138ca3afaa5093238e2fa66daf4d84e8b8ab&name_0=PR&name_1=Stateless%20tests%20%28amd_asan_ubsan%2C%20distributed%20plan%2C%20parallel%29

@clickhouse-gh

clickhouse-gh Bot commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.80% 85.80% +0.00%
Functions 92.70% 92.70% +0.00%
Branches 77.90% 78.00% +0.10%

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

Full report · Diff report

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Great!

@alexey-milovidov alexey-milovidov self-assigned this Jul 12, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jul 12, 2026
Merged via the queue into master with commit 5c342b8 Jul 12, 2026
176 checks passed
@alexey-milovidov
alexey-milovidov deleted the in-array-column-right-argument branch July 12, 2026 03:11
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 12, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…right-argument

Fix `IN` with a bare array column on the right argument
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

3 participants