Repository navigation
Conversation
`transform`'s `initializeTransformCache` built `cache->default_column` via
the `Field`-level path: `convertFieldToType((*default_col)[0], *result_type)`
followed by `default_column->insert(f)`. The `Field` abstraction collapses
some types: a `FixedString` value becomes a `String` `Field` keeping its raw
bytes (no trailing-zero trimming), and an `Enum` value becomes its underlying
integer `Field`. So when the cached default went through the `String`
representation, `FixedString` defaults kept their trailing `\0` bytes and
`Enum` defaults turned into their numeric storage value -- both disagree
with `castColumn(default, result_type)` (which is what
`cache->to_column` already used for the matched-`dst` branch).
The matched branch and the default branch must agree, and both must agree
with `cast(default AS result_type)`. Use `castColumn` for `cache->default_column`
the same way `cache->to_column` does. The `onlyNull()` path is preserved to
keep prior semantics for `Nothing`/only-`NULL` defaults (insert the default
value of `result_type`).
This also fixes the `caseWithExpression` non-determinism reported by
`FunctionsStress.stress` ("broken_determinism: different result on the same
input ... while re-executing after making arguments {...} const"):
`caseWithExpression` picks a fast `transform` path when all `WHEN`/`THEN`
values are constant and falls back to `multiIf` otherwise, so the same call
flips between paths when the stress test randomizes `const`-ness of
arguments, exposing the inconsistency. The fix is in `transform` itself,
which fixes both the direct `transform(...)` call and the `caseWithExpression`
fast path.
Original reproducers (now consistent):
SELECT transform(toUInt32(0), [toUInt32(1)], ['a'], toFixedString('b', 3));
-- now 'b' (was 'b\0\0', should agree with cast(toFixedString('b', 3) AS String))
SELECT transform(toUInt32(0), [toUInt32(1)], ['a'], cast('cV1' AS Enum8('cV1' = 42)));
-- now 'cV1' (was '42', should agree with cast(... AS String))
SELECT caseWithExpression(materialize(toUInt32(2)), toUInt64(1), 'match', toFixedString('a', 3));
-- now 'a' from both transform and multiIf paths
CI report (`FunctionsStress.stress`):
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=104299&sha=af067fba3bc6ebb1b269dff7f023bb2697aa1f94&name_0=PR&name_1=Unit%20tests%20%28tsan%2C%20function_prop_fuzzer%29
Pointed at by ClickHouse#104299 .
Co-Authored-By: Claude Opus 4.7 <[email protected]>
|
Pre-PR validation gate (session a) Deterministic repro? Yes -- at SQL level:
b) Root cause explained? Yes. c) Fix matches root cause? Yes -- replaces d) Test intent preserved? / New tests added? Existing 13 transform / case-related stateless tests still pass against the fix:
e) Both directions demonstrated? Yes -- the new regression test returns f) Fix is general, not a narrow patch? Yes. The fix is in
Asking "Why did this bad state arrive here?": the bad bytes came from the |
|
cc @rschu1ze @alexey-milovidov — could you review this? Fixes a real conversion bug in |
| default_column->insertDefault(); | ||
| cache->default_column = std::move(default_column); | ||
| { | ||
| /// Use `castColumn` (not `convertFieldToType` + `insert`) so the conversion is |
There was a problem hiding this comment.
No need for this big comment here. After the change the comment about Field usage sounds strange as we don't use it anymore. Let's simplify it as much as possible.
There was a problem hiding this comment.
Done — pushed in 70a6bb6. Stripped the big comment along with the simplification below (the comment described the old Field path which is gone).
| auto casted = castColumn(arguments[3], result_type)->cut(0, 1); | ||
| if (isColumnConst(*casted)) | ||
| casted = casted->convertToFullColumnIfConst(); | ||
| cache->default_column = std::move(casted); |
There was a problem hiding this comment.
I think we can simplify it to:
| auto casted = castColumn(arguments[3], result_type)->cut(0, 1); | |
| if (isColumnConst(*casted)) | |
| casted = casted->convertToFullColumnIfConst(); | |
| cache->default_column = std::move(casted); | |
| cache->default_column = castColumn(arguments[3], result_type)->cloneResized(1)->convertToFullColumnIfConst(); |
Or there is a problem with this approach?
There was a problem hiding this comment.
Applied in 70a6bb6 — cloneResized(1)->convertToFullColumnIfConst() works fine. cloneResized(1) on a ColumnConst(N) returns ColumnConst(1), and the subsequent convertToFullColumnIfConst unwraps it; equivalent to the previous cut(0, 1) + explicit isColumnConst check. Re-ran the regression test 50/50 with randomization and the sibling transform tests (00153_transform, 00165_transform_non_const_default) 20/20 each.
|
Workflow [PR], commit [70a6bb6] Summary: ❌
AI ReviewSummaryThis PR fixes Final Verdict
|
Per @Avogar's review on ClickHouse#104804 : - Strip the now-stale comment about `Field` (the previous `convertFieldToType` path is gone, so the explanation no longer matches the code). - Collapse the `cut(0, 1)` + `isColumnConst` check + `convertToFullColumnIfConst` sequence into a one-liner using `cloneResized(1)->convertToFullColumnIfConst()`, matching the suggestion in the inline comment. Behavior is unchanged. `cloneResized(1)` on a `ColumnConst(N)` returns `ColumnConst(1)`; the subsequent `convertToFullColumnIfConst` unwraps it to a full one-row column — identical to the prior code, just shorter. Verified locally: - Regression test `04237_fix_caseWithExpression_transform_const_determinism` passes 50/50 with full randomization. - Sibling transform tests (`00153_transform`, `00165_transform_non_const_default`) pass 20/20 each. - Original `transform` bug repros (`FixedString` default, `Enum` default) still produce the corrected output. Co-Authored-By: Claude Opus 4.7 <[email protected]>
LLVM Coverage Report
Changed lines: 100.00% (7/7) · Uncovered code |
|
Closing — superseded by @Algunenano's PR #104858 ("Fix more non-deterministic function results found by the functions stress test"), merged into master on 2026-05-15 16:06 UTC. PR #104858 applies the same fix ( Same supersession pattern as our PR #104769 → @vitlibar's PR #104812 a few days ago. Notes for the record:
Thanks @Avogar for the simplification feedback. |
transform'sinitializeTransformCachebuiltcache->default_columnvia theField-level path:convertFieldToType((*default_col)[0], *result_type)followed bydefault_column->insert(f). TheFieldabstraction collapses some types: aFixedStringvalue becomes aStringFieldkeeping its raw bytes (no trailing-zero trimming), and anEnumvalue becomes its underlying integerField. So when the cached default went through theStringrepresentation,FixedStringdefaults kept their trailing\0bytes andEnumdefaults turned into their numeric storage value -- both disagree withcastColumn(default, result_type)(which is whatcache->to_columnalready used for the matched-dstbranch).The matched branch and the default branch must agree, and both must agree with
cast(default AS result_type). The fix usescastColumnforcache->default_columnthe same waycache->to_columndoes. TheonlyNull()path is preserved to keep prior semantics forNothing/only-NULLdefaults (insert the default value ofresult_type).This also fixes the
caseWithExpressionnon-determinism reported byFunctionsStress.stress("broken_determinism: different result on the same input ... while re-executing after making arguments {...} const"):caseWithExpressionpicks a fasttransformpath when allWHEN/THENvalues are constant and falls back tomultiIfotherwise, so the same call flips between paths when the stress test randomizesconst-ness of arguments, exposing the inconsistency. The fix is intransformitself, which fixes both the directtransform(...)SQL function and thecaseWithExpressionfast path.Original reproducers (now consistent):
Linked from #104299 (pointed at by
@alexey-milovidov).CI report (failing
FunctionsStress.stress): https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=104299&sha=af067fba3bc6ebb1b269dff7f023bb2697aa1f94&name_0=PR&name_1=Unit%20tests%20%28tsan%2C%20function_prop_fuzzer%29Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
transformreturning different bytes for its default branch than for matched branches and thanCAST. When the default value was aFixedStringand the result type wasString, trailing\0bytes were preserved instead of trimmed; when the default value was anEnumand the result type wasString, the underlying numeric storage was returned instead of the value's name. Also fixes a relatedcaseWithExpressionnon-determinism where the result depended on whetherWHEN/THENarguments wereconst, because the function picks a fasttransformpath when all areconstand falls back tomultiIfotherwise.Documentation entry for user-facing changes