Skip to content

fix(plan): sanitize non-finite plan stats (cherry-pick #24672) - #24682

Merged
XuPeng-SH merged 2 commits into
matrixorigin:3.0-devfrom
ck89119:cherrypick-24520-to-3.0-dev
May 28, 2026
Merged

XuPeng-SH merged 2 commits into
matrixorigin:3.0-devfrom
ck89119:cherrypick-24520-to-3.0-dev

Conversation

@ck89119

@ck89119 ck89119 commented May 28, 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 #24520

What this PR does / why we need it:

Cherry-pick PR #24672 to 3.0-dev.

3.0-dev is affected by the same defect: optimizer stats can produce non-finite (+Inf/NaN) selectivity via unchecked divisions in pkg/sql/plan/stats.go, which then breaks plan JSON marshaling on CN (serialize plan to json error: json: unsupported value: +Inf) and drops client connections under load.

This PR prevents non-finite planner statistics from leaking into plan JSON and cost estimation paths by:

  • guarding stats ratio calculations against division by zero and non-finite results;
  • sanitizing non-finite float values before plan JSON marshaling;
  • clamping expression and join selectivity to the valid [0, 1] range;
  • clamping ANTI JOIN right-side selectivity before computing output rows;
  • adding regression coverage for zero stats, year(...) equality selectivity, NOT/JOIN propagation, ANTI JOIN, and JSON marshal sanitization.

Conflicts were resolved to match 3.0-dev (e.g. w.Stats accessor naming, pb/plan import alias, and dropping the FUNCTION_SCAN IndexReaderParam path absent in 3.0-dev).

- [ ] API-change
- [x] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

issue matrixorigin#24520

Cherry-pick PR matrixorigin#24643 to 4.0-dev.

This PR prevents non-finite planner statistics from leaking into plan
JSON and cost estimation paths by:

- guarding stats ratio calculations against division by zero and
non-finite results;
- sanitizing non-finite float values before plan JSON marshaling;
- clamping expression and join selectivity to the valid [0, 1] range;
- clamping ANTI JOIN right-side selectivity before computing output
rows;
- adding regression coverage for zero stats, year(...) equality
selectivity, NOT/JOIN propagation, ANTI JOIN, and JSON marshal
sanitization.

---------

Co-authored-by: XuPeng-SH <[email protected]>
@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 →

…py PhyPlan before sanitize

- estimateExprSelectivity: clamp the cached and prefix_eq early returns so a
  non-finite cached value or a small-table (TableCnt<100) prefix_eq cannot leak
  selectivity > 1 past the final clamp.
- calcSelectivityByMinMax/ForDecimal: a zero-width range (max==min) with a
  non-matching bound divides 0/0 -> NaN; return the low selectivity instead of
  leaking NaN that the caller would clamp to full-table selectivity.
- marshalPlanHandler: deep-copy PhyPlan before Marshal so the in-place
  non-finite sanitizer cannot mutate the caller's live physical plan through the
  shared OperatorStats.BackgroundQueries (*plan.Query) pointers.
- tests: assert plan JSON is parseable/non-empty with non-finite stats replaced,
  that Marshal does not mutate a shared PhyPlan, plus prefix_eq small-table,
  zero-width range, and concrete ANTI JOIN outcnt coverage.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
@matrix-meow matrix-meow added size/L Denotes a PR that changes [500,999] lines and removed size/M Denotes a PR that changes [100,499] lines labels May 28, 2026
@XuPeng-SH
XuPeng-SH merged commit 8f8ef61 into matrixorigin:3.0-dev May 28, 2026
23 checks passed
@ck89119
ck89119 deleted the cherrypick-24520-to-3.0-dev branch May 29, 2026 03:17

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 size/L Denotes a PR that changes [500,999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants