Repository navigation
Interpret an array subquery on the right of IN as a set of its elements - #110169
Conversation
`x IN (SELECT groupArray(x) ...)` previously treated the whole array
returned by the subquery as a single opaque set value: the left argument
was coerced into the array type, producing a confusing
`CANNOT_READ_ARRAY_FROM_TEXT` ("Array does not start with '[' character")
or `TYPE_MISMATCH` error.
The right side of `IN` treats an array as the set of its elements, so
`x IN [1, 2, 3]`, `x IN array_returning_function()` and a bare array
column `x IN arr` are already handled this way. Extend the same
dimension-based rule to a subquery: when the subquery's single result
column is an `Array` exactly one dimension deeper than the left argument,
wrap it in `SELECT arrayJoin(<column>) FROM (<subquery>)` so the array
elements become the set elements. Equal-depth arrays stay a one-element
set (equality), matching the bare-column behaviour.
Closes: #37066
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Workflow [PR], commit [cc0d703] Summary: ✅
AI ReviewSummaryThis PR extends analyzer-side Final Verdict
|
`NameAndTypePair` takes its arguments by const reference, so `std::move` of `element_name` and `element_type` did not move anything and failed the `arm_tidy` build: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=110169&sha=2a1abc2e8d60090dd27f8ed7a9cca15853e034c9&name_0=PR&name_1=Build%20(arm_tidy) Co-Authored-By: Claude Fable 5 <[email protected]>
An array subquery on the right of `IN` is now the set of its elements, so `(1, 2) IN (SELECT [0, 0] UNION ALL SELECT [1, 1])` tests the tuple against a set of `UInt8` elements and fails with `ILLEGAL_TYPE_OF_ARGUMENT` - the same exception as the pre-existing `(1, 2) IN (SELECT 0 UNION ALL SELECT 1)` - instead of `TYPE_MISMATCH` from coercing the tuple into the array type. Failed in Fast test: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=110169&sha=2a1abc2e8d60090dd27f8ed7a9cca15853e034c9&name_0=PR&name_1=Fast%20test Co-Authored-By: Claude Fable 5 <[email protected]>
… path An array subquery on the right of `IN` is the set of its elements, flattened with `arrayJoin`. So far this was only applied in the regular `IN` handling. When `rewrite_in_to_join = 1` (which requires `allow_experimental_correlated_subqueries = 1`), the earlier branch rewrites a non-constant `x IN (subquery)` into a correlated `EXISTS` before the regular `IN` handling runs, so the right side was still compared as `x = <array>` instead of being flattened into set elements, keeping the reported bug alive and making the new behavior depend on an unrelated setting. Factor the flattening into a shared helper `flattenArraySubqueryOnRightOfIn` and call it in both the regular `IN` path and before building the `EXISTS` rewrite. Add regression queries under `rewrite_in_to_join = 1` to `04512_in_function_array_subquery_right_argument`. Addresses the AI review finding on #110169
Addresses the review on `resolveFunction.cpp:784`: verify how an array subquery on the right of `IN` with `Nullable` elements interacts with `transform_null_in` and `rewrite_in_to_join`. On the regular `IN` path the flattened array subquery honors `transform_null_in`: with `transform_null_in = 1` a `NULL` element is a set member (a `NULL` left argument matches), and with the default `transform_null_in = 0` a comparison against `NULL` is unknown (`NULL`). Under `rewrite_in_to_join` the non-constant `x IN (subquery)` is lowered to a correlated `EXISTS` that compares with `equals`, which does not honor `transform_null_in` for a `NULL` set element. This is a pre-existing property of the rewrite that applies to a scalar `QueryNode` subquery too, so the flattened array subquery behaves exactly like the equivalent `SELECT arrayJoin(...)` scalar subquery and adds no new inconsistency: the test asserts the array-subquery and the `arrayJoin`-subquery forms produce the identical result under the rewrite. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
… of IN Addresses the review finding that the array-as-set flattening only ran on the subquery path, so a direct table reference on the right of `IN` whose single column is an `Array` kept the old opaque-array behavior: `2 IN some_array_table` failed with a type mismatch while the equivalent `2 IN (SELECT arr FROM some_array_table)` now works. The `TableNode` branch of the `IN` handling in `QueryAnalyzer::resolveFunction` now calls a new helper `flattenArrayTableExpressionOnRightOfIn`, which, when the table has a single `Array` column exactly one dimension deeper than the left argument, wraps it as a `SELECT column FROM table` subquery and reuses `flattenArraySubqueryOnRightOfIn` (so the elements become the set elements). Tables with a non-`Array` column, a wrong depth, or more than one column are left untouched, so the ordinary `x IN table` path and any `StorageSet` fast path are unchanged. The `TableFunctionNode` branch, which already wraps the table function into a one-column subquery, now runs the same flattening on the wrapped subquery for consistency. Added regressions to `04512_in_function_array_subquery_right_argument` for the direct-table spelling: element membership, `NOT IN`, multiple rows, `Array` left argument with an `Array(Array(...))` column, equal-depth equality, nullable elements with `transform_null_in`, a non-`Array` table regression guard, and the `rewrite_in_to_join` path. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
@groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=110169&sha=383b1435878feb788eea2d4e49dd3e32c86b8c7e&name_0=PR&name_1=Stress%20test%20%28arm_asan_ubsan%29 and provide a fix in a separate PR. If the fix is already in progress, link it here. It is an UndefinedBehaviorSanitizer report (signed overflow in |
|
Fix is already in progress: #106961 "Fix signed integer overflow in condition_variable::wait_for with huge timeouts". The report here (STID 2881-3dbd, Deterministic repro on master: #106961 is under review. |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 61/65 (93.85%) · Uncovered code |
|
Hi @alexey-milovidov — while reviewing this PR I found the following:
Happy to discuss — close anything that's wrong or already addressed. |
… path An array subquery on the right of `IN` is the set of its elements, flattened with `arrayJoin`. So far this was only applied in the regular `IN` handling. When `rewrite_in_to_join = 1` (which requires `allow_experimental_correlated_subqueries = 1`), the earlier branch rewrites a non-constant `x IN (subquery)` into a correlated `EXISTS` before the regular `IN` handling runs, so the right side was still compared as `x = <array>` instead of being flattened into set elements, keeping the reported bug alive and making the new behavior depend on an unrelated setting. Factor the flattening into a shared helper `flattenArraySubqueryOnRightOfIn` and call it in both the regular `IN` path and before building the `EXISTS` rewrite. Add regression queries under `rewrite_in_to_join = 1` to `04512_in_function_array_subquery_right_argument`. Addresses the AI review finding on ClickHouse#110169
…y-as-set Interpret an array subquery on the right of IN as a set of its elements
The right side of
INtreats an array as the set of its elements, sox IN [1, 2, 3],x IN array_returning_function()and a bare array columnx IN arrare all handled that way. A subquery returning an array, however, was not:x IN (SELECT groupArray(x) ...)treated the whole array as a single opaque set value, coercing the left argument into the array type and producing a confusing error instead of a result:This extends the same dimension-based rule that was recently added for a bare array column (#109416) to a subquery: when the subquery's single result column is an
Arrayexactly one dimension deeper than the left argument, the subquery is wrapped asSELECT arrayJoin(<column>) FROM (<subquery>)so the array elements become the set elements. Both queries above now return10. Equal-depth arrays (e.g.Array(T)on both sides) stay a one-element set, i.e. equality, matching the bare-column behaviour.Closes: #37066
Related: #109416
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
A subquery on the right side of
INwhose single column is an array one dimension deeper than the left argument is now interpreted as the set of the array's elements (like an array literal or an array-returning function), instead of failing with a confusing type-mismatch error. For example,x IN (SELECT groupArray(x) FROM ...)now works.Documentation entry for user-facing changes
Version info
26.7.1.972(included in26.7and later)