Skip to content

Cherry pick #118606 to 26.7: Proper partition pruning with dates - #119242

Merged
robot-clickhouse merged 15 commits into
backport/26.7/118606from
cherrypick/26.7/118606
Sep 10, 2026
Merged

robot-clickhouse merged 15 commits into
backport/26.7/118606from
cherrypick/26.7/118606

Conversation

@robot-ch-test-poll2

Copy link
Copy Markdown
Contributor

Original pull-request #118606

Do not merge this PR manually

This pull-request is a first step of an automated backporting.
It contains changes similar to calling git cherry-pick locally.
If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.

The check results does not matter at this step - you can safely ignore them.

Before you resolve anything

Conflicts are often caused by a prerequisite change that has not been backported yet, rather than by a real divergence. The bot re-tries this cherry-pick against the release branch on every run, so if that is the case here it will merge itself as soon as the prerequisite lands, and you will see a comment saying so. Manual resolution is only needed while the conflict persists.

Troubleshooting

If the conflicts were resolved in a wrong way

If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

scanhex12 and others added 14 commits September 7, 2026 15:16
…nversion

`ToDateMonotonicity` and `ToDateTimeMonotonicity` ask `WhichDataType` about the
argument type as it comes. For `Nullable(DateTime64(6))` none of the branches
match, so `toDate`, `toDate32`, `toDateTime` and `toDateTime64` are reported as
non-monotonic and key analysis drops the atom: a `WHERE toDate(x) = ...` over a
nullable key reads every granule. `IFunctionDateOrDateTime` already unwraps the
wrapper, which is why `toYYYYMMDD(x)` uses the index in the same query while
`toDate(x)` does not.

Monotonicity of a value-wise conversion does not depend on the wrapper: `NULL`
maps to `NULL` and keeps its place in the ordering. Unwrap `LowCardinality` and
`Nullable` before the type dispatch, so the decision is made about the value
type. For an argument that is neither, this changes nothing.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…ed column

Partition pruning builds a `KeyCondition` whose key is the partition expression,
for example `day(ts)`, and the source column is offered as the key's argument.
That covers a predicate on the column itself, because
`canConstantBeWrappedByMonotonicFunctions` finds `ts` among the key
subexpressions and pushes the constant through `day`. It does not cover
`toDate(ts) = ...`: that expression is not a subexpression of the key, and
composing the two directions (invert `toDate` on the constant, then apply `day`)
is something `KeyCondition` does not do. So any filter that wrapped the
partition source column in a function was ignored, and a table partitioned by
`days(created_at)` was read in full.

The partition value of a monotonic transform is a bound on the source column:
every row of the file shares the partition value, so `day = 19722` says the
column is within `[2023-12-31 00:00:00, 2023-12-31 23:59:59.999999]`. Derive
that range and hand it to the per-column conditions that pruning already builds
from the manifest bounds. Those have the source column as their key, so
`KeyCondition` handles a monotonic function chain over it, and no new condition
analysis is needed. The derived range does not come from the manifest bounds, so
this also prunes files that carry no column statistics at all.

The month and year boundaries are the calendar arithmetic of `DateLUT` around
`ExtendedDayNum(0)`, which is the epoch the transforms count from; a partition
value no date can represent saturates there and is rejected by a round trip
through the forward transform.

`day`, `hour`, `month` and `year` are inverted. `bucket` and `void` cannot be,
and `truncate` is left for later; an `identity` partition already works, because
there the source column is the key itself. A non-null partition value implies
the source column has no nulls in that file, because a transform returns null
for a null input, so the range needs no separate null check.

Closes: #103433

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
#103447 rewrote the filter itself,
substituting `toDate` with `toRelativeDayNum` and recomputing the constant, and
the review named two ways that loses rows: a constant that is not aligned to the
transform's granularity (`toStartOfHour(ts) < '... 10:30:00'` must keep the
10:00 hour, the rewrite dropped it) and a value before 1970, where the unsigned
relative transforms invert the ordering (`toRelativeDayNum('1969-12-31')` is
`65535` while `1970-01-01` is `0`).

Deriving the source range from the partition value leaves the predicate alone,
so both are decided by `KeyCondition` over an exact range instead of by an
equivalence that has to be proven. Test them: a non-aligned constant on the
hour, day and month transforms, and a table whose `days` and `years` partition
values are negative.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Refining a closed interval into a finer unit is the same arithmetic three times
over: an hour into seconds, a day into seconds, and a second into the ticks of
the column's scale. `[a, b]` becomes `[a * factor, (b + 1) * factor - 1]`, so
say that once and drop the `multiplyChecked`/`addChecked` pair whose only job
was to thread the overflow checks through those expressions.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
A partition value comes straight from manifest metadata, so the arithmetic that
turns it into a range must not assume it is in range. Carry the interval
half-open, where each step is one `addOverflow` or `mulOverflow` away from the
previous one, and close it at the end with a checked subtraction: a value no
date can represent now fails a check and leaves the file unpruned instead of
wrapping around through signed overflow.

The interval is closed rather than handed to `KeyCondition` half-open on
purpose. Key analysis applies the filter's functions to the two ends of the
range, so the excluded upper end of a January partition maps to February and
`toStartOfMonth(ts) = '2024-02-01'` keeps the January file; the last value
inside the interval maps to January and prunes it.

`canBePruned` reads the partition value once instead of naming it in two
scopes, which `clang-tidy` reports as shadowing when the outer one is hoisted.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
`rangeOfPartitionValue` repeated the same three steps in each branch: build the
interval, take the last value inside it, wrap the two ends into a `Range`, with
the emptiness of every step threaded by hand. What the branches actually differ
in is the unit the column stores its values in, so let the emptiness propagate
through the helpers and leave one line per type.

Both ends of a month or year interval are now checked by the round trip through
the forward transform. The calendar saturates at the top of its range, where one
month past the last one is 9999-12-31 rather than the first excluded day, and
taking that for the excluded end would drop 9999-12-31 out of the interval and
let `toDate32(ts) = '9999-12-31'` prune the file that holds it.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
`DataTypes/DataTypeLowCardinality.h` was included twice, which fails the
clang-tidy check with `readability-duplicate-include`. The second include came
with the merge of `master`.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
`unwrapTypeForMonotonicity` was `removeLowCardinalityAndNullable` written again, as
pointed out in review. That function takes a `DataTypePtr`, while a monotonicity
implementation only gets a `const IDataType &`, so it now has an overload for a
reference and the local copy in `FunctionsConversion.h` is gone.

A reference overload rather than `type.getPtr()`: `getPtr` is `shared_from_this`,
which throws for a type that no `shared_ptr` owns, and monotonicity is called that
way, e.g. `gtest_conversion_monotonic.cpp` passes stack-allocated types.

PR: #118606

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Reverts the `removeLowCardinalityAndNullable` overload for `const IDataType &`:
the two monotonicity implementations that need the unwrapping do it themselves.

PR: #118606

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…todate

Proper partition pruning with dates
@robot-ch-test-poll2 robot-ch-test-poll2 added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default labels Sep 10, 2026
Resolve the cherry-pick conflicts of #118606 for 26.7:

- `src/Functions/FunctionsConversion.h`: keep only the new `get` signature of
  `ToDateMonotonicity`; the `getPreimage` overload next to it comes from another
  change that is not in 26.7.
- `src/Storages/ObjectStorage/DataLakes/Iceberg/ManifestFilesPruning.cpp`: hoist
  `partition_value` out of the partition key block and keep the 26.7 way of
  computing `has_no_nulls`, so that the new partition range pruning runs even
  when a column has no min/max hyperrectangle in the manifest entry.
- `src/Storages/ObjectStorage/DataLakes/Iceberg/ManifestFile.h`,
  `src/Storages/ObjectStorage/DataLakes/Iceberg/ManifestFileIterator.cpp`: add
  `PartitionSpecsEntry::tuple_index`, taken from #116129 which is not in 26.7.
  The pruning needs it to address the partition tuple: entries whose source
  column is missing from the manifest schema are skipped, so the position in
  `PartitionSpecification` is not the position in the partition tuple.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@robot-clickhouse
robot-clickhouse merged commit 5a49839 into backport/26.7/118606 Sep 10, 2026
@robot-clickhouse
robot-clickhouse deleted the cherrypick/26.7/118606 branch September 10, 2026 14:59
@clickhouse-gh clickhouse-gh Bot added the external Author is not a member of the ClickHouse organization label Sep 11, 2026
@maxknv maxknv removed the external Author is not a member of the ClickHouse organization label Sep 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants