Repository navigation
Conversation
…hing
`GROUP BY f(<column>, NULL)` returned one group per distinct argument value
instead of a single group, silently. `LIMIT BY` dropped rows the same way, and
`optimize_truncate_order_by_after_group_by_keys` removed a still-needed
`ORDER BY` tiebreaker.
The overload resolver replaces the result type with one that admits at most a
single value, before `getReturnTypeImpl` ever runs: `Nullable(Nothing)` when
`useDefaultImplementationForNulls` meets a NULL constant argument
(`src/Functions/IFunction.cpp:901-908`), and bare `Nothing` for a `Nothing`
argument (`:892-899`). The implementation's `isInjective` still describes the
un-collapsed function and answers `true`, so consumers treat `f(x)` and `x` as
inducing the same partition and rewrite `f(x)` to `x`. A function mapping its
whole domain onto one value is injective only over a single-valued domain, so
that rewrite changes the answer. This is why the reported query lost its
grouping while `SELECT DISTINCT` over the same expression stayed correct: the
function is gone from the key before the aggregator runs.
`FunctionToFunctionBaseAdaptor::isInjective` now declines when the built result
type admits at most one value. The predicate is complete over both collapse
paths, whose entire output set is `{Nothing, Nullable(Nothing)}`;
`LowCardinality(Nothing)` cannot be constructed, because
`IDataType::canBeInsideLowCardinality` defaults to false and `DataTypeNothing`
does not override it. Declining is monotonically safe: `isInjective() == true`
is a permission to rewrite, never an obligation, so every consumer becomes more
conservative and no optimization worth keeping is lost, the expression being a
constant NULL.
The definition moved out of line into `IFunctionAdaptors.cpp` so that
`IDataType.h` is not pulled into `IFunctionAdaptors.h`, which is included
across `src/Functions/` and today sees only `IDataType_fwd.h`.
The class cannot be closed function by function: `concatAssumeInjective` is an
explicit user opt-in that must be honoured, and `toFixedString` of a
`FixedString` is genuinely injective, yet both still collapse to
`Nullable(Nothing)` for a NULL argument.
`FunctionToOverloadResolverAdaptor` is deliberately unchanged. A resolver has
no result type, so the old analyzer's AST-level rewrite cannot express this
guard, and its arm is out of scope.
`gtest_functions_stress.cpp` cross-checks that the resolver and the built
function agree about injectivity. That check now legitimately diverges for a
collapsed result, so the `Nothing` case is whitelisted beside the existing
Dynamic/Variant/Object one, narrowed to the collapsed result type so a genuine
mismatch is still reported.
Related: ClickHouse#117767
The guard declines injectivity for both types the overload resolver can substitute, but every assertion in the test used a literal `NULL`, which only reaches the `Nullable(Nothing)` path (`src/Functions/IFunction.cpp:901-908`). Two assertions now cover the other path, bare `Nothing` from an argument that is already typed `Nothing` (`:892-899`), reached with `assumeNotNull(materialize(NULL))`. That branch is asserted on the query tree rather than on a result because a non-empty column of type `Nothing` cannot be created (`:428`), so every query that evaluates such an expression throws `ILLEGAL_COLUMN` on both a pristine and a guarded build. The rewrite decision is the only observable difference: a pristine build unwraps the key, a guarded build retains it, and with `optimize_injective_functions_in_group_by = 0` both retain it, which is the oracle arm beside it. Related: ClickHouse#117767
The test asserts that a function whose result type collapses to Nothing or Nullable(Nothing) is no longer treated as injective, so it needs a carrier that keeps claiming injectivity. toFixedString is a poor choice for that: several open PRs are removing false isInjective claims function by function, and ClickHouse#117238 rewrites FunctionToFixedString::isInjective to consult its width argument. For toFixedString(fs, assumeNotNull(materialize(NULL))) that argument column is nullptr, so the function would start declining on its own and the two bare-Nothing arms would pass whether or not the guard exists. concatAssumeInjective cannot be silenced that way. Its claim is an explicit user opt-in rather than an assertion about the function's mathematics: ConcatImpl returns the flag it was constructed with and never inspects its arguments. It also overrides neither useDefaultImplementationForNulls nor useDefaultImplementationForNothing, so both collapse paths are reachable through this one function and the whole test becomes a single-function A/B - a NULL argument against a non-NULL one. Both narrowness controls move to concatAssumeInjective(fs, 'x') for the same reason, which also removes the last dependency on a function whose claim is being corrected elsewhere. The reference was regenerated by running the test. Discrimination is unchanged: against a build without the guard, exactly four assertions flip (the keyed GROUP BY, the bare-Nothing query tree, the LIMIT BY and the ORDER BY tiebreaker) while every pass-disabled oracle arm and both narrowness controls hold. Related: ClickHouse#117767
Internal second-model review: 3 rounds, 0 open findingsEvery round was reviewed cold by me and then adversarially by an independent second model. Closed in the earlier rounds:
Against a build without the guard, exactly four assertions of the new test flip, while every |
|
Workflow [PR], commit [372e985] Summary: ✅
AI ReviewSummaryThe PR prevents incorrect key rewrites when a function's result collapses to Final VerdictStatus: ✅ Approve |
|
cc @rschu1ze @vdimir, could you review this? The overload resolver collapses a function call's result type to |
The guard in the previous commit declined injectivity for every function whose result type is `Nothing` or `Nullable(Nothing)`. One case too many: when every argument type also holds at most one value the argument domain is a single tuple, so any function over it is injective and declining there only loses a valid rewrite. Master's 04805_group_by_injective_function_of_constant pins that rewrite. In `GROUP BY materialize(NULL), k` the constant-derived key is dropped because `materialize` is injective over its single-valued argument, leaving `GROUP BY k`. The blanket guard kept the redundant key, so the test's query-tree assertion went from 0 to 1 and `Fast test` reported a mismatch with the reference. Decline only when an argument type can hold more than one value, which is what makes the claim false. An empty sample carries no argument types and still declines, so the call sites that pass none behave as before. Three builds, both directions: pristine master fails 05090 and passes 04805, the blanket guard passes 05090 and fails 04805, this commit passes both. The 18 tests of the injective and truncate_order_by family pass, and both tests pass 50 runs at -j 8. Co-Authored-By: Claude Opus 5 <[email protected]>
The skip was added alongside a guard that declined injectivity for every collapsed result type, so its note read "mapping a whole domain onto one value is not injective". That is false where the domain holds a single tuple, which the guard now allows. Give the reason that survives: the result type is known only after `build`, so the resolver can still claim injectivity the built function declines. The condition is unchanged; it stays deliberately broad, since a skip only ever suppresses a mismatch report. Co-Authored-By: Claude Opus 5 <[email protected]>
NothingNothing
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes98 object files changed (-2.42 MiB total), 0 added.
719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units264 translation units recompiled, 2628 s compile time in total, 264 of them have a recent master baseline. |
CI finish ledger - a7a97e0Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20260904-020000 |
|
Fixing PR for the It adds the missing arm to |
…-collapsed-nothing-result # Conflicts: # src/Functions/IFunctionAdaptors.h
Backport #117894 to 26.10: Do not claim injectivity for a function that collapses varying arguments to `Nothing`
Closes: #117767
Related: #117197
Related: #117210
Related: #117238
Related: #117239
Related: #117357
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed wrong results when a
GROUP BYorLIMIT BYkey, or anORDER BYexpression, was a function call whose result type collapses toNullable(Nothing)because an argument is aNULLconstant, such asconcatAssumeInjective(s, NULL). Such a call was still treated as injective and replaced by its argument in the key, soGROUP BYandLIMIT BYproduced one group or row per distinct argument value, andoptimize_truncate_order_by_after_group_by_keysdropped a still-neededORDER BYtiebreaker.Description
Reported for
UUIDStringToNum(<column>, NULL), whose claim #117210 has since dropped. The class still reproduces on master and on 26.8.28.14:GROUP BY f(<column>, NULL)returns one group per distinct argument value instead of a single group.The overload resolver replaces the result type before
getReturnTypeImplruns:Nullable(Nothing)whenuseDefaultImplementationForNullsmeets aNULLconstant argument, bareNothingfor aNothingargument. The built function'sisInjectivestill describes the un-collapsed function and answerstrue, so consumers rewritef(x)tox. A function mapping its whole domain onto one value is injective only over a single-valued domain.The fix answers
falsewhen the result type admits at most one value and some argument type is notNothingorNullable(Nothing). With every argument of those types the domain is one tuple wide, the claim holds, and the rewrite master's04805_group_by_injective_function_of_constantpins is kept. Declining is monotonically safe: the claim is a permission to rewrite, never an obligation.The per-function fixes above do not close the class: they remove claims that are false in general, while
concatAssumeInjectiveis a documented user opt-in that must be respected and still collapses for aNULLargument.Workflow [PR]
Sync PR [sync-upstream/pr/117894]
Version info
26.11.1.2784-master(included in26.11and later)26.10.7.21