Skip to content

Reject a non-constant String compared with a constant of an incompatible type at analysis - #121719

Closed
fm4v wants to merge 1 commit into
ClickHouse:masterfrom
fm4v:nik/comparison-reject-non-const-string-at-analysis
Closed

fm4v wants to merge 1 commit into
ClickHouse:masterfrom
fm4v:nik/comparison-reject-non-const-string-at-analysis

Conversation

@fm4v

@fm4v fm4v commented Sep 23, 2026 •

Copy link
Copy Markdown
Member

Related: #121006
Related: #121146
Related: #111924

FunctionComparison::getReturnTypeImpl admits a String/FixedString operand against any other type ("Everything can be compared with string by conversion"), but at execution only a constant string is converted to the other operand's type (executeWithConstString). A non-constant string against a constant without a common supertype (s != 1, toString(uuid_col) != toUUID(...), s = toDate(...)) always throws NO_COMMON_TYPE, but only when the comparison is executed. So whether the query fails depends on whether an optimization removes the comparison first:

SELECT count() FROM numbers(3) WHERE toString(number) != 1 AND toString(number) != 2;                           -- NO_COMMON_TYPE
SELECT count() FROM numbers(3) WHERE toString(number) != 1 AND toString(number) != 2 AND toString(number) != 3;  -- 1 (rewritten to NOT IN)
SELECT count() FROM numbers(3) WHERE toString(number) = 1 AND 0;                                                 -- 0 (never executed)

This PR checks that pair when the comparison is resolved, if the other operand is a constant, so the error is raised during analysis, before any optimizer pass, for every chain length and with both analyzers. This replaces #121146, which tried to make LogicalExpressionOptimizerPass predict which comparisons would throw by re-implementing the execution rules of FunctionComparison in the optimizer.

Not affected: a constant string against anything (number = '1', '1' = 1), Enum constants (common type String), FixedString(16) against IPv6, tuples (a string is parsed as a tuple), Dynamic/Variant/JSON constants, and pairs where neither side is a constant, which keep the existing execution-time behaviour.

One more case becomes an error: a string column that is constant only at execution, e.g. SELECT count() FROM (SELECT 'a' AS s) WHERE s = 1, because the analyzer does not see its constness. This relied on the execution-time coercion by accident and fails as soon as the column is materialized.

The query in #121006 keeps failing, now consistently: in 26.6 it succeeded only because the NOT IN rewrite built a set from the UUID constants that could never match a String (#117240), silently dropping the filter.

Changelog category (leave one):

  • Backward Incompatible Change

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

Comparing a non-constant String or FixedString expression with a constant of a type that has no common type with it (for example s != 1 or toString(uuid) = toUUID('...')) now throws NO_COMMON_TYPE during query analysis. Previously the error was raised only when the comparison was executed, so the same condition was accepted when an optimization removed it, e.g. a chain of three or more such comparisons rewritten to NOT IN, or a filter that was never executed. Use a constant of the string type, e.g. s != '1', or convert one side explicitly.


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

…tible type at analysis

`FunctionComparison` admitted a `String`/`FixedString` operand against any type when resolving, but at execution only a constant string is converted to the other operand's type, so `s != 1` with a non-constant `s` failed with `NO_COMMON_TYPE` only when executed. Optimizations that remove or merge the comparison first (the chain rewrite to `IN`/`NOT IN` at `optimize_min_*_chain_length`, a constant-false filter) made the same predicate accepted or rejected depending on chain length.

Check this pair in `getReturnTypeImpl` when the other operand is a constant, so the error is raised during analysis regardless of later optimizations.

Related: ClickHouse#121006
Related: ClickHouse#121146
@clickhouse-gh

clickhouse-gh Bot commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [25a9746]

Summary: ❌

job_name test_name status info comment
Fast test FAIL
02513_date_string_comparison FAIL cidb
03925_variant_adaptor_const_lowcardinality FAIL cidb
05241_comparison_non_const_string_with_constant FAIL cidb
04330_join_disjunctions_pushdown_using_type_mismatch FAIL cidb
03362_optimize_using_constraints_type_mismatch FAIL cidb
01268_DateTime64_in_WHERE FAIL cidb
04004_const_column_get_extremes FAIL cidb
Code Review DROPPED
Fast test (arm_darwin) DROPPED
Build (amd_debug) DROPPED
Build (amd_asan_ubsan) DROPPED
Build (amd_tsan) DROPPED
Build (amd_msan) DROPPED
Build (amd_binary) DROPPED
Build (arm_debug) DROPPED
Build (arm_asan_ubsan) DROPPED

@clickhouse-gh clickhouse-gh Bot added pr-backward-incompatible Pull request with backwards incompatible changes comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... labels Sep 23, 2026
@fm4v fm4v closed this Oct 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... pr-backward-incompatible Pull request with backwards incompatible changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant