Skip to content

perf(function): avoid EXP overflow error allocations - #28592

Merged
XuPeng-SH merged 3 commits into
matrixorigin:mainfrom
daviszhen:fix/issue-28560-sum-exp
Sep 10, 2026
Merged

XuPeng-SH merged 3 commits into
matrixorigin:mainfrom
daviszhen:fix/issue-28560-sum-exp

Conversation

@daviszhen

Copy link
Copy Markdown
Contributor

What type of PR is this?

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

Which issue(s) this PR fixes:

Fixes #28560

What this PR does / why we need it:

EXP overflow is a row-local NULL, but the current evaluator obtains that result by constructing a formatted moerr for every overflowing row and immediately discarding it. On the 1-billion-row SUM(EXP(...)) workload this turns expected data-domain rejection into billions of error allocations, stack captures, formatting operations, and GC work.

This change adds an error-free unary NULL-check path and makes EXP use math.Exp plus an infinity check. SQL behavior is unchanged: finite results, underflow, input NULLs, constant inputs, and selection masks retain their existing semantics; overflow still produces NULL. Existing null-on-error callers keep their current error callback contract.

A local alloc profile of the unfixed 8192-row all-overflow benchmark attributed 95.5% of benchmark allocation space cumulatively to momath.Exp / NewOutOfRangeNoCtxf; stack capture was 45.7% and fmt.Sprintf was 22.3% of total allocation space. After the change the CPU profile is dominated by the intended math.Exp and NULL bitmap work.

Benchmark on linux/amd64 (8192 overflowing values per operation):

before: 14.54-16.95 ms/op   4,655,216-4,655,552 B/op   81,928-81,929 allocs/op
after:   0.103-0.121 ms/op      2,040-2,201 B/op        8 allocs/op

The test suite includes an allocation-scaling regression comparing one overflow row with an 8192-row overflow batch.

Validation:

  • go test ./pkg/sql/plan/function -count=1
  • focused EXP/COT tests with -count=5
  • focused EXP/COT tests with -race
  • go vet ./pkg/sql/plan/function
  • git diff --check

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

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

Deep review completed at exact head 1e7a6b886d993eecd5d7b5ef48ab2de417fb43cc. I audited the full three-file diff, the referenced #28560 failure, EXP’s prior momath.Exp semantics, the generic null-on-error refactor, constant/vector/NULL/selection paths, result reuse, and the allocation regression. No blocking issue found. The new predicate helper preserves the old callback contract exactly, while EXP replaces only the per-row formatted error with the same math.Exp + infinity decision; finite values, NaN, ±Inf, underflow, input NULLs, constants, and masks retain their behavior. Validation on the exact head: full pkg/sql/plan/function tests pass; focused EXP/COT tests pass five repetitions and under -race; the overflow benchmark is stable at ~78.6–78.8 µs/op, 2040 B/op, 8 allocs/op across three runs; git diff --check passes. GitHub required checks are complete without failures (Darwin/compatibility jobs were skipped by CI routing).

@mergify

mergify Bot commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

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

@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 review completed at exact head 1e7a6b8.

No confirmed blocker found. The EXP change preserves finite values, overflow-to-NULL, underflow, NaN/Inf behavior, NULLs, constants, selected rows, result reset, and cleanup ownership while removing discarded error/formatting allocations.

Nonblocking test/benchmark cleanup issue: pkg/sql/plan/function/func_builtin_test.go:2755 and :2788 create process memory pools registered in globalPools but never delete them. Repeated test/benchmark invocations retain pool objects until process exit. Register pool deletion after vector/result cleanup.

Validation: exact-head CI run 34436798813 passed Ubuntu UT, SCA, coverage, and active BVT; func_math_exp.test recorded 39/39 and func_math_power.test 101/101. Not independently run: fresh local race/vet, live SQL, fault injection, cancellation measurement, same-instance BVT repetition, mixed-version/downgrade/restore, and benchmarks.

@mergify mergify Bot added the queued label Sep 10, 2026
@mergify

mergify Bot commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-09-10 05:56 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • 🟠 Checks running · in-place
  • 🚫 Left the queue — 2026-09-10 06:07 UTC · at 7f107cf375f5ed0a77391ae4858c94628a2541b2

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

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

Reason

Pull request #28592 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

@XuPeng-SH
XuPeng-SH merged commit e395940 into matrixorigin:main Sep 10, 2026
27 of 28 checks passed
@mergify mergify Bot added dequeued and removed queued labels Sep 10, 2026
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
)

## What type of PR is this?

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

## Which issue(s) this PR fixes:

Fixes matrixorigin#28560

## What this PR does / why we need it:

`EXP` overflow is a row-local `NULL`, but the current evaluator obtains
that result by constructing a formatted `moerr` for every overflowing
row and immediately discarding it. On the 1-billion-row `SUM(EXP(...))`
workload this turns expected data-domain rejection into billions of
error allocations, stack captures, formatting operations, and GC work.

This change adds an error-free unary NULL-check path and makes `EXP` use
`math.Exp` plus an infinity check. SQL behavior is unchanged: finite
results, underflow, input NULLs, constant inputs, and selection masks
retain their existing semantics; overflow still produces NULL. Existing
null-on-error callers keep their current error callback contract.

A local alloc profile of the unfixed 8192-row all-overflow benchmark
attributed 95.5% of benchmark allocation space cumulatively to
`momath.Exp` / `NewOutOfRangeNoCtxf`; stack capture was 45.7% and
`fmt.Sprintf` was 22.3% of total allocation space. After the change the
CPU profile is dominated by the intended `math.Exp` and NULL bitmap
work.

Benchmark on linux/amd64 (8192 overflowing values per operation):

```text
before: 14.54-16.95 ms/op   4,655,216-4,655,552 B/op   81,928-81,929 allocs/op
after:   0.103-0.121 ms/op      2,040-2,201 B/op        8 allocs/op
```

The test suite includes an allocation-scaling regression comparing one
overflow row with an 8192-row overflow batch.

Validation:

- `go test ./pkg/sql/plan/function -count=1`
- focused EXP/COT tests with `-count=5`
- focused EXP/COT tests with `-race`
- `go vet ./pkg/sql/plan/function`
- `git diff --check`

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
)

## What type of PR is this?

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

## Which issue(s) this PR fixes:

Fixes matrixorigin#28560

## What this PR does / why we need it:

`EXP` overflow is a row-local `NULL`, but the current evaluator obtains
that result by constructing a formatted `moerr` for every overflowing
row and immediately discarding it. On the 1-billion-row `SUM(EXP(...))`
workload this turns expected data-domain rejection into billions of
error allocations, stack captures, formatting operations, and GC work.

This change adds an error-free unary NULL-check path and makes `EXP` use
`math.Exp` plus an infinity check. SQL behavior is unchanged: finite
results, underflow, input NULLs, constant inputs, and selection masks
retain their existing semantics; overflow still produces NULL. Existing
null-on-error callers keep their current error callback contract.

A local alloc profile of the unfixed 8192-row all-overflow benchmark
attributed 95.5% of benchmark allocation space cumulatively to
`momath.Exp` / `NewOutOfRangeNoCtxf`; stack capture was 45.7% and
`fmt.Sprintf` was 22.3% of total allocation space. After the change the
CPU profile is dominated by the intended `math.Exp` and NULL bitmap
work.

Benchmark on linux/amd64 (8192 overflowing values per operation):

```text
before: 14.54-16.95 ms/op   4,655,216-4,655,552 B/op   81,928-81,929 allocs/op
after:   0.103-0.121 ms/op      2,040-2,201 B/op        8 allocs/op
```

The test suite includes an allocation-scaling regression comparing one
overflow row with an 8192-row overflow batch.

Validation:

- `go test ./pkg/sql/plan/function -count=1`
- focused EXP/COT tests with `-count=5`
- focused EXP/COT tests with `-race`
- `go vet ./pkg/sql/plan/function`
- `git diff --check`

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
)

## What type of PR is this?

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

## Which issue(s) this PR fixes:

Fixes matrixorigin#28560

## What this PR does / why we need it:

`EXP` overflow is a row-local `NULL`, but the current evaluator obtains
that result by constructing a formatted `moerr` for every overflowing
row and immediately discarding it. On the 1-billion-row `SUM(EXP(...))`
workload this turns expected data-domain rejection into billions of
error allocations, stack captures, formatting operations, and GC work.

This change adds an error-free unary NULL-check path and makes `EXP` use
`math.Exp` plus an infinity check. SQL behavior is unchanged: finite
results, underflow, input NULLs, constant inputs, and selection masks
retain their existing semantics; overflow still produces NULL. Existing
null-on-error callers keep their current error callback contract.

A local alloc profile of the unfixed 8192-row all-overflow benchmark
attributed 95.5% of benchmark allocation space cumulatively to
`momath.Exp` / `NewOutOfRangeNoCtxf`; stack capture was 45.7% and
`fmt.Sprintf` was 22.3% of total allocation space. After the change the
CPU profile is dominated by the intended `math.Exp` and NULL bitmap
work.

Benchmark on linux/amd64 (8192 overflowing values per operation):

```text
before: 14.54-16.95 ms/op   4,655,216-4,655,552 B/op   81,928-81,929 allocs/op
after:   0.103-0.121 ms/op      2,040-2,201 B/op        8 allocs/op
```

The test suite includes an allocation-scaling regression comparing one
overflow row with an 8192-row overflow batch.

Validation:

- `go test ./pkg/sql/plan/function -count=1`
- focused EXP/COT tests with `-count=5`
- focused EXP/COT tests with `-race`
- `go vet ./pkg/sql/plan/function`
- `git diff --check`

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>

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/enhancement size/S Denotes a PR that changes [10,99] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: 10Y Math SUM(EXP()) runs beyond 7200s and leaves CN execution resources occupied

4 participants