Skip to content

Optimize packed DATETIME to Decimal128 conversion - #29581

Merged
XuPeng-SH merged 5 commits into
matrixorigin:mainfrom
LeftHandCold:fix/29580-temporal-decimal-cast
Oct 3, 2026
Merged

XuPeng-SH merged 5 commits into
matrixorigin:mainfrom
LeftHandCold:fix/29580-temporal-decimal-cast

Conversation

@LeftHandCold

Copy link
Copy Markdown
Collaborator

What type of PR is this?

  • BUG
  • Improvement

Which issue(s) this PR fixes:

Fixes #29580.

What this PR does / why we need it:

Replace the successful DATETIME/TIMESTAMP-to-DECIMAL128 per-row format-and-parse conversion with exact arithmetic. The existing session time-zone conversion remains unchanged. This is a 16-line production change; no planner, storage format, cache, background task, or shared state changes.

Root cause and semantic boundaries

The reported BigData profile spends most CPU in packedDatetimeDecimal128: it formats YYYYMMDDHHMMSS.ffffff and parses the resulting string for every row. The packed coefficient fits Decimal128, so we can construct it exactly and use the existing Scale rounding implementation.

Overflow and unsupported internal width/scale/value inputs fall back to the original parser, preserving its exact error. Decimal64 is deliberately not changed: its existing 18-significant-digit parse followed by scale conversion can round twice. Replacing that path would change results.

Validation

Based on main fcd9686e0de5c71b6cd2913f922ad54e7df0a324.

  • 31,570 deterministic comparisons against the original text parser, including zero, boundaries, fractional scales, invalid internal inputs, overflow and exact error strings.
  • Fixed expected-value rounding controls, including unchanged Decimal64 double rounding.
  • Existing NewCast DATETIME/TIMESTAMP/NULL/time-zone tests and complete pkg/sql/plan/function: pass (15.475s).
  • go vet and incremental golangci-lint: pass.
  • Fresh native build with provenance and candidate mo-service: pass.
  • Real isolated SQL service: two existing temporal Golden fixtures replayed twice (84 SQL statements), plus 18 boundary/NULL/zero-date/time-zone/mixed-IN/cleanup assertions. Results matched; the original BigData mixed-IN shape also matched the unchanged base service. All test-owned SQL resources and process cleaned up.

Same-mode microbenchmark medians from 3 runs:

Scale Original parser Arithmetic Speedup
0 414.80 ns 92.27 ns 4.50x
4 429.80 ns 74.83 ns 5.74x
6 387.60 ns 45.86 ns 8.45x
18 394.00 ns 49.35 ns 7.98x

Successful conversion allocations decrease from 40 B / 3 allocations to 0 B / 0 allocations. These are helper benchmarks, not a claim that the full 1B-row BigData query improves by the same factor. No full BigData rerun has been performed.

Risk review

The old parser is the differential oracle and remains the fallback; no changes to Decimal64, SQL coercion rules, TIMESTAMP time-zone conversion, NULL handling, or error ownership. This bounded internal equivalence optimization does not introduce concurrency/lifecycle ownership changes.

@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@XuPeng-SH XuPeng-SH left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Post-main-merge revalidation: current head a962563b5560e0557454f001034db14b37791989 is a pure merge of reviewed commit 50dd48af... with main 6249e488b51f9b6887e8db95ce9bfc92b4eb49e9. Its PR delta is still exactly the same 16 implementation lines and 101 test/benchmark lines. The cast implementation, differential tests and Decimal128/type arithmetic are unchanged by the merge; the incoming main temporal refactor does not replace this conversion owner. Independently verified native input/build provenance and reran the packed differential/rounding tests plus temporal numeric type/NULL boundary tests locally on a962563: PASS. Also reran the packed-value, TIMESTAMP session-timezone and time-numeric arithmetic regressions: PASS. Approval remains valid for this delta. New-head CI run 37107834123 was still running when inspected; the successful CI cited below belongs to the pre-merge head and is not relabeled as new-head CI evidence. No waiting for the new CI is implied.

Deep review of 50dd48a against fcd9686. Decision: APPROVE; no verified blocking finding. No subagents were used.

Design and ownership: this is a bounded equivalence optimization at the existing packed-temporal conversion owner. The 16 production lines reuse Decimal128 arithmetic and Scale rather than adding a second rounding policy, cache or executor. The old parser remains the exact-error fallback. Decimal64 is correctly left unchanged because its intermediate 18-digit rounding is observably different.

Adversarial analysis: checked ZeroDatetime and epoch, fractional half boundaries and calendar-digit carry, precision limits, scale above fractional precision, unsupported internal inputs and overflow. For admitted nonnegative inputs the packed coefficient has at most 21 digits, so Mul128/Add64 cannot overflow; Scale and the strict precision bound match ParseDecimal128. TIMESTAMP session-timezone conversion, NULL/vector append ownership and selection remain in their existing callers. No persistence, data-format, tenant, synchronization or lifecycle contract changes.

Tests and cost: the 31,570 deterministic parser comparisons cover width/scale/value interactions; literal expected-value controls independently pin rounding and the unchanged Decimal64 counterpart. The added benchmark compares the actual helper with the original format/parser path. Implementation +16, tests/benchmark +101, documents +0; the increment buys a concrete hot-path reduction without a framework. Reported 4.50–8.45x helper gains and zero successful-path allocations are not a whole-query speedup claim; full BigData A/B remains unmeasured.

Evidence reused: exact-head CI run 37102603201 is successful (head SHA verified), including native build, UT/race stage, SCA and both multi-CN BVT modes. Downloaded UT checkpoints confirm the resource-heavy race stage terminated with status 0; exact-head coverage also executes the new success/fallback branches. Existing temporal SQL BVT execution is verified in the job report. The attempted local focused run was stopped by the wrapper's native-provenance guard before compilation because the primary checkout has different native inputs; it is not counted as a pass. Author's current-head differential, timezone/NULL and isolated real-SQL evidence is consistent with the traced contract.

Historical comments: no actionable review thread exists; the sole bot comment is an account/subscription notice.

@mergify mergify Bot added the queued label Oct 3, 2026
@mergify

mergify Bot commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-10-03 07:52 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • 🟠 Checks running · in-place
  • 🚫 Left the queue — 2026-10-03 09:00 UTC · at 1cc04805077c19e0ea0166b2f702fe508816107a

This pull request spent 1 hour 8 minutes 17 seconds in the queue, with no time running CI.

Reason

The pull request #29581 has been manually updated

Waiting for any of
  • check-neutral = CI Required
  • check-skipped = CI Required
  • check-success = CI Required
All conditions

Requeued — the merge queue status continues in this comment ↓.

@LeftHandCold

Copy link
Copy Markdown
Collaborator Author

CI failure root cause and minimal correction:

  • The failed SCA and UT coverage jobs on a962563b5560e0557454f001034db14b37791989 both report prepared_explain_test.go:272: undefined: getSqlForCheckRoleHasTableLevelPrivilege.
  • Main's refactor(sql): consolidate active owners and retire obsolete paths #29574 removed that wrapper but missed this test caller. The exact base 6249e488b51f9b6887e8db95ce9bfc92b4eb49e9 contains the same stale reference; it is not a DATETIME conversion failure. The coverage aggregator did not calculate a coverage verdict because a producer failed to compile.
  • The correction changes only that test call to the existing getSqlForCheckRoleHasTableLevelPrivilegeWithObjType(..., objectTypeTable, ...), exactly matching the removed wrapper's behavior. It does not restore dead production code or skip authorization assertions.

Validation: reproduced the undefined symbol before the patch; the prepared authorization allow/revoke/regrant tests, complete frontend package, complete function package, and temporal conversion tests pass after it. The original DATETIME optimization and its parser-equivalence tests are unchanged.

While validating, the same one-line fix was independently pushed as 4d69221d143f787182dab71094ce2c33ee787ed0. I retained that contribution rather than overwriting it. Its complete tree is identical to my locally verified 6bf6d37201: frontend 22.913s and function 11.283s, exit 0. This also preserves the subsequent main sync (#29528). New-head CI has started; that is not yet a claim that all remote checks passed.

The full repository make static-check-analysis subsequently passed at exact head 4d69221d143f787182dab71094ce2c33ee787ed0: exit 0, zero lint issues, Go 1.26.4 Linux amd64, pinned molint and golangci-lint 2.6.2. This includes the full test dependency graph and full-package analysis, not only changed-line lint. New CI: https://github.com/matrixorigin/matrixone/actions/runs/37111575599 ; remote arm64/BVT/coverage results remain pending.

@mergify

mergify Bot commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-10-03 10:13 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • 🟠 Checks running · in-place
  • 🚫 Left the queue — 2026-10-03 10:23 UTC · at 68ae74ec8c5d44320eed16bdb66f6dc78aa4a6da

This pull request spent 10 minutes 7 seconds in the queue, with no time running CI.

Reason

Pull request #29581 has been dequeued

Pull request from fork cannot be queued. This pull request comes from a fork, and Mergify needs the author's permission to update its branch.

The author needs to enable "Allow edits from maintainers" on this pull request.

Hint

You should look at the reason for the failure and decide if the pull request needs to be fixed or if you want to requeue it.
If you do update this pull request, it will automatically be requeued once the queue conditions match again.
If you think this was a flaky issue instead, you can requeue the pull request, without updating it, by posting a @mergifyio queue comment.

Waiting for any of
  • check-neutral = CI Required
  • check-skipped = CI Required
  • check-success = CI Required
All conditions

Tick the box to put this pull request back in the merge queue (same as @mergifyio queue).

  • Requeue this pull request

This branch was previously deployed

1 inactive deployment
ci — 68ae74ec Deployed Oct 3, 2026 by XuPeng-SH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Something isn't working kind/enhancement size/M Denotes a PR that changes [100,499] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Performance]: DATETIME-to-DECIMAL predicates repeatedly format and parse each row

3 participants