Skip to content

Do not stash scale-divergent columns in the function property stress test - #110070

Merged
alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-functionsstress-scale-divergent-stash
Jul 12, 2026
Merged

alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-functionsstress-scale-divergent-stash

Conversation

@groeneai

@groeneai groeneai commented Jul 11, 2026 •

Copy link
Copy Markdown
Collaborator

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

...

Description

Fixes the chronic Function writeSlice expects same column types for GenericValueSlice and GenericArraySink LOGICAL_ERROR that the function property stress test (FunctionsStress.stress in gtest_functions_stress.cpp) keeps surfacing (issue #108517; seen most recently on #96130's Unit tests (msan, function_prop_fuzzer) at report https://s3.amazonaws.com/clickhouse-test-reports/PRs/96130/840dde3c3a33b9412f7837fde92cd5c815aef79a/unit_tests_msan_function_prop_fuzzer/unit_tests_msan_function_prop_fuzzer.log , offending query SELECT arrayPushBack(a, CAST('1900-01-01 00:00:00.0' AS DateTime64(1))) FROM system.one ARRAY JOIN CAST([...] AS Array(Array(LowCardinality(Nullable(Date)))))).

The error is correct and unreachable from real SQL (the analyzer unifies operands to the least supertype before execution, verified: the exact query above returns results with no error). It is a stress-test artifact. The test validates every function result with columnMatchesType before stashing it in additional_random_values for reuse as another function's argument. That check compared only getDataType(), which for Decimal/DateTime64/Time64 reports the physical TypeIndex (Decimal64 for every scale). A function whose result column carried a scale different from its declared type passed, got stashed, and was reused as an argument to an innocent consumer such as arrayPushBack, where the declared-type cast is skipped and the scale-divergent physical column flows straight into GatherUtils, tripping writeSlice.

This re-lands the scale-aware detection removed in #108981 (revert of #108551), but not as a hard throw inside the test:

  • columnMatchesType gains an opt-in strict_decimal_scale argument (default off, so the engine callers in resolveFunction, ColumnFunction, ExpressionActions, ActionsDAG keep the scale-blind behaviour they rely on).
  • The stress test uses it to detect a scale-divergent result, attributes it to the producing function via a new soft, default-ignored problem (scale_divergent_result), and never stashes it.

Skipping the stash is the complete cut of the propagation path: generated random arguments are always scale-consistent (built via type->createColumn()), so the stash was the only way a divergent column could become an argument. Because the strict check is only a soft, ignored report (not a throw), a newly appearing scale-divergent producer is recorded but cannot redden the whole run, which is exactly the failure mode that caused #108551 to be reverted. The known producers found while #108551 was live (changeDate/timeSlots in #108994, reinterpret in #108878) are already fixed on master; this change makes the harness robust to any remaining or future ones.

Regression coverage: gtest_validate_column_type asserts that a scale-7 column labelled DateTime64(3) is accepted by the default check and rejected by the strict check, both at top level and nested inside Array(DateTime64(3)) (the exact #108517 shape).

Version info

  • Merged into: 26.7.1.816 (included in 26.7 and later)

…test

The FunctionsStress property test (gtest_functions_stress.cpp) validates every
function result with columnMatchesType before stashing it in
additional_random_values for reuse as a later function's argument. That check
compared only getDataType(), which for Decimal/DateTime64/Time64 reports the
physical TypeIndex (Decimal64 for every scale). A function whose result column
carried a scale different from its declared type therefore passed, got stashed,
and was reused as an argument to an innocent consumer such as arrayPushBack,
surfacing as a misattributed 'writeSlice expects same column types' LOGICAL_ERROR
(issue ClickHouse#108517).

Re-add the scale-aware detection to columnMatchesType behind an opt-in
strict_decimal_scale argument (default off, so the engine callers in
resolveFunction, ColumnFunction, ExpressionActions, ActionsDAG are unchanged),
and use it in the stress test to detect a scale-divergent result. Such a result
is attributed to the producing function via a new soft, default-ignored problem
(scale_divergent_result) and is never stashed. Skipping the stash is the complete
cut of the propagation path: generated random arguments are always
scale-consistent (built via type->createColumn()), so the stash was the only way
a divergent column could enter as an argument.

Unlike the reverted ClickHouse#108551, the strict check is not a hard throw inside the
test, so it cannot redden the whole run when a new scale-divergent producer
appears; it just reports and skips it.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes for the primitive: columnMatchesType(scale-7 column, DateTime64(3), strict=true) returns false deterministically (unit test). The stress-test crash itself is inherently rare (needs a randomized scale-divergent producer, ~2 hits/600s across 32 threads in CI), which is exactly why a hard gate is wrong; the fix cuts the propagation path unconditionally.
b Root cause explained? A function returns a Decimal/DateTime64/Time64 column whose physical scale differs from its declared type. columnMatchesType compared only getDataType() (scale-blind: Decimal64 for every scale), so it passed, the column was stashed in additional_random_values, and reused as an argument to arrayPushBack, where the declared-type cast is skipped so the divergent physical column reaches GatherUtils and trips writeSlice. Verified the exact failing SQL SUCCEEDS on a clean server (analyzer unifies to least supertype), so the error is unreachable from real SQL and is a harness artifact.
c Fix matches root cause? Yes. Detect the scale-divergent result at the stash gate (opt-in strict_decimal_scale) and never stash it. Generated random args are always scale-consistent (type->createColumn()), so the stash was the only propagation path.
d Test intent preserved / new tests added? Yes. The harness still catches real bugs; the new scale_divergent_result problem attributes the divergent result to the producing function (soft, default-ignored, so it does not redden). New gtest_validate_column_type asserts detection at top level and nested inside Array(DateTime64(3)) (the #108517 shape).
e Both directions demonstrated? Yes for the gate: matching column accepted, scale-7-as-3 column accepted by default check and rejected by strict check (unit test, both top-level and nested). Stress test still PASSES with the change (no abort).
f Fix is general across code paths? Yes. The cut is at the single shared stash site (checkFunctionExecutionResults), not one producer. Individual producers (changeDate/timeSlots #108994, reinterpret #108878) were already fixed; this makes the harness robust to any remaining/future one instead of chasing them one at a time.
g Fix generalizes across inputs? Yes. decimalScaleMatches covers Decimal32/64/128/256, DateTime64 and Time64, and the recursion applies it through Array/Nullable/Tuple/Map/LowCardinality/Variant wrappers.
h Backward compatible? Yes. strict_decimal_scale defaults to false, so the engine callers (resolveFunction, ColumnFunction, ExpressionActions, ActionsDAG) are byte-for-byte unchanged. Test-only behavior change otherwise.
i Invariants and contracts preserved? Yes. columnMatchesType's existing contract is unchanged at the default; the strict path only adds a stricter check. No engine data-structure or concurrency contract is touched.

Session id: cron:clickhouse-worker-slot-0:20260710-230500

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @alexey-milovidov @rschu1ze could you review this? It is the proper fix for the chronic writeSlice LOGICAL_ERROR in FunctionsStress (issue #108517), after the revert of #108551. Instead of a hard scale-strict throw (which reddened the whole test on every unfixed producer), the test now detects a scale-divergent function result, reports it as a soft default-ignored problem, and skips stashing it in additional_random_values, cutting the only path by which such a column becomes another function's argument. columnMatchesType stays scale-blind by default so the engine callers are unchanged.

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Jul 11, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 11, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [a921779]

Summary: ✅


AI Review

Summary

This PR hardens the FunctionsStress harness against scale-divergent Decimal/DateTime64/Time64 result columns by threading an opt-in strict scale check through columnMatchesType, flagging divergent results in checkAndFixupColumn, and preventing them from being stashed into additional_random_values. The earlier single-row fallback hole is closed in the current head because each row_result is checked before insertRangeFrom, so I do not see a remaining correctness issue in the reviewed patch.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Jul 11, 2026
…s test

Move the strict scale-divergence detection from the final merged result into
checkAndFixupColumn on the stress-test path, so it runs on the bulk result and
on every per-row result before insertRangeFrom merges it. A per-row divergence
(row0 DateTime64(3), row1 DateTime64(7)) is a raw memcpy in ColumnDecimal, so
the merged column can end up matching the declared scale and previously slipped
the final-column-only check and got stashed. Now caught before the merge.

Still a soft, default-ignored problem (scale_divergent_result); no throw.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate (follow-up: per-row scale detection)

Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? N/A - harness-internal robustness follow-up. The original divergence was reproduced/analyzed on #108517; this change hardens the single-row-fallback path the reviewer identified (row0 DateTime64(3), row1 DateTime64(7) merged via insertRangeFrom memcpy).
b Root cause explained? Yes. Strict scale check ran only on the final merged result; per-row row_result is checked scale-blind (:1835) then merged with ColumnDecimal::insertRangeFrom (raw memcpy, no rescale, ColumnDecimal.cpp:455), so a per-row divergence hides inside a merged column matching the declared scale and could be stashed.
c Fix matches root cause? Yes. Detection moved into checkAndFixupColumn (stress-test path, stats != nullptr), which runs on the bulk result and every per-row result BEFORE the merge; a member flag drives the stash-skip.
d Test intent preserved? Yes. Still a soft default-ignored problem (scale_divergent_result), no throw. Detection is now strictly broader (per-row) than before; no assertion weakened.
e Demonstrated both directions? ValidateColumnType gtest 2/2 PASS; FunctionsStress.stress runs clean to completion (89983 ms, no abort/writeSlice/LOGICAL_ERROR). The strict_decimal_scale=true primitive and nested-Array detection are covered by the ValidateColumnType gtest.
f Fix general, not narrow patch? Yes. Check now covers ALL executed columns (bulk + per-row), not just the final merged one - the reviewer's exact gap.
g Generalizes across inputs/types? Yes. columnMatchesType(..., true) recurses through Array/Nullable/Tuple/Map/LowCardinality; ValidateColumnType gtest covers top-level and nested Array(DateTime64(3)).
h Backward compatible? N/A - test-only change; strict_decimal_scale stays opt-in (default OFF), engine callers unchanged.
i Invariants/contracts preserved? Yes. checkAndFixupColumn's throw-on-type-mismatch contract unchanged; new block only sets a flag + soft-reports when stats != nullptr; flag reset per operation.

Session id: cron:clickhouse-worker-slot-1:20260711-033300

@clickhouse-gh

clickhouse-gh Bot commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 49.30% 85.80% +36.50%
Functions 63.90% 92.70% +28.80%
Branches 38.10% 77.90% +39.80%

Changed lines: Changed C/C++ lines covered: 63/103 (61.17%) · Uncovered code

Full report · Diff report

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — a921779

Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
CH Inc sync — CH Inc sync (private, not actionable)

No test failures on this head. Session id: cron:our-pr-ci-monitor:20260711-080000

@alexey-milovidov alexey-milovidov self-assigned this Jul 12, 2026
@alexey-milovidov
alexey-milovidov merged commit 19be9d5 into ClickHouse:master Jul 12, 2026
174 of 175 checks passed
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 12, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…ionsstress-scale-divergent-stash

Do not stash scale-divergent columns in the function property stress test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-ci 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