Skip to content

Fixed parsing of empty sparsevec in SparseVector - #37

Closed
OsamaAnsar wants to merge 1 commit into
pgvector:masterfrom
OsamaAnsar:fix/sparsevector-empty-sql
Closed

OsamaAnsar wants to merge 1 commit into
pgvector:masterfrom
OsamaAnsar:fix/sparsevector-empty-sql

Conversation

@OsamaAnsar

Copy link
Copy Markdown
Contributor

new SparseVector('{}/3') (the text pgvector returns for an all-zero sparsevec) parses to NaN instead of an empty vector. ''.split(',') is [''], so #fromSql pushes parseInt('') - 1 and parseFloat(undefined).

Real output on current master (run against src/index.js):

new SparseVector([0, 0, 0]).toPostgres()  -> {}/3
new SparseVector('{}/3')                  -> dimensions 3, indices [ NaN ], values [ NaN ]
  .toPostgres()                           -> {NaN:NaN}/3
  .toArray()                              -> [ 0, 0, 0, NaN: NaN ]   (own keys: '0', '1', '2', 'NaN')

With this change:

new SparseVector('{}/3')                  -> dimensions 3, indices [], values []
  .toPostgres()                           -> {}/3
  .toArray()                              -> [ 0, 0, 0 ]             (own keys: '0', '1', '2')

The fix takes the text between the braces and only loops over elements if it is not empty. Non-empty input like {1:1,3:2}/4 parses as before.

Added a fromSqlEmpty test to tests/sparse-vector.test.js. It is a pure unit test and does not need a database.

What I ran:

  • node --test tests/sparse-vector.test.js tests/utils.test.js: 8 pass.
  • With only src/sparse-vector.js reverted to master, the new test fails (actual: [ 0, 0, 0, NaN: NaN ]) and the other 5 pass; with the fix restored all 6 pass.
  • npm run build and npm run check (after prisma generate) complete without errors.
  • I did not run the rest of the test suite (the ORM/driver tests) because it needs a Postgres server with pgvector, which I don't have here.

An all-zero sparsevec is returned by Postgres as {}/n. Splitting the empty
body on ',' gave [''], so the constructor pushed parseInt('') - 1 and
parseFloat(undefined), leaving NaN in indices and values.
@ankane ankane closed this in c1f4190 Oct 12, 2026
@ankane

ankane commented Oct 12, 2026

Copy link
Copy Markdown
Member

Hi @OsamaAnsar, thanks for the PR. Merged a version of this in the commit above.

@OsamaAnsar

Copy link
Copy Markdown
Contributor Author

Hi @OsamaAnsar, thanks for the PR. Merged a version of this in the commit above.

Thanks!

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants