Skip to content

Unwrap Nullable before deciding the monotonicity of toString - #120680

Closed
navneet83 wants to merge 6 commits into
ClickHouse:masterfrom
navneet83:fix-tostring-monotonicity-nullable
Closed

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

Conversation

@navneet83

@navneet83 navneet83 commented Sep 17, 2026 •

Copy link
Copy Markdown
Member

Closes: #119557

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 WHERE CAST(key AS String) ... over a Nullable(Enum), Nullable(DateTime) or Nullable(Time) primary key with allow_nullable_key.

Documentation entry for user-facing changes

ToStringMonotonicity unwrapped LowCardinality but not Nullable, so for a Nullable key every type guard was skipped and the generic integer branch declared toString monotonic; key analysis then mapped granule bounds through toString and dropped or over-counted granules. Nullable is now unwrapped like LowCardinality; CAST(key AS String) resolves to the same struct. Where toString is monotonic on the wrapped type (String, Date, a fixed-offset DateTime), a Nullable key is now used for pruning as a plain one is, 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; 05230 pins the error and the pruning on a part without NULL.


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

`ToStringMonotonicity` stripped `LowCardinality` but not `Nullable`, so over a
`Nullable(Enum)`, a `Nullable(DateTime)` in a zone that changes its offset or a
`Nullable(Time)` primary key every type guard was bypassed and the generic
same-digit-count branch declared `toString` monotonic; key analysis then mapped
granule bounds through `toString` and returned wrong counts. `Nullable` now comes
off like `LowCardinality`. Where `toString` is monotonic on the wrapped type, a
`Nullable` key is now pruned as a plain one is. `CAST(k AS String)` resolves to
the same struct.

ClickHouse#119557
@clickhouse-gh

clickhouse-gh Bot commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [7406eed]

Summary: ❌

job_name test_name status info comment
Stress test (arm_msan) FAIL
Cannot start clickhouse-server FAIL cidb
Check failed FAIL cidb
Build profile diff ERROR

AI Review

Summary

This PR fixes the wrong monotonicity verdict for toString and CAST(... AS String) on Nullable MergeTree keys by unwrapping Nullable before the ToStringMonotonicity type dispatch. I re-checked the current change against the existing review threads and the relevant KeyCondition / read-in-order consumers; the remaining ORDER BY CAST(...) LIMIT behavior is now an explicit, tested decision, and I do not see an unresolved correctness gap in the current code.

Final Verdict
  • Status: ✅ Approve

LLVM Coverage Report

Measured on commit 7406eed.

Metric Baseline Current Δ
Lines 88.40% 88.40% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 80.60% 80.60% +0.00%

Changed lines: Changed C/C++ lines covered: 8/8 (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 17, 2026
@rschu1ze

Copy link
Copy Markdown
Member

@navneet83 Can you please review #118581?

Comment thread src/Functions/FunctionsConversion.h Resolved
…ill throws on it

`NULL` sorts last and reaches the monotonicity check as an infinite bound. With `Nullable` unwrapped,
`CAST(s AS String) = 'cc'` over a `Nullable(String)` key pruned that granule and returned a count
where it used to raise `CANNOT_INSERT_NULL_IN_ORDINARY_COLUMN`.
@clickhouse-gh clickhouse-gh Bot added the comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... label Sep 17, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing commit 7406eed5348cfff8a46debb40aee304ca4973cd9 with master failed: RuntimeError: CI logs cluster query failed: SELECT file, symbol, sumIf(size, side = 'pr….

See the job log for details.

Comment thread src/Functions/FunctionsConversion.h Resolved
`isNull` on a range bound is also the infinite bound of a part's first and last
granule, so the guard in `ToStringMonotonicity` cost `toString` those granules on
every `Nullable` key. `CAST` to a non-`Nullable` type is the function that throws
on a `NULL`, so it is `CAST` that treats a range open on one side as not monotonic;
`toString` prunes a `Nullable` key like a plain one.
Comment thread src/Functions/FunctionsConversion.cpp Resolved
…tself

`KeyCondition` evaluates a function chain over the whole index column the
first time a mark needs it, and on a part holding a `NULL` the last mark is
that `NULL`, so `CAST` to a non-`Nullable` type raises the row-level error
during the analysis whether or not a range is declared non-monotonic, as
`CAST` to `Date` does on a `Nullable(DateTime)` key today. The guard only
traded `toString` pruning on a part's last granule for nothing, so it goes;
`05230` pins the error on a part with a `NULL` and the pruning on one without.
Comment thread src/Functions/FunctionsConversion.h Resolved
Read in key order, `LIMIT` stops before the `NULL` row and the query returns,
as `ORDER BY CAST(d AS Date) LIMIT 1` does on a `Nullable(DateTime)` key
today; without the order the whole column is evaluated and `CAST` raises.
@navneet83

Copy link
Copy Markdown
Member Author

Superseded by #121081, squashed.

@navneet83 navneet83 closed this Sep 19, 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-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

3 participants