Skip to content

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

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

XuPeng-SH merged 5 commits into
matrixorigin:4.0-devfrom
ck89119:cherrypick-24520-to-4.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 #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.

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

No material issues found after reviewing the cherry-pick and the regression coverage.

@XuPeng-SH
XuPeng-SH merged commit b23475b into matrixorigin:4.0-dev May 28, 2026
0 of 3 checks passed
@ck89119
ck89119 deleted the cherrypick-24520-to-4.0-dev branch May 28, 2026 14:14
XuPeng-SH added a commit that referenced this pull request May 28, 2026
## What type of PR is this?

- [ ] API-change
- [x] 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).

---------

Co-authored-by: XuPeng-SH <[email protected]>
Co-authored-by: Claude Opus 4.7 <[email protected]>

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/M Denotes a PR that changes [100,499] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants