-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement support for vector type (#439)
Vectors get decoded into array.array. Encoding supports any list-like array of numbers, but has an optimized fast path for things like array and ndarray that avoids needing to box integers.
- Loading branch information
Showing
2 changed files
with
245 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
# | ||
# This source file is part of the EdgeDB open source project. | ||
# | ||
# Copyright 2019-present MagicStack Inc. and the EdgeDB authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
from edgedb import _testbase as tb | ||
import edgedb | ||
|
||
import array | ||
|
||
|
||
# An array.array subtype where indexing doesn't work. | ||
# We use this to verify that the non-boxing memoryview based | ||
# fast path works, since the slow path won't work on this object. | ||
class brokenarray(array.array): | ||
def __getitem__(self, i): | ||
raise AssertionError("the fast path wasn't used!") | ||
|
||
|
||
class TestVector(tb.SyncQueryTestCase): | ||
def setUp(self): | ||
super().setUp() | ||
|
||
if not self.client.query_required_single(''' | ||
select exists ( | ||
select sys::ExtensionPackage filter .name = 'vector' | ||
) | ||
'''): | ||
self.skipTest("feature not implemented") | ||
|
||
self.client.execute(''' | ||
create extension vector version '1.0' | ||
''') | ||
|
||
def tearDown(self): | ||
try: | ||
self.client.execute(''' | ||
drop extension vector version '1.0' | ||
''') | ||
finally: | ||
super().tearDown() | ||
|
||
async def test_vector_01(self): | ||
# if not self.client.query_required_single(''' | ||
# select exists ( | ||
# select sys::ExtensionPackage filter .name = 'vector' | ||
# ) | ||
# '''): | ||
# self.skipTest("feature not implemented") | ||
|
||
# self.client.execute(''' | ||
# create extension vector version '1.0' | ||
# ''') | ||
|
||
val = self.client.query_single(''' | ||
select <vector::vector>'[1.5,2.0,3.8]' | ||
''') | ||
self.assertTrue(isinstance(val, array.array)) | ||
self.assertEqual(val, array.array('f', [1.5, 2.0, 3.8])) | ||
|
||
val = self.client.query_single( | ||
''' | ||
select <str><vector::vector>$0 | ||
''', | ||
[3.0, 9.0, -42.5], | ||
) | ||
self.assertEqual(val, '[3,9,-42.5]') | ||
|
||
val = self.client.query_single( | ||
''' | ||
select <str><vector::vector>$0 | ||
''', | ||
array.array('f', [3.0, 9.0, -42.5]) | ||
) | ||
self.assertEqual(val, '[3,9,-42.5]') | ||
|
||
val = self.client.query_single( | ||
''' | ||
select <str><vector::vector>$0 | ||
''', | ||
array.array('i', [1, 2, 3]), | ||
) | ||
self.assertEqual(val, '[1,2,3]') | ||
|
||
# Test that the fast-path works: if the encoder tries to | ||
# call __getitem__ on this brokenarray, it will fail. | ||
val = self.client.query_single( | ||
''' | ||
select <str><vector::vector>$0 | ||
''', | ||
brokenarray('f', [3.0, 9.0, -42.5]) | ||
) | ||
self.assertEqual(val, '[3,9,-42.5]') | ||
|
||
# I don't think it's worth adding a dependency to test this, | ||
# but this works too: | ||
# import numpy as np | ||
# val = self.client.query_single( | ||
# ''' | ||
# select <str><vector::vector>$0 | ||
# ''', | ||
# np.asarray([3.0, 9.0, -42.5], dtype=np.float32), | ||
# ) | ||
|
||
# Some sad path tests | ||
with self.assertRaises(edgedb.InvalidArgumentError): | ||
self.client.query_single( | ||
''' | ||
select <vector::vector>$0 | ||
''', | ||
[3.0, None, -42.5], | ||
) | ||
|
||
with self.assertRaises(edgedb.InvalidArgumentError): | ||
self.client.query_single( | ||
''' | ||
select <vector::vector>$0 | ||
''', | ||
[3.0, 'x', -42.5], | ||
) | ||
|
||
with self.assertRaises(edgedb.InvalidArgumentError): | ||
self.client.query_single( | ||
''' | ||
select <vector::vector>$0 | ||
''', | ||
'foo', | ||
) |