Repository navigation
Conversation
Pre-PR validation gate
|
|
Workflow [PR], commit [8bb9f77] AI ReviewSummaryThis PR tries to fix analyzer wrong results and Missing context / blind spots
Findings
Tests
Final VerdictStatus: Minimum required action: address or prove safe the unmatched non- |
|
|
||
| -- A function column BEFORE a plain column and its alias used to throw CANNOT_PARSE_DATETIME. | ||
| -- materialize() keeps the function value out of the output so the reference stays stable. | ||
| SELECT materialize(toDateTime('2020-01-01 00:00:00')) AS f, x, xx |
There was a problem hiding this comment.
The same issue with constants, add more tests without materialize
SELECT toDateTime('2020-01-01 00:00:00') AS f, x, xx
FROM remote('127.0.0.{1,2}', currentDatabase(), t_04339) ORDER BY x;
SELECT 7 AS f, x, xx, y
FROM remote('127.0.0.{1,2}', currentDatabase(), t_04339) ORDER BY x;
There was a problem hiding this comment.
Added both, without materialize:
SELECT toDateTime('2020-01-01 00:00:00') AS f, x, xx FROM remote('127.0.0.{1,2}', currentDatabase(), t_04339) ORDER BY x;
SELECT 7 AS f, x, xx, y FROM remote('127.0.0.{1,2}', currentDatabase(), t_04339) ORDER BY x;Both fail on the base (CANNOT_PARSE_DATETIME / CANNOT_PARSE_TEXT) and pass with the fix.
9443b85 to
f7e74f4
Compare
Pre-PR validation gate (updated after clickhouse-gh[bot] review)
Session id: cron:clickhouse-worker-slot-1:20260615-153400 |
…ables (analyzer) With the analyzer, reading an ALIAS column from a remote/distributed table together with a non-storage function column and ORDER BY (so the query runs to WithMergeableState) could route the alias value into the wrong column slot, throwing CANNOT_PARSE_* or silently returning wrong results. The local plan inlines ALIAS columns into their defining expressions and orders them among the projection columns, while the outer planner keeps the ALIAS column under its identifier next to the storage columns. The headers then carry the same columns in a different order, and the position-based convert that renames the remote header to the expected header paired the wrong columns. Fix: before that convert, reorder the local source columns to the expected order. Storage and function columns share names across both lists and are matched by name; the leftover columns are paired positionally, but only when every leftover expected column is provably a table ALIAS column (its name is an ALIAS identifier). A column whose name merely differs between the two plans for another reason (for example a function that references an ALIAS column) is not paired positionally; in that case the source is left unchanged and the convert keeps its prior behavior. A final type check guards the reorder, so already-matching plans get the identity permutation and are untouched. The reorder is safe for shard reconciliation because both shard paths match the shard block to the header by name, not by position. Closes: ClickHouse#97899 Co-Authored-By: Claude Opus 4.8 <[email protected]>
f7e74f4 to
54fc2da
Compare
The test uses remote('127.0.0.{1,2}', ...) which needs two shards to reproduce
the analyzer alias-column ordering bug. macOS runners cannot bind the 127.0.0.2
loopback alias, so the second shard connection deterministically times out
(SOCKET_TIMEOUT) and the Fast test (arm_darwin) fails. Skip it there, matching
the existing remote('127.0.0.{1,2}') entries in ci/defs/darwin.skip.
Co-Authored-By: Claude Opus 4.8 <[email protected]>
| /// Only pair leftovers positionally when every leftover is a real ALIAS column; | ||
| /// otherwise the relative-order assumption does not hold and we must not reorder. | ||
| if (!alias_column_identifiers.contains(expected[i].name)) | ||
| return source; |
There was a problem hiding this comment.
This still falls back to the old positional conversion for the case the PR description calls out as the reason pure name matching was unsafe: a non-ALIAS function column whose action-node name differs between the real and only_analyze plans. With source [__table1.x, _CAST(now_src), concat(__table1.x, 'aaa')] and expected [__table1.x, __table1.xx, _CAST(now_expected)], __table1.x matches, __table1.xx is recorded as an ALIAS leftover, then the unmatched now column hits this branch and returns the original order. The following MatchColumnsMode::Position convert then pairs concat(...) with the now slot again, reproducing the CANNOT_PARSE_DATETIME/wrong-result path. Please either keep unmatched non-ALIAS function slots in the permutation instead of bailing back to position matching, or add a regression that executes the original now, generateUUIDv4, and rand cases. For unstable output, FORMAT Null is enough to prove they no longer throw.
There was a problem hiding this comment.
The now/generateUUIDv4/rand columns do not reach this branch, so the original issue shapes are not reproduced on the fixed binary. The premise that _CAST(now_src) differs from _CAST(now_expected) does not hold: the constant is folded once in the shared analyzed query tree, so the function's action-node name is byte-identical in both the ReadFromRemote (source) and only_analyze (expected) headers. It is therefore matched by name in the first pass and the only leftover is the genuine table ALIAS.
EXPLAIN header=1 for SELECT now(), x, xx FROM remote('127.0.0.{1,2}', db, t) ORDER BY x (fixed binary):
Change remote column names to local column names
Header: __table1.x String
__table1.xx String
_CAST(1781600130_DateTime, 'DateTime'_String) DateTime
ReadFromRemote
Header: __table1.x String
_CAST(1781600130_DateTime, 'DateTime'_String) DateTime
concat(__table1.x, 'aaa'_String) String
_CAST(1781600130_DateTime, ...) is identical on both sides, so now is name-matched; __table1.xx (an alias identifier) is the sole leftover; the reorder maps concat(...) to the xx slot. rand() and generateUUIDv4() behave the same (rand() UInt32, generateUUIDv4() UUID identical in both headers).
EXPLAIN actions=1 confirms the final mapping Positions: 0 3 2 -> [__table1.x, __table1.xx, _CAST(now)DateTime], no concat fed into the DateTime slot.
Added the now/generateUUIDv4/rand regressions to 04339 with FORMAT Null per your suggestion. They throw CANNOT_PARSE_* on the base binary and pass on the fixed one (10/10 randomized).
The function-referencing-alias shape (e.g. ax + 1000 next to a same-type alias) does land in the leftover lists and is intentionally left to the position fallback by the alias-identifier guard. That is a separate pre-existing bug (identical wrong result on unmatched master, outside #97899) and is noted as out of scope.
Cover the original issue ClickHouse#97899 shapes where a non-deterministic function (now(), generateUUIDv4(), rand()) sits before a plain column and its ALIAS column over a two-shard remote read with ORDER BY. These used to throw CANNOT_PARSE_DATETIME / CANNOT_PARSE_UUID / CANNOT_PARSE_TEXT under the analyzer. FORMAT Null is used because the output is non-deterministic; it still proves the queries run to completion instead of throwing. These functions do not hit the positional-fallback path in alignRemoteColumnsToExpectedOrder: their action-node name is folded once in the shared analyzed query tree (e.g. _CAST(<epoch>_DateTime, 'DateTime'), rand(), generateUUIDv4()), so it is byte-identical in the ReadFromRemote (source) and only_analyze (expected) headers and is matched by name in the first pass. The only leftover column is the genuine table ALIAS, so the alias-identifier guard holds and the reorder is applied. Verified with EXPLAIN header=1/actions=1. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (updated: now/generateUUIDv4/rand regressions)
Session id: cron:clickhouse-worker-slot-4:20260616-083800 |
|
Closing in favor of #107675, the consolidated fix for #97899 (also #81631, #106398). Verified #107675 fixes every #97899 shape, including the function-referencing-alias case ( |
Closes: #97899
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix wrong results and
CANNOT_PARSE_*exceptions when reading anALIAScolumn from a distributed/remote table together with a function column (now(),generateUUIDv4(),rand(), ...) andORDER BYwithenable_analyzer = 1.Description
With
enable_analyzer = 1, a query that reads anALIAScolumn from a remote/distributed table together with a function column andORDER BYcould throwCANNOT_PARSE_*or silently return wrong results:Root cause: for a remote read the local/
ReadFromRemoteplan inlinesALIAScolumns into their expressions and places them among the projection columns, while the outer planner keeps theALIAScolumn next to the storage columns. When anALIAScolumn coexists with a function column the two headers end up in different orders.getQueryPlanForTableExpressionthen renames the local header to the expected header withActionsDAG::MatchColumnsMode::Position, which pairs columns by position and maps the shard's alias value into the function column slot (the alias stringconcat(x, 'aaa')getsCASTto the function's type). Position matching is used here because constant function columns get value-dependent names that differ between the two planner runs, so a pure name match would not work.Fix: before the position-based convert, reorder the local source columns to the expected order. Storage and function columns share names across both lists and are matched by name; the leftover columns (the
ALIAScolumns, named differently on each side but in the same relative order) are paired positionally. The reorder is only applied for remote reads with size-matching headers and only when it makes the types line up with the expected header position by position, so already-matching plans get the identity permutation and are untouched. The reorder is safe for shard reconciliation because both shard paths match the shard block to the header by name, not by position.Verified on a 2-shard
remote('127.0.0.{1,2}', ...)setup: the new test04339_distributed_alias_column_with_functionfails before the change (CANNOT_PARSE_DATETIME / silent wrong results) and passes after it. This is a different root cause from the recently merged #106404 (shard-sidebuildQueryTreeDistributedname dedup): confirmed the repro still failed identically on a build containing #106404 before this change.