Repository navigation
Fix Bad cast FunctionNode to ConstantNode with WITH FILL on Distributed - #109938
Conversation
ReplaceLongConstWithScalarVisitor replaced WITH FILL FROM/TO/STEP/STALENESS constants with __getScalar function nodes when sending a query to remote shards. The planner reads those values directly via as<ConstantNode &>() in extractWithFillValue (PlannerSorting.cpp), so a FunctionNode there caused a "Bad cast from type DB::FunctionNode to DB::ConstantNode" abort in debug/sanitizer builds (caught exception in release). Skip the WITH FILL constant children in needChildVisit, mirroring the existing LIMIT/OFFSET/LIMIT BY guard. optimize_const_name_size = 0 replaces all constants and reliably triggers the issue. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-3:20260709-200500 |
|
cc @PedroTadim @vdimir — could you review? This mirrors the LIMIT/OFFSET/LIMIT BY guard in |
|
Workflow [PR], commit [6d638af] Summary: ✅
AI ReviewSummaryThis PR fixes the reported Findings
Final VerdictChanges needed before merge: the fix closes the reported |
| return false; | ||
| } | ||
|
|
||
| if (auto * sort_node = parent->as<SortNode>()) |
There was a problem hiding this comment.
This fixes the SortNode branch, but the same ConstantNode invariant is still broken for window frame offsets. ReplaceLongConstWithScalarVisitor will still traverse WindowNode::getFrameBeginOffsetNode / getFrameEndOffsetNode, and src/Analyzer/WindowNode.cpp serializes those children back into the SQL we send to shards. Distributed window queries are already supported here (tests/queries/0_stateless/01568_window_functions_distributed.sql), so with SETTINGS optimize_const_name_size = 0 a query like ... OVER (ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) on a Distributed / remote table will still turn that 1 into __getScalar(...) on the shard query. The shard analyzer then rejects it at src/Analyzer/Resolve/QueryAnalyzer.cpp and src/Analyzer/Resolve/QueryAnalyzer.cpp because frame offsets must also stay ConstantNodes.
I think this visitor needs the same skip for WindowNode frame offset children, plus a regression test that covers a distributed window query with a bounded ROWS/RANGE frame.
|
Thanks for flagging this. I checked whether window frame offsets hit the same The But the shard does not reject it and results are correct. Why WITH FILL is different. The I reproduced the WITH FILL crash locally to confirm the harness, then ran window frame offset queries (ROWS + RANGE, begin + end offsets) over test_shard_localhost, So the frame-offset skip isn't needed for correctness here, and per our contributing guidance I'd rather not add a guard I can't back with a failing repro. If you have a shape that does trigger it (a planner path that reads the frame-offset node directly, or a config where |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 11/11 (100.00%) · Uncovered code |
CI finish ledger — 6d638afCI fully finished (Finish Workflow: pass, Mergeable Check: pass, CH Inc sync: pass). No failing checks on this head.
Session id: cron:our-pr-ci-monitor:20260710-020000 |
|
@yakov-olkhovskiy maybe you can review this? |
…e_const_name_size LIMIT BY bad-cast, unrelated stress failure)
…an shard `04401_distributed_with_fill_const_to_scalar` (added by ClickHouse#109938) runs distributed `ORDER BY ... WITH FILL` queries. The CI `distributed plan` shard installs `users.d/distributed_plan.xml`, which turns on `serialize_query_plan` globally. `serializeSortDescription` throws `NOT_IMPLEMENTED: WITH FILL is not supported in serialized sort description`, so every `WITH FILL` query fails there. On master the test fails ~6/8 times in `Stateless tests (amd_asan_ubsan, distributed plan, parallel)` while being green in every default-settings shard (`serialize_query_plan` defaults to 0). The test targets the query-tree shard rewrite in `buildQueryTreeForShard` (the `Bad cast from type DB::FunctionNode to DB::ConstantNode` fix), which is only exercised with `serialize_query_plan = 0` anyway. Pin the setting to 0 on each query, mirroring the existing opt-out used by other distributed tests (e.g. `02535_max_parallel_replicas_custom_key_rmt`, `03403_parallel_blocks_marshalling_for_distributed`). Results are unchanged, so the reference file stays identical. Surfaced on the CI report of PR ClickHouse#96823 (shard `Stateless tests (amd_asan_ubsan, distributed plan, parallel)`). Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Hi — this looks like a backport candidate for |
…lar test Issue ClickHouse#110237 reported the same Bad cast FunctionNode->ConstantNode as ClickHouse#109938 but via the STALENESS bound (extractWithFillValueWithIntervalKind), which the 04401 regression test did not exercise. Add a DESC WITH FILL STALENESS -2 query and a negative TO/STEP query so a regression that drops only the staleness/step guard line is caught. Co-Authored-By: Claude Opus 4.8 <[email protected]>
…th-fill-const-scalar-bad-cast Fix Bad cast FunctionNode to ConstantNode with WITH FILL on Distributed
…an shard `04401_distributed_with_fill_const_to_scalar` (added by ClickHouse#109938) runs distributed `ORDER BY ... WITH FILL` queries. The CI `distributed plan` shard installs `users.d/distributed_plan.xml`, which turns on `serialize_query_plan` globally. `serializeSortDescription` throws `NOT_IMPLEMENTED: WITH FILL is not supported in serialized sort description`, so every `WITH FILL` query fails there. On master the test fails ~6/8 times in `Stateless tests (amd_asan_ubsan, distributed plan, parallel)` while being green in every default-settings shard (`serialize_query_plan` defaults to 0). The test targets the query-tree shard rewrite in `buildQueryTreeForShard` (the `Bad cast from type DB::FunctionNode to DB::ConstantNode` fix), which is only exercised with `serialize_query_plan = 0` anyway. Pin the setting to 0 on each query, mirroring the existing opt-out used by other distributed tests (e.g. `02535_max_parallel_replicas_custom_key_rmt`, `03403_parallel_blocks_marshalling_for_distributed`). Results are unchanged, so the reference file stays identical. Surfaced on the CI report of PR ClickHouse#96823 (shard `Stateless tests (amd_asan_ubsan, distributed plan, parallel)`). Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…lar test Issue ClickHouse#110237 reported the same Bad cast FunctionNode->ConstantNode as ClickHouse#109938 but via the STALENESS bound (extractWithFillValueWithIntervalKind), which the 04401 regression test did not exercise. Add a DESC WITH FILL STALENESS -2 query and a negative TO/STEP query so a regression that drops only the staleness/step guard line is caught. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a
Bad cast from type DB::FunctionNode to DB::ConstantNodelogical error (server abort in debug/sanitizer builds) when runningSELECT ... ORDER BY ... WITH FILLagainst aDistributedtable with a lowoptimize_const_name_size.Description
When a query is sent to remote shards,
ReplaceLongConstWithScalarVisitor(src/Storages/buildQueryTreeForShard.cpp) replaces large constants with__getScalar('hash')function nodes. It already skipsLIMIT/OFFSET/LIMIT BYconstants, which the planner reads directly asConstantNode, but it did not skip theWITH FILL FROM/TO/STEP/STALENESSconstants under aSortNode.extractWithFillValueinsrc/Planner/PlannerSorting.cppreads those values vianode->as<ConstantNode &>(), so a__getScalarFunctionNodethere causedBad cast from type DB::FunctionNode to DB::ConstantNode— an abort in debug/sanitizer builds, a caught exception in release.Fix: extend
needChildVisitto also skip theWITH FILLconstant children, mirroring the existingLIMITguard.optimize_const_name_size = 0replaces all constants with__getScalarand reliably triggers the issue; it is CI-randomized, which is why the AST fuzzer and several stress runs surfaced this.Found by the AST fuzzer / stress tests.
Example CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=101791&sha=1859ccbc35ec&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%29
Closes #109992
Version info
26.7.1.758(included in26.7and later)