Skip to content

Fix top-K dynamic filtering for empty Tuple columns - #114182

Merged
fm4v merged 8 commits into
ClickHouse:masterfrom
Boulea7:fix/topk-empty-tuple-dynamic-filter
Aug 28, 2026
Merged

fm4v merged 8 commits into
ClickHouse:masterfrom
Boulea7:fix/topk-empty-tuple-dynamic-filter

Conversation

@Boulea7

@Boulea7 Boulea7 commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor

Closes: #113419
Related: #113406

Tuple() can be sorted, but the scalar comparison functions used by __topKFilter reject zero-sized tuples. This change skips top-K dynamic filtering when the sort column is directly Tuple(). A nested empty Tuple, including through Nullable, remains eligible for the optimization and is routed through the general column-comparison path. Composite types with their own comparison implementation, such as Array(Tuple()), are not misclassified and keep vectorized filtering.

Local validation used the final SQL test blob against the existing debug binary: all 10 Praktika stateless runs passed, check_cpp passed, and the produced output matched the reference with an empty diff. These were local checks, not upstream CI.

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fixes ORDER BY ... LIMIT queries on Tuple() columns raising a NOT_IMPLEMENTED exception when top-K dynamic filtering is enabled. Direct empty tuples now skip the optimization, while supported nested composite types continue to use top-K filtering.

Version info

  • Merged into: 26.9.1.279 (included in 26.9 and later)
  • Backported to: 26.8.1.2028, 26.7.6.40, 26.6.4.36

Change-Id: If95684f921da6ee7ea73102af169ebb75555e4a1
Signed-off-by: zhanglangning <[email protected]>
Change-Id: Ib8435a6a5a02aa37454163851262b71c56e95d4b
Signed-off-by: zhanglangning <[email protected]>
Change-Id: I71f267433345104a41885f456db0cf4af3d31a4e
Signed-off-by: zhanglangning <[email protected]>
Change-Id: Ib415021efb6a19653ac423d2d4bc0a6d62fe40ba
Signed-off-by: zhanglangning <[email protected]>
Change-Id: I0fc045e87e501200bc76e8a4a90a0f43c6c90576
Signed-off-by: zhanglangning <[email protected]>
Change-Id: I469239655dd4073b28b9560103bae45deb8bea35
Signed-off-by: zhanglangning <[email protected]>
Change-Id: I1f93f7e4a895e2496a414d790c9990efd1873108
Signed-off-by: zhanglangning <[email protected]>
Change-Id: I17001f5d22ea53782b913b016b29046e278cf0ab
Signed-off-by: zhanglangning <[email protected]>
@CLAassistant

CLAassistant commented Aug 10, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@SmitaRKulkarni SmitaRKulkarni added the can be tested Allows running workflows for external contributors label Aug 11, 2026
@clickhouse-gh

clickhouse-gh Bot commented Aug 11, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [a3ad814]

Summary: ❌

job_name test_name status info comment
Stress test (amd_asan_ubsan) FAIL
Sanitizer (STID: 1367-26c6) FAIL cidb, issue ISSUE EXISTS
Stress test (amd_tsan) FAIL
Hung check failed, possible deadlock found FAIL cidb IGNORED
Stress test (arm_release) FAIL
Hung check failed, possible deadlock found FAIL cidb IGNORED

AI Review

Summary

This PR fixes ORDER BY ... LIMIT on Tuple() sort keys under top-K dynamic filtering by skipping the optimization for direct empty tuples and routing nested empty-tuple carriers through the column-comparison path. I did not find a remaining correctness gap in the touched planner/runtime paths, and the focused regression covers the key supported carriers (Tuple(), nested tuples, nullable nesting, and Array(Tuple())).

Final Verdict

Status: ✅ Approve

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.70% 86.70% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 79.00% 79.00% +0.00%

Changed lines: Changed C/C++ lines covered: 25/25 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Aug 11, 2026
/// `Tuple` comparison functions reject an empty `Tuple` nested inside another `Tuple`,
/// while the column comparison path supports it. Other composite types have their
/// own comparison implementations, so only descend through `Tuple` and `Nullable`.
bool hasEmptyTuple(const DataTypePtr & type)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure this code check already exists elsewhere and can be reused?

@PedroTadim

Copy link
Copy Markdown
Member

@KochetovNicolai @shankar-iyer easy PR to review?

@fm4v
fm4v added this pull request to the merge queue Aug 28, 2026
Merged via the queue into ClickHouse:master with commit 6ea4e96 Aug 28, 2026
178 of 182 checks passed
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added pr-synced-to-cloud The PR is synced to the cloud repo pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR labels Aug 28, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Aug 28, 2026
clickhouse-gh Bot added a commit that referenced this pull request Aug 28, 2026
Backport #114182 to 26.8: Fix top-K dynamic filtering for empty Tuple columns
clickhouse-gh Bot added a commit that referenced this pull request Aug 28, 2026
Backport #114182 to 26.6: Fix top-K dynamic filtering for empty Tuple columns
@clickgapai

Copy link
Copy Markdown
Contributor

@Boulea7 @fm4v — ClickGap found the following in this PR:

Close anything that's wrong or already addressed.

groeneai added a commit to groeneai/ClickHouse that referenced this pull request Aug 29, 2026
One conflict, in src/Processors/QueryPlan/Optimizations/optimizeTopK.cpp,
and it was an add/add on adjacent include lines: master's ClickHouse#114182 added
<DataTypes/DataTypeTuple.h> for its empty-Tuple guard on the same line this
branch had added <DataTypes/IDataType.h>. Both includes are used, so both are
kept in alphabetical order.

The two changes are independent conditions on the same use_dynamic_filtering
initializer and both stay live: master's zero-element-Tuple skip and this
branch's read-in-order projection guard. readWouldBeInOrderForColumn is
byte-identical to the pre-merge revision.
clickhouse-gh Bot added a commit that referenced this pull request Aug 29, 2026
Backport #114182 to 26.7: Fix top-K dynamic filtering for empty Tuple columns
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Sep 4, 2026
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Sep 4, 2026
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Sep 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.6-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Top-K dynamic filtering over a Tuple() column: ORDER BY ALL DESC LIMIT n throws NOT_IMPLEMENTED "Comparison of zero-sized tuples" at default settings

7 participants