Skip to content

Fix exception-safety classification for Tuple comparisons - #125978

Open
Shoryamishra61 wants to merge 2 commits into
ClickHouse:masterfrom
Shoryamishra61:fix/125919-tuple-comparison-canthrow
Open

Shoryamishra61 wants to merge 2 commits into
ClickHouse:masterfrom
Shoryamishra61:fix/125919-tuple-comparison-canthrow

Conversation

@Shoryamishra61

@Shoryamishra61 Shoryamishra61 commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Closes: #125919

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):

Fix a bug where comparison of Tuples containing Variant or Dynamic types was incorrectly classified as non-throwing, causing the optimizer to move potentially throwing tuple comparisons ahead of guard conditions in WHERE. Fixes #125919.

Detailed Description

comparisonCanThrow(left, right) in src/Functions/FunctionsComparison.h uses an exact-type equality check (left->equals(*right)) that excludes top-level Variant, Dynamic, and Object. However, when these types are nested inside a Tuple (e.g. Tuple(Variant(String, UInt64))), the outer type is Tuple, causing comparisonCanThrow to return false.

Because FunctionComparison::executeTuple decomposes comparisons into element-wise function evaluations, comparing variant alternatives with incompatible types (e.g. UInt64 vs String) throws NO_COMMON_TYPE at runtime. Misclassifying the tuple comparison as non-throwing allowed FilterStep::canBeMovedToTheFrontOfAndChain to reorder the tuple comparison ahead of guard conditions in WHERE, evaluating it on rows that should have been excluded.

This change:

  1. Adds element-wise recursive inspection in comparisonCanThrow when comparing DataTypeTuple instances, ensuring a tuple comparison is classified as non-throwing if and only if both sides have the same non-zero element count and every element comparison cannot throw.
  2. Excludes DataTypeTuple from the identical-types IColumn::compareAt fast path to enforce the recursive element-wise check.
  3. Extends 05259_short_circuit_reorder_arguments with guarded Tuple(Variant), Tuple(Dynamic), nested tuple, relational operator, and explain plan verification.

Workflow [PR]
Sync PR [sync-upstream/pr/125978]

comparisonCanThrow(left, right) in FunctionsComparison.h checked !which_left.isVariant() && !which_left.isDynamic() && !which_left.isObject() on the outer type. When these types are nested inside a Tuple (such as Tuple(Variant(String, UInt64))), the outer type is Tuple, causing comparisonCanThrow to return false.

Because executeTuple decomposes comparisons into element-wise function calls, comparing variant alternatives with incompatible types throws NO_COMMON_TYPE at runtime. Misclassifying the tuple comparison as non-throwing allowed the optimizer (FilterStep::canBeMovedToTheFrontOfAndChain) to move the tuple comparison ahead of guard conditions in WHERE, evaluating it on rows that should have been excluded.

Fix this by:
1. Adding an element-wise recursive check in comparisonCanThrow for DataTypeTuple, ensuring a tuple comparison is classified as non-throwing if and only if both sides have the same non-zero element count and every element comparison cannot throw.
2. Excluding DataTypeTuple from the identical-types IColumn::compareAt shortcut to enforce the recursive element-wise check.
3. Adding regression tests to 05259_short_circuit_reorder_arguments.

Fixes ClickHouse#125919.
Copilot AI balanced review requested due to automatic review settings October 15, 2026 09:07

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@clickhouse-gh clickhouse-gh Bot added comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... external Author is not a member of the ClickHouse organization labels Oct 15, 2026
@arsenmuk arsenmuk added the can be tested Allows running workflows for external contributors label Oct 16, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [a0b132a]

Summary: ❌

job_name test_name status info comment
Integration tests (arm_binary, 3/4) FAIL
test_backward_compatibility/test_custom_s3_disk_remote_url_allow_hosts.py::test_table_on_custom_s3_disk_loads_after_upgrade FAIL cidb
test_backward_compatibility/test_custom_s3_disk_remote_url_allow_hosts.py::test_database_on_custom_s3_disk_loads_after_upgrade FAIL cidb
Performance Comparison (arm_release, master_head, 5/6) FAIL Performance dashboard
datetime_comparison #6 FAIL dashboard
datetime_comparison #5 FAIL dashboard
rand #0 unstable query history
rand #1 unstable query history

AI Review

Summary

The PR recursively checks Tuple element comparisons in comparisonCanThrow, preventing throwing comparisons involving Variant or Dynamic from moving ahead of guard predicates. No actionable findings. The updated regression test passes in the Fast test report.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Oct 16, 2026

This branch has not been deployed

No deployments
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 comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... external Author is not a member of the ClickHouse organization pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

comparisonCanThrow skips Variant inside Tuple: reordered WHERE throws

3 participants