Skip to content

Cherry pick #117238 to 26.8: Stop claiming injectivity for toString of a folding date-time and toFixedString of a String - #125418

Merged
robot-ch-test-poll merged 22 commits into
backport/26.8/117238from
cherrypick/26.8/117238
Oct 13, 2026
Merged

robot-ch-test-poll merged 22 commits into
backport/26.8/117238from
cherrypick/26.8/117238

Conversation

@robot-ch-test-poll4

Copy link
Copy Markdown
Contributor

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-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

alexey-milovidov and others added 21 commits August 31, 2026 03:11
…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.
`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.
…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]>
`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]>
…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`
@robot-ch-test-poll4 robot-ch-test-poll4 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 Oct 12, 2026
Resolve the conflicts of the 26.8 backport of #117238:

- `actionsDAGUtils.h` / `actionsDAGUtils.cpp`: keep only the new
  `getFunctionArgumentColumns`; the lineage tracing, `mapHeaderColumnsToInputs`
  and `findReachableNodes` around it are master-only and not part of this change.
- `FunctionsConversion.h`: take the new `isInjective` of `FunctionConvert`.
  26.8 still has `IDataType::forEachChild` and no `DataTypes/TypeTree.h`, so
  `renderingCollapsesDistinctValues` walks the type with `check(*type)` and
  `forEachChild` as in the original version of the PR, instead of
  `forEachInTypeTree`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robot-ch-test-poll
robot-ch-test-poll merged commit 29ac926 into backport/26.8/117238 Oct 13, 2026
1 check passed
@robot-ch-test-poll
robot-ch-test-poll deleted the cherrypick/26.8/117238 branch October 13, 2026 02:06
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.

4 participants