Skip to content

fix(vector): reject non-finite results in vector-vector arithmetic (#29085) - #29336

Merged
XuPeng-SH merged 2 commits into
matrixorigin:mainfrom
cpegeric:bug_29085
Sep 24, 2026
Merged

XuPeng-SH merged 2 commits into
matrixorigin:mainfrom
cpegeric:bug_29085

Conversation

@cpegeric

@cpegeric cpegeric commented Sep 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 #29085

What this PR does / why we need it:

Vector-vector +, -, *, / did not check whether the result stayed finite, so two finite vectors could overflow to [+Inf,+Inf] and — since the SQL binary operators return the value as-is — persist Infinity into a VECF32/VECF64 column via CTAS or INSERT ... SELECT. The vector-scalar path (ScalarOp) and a direct [Inf,Inf] insert are both already rejected; this closes the vector-vector gap.

Root cause: pkg/vectorize/moarray/external.go's Add/Subtract/Multiply/Divide returned the element-wise result without the finite check ScalarOp already applies to vector-scalar arithmetic.

Fix:

  • Add a shared checkVectorFinite and call it before returning from all four ops.
  • It uses the metric.CheckFinite* test x-x != 0 (0 for every finite x, NaN for +Inf/-Inf/NaN alike) rather than math.IsInf(float64(x),0): it also rejects a NaN produced from a non-finite input (e.g. Inf-Inf, which [Bug]: vector functions underflow or overflow on finite extreme magnitudes #29083 shows a cast can feed in) that an IsInf check would miss, and stays in T's domain with no float64 widening per element.
  • Error text matches ScalarOp's "vector contains infinity values" (surfaces as ERROR 20101), for a consistent arithmetic-overflow message across both paths.
  • The check runs after the existing div-by-zero and dimension guards, so those still fire first; finite results are unaffected.

Tests: TestVectorVectorArithmeticRejectsOverflow covers all four ops × VECF32/VECF64 with overflowing finite inputs (the issue's reproduction values), NaN-from-Inf inputs, finite controls (no false positives), and the preserved div-by-zero / dimension-mismatch guards. Build, existing arithmetic/scalar tests, and gofmt all clean.

…atrixorigin#29085)

moarray.Add/Subtract/Multiply/Divide returned their element-wise result without the
finite-result check ScalarOp already applies to vector-scalar arithmetic. Two finite
vectors could overflow to [+Inf,+Inf] and, since the SQL binary operators
(arithmetic.go plusFnArray/minusFnArray/...) return the value as-is, persist Infinity
into a VECF32/VECF64 column via CTAS or INSERT ... SELECT -- while the vector-scalar
path and a direct insert of [Inf,Inf] are both rejected.

Add a shared checkVectorFinite and call it before returning from all four ops. It uses
the metric.CheckFinite* test x-x != 0 (0 for every finite x, NaN for +Inf/-Inf/NaN
alike) rather than math.IsInf(float64(x),0): it also rejects a NaN produced from a
non-finite input (e.g. Inf-Inf, which matrixorigin#29083 shows a cast can feed in) that an IsInf
check would miss, and stays in T's domain with no float64 widening per element. The
error text matches ScalarOp's "vector contains infinity values" for a consistent
arithmetic-overflow message. The check runs after the existing div-by-zero and
dimension guards, so those still fire first; finite results are unaffected.

Test TestVectorVectorArithmeticRejectsOverflow covers all four ops x VECF32/VECF64 with
overflowing finite inputs (the issue's reproduction values), NaN-from-Inf inputs, finite
controls, and the preserved div-by-zero / dimension-mismatch guards.

Co-Authored-By: Claude Opus 4.8 (1M context) <[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 →

@cpegeric

Copy link
Copy Markdown
Contributor Author

fixed

@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 head a017f89 against main at d7d940e. I found no merge-blocking correctness, resource-lifecycle, or error-propagation issue. The shared finite-result check rejects both overflow and NaN while preserving division-by-zero and dimension error precedence.

Validation: the baseline reproduces the bug, with CTAS and INSERT ... SELECT persisting [+Inf,+Inf]. The PR rejects it across vecf32/vecf64 and all four operators. SQL probes also verified NULL/CASE handling, prepared-statement reuse after an error, and that failed writes do not persist non-finite results. Owning package tests, affected SQL arithmetic tests, build, vet, scoped lint, and current required CI passed.

Non-blocking follow-ups:

  • Add permanent public SQL and length 7/8/9 boundary regressions. The committed arithmetic tests currently exercise short vectors directly.
  • Record or consider reducing the extra result scan's cost. In a single-thread microbenchmark, 1536-element Add measured ~1.06 us on base vs ~1.81 us on this head; Divide measured ~2.14 us vs ~2.83 us. This does not establish an end-to-end SQL regression.

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

mergify Bot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-09-24 16:50 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • 🟠 Checks running · in-place
  • 🚫 Left the queue — 2026-09-24 16:51 UTC · at 779c7e2e1d72b8b815753a0981d50c917248d2cd

This pull request spent 1 minute 27 seconds in the queue, with no time running CI.

Reason

The pull request can't be updated
> This pull request seems to come from a fork, and Mergify needs the author's permission to update its branch.
The author needs to enable ["Allow edits from maintainers"](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/allowing-changes-to-a-pull-request-branch-created-from-a-fork) on this pull request, or update the branch manually.

Hint

You should update or rebase your pull request manually. If you do, this pull request will automatically be requeued once the queue conditions match again.
If you think this was a flaky issue, 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 2fb7b1f into matrixorigin:main Sep 24, 2026
19 of 20 checks passed
@mergify mergify Bot removed the queued label Sep 24, 2026

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