Skip to content

Commit d173b4e

Browse files
Add vectorstore API support
1 parent e851d08 commit d173b4e

File tree

7 files changed

+251
-3
lines changed

7 files changed

+251
-3
lines changed

docs/src/whatsnew.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ This document outlines features and improvements from each release.
88
are for non-production testing and evaluation, and may include
99
changes to the API.
1010

11+
v1.14.0 - May 28, 2025
12+
------------------------
13+
* Add ``vector_db`` property to a ``Connection`` object to get the vectorstor API to the current database
14+
* Add ``vector_db(...)`` function to connect to a database and optain a vectorstor API
15+
1116
v1.13.0 - April 16, 2025
1217
------------------------
1318
* Refactor external function server (experimental) type system

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ sqlparams
77
tomli>=1.1.0; python_version < '3.11'
88
typing_extensions<=4.13.2
99
wheel
10+
singlestore-vecorstore>=0.1.1

setup.cfg

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = singlestoredb
3-
version = 1.13.1
3+
version = 1.14.0
44
description = Interface to the SingleStoreDB database and workspace management APIs
55
long_description = file: README.md
66
long_description_content_type = text/markdown
@@ -26,6 +26,7 @@ install_requires =
2626
setuptools
2727
sqlparams
2828
wheel
29+
singlestore-vecorstore>=0.1.1
2930
tomli>=1.1.0;python_version < '3.11'
3031
typing-extensions<=4.13.2;python_version < '3.11'
3132
python_requires = >=3.8

singlestoredb/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
1414
"""
1515

16-
__version__ = '1.13.1'
16+
__version__ = '1.14.0'
1717

1818
from typing import Any
1919

@@ -31,7 +31,7 @@
3131
Date, Time, Timestamp, DateFromTicks, TimeFromTicks, TimestampFromTicks,
3232
Binary, STRING, BINARY, NUMBER, DATETIME, ROWID,
3333
)
34-
34+
from .vectorstore import vector_db
3535

3636
#
3737
# This function is defined here to prevent the side-effect of

singlestoredb/connection.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env python
22
"""SingleStoreDB connections and cursors."""
3+
34
import abc
45
import inspect
56
import io
@@ -23,6 +24,7 @@
2324
from urllib.parse import unquote_plus
2425
from urllib.parse import urlparse
2526

27+
2628
import sqlparams
2729
try:
2830
from pandas import DataFrame
@@ -1288,6 +1290,15 @@ def show(self) -> ShowAccessor:
12881290
"""Access server properties managed by the SHOW statement."""
12891291
return ShowAccessor(self)
12901292

1293+
@property
1294+
def vector_db(self) -> Any:
1295+
"""
1296+
Get vectorstore API accessor
1297+
"""
1298+
from vectorstore import VectorDB
1299+
if not hasattr(self, '_vector_db'):
1300+
self._vector_db = VectorDB(connection=self)
1301+
return self._vector_db
12911302

12921303
#
12931304
# NOTE: When adding parameters to this function, you should always
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import os
2+
import unittest
3+
4+
import singlestoredb as s2
5+
from . import utils
6+
7+
from vectorstore import VectorDB
8+
9+
class TestVectorDB(unittest.TestCase):
10+
11+
driver = s2
12+
13+
dbname: str = ''
14+
dbexisted: bool = False
15+
16+
@classmethod
17+
def setUpClass(cls):
18+
sql_file = os.path.join(os.path.dirname(__file__), 'empty.sql')
19+
cls.dbname, cls.dbexisted = utils.load_sql(sql_file)
20+
21+
@classmethod
22+
def tearDownClass(cls):
23+
if not cls.dbexisted:
24+
utils.drop_database(cls.dbname)
25+
26+
def test_vectordb_from_params(self):
27+
db: VectorDB = s2.vector_db(database=type(self).dbname)
28+
index = db.create_index(name="test_index", dimension=3, tags={"name": "test_tag"})
29+
assert index.name == "test_index"
30+
assert index.dimension == 3
31+
assert index.tags == {"name": "test_tag"}
32+
assert db.has_index("test_index")
33+
34+
def test_vectordb_from_connection(self):
35+
with s2.connect(database=type(self).dbname) as conn:
36+
db: VectorDB = conn.vector_db
37+
index = db.create_index(name="test_index_1", dimension=4, tags={"name": "test_tag"})
38+
assert index.name == "test_index_1"
39+
assert index.dimension == 4
40+
assert index.tags == {"name": "test_tag"}
41+
assert db.has_index("test_index_1")

singlestoredb/vectorstore.py

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
from __future__ import annotations
2+
from typing import Optional, Dict, Callable, Any
3+
4+
from vectorstore import (
5+
IndexInterface,
6+
IndexList,
7+
IndexModel,
8+
MatchTypedDict,
9+
Metric,
10+
IndexStatsTypedDict,
11+
NamespaceStatsTypedDict,
12+
Vector,
13+
VectorDictMetadataValue,
14+
VectorMetadataTypedDict,
15+
VectorTuple,
16+
VectorTupleWithMetadata,
17+
DeletionProtection,
18+
AndFilter,
19+
EqFilter,
20+
ExactMatchFilter,
21+
FilterTypedDict,
22+
GteFilter,
23+
GtFilter,
24+
InFilter,
25+
LteFilter,
26+
LtFilter,
27+
NeFilter,
28+
NinFilter,
29+
OrFilter,
30+
SimpleFilter
31+
)
32+
33+
def vector_db(
34+
host: Optional[str] = None, user: Optional[str] = None,
35+
password: Optional[str] = None, port: Optional[int] = None,
36+
database: Optional[str] = None, driver: Optional[str] = None,
37+
pure_python: Optional[bool] = None, local_infile: Optional[bool] = None,
38+
charset: Optional[str] = None,
39+
ssl_key: Optional[str] = None, ssl_cert: Optional[str] = None,
40+
ssl_ca: Optional[str] = None, ssl_disabled: Optional[bool] = None,
41+
ssl_cipher: Optional[str] = None, ssl_verify_cert: Optional[bool] = None,
42+
tls_sni_servername: Optional[str] = None,
43+
ssl_verify_identity: Optional[bool] = None,
44+
conv: Optional[Dict[int, Callable[..., Any]]] = None,
45+
credential_type: Optional[str] = None,
46+
autocommit: Optional[bool] = None,
47+
results_type: Optional[str] = None,
48+
buffered: Optional[bool] = None,
49+
results_format: Optional[str] = None,
50+
program_name: Optional[str] = None,
51+
conn_attrs: Optional[Dict[str, str]] = {},
52+
multi_statements: Optional[bool] = None,
53+
client_found_rows: Optional[bool] = None,
54+
connect_timeout: Optional[int] = None,
55+
nan_as_null: Optional[bool] = None,
56+
inf_as_null: Optional[bool] = None,
57+
encoding_errors: Optional[str] = None,
58+
track_env: Optional[bool] = None,
59+
enable_extended_data_types: Optional[bool] = None,
60+
vector_data_format: Optional[str] = None,
61+
parse_json: Optional[bool] = None,
62+
pool_size: Optional[int] = 5,
63+
max_overflow: Optional[int] = 10,
64+
timeout: Optional[float] = 30,
65+
) -> "VectorDB":
66+
"""
67+
Return a vectorstore API connection.
68+
Database should be specified in the URL or as a keyword.
69+
70+
Parameters
71+
----------
72+
host : str, optional
73+
Hostname, IP address, or URL that describes the connection.
74+
The scheme or protocol defines which database connector to use.
75+
By default, the ``mysql`` scheme is used. To connect to the
76+
HTTP API, the scheme can be set to ``http`` or ``https``. The username,
77+
password, host, and port are specified as in a standard URL. The path
78+
indicates the database name. The overall form of the URL is:
79+
``scheme://user:password@host:port/db_name``. The scheme can
80+
typically be left off (unless you are using the HTTP API):
81+
``user:password@host:port/db_name``.
82+
user : str, optional
83+
Database user name
84+
password : str, optional
85+
Database user password
86+
port : int, optional
87+
Database port. This defaults to 3306 for non-HTTP connections, 80
88+
for HTTP connections, and 443 for HTTPS connections.
89+
database : str, optional
90+
Database name.
91+
pure_python : bool, optional
92+
Use the connector in pure Python mode
93+
local_infile : bool, optional
94+
Allow local file uploads
95+
charset : str, optional
96+
Character set for string values
97+
ssl_key : str, optional
98+
File containing SSL key
99+
ssl_cert : str, optional
100+
File containing SSL certificate
101+
ssl_ca : str, optional
102+
File containing SSL certificate authority
103+
ssl_cipher : str, optional
104+
Sets the SSL cipher list
105+
ssl_disabled : bool, optional
106+
Disable SSL usage
107+
ssl_verify_cert : bool, optional
108+
Verify the server's certificate. This is automatically enabled if
109+
``ssl_ca`` is also specified.
110+
ssl_verify_identity : bool, optional
111+
Verify the server's identity
112+
conv : dict[int, Callable], optional
113+
Dictionary of data conversion functions
114+
credential_type : str, optional
115+
Type of authentication to use: auth.PASSWORD, auth.JWT, or auth.BROWSER_SSO
116+
autocommit : bool, optional
117+
Enable autocommits
118+
results_type : str, optional
119+
The form of the query results: tuples, namedtuples, dicts,
120+
numpy, polars, pandas, arrow
121+
buffered : bool, optional
122+
Should the entire query result be buffered in memory? This is the default
123+
behavior which allows full cursor control of the result, but does consume
124+
more memory.
125+
results_format : str, optional
126+
Deprecated. This option has been renamed to results_type.
127+
program_name : str, optional
128+
Name of the program
129+
conn_attrs : dict, optional
130+
Additional connection attributes for telemetry. Example:
131+
{'program_version': "1.0.2", "_connector_name": "dbt connector"}
132+
multi_statements: bool, optional
133+
Should multiple statements be allowed within a single query?
134+
connect_timeout : int, optional
135+
The timeout for connecting to the database in seconds.
136+
(default: 10, min: 1, max: 31536000)
137+
nan_as_null : bool, optional
138+
Should NaN values be treated as NULLs when used in parameter
139+
substitutions including uploaded data?
140+
inf_as_null : bool, optional
141+
Should Inf values be treated as NULLs when used in parameter
142+
substitutions including uploaded data?
143+
encoding_errors : str, optional
144+
The error handler name for value decoding errors
145+
track_env : bool, optional
146+
Should the connection track the SINGLESTOREDB_URL environment variable?
147+
enable_extended_data_types : bool, optional
148+
Should extended data types (BSON, vector) be enabled?
149+
vector_data_format : str, optional
150+
Format for vector types: json or binary
151+
pool_size : int, optional
152+
The number of connections to keep in the connection pool. Default is 5.
153+
max_overflow : int, optional
154+
The maximum number of connections to allow beyond the pool size.
155+
Default is 10.
156+
timeout : float, optional
157+
The timeout for acquiring a connection from the pool in seconds.
158+
Default is 30 seconds.
159+
160+
See Also
161+
--------
162+
:class:`Connection`
163+
164+
Returns
165+
-------
166+
:class:`VectorDB`
167+
168+
"""
169+
from vectorstore import VectorDB
170+
return VectorDB(host=host, user=user, password=password, port=port,
171+
database=database, driver=driver, pure_python=pure_python,
172+
local_infile=local_infile, charset=charset,
173+
ssl_key=ssl_key, ssl_cert=ssl_cert, ssl_ca=ssl_ca,
174+
ssl_disabled=ssl_disabled, ssl_cipher=ssl_cipher,
175+
ssl_verify_cert=ssl_verify_cert,
176+
tls_sni_servername=tls_sni_servername,
177+
ssl_verify_identity=ssl_verify_identity, conv=conv,
178+
credential_type=credential_type, autocommit=autocommit,
179+
results_type=results_type, buffered=buffered,
180+
results_format=results_format, program_name=program_name,
181+
conn_attrs=conn_attrs, multi_statements=multi_statements,
182+
client_found_rows=client_found_rows,
183+
connect_timeout=connect_timeout, nan_as_null=nan_as_null,
184+
inf_as_null=inf_as_null, encoding_errors=encoding_errors,
185+
track_env=track_env,
186+
enable_extended_data_types=enable_extended_data_types,
187+
vector_data_format=vector_data_format,
188+
parse_json=parse_json, pool_size=pool_size,
189+
max_overflow=max_overflow, timeout=timeout)

0 commit comments

Comments
 (0)