Repository navigation
Cherry pick #117238 to 26.9: Stop claiming injectivity for toString of a folding date-time and toFixedString of a String - #125419
Merged
robot-clickhouse-ci-1 merged 22 commits intoOct 13, 2026
Conversation
…toFixedString` of a `String` Both claims were unconditional and both are false: - `toString` of a date-time in a time zone with a UTC offset transition is many-to-one: in a fall-back hour two distinct instants print the same wall-clock string. - `toFixedString` NUL-pads a `String` to the target width, so 'a' and 'a\0' both become the same `FixedString(2)`. A `FixedString` argument is fine - all its values have the same length, so padding appends the same suffix to every one of them. An increasing number of default-on rewrites take `isInjective` as a proof, so the two claims produced wrong results in several places at once: `uniqExact` counted fold duplicates separately, `optimize_truncate_order_by_after_group_by_keys` dropped an explicit `ORDER BY` tiebreaker, `GROUP BY` and `LIMIT n BY` over such a key split one logical group, and `allow_window_partitions_independently` skipped the scatter and evaluated one logical window partition per table partition. Both answers depend on the argument types, so a caller that passes none gets no claim. Three query-plan call sites passed nothing while holding an `ActionsDAG` node that knows its children's types; they now pass them, which keeps `toString` of an integer injective for read-in-order and for the outer-to-inner join conversion. Closes: #116931 Closes: #116935 Closes: #116828 Closes: #103686
`ConcatOverloadResolver::isInjective` returned `arguments.size() == 1` unconditionally, on the comment "concat delegates to toString which is injective". Now that `toString` answers per argument type, the resolver and the function it builds disagreed for a date-time argument - `FunctionsStress.stress` reports the mismatch. Ask `toString` instead.
… claims further
Three cases still overclaimed injectivity on the previous head:
- Multi-argument `toString`. The rendering time zone comes from the second argument, not from the
type of the first, so `toString(dt, 'Europe/Amsterdam')` folds even when `dt` is
`DateTime('UTC')`, and `toString(x, NULL)` maps every row onto `NULL`. Only the single-argument
form is claimed now.
- `toString(Date32)`. Day numbers out of the type range are saturated to `0000-01-01` and
`9999-12-31` when formatted, exactly as `ToStringMonotonicity` already documents, so two distinct
`Date32` values can render into the same text.
- `toFixedString(FixedString(M), N)` with `M > N`. Execution throws `String too long for type
FixedString(N)`, so eliminating the call would turn a query that must fail into one that
succeeds. The claim now requires a constant target width with `M <= N`.
Also treat the type-erased `Variant`, `Dynamic` and `Object` as collapsing: their alternatives
render into a common text space, so `toString(1::Variant(Int64, String))` equals
`toString('1'::Variant(Int64, String))`. `renderingCollapsesTimePoints` is renamed to
`renderingCollapsesDistinctValues` accordingly.
…s-tostring-tofixedstring
`optimizeGroupBy` and the `uniq*` rewrite in `TreeOptimizer` passed `{}` to `isInjective`, which is
undecidable now that the answer depends on the arguments, so `GROUP BY toString(number)` stopped
being rewritten and `01244_optimize_distributed_group_by_sharding_key` lost the sharding-key
aggregation it asserts. Both call sites now resolve what the AST alone decides - an identifier that
names a source column, and a literal - through the new `tryGetASTFunctionArgumentColumns`, and claim
nothing when an argument stays unresolved.
Also cover the three cases from the review in
`05064_injective_claims_tostring_datetime_and_tofixedstring`: an explicit folding time zone and a
`NULL` time zone argument, an out-of-range `Date32` that saturates when formatted, and the
`toFixedString(FixedString(3), 2)` exception that must not be optimized away.
`EXPLAIN QUERY TREE` in the test now asks for the analyzer explicitly - it is `NOT_IMPLEMENTED` in
the old-analyzer job.
…s-tostring-tofixedstring
…lving compound identifiers
Three findings of the review on the previous head.
`FunctionToFixedString::isInjective` read the target width with `getUInt` before
knowing that the second argument is an unsigned integer. For a call site that
passes AST-derived arguments, `toFixedString(f, -1)` over a `FixedString(3)`
wrapped the literal around to a huge `UInt64`, the claim was made, and `GROUP BY`
/ `LIMIT BY` / `uniq*` eliminated a call that `getReturnTypeImpl` has to reject
with `Second argument for function toFixedString must be unsigned integer`. The
claim now requires an unsigned width type first.
`tryGetASTFunctionArgumentColumns` resolved an identifier by its short name. It
runs before `collectJoinedColumns`, so `source_columns` describes only the source
side of a `JOIN`, and a qualified right-hand key such as `r.dt` - or a subcolumn
such as `tuple.dt` - was resolved against an unrelated source column of the same
short name. With `l.dt UInt32` and `r.dt DateTime('Europe/Amsterdam')` the old
analyzer could still treat `toString(r.dt)` as injective and drop it, even though
that rendering folds across a UTC offset transition. A compound identifier is now
undecidable here and gives no claim at all.
`ReadInOrderOptimizer::getFixedPoint` was the last caller passing `{}` to
`isInjective`. It now resolves the argument columns from
`TreeRewriterResult::source_columns` the same way the `TreeOptimizer` rewrites
do, so a wrapper that stays injective for its actual arguments can be peeled
again when inferring a fixed sorting-key prefix.
The first two regressions are in
`05064_injective_claims_tostring_datetime_and_tofixedstring` and were verified to
be load-bearing: with the fixes reverted, the negative width is optimized away
instead of throwing, and the qualified `JOIN` key gives 2 groups instead of 1.
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…s-tostring-tofixedstring
Co-Authored-By: Claude Fable 5.1 <[email protected]>
`tryGetASTFunctionArgumentColumns` runs from `TreeOptimizer::apply` and from `ReadInOrderOptimizer`, both before `getArrayJoinedColumns` resolves the `ARRAY JOIN` result names. A plain identifier could therefore still denote an `ARRAY JOIN` result that shadows a source column of the same name (`ARRAY JOIN arr AS dt` over a table that has a `dt`), and was typed as the source column. The new `getArrayJoinResultNames` collects the alias or column name of every `ARRAY JOIN` expression of the query, and the resolver returns `nullopt` for such a name, so the injective rewrites and `getFixedPoint` claim nothing. Test: `enable_analyzer = 0` is refused since v26.9, so the old-analyzer cases of the test now run at the default settings, and the `ARRAY JOIN` shadowing shape is added there. Co-Authored-By: Claude Fable 5.1 <[email protected]>
…s-tostring-tofixedstring
…ty claims `toString` renders every `NaN` payload as the same `nan`, while hash tables compare floating-point keys bitwise, so `renderingCollapsesDistinctValues` now reports `Float32`, `Float64` and `BFloat16` as collapsing. `getFixedPoint` in `ReadInOrderOptimizer` no longer reports a bare identifier that names an `ARRAY JOIN` result as a fixed sorting key column. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
# Conflicts: # src/Functions/FunctionsConversion.h # src/Storages/ReadInOrderOptimizer.cpp # src/Storages/ReadInOrderOptimizer.h
…ding representations `SerializationBool::serializeText` renders `bool_true_representation` / `bool_false_representation`, which are unconstrained, so with equal values `toString(false)` and `toString(true)` produce the same string. Take the captured `format_settings` into account in `renderingCollapsesDistinctValues`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Master replaced `IDataType::forEachChild` with `getNumberOfChildren` / `getChild` and the walks in `DataTypes/TypeTree.h` (d0b1924), so the merged tree did not compile. `forEachInTypeTree` visits the root and every nested type in pre-order, which is what `renderingCollapsesDistinctValues` did by hand. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ing-tofixedstring Stop claiming injectivity for `toString` of a folding date-time and `toFixedString` of a `String`
Resolve conflicts of the cherry-pick of #117238 onto 26.9: - `actionsDAGUtils.h/.cpp`: keep the release-branch code (it has no `HeaderColumnsToInputs` / `findReachableNodes`) and add only `getFunctionArgumentColumns` from the PR. - `FunctionsConversion.h`: take the PR's `isInjective`; the release branch still has `IDataType::forEachChild` and no `DataTypes/TypeTree.h`, so `renderingCollapsesDistinctValues` walks the type tree with `forEachChild` as in the original version of the PR. Co-Authored-By: Claude Opus 5.5 <[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 #117238
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