Skip to content

Fix logical error when using LIMIT BY with distributed tables and optimize_const_name_size#95017

Merged
alexey-milovidov merged 2 commits intomasterfrom
fix-distributed-limit-by-const-to-scalar
Jan 24, 2026
Merged

Fix logical error when using LIMIT BY with distributed tables and optimize_const_name_size#95017
alexey-milovidov merged 2 commits intomasterfrom
fix-distributed-limit-by-const-to-scalar

Conversation

@alexey-milovidov
Copy link
Copy Markdown
Member

The ReplaceLongConstWithScalarVisitor in buildQueryTreeForShard was replacing constant nodes with __getScalar function nodes for efficient query distribution. It correctly skipped LIMIT and OFFSET clauses, but did not skip LIMIT BY N and LIMIT BY N OFFSET M clauses.

When the query planner later accessed getLimitByLimit()->as<ConstantNode &>(), it failed with "Bad cast from type DB::FunctionNode to DB::ConstantNode" because the node had been replaced with a function node.

The fix adds checks to also skip getLimitByLimit and getLimitByOffset nodes in the visitor's needChildVisit method.

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

See https://fiddle.clickhouse.com/af71a873-863c-4182-8ef2-78207375ba25

Closes #93322
Closes #89607

…e_const_name_size

The `ReplaceLongConstWithScalarVisitor` in `buildQueryTreeForShard` was replacing
constant nodes with `__getScalar` function nodes for efficient query distribution.
It correctly skipped LIMIT and OFFSET clauses, but did not skip LIMIT BY LIMIT
and LIMIT BY OFFSET clauses.

When the query planner later accessed `getLimitByLimit()->as<ConstantNode &>()`,
it failed with "Bad cast from type DB::FunctionNode to DB::ConstantNode" because
the node had been replaced with a function node.

The fix adds checks to also skip `getLimitByLimit` and `getLimitByOffset` nodes
in the visitor's `needChildVisit` method.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@clickhouse-gh
Copy link
Copy Markdown
Contributor

clickhouse-gh bot commented Jan 24, 2026

Workflow [PR], commit [d4b0f39]

Summary:

job_name test_name status info comment
Stateless tests (amd_debug, AsyncInsert, s3 storage, parallel) failure
00062_replicated_merge_tree_alter_zookeeper_long FAIL cidb
Integration tests (arm_binary, distributed plan, 3/4) failure
test_storage_s3_queue/test_6.py::test_ordered_mode_with_regex_partitioning[S3Queue-0-16-1] FAIL cidb
BuzzHouse (amd_debug) failure
Logical error: 'Inconsistent AST formatting: the query: (STID: 1941-1bfa) FAIL cidb, issue

@alexey-milovidov alexey-milovidov merged commit 37de7bf into master Jan 24, 2026
128 of 134 checks passed
@alexey-milovidov alexey-milovidov deleted the fix-distributed-limit-by-const-to-scalar branch January 24, 2026 16:54
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jan 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-ci pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

3 participants