Skip to content

Use skip indexes through a CAST that drops Nullable - #124220

Merged
CurtizJ merged 3 commits into
ClickHouse:masterfrom
CurtizJ:skip-index-lossless-conversion-drop-nullable
Oct 7, 2026
Merged

CurtizJ merged 3 commits into
ClickHouse:masterfrom
CurtizJ:skip-index-lossless-conversion-drop-nullable

Conversation

@CurtizJ

@CurtizJ CurtizJ commented Oct 6, 2026 •

Copy link
Copy Markdown
Member

Related: #123239

A follow-up to #123239. bloom_filter and text indexes now also look through a CAST that drops Nullable from the indexed column, e.g. CAST(x, 'String') for x Nullable(String), or a typed JSON path Nullable(String) read as Document.TypeName::String. A user needs this for the second case.

Changelog category (leave one):

  • Improvement

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

bloom_filter and text indexes are now used when a Nullable indexed column is cast to the non-Nullable type, such as CAST(x, 'String') or json.path::String. A query that would throw on a NULL value may not throw when the index skips the granules with NULL.


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

Version info

  • Merged into: 26.10.1.1785-master (included in 26.10 and later)
  • Backported to: 26.9.13.17, 26.8.21.11, 26.7.26.3

`bloom_filter` and text indexes now look through `CAST(x, 'T')` where `x` is
`Nullable(T)`, e.g. a typed JSON path `Nullable(String)` read as `::String`.
The cast does not change a non-NULL value and throws on NULL. A query that
would throw may not throw if the index skips the granules with NULL or the
text index is read directly; this is accepted.

Related: ClickHouse#123239

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [9a262f8]

Summary: ✅


AI Review

Summary

This PR teaches bloom_filter and text indexes to look through CAST operations that drop Nullable, and it correctly blocks the exact text-index direct-read rewrite for the already-reported NOT hasToken(...) case. I still found one correctness regression: making unwrapLosslessConversion drop Nullable by default also changes the text-index row-scan rewrite for preprocessed indexes, so queries that should still raise CANNOT_INSERT_NULL_IN_ORDINARY_COLUMN can silently stop throwing.

Findings
  • ❌ Blockers
    • [src/Storages/MergeTree/RPNBuilder.h:104] allow_drop_nullable = true now affects every caller, but processTextIndexFunction in src/Processors/QueryPlan/Optimizations/optimizeDirectReadFromTextIndex.cpp:781 still unwraps the haystack on the row-scan path. For a preprocessed text index (for example preprocessor = lower(s)), hasToken(CAST(s, 'String'), 'foo') becomes hasToken(lower(s), ...) instead of hasToken(lower(CAST(s, 'String')), ...), so a NULL row no longer raises CANNOT_INSERT_NULL_IN_ORDINARY_COLUMN even when the index is only used for pruning.
    • Suggested fix: keep allow_drop_nullable opt-in at the prune-only analysis call sites, or pass allow_drop_nullable = false in the row-scan preprocessor rewrite so the throwing CAST is preserved there.
Tests
  • ⚠️ Add a stateless text-index regression that uses a preprocessor such as lower(...) together with CAST(nullable_col, 'String'), and verify that the index can still be analyzed for pruning while the row-level filter continues to raise CANNOT_INSERT_NULL_IN_ORDINARY_COLUMN on NULL input.
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required actions: preserve the throwing CAST in the text-index row-scan/preprocessor rewrite, and add regression coverage for that path.

LLVM Coverage Report

Measured on commit 9a262f8.

Metric Baseline Current Δ
Lines 88.30% 88.30% +0.00%
Functions 92.10% 91.80% -0.30%
Branches 79.70% 79.70% +0.00%

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

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Oct 6, 2026
Comment thread src/Storages/MergeTree/RPNBuilder.cpp Resolved
@clickhouse-gh clickhouse-gh Bot added the comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. label Oct 6, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 9a262f8a1 with master 2963d7f2b (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

357 translation units recompiled, 3685 s compile time in total, 357 of them have a recent master baseline.

Job report

…able`

Direct read replaces or short-circuits the predicate, so the `CAST` no longer
throws on NULL: `NOT hasToken(CAST(s, 'String'), 'a')` returned the NULL row
instead of raising `CANNOT_INSERT_NULL_IN_ORDINARY_COLUMN`.

`unwrapLosslessConversion` gets an `allow_drop_nullable` flag. The optimizer
uses direct read only if the predicate also matches the index with
`allow_drop_nullable = false`, otherwise the index is used only to skip granules.

Related: ClickHouse#124220 (comment)

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…ex condition

Do not pass `allow_drop_nullable` through `MergeTreeIndexConditionText`.
The optimizer disables direct read if `unwrapLosslessConversion` of an
argument of the predicate stops earlier with `allow_drop_nullable = false`.

Related: ClickHouse#124220 (comment)

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Comment thread src/Storages/MergeTree/RPNBuilder.h Resolved
@rschu1ze rschu1ze self-assigned this Oct 7, 2026
@@ -0,0 +1,59 @@
-- Skip indexes look through a `CAST` that drops `Nullable` from the indexed column. It does not change a non-NULL
-- value, and it throws on NULL. A query that would throw does not throw if the granules with NULL are skipped.

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.

That CAST(Null, String`) throws is the expected behavior and it says the same here (l. 2).

For my understanding, we cannot throw an exception on the index paths because the bloom filter and text indexes don't store Nulls, right?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, skip indexes don't store nulls.

@CurtizJ
CurtizJ added this pull request to the merge queue Oct 7, 2026
Merged via the queue into ClickHouse:master with commit 8284915 Oct 7, 2026
152 checks passed
@CurtizJ
CurtizJ deleted the skip-index-lossless-conversion-drop-nullable branch October 7, 2026 23:44
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 7, 2026
@CurtizJ CurtizJ added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Oct 8, 2026
@robot-ch-test-poll robot-ch-test-poll added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Oct 8, 2026
robot-ch-test-poll2 added a commit that referenced this pull request Oct 8, 2026
Cherry pick #124220 to 26.7: Use skip indexes through a `CAST` that drops `Nullable`
robot-clickhouse added a commit that referenced this pull request Oct 8, 2026
robot-ch-test-poll2 added a commit that referenced this pull request Oct 8, 2026
Cherry pick #124220 to 26.8: Use skip indexes through a `CAST` that drops `Nullable`
robot-clickhouse added a commit that referenced this pull request Oct 8, 2026
robot-ch-test-poll2 added a commit that referenced this pull request Oct 8, 2026
Cherry pick #124220 to 26.9: Use skip indexes through a `CAST` that drops `Nullable`
robot-clickhouse added a commit that referenced this pull request Oct 8, 2026
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 8, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 8, 2026
Backport #124220 to 26.9: Use skip indexes through a `CAST` that drops `Nullable`
clickhouse-gh Bot added a commit that referenced this pull request Oct 8, 2026
Backport #124220 to 26.8: Use skip indexes through a `CAST` that drops `Nullable`
CurtizJ added a commit that referenced this pull request Oct 8, 2026
Backport #124220 to 26.7: Use skip indexes through a `CAST` that drops `Nullable`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-improvement Pull request with some product improvements pr-must-backport Pull request should be backported intentionally. Use this label with great care! 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants