Repository navigation
perf(types): format dates, times and decimals without fmt - #33345
Conversation
Decimal64.Format and Decimal128.Format built their string one digit at a
time with fmt.Sprintf("%c") and prepended it, allocating per digit. They
now write the digits into a stack buffer from the right, with the same
control flow, as Decimal256.Format already did. Decimal128 takes its
digits by exact 128-bit division by 10 (math/bits.Div64) instead of a
rounding Div128 followed by a correction.
Datetime.String2, Date.String, Time.String2 and Timestamp.String2 write
their digits directly. The fmt code stays as the fallback for values the
fast path does not cover: years outside 0..9999, negative datetimes,
TIME hours of three or more digits, and a Timestamp scale above 6 (which
still panics as before).
BenchmarkFormat (median of 5):
Decimal64.Format scale 0 766 -> 26 ns -97% 12 -> 1 allocs
Decimal64.Format scale 2 406 -> 19 ns -95% 7 -> 1
Decimal128.Format scale 4 839 -> 42 ns -95% 15 -> 1
Datetime.String2 scale 6 329 -> 29 ns -91% 5 -> 1
Timestamp.String2 scale 6 338 -> 32 ns -90% 5 -> 1
Time.String2 scale 3 205 -> 20 ns -90% 3 -> 1
Date.String 123 -> 16 ns -87% 2 -> 1
End to end, BenchmarkColumnSlicesRows (MySQL result rows, 1024 x 8 mixed
columns): text 105 -> 47 ns/cell (-55%), binary 121 -> 60 ns/cell (-50%),
~4,650 fewer allocations per batch; an all-int64 batch is unchanged.
The fmt-based implementations are kept in format_legacy_test.go;
TestFormatMatchesLegacy requires identical output, including panics, for
edge values and 20,000 random values per type, scales -3..40 and four
time zones.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
|
This pull request is merged. |
|
mo-bot review · Commit: Review completed. Read the assessment. |
XuPeng-SH
left a comment
There was a problem hiding this comment.
mo-bot review · mo-bot-local · review
Commit: e2f9653f7f1b164d27551606cf5a7ac7f33122d7
Assessment: request_changes
The direct digit writers reuse the existing lookup table and substantially improve the measured in-range formatter costs. The date formatter needs an additional boundary guard to preserve behavior for raw dates that reach existing Parquet conversion paths.
Model: gpt-6.1-terra / max
Session: 01a219dc-9732-7b72-bb9a-a6303daa1919 · slot 0 · generation 19
- P2
pkg/container/types/date.go:443: A Parquet DATE value of -719164 days since the Unix epoch is converted to a VARCHAR or JSON value.
Impact: The conversion now panics with an index-out-of-range error instead of returning the string produced by the previous formatter, interrupting the affected query or load.
Evidence: The value maps to Date(-2), whose Calendar(true) result is (1, 2, 224). The fast path checks only the year before using day 224 to index the 100-entry digit table. The Parquet conversion at pkg/sql/colexec/external/parquet.go:4615 passes raw dates to String(). A focused conversion test passed on the merge base and failed on the PR head, while an ordinary-date control passed on both. Route out-of-range month or day components through the existing slow formatter, and cover this boundary.
Validation:
-
Private /tmp and loopback preflight passed; receipt checks/1795737165177704805-5.json.
-
The full pkg/container/types test suite passed, including TestFormatMatchesLegacy; receipt checks/1795737361387199642-5.json and types-tests.log.
-
The six legacy formatter test bodies match the frozen merge-base implementations under AST formatting; receipt checks/1795737490367591318-6.json.
-
Three bounded BenchmarkFormat repetitions showed substantial in-range component improvements; receipt checks/1795737460549016591-5.json and format-benchmarks.log.
-
The focused Parquet conversion probe failed on the candidate and passed on the merge base; receipts checks/1795737654219096885-7.json and checks/1795737733455858830-5.json.
Limitations:
-
The Parquet probe exercised the conversion helper used by the mapper, rather than a running SQL service.
-
The frontend end-to-end benchmark reported in the PR was not independently rerun.
What type of PR is this?
Which issue(s) this PR fixes:
issue #10181 (found while profiling step 6b; not part of the type-dispatch stack)
What this PR does / why we need it:
This PR is independent: it's based on
main, not on the #10181 stack.A CPU profile of MySQL result-row encoding (from #33344) showed
fmt.SprintfinsideDatetime.String2andDecimal64.Formattaking about 15% of the time for mixed columns. This PR removesfmtfrom the date, time and decimal formatters.Decimal64.FormatandDecimal128.Formatbuilt their string one digit at a time withfmt.Sprintf("%c")and prepended it, allocating per digit. They now write digits into a stack buffer from the right, with the same control flow, asDecimal256.Formatalready did.Decimal128takes its digits by exact 128-bit division by 10 (math/bits.Div64) instead of a roundingDiv128plus a correction.Datetime.String2,Date.String,Time.String2andTimestamp.String2write their digits directly. Thefmtcode stays as the fallback for values the fast path doesn't cover:Timestampscale above 6, which still panics as before.Speed
BenchmarkFormat, median of 5:Decimal64.Format, scale 0Decimal64.Format, scale 2Decimal128.Format, scale 4Datetime.String2, scale 6Timestamp.String2, scale 6Time.String2, scale 3Date.StringEnd to end,
BenchmarkColumnSlicesRowsencodes MySQL result rows of 1024 × 8 mixed columns (ints, decimal, varchar, date, datetime):The benchmark file is identical to the one in #33344, so the two PRs don't conflict.
Correctness
fmt-based implementations are kept informat_legacy_test.go.TestFormatMatchesLegacyrequires identical output, including panics. It covers edge values and 20,000 random values per type, scales −3..40 and four time zones: negative and out-of-range datetimes,MinInt64, three-digit TIME hours, the decimal sign bit, and full 128-bit decimals../pkg/container/types/,./pkg/container/vector/, all of./pkg/frontend/...and all of./pkg/sql/plan/function/...pass.🤖 Generated with Claude Code