Skip to content

Unwrap Nullable before deciding the monotonicity of toString - #121081

Draft
navneet83 wants to merge 4 commits into
ClickHouse:masterfrom
navneet83:fix-tostring-monotonicity-nullable-2
Draft

navneet83 wants to merge 4 commits into
ClickHouse:masterfrom
navneet83:fix-tostring-monotonicity-nullable-2

Conversation

@navneet83

@navneet83 navneet83 commented Sep 19, 2026 •

Copy link
Copy Markdown
Member

DRAFT / work in progress. The description is being rewritten; the code is unchanged.

ToStringMonotonicity unwrapped LowCardinality but not Nullable, so for a Nullable key every type guard was skipped and the generic branch declared toString monotonic: an Enum, a Time, or a DateTime in a time zone that turns the clocks back was pruned as if the order of the strings matched the order of the values, and rows were dropped or counted twice. Nullable now comes off like LowCardinality; where toString is monotonic on the wrapped type (String, Date, a fixed-offset DateTime) a Nullable key is pruned like a plain one, and ORDER BY toString(key) reads in order, NULL last in both orders.

CAST(key AS String) resolves to the same struct. On a part holding a NULL the index analysis evaluates the chain over the index column, whose last mark is that NULL, so CAST to a non-Nullable type raises the error the row-level evaluation would, as CAST to Date does on a Nullable(DateTime) key today. Nothing is hidden by pruning. Read in key order, LIMIT stops before the NULL row is reached, as it does for CAST to Date today. 05230 pins the error, the pruning on a part without NULL, and ORDER BY CAST(...) LIMIT with read-in-order on and off.

Closes: #119557
Supersedes: #120680

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

Fixed wrong results of WHERE toString(key) = ... and IN on a Nullable primary key of a type on which toString is not monotonic (Enum, Time, DateTime with a time zone that turns the clocks back): the index treated the function as monotonic and pruned matching granules.

`ToStringMonotonicity` unwrapped `LowCardinality` but not `Nullable`, so for a
`Nullable` key every type guard was skipped and the generic branch declared
`toString` monotonic: an `Enum`, a `Time`, or a `DateTime` whose time zone
turns the clocks back was pruned as if the order of the strings matched the
order of the values, and rows were dropped or counted twice. `Nullable` now
comes off like `LowCardinality`; where `toString` is monotonic on the wrapped
type a `Nullable` key is pruned like a plain one, and `ORDER BY toString(key)`
reads in order, `NULL` last in both orders.

`CAST(key AS String)` resolves to the same struct. On a part holding a `NULL`
the index analysis evaluates the chain over the index column, whose last mark
is that `NULL`, so `CAST` to a non-`Nullable` type raises the same error the
row-level evaluation would, as `CAST` to `Date` does on a `Nullable(DateTime)`
key today; nothing is hidden by pruning. Read in key order, `LIMIT` stops
before the `NULL` row, as it does for `CAST` to `Date` today.
@clickhouse-gh

clickhouse-gh Bot commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [60784b8]

Summary: ✅


AI Review

Summary

This PR fixes the original wrong-results bug by unwrapping Nullable before toString monotonicity checks, and it also closes the previously reported gap in the two-argument toString(d, tz) path by evaluating monotonicity in the formatting time zone. The new stateless coverage exercises the affected Nullable key shapes, the CAST(... AS String) aliasing, and the read-in-order/error behavior around NULL. I did not find a remaining correctness issue in the current diff.

Final Verdict

✅ No new findings.

LLVM Coverage Report

Measured on commit 60784b8.

Metric Baseline Current Δ
Lines 88.10% 88.10% +0.00%
Functions 91.70% 91.70% +0.00%
Branches 79.40% 79.40% +0.00%

Changed lines: Changed C/C++ lines covered: 12/12 (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 Sep 19, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... label Sep 19, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Commit 60784b81c94eaee4d13f8f3f8303aa43e7556ce6 was not compared: the CI logs cluster did not answer (every POST attempt failed with an exception).

See the job log for details.

Comment thread src/Functions/FunctionsConversion.h Resolved
…y check

`KeyCondition` asked `ToStringMonotonicity` about the key's own time zone, while
`toString(d, 'America/New_York')` formats in the argument's: a fixed-offset key
formatted in a zone that turns the clocks back was declared strictly monotonic and
mis-pruned. The constant is substituted into the type as for the date functions.
Predates the `Nullable` unwrap, which reaches this branch for one more wrapper.
@clickhouse-gh clickhouse-gh Bot added comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. and removed comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... labels Sep 23, 2026
@navneet83
navneet83 marked this pull request as ready for review September 23, 2026 23:19
@navneet83
navneet83 marked this pull request as draft October 4, 2026 17:03
@clickhouse-gh clickhouse-gh Bot added comp-key-condition Analysis of a filter against a key. KeyCondition turns a WHERE condition into ranges over the pri... and removed comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. labels Nov 19, 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

comp-key-condition Analysis of a filter against a key. KeyCondition turns a WHERE condition into ranges over the pri... pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wrong results with allow_nullable_key: ToStringMonotonicity ignores Nullable, declaring toString monotonic over Nullable(Enum) / Nullable(DateTime) keys

2 participants