Skip to content

Fix more non-deterministic function results found by the functions stress test - #104858

Merged
Algunenano merged 4 commits into
ClickHouse:masterfrom
Algunenano:fix-case-with-expression-const-determinism
May 15, 2026
Merged

Algunenano merged 4 commits into
ClickHouse:masterfrom
Algunenano:fix-case-with-expression-const-determinism

Conversation

@Algunenano

@Algunenano Algunenano commented May 13, 2026 •

Copy link
Copy Markdown
Member

Follow-up to #104710. Three more const/non-const divergences caught by the functions stress test (caseWithExpression and friends in src/Functions/tests/gtest_functions_stress.cpp). Each commit fixes one root cause and adds a stateless regression test.

  • transform const default: the optimized cache built default_column by routing the constant default through convertFieldToType, which loses the source type and falls back to a raw numeric path. That mis-converted Date/Date32 → DateTime/DateTime64 (days were treated as seconds, e.g. Date('2149-06-04') rendered as 1970-01-01 18:12:13.000000), emitted the underlying integer instead of the value name for Enum → String, and kept the trailing NUL padding for FixedString → String. Switched to castColumn, matching the surrounding code that already uses it for the from and to arrays. caseWithExpression rewrites to transform when all WHEN/THEN values are constant, so it inherited the fix.
  • String vs constant FixedString comparison: string_vector_constant in StringComparisonImpl/StringEqualsImpl used a plain memcmp, while the vector-vs-vector path used memcmpSmallLikeZeroPaddedAllowOverflow15 (zero-padded, per SQL semantics toFixedString('abc', 5) = 'abc'). When the right operand was a constant FixedString the two paths disagreed: equals(materialize(''::String), '\0\0\0\0'::FixedString(4)) returned 0 while every other arrangement returned 1, with the same flip for less, lessOrEquals, isDistinctFrom, isNotDistinctFrom. Track whether the constant operand wraps a FixedString in executeString and dispatch to a new stringVectorConstantFixedString (and its symmetric form) that uses the zero-padded comparison.
  • if const-condition FixedString padding: the non-const dispatch in executeString copies FixedString operands into a String result through FixedStringSource → StringSink, preserving the full N bytes including trailing NULs. The const-condition fast path used castColumn, which trims them. ifNull rewrites to if and inherited the divergence. Routed the const-condition cast through a helper that copies bytes verbatim, matching the non-const path. multiIf is intentionally left alone: it already trims consistently on both paths, so the stress test cannot flip its output.

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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 (and caseWithExpression through it) with a constant Date/Date32/Enum/FixedString default, comparison operators between a String and a constant FixedString, and if/ifNull/nullIf with a FixedString branch under a constant condition.

Version info

  • Merged into: 26.5.1.685

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.
@clickhouse-gh

clickhouse-gh Bot commented May 13, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [4831b15]

Summary: ❌

job_name test_name status info comment
Stress test (arm_ubsan) FAIL
Hung check failed, possible deadlock found FAIL cidb IGNORED

AI Review

Summary

This PR fixes three real const-vs-non-const determinism bugs in transform, if/ifNull/nullIf, and String vs constant FixedString comparisons, and adds focused stateless regression coverage for each path. After reviewing the current diff and code (including prior inline discussion points), I do not see unresolved contract violations, safety issues, or missing critical evidence for the claimed fixes.

Final Verdict
  • Status: ✅ Approve

Comment thread src/Functions/if.cpp Resolved
Comment thread src/Functions/if.cpp Resolved
Comment thread src/Functions/FunctionsComparison.h Resolved
Comment thread src/Functions/FunctionsComparison.h Resolved
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.
@clickhouse-gh

clickhouse-gh Bot commented May 15, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 90.90% 90.90% +0.00%
Branches 76.60% 76.60% +0.00%

Changed lines: 90.40% (113/125) | lost baseline coverage: 2 line(s) · Uncovered code

Full report · Diff report

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants