Repository navigation
Fix more non-deterministic function results found by the functions stress test - #104858
Merged
Algunenano merged 4 commits intoMay 15, 2026
Merged
Algunenano merged 4 commits into
Algunenano merged 4 commits into
Conversation
When the default argument of `transform` is a constant, the cached
default column was built by routing the value through
`convertFieldToType`. That call drops the source type, so it falls back
to a raw numeric conversion that is wrong in several cases:
- `Date` / `Date32` -> `DateTime` / `DateTime64` treats the stored days
as if they were seconds (or whatever the target scale demands), so
e.g. `Date('2149-06-04')` rendered as `1970-01-01 18:12:13.000000`.
- `Enum` -> `String` emits the underlying integer instead of the value
name (`-24408` rather than `cV5`).
- `FixedString` -> `String` keeps the trailing NUL padding instead of
trimming it.
Switch to `castColumn`, which is what the surrounding code already uses
for the `from_column` and `to_column` arrays, so the proper source-aware
cast pipeline runs. `caseWithExpression` inherits the fix because it
rewrites to `transform` when all WHEN/THEN values are constant; the
stress test (`gtest_functions_stress` for `caseWithExpression`) surfaced
the bug as `broken_determinism`: the same row produced different results
depending on whether the ELSE branch was held as a constant or as a
materialized column.
Comparing a vector `String` with a constant `FixedString` followed a plain
`memcmp` path through `string_vector_constant`, while the vector-vs-vector
path uses `memcmpSmallLikeZeroPaddedAllowOverflow15` so that the trailing
NUL padding of a `FixedString` does not count as content (per the SQL
semantics that `toFixedString('abc', 5) = 'abc'`).
The result disagreed across const/materialize arrangements. For example,
`equals(materialize(''::String), '\0\0\0\0'::FixedString(4))` returned
`0`, while every other arrangement of the same comparison returned `1`.
The same flip showed up in `less`, `lessOrEquals`, `isDistinctFrom`, and
`isNotDistinctFrom`.
Track whether the constant operand wraps a `FixedString` in `executeString`
and dispatch to a new `stringVectorConstantFixedString` (and its symmetric
`constantFixedStringStringVector`) that uses the zero-padded comparison.
The `FixedString` vector vs constant paths were already correct.
The `if` function's non-const condition path copies `FixedString` operands into a `String` result through `FixedStringSource` -> `StringSink`, which preserves the source's full `N` bytes including trailing NUL padding. The const-condition fast path went through `castColumn`, which trims that padding. The two paths produced different lengths for the same input, and `ifNull` (which rewrites to `if`) inherited the divergence. For the const-condition path, switch the cast to a helper that copies `FixedString` bytes verbatim into a `ColumnString` so both paths agree. Other conversions still go through `castColumn` unchanged. This is a behavior change for users who relied on the trimmed const-condition output of `if`/`ifNull`/`nullIf` involving a `FixedString`. Two existing tests (`00197_if_fixed_string`, `01355_if_fixed_string`) already pinned the non-const "preserve padding" behavior and continue to pass; the new `04236_*` test pins it for the const-condition path too. `multiIf` was intentionally left alone: it already trims consistently across const/non-const conditions, so the stress test cannot flip its result by toggling constness. Aligning it with `if` would be a separate behavior change for users who rely on the trimmed output today.
Contributor
|
Workflow [PR], commit [4831b15] Summary: ❌
AI ReviewSummaryThis PR fixes three real const-vs-non-const determinism bugs in Final Verdict
|
This was referenced May 13, 2026
1 task done
1 task done
Merged
antonio2368
reviewed
May 15, 2026
Two more `if` const/dynamic divergences with the same root cause as the previous commit, found by @antonio2368: - `executeForNullThenElse` is the dispatch path when `then` or `else` is the constant `NULL`. Its materialized-condition branches used `castColumn` to align the surviving side with `result_type`, which trimmed a `FixedString` operand's trailing padding while the rest of `if` preserved it. Replaced with `castForIf`. - `getConstantResultForNonConstArguments` was folding `if(const_cond, const_branch, materialize(...))` at DAG-build time through `castColumn`, producing a trimmed constant. The runtime path (`executeString`) preserved the padding, so the same expression returned different lengths depending on whether the optimizer reached the constant-fold. Replaced with `castForIf`. Also clean up `executeString`'s const dispatch in `FunctionsComparison.h` per review: drop the `c0_const_is_fixed_string` / `c1_const_is_fixed_string` booleans and keep the `ColumnString *` / `ColumnFixedString *` pointers alive in the outer scope, so the dispatch can branch on them directly. Restore the snake_case + `NOLINT` naming for the new methods to match the rest of the file.
antonio2368
approved these changes
May 15, 2026
Contributor
LLVM Coverage Report
Changed lines: 90.40% (113/125) | lost baseline coverage: 2 line(s) · Uncovered code |
Algunenano
enabled auto-merge
May 15, 2026 16:06
Merged
via the queue into
ClickHouse:master
with commit May 15, 2026
1850ea5
165 of 167 checks passed
This was referenced May 15, 2026
This was referenced May 17, 2026
This was referenced May 19, 2026
Closed
1 task
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.
Follow-up to #104710. Three more const/non-const divergences caught by the functions stress test (
caseWithExpressionand friends insrc/Functions/tests/gtest_functions_stress.cpp). Each commit fixes one root cause and adds a stateless regression test.transformconst default: the optimized cache builtdefault_columnby routing the constant default throughconvertFieldToType, which loses the source type and falls back to a raw numeric path. That mis-convertedDate/Date32→DateTime/DateTime64(days were treated as seconds, e.g.Date('2149-06-04')rendered as1970-01-01 18:12:13.000000), emitted the underlying integer instead of the value name forEnum→String, and kept the trailing NUL padding forFixedString→String. Switched tocastColumn, matching the surrounding code that already uses it for thefromandtoarrays.caseWithExpressionrewrites totransformwhen all WHEN/THEN values are constant, so it inherited the fix.Stringvs constantFixedStringcomparison:string_vector_constantinStringComparisonImpl/StringEqualsImplused a plainmemcmp, while the vector-vs-vector path usedmemcmpSmallLikeZeroPaddedAllowOverflow15(zero-padded, per SQL semanticstoFixedString('abc', 5) = 'abc'). When the right operand was a constantFixedStringthe two paths disagreed:equals(materialize(''::String), '\0\0\0\0'::FixedString(4))returned0while every other arrangement returned1, with the same flip forless,lessOrEquals,isDistinctFrom,isNotDistinctFrom. Track whether the constant operand wraps aFixedStringinexecuteStringand dispatch to a newstringVectorConstantFixedString(and its symmetric form) that uses the zero-padded comparison.ifconst-conditionFixedStringpadding: the non-const dispatch inexecuteStringcopiesFixedStringoperands into aStringresult throughFixedStringSource→StringSink, preserving the fullNbytes including trailing NULs. The const-condition fast path usedcastColumn, which trims them.ifNullrewrites toifand inherited the divergence. Routed the const-condition cast through a helper that copies bytes verbatim, matching the non-const path.multiIfis intentionally left alone: it already trims consistently on both paths, so the stress test cannot flip its output.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix three more functions returning different results for the same input depending on whether arguments arrived as columns or constants:
transform(andcaseWithExpressionthrough it) with a constantDate/Date32/Enum/FixedStringdefault, comparison operators between aStringand a constantFixedString, andif/ifNull/nullIfwith aFixedStringbranch under a constant condition.Version info
26.5.1.685