Repository navigation
Fix THERE_IS_NO_COLUMN for optimize_rewrite_aggregate_function_with_if with distributed queries - #106908
Conversation
…if` with distributed queries When `RewriteAggregateFunctionWithIfPass` rewrites `sum(if(cond, val, null))` to `sumIf(_CAST(val, 'Nullable(T)'), cond)`, the `_CAST` stays as a FunctionNode on the initiator (no resolve pass runs after optimization). On the remote (secondary) server, the same `_CAST` gets constant-folded during re-resolution, producing a ConstantNode with result type `Nullable(T)`. The column naming function `calculateActionNodeNameWithCastIfNeeded` used `getResultType()` for the inner type annotation, which gives `T` on the initiator (from the pre-cast constant) but `Nullable(T)` on the remote (from the folded result). This caused a column name mismatch: - Initiator: `_CAST(0_UInt8, 'Nullable(UInt8)'_String)` - Remote: `_CAST(0_Nullable(UInt8), 'Nullable(UInt8)'_String)` Fix: when the constant was folded from `_CAST` (`receivedFromInitiatorServer` is true), use the source `_CAST` function's first argument type as the inner type annotation. This mirrors how the initiator names it. Closes: #82941 Co-authored-by: Yakov Olkhovskiy <[email protected]>
|
|
|
Workflow [PR], commit [654bc64] Summary: ✅
AI ReviewSummaryThis PR fixes the distributed analyzer column-name mismatch for Missing context / blind spots
Final VerdictStatus: ✅ Approve |
The bugfix validation requires the test to fail on master. Using
`remote('127.0.0.1', ...)` (single host) doesn't trigger the distributed
execution path — the query runs locally without header comparison between
initiator and remote. Change to `remote('127.0.0.{1,2}', ...)` which
forces actual distributed execution and reproduces the column name mismatch.
Co-authored-by: Yakov Olkhovskiy <[email protected]>
The previous fix was too aggressive — it used the source _CAST's first argument type for ALL `receivedFromInitiatorServer` constants, which broke cases where the `_CAST` was added during AST serialization of a folded constant (e.g., NULL with type Nullable(String)). Now we only use the first argument's type when it's a "simple" constant whose natural field type matches its stored type (i.e., it wouldn't need its own _CAST wrapping). This correctly distinguishes: - Case A: `_CAST` was a live FunctionNode on the initiator (optimization pass) — first arg `0` has type UInt8, field type matches → use UInt8 - Case B: `_CAST` was added during serialization — first arg NULL has type Nullable(Nothing), field type is Nullable → needs cast → fall back to `getResultType` Co-authored-by: Yakov Olkhovskiy <[email protected]>
- Add comment to empty catch block to satisfy style checker - Force `optimize_rewrite_aggregate_function_with_if = 1` in test to prevent the stateless runner from randomizing it to 0 Co-authored-by: Yakov Olkhovskiy <[email protected]>
Replace `FieldToDataType` + try/catch with a simpler check: call `requiresCastCall(type, type)` on the first arg's result type directly. For literals parsed from AST, the natural field type always equals the result type, so this is equivalent. The check reduces to testing if the type is Nullable/Array/Tuple (which triggers cast wrapping). Co-authored-by: Yakov Olkhovskiy <[email protected]>
…apping in _CAST The previous approach of modifying `calculateActionNodeNameWithCastIfNeeded` was fundamentally flawed — it couldn't distinguish between _CAST nodes created by the optimization pass vs those added during AST serialization, causing regressions in multiple tests. New approach: in `RewriteAggregateFunctionWithIfPass`, when the kept branch is a ConstantNode and needs type promotion to Nullable, use `convertToNullable` directly instead of `createCastFunction`. This avoids the naming mismatch entirely because: - On initiator: the constant is already Nullable, named via getResultType - On remote: after AST round-trip and folding, same getResultType applies - No FunctionNode vs folded-ConstantNode asymmetry exists For non-constant arguments (column references, expressions), the original `createCastFunction` path is preserved since those don't have the naming divergence issue. Co-authored-by: Yakov Olkhovskiy <[email protected]>
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 11/14 (78.57%) | Lost baseline coverage: none · Uncovered code |
|
📊 Cloud Performance Report ✅ AI verdict: This PR narrowly changes how the aggregate-function-with-if rewrite handles constant arguments (converting constants directly to Nullable instead of wrapping in CAST) to fix column-name mismatches in distributed queries. The only flagged query, TPC-H Q4, is a count(*) with an EXISTS semi-join and never triggers that rewrite path, so the +19.86% delta (borderline and not a large shift) is run-to-run variance rather than a real PR effect. Downgrading it to not-sure. No other queries regressed and no cross-query patterns were observed. clickbench🟢 No significant changes tpch_adapted_1_officialFlagged queries (1 of 22)
q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. Debug info
|
|
Hi @yakov-olkhovskiy @KochetovNicolai — a couple of suggestions to make this PR easier for reviewers and the changelog reader. Description (currently
Ignore this if the current title/body is intentional. |
Same macOS loopback issue as 04336 in this PR. The test runs
remote('127.0.0.{1,2}', system.one), connecting to 127.0.0.2. Linux
auto-routes all of 127.0.0.0/8 to loopback, but macOS only binds 127.0.0.1, so
the connection to 127.0.0.2:9000 times out on the Fast test (arm_darwin) runner.
CIDB shows the failure is macOS-only (0 OK / 3 FAIL on arm_darwin across 3
unrelated PRs, 100% pass on every Linux job including the flaky check 50/50 on
4 sanitizers). The shard tag does not skip it because Fast test forces
clickhouse-test --shard. Add it to ci/defs/darwin.skip, next to its sibling
distributed tests. Added by ClickHouse#106908.
Co-Authored-By: Claude Opus 4.8 <[email protected]>
Closes: #82941
Related: #83363
When
RewriteAggregateFunctionWithIfPassrewritessum(if(cond, val, null))tosumIf(_CAST(val, 'Nullable(T)'), cond), the_CASTstays as a FunctionNode on the initiator (no resolve pass runs after optimization). On the remote (secondary) server, the same_CASTgets constant-folded during re-resolution, producing a ConstantNode with result typeNullable(T).The column naming function
calculateActionNodeNameWithCastIfNeededusedgetResultTypefor the inner type annotation, which givesTon the initiator (from the pre-cast constant) butNullable(T)on the remote (from the folded result). This caused a column name mismatch:_CAST(0_UInt8, 'Nullable(UInt8)'_String)_CAST(0_Nullable(UInt8), 'Nullable(UInt8)'_String)Fix: when the constant was folded from
_CAST(receivedFromInitiatorServeris true), use the source_CASTfunction's first argument type as the inner type annotation. This mirrors how the initiator names it by recursively walking the FunctionNode's arguments.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix
THERE_IS_NO_COLUMNexception for distributed queries involvingoptimize_rewrite_aggregate_function_with_ifoptimization when the aggregate function argument requires a cast to Nullable type.Version info
26.6.1.733