Repository navigation
Cherry pick #118606 to 26.7: Proper partition pruning with dates - #119242
Merged
Merged
Conversation
…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
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-picklocally.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:
pr-cherrypicklabel from the PRYou also need to check the Original pull-request for
pr-backports-createdlabel, and delete if it's presented thereThe PR source
The PR is created in the CI job