Skip to content

Commit

Permalink
Fixed normalize for nil values
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Oct 7, 2024
1 parent 02788d3 commit 275677b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/neighbor/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ def self.validate(value, dimensions:, type:, adapter:)
end

def self.normalize(value, column_info:)
return nil if value.nil?

raise Error, "Normalize not supported for type" unless [:cube, :vector, :halfvec, :binary].include?(column_info&.type)

norm = Math.sqrt(value.sum { |v| v * v })
Expand Down
6 changes: 6 additions & 0 deletions test/cube_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,15 @@ def test_normalize
item = CosineItem.new
item.cube_embedding = [0, 3, 4]
assert_elements_in_delta [0, 0.6, 0.8], item.cube_embedding

item.save!
assert_elements_in_delta [0, 0.6, 0.8], item.cube_embedding
assert_elements_in_delta [0, 0.6, 0.8], Item.last.cube_embedding

item.cube_embedding = nil
item.save!
assert_nil item.cube_embedding
assert_nil Item.last.cube_embedding
end

def test_insert
Expand Down

0 comments on commit 275677b

Please sign in to comment.