Skip to content

Add monotonicity for assumeNotNull, coalesce, ifNull to allow granules pruning for primary key and skip indexes - #94754

Merged
nihalzp merged 8 commits into
ClickHouse:masterfrom
nihalzp:assume-not-null-monotonicity
Feb 3, 2026
Merged

nihalzp merged 8 commits into
ClickHouse:masterfrom
nihalzp:assume-not-null-monotonicity

Conversation

@nihalzp

@nihalzp nihalzp commented Jan 21, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Improvement

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

Improve assumeNotNull, coalesce, ifNull to enable primary key and skip index pruning for range predicates when key columns are wrapped in these functions. Closes #94689.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.2.1.183

@clickhouse-gh

clickhouse-gh Bot commented Jan 21, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [22d7d9a]

Summary: ❌

job_name test_name status info comment
Stress test (amd_debug) failure
Logical error: Unexpected return type from A. Expected B. Got C. Action: (STID: 1611-3d1e) FAIL cidb IGNORED
Stress test (arm_asan) failure
Logical error: Unexpected return type from A. Expected B. Got C. Action: (STID: 1611-3d19) FAIL cidb, issue ISSUE EXISTS
Stress test (arm_asan, s3) failure
Logical error: Block structure mismatch in A stream: different number of columns: (STID: 0993-38e6) FAIL cidb, issue ISSUE EXISTS
Upgrade check (amd_msan) failure
Error message in clickhouse-server.log (see upgrade_error_messages.txt) FAIL cidb IGNORED
Integration tests (amd_tsan, 1/6) error IGNORED

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Jan 21, 2026
@yariks5s yariks5s self-assigned this Jan 21, 2026
@nihalzp
nihalzp added this pull request to the merge queue Feb 3, 2026
Merged via the queue into ClickHouse:master with commit 46186d9 Feb 3, 2026
249 of 260 checks passed
@nihalzp
nihalzp deleted the assume-not-null-monotonicity branch February 3, 2026 00:15
@toddpayi

toddpayi commented Feb 3, 2026

Copy link
Copy Markdown

Thank you so much, impressive turn around time!

@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Feb 3, 2026
pull Bot pushed a commit to admariner/ClickHouse that referenced this pull request Apr 24, 2026
Rewrite `<op>(coalesce(a, b), const)` and `<op>(ifNull(a, b), const)` (and the
flipped-argument forms) into `(a <op> const) OR (isNull(a) AND b <op> const)`
inside `cloneDAGWithInversionPushDown`, before index analysis. The existing
`use_skip_indexes_for_disjunctions` machinery in
`MergeTreeDataSelectExecutor::mergePartialResultsForDisjunctions` then combines
per-column pruning across the OR. PK analysis also benefits when one input is a
primary key column.

The rewrite only triggers when both `coalesce`/`ifNull` arguments are
non-constant - the single-constant case is already handled by the monotonic
chain introduced in ClickHouse#94754. Scope is limited to `equals`, `less`, `greater`,
`lessOrEquals`, `greaterOrEquals`; negated and IN forms fall through unchanged.

Gated behind the new setting `allow_key_condition_coalesce_rewrite` (default
`true`).

Co-Authored-By: Claude Opus 4.7 <[email protected]>
pull Bot pushed a commit to admariner/ClickHouse that referenced this pull request Apr 24, 2026
Follow-up to 57be91e: `tryRewriteCoalesceComparison` in `KeyCondition.cpp`
was hard-gated to 2-argument `coalesce`/`ifNull`. Extend it to arbitrary N by
rewriting `<op>(coalesce(a_1, ..., a_N), const)` into

    (a_1 <op> const)
    OR (isNull(a_1) AND a_2 <op> const)
    OR (isNull(a_1) AND isNull(a_2) AND a_3 <op> const)
    ...
    OR (isNull(a_1) AND ... AND isNull(a_{N-1}) AND a_N <op> const)

so per-column PK and skip indexes on each `a_i` are consulted via the existing
`use_skip_indexes_for_disjunctions` machinery in
`MergeTreeDataSelectExecutor::mergePartialResultsForDisjunctions`. The final
branch needs no trailing `isNull(a_N)` - if execution reaches it, `a_N` is the
`coalesce` result regardless of nullability.

N=2 produces the same DAG shape as before, so `04114_coalesce_two_column_index_pruning`
still passes byte-for-byte. Added `04115_coalesce_n_column_index_pruning`
covering N=3, N=4, flipped-argument form, partial-column-indexed pruning,
each of the 5 ordered operators, and the `allow_key_condition_coalesce_rewrite=0`
off-baseline.

The bail-out on any constant argument is kept intentionally: the 2-arg single-
constant case is still handled by the monotonic-chain path from ClickHouse#94754, and
a constant in the middle of the list would truncate `coalesce` (short-circuit
on the first non-NULL), which needs separate handling.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-improvement Pull request with some product improvements 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.

Enable skip index usage through monotonic function wrappers (assumeNotNull, coalesce, ifNull)

4 participants