Skip to content

fix: accept fractional prepared numeric overloads - #27556

Merged
XuPeng-SH merged 22 commits into
matrixorigin:mainfrom
daviszhen:fix-issue-27294-prepared-overloads
Aug 28, 2026
Merged

XuPeng-SH merged 22 commits into
matrixorigin:mainfrom
daviszhen:fix-issue-27294-prepared-overloads

Conversation

@daviszhen

@daviszhen daviszhen commented Aug 24, 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:

issue #27294

What this PR does / why we need it:

Server-side prepared ABS(?) and SLEEP(?) were bound to the first integer overload while the parameter marker was still represented as text. Fractional and decimal-looking values then failed during the cached implicit integer cast.

This change gives only parameter-bearing ABS/SLEEP expressions a stable DOUBLE binding domain during PREPARE. It leaves ordinary column calls and other prepared plans on their existing paths, so execution can reuse the cached compile without the broad per-execution plan specialization that caused the TPCC regression.

Regression coverage:

  • planner tests for direct and nested parameter expressions, plus an ordinary integer-column control;
  • real COM_STMT_PREPARE/COM_STMT_EXECUTE coverage for integer, float, textual fractional values and statement reuse.

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

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

Forcing prepared ABS to FLOAT64 is not semantically safe. A binary-protocol INT64 value of -9007199254740993 must produce the exact integer 9007199254740993; the new cast rounds it to 9007199254740992. It also changes the integer-domain behavior at MinInt64.

The recursive containsPreparedParamExpr makes the issue broader: abs(case when ? then bigint_col else bigint_col end) converts ordinary BIGINT column values to FLOAT64 even though the parameter only controls the branch.

SLEEP can reasonably have a stable DOUBLE input, but polymorphic ABS must preserve integer, decimal, and floating domains, likely through runtime type-category specialization rather than one approximate universal type. Please add binary-protocol regressions beyond 2^53 and for a parameter nested in a CASE around BIGINT data.

@daviszhen

Copy link
Copy Markdown
Contributor Author

Updated exact head: 1d9b1ae, with latest main d511de3 merged.

Closed the two latest P1 blockers:

  • all value-bearing parameter positions inside deferred ABS expressions are collected and rebound together in one pass; CASE and IF condition-only markers stay excluded, while mixed numeric categories and flattened scalar-subquery sources are covered;
  • every runtime parameter now retains provenance before overload-specialized plans enter the semantic-category cache, so unrelated direct, nested, and Decimal256 values are restored on cache hits.

New real COM_STMT controls cover multi-marker arithmetic, mixed INT64 and DECIMAL metadata/value, IF, CASE, single and dual scalar subqueries, v29 direct and nested stale-value reproduction, and v30 Decimal256 same-key value changes.

Fresh validation after merging main:

  • full go test ./pkg/sql/plan ./pkg/frontend -count=1
  • go test ./pkg/tests/issues -run TestIssue27294PreparedNumericOverloads -count=1
  • focused planner/frontend race tests
  • go vet ./pkg/sql/plan ./pkg/frontend ./pkg/tests/issues
  • git diff --check

Independent local review: code-review APPROVE; architecture CLEAR.

@aptend @XuPeng-SH please re-review the updated exact head.

@daviszhen

Copy link
Copy Markdown
Contributor Author

Final exact head after merging current main 424fe3d: 78b4f61. The reviewer fixes are unchanged by the merge. Focused planner/frontend tests and the real TestIssue27294PreparedNumericOverloads COM_STMT regression passed again after this merge.

…pared-overloads

# Conflicts:
#	pkg/frontend/computation_wrapper.go
#	pkg/frontend/computation_wrapper_test.go
#	pkg/sql/plan/utils.go
#	pkg/sql/plan/visit_plan_rule.go
@daviszhen

Copy link
Copy Markdown
Contributor Author

已推送合并提交 0f6645c527,同步最新 main(8b0a5ff873)并解决 computation_wrapper.go、utils.go、visit_plan_rule.go 等冲突。旧一轮唯一失败的 SCA annotation 是 runner 与 GitHub 失联,并非代码诊断;本地使用 CI 同等参数执行 go vet -mod=readonly -tags matrixone_test -unsafeptr=false ./pkg/... 通过。新一轮 SCA/UT 已重新触发,当前运行中。

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

Deep-reviewed exact head 0f6645c52754824a4e4d0fdc1f1cb1df48cf80e6. The two correctness blockers from the previous review are closed: all value-source parameters are rebound, and every materialized parameter is restored before a same-category cache hit. The real COM_STMT regression passes, including multi-marker exactness, DECIMAL256, scalar subqueries, v29/v30 cache isolation, MinInt64, and UINT64 boundaries.

Two blocking performance-contract problems remain.

  1. [P1 performance] Prepared-ABS provenance inflates every plan.Expr, including unrelated ordinary queries.

    Five scalar fields were added directly to the globally hot plan.Expr protobuf. On this exact head, unsafe.Sizeof(plan.Expr{}) is 184 bytes versus 160 bytes on base 8b0a5ff873. That crosses the Go allocator size class from 160 to 192 bytes: approximately 20% more heap per independently allocated expression. Planner construction, optimizer rewrites, DeepCopyExpr, RPC plan materialization, and every non-prepared SQL statement pay this cost, even though only prepared ABS needs the metadata.

    The PR provides no allocation/plan-build evidence for this global footprint change. Please keep the rare provenance sparse, for example as one optional nested metadata pointer or plan-level sidecar, and show representative planner/deep-copy allocation evidence. The wire fields may still be explicit; they do not need five always-resident scalars on every expression.

  2. [P2 performance/selection] The explicit DOUBLE-cast fast path is still classified as deferred specialization.

    The issue documents ABS(CAST(? AS DOUBLE)) as an already-working control, and the implementation says explicit user DOUBLE casts should remain ordinary DOUBLE expressions. However, through the real parser:

    prepare s from 'select abs(cast(? as double))'
    PreparedPlanNumericFallbackParamPositions(...) == [0]
    

    A temporary planner assertion expecting no fallback fails exactly with [0]. The helper UT constructs tree.CastExpr directly, but the parsed SQL shape is not recognized by containsExplicitFloatCast. Consequently this fixed-domain query allocates a runtime plan/compile unnecessarily; alternating integer/float protocol descriptors change preparedRuntimeSemanticKey and repeatedly deep-copy/recompile although the explicit cast fixes the output and overload to DOUBLE.

    Please derive the explicit-cast boundary from the actual parsed/bound shape and add a public parser-path plus compile-count/cache regression proving that explicit-cast executions stay on the original cached compile across parameter categories.

Evidence:

  • exact-head GitHub CI: green;
  • focused real COM_STMT issue regression: PASS;
  • temporary parser-path metadata counterexample: FAIL as described, then removed;
  • base/head plan.Expr size checks: 160/184 bytes, then removed;
  • no new goroutine, wait-chain, resource leak, or log-storm path found.

…pared-overloads

# Conflicts:
#	pkg/frontend/computation_wrapper.go
#	pkg/frontend/computation_wrapper_test.go
#	pkg/frontend/mysql_cmd_executor.go
#	pkg/frontend/types.go
#	pkg/pb/plan/plan.pb.go
@daviszhen

Copy link
Copy Markdown
Contributor Author

Addressed the latest review at exact head b958ced and merged upstream/main e5f26b6.\n\n1. Sparse prepared-ABS provenance\n- Replaced the five always-resident plan.Expr scalar fields with one optional PreparedNumericMetadata pointer.\n- Ordinary expressions leave the pointer nil; unsafe.Sizeof(plan.Expr{}) is now 168 bytes instead of 184 bytes on the reviewed head.\n- DeepCopyExpr copies the metadata only when present.\n- Added a committed benchmark. On the merged head (amd64, count=3), ordinary deep copy is 246-251 ns/op, 696 B/op, 8 allocs/op; the rare metadata-bearing path is 274-275 ns/op, 760 B/op, 9 allocs/op.\n- Proto round-trip, deep-copy, flattening, parameter-order reset, and scalar-subquery source provenance tests cover the sparse representation.\n\n2. Explicit DOUBLE-cast fast path\n- FLOAT/DOUBLE user casts now use the dedicated explicit-cast overload, preserving the parsed semantic boundary in the bound plan.\n- Deferred-ABS discovery and runtime-specialization admission now require the explicit fallback marker and exclude the explicit cast boundary.\n- Added parser-to-plan coverage proving ABS(CAST(? AS DOUBLE)) has no fallback positions and requires no runtime specialization.\n- Added frontend cache coverage that executes the same prepared statement first with INT64 and then DOUBLE descriptors and proves both executions reuse the original plan and cached compile without creating a runtime specialization entry.\n\nConflict integration\n- Integrated prepared-ABS specialization with main's new direct-result specialization path: numeric-prefix/ABS categories use the full semantic key, direct-result-only executions use the direct-result key, and parameter provenance is retained when the paths overlap.\n- GitHub now reports the PR mergeable; CI has restarted on the new head.\n\nLocal validation:\n- full go test ./pkg/sql/plan ./pkg/frontend\n- real TestIssue27294PreparedNumericOverloads\n- go vet ./pkg/sql/plan ./pkg/frontend ./pkg/tests/issues\n- git diff --check\n- benchmark above, count=3\n\nPlease re-review exact head b958ced.

@aunjgr aunjgr 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 b958ced6ffde0d694f276aa42187ea4cffc0b756 against its current merge base.

The prior global Expr-footprint and direct ABS(CAST(? AS DOUBLE)) findings are materially addressed: prepared metadata is now one optional pointer (ordinary Expr size 168 bytes), and the real parser path preserves a top-level explicit DOUBLE cast without deferred specialization.

One blocking correctness gap remains in the new detector. containsExplicitFloatCast recursively treats a DOUBLE cast anywhere in CASE/IF—including control-only conditions—as fixing the ABS argument's value domain. For example:

ABS(CASE WHEN CAST(? AS DOUBLE) > 0 THEN ? ELSE ? END)

The condition cast does not determine the type/exactness of the selected value parameters, but it makes hasExplicitFloatCast true, so markPreparedNumericFallback is skipped. A selected INT64 value such as -9007199254740993 therefore stays on the provisional DOUBLE path and can be rounded. The same problem applies to IF/control-only subtrees and unrelated nested functions.

Make the explicit-float test value-domain aware—only a cast that fixes the produced ABS argument should suppress fallback—and add real parser/COM_STMT controls with a control-only DOUBLE cast plus wide integer/DECIMAL value branches. Exact-head CI is green but does not cover this separation.

@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 b958ced6ffde0d694f276aa42187ea4cffc0b756 against merge base e5f26b66195373f9bff512ac9f428788d1c07e8c.

The prior wide-integer/DECIMAL, scalar-subquery, multi-marker, stale-cache, global Expr-footprint, and top-level explicit-DOUBLE findings are materially closed. I also exercised the control-only cast concern independently: the parser-produced CASE WHEN CAST(? AS DOUBLE) > 0 THEN ? ELSE ? END plan retains fallback positions [1,2]; the direct IF(CAST(? AS DOUBLE), ?, ?) shape has no bounded ABS fallback metadata, but remains on the generic runtime-specialization path and a real COM_STMT execution returned BIGINT 9007199254740993 exactly. I therefore could not reproduce a correctness failure from that shape.

Exact-head verification passed: full pkg/sql/plan and pkg/frontend, focused race coverage for prepared/runtime-specialization paths, and the embedded COM_STMT regression TestIssue27294PreparedNumericOverloads.

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

Reviewed exact head b958ced. Rechecked prepared numeric overload selection for fractional values across parameter binding, decimal/float coercion, result metadata, NULL/error boundaries, and direct-vs-prepared compatibility. The revised dispatch accepts the intended fractional prepared inputs without broadening unrelated overloads or adding per-row work; exact-head CI is green. No blocking correctness, compatibility, unhappy-path, or performance issue remains.

@daviszhen

Copy link
Copy Markdown
Contributor Author

Updated exact head: 7b34b2633e11d33ed4c57864829b32bc1c28b93e.

The newest exact-head review on b958ced6ff independently verified the control-only CAST/CASE/IF concern and approved the implementation: fallback positions remain on the value branches, while direct IF shapes continue through the generic runtime-specialization path without losing wide-integer exactness. No additional correctness defect was identified by that review.

I have now:

  • merged current upstream/main 02af906c36 without conflicts;
  • retained the prepared numeric specialization fixes unchanged;
  • resolved the two already-addressed legacy review threads;
  • rerun the owning-package and real protocol regressions.

Fresh validation on the new head:

  • go test ./pkg/sql/plan ./pkg/frontend -count=1
  • go test ./pkg/tests/issues -run TestIssue27294PreparedNumericOverloads -count=1
  • go vet ./pkg/sql/plan ./pkg/frontend ./pkg/tests/issues
  • git diff --check upstream/main...HEAD

All passed. @aunjgr please re-review the updated exact head so the superseded CHANGES_REQUESTED state can be cleared.

@aunjgr aunjgr 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 7b34b2633e6af14bca4f238012039c3031a8854e against merge base 02af906c367b3ef7e530336651ba033f8c14861c and newest main@958a4ceb9e73c5e70928304996873e089279b667.

The previously demonstrated blockers are closed: all numeric value-source parameters are represented and rebound, every materialized parameter is restored before cache publication/reuse, same-category executions cannot retain prior values, and protocol-generation/category changes replace the bounded runtime specialization entry. Prepared metadata is sparse on one optional pointer, avoiding the prior global Expr size-class inflation, while real parser top-level DOUBLE casts remain fixed-domain and skip unnecessary specialization.

I also rechecked the control-only float-cast concern. Although the AST detector is intentionally broad, CASE/IF value parameters that need runtime typing remain covered by the generic prepared-runtime specialization when the ABS-specific fallback marker is absent; they are not stranded as first-execution DOUBLE literals. The real COM_STMT matrix covers multi-marker INT64/UINT64/DECIMAL256 exactness, selected CASE/IF branches, scalar subqueries, same-category value changes, NULL/error boundaries, and v29/v30 cache isolation. The increment from the last semantic head is only a current-main merge, and exact-head CI is fully green.

No blocking correctness, result-metadata, cache-generation, compatibility, ownership, unhappy-path, or performance issue remains.

@mergify

mergify Bot commented Aug 28, 2026 •

Copy link
Copy Markdown
Contributor

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

@mergify

mergify Bot commented Aug 28, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-08-28 13:41 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • 🟠 Checks running · in-place
  • 🚫 Left the queue — 2026-08-28 13:57 UTC · at 62c583cc3e791aeb3514af281a0a8da3f8c3f9a4

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

Waiting for
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone CI / SCA Test on Linux/arm64
    • check-skipped = Matrixone CI / SCA Test on Linux/arm64
    • check-success = Matrixone CI / SCA Test on Linux/arm64
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
    • check-success = Matrixone Utils CI / Coverage
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-skipped = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-success = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
    • check-success = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
All conditions
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone CI / SCA Test on Linux/arm64
    • check-skipped = Matrixone CI / SCA Test on Linux/arm64
    • check-success = Matrixone CI / SCA Test on Linux/arm64
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
    • check-success = Matrixone Utils CI / Coverage
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-skipped = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-success = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
    • check-success = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
  • #review-threads-unresolved = 0 [🛡 GitHub branch protection]
  • github-review-approved [🛡 GitHub branch protection]

Reason

Pull request #27556 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.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/enhancement size/XXL Denotes a PR that changes 2000+ lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants