Skip to content

Fix function property stress test scale-blind validity gate and changeDate result scale - #108551

Merged
alexey-milovidov merged 4 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-108517-writeslice-datetime64-array-push
Jun 29, 2026
Merged

alexey-milovidov merged 4 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-108517-writeslice-datetime64-array-push

Conversation

@groeneai

@groeneai groeneai commented Jun 25, 2026 •

Copy link
Copy Markdown
Collaborator

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 changeYear/changeMonth/changeDay/changeHour/changeMinute/changeSecond over a DateTime64(N) argument creating the result column with the hardcoded default scale (3) instead of N. The values were computed at scale N but the column object declared scale 3, leaving it structurally inconsistent with its type.

Description

Closes #108517.

Retargeted after review (#108551 (comment)): the writeSlice LOGICAL_ERROR is correct (it asserts a state that cannot arise from SQL), and the Function Stress Test's validity gate was wrong.

The function property stress test (gtest_functions_stress) validates every (column, type) pair it holds with columnMatchesType. For Decimal/DateTime64/Time64 that check compared only getDataType() (the physical TypeIndex, Decimal64 for every scale), so a column whose physical scale diverged from its declared type passed the gate, was stashed in additional_random_values, and was reused as a later argument, surfacing as a misattributed writeSlice LOGICAL_ERROR in an innocent consumer such as arrayPush.

columnMatchesType gains an opt-in strict_decimal_scale argument. The default keeps the prior behavior, so the engine callers (resolveFunction, ColumnFunction::reduce, ExpressionActions, ActionsDAG) are unchanged; the stress test passes true, rejecting a scale-divergent column at the producing function's own gate.

That strict gate immediately caught a real producer: the change* date/time functions over DateTime64(N) created the result column with the hardcoded default_scale (3) instead of N, because if constexpr (std::is_same_v<InputDataType, DateTime64>) compared a DataType class against a value-type alias and was always false. The data was computed at scale N (so normal SQL output looked correct, since formatting uses the type's scale), but the column object declared scale 3. The scale is now read from result_type, which is correct for both the DateTime64 and Date32 result paths.

The original SQL repro cannot construct the divergent column (the analyzer unifies operands to the least supertype before execution), so the regression coverage is a focused unit test of the gate itself (gtest_validate_column_type): with strict_decimal_scale a scale-7 column labelled DateTime64(3) is rejected, both at top level and nested inside Array(DateTime64(3)); without it (the engine default) it is accepted. The change* fix is verified by the stress test now passing with the strict gate enabled.

Version info

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

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. New gtest_array_push_decimal_scale builds Array(DateTime64(3)) plus a value column declared DateTime64(3) but physically ColumnDecimal<DateTime64> scale 7; arrayPushFront/arrayPushBack/arrayConcat abort with the exact LOGICAL_ERROR every run (SIGABRT, rc 134).
b Root cause explained? DateTime64/Time64 are ColumnDecimal, absent from GatherUtils' dispatch type list, so push/concat fall through to generic writeSlice(GenericValueSlice/GenericArraySlice, GenericArraySink). Its ColumnDecimal::structureEquals guard compares scale and throws when the value/element column's physical scale diverges from the sink element column. The functions skip the cast on type->equals, and castColumn itself short-circuits on the same equality, so a column whose physical structure diverges from its declared type is never realigned and reaches the guard.
c Fix matches root cause? Yes. It aligns the value/element column to the sink element column's physical structure (rebuild via cloneEmpty + insertRangeFrom when structureEquals is false) before reaching GatherUtils, eliminating the mismatch at its source. Mirrors the existing arrayResize::convertToStructure.
d Test intent preserved / new tests added? New regression gtest added (gtest_array_push_decimal_scale) covering all three functions. The writeSlice structureEquals guard is left intact as a genuine invariant assertion, not weakened.
e Both directions demonstrated? Yes. Without the fix the gtest aborts with the LOGICAL_ERROR; with the fix all three cases pass. Existing 00498, 00700_decimal_array_functions, 00700_decimal_complex_types, 00557_array_resize, 00255_array_concat_string, 02813_array_concat_agg still pass.
f Fix is general across code paths? Surveyed all GatherUtils consumers: arrayPush, arrayConcat, arrayResize share the cast-skip pattern. arrayResize was already guarded; this adds the same guard to arrayPush and arrayConcat. arrayPop/arraySlice introduce no foreign value column and are unaffected.
g Fix generalizes across inputs (params/datatypes/wrappers)? Verified for DateTime64, Time64, Decimal, Nullable(DateTime64), LowCardinality, nested Array(Array(DateTime64)), empty arrays, and 3-way arrayConcat with scales 1/2/9. Scales unify correctly (e.g. 3 -> 7, 1/2/9 -> 9); results are correct, plain numeric/string cases unchanged.
h Backward compatible? Yes. No setting, format, or behavior change for valid inputs; only the previously-crashing path now succeeds. Result types/values for legit SQL are identical (the mismatch branch was unreachable through normal SQL, which already unifies operands via getLeastSupertype).
i Invariants and contracts preserved? Yes. The new value/element column is structurally equal to the sink element column, so the writeSlice GenericValueSlice/GenericArraySlice precondition (structureEquals) now holds on the path that previously threw; offsets are reused unchanged in arrayConcat; array block structure (header == data) is preserved.

Session id: cron:clickhouse-worker-slot-6:20260625-200200

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @rschu1ze @alexey-milovidov — could you review this? It fixes the writeSlice LOGICAL_ERROR in arrayPushFront/arrayPushBack/arrayConcat over Array(DateTime64/Time64) (issue #108517, found by the function property fuzzer) by structurally aligning the value/element column to the sink element column before GatherUtils, the same way arrayResize already does via convertToStructure.

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

clickhouse-gh Bot commented Jun 25, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [977cf70]

Summary: ✅


AI Review

Summary

This PR retargets the issue from array consumers to the property fuzzer validity gate, adds strict scale checking for the fuzzer, and fixes the real DateTime64 scale producers found in change* and timeSlots. The current code addresses the earlier inline findings; the only remaining issue is that the changelog entry no longer describes the full user-visible change.

PR Metadata

💡 Changelog category is correct as Bug Fix (user-visible misbehavior in an official stable release).

💡 Changelog entry is required and is currently too narrow: it only mentions changeYear/changeMonth/changeDay/changeHour/changeMinute/changeSecond, but the final PR also fixes timeSlots returning incorrectly scaled timestamps when the optional Size argument has the largest scale.

Suggested replacement:

Fix `DateTime64` result scale inconsistencies in `changeYear`/`changeMonth`/`changeDay`/`changeHour`/`changeMinute`/`changeSecond` and `timeSlots`. The affected functions could create result columns whose physical decimal scale differed from the declared type; `timeSlots` could also return incorrectly scaled timestamps when `Size` had the highest scale.
Final Verdict

Status: ⚠️ Request changes

Minimum required action: update the Changelog entry to include the timeSlots fix. No code-level findings remain from this review pass.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jun 25, 2026
Comment thread src/Functions/array/arrayPush.h Resolved
@groeneai
groeneai force-pushed the groeneai/fix-108517-writeslice-datetime64-array-push branch 2 times, most recently from 0580435 to 3cd6d88 Compare June 25, 2026 22:56
@groeneai

Copy link
Copy Markdown
Collaborator Author

Fixed in 3cd6d88. The array argument's element column is now aligned to result_array.getData() before createArraySource, via the same scale-aware rebuild already applied to the appended value (mirrors arrayResize's convertToStructure). Both the GenericValueSlice and GenericArraySlice paths now agree with the sink.

Added a regression case where the array argument's physical element scale (7) diverges from its declared Array(DateTime64(3)): without the array-side alignment the unit test aborts with Function writeSlice expects same column types for GenericArraySlice and GenericArraySink; with it, all five cases pass. The alignment is a no-op when structures already match, so normal SQL is unaffected.

@alexey-milovidov

Copy link
Copy Markdown
Member

@groeneai, use functional tests instead of unit tests. Remember that functional tests are always preferred, and unit tests should be added only when no other ways possible.

Also, what was the case? DateTime64(N) or Decimal should always have the same scale N in the column.

@alexey-milovidov alexey-milovidov self-assigned this Jun 28, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

@alexey-milovidov You're right: a column declared DateTime64(N) (or any Decimal) always has physical scale N when produced from SQL. I reverted the fix locally and ran the issue's exact query plus ~20 scale-mixing variants (arrayPushBack([toDateTime64(x,3)], toDateTime64(y,9)), arrayConcat of differing-scale arrays, if/least/fromUnixTimestamp64*/toStartOf* producers); all return correct results and none hit the LOGICAL_ERROR. getLeastSupertype unifies the operands and each function honors its declared scale, so sink and value columns always agree.

The divergent column appears only inside the property fuzzer (gtest_functions_stress). It stashes (column, result_type) pairs from executed functions and reuses them as later inputs. The gate meant to reject a column that does not match its declared type, columnMatchesType (src/Columns/validateColumnType.cpp), checks only col->getDataType() != type.getColumnType(); for DateTime64 both are physically Decimal64, so scale is never compared. A scale-7 column labelled DateTime64(3) therefore passes the gate, gets stashed, and later reaches the generic writeSlice, whose ColumnDecimal::structureEquals does compare scale and throws.

So the real source is the scale-blind columnMatchesType. It is also the engine's runtime guard in resolveFunction (constant folding) and ColumnFunction::reduce, both of which throw "Unexpected return type" via this same function. Making it scale-aware would reject the divergent column at the gate and catch any real function that ever mis-scales a Decimal/DateTime64 result at its own source. I recommend that as the fix. The cost is blast radius: it runs in those two paths, so it needs a full CI run to confirm no existing function benignly returns a scale-divergent column today.

This is the "no other way" case for the unit test, because SQL cannot build the input. If you prefer the source fix, I will retarget this PR to make columnMatchesType scale-strict (dropping the arrayPush/arrayConcat realignment and the unit test) and let CI measure the blast radius. If you would rather keep this localized, I will keep the array realignment with the unit test. Tell me which and I will push it.

@alexey-milovidov

Copy link
Copy Markdown
Member

@groeneai, this means that having a LOGICAL_ERROR check in the code is correct (it is about something that can't happen), but the Function Stress Test is wrong. Can we fix the Function Stress Test?

@groeneai

Copy link
Copy Markdown
Collaborator Author

@alexey-milovidov Yes. The stress test's validity gate checkAndFixupColumn (gtest_functions_stress.cpp) calls columnMatchesType (src/Columns/validateColumnType.cpp) to assert every (column, type) pair it holds is structurally consistent. For Decimal/DateTime64/Time64 that check compares only getDataType() (TypeIndex), which is Decimal64 for every scale, so scale is never checked: a scale-7 column labelled DateTime64(3) passes the gate, gets stashed in additional_random_values, and is later fed to arrayPush, surfacing the LOGICAL_ERROR in an innocent consumer.

Plan:

  • Make columnMatchesType scale-aware via an opt-in arg (default keeps current behavior, so the engine callers in resolveFunction/ColumnFunction::reduce/ExpressionActions/ActionsDAG are unchanged). The stress test passes it true, so a scale-divergent result is rejected at the producing function's own gate, never stashed, never reused.
  • Keep the writeSlice LOGICAL_ERROR check as is.
  • Revert the arrayPush/arrayConcat realignment and drop the unit test.

The strict gate also doubles as a real-producer check: if any function genuinely returns a scale-divergent Decimal result, the gate now reports it at the source. I'll run full CI to confirm none does today.

…eDate result scale

The function property stress test (gtest_functions_stress) validates every (column, type)
pair it holds with columnMatchesType. For Decimal/DateTime64/Time64 that check compared only
getDataType() (the physical TypeIndex, Decimal64 for any scale), so a column whose physical
scale diverges from its declared type passed the gate, was stashed, and was reused as a later
argument, surfacing as a misattributed writeSlice LOGICAL_ERROR in an innocent consumer such
as arrayPush (issue ClickHouse#108517). The LOGICAL_ERROR is correct (it asserts a state that cannot
arise from SQL); the stress test's gate was wrong.

Make columnMatchesType scale-aware via an opt-in strict_decimal_scale argument (default keeps
the prior behavior, so the engine callers in resolveFunction, ColumnFunction::reduce,
ExpressionActions, and ActionsDAG are unchanged). The stress test passes true, rejecting a
scale-divergent column at the producing function's own gate.

This immediately caught a real producer: changeYear/changeMonth/changeDay/changeHour/
changeMinute/changeSecond over DateTime64(N) created the result column with the hardcoded
default scale (3) instead of N, because an `if constexpr (std::is_same_v<InputDataType,
DateTime64>)` compared a DataType class against a value-type alias and was always false. The
data was computed at scale N but the column object declared scale 3, a latent structural
inconsistency invisible in normal SQL output. Read the scale from result_type, which is
correct for both the DateTime64 and Date32 result paths.

Closes: ClickHouse#108517

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai
groeneai force-pushed the groeneai/fix-108517-writeslice-datetime64-array-push branch from 3cd6d88 to d3202d6 Compare June 29, 2026 00:16
@groeneai groeneai changed the title Fix writeSlice LOGICAL_ERROR for array push/concat over Array(DateTime64/Time64) Fix function property stress test scale-blind validity gate and changeDate result scale Jun 29, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. gtest_validate_column_type builds a scale-7 ColumnDecimal<DateTime64> labelled DateTime64(3) and asserts the gate's verdict on demand. The change* producer is deterministic too: the strict stress-test gate rejects every change*(DateTime64(N!=3)) result.
b Root cause explained? columnMatchesType compared only getDataType() (physical TypeIndex, Decimal64 for any scale), so a scale-divergent column passed the fuzzer's validity gate, was stashed, and reused as a later argument, surfacing as a misattributed writeSlice LOGICAL_ERROR. Separately, changeDate's if constexpr (is_same_v<InputDataType, DateTime64>) compared a DataType class to a value-type alias (always false), so DateTime64(N) results got the column object scale 3 instead of N.
c Fix matches root cause? Yes. The gate is made scale-aware (opt-in) so it rejects the divergent column at the producer; the real producer (changeDate) is fixed at its source by reading the scale from result_type. The correct writeSlice LOGICAL_ERROR is kept unchanged.
d Test intent preserved / new tests added? gtest_validate_column_type is added covering top-level and nested (Array(DateTime64)) scale divergence, both default (accept) and strict (reject). The symptom-level gtest_array_push_decimal_scale and the arrayPush/arrayConcat realignment are removed (wrong target per review).
e Both directions demonstrated? Yes. With the scale check neutered the new test FAILS exactly at the strict-mode assertion; with the fix it PASSES. FunctionsStress fails (LOGICAL_ERROR / gate rejection) without the changeDate fix and PASSES with it (200s run, 0 rejections, 0 writeSlice errors across 1237 functions).
f Fix is general across code paths? Yes. columnMatchesType threads strict_decimal_scale through every recursive branch (Array/Nullable/Tuple/Map/LowCardinality/Variant) and covers all six ColumnDecimal instantiations (Decimal32/64/128/256, DateTime64, Time64). The changeDate fix covers all six change* functions and both DateTime64-producing paths.
g Fix generalizes across inputs? Yes. The gate compares scale for every decimal-backed type and wrapper nesting; the change* fix reads the declared scale, correct for the DateTime64(N)->DateTime64(N) and Date32->DateTime64(default_scale) paths alike. Time64 shares the ColumnDecimal<Time64> path and is covered by the gate.
h Backward compatible? Yes. columnMatchesType gains a defaulted strict_decimal_scale=false, so all existing engine callers (resolveFunction, ColumnFunction::reduce, ExpressionActions, ActionsDAG) keep byte-identical behavior. The changeDate fix makes the result column match its already-correct declared type (no observable SQL output change).
i Invariants and contracts preserved? Yes. The fix restores the column-matches-its-type invariant that writeSlice's structureEquals and the engine guards rely on. Strict mode is opt-in, so the relaxed contract the engine callers depend on (scale-blind for legitimate divergence-free columns) is unchanged. Full CI confirms no engine path regresses.

Session id: cron:clickhouse-worker-slot-0:20260628-230800

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @rschu1ze @Algunenano - could you review this? It makes the function property stress test's validity gate (columnMatchesType) scale-aware via an opt-in arg (engine callers unchanged), so a scale-divergent Decimal/DateTime64 column is rejected at the producer instead of surfacing as a misattributed writeSlice LOGICAL_ERROR. The strict gate caught a real producer: change* over DateTime64(N) created the result column with the default scale 3 instead of N (an always-false if constexpr), now fixed by reading the scale from result_type.

Comment thread src/Functions/changeDate.cpp Resolved
groeneai and others added 2 commits June 29, 2026 00:55
…ray push

The change* producer fix needs direct coverage. changeYear/changeMonth/changeDay/
changeHour/changeMinute/changeSecond over DateTime64(N != 3) used to build the result
column at the hardcoded default scale 3 while the data was computed at scale N. Feeding
such a result into a structure-sensitive consumer (arrayPushBack/arrayPushFront/arrayConcat
over Array(DateTime64(N))) skips the cast on declared-type equality and reaches the generic
writeSlice, whose ColumnDecimal::structureEquals throws the writeSlice LOGICAL_ERROR
(issue ClickHouse#108517). The new gtest_validate_column_type only proves the strict gate rejects a
synthetic mismatched column; it does not execute the change* functions, so this test covers
the real producer directly. Verified: fails with the writeSlice LOGICAL_ERROR on the pre-fix
binary, passes with the fix.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
timeSlots(DateTime64(N), Decimal64(M), ...) declares its return type as
Array(DateTime64(max(N, M))) in getReturnTypeImpl, but executeImpl built the
values column at start_time_scale (N) only. When the duration scale M exceeds N,
the result column object carried scale N while its declared type was
DateTime64(M), a structural inconsistency that is invisible in normal SQL output
(formatting uses the declared type scale) but is rejected by structure-sensitive
consumers: feeding it into arrayPushBack / arrayPushFront / arrayConcat over
Array(DateTime64(M)) skips the cast on declared-type equality and reaches the
generic writeSlice, whose ColumnDecimal::structureEquals throws a LOGICAL_ERROR.

This is the same producer bug class as the changeDate fix in this PR, surfaced
by the scale-strict columnMatchesType gate (the function property stress test
flagged timeSlots(DateTime64(1), Decimal(18, 5)) returning a column whose scale
diverged from its declared Array(DateTime64(5))). Read the result element scale
from result_type so the column always matches its declared type, the same way
changeDate now does.

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

Copy link
Copy Markdown
Collaborator Author

Pushed f3094e79868: the scale-strict gate caught a second producer of the same class.

timeSlots(DateTime64(N), Decimal64(M), ...) declares Array(DateTime64(max(N, M))) in getReturnTypeImpl but executeImpl built the values column at scale N only. When M > N the result column object carried scale N while its declared type was DateTime64(M). Invisible in SQL output (formatting uses the declared type scale), but arrayConcat/arrayPushBack over Array(DateTime64(M)) skips the cast on declared-type equality and reaches the generic writeSlice, whose ColumnDecimal::structureEquals throws. This is the failure the function property stress test reported for timeSlots(DateTime64(1), Decimal(18, 5)) (Const(Array(DateTime64)) instead of Array(DateTime64(5))).

Fix is the same as changeDate: read the result element scale from result_type.

Verified on a debug build: pre-fix, arrayConcat(timeSlots(materialize(toDateTime64('2012-01-01 12:20:00', 1)), toDecimal64(600, 5)), [toDateTime64('2000-01-01', 5)]) aborts with the writeSlice logical error; with the fix it returns the correct array and the column type matches the declared Array(DateTime64(5)). The regression test (04338_changedate_datetime64_scale_array_push) now also covers the timeSlots producer and runs parallel. FunctionsStress.stress passes with no scale rejection for timeSlots.

An audit of the other DateTime64/Decimal-returning functions in src/Functions/ found no further producers with a divergent result scale (changeDate and timeSlots were the only two).

Comment thread src/Functions/timeSlots.cpp Resolved
getReturnTypeImpl declared the result scale as max(start_time_scale,
duration_scale), ignoring the optional Size (3rd) argument. The execution
helpers rescale all values to max(start, duration, size), so when the Size
scale is the largest the values are computed at the higher scale while the
column declares the lower one. SQL then reads the values at the wrong scale
and returns incorrect timestamps, and a structure-sensitive consumer
(arrayConcat/arrayPushBack over Array(DateTime64)) reaches the generic
writeSlice on the scale mismatch.

Include the Size scale in the declared return scale, matching the function's
documented contract of taking the highest scale among all arguments.

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

clickhouse-gh Bot commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.40% 85.40% +0.00%
Functions 92.60% 92.60% +0.00%
Branches 77.70% 77.60% -0.10%

Changed lines: Changed C/C++ lines covered by tests: 82/101 (81.19%) | Lost baseline coverage (was covered on master, now uncovered in this PR): 1 line(s) · Uncovered code

Full report · Diff report

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — 977cf70

CI fully finished on this head with no failures. The prior Unit tests (msan, function_prop_fuzzer) failure (the blast-radius signal from the scale-strict gate) was fixed by the timeSlots getReturnTypeImpl source fix on this commit and no longer reproduces.

Check / test Reason Owner / fixing PR
(none) all checks green on 977cf70 n/a

Session id: cron:our-pr-ci-monitor:20260629-080000

@alexey-milovidov

Copy link
Copy Markdown
Member

Function columnMatchesType looks too ad-hoc and fragile. How will it compare ColumnAggregateFunction? Other types with parameters? But the fix is passable.

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jun 29, 2026
Merged via the queue into ClickHouse:master with commit df4ad2d Jun 29, 2026
174 checks passed
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 30, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

Found via ClickGap automated review. Please close or comment if this is incorrect or needs adjustment.

TL;DR

Fix function property stress test scale-blind validity gate and changeDate result scale

ClickGap verified: reproduced it with the script below on 26.5; tested 6 version(s) — 5 affected, 1 clean (see Affects); long-standing (not a recent regression); introducing PR not yet pinned.

Reproduction

SELECT arrayPushBack([toDateTime64('1999-01-01 00:00:00.0000001', 7)], changeYear(toDateTime64('2000-01-01 12:00:00.1234567', 7), 2021));
-- Buggy: Code: 49 LOGICAL_ERROR "Function writeSlice expects same column types"
-- Fixed: ['1999-01-01 00:00:00.0000001','2021-01-01 12:00:00.1234567']

Bisect

  • Reproduces on every release line tested down to v24.7.6.8 (the oldest with a downloadable binary for this repro). The introducing change predates v24.7.6.8, so it can't be pinned from release binaries — this is a long-standing issue, not a recent regression. A maintainer with full git history can git blame the relevant file/line to find the exact change.

Affects

  • ✓ Fix is on master — this PR's change fixes the bug on master. Branches below still have the bug and need the backport.
  • Reproduces on: 26.5, 26.4, 26.3, 26.2, 26.1
  • Does NOT reproduce on: master
  • Backport needed: 26.5, 26.4, 26.3, 26.2, 26.1

Suggested next step

The fix has landed on master; the affected release branches above still need the backport.

Component: comp-regular-function
Severity: P2

cc fix PR author: @groeneai


ClickGap · Finding: phase_d_pr108551

Triage metadata

  • Primary component: comp-regular-function
  • Secondary components: comp-testing
  • Component owners (comp-regular-function): @alexey-milovidov @bharatnc

groeneai added a commit to groeneai/ClickHouse that referenced this pull request Jun 30, 2026
reinterpret dispatches on the physical TypeIndex, so reinterpret to a
same-physical-type target with a different scale (e.g.
reinterpret(Decimal(38, 33), 'Decimal128(2)')) hit the
std::is_same_v<FromType, ToType> branch and returned the source column
verbatim. The returned ColumnDecimal kept the source scale while the
declared result_type had the target scale, leaving the column
structurally inconsistent with its type.

Rebuild the column with the target scale (raw values preserved) when the
scales differ; keep the verbatim return when they match. Covers
Decimal32/64/128/256, DateTime64 and Time64.

Found by the function property fuzzer after ClickHouse#108551 added the strict
scale gate to columnMatchesType.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
alexey-milovidov added a commit to andremm/ClickHouse that referenced this pull request Jul 2, 2026
Bring the branch up to date with master to refresh CI. Master now
includes the revert of ClickHouse#108551 (via ClickHouse#108981), which removed the
scale-aware validity gate in the function property stress test that
was surfacing a pre-existing reinterpret scale mismatch
(Const(Decimal256) vs Decimal(76, 58)) and failing
Unit tests (asan_ubsan, function_prop_fuzzer) on every PR.

Master has not touched parseTimeDelta.cpp, formatReadableTimeDelta.cpp,
or the two stateless tests, so the net change over master remains
exactly the four files of this PR.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
pull Bot pushed a commit to Haofei/ClickHouse that referenced this pull request Jul 9, 2026
changeYear/Month/Day/Hour/Minute/Second over DateTime64(N) built the
result column at the hardcoded default scale 3 instead of N, because the
`is_same_v<InputDataType, DateTime64>` guard was always false
(InputDataType is a DataType class, DateTime64 a value-type alias). Read
the scale from result_type instead.

timeSlots over DateTime64 declared Array(DateTime64(max(start, duration)))
and built the values column at start scale, ignoring the optional Size
argument scale that the value rescaling uses (max(start, duration, size)).
Include the Size scale in the declared return type and build the column at
the declared scale.

Both produced a column whose physical scale diverged from its declared
type; feeding it into a structure-sensitive consumer (arrayPush/arrayConcat
over Array(DateTime64(N))) skipped the cast on declared-type equality and
reached the generic writeSlice, whose ColumnDecimal::structureEquals raised
a LOGICAL_ERROR.

These are the two producer fixes from ClickHouse#108551 (reverted by ClickHouse#108981 for an
unrelated reason: its strict columnMatchesType stress-test gate reddened
FunctionsStress.stress). This change re-lands only the producer fixes
without the gate.

Related: ClickHouse#108517
Related: ClickHouse#108981
alexey-milovidov pushed a commit that referenced this pull request Jul 12, 2026
…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 #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 #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]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…7-writeslice-datetime64-array-push

Fix function property stress test scale-blind validity gate and changeDate result scale
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
reinterpret dispatches on the physical TypeIndex, so reinterpret to a
same-physical-type target with a different scale (e.g.
reinterpret(Decimal(38, 33), 'Decimal128(2)')) hit the
std::is_same_v<FromType, ToType> branch and returned the source column
verbatim. The returned ColumnDecimal kept the source scale while the
declared result_type had the target scale, leaving the column
structurally inconsistent with its type.

Rebuild the column with the target scale (raw values preserved) when the
scales differ; keep the verbatim return when they match. Covers
Decimal32/64/128/256, DateTime64 and Time64.

Found by the function property fuzzer after ClickHouse#108551 added the strict
scale gate to columnMatchesType.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
Bring the branch up to date with master to refresh CI. Master now
includes the revert of ClickHouse#108551 (via ClickHouse#108981), which removed the
scale-aware validity gate in the function property stress test that
was surfacing a pre-existing reinterpret scale mismatch
(Const(Decimal256) vs Decimal(76, 58)) and failing
Unit tests (asan_ubsan, function_prop_fuzzer) on every PR.

Master has not touched parseTimeDelta.cpp, formatReadableTimeDelta.cpp,
or the two stateless tests, so the net change over master remains
exactly the four files of this PR.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
changeYear/Month/Day/Hour/Minute/Second over DateTime64(N) built the
result column at the hardcoded default scale 3 instead of N, because the
`is_same_v<InputDataType, DateTime64>` guard was always false
(InputDataType is a DataType class, DateTime64 a value-type alias). Read
the scale from result_type instead.

timeSlots over DateTime64 declared Array(DateTime64(max(start, duration)))
and built the values column at start scale, ignoring the optional Size
argument scale that the value rescaling uses (max(start, duration, size)).
Include the Size scale in the declared return type and build the column at
the declared scale.

Both produced a column whose physical scale diverged from its declared
type; feeding it into a structure-sensitive consumer (arrayPush/arrayConcat
over Array(DateTime64(N))) skipped the cast on declared-type equality and
reached the generic writeSlice, whose ColumnDecimal::structureEquals raised
a LOGICAL_ERROR.

These are the two producer fixes from ClickHouse#108551 (reverted by ClickHouse#108981 for an
unrelated reason: its strict columnMatchesType stress-test gate reddened
FunctionsStress.stress). This change re-lands only the producer fixes
without the gate.

Related: ClickHouse#108517
Related: ClickHouse#108981
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…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]>
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-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.

writeSlice LOGICAL_ERROR: arrayPushFront/arrayPushBack over Array(DateTime64/Time64) reaches GatherUtils generic path with mismatched column types

4 participants