Repository navigation
Fix DateTime64 and Time64 output truncation for scale above 6 with the groups-of-thousands setting - #112854
Conversation
With date_time_64_output_format_cut_trailing_zeros_align_to_groups_of_thousands
enabled, the printed fraction of a DateTime64 or Time64 value was capped at 6
digits. The setting rounds the width up to the next multiple of three that keeps
every significant digit, but the round-up was written as a hardcoded two-way
choice with a ceiling of 6:
size_t new_scale = (last_non_zero_pos >= 3) ? 6 : 3;
so any last-significant-digit position at 3 or beyond collapsed to exactly 6 and
writeString emitted only 6 characters, dropping digits 7 to 9. The clearest
symptom is not lost trailing precision but a non-zero fraction rendering as all
zeros: a DateTime64(9) holding 700 nanoseconds printed .000000 while
toUnixTimestamp64Nano of the same value modulo 1000000000 is 700.
Removing only the ceiling keeps the documented rule intact. The expression exists
twice, in writeDateTime64FractionalText and in writeTime64FractionalText, so
Time64 carried the identical defect; both are fixed and reverting either site
alone reddens a disjoint set of test rows.
The width is deliberately not clamped to the declared scale. Master already pads
past the declared scale, which is what aligning to groups of thousands does: a
DateTime64(1) value of .7 prints .700 today, and .00007 at scale 5 prints
.000070. Enumerating all 45 combinations of scale and last-significant-digit
position, the new rule is byte-identical to master in 39 and differs only in the
6 where master loses a significant digit, always emitting a width that is a
multiple of three, at most 9. A clamp would instead change 9 combinations at
scales 1, 2, 4 and 5 and emit widths that are not multiples of three, so it would
regress scales below the ones this bug concerns, including the DateTime64(6)
neighbourhood the setting was introduced for. The new test pins those rows.
The setting's own description documented the output range as [0, 3, 6] and is
updated in the same change, since it is the user-facing contract reported by
system.settings. The default stays false, so no SettingsChangesHistory entry is
needed and nothing changes for anyone who has not opted in.
Closes: ClickHouse#104997
…tring and CAST Round 1 review found that rewriting the description of date_time_64_output_format_cut_trailing_zeros_align_to_groups_of_thousands introduced the word Time64 unqualified. The word is new in this branch: on master that description does not mention Time64 at all. Time64 honours the setting only on the toString and CAST routes (FunctionsConversion.h), not in the row output formats: SerializationTime64::serializeText discards its FormatSettings argument and calls the plain writeTime64Text, whereas SerializationDateTime64 reads the setting. The description is user-facing (system.settings.description), so an unqualified promise there overstates what the code delivers. Name the routes instead of dropping Time64 entirely, since Time64 genuinely is fixed on toString and CAST and the test asserts exactly those rows. Wiring the serializer is a separate behaviour change and is tracked separately. Measured with the rebuilt binary: for a Time64 value with trailing zeros, toString and CAST trim it while TSV and JSONEachRow do not, and the DateTime64 controls trim on both routes.
Internal second-model reviewTwo independent cold reviews plus two second-model gate passes ran over this diff before it
1. Real and introduced here: on 2. The 3. The rename is recorded, and it changed the filename only, not a byte of SQL. The Independently re-derived while reviewing: the two changed expressions are the only Session id: cron:clickhouse-review-slot-50:20260801-022200 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-review-slot-50:20260801-022200 |
|
cc @yariks5s @Algunenano, could you review this? The align-to-groups-of-thousands round-up in |
|
Workflow [PR], commit [a3ccf02] Summary: ✅
AI ReviewSummaryThis PR fixes the hardcoded 6-digit ceiling in the grouped-thousands trimming path for Findings
💡 Nits
Final VerdictChanges requested. LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 11/11 (100.00%) · Uncovered code |
The AI reviewer noted that `date_time_64_output_format_cut_trailing_zeros_align_to_groups_of_thousands` carried only its 24.10 introduction row, which still describes the old `(0, 3, 6)` output scale. `system.settings_changes` exposes that `reason` text directly, so the compatibility audit path reported pre-fix semantics for a setting whose contract this change widens to `[0, 3, 6, 9]`. Add a 26.8 row recording the semantics change. `previous_value` equals `new_value` (`false`), so `applyCompatibilitySetting` skips it and no user value moves: the row is a history record, not a default flip. This is the established shape for a semantics-only change - 31 settings already carry an older introduction row plus a later value-noop row, for example `allow_experimental_delta_lake_writes` (25.9 then 26.7, "Delta Lake writes were moved to Beta.") and `enable_full_text_index` (25.12 then 26.2, "The text index is now GA"). Behavior is unchanged: `src/IO/WriteHelpers.h` is untouched and the exhaustive scale by last-non-zero-position grid is byte-identical. Co-Authored-By: Claude Fable 5 <[email protected]>
…oute assertions discriminating Two findings from the second-model re-review, both reproduced locally. The setting description claimed row output formats are trimmed. They are only under the default `date_time_output_format = 'simple'`: `SerializationDateTime64::serializeText` consults the setting in the `Simple` branch alone, so `iso` and `unix_timestamp` always print the full declared scale. Measured with the setting on and a `DateTime64(9)` value of `.123000000`: `simple` prints `.123`, `iso` prints `.123000000Z`, `unix_timestamp` prints `1704067200.123000000`. Conversion with `toString` and `CAST` is trimmed in all three modes, so the description now separates the two. The `Time64` and multi-route assertions all used a scale 9 value whose every digit is significant. Such a value prints nine digits whether or not the route consults the setting, so ten of those rows would have stayed green if a route silently stopped honouring it. Each route gains a scale 7 twin, which reads `.123456` before the fix, `.123456700` after it, and `.1234567` if the route bypasses the setting. Rows that change when the setting is forced off went from 26 to 34, and every new row reddens when the pre-fix six-digit ceiling is restored at both call sites. Added rows only; no existing expectation moved, and `src/IO/WriteHelpers.h` is untouched by this commit. Co-Authored-By: Claude Fable 5 <[email protected]>
The second-model re-review found that the new description sentence claims `Time64` is trimmed the same way as `DateTime64`, which overclaims for an all-zero fraction. Measured: with the setting on, a zero-fraction `DateTime64(9)` prints `2024-01-01 00:00:00` while a zero-fraction `Time64(9)` prints `100:00:00.000`. The cause is the caller, not the new rule: the `DateTime64` writer emits the delimiter only when the fraction is non-zero, while the `Time64` writer always emits it for a positive scale. This is pre-existing and unchanged here - for an all-zero fraction the old and the new formula both yield width three, and neither caller is touched by this change - so only the wording is corrected, in both the setting description and the history reason. Three assertions pin the asymmetry so the two descriptions cannot drift from the behavior. Aligning the `Time64` writer with the `DateTime64` one is a separate behavior change and is left out of this change. Co-Authored-By: Claude Fable 5 <[email protected]>
Head branch was pushed to by a user without write access
The setting description and the 26.8 history reason both said Time64 is
trimmed by toString and CAST without qualification. That over-promises: only
a DIRECT scalar conversion reaches
writeTime64TextCutTrailingZerosAlignToGroupOfThousands via
FunctionsConversion.h. A Time64 nested in a tuple, array or map is
serialized by ConvertImplGenericToString through
SerializationTime64::serializeText, which discards its FormatSettings and
always calls writeTime64Text, so the declared scale is printed.
Measured on a build whose served buildId was asserted against its own ELF
Build ID, setting enabled, scale 7 value .1234567:
toString(toTime64(...)) 100:00:00.123456700 trimmed
toString(tuple(toTime64(...))) 100:00:00.1234567 not trimmed
toString([toTime64(...)]) 100:00:00.1234567 not trimmed
toString(map('k', toTime64(...)) 100:00:00.1234567 not trimmed
The DateTime64 twins of those composite queries DO trim, because
SerializationDateTime64::serializeText reads the setting, so the gap is
specific to Time64 rather than to composite output.
Corrects the wording in both carriers and pins the contract with five test
rows, using a scale 7 value that discriminates three ways: .123456 under the
pre-fix ceiling, .123456700 when the route consults the setting, .1234567
when it does not. Restoring the setting read in
SerializationTime64::serializeText reddens exactly those three Time64 rows
and nothing else, so they are not vacuous.
Teaching SerializationTime64 to honor the setting is a behavior change
across every nested and row-output carrier and belongs in its own PR; this
change only stops the documentation from promising it.
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 26/26 (100.00%) · Uncovered code |
CI finish ledger — b561f72Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task 174 check-runs, 0 incomplete (paginated check-runs API),
Not PR-caused: this PR only changes Session id: cron:our-pr-ci-monitor:20260802-000000 |
| DECLARE(Bool, date_time_64_output_format_cut_trailing_zeros_align_to_groups_of_thousands, false, R"( | ||
| Dynamically trim the trailing zeros of datetime64 values to adjust the output scale to [0, 3, 6], | ||
| corresponding to 'seconds', 'milliseconds', and 'microseconds')", 0) \ | ||
| Dynamically trim the trailing zeros of `DateTime64` values, rounding the output scale up to the next |
There was a problem hiding this comment.
Trimmed to 3 lines / 305 chars in b223f34 (was 8 lines / 826). Kept the contract: the widened [0, 3, 6, 9] round-up, the dropped all-zero fraction, and one sentence on Time64. The scope caveats it carried are in the PR description and pinned by the test, so they do not belong in a user-facing description.
I audited the rest of the diff for the same problem and found two more spots:
- the new
SettingsChangesHistory.cppnote was 603 chars, the longest of all 1230 rows (median 51) - trimmed to 161, recorded values untouched; - two test comment blocks were 4 lines - trimmed to 3.
No behaviour change: all 140 query-result lines in the reference are byte-identical, and 01324_settings_documentation still passes. The reference is regenerated only because -- { echo } echoes the edited comments.
@PedroTadim asked for the description of date_time_64_output_format_cut_trailing_zeros_align_to_groups_of_thousands to be trimmed: at 826 characters over 8 lines it was the 10th longest of the 295 descriptions in FormatFactorySettings.h, against a median of 106. The scope caveats it carried (which conversion routes trim, the date_time_output_format interaction, the nested-Time64 and Time64 row-format gaps) are already documented in the PR description and pinned row by row by the test, so they do not belong in a user-facing setting description. What remains is the contract: the widened [0, 3, 6, 9] round-up, the dropped all-zero fraction, and the one-sentence Time64 qualification. Audited the rest of the diff for the same problem: - SettingsChangesHistory.cpp: the new row's note was 603 characters, the longest of all 1230 rows (median 51). Trimmed to 161. The recorded values are untouched, so the compatibility audit path is unaffected. - The test had two comment blocks of four lines, over the three-line-per-block convention. Trimmed to three; the rationale they carried is in the PR body. No behavior change: every one of the 140 query-result lines in the reference is byte-identical. The reference is regenerated only because `-- { echo }` echoes the edited comments. No test asserts this description text; 01324 (descriptions start with a capital letter) still passes. Co-Authored-By: Claude Fable 5 <[email protected]>
The `date_time_output_format` group said the 'simple'-mode limit was documented by the setting, which was true before the previous commit trimmed that sentence out. State the limit in the test instead of citing the description for it. The limit itself is unchanged and pre-existing: `iso` and `unix_timestamp` never consulted the setting (SerializationDateTime64.cpp:51-56), which is what these rows pin. Results are byte-identical. Co-Authored-By: Claude Fable 5 <[email protected]>
|
📊 Cloud Performance Report
This change only widens DateTime64/Time64 trailing-zero output trimming from [0,3,6] to [0,3,6,9], gated behind a setting that defaults to off, plus doc/history/test additions. The single flagged row, clickbench Q15, is a GROUP BY aggregation that never touches that formatting path, and its CPU time is essentially unchanged (2618.5 vs 2623.8 ms) even though wall-clock median moved -11.5%. The deterministic gate already marked it not_sure due to an unstable baseline for this query, which is consistent with the data: the delta is run-to-run variance, not a PR effect. No action needed. clickbenchFlagged queries (1 of 43)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. tpch_adapted_1_official🟢 No significant changes Debug info
|
CI finish ledger — b223f344077f9d0e05fa4a7fc0d7b1b45e5c6b81Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task 174 check-runs, 0 incomplete, The previous ledger covered Session id: cron:our-pr-ci-monitor:20260802-200000 |
| {"input_format_read_datetime_number_as_raw_value", true, false, "From 26.8, an unquoted number for a `DateTime`/`DateTime64` column in the `JSON` and `Values`/`Quoted` paths (and in `JSONExtract` and typed `JSON`) is a Unix timestamp in seconds, consistent with the `Values` format, `CAST` and `toDateTime64`. Set this to `true` (or `SET compatibility = '26.7'`) to restore the pre-26.8 behavior, where a bare unquoted integer fed to a `DateTime64` column was read as the raw scaled value (ticks). The tab-separated, CSV and other escaped/whole-text formats are not governed by this setting."}, | ||
| {"query_plan_short_circuit_constant_false_join", false, true, "New setting to short-circuit a JOIN with a constant-false ON condition so the non-contributing side is not read. previous_value=false so `compatibility` with versions before 26.8 restores the pre-existing behavior (no short-circuit)."}, | ||
| {"distributed_cache_min_inflight_bytes_to_discard_connection_on_seek", 0, 4 * 1024 * 1024, "New setting to drop and reopen a distributed cache connection on a seek when too many in-flight bytes would otherwise be discarded. Defaults to 4 MiB; 0 restores the previous behavior (always reuse the connection via the read range id)."}, | ||
| {"date_time_64_output_format_cut_trailing_zeros_align_to_groups_of_thousands", false, false, "The trimmed output scale widened from [0, 3, 6] to [0, 3, 6, 9], so a `DateTime64` or `Time64` value with scale 7 to 9 no longer loses the digits past the sixth. The default is unchanged; this row records the semantics change for the compatibility audit path."}, |
There was a problem hiding this comment.
@groeneai you just changed the setting description, why do need to add at the settings history?
There was a problem hiding this comment.
You are right, removed in 290109d.
The row was inert on top of being unnecessary: applyCompatibilitySetting skips any entry whose previous_value equals the current value, and this one recorded false -> false because the default did not move. The behaviour change is unconditional code in writeDateTime64FractionalText, not a setting value, so SET compatibility = '26.7' still prints .000000700. Measured, so the row described a restore path that does not exist.
The 24.10 row that introduced the setting stays, so system.settings_changes still lists it and 03999_stateless_settings_history still passes.
| corresponding to 'seconds', 'milliseconds', and 'microseconds')", 0) \ | ||
| Dynamically trim the trailing zeros of `DateTime64` values, rounding the output scale up to the next | ||
| multiple of three that keeps every significant digit: [0, 3, 6, 9]. An all-zero fraction is dropped. | ||
| `Time64` is trimmed only by a direct scalar `toString` or `CAST`, and keeps three digits when all-zero.)", 0) \ |
There was a problem hiding this comment.
@groeneai remove the time64 reference here. This setting only applies to Datetime?
There was a problem hiding this comment.
Removed in 290109d.
To answer the question though: it does apply to Time64, and it already did before this PR. FunctionsConversion.h routes Time64 through the same trimming writer on master, so toString(toTime64('100:00:00.123000', 6)) returns 100:00:00.123 with the setting on there too. What it does not reach is row output and nested values, because SerializationTime64::serializeText discards its FormatSettings.
None of that is new here, and master's description never mentioned Time64 either, so documenting the split belongs in its own change rather than in a description you asked to keep short. The asymmetry stays pinned by the test rows.
…ption Two review asks on the same pair of lines. The 26.8 SettingsChangesHistory.cpp row is inert. applyCompatibilitySetting skips any entry whose previous_value equals its current value, and this row records false -> false because the default did not move. The behaviour change is unconditional code in writeDateTime64FractionalText, not a setting value, so SET compatibility = '26.7' still prints .000000700. The row therefore described a restore path that does not exist. The 24.10 row that introduced the setting stays, so system.settings_changes still lists it and 03999 still passes. The Time64 sentence is dropped from the setting description. The setting does reach Time64 through a scalar toString or CAST, but that routing predates this change (FunctionsConversion.h already called the trimming writer for Time64 on master), so documenting it here mixes an unrelated pre-existing scope note into a description that was asked to stay short. The asymmetry remains pinned by the test rows and explained in the PR description. Co-Authored-By: Claude Fable 5 <[email protected]>
CI finish ledger - 290109dEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
175/175 check-runs completed; The UBSan report is in Session id: cron:our-pr-ci-monitor:20260803-163000 |
|
Fixing PR for the That line read "a fix task is created" because no fixing PR existed at the time. It now does, so the owner column resolves to a PR link. The failure is a trunk bug in Session id: cron:clickhouse-review-slot-11:20260803-223900 |
|
@yariks5s you can review this better than me. Thank you 🙏 |
|
While working in this file I also noticed |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Compile time of recompiled translation units2523 translation units recompiled, 13687 s compile time in total, 2523 of them have a recent master baseline. |
CI finish ledger - a43bfd8Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task
Two independent measurements put the fix outside the tested tree: this run was dispatched at Session id: cron:our-pr-ci-monitor:20260827-043000 |
The only failure on the previous head was `Docs examples`. It tripped on a known-failure entry for `catboostEvaluate`, whose example stopped existing when the CatBoost integration was removed from master. ClickHouse#116566 reverted that removal 13 minutes after the previous run was dispatched, so the merge commit that run tested predated the fix. Co-Authored-By: Claude Opus 5 <[email protected]>
| multiple of three that keeps every significant digit: [0, 3, 6, 9]. An all-zero fraction is dropped.)", 0) \ | ||
| DECLARE(Bool, input_format_read_datetime_number_as_raw_value, false, R"( |
There was a problem hiding this comment.
This description still over-promises the setting's DateTime64 contract. SerializationDateTime64::serializeText only consults date_time_64_output_format_cut_trailing_zeros_align_to_groups_of_thousands in the simple branch; the iso and unix_timestamp branches still ignore it and always print the declared scale. The new test already pins that divergence, so system.settings / help now tell users this trims generic DateTime64 output even in modes where it does not. Please narrow the wording to simple text output, or extend the non-simple branches to honor the setting.
|
Hi — this looks like a backport candidate for |
|
The ask is right, the branch list is not. Measured just now with one The worst row is not the loss of trailing digits, it is a non-zero value printing as zero: Port risk, from a 3-way merge per file against each branch tip with base
@yariks5s could you apply |
|
Hi @yariks5s, gentle ping: could you apply |
Closes: #104997
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixes silent truncation of
DateTime64andTime64values with a scale above 6 whendate_time_64_output_format_cut_trailing_zeros_align_to_groups_of_thousandsis enabled. The printed fraction was capped at 6 digits, so aDateTime64(9)value of00:00:00.000000700rendered as00:00:00.000000even though the stored value was not zero. The documented output range of the setting widens from[0, 3, 6]to[0, 3, 6, 9].Description
The setting rounds the printed fraction up to the next multiple of three that keeps every significant digit, but the round-up carried a hardcoded ceiling of 6, so a significant digit at position 6 or beyond was never written. The symptom is not lost trailing precision but a non-zero fraction rendering as all zeros.
The same expression exists twice, in
writeDateTime64FractionalTextandwriteTime64FractionalText, soTime64has the identical defect. Both are fixed; separate mutations confirm each is load-bearing.The fix removes only the ceiling and keeps the existing round-up rule. It deliberately does not clamp the width to the declared scale, because master already pads past it: a
DateTime64(1)of.7prints.700today. Over all 45 scale and last-significant-digit combinations the new rule matches master in 39 and differs only in the 6 where master loses a digit, always emitting a multiple of three, at most 9. Clamping would break that invariant at scales 1, 2, 4 and 5, so the test pins those rows.The new SQL test fails on master and passes with the fix; a sweep against a baseline with the fix reverted shows 0 regressions over 488 tests. The setting defaults to
false, so nothing changes unless it is enabled. The description is updated, since it documented a hard 6.Three pre-existing scope limits are left unchanged, each pinned by a test row: row output honors the setting only under the default
date_time_output_format = 'simple'; forTime64only a direct scalartoStringorCASTtrims, since a nested value and the row formats reachSerializationTime64::serializeText, which discards itsFormatSettings; and an all-zeroTime64fraction keeps three digits whereDateTime64drops it. Each is its own change.Workflow [PR]
Sync PR [sync-upstream/pr/112854]
Version info
26.9.1.203(included in26.9and later)