Skip to content

fix: support prepared TIME numeric arithmetic - #29036

Merged
XuPeng-SH merged 16 commits into
mainfrom
codex/issue-28963-time-prepared
Sep 19, 2026
Merged

XuPeng-SH merged 16 commits into
mainfrom
codex/issue-28963-time-prepared

Conversation

@XuPeng-SH

@XuPeng-SH XuPeng-SH commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

Fixes #28963

What this PR does / why we need it:

Specialize prepared TIME arithmetic from execution-time source types without losing fractional precision, changing ordinary-query overflow behavior, or leaking one execution's types into the next.

  • Coerce completed integer operands only at the immediate TIME addition/subtraction/modulo boundary, including numeric-consumer fallback rebinding (ABS/SIGN/ELT).
  • Preserve nested signed/unsigned arithmetic, explicit casts, NULL envelopes, and cached-plan reuse; retain the wider multiplication domain.
  • Preserve main's integer-argument, persisted-observer, and IP-expression contracts through the rebase onto 3ac87c30625fc082391e5384a4c94e50a2916cf6.
  • Exercise ordinary versus prepared results/errors over SQL and binary protocols, TIME(0/6), overflow, NULL, and repeated parameter-type changes. No new cluster fixture, retries, or CI timeout changes.

Validation

Current head: 7f27de35bd1cbc35455d2908a036763796313c24. macOS arm64, Go 1.26.4, own-worktree native libraries.

  • Focused planner contract tests: PASS (2.777s).
  • Full pkg/sql/plan: PASS (6.132s).
  • Frontend COM_STMT TIME suites: PASS (1.489s).
  • Embedded ordinary/SQL-prepared/binary-prepared TIME suite: PASS (10.704s after final test-helper lint repair).
  • Prepared pagination source-domain regression: PASS (13.804s).
  • Scoped vet and configured full-package golangci-lint (plan, frontend, embed): PASS, 0 lint issues including unused checks. Molint exits 0; its seven historical diagnostics were reproduced on the frozen main baseline and matched.
  • GPT-6 medium design and independent complete-change final review, including final test delta: PASS, no unresolved material blocker.

The existing BVT SQL/result pair was reviewed but not rerun in an external multi-CN deployment. Fresh Linux CI is pending, not claimed green. Nested ABS(TIME + (? + 1)) PREPARE rejection exists before and after this execute-time repair and is not claimed fixed. No measured performance-parity claim is made; this is bind-time specialization, not a new per-row algorithm.

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

@matrix-meow matrix-meow added the size/M Denotes a PR that changes [100,499] lines label Sep 17, 2026

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Force review for exact head 72f9c2d.

Scope reviewed: all 3 changed files and the prepared TIME arithmetic binding/execution paths.

  • TIME is handled only as a numeric-context hint for prepared-marker inference; the TIME expression itself remains TIME. The hint preserves the operand scale and uses the existing decimal coercion route.
  • At execute time, numeric protocol/source values are rebound through their runtime type instead of being narrowed to the provisional TIME-derived decimal hint. Explicit CAST expressions remain outside this implicit-cast branch.
  • Coverage includes both operand orders for +, -, *, /, %, and DIV; nested parameters; scale 0 and scale 6; explicit string casts; integer and decimal transport; SQL/binary source types; large integer values; and actual expression execution.
  • The implementation keeps non-TIME/date boundaries explicit and does not broaden ordinary unsupported-type behavior.

The PR is mergeable without conflict and only behind the base branch. GitHub checks were unstable during this review, so I am not asserting a green-CI result here.

No evidence-backed P0/P1/P2 blocker found. This is a comment-only review because the PR is authored by the review requester.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

GPT-6 Astra medium independent review for exact head 72f9c2d.

P2 — missing real prepared SQL/protocol regression coverage.

The new execution tests manually construct SourceType and RuntimeType and call planner specialization directly. They do not verify the frontend's real PREPARE/EXECUTE path, reuse of the same prepared statement with changed parameter types, or the binary-protocol branch as it is actually produced by the protocol layer. The PR also does not add a real BVT for these paths.

Please add a real PREPARE/EXECUTE regression, including decimal and NULL values, repeated execution with changed parameter types, result precision/scale assertions, and a binary-protocol entry-point case where applicable. The direct planner tests are useful unit coverage but do not close this end-to-end contract.

Astra medium found no P0/P1 or other P2 issue and no clear flaky path. This is comment-only because the PR is authored by the review requester; code review should not be treated as approve until this validation gap is closed.

@aptend aptend 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.

Reviewed exact head 72f9c2df8a9be5e29ae42f9671aeb0f933c33956 and found a blocking execute-time coercion regression. The focused tests added by this PR pass, but a counterexample covering the other advertised operators fails as described inline. Please preserve the direct TIME coercion contract and add public PREPARE/EXECUTE plus binary-protocol regression coverage before re-review.

return nil, typedErr
}
if typedOK {
boundArgs[i] = typed

@aptend aptend Sep 17, 2026 •

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.

[P1] Preserve the TIME operand when rebinding the parameter

At PREPARE time the TIME operand has already been wrapped in its implicit DECIMAL64(18, scale) cast. This branch replaces only the parameter-side cast and then rebinds the outer operator, so boundArgs now looks like DECIMAL64 + INT64 instead of TIME + INT64. The common-decimal resolver therefore promotes +, -, and % to DECIMAL128(38, scale), while the direct temporal coercion rule keeps those same operations in DECIMAL64(18, scale).

I reproduced this on the exact head through both SQL-style SourceType=int64 and binary-style RuntimeType=int64: select cast('03:04:05.123456' as time(6)) + ? specializes to DECIMAL128, whereas select cast('03:04:05.123456' as time(6)) + cast(10 as signed) is DECIMAL64. The difference is not metadata-only: with 9223372036854775807, the prepared form returns 9223372036854775808.000000, but the ordinary form raises the DECIMAL64 overflow required by the existing TIME/INT64 contract. - and % show the same domain drift.

Please rebind from the original TIME expression, or otherwise preserve its temporal coercion provenance, and add real SQL PREPARE/EXECUTE and binary-protocol regressions for all supported operators, including this INT64 boundary case.

@XuPeng-SH

Copy link
Copy Markdown
Contributor Author

修复并推送 commit b8677e187b 到本 PR。

上一轮 CI 35241600621 的失败不是 base/runner 问题,而是上一版修复仍把 PREPARE 阶段的 DECIMAL64(18) hint 当作 execute-time 严格 NULL envelope,导致嵌套 TIME +/-/% 在 NULL 参数下分别出现 18/39 等错误 precision。

本次修复:

  • 用现有函数 resolver 对嵌套 +/-/%/mod 做 bottom-up envelope 推导;
  • 仅对无类型 NULL 使用 DECIMAL128(38) inference,保留 typed NULL 与显式 CAST;
  • 在 TIME 边界保留完整整数结果的 DECIMAL64(scale 0) 语义;
  • position-scoped specialization 不再把未选中的 marker 当 NULL;
  • 保持 ABS 等 deferred numeric consumer 对运行时 envelope cast 的绑定;
  • 新增显式 SIGNED、常量折叠 typed NULL、未选中 marker 的 focused UT。

本地通过 gofmt、git diff --check、go vet ./pkg/sql/plan、go test -c ./pkg/sql/plan。本机缺少 cgo/libmo.dylib,无法执行 CGo runtime UT,未将编译结果当作运行通过。

新 CI 已启动:

@XuPeng-SH

Copy link
Copy Markdown
Contributor Author

CI repair pushed in 6bd38d9. Root cause: PREPARE-time numericAstTypeScan treated the synthetic DECIMAL64 context from TIME as a genuine numeric operand. For CAST(NULL AS SIGNED) + ?, that widened the nested domain to DECIMAL256; the outer resolver then rejected [TIME, DECIMAL256] before execute-time NULL-envelope rebinding could run. Fix: track TIME-derived decimal as a temporal hint, exclude it from otherwise integral subtrees, and keep weak-decimal literals deferred so explicit outer DECIMAL context is preserved. No resolver-table or test-only bypass. Coverage: retained the original regression; added reverse operand order, non-NULL signed result-4 control, explicit DECIMAL plus weak-literal context and DOUBLE control; extended the existing prepare BVT fixture with metadata checks. Validation: focused prepared-time tests, weak-literal counterexample, and the complete pkg/sql/plan test binary pass locally; go test -c, go vet ./pkg/sql/plan, gofmt, and git diff --check pass. Fresh remote CI/BVT are now running and remain pending.

@XuPeng-SH

Copy link
Copy Markdown
Contributor Author

Rebased onto latest main and pushed 437eee40f8 (force-with-lease).

Adopted the bounded optimization reviewed in this update: compute the temporal peer once per TIME +/-/%/mod expression and avoid repeated numeric-marker tree scans, without widening coercion semantics. Current local validation includes plan/frontend/embed CGo tests, full pkg/sql/plan, and clean multi-CN BVT 62/62 twice. Remote CI is running on the new head.

@XuPeng-SH

Copy link
Copy Markdown
Contributor Author

Updated and rebased onto latest main (c4e81d580b).

Fixes included:

  • resolved the base_binder.go rebase conflict while preserving latest-main prepared numeric peer metadata;
  • preserved TIME provenance at execute-time so prepared TIME(6) + / - / % keeps the ordinary DECIMAL64(18,6) domain and matches INT64 overflow behavior;
  • added a fractional TIME boundary regression for direct planner/PREPARE specialization;
  • retained the existing SQL PREPARE/EXECUTE and COM_STMT protocol coverage.

Validation at exact pushed head ec8b29e95a:

  • planner TestPreparedTimeArithmetic*: PASS;
  • frontend COM_STMT focused tests: PASS;
  • embedded MySQL protocol test: PASS (9.6s);
  • incremental SCA package closure: pkg/embed, pkg/frontend, pkg/sql/plan; gofmt/diff check clean and golangci-lint --new-from-rev origin/main: 0 issues.
  • native CGo dependencies were reused from the clean local artifact root; this is only a local worktree layout detail.

Remote CI is intentionally not awaited or claimed.

@XuPeng-SH

Copy link
Copy Markdown
Contributor Author

Final verification on head 637159a33173990efee1cfb66dd70c4542bef4d1:

  • Rebased onto main at c4e81d580b924b6272f988edbd8025dc5cb90da4.
  • Added the real COM_STMT overflow regression check, including result consumption and rows.Err() validation.
  • Focused planner, frontend, and embed tests pass; the embed MySQL-protocol test passes.
  • Incremental SCA against the merge-base passes: gofmt, git diff --check, go vet, molint (no changed-file findings), and golangci-lint --new-from-rev (0 issues).

Remote CI was not awaited.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Astra medium review on current head bfa4f4a.\n\n发现 1 个 P2,未发现 P0/P1:\n\n[P2] ABS 的 fallback 重绑会撤销 TIME 整数边界转换,改变结果类型及溢出行为。位置:pkg/sql/plan/visit_plan_rule.go:1369、1509。\n\n反例:\nPREPARE p FROM 'SELECT ABS(CAST(''00:00:01'' AS TIME(0)) + ?)';\nSET @v = CAST(9223372036854775807 AS SIGNED);\nEXECUTE p USING @v;\n\n主路径会先把完成的整数操作数转换到 TIME 的 DECIMAL64 边界;ABS 后的 rebindPreparedNumericExprWithBound 从原始表达式重建参数,剥离隐式 cast,直接调用 BindFuncExprImplByPlanExpr,没有再次应用 coercePreparedTemporalIntegerOperand。于是 TIME peer 可能按 DECIMAL64 + INT64 -> DECIMAL128 选型,绕过本应发生的 DECIMAL64 溢出并改变小整数结果 precision。当前新增保护只覆盖 NULL envelope,非 NULL 路径仍受影响。\n\n建议让 ABS 等 fallback 重绑同样遵守 TIME 边界转换,并补充非 NULL、溢出、NULL 后恢复三种执行状态测试。现有 integer-argument 整合回归已有闭合证据;当前 head UT/BVT 已通过。本 PR 为自有 PR,提交 COMMENT。

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Force re-review of exact head 7f27de3.

未确认 P0/P1/P2 代码缺陷。此前 ABS fallback 丢失 TIME 整数边界的问题已修复:递归 numeric fallback 在 overload resolution 前重新对已完成的整数 operand 应用 TIME 边界转换,保留嵌套整数运算、溢出语义、TIME(0/6) scale、显式 CAST 和乘法宽域;SIGN 与完整参数列表的 ELT rebinding 也已检查。

已复查完整 diff、planner/frontend 执行路径、NULL envelope、参数解码、runtime-plan cache 和新增测试,未发现新的 cache contamination、清理、liveness 或无界保留问题。

验证边界:本次为只读 evidence review,未独立重跑测试;检查了已有 focused planner、frontend、embed PASS 日志。未等待 CI 或重跑外部 BVT/multi-CN。嵌套 mixed signed/unsigned 的负值/underflow 组合及真实协议下 SIGN/ELT 覆盖仍较弱,但目前没有可确认的代码反例。PR 已声明的 ABS(TIME + (? + 1)) PREPARE 限制不宣称已修复。

本 PR 为自有 PR,提交 COMMENT。

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Force re-review of exact head 8d1b277.

未确认 P0/P1/P2;prepared TIME arithmetic 当前未发现阻断回归。

7f27de3 -> 8d1b277 是 rebase,range-diff 显示 15 个 PR commits 均 patch-equivalent。我重新核对了当前 planner/frontend/embed 路径及新 base binder 交互。

TIME integer boundary 在普通 specialization 和 ABS/SIGN/ELT fallback rebinding 中仍存在;nested integer arithmetic 先完成再做 TIME boundary cast,TIME(0/6) scale、乘法宽域、显式 CAST 边界、NULL envelope、runtime plan cache/reuse 和 binary execute 错误清理均保持。

当前 head 的 Linux UT、真实 MySQL-protocol TIME suite、SCA 和 CI Required 已通过;PROXY BVT 中 prepared_time_arithmetic.sql 为 62/62 成功,0 ignored/abnormal。

验证缺口:nested unsigned underflow、mixed signed/unsigned extremes、真实 TCP SIGN/ELT error recovery 覆盖仍不完整;这些不是已确认 defect。ABS(TIME + (? + 1)) 的 PREPARE 拒绝仍明确不在本修复范围。

另外,PR 正文仍引用旧 head 7f27de3 并写 fresh Linux CI pending,建议更新为当前 head/CI 状态。本 PR 为自有 PR,提交 COMMENT。

This branch was previously deployed

1 inactive deployment
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/test-ci size/XXL Denotes a PR that changes 2000+ lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Compatibility]: prepared TIME arithmetic rejects numeric parameters

3 participants