Skip to content

[Backport 3.7] [Sparse ANN] Fold sparse vector tokens into the signed-short range - #1929

Closed
chishui wants to merge 1 commit into
opensearch-project:3.7from
chishui:backport/backport-1926-to-3.7
Closed

[Backport 3.7] [Sparse ANN] Fold sparse vector tokens into the signed-short range#1929
chishui wants to merge 1 commit into
opensearch-project:3.7from
chishui:backport/backport-1926-to-3.7

Conversation

@chishui

@chishui chishui commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator

Backport 936d3c4 from #1926.

The automatic backport failed due to a CHANGELOG.md conflict only — main's Bug Fixes section carries an unrelated SemanticHighlighter entry (#1906) that isn't on 3.7. The Java changes cherry-picked cleanly; the changelog entry was re-added alongside 3.7's existing Bug Fixes entries.

SparseVectorTests passes locally on this branch.

…pensearch-project#1926)

* [Sparse ANN] Fold sparse vector tokens into the signed-short range

Token IDs are folded into a fixed range via `token % MODULUS_FOR_SHORT` and
stored in a signed short[] to keep the memory footprint low. The modulus was
65536, so a token folding into [32768, 65535] was cast to a negative short
and, when read back, sign-extended to a negative int:

- getToken()/iterator() returned a negative token (e.g. 40000 -> -25536)
- toDenseVector() threw NegativeArraySizeException on `maxToken + 1`
- dotProduct() mis-scored via a negative index / bad bounds check

Those values never produced a usable index, so the effective working range
was already [0, 32767]. Set MODULUS_FOR_SHORT to 32768 so every folded token
is <= Short.MAX_VALUE and round-trips without sign extension. Because 32768
divides 65536, every token that previously worked folds to the same value, so
existing indices are unaffected.

Adds unit tests covering the constructor/iterator round-trip, toDenseVector(),
and dotProduct() for a token folding to Short.MAX_VALUE.

Signed-off-by: Liyun Xiu <xiliyun@amazon.com>

* update changelog

Signed-off-by: Liyun Xiu <xiliyun@amazon.com>

---------

Signed-off-by: Liyun Xiu <xiliyun@amazon.com>
(cherry picked from commit 936d3c4)
Signed-off-by: Liyun Xiu <xiliyun@amazon.com>
@github-actions

github-actions Bot commented Aug 4, 2026

Copy link
Copy Markdown

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

🧪 PR contains tests
🔒 No security concerns identified
✅ No TODO sections
🔀 No multiple PR themes
⚡ No major issues detected

@heemin32

heemin32 commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator

We don't allow back porting to older version unless it is security related.

@heemin32 heemin32 closed this Aug 4, 2026
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.

2 participants