Skip to content

fix: Respect offset in Vector.indexOf and includes - #532

Open
bardrop wants to merge 1 commit into
apache:mainfrom
bardrop-oss:fix-indexof-offset
Open

bardrop wants to merge 1 commit into
apache:mainfrom
bardrop-oss:fix-indexof-offset

Conversation

@bardrop

@bardrop bardrop commented Nov 14, 2026 •

Copy link
Copy Markdown

What's Changed

Vector.indexOf(element, offset) and includes(element, offset) did not respect offset:

  • Single-chunk vectors: the constructor installed this.indexOf = (index) => indexOf(unchunkedData, index), so the offset was dropped.
  • Chunked vectors: wrapChunkedIndexOf started its running total at 0 even when the search began in a later chunk, so the result was chunk-local. An offset past the end returned null from binarySearch instead of -1.
  • Searching for null with an offset returned an index relative to the offset (indexOfNull started counting at 0 and read data.length bits from the offset), and the Null type ignored the offset.

Changes:

  • The single-chunk indexOf passes the offset through.
  • chunkedIndexOf starts the total at the length of the chunks before the start chunk, and an out-of-range offset gives -1.
  • New toIndexOfStart() turns the offset into a start index the same way Array.prototype.indexOf does (fractions truncated, negative counts back from the end). Without it, passing the offset through would let a negative offset reach the visitors (for example indexOf(null, -2) on a single chunk returned -2), where it used to be ignored.
  • indexOfNull and the Null type visitor return indices in the whole data.

The new test in test/unit/vector/vector-tests.ts compares indexOf and includes with Array.prototype.indexOf / includes for one chunk, two chunks, with nulls, and the Null type, over offsets like 1.5, 99, -1, -99, NaN and ±Infinity. All 5 cases fail on main and pass with this change. The full src jest run (7250 tests), eslint and tsc on the build tsconfigs pass.

AI usage: I used an AI coding assistant (Claude) to help find the cause, write the fix and the test. I reviewed every line, checked the before/after output against Array.prototype.indexOf myself, and can answer questions about it.

Closes #531.

Single-chunk vectors dropped the offset argument. Chunked vectors
returned an index relative to the chunk the search started in, and
returned null when the offset was past the end.

Offsets now follow Array.prototype.indexOf: fractions are truncated and
negative values count back from the end. Searching for null with an
offset also returns the index in the whole vector.

Closes apache#531.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Vector.indexOf / includes: offset is ignored for single-chunk vectors and gives chunk-local results for chunked vectors

1 participant