Skip to content

Fix changeDate and timeSlots DateTime64 result column scale - #108994

Merged
yariks5s merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/fix-producer-datetime64-decimal-scales
Jul 9, 2026
Merged

yariks5s merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/fix-producer-datetime64-decimal-scales

Conversation

@groeneai

@groeneai groeneai commented Jun 30, 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 DateTime64(N) building the result column at the hardcoded default scale 3 instead of N, and timeSlots over DateTime64 ignoring the optional Size argument scale in its declared return type. Both produced a column whose physical scale diverged from its declared type, which led to a LOGICAL_ERROR (writeSlice expects same column types) when the result was passed to arrayPushBack/arrayPushFront/arrayConcat, and timeSlots with the largest scale in Size returned wrong timestamps.

Description

Re-lands the two producer fixes from #108517 (originally in #108551, reverted by #108981 for an unrelated reason: that PR also added a strict columnMatchesType scale gate to the function property stress test, which reddened FunctionsStress.stress). This change re-lands only the producer fixes, without the gate, so it cannot affect the stress test.

changeDate: the DataTypeDateTime64 result branch built the column at DataTypeDateTime64::default_scale (3) because the if constexpr (std::is_same_v<InputDataType, DateTime64>) guard was always false (InputDataType is a DataType class, DateTime64 a value-type alias). The values were computed at the declared scale N, so a DateTime64(N != 3) result carried a physically scale-3 column. Now reads the scale from result_type.

timeSlots: getReturnTypeImpl declared Array(DateTime64(max(start_scale, duration_scale))) and executeImpl built the values column at start_scale, but TimeSlotsImpl rescales values to max(start_scale, duration_scale, time_slot_scale). The 3rd Size argument scale is now included in the declared return type, and the column is built at the declared scale read from result_type.

Either divergence is invisible to a same-declared-type cast, so feeding the result into a structure-sensitive consumer (arrayPush*/arrayConcat over Array(DateTime64(N))) reached the generic writeSlice, whose ColumnDecimal::structureEquals raised the LOGICAL_ERROR. The production check is correct and unchanged; the fix makes each producer build a column consistent with its declared type.

Regression test 04338_changedate_datetime64_scale_array_push (parallel-safe) covers both functions over DateTime64(N != 3) through the array consumers. It raises the writeSlice LOGICAL_ERROR on clean master (server abort in debug/sanitizer builds) and passes with the fix.

The reinterpret producer of the same divergence is fixed separately in #108878.

Version info

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

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
@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. SELECT arrayPushBack([toDateTime64('2020-01-01 00:00:00', 7)], changeSecond(materialize(toDateTime64('2020-01-01 12:00:00', 7)), 30)) raises the writeSlice LOGICAL_ERROR every time on clean master; SELECT timeSlots(toDateTime64('1970-01-01 00:00:01.0', 1, 'UTC'), toDecimal64(1, 1), toDecimal64(0.5, 4)) returns wrong scale-1 timestamps.
b Root cause explained? Yes. changeDate: the DateTime64 result branch built the column at default_scale 3 because is_same_v<InputDataType, DateTime64> is always false (class vs value-type alias), while values were computed at declared scale N; the scale-N-vs-3 column diverges from its type. timeSlots: getReturnTypeImpl declared max(start, duration) ignoring the Size arg, but values are rescaled to max(start, duration, size); the column then diverges from its declared type, or returns wrong timestamps when Size scale is largest. A structure-sensitive consumer skips the cast on declared-type equality and reaches generic writeSlice, whose ColumnDecimal::structureEquals throws.
c Fix matches root cause? Yes. changeDate reads the scale from result_type; timeSlots includes the Size scale in the declared return type and builds the column at the declared scale. Each producer now emits a column consistent with its declared type. No symptom guard.
d Test intent preserved / new tests added? Yes. New functional test 04338_changedate_datetime64_scale_array_push exercises changeYear/Month/Day/Hour/Minute/Second and timeSlots over DateTime64(N != 3) through arrayPush/arrayConcat plus toTypeName scale assertions. The production writeSlice LOGICAL_ERROR check is unchanged.
e Both directions demonstrated? Yes. Pre-fix binary (Build ID b081cb93): test raises writeSlice LOGICAL_ERROR (server abort in debug). Fixed binary (Build ID 6295fb59): test passes, output matches reference; 02982_changeDate / 00734_timeslot / 02319_timeslots_dt64 / 04113_function_timeslots_combinations all pass (no regression).
f Fix is general across code paths? Yes. Audited all DateTime64/Decimal-returning functions in src/Functions/. The same divergence existed in reinterpret (fixed separately in #108878). YYYYMMDDhhmmssToDateTime64 is not a producer (its 2 stress-test hits were a reinterpret-divergent precision argument flowing through it). No other producer found.
g Fix generalizes across inputs? Yes. Verified across the scale matrix: changeSecond scales 0/2/4/6 (declared == physical, correct values); timeSlots (start,dur,size) combos {1,1,4}->4, {2,3,7}->7, {9,1,1}->9, {5,5,5}->5, {3,6,2}->6 all declare max-of-all-three. The test asserts toTypeName so the scale is checked, not just the values.
h Backward compatible? Yes. No setting, format, or wire change. The result column now carries the scale its declared type always promised; SQL output is identical (formatting already used the type scale). Only the internal column object's scale is corrected.
i Invariants and contracts preserved? Yes. The fix restores the IColumn invariant that a Decimal/DateTime64 column's physical scale equals its declared type's scale. getReturnTypeImpl and executeImpl now agree on the scale (executeImpl reads it from result_type). No lock/ownership/lifetime change.

Session id: cron:clickhouse-worker-slot-3:20260630-201200

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @rschu1ze @Algunenano - could you review this? It re-lands the two producer fixes from the reverted #108551 (changeDate building the DateTime64 result column at default_scale 3 due to an always-false is_same_v guard, and timeSlots omitting the Size-arg scale from its declared return type), WITHOUT the strict columnMatchesType stress-test gate that caused the revert. The result-column scale now matches the declared type, so arrayPush/arrayConcat over Array(DateTime64(N)) no longer hits the writeSlice LOGICAL_ERROR. Functional regression added; reinterpret (the other producer) is fixed separately in #108878.

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

clickhouse-gh Bot commented Jun 30, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [5ddb58b]

Summary: ✅


AI Review

Summary

This PR fixes two producer-side DateTime64 scale mismatches: changeDate now builds its result column at the declared result scale, and timeSlots now includes the optional Size scale in the declared result type and builds the nested values column at that same scale. The patch matches the root cause described in the PR and linked issue, and the added regression exercises the broken array-consumer paths and the Size-scale value/type behavior.

Final Verdict

Status: ✅ Approve

No blocking or major findings. I did not post inline review comments.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jun 30, 2026
@groeneai

groeneai commented Jul 1, 2026

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — 5ddb58b

CI fully finished (Finish Workflow = success). One test failure, not PR-caused; owned by an existing external fix PR.

Check / test Reason Owner / fixing PR
Stateless tests (arm_binary, parallel) / 02875_parallel_replicas_cluster_all_replicas flaky (transient) - UNEXPECTED_PACKET_FROM_SERVER (expected TablesStatusResponse, got ProfileInfo), the #93018 connection-pool ProfileInfo-desync race; CI same-settings rerun passed 180/180, and this PR only touches changeDate/timeSlots DateTime64 result-column scale, which cannot affect distributed connection pooling #108854 (external, open)

The changeDate/timeSlots DateTime64 scale fixes and the regression test 04338_changedate_datetime64_scale_array_push all pass. No PR-caused failures.

Session id: cron:our-pr-ci-monitor:20260701-033000

@yariks5s yariks5s self-assigned this Jul 1, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

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

Changed lines: Changed C/C++ lines covered: 18/18 (100.00%) · Uncovered code

Full report · Diff report

@yariks5s
yariks5s added this pull request to the merge queue Jul 9, 2026
Merged via the queue into ClickHouse:master with commit 87bfca8 Jul 9, 2026
512 of 515 checks passed
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 9, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

Hi — this looks like a backport candidate for 26.1, 26.2, 26.3, 26.4, 26.5, 26.6. I ran this PR's own regression test against each branch's binary and the bug reproduces there (the fix isn't present yet).

zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…cer-datetime64-decimal-scales

Fix changeDate and timeSlots DateTime64 result column scale
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.

5 participants