Skip to content

fix(vector): reject NaN/Inf vector elements at the cast boundary (#28688) - #28881

Merged
mergify[bot] merged 3 commits into
matrixorigin:mainfrom
cpegeric:bug_28688
Sep 14, 2026
Merged

mergify[bot] merged 3 commits into
matrixorigin:mainfrom
cpegeric:bug_28688

Conversation

@cpegeric

@cpegeric cpegeric commented Sep 14, 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 #28688

What this PR does / why we need it:

Fixes #28688 — creating an IVFFLAT index on a column that contains a NaN or ±Inf vector component panicked with a Go
runtime stack returned to the client:

internal error: panic runtime error: index out of range [-8]:
vector.(*Vector).UnionOne
productl2.probeRun[...]

Affected every float vector type IVFFLAT supports (vecf32, vecf64, vecf16, vecbf16); integer narrow vectors are not
affected.

Root cause

A non-finite probe makes every centroid distance non-finite (NaN - x = NaN, Inf² = +Inf), and the distance functions
propagate that silently (no guard). In the limit==1 nearest-centroid assignment the argmin seeds minIdx = -1 / minDist = MaxFloat and only advances on dist < minDist — but NaN < x and +Inf < MaxFloat are both false, so minIdx stays -1.
productl2 then calls Vector.UnionOne(src, -1) → byte offset -1 × 8 = -8 → panic. kmeans centroid means were equally
exposed (a non-finite training row makes the mean non-finite, poisoning all subsequent distances).

Fix

Reject NaN/±Inf vector components at the cast boundary — pkg/container/types/array_str.go, in stringToT after each
strconv.ParseFloat:

  • New rejectNonFiniteArrayElem(num) returns an error on NaN/±Inf.
  • float32/float64: checked on the parsed value (matched-bitSize ParseFloat already errors on overflow).
  • BF16/Float16: checked on the narrowed value — a finite literal can overflow the 16-bit range to ±Inf during
    narrowing (70000 → f16 +Inf, 3.4e38 → bf16 +Inf), which a pre-narrowing check would miss.
  • int8/uint8 untouched (integers cannot be non-finite).

Because every write path funnels through StringToArray → stringToT, this single check covers INSERT, UPDATE, LOAD DATA
(CSV), and parquet plain-string import
— so no non-finite value ever reaches storage or any index (kmeans, argmin, top-k
heap, usearch, cuVS). Errors surface cleanly as internal error: vector element cannot be NaN or Inf: <value> instead of a
runtime panic.

Testing

  • Unit (array_str_test.go): NaN/+Inf/-Inf (incl. lowercase) rejected across float32/float64/BF16/Float16; finite
    values and int8/uint8 parse normally; the narrow-type overflow case (70000→f16, 3.4e38→bf16) is rejected while in-range
    narrow values still parse.
  • BVT (cases/array/array_nonfinite.sql): INSERT / UPDATE / LOAD DATA reject non-finite for vecf32/f64/bf16/f16; a
    finite LOAD control loads normally; CREATE INDEX … USING ivfflat builds on the surviving finite rows (the issue's exact
    scenario). mo-tester run mode 45/45 (100%).
  • Validated live on a two-CN cluster: the issue's repro now errors at INSERT instead of panicking at CREATE INDEX.

…rixorigin#28688)

Non-finite vector components (NaN/±Inf) poisoned IVFFLAT build: a non-finite
probe makes every centroid distance non-finite, the nearest-centroid argmin
never advances past its -1 sentinel, and productl2 fed -1 into Vector.UnionOne
-> "index out of range [-8]" panic, returning a Go stack to the client. kmeans
centroid means were equally exposed.

Fix at the write boundary: stringToT rejects NaN/±Inf after parsing each float
element, so non-finite values never reach storage or any index (kmeans, argmin,
top-k heap, usearch, cuVS). Covers INSERT, UPDATE, LOAD DATA (CSV), and parquet
plain-string, since all funnel through StringToArray -> stringToT. Integer narrow
vectors (vecint8/vecuint8) cannot be non-finite and are untouched.

For BF16/Float16 the check runs on the NARROWED value: a finite literal can
overflow the 16-bit range to ±Inf during narrowing (e.g. 70000 -> f16 +Inf,
3.4e38 -> bf16 +Inf), which the pre-narrowing check would miss.

Tests: unit tests for NaN/±Inf across all float types plus the narrow-overflow
case; BVT array_nonfinite.sql covers INSERT/UPDATE/LOAD DATA rejection for
vecf32/f64/bf16/f16 and confirms IVFFLAT builds on the surviving finite rows.

Note: rejecting at the shared parse boundary also rejects re-parsing of
pre-existing non-finite vectors (export->reimport / restore of data written
before this fix). Non-finite vectors are data errors with no valid use; handling
already-stored non-finite data (index-build/heap/kmeans hardening) is a separate
follow-up.

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 →

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

GPT-6 medium deep review at head c00b64b. The cast-boundary checks reject NaN/Inf for float vectors and validate values after narrowing for BF16/F16, while preserving integer-vector behavior. I found no introduced correctness blocker. The exact-head CI is green; this change adds no new racing UT whose individual runtime exceeds the 45.55s limit.

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

mergify Bot commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-09-14 18:41 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • ✅ Checks passed · in-place
  • ✅ Merged — 2026-09-14 19:34 UTC · at b33b063f3a342710dea9e886cb3fca62331a1988 · squash

This pull request spent 53 minutes 21 seconds in the queue, including 53 minutes 3 seconds running CI.

Required conditions to merge

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

GPT-6 medium deep review at exact head f6b0e7b. The PR's effective diff is unchanged from the reviewed vector cast-boundary change; the new head only syncs main. It rejects NaN/Inf for float vectors and validates after narrowing for BF16/F16, while leaving integer-vector behavior unchanged. I found no introduced correctness blocker. CI has restarted on this synchronized head and is still in progress; this approval records the code review, not a claim that the new run has completed.

@mergify
mergify Bot merged commit b33b063 into matrixorigin:main Sep 14, 2026
27 of 28 checks passed
@mergify mergify Bot removed the queued label Sep 14, 2026
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…rixorigin#28688) (matrixorigin#28881)

Fixes matrixorigin#28688 — creating an IVFFLAT index on a column that contains a `NaN` or `±Inf` vector component panicked with a Go
runtime stack returned to the client:

internal error: panic runtime error: index out of range [-8]:
vector.(*Vector).UnionOne
productl2.probeRun[...]

Affected every float vector type IVFFLAT supports (`vecf32`, `vecf64`, `vecf16`, `vecbf16`); integer narrow vectors are not
affected.

## Root cause

A non-finite probe makes **every** centroid distance non-finite (`NaN - x = NaN`, `Inf² = +Inf`), and the distance functions
propagate that silently (no guard). In the `limit==1` nearest-centroid assignment the argmin seeds `minIdx = -1` / `minDist =
MaxFloat` and only advances on `dist < minDist` — but `NaN < x` and `+Inf < MaxFloat` are both false, so `minIdx` stays `-1`.
`productl2` then calls `Vector.UnionOne(src, -1)` → byte offset `-1 × 8 = -8` → panic. kmeans centroid means were equally
exposed (a non-finite training row makes the mean non-finite, poisoning all subsequent distances).

## Fix

Reject `NaN`/`±Inf` vector components at the **cast boundary** — `pkg/container/types/array_str.go`, in `stringToT` after each
`strconv.ParseFloat`:

- New `rejectNonFiniteArrayElem(num)` returns an error on `NaN`/`±Inf`.
- `float32`/`float64`: checked on the parsed value (matched-bitSize `ParseFloat` already errors on overflow).
- `BF16`/`Float16`: checked on the **narrowed** value — a finite literal can overflow the 16-bit range to `±Inf` during
narrowing (`70000` → f16 `+Inf`, `3.4e38` → bf16 `+Inf`), which a pre-narrowing check would miss.
- `int8`/`uint8` untouched (integers cannot be non-finite).

Because every write path funnels through `StringToArray → stringToT`, this single check covers **INSERT, UPDATE, LOAD DATA
(CSV), and parquet plain-string import** — so no non-finite value ever reaches storage or any index (kmeans, argmin, top-k
heap, usearch, cuVS). Errors surface cleanly as `internal error: vector element cannot be NaN or Inf: <value>` instead of a
runtime panic.

## Testing

- **Unit** (`array_str_test.go`): `NaN`/`+Inf`/`-Inf` (incl. lowercase) rejected across `float32/float64/BF16/Float16`; finite
values and `int8/uint8` parse normally; the narrow-type overflow case (`70000`→f16, `3.4e38`→bf16) is rejected while in-range
narrow values still parse.
- **BVT** (`cases/array/array_nonfinite.sql`): INSERT / UPDATE / LOAD DATA reject non-finite for `vecf32/f64/bf16/f16`; a
finite LOAD control loads normally; `CREATE INDEX … USING ivfflat` builds on the surviving finite rows (the issue's exact
scenario). mo-tester run mode **45/45 (100%)**.
- Validated live on a two-CN cluster: the issue's repro now errors at INSERT instead of panicking at CREATE INDEX.

Approved by: @XuPeng-SH
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…rixorigin#28688) (matrixorigin#28881)

Fixes matrixorigin#28688 — creating an IVFFLAT index on a column that contains a `NaN` or `±Inf` vector component panicked with a Go
runtime stack returned to the client:

internal error: panic runtime error: index out of range [-8]:
vector.(*Vector).UnionOne
productl2.probeRun[...]

Affected every float vector type IVFFLAT supports (`vecf32`, `vecf64`, `vecf16`, `vecbf16`); integer narrow vectors are not
affected.

## Root cause

A non-finite probe makes **every** centroid distance non-finite (`NaN - x = NaN`, `Inf² = +Inf`), and the distance functions
propagate that silently (no guard). In the `limit==1` nearest-centroid assignment the argmin seeds `minIdx = -1` / `minDist =
MaxFloat` and only advances on `dist < minDist` — but `NaN < x` and `+Inf < MaxFloat` are both false, so `minIdx` stays `-1`.
`productl2` then calls `Vector.UnionOne(src, -1)` → byte offset `-1 × 8 = -8` → panic. kmeans centroid means were equally
exposed (a non-finite training row makes the mean non-finite, poisoning all subsequent distances).

## Fix

Reject `NaN`/`±Inf` vector components at the **cast boundary** — `pkg/container/types/array_str.go`, in `stringToT` after each
`strconv.ParseFloat`:

- New `rejectNonFiniteArrayElem(num)` returns an error on `NaN`/`±Inf`.
- `float32`/`float64`: checked on the parsed value (matched-bitSize `ParseFloat` already errors on overflow).
- `BF16`/`Float16`: checked on the **narrowed** value — a finite literal can overflow the 16-bit range to `±Inf` during
narrowing (`70000` → f16 `+Inf`, `3.4e38` → bf16 `+Inf`), which a pre-narrowing check would miss.
- `int8`/`uint8` untouched (integers cannot be non-finite).

Because every write path funnels through `StringToArray → stringToT`, this single check covers **INSERT, UPDATE, LOAD DATA
(CSV), and parquet plain-string import** — so no non-finite value ever reaches storage or any index (kmeans, argmin, top-k
heap, usearch, cuVS). Errors surface cleanly as `internal error: vector element cannot be NaN or Inf: <value>` instead of a
runtime panic.

## Testing

- **Unit** (`array_str_test.go`): `NaN`/`+Inf`/`-Inf` (incl. lowercase) rejected across `float32/float64/BF16/Float16`; finite
values and `int8/uint8` parse normally; the narrow-type overflow case (`70000`→f16, `3.4e38`→bf16) is rejected while in-range
narrow values still parse.
- **BVT** (`cases/array/array_nonfinite.sql`): INSERT / UPDATE / LOAD DATA reject non-finite for `vecf32/f64/bf16/f16`; a
finite LOAD control loads normally; `CREATE INDEX … USING ivfflat` builds on the surviving finite rows (the issue's exact
scenario). mo-tester run mode **45/45 (100%)**.
- Validated live on a two-CN cluster: the issue's repro now errors at INSERT instead of panicking at CREATE INDEX.

Approved by: @XuPeng-SH
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…rixorigin#28688) (matrixorigin#28881)

Fixes matrixorigin#28688 — creating an IVFFLAT index on a column that contains a `NaN` or `±Inf` vector component panicked with a Go
runtime stack returned to the client:

internal error: panic runtime error: index out of range [-8]:
vector.(*Vector).UnionOne
productl2.probeRun[...]

Affected every float vector type IVFFLAT supports (`vecf32`, `vecf64`, `vecf16`, `vecbf16`); integer narrow vectors are not
affected.

## Root cause

A non-finite probe makes **every** centroid distance non-finite (`NaN - x = NaN`, `Inf² = +Inf`), and the distance functions
propagate that silently (no guard). In the `limit==1` nearest-centroid assignment the argmin seeds `minIdx = -1` / `minDist =
MaxFloat` and only advances on `dist < minDist` — but `NaN < x` and `+Inf < MaxFloat` are both false, so `minIdx` stays `-1`.
`productl2` then calls `Vector.UnionOne(src, -1)` → byte offset `-1 × 8 = -8` → panic. kmeans centroid means were equally
exposed (a non-finite training row makes the mean non-finite, poisoning all subsequent distances).

## Fix

Reject `NaN`/`±Inf` vector components at the **cast boundary** — `pkg/container/types/array_str.go`, in `stringToT` after each
`strconv.ParseFloat`:

- New `rejectNonFiniteArrayElem(num)` returns an error on `NaN`/`±Inf`.
- `float32`/`float64`: checked on the parsed value (matched-bitSize `ParseFloat` already errors on overflow).
- `BF16`/`Float16`: checked on the **narrowed** value — a finite literal can overflow the 16-bit range to `±Inf` during
narrowing (`70000` → f16 `+Inf`, `3.4e38` → bf16 `+Inf`), which a pre-narrowing check would miss.
- `int8`/`uint8` untouched (integers cannot be non-finite).

Because every write path funnels through `StringToArray → stringToT`, this single check covers **INSERT, UPDATE, LOAD DATA
(CSV), and parquet plain-string import** — so no non-finite value ever reaches storage or any index (kmeans, argmin, top-k
heap, usearch, cuVS). Errors surface cleanly as `internal error: vector element cannot be NaN or Inf: <value>` instead of a
runtime panic.

## Testing

- **Unit** (`array_str_test.go`): `NaN`/`+Inf`/`-Inf` (incl. lowercase) rejected across `float32/float64/BF16/Float16`; finite
values and `int8/uint8` parse normally; the narrow-type overflow case (`70000`→f16, `3.4e38`→bf16) is rejected while in-range
narrow values still parse.
- **BVT** (`cases/array/array_nonfinite.sql`): INSERT / UPDATE / LOAD DATA reject non-finite for `vecf32/f64/bf16/f16`; a
finite LOAD control loads normally; `CREATE INDEX … USING ivfflat` builds on the surviving finite rows (the issue's exact
scenario). mo-tester run mode **45/45 (100%)**.
- Validated live on a two-CN cluster: the issue's repro now errors at INSERT instead of panicking at CREATE INDEX.

Approved by: @XuPeng-SH

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.

[Bug]: IVFFLAT index creation panics on vectors containing NaN or Infinity

3 participants