Repository navigation
Conversation
SparseVector._from_dense only checked ndim for scipy inputs. 2D arrays raised a raw TypeError from float(), and (n, 1) column arrays were silently accepted as n-dimensional vectors, unlike Vector, HalfVector and Bit which raise 'expected ndim to be 1'.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
SparseVectoraccepted any NumPy array and only failed (or silently misbehaved) deep inside_from_dense.Vector,HalfVector,Bitand the scipy path ofSparseVectorall raiseValueError('expected ndim to be 1')for non-1D input; this makes the dense NumPy path consistent.Root Cause / Motivation
_from_densedoeslen(value)andfloat(v)per element:SparseVector(np.zeros((2, 2)))raisedTypeError: only length-1 arrays can be converted to Python scalarsSparseVector(np.array(5.0))raisedTypeError: len() of unsized objectSparseVector(np.array([[1], [0], [2]]))was silently accepted as a 3-dimensional vector (each size-1 row coerced byfloat()), whileVectorrejects the same inputCallers that catch
ValueErrorfor bad input (e.g. the SQLAlchemytest_bad_ndimpattern) never see these cases.Changes
pgvector/sparsevec.pyValueError('expected ndim to be 1')in_from_densefor non-1D ndarraystests/test_sparse_vector.pytest_init_ndarray_ndimcovers 2D, column, and 0-d arrays (fails before the fix)CHANGELOG.mdTesting
Impact
Consistent, catchable error for bad NumPy input across all vector types; no behavior change for valid 1D input.
🤖 Generated with Claude Code