sqlalchemy-singlestoredb 1.2.1__tar.gz → 1.2.3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sqlalchemy_singlestoredb-1.2.3/PKG-INFO +192 -0
- sqlalchemy_singlestoredb-1.2.3/README.md +169 -0
- {sqlalchemy_singlestoredb-1.2.1 → sqlalchemy_singlestoredb-1.2.3}/pyproject.toml +4 -3
- {sqlalchemy_singlestoredb-1.2.1 → sqlalchemy_singlestoredb-1.2.3}/sqlalchemy_singlestoredb/__init__.py +1 -1
- {sqlalchemy_singlestoredb-1.2.1 → sqlalchemy_singlestoredb-1.2.3}/sqlalchemy_singlestoredb/base.py +4 -2
- {sqlalchemy_singlestoredb-1.2.1 → sqlalchemy_singlestoredb-1.2.3}/sqlalchemy_singlestoredb/ddlelement.py +12 -12
- {sqlalchemy_singlestoredb-1.2.1 → sqlalchemy_singlestoredb-1.2.3}/sqlalchemy_singlestoredb/dtypes.py +52 -3
- {sqlalchemy_singlestoredb-1.2.1 → sqlalchemy_singlestoredb-1.2.3}/sqlalchemy_singlestoredb/tests/conftest.py +7 -5
- {sqlalchemy_singlestoredb-1.2.1 → sqlalchemy_singlestoredb-1.2.3}/sqlalchemy_singlestoredb/tests/test_basics.py +8 -4
- sqlalchemy_singlestoredb-1.2.3/sqlalchemy_singlestoredb/tests/test_connect_args.py +85 -0
- sqlalchemy_singlestoredb-1.2.3/sqlalchemy_singlestoredb/tests/test_connect_retry.py +103 -0
- {sqlalchemy_singlestoredb-1.2.1 → sqlalchemy_singlestoredb-1.2.3}/sqlalchemy_singlestoredb/tests/test_vector_type.py +74 -0
- sqlalchemy_singlestoredb-1.2.3/sqlalchemy_singlestoredb/tests/utils.py +58 -0
- sqlalchemy_singlestoredb-1.2.3/sqlalchemy_singlestoredb.egg-info/PKG-INFO +192 -0
- {sqlalchemy_singlestoredb-1.2.1 → sqlalchemy_singlestoredb-1.2.3}/sqlalchemy_singlestoredb.egg-info/SOURCES.txt +4 -1
- sqlalchemy_singlestoredb-1.2.1/PKG-INFO +0 -44
- sqlalchemy_singlestoredb-1.2.1/README.md +0 -21
- sqlalchemy_singlestoredb-1.2.1/sqlalchemy_singlestoredb.egg-info/PKG-INFO +0 -44
- {sqlalchemy_singlestoredb-1.2.1 → sqlalchemy_singlestoredb-1.2.3}/LICENSE +0 -0
- {sqlalchemy_singlestoredb-1.2.1 → sqlalchemy_singlestoredb-1.2.3}/setup.cfg +0 -0
- {sqlalchemy_singlestoredb-1.2.1 → sqlalchemy_singlestoredb-1.2.3}/sqlalchemy_singlestoredb/column.py +0 -0
- {sqlalchemy_singlestoredb-1.2.1 → sqlalchemy_singlestoredb-1.2.3}/sqlalchemy_singlestoredb/compat.py +0 -0
- {sqlalchemy_singlestoredb-1.2.1 → sqlalchemy_singlestoredb-1.2.3}/sqlalchemy_singlestoredb/examples/shard_key_examples.py +0 -0
- {sqlalchemy_singlestoredb-1.2.1 → sqlalchemy_singlestoredb-1.2.3}/sqlalchemy_singlestoredb/examples/table_constructor_examples.py +0 -0
- {sqlalchemy_singlestoredb-1.2.1 → sqlalchemy_singlestoredb-1.2.3}/sqlalchemy_singlestoredb/examples/universal/main.py +0 -0
- {sqlalchemy_singlestoredb-1.2.1 → sqlalchemy_singlestoredb-1.2.3}/sqlalchemy_singlestoredb/reflection.py +0 -0
- {sqlalchemy_singlestoredb-1.2.1 → sqlalchemy_singlestoredb-1.2.3}/sqlalchemy_singlestoredb/tests/__init__.py +0 -0
- {sqlalchemy_singlestoredb-1.2.1 → sqlalchemy_singlestoredb-1.2.3}/sqlalchemy_singlestoredb/tests/run_all_tests.py +0 -0
- {sqlalchemy_singlestoredb-1.2.1 → sqlalchemy_singlestoredb-1.2.3}/sqlalchemy_singlestoredb/tests/test.sql +0 -0
- {sqlalchemy_singlestoredb-1.2.1 → sqlalchemy_singlestoredb-1.2.3}/sqlalchemy_singlestoredb/tests/test_advanced_indexes.py +0 -0
- {sqlalchemy_singlestoredb-1.2.1 → sqlalchemy_singlestoredb-1.2.3}/sqlalchemy_singlestoredb/tests/test_column_group.py +0 -0
- {sqlalchemy_singlestoredb-1.2.1 → sqlalchemy_singlestoredb-1.2.3}/sqlalchemy_singlestoredb/tests/test_database_fixtures.py +0 -0
- {sqlalchemy_singlestoredb-1.2.1 → sqlalchemy_singlestoredb-1.2.3}/sqlalchemy_singlestoredb/tests/test_declarative_reflection.py +0 -0
- {sqlalchemy_singlestoredb-1.2.1 → sqlalchemy_singlestoredb-1.2.3}/sqlalchemy_singlestoredb/tests/test_error_handling.py +0 -0
- {sqlalchemy_singlestoredb-1.2.1 → sqlalchemy_singlestoredb-1.2.3}/sqlalchemy_singlestoredb/tests/test_error_handling_pytest.py +0 -0
- {sqlalchemy_singlestoredb-1.2.1 → sqlalchemy_singlestoredb-1.2.3}/sqlalchemy_singlestoredb/tests/test_integration_features.py +0 -0
- {sqlalchemy_singlestoredb-1.2.1 → sqlalchemy_singlestoredb-1.2.3}/sqlalchemy_singlestoredb/tests/test_json_type.py +0 -0
- {sqlalchemy_singlestoredb-1.2.1 → sqlalchemy_singlestoredb-1.2.3}/sqlalchemy_singlestoredb/tests/test_key_reflection.py +0 -0
- {sqlalchemy_singlestoredb-1.2.1 → sqlalchemy_singlestoredb-1.2.3}/sqlalchemy_singlestoredb/tests/test_persisted_column.py +0 -0
- {sqlalchemy_singlestoredb-1.2.1 → sqlalchemy_singlestoredb-1.2.3}/sqlalchemy_singlestoredb/tests/test_priority1_features.py +0 -0
- {sqlalchemy_singlestoredb-1.2.1 → sqlalchemy_singlestoredb-1.2.3}/sqlalchemy_singlestoredb/tests/test_shard_key.py +0 -0
- {sqlalchemy_singlestoredb-1.2.1 → sqlalchemy_singlestoredb-1.2.3}/sqlalchemy_singlestoredb/tests/test_sort_key.py +0 -0
- {sqlalchemy_singlestoredb-1.2.1 → sqlalchemy_singlestoredb-1.2.3}/sqlalchemy_singlestoredb/tests/test_table_options.py +0 -0
- {sqlalchemy_singlestoredb-1.2.1 → sqlalchemy_singlestoredb-1.2.3}/sqlalchemy_singlestoredb/tests/test_table_types.py +0 -0
- {sqlalchemy_singlestoredb-1.2.1 → sqlalchemy_singlestoredb-1.2.3}/sqlalchemy_singlestoredb/tests/test_vector_key.py +0 -0
- {sqlalchemy_singlestoredb-1.2.1 → sqlalchemy_singlestoredb-1.2.3}/sqlalchemy_singlestoredb.egg-info/dependency_links.txt +0 -0
- {sqlalchemy_singlestoredb-1.2.1 → sqlalchemy_singlestoredb-1.2.3}/sqlalchemy_singlestoredb.egg-info/entry_points.txt +0 -0
- {sqlalchemy_singlestoredb-1.2.1 → sqlalchemy_singlestoredb-1.2.3}/sqlalchemy_singlestoredb.egg-info/requires.txt +0 -0
- {sqlalchemy_singlestoredb-1.2.1 → sqlalchemy_singlestoredb-1.2.3}/sqlalchemy_singlestoredb.egg-info/top_level.txt +0 -0
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: sqlalchemy_singlestoredb
|
|
3
|
+
Version: 1.2.3
|
|
4
|
+
Summary: SQLAlchemy dialect for the SingleStoreDB database
|
|
5
|
+
Author-email: SingleStore <support@singlestore.com>
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/singlestore-labs/sqlalchemy-singlestoredb
|
|
8
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
11
|
+
Requires-Python: >=3.9
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Requires-Dist: singlestoredb>=1.0.0
|
|
15
|
+
Requires-Dist: sqlalchemy<3.0.0dev,>=1.4.0
|
|
16
|
+
Provides-Extra: dev
|
|
17
|
+
Requires-Dist: coverage; extra == "dev"
|
|
18
|
+
Requires-Dist: docker; extra == "dev"
|
|
19
|
+
Requires-Dist: pytest; extra == "dev"
|
|
20
|
+
Requires-Dist: pytest-cov; extra == "dev"
|
|
21
|
+
Requires-Dist: tomli; python_version < "3.11" and extra == "dev"
|
|
22
|
+
Dynamic: license-file
|
|
23
|
+
|
|
24
|
+
# <img src="https://raw.githubusercontent.com/singlestore-labs/singlestoredb-python/main/resources/singlestore-logo.png" height="60" valign="middle"/> SingleStoreDB SQLAlchemy Dialect
|
|
25
|
+
|
|
26
|
+
This project contains a [SQLAlchemy](https://sqlalchemy.org) dialect which allows
|
|
27
|
+
you to use the SQLAlchemy APIs with the [SingleStoreDB](https://singlestore.com) database.
|
|
28
|
+
|
|
29
|
+
## Features
|
|
30
|
+
|
|
31
|
+
- **SingleStore-specific data types**: VECTOR for AI/ML embeddings, JSON for semi-structured data
|
|
32
|
+
- **Computed columns**: PersistedColumn for server-side computed values
|
|
33
|
+
- **Data distribution**: ShardKey for optimal data partitioning across nodes
|
|
34
|
+
- **Query optimization**: SortKey for improved query performance
|
|
35
|
+
- **Vector similarity search**: VectorKey for AI/ML similarity operations
|
|
36
|
+
- **Full-text search**: FullTextIndex for text search capabilities
|
|
37
|
+
- **JSON array indexing**: MultiValueIndex for efficient JSON array queries
|
|
38
|
+
- **Table types**: ColumnStore (analytical) and RowStore (transactional) with reference table support
|
|
39
|
+
- **Wide table optimization**: ColumnGroup for tables with many columns
|
|
40
|
+
|
|
41
|
+
## Install
|
|
42
|
+
|
|
43
|
+
This package can be installed from PyPI using `pip`:
|
|
44
|
+
```bash
|
|
45
|
+
pip install sqlalchemy-singlestoredb
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Quick Start
|
|
49
|
+
|
|
50
|
+
### Connecting to SingleStoreDB
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
from sqlalchemy import create_engine, text
|
|
54
|
+
|
|
55
|
+
# Basic connection
|
|
56
|
+
engine = create_engine('singlestoredb://user:password@host:3306/database')
|
|
57
|
+
|
|
58
|
+
# Test the connection
|
|
59
|
+
with engine.connect() as conn:
|
|
60
|
+
result = conn.execute(text('SELECT 1'))
|
|
61
|
+
print(result.fetchone())
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Creating Tables (Core API)
|
|
65
|
+
|
|
66
|
+
```python
|
|
67
|
+
from sqlalchemy import Column, DateTime, Integer, MetaData, String, Table
|
|
68
|
+
from sqlalchemy_singlestoredb import ShardKey, SortKey, VECTOR, JSON
|
|
69
|
+
|
|
70
|
+
metadata = MetaData()
|
|
71
|
+
|
|
72
|
+
users = Table(
|
|
73
|
+
'users', metadata,
|
|
74
|
+
Column('id', Integer, primary_key=True),
|
|
75
|
+
Column('name', String(100)),
|
|
76
|
+
Column('email', String(255)),
|
|
77
|
+
Column('created_at', DateTime),
|
|
78
|
+
singlestoredb_shard_key=ShardKey('id'),
|
|
79
|
+
singlestoredb_sort_key=SortKey('created_at'),
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
metadata.create_all(engine)
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### Creating Tables (ORM)
|
|
86
|
+
|
|
87
|
+
```python
|
|
88
|
+
from sqlalchemy import Column, DateTime, Integer, String
|
|
89
|
+
from sqlalchemy.orm import declarative_base
|
|
90
|
+
from sqlalchemy_singlestoredb import ShardKey, SortKey
|
|
91
|
+
|
|
92
|
+
Base = declarative_base()
|
|
93
|
+
|
|
94
|
+
class User(Base):
|
|
95
|
+
__tablename__ = 'users'
|
|
96
|
+
|
|
97
|
+
id = Column(Integer, primary_key=True)
|
|
98
|
+
name = Column(String(100))
|
|
99
|
+
email = Column(String(255))
|
|
100
|
+
created_at = Column(DateTime)
|
|
101
|
+
|
|
102
|
+
__table_args__ = {
|
|
103
|
+
'singlestoredb_shard_key': ShardKey('id'),
|
|
104
|
+
'singlestoredb_sort_key': SortKey('created_at'),
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
Base.metadata.create_all(engine)
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## Key SingleStore Features
|
|
111
|
+
|
|
112
|
+
### Vector Search for AI/ML
|
|
113
|
+
|
|
114
|
+
SingleStoreDB supports vector similarity search for AI/ML applications:
|
|
115
|
+
|
|
116
|
+
```python
|
|
117
|
+
from sqlalchemy import Column, Integer, String, func, select
|
|
118
|
+
from sqlalchemy.orm import declarative_base, Session
|
|
119
|
+
from sqlalchemy_singlestoredb import VECTOR, VectorKey, ColumnStore
|
|
120
|
+
|
|
121
|
+
Base = declarative_base()
|
|
122
|
+
|
|
123
|
+
class Document(Base):
|
|
124
|
+
__tablename__ = 'documents'
|
|
125
|
+
|
|
126
|
+
id = Column(Integer, primary_key=True)
|
|
127
|
+
title = Column(String(200))
|
|
128
|
+
embedding = Column(VECTOR(1536))
|
|
129
|
+
|
|
130
|
+
__table_args__ = {
|
|
131
|
+
'singlestoredb_table_type': ColumnStore(),
|
|
132
|
+
'singlestoredb_vector_key': [
|
|
133
|
+
VectorKey('embedding', index_options={'metric_type': 'COSINE_SIMILARITY'}),
|
|
134
|
+
],
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
# Find similar documents
|
|
138
|
+
query_embedding = [0.1, 0.2, ...] # Your embedding vector
|
|
139
|
+
with Session(engine) as session:
|
|
140
|
+
stmt = (
|
|
141
|
+
select(Document)
|
|
142
|
+
.order_by(func.dot_product(Document.embedding, query_embedding).desc())
|
|
143
|
+
.limit(10)
|
|
144
|
+
)
|
|
145
|
+
similar_docs = session.execute(stmt).scalars().all()
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### Data Distribution with Shard Keys
|
|
149
|
+
|
|
150
|
+
Shard keys control how data is distributed across nodes:
|
|
151
|
+
|
|
152
|
+
```python
|
|
153
|
+
from sqlalchemy_singlestoredb import ShardKey
|
|
154
|
+
|
|
155
|
+
__table_args__ = {
|
|
156
|
+
# Single column shard key
|
|
157
|
+
'singlestoredb_shard_key': ShardKey('user_id'),
|
|
158
|
+
|
|
159
|
+
# Multi-column shard key
|
|
160
|
+
'singlestoredb_shard_key': ShardKey('user_id', 'region_id'),
|
|
161
|
+
|
|
162
|
+
# Empty shard key for random distribution
|
|
163
|
+
'singlestoredb_shard_key': ShardKey(),
|
|
164
|
+
}
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### Table Types
|
|
168
|
+
|
|
169
|
+
Choose the right table type for your workload:
|
|
170
|
+
|
|
171
|
+
```python
|
|
172
|
+
from sqlalchemy_singlestoredb import ColumnStore, RowStore
|
|
173
|
+
|
|
174
|
+
# ColumnStore (default) - Best for analytical workloads
|
|
175
|
+
__table_args__ = {'singlestoredb_table_type': ColumnStore()}
|
|
176
|
+
|
|
177
|
+
# RowStore - Best for transactional workloads
|
|
178
|
+
__table_args__ = {'singlestoredb_table_type': RowStore()}
|
|
179
|
+
|
|
180
|
+
# Reference table - Replicated to all nodes for fast joins
|
|
181
|
+
__table_args__ = {'singlestoredb_table_type': RowStore(reference=True)}
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
## License
|
|
185
|
+
|
|
186
|
+
This library is licensed under the [Apache 2.0 License](https://raw.githubusercontent.com/singlestore-labs/singlestoredb-python/main/LICENSE?token=GHSAT0AAAAAABMGV6QPNR6N23BVICDYK5LAYTVK5EA).
|
|
187
|
+
|
|
188
|
+
## Resources
|
|
189
|
+
|
|
190
|
+
* [Documentation](https://sqlalchemy-singlestoredb.labs.singlestore.com)
|
|
191
|
+
* [SingleStore](https://www.singlestore.com)
|
|
192
|
+
* [SQLAlchemy](https://sqlalchemy.org)
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
# <img src="https://raw.githubusercontent.com/singlestore-labs/singlestoredb-python/main/resources/singlestore-logo.png" height="60" valign="middle"/> SingleStoreDB SQLAlchemy Dialect
|
|
2
|
+
|
|
3
|
+
This project contains a [SQLAlchemy](https://sqlalchemy.org) dialect which allows
|
|
4
|
+
you to use the SQLAlchemy APIs with the [SingleStoreDB](https://singlestore.com) database.
|
|
5
|
+
|
|
6
|
+
## Features
|
|
7
|
+
|
|
8
|
+
- **SingleStore-specific data types**: VECTOR for AI/ML embeddings, JSON for semi-structured data
|
|
9
|
+
- **Computed columns**: PersistedColumn for server-side computed values
|
|
10
|
+
- **Data distribution**: ShardKey for optimal data partitioning across nodes
|
|
11
|
+
- **Query optimization**: SortKey for improved query performance
|
|
12
|
+
- **Vector similarity search**: VectorKey for AI/ML similarity operations
|
|
13
|
+
- **Full-text search**: FullTextIndex for text search capabilities
|
|
14
|
+
- **JSON array indexing**: MultiValueIndex for efficient JSON array queries
|
|
15
|
+
- **Table types**: ColumnStore (analytical) and RowStore (transactional) with reference table support
|
|
16
|
+
- **Wide table optimization**: ColumnGroup for tables with many columns
|
|
17
|
+
|
|
18
|
+
## Install
|
|
19
|
+
|
|
20
|
+
This package can be installed from PyPI using `pip`:
|
|
21
|
+
```bash
|
|
22
|
+
pip install sqlalchemy-singlestoredb
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Quick Start
|
|
26
|
+
|
|
27
|
+
### Connecting to SingleStoreDB
|
|
28
|
+
|
|
29
|
+
```python
|
|
30
|
+
from sqlalchemy import create_engine, text
|
|
31
|
+
|
|
32
|
+
# Basic connection
|
|
33
|
+
engine = create_engine('singlestoredb://user:password@host:3306/database')
|
|
34
|
+
|
|
35
|
+
# Test the connection
|
|
36
|
+
with engine.connect() as conn:
|
|
37
|
+
result = conn.execute(text('SELECT 1'))
|
|
38
|
+
print(result.fetchone())
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Creating Tables (Core API)
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
from sqlalchemy import Column, DateTime, Integer, MetaData, String, Table
|
|
45
|
+
from sqlalchemy_singlestoredb import ShardKey, SortKey, VECTOR, JSON
|
|
46
|
+
|
|
47
|
+
metadata = MetaData()
|
|
48
|
+
|
|
49
|
+
users = Table(
|
|
50
|
+
'users', metadata,
|
|
51
|
+
Column('id', Integer, primary_key=True),
|
|
52
|
+
Column('name', String(100)),
|
|
53
|
+
Column('email', String(255)),
|
|
54
|
+
Column('created_at', DateTime),
|
|
55
|
+
singlestoredb_shard_key=ShardKey('id'),
|
|
56
|
+
singlestoredb_sort_key=SortKey('created_at'),
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
metadata.create_all(engine)
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Creating Tables (ORM)
|
|
63
|
+
|
|
64
|
+
```python
|
|
65
|
+
from sqlalchemy import Column, DateTime, Integer, String
|
|
66
|
+
from sqlalchemy.orm import declarative_base
|
|
67
|
+
from sqlalchemy_singlestoredb import ShardKey, SortKey
|
|
68
|
+
|
|
69
|
+
Base = declarative_base()
|
|
70
|
+
|
|
71
|
+
class User(Base):
|
|
72
|
+
__tablename__ = 'users'
|
|
73
|
+
|
|
74
|
+
id = Column(Integer, primary_key=True)
|
|
75
|
+
name = Column(String(100))
|
|
76
|
+
email = Column(String(255))
|
|
77
|
+
created_at = Column(DateTime)
|
|
78
|
+
|
|
79
|
+
__table_args__ = {
|
|
80
|
+
'singlestoredb_shard_key': ShardKey('id'),
|
|
81
|
+
'singlestoredb_sort_key': SortKey('created_at'),
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
Base.metadata.create_all(engine)
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## Key SingleStore Features
|
|
88
|
+
|
|
89
|
+
### Vector Search for AI/ML
|
|
90
|
+
|
|
91
|
+
SingleStoreDB supports vector similarity search for AI/ML applications:
|
|
92
|
+
|
|
93
|
+
```python
|
|
94
|
+
from sqlalchemy import Column, Integer, String, func, select
|
|
95
|
+
from sqlalchemy.orm import declarative_base, Session
|
|
96
|
+
from sqlalchemy_singlestoredb import VECTOR, VectorKey, ColumnStore
|
|
97
|
+
|
|
98
|
+
Base = declarative_base()
|
|
99
|
+
|
|
100
|
+
class Document(Base):
|
|
101
|
+
__tablename__ = 'documents'
|
|
102
|
+
|
|
103
|
+
id = Column(Integer, primary_key=True)
|
|
104
|
+
title = Column(String(200))
|
|
105
|
+
embedding = Column(VECTOR(1536))
|
|
106
|
+
|
|
107
|
+
__table_args__ = {
|
|
108
|
+
'singlestoredb_table_type': ColumnStore(),
|
|
109
|
+
'singlestoredb_vector_key': [
|
|
110
|
+
VectorKey('embedding', index_options={'metric_type': 'COSINE_SIMILARITY'}),
|
|
111
|
+
],
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
# Find similar documents
|
|
115
|
+
query_embedding = [0.1, 0.2, ...] # Your embedding vector
|
|
116
|
+
with Session(engine) as session:
|
|
117
|
+
stmt = (
|
|
118
|
+
select(Document)
|
|
119
|
+
.order_by(func.dot_product(Document.embedding, query_embedding).desc())
|
|
120
|
+
.limit(10)
|
|
121
|
+
)
|
|
122
|
+
similar_docs = session.execute(stmt).scalars().all()
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### Data Distribution with Shard Keys
|
|
126
|
+
|
|
127
|
+
Shard keys control how data is distributed across nodes:
|
|
128
|
+
|
|
129
|
+
```python
|
|
130
|
+
from sqlalchemy_singlestoredb import ShardKey
|
|
131
|
+
|
|
132
|
+
__table_args__ = {
|
|
133
|
+
# Single column shard key
|
|
134
|
+
'singlestoredb_shard_key': ShardKey('user_id'),
|
|
135
|
+
|
|
136
|
+
# Multi-column shard key
|
|
137
|
+
'singlestoredb_shard_key': ShardKey('user_id', 'region_id'),
|
|
138
|
+
|
|
139
|
+
# Empty shard key for random distribution
|
|
140
|
+
'singlestoredb_shard_key': ShardKey(),
|
|
141
|
+
}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### Table Types
|
|
145
|
+
|
|
146
|
+
Choose the right table type for your workload:
|
|
147
|
+
|
|
148
|
+
```python
|
|
149
|
+
from sqlalchemy_singlestoredb import ColumnStore, RowStore
|
|
150
|
+
|
|
151
|
+
# ColumnStore (default) - Best for analytical workloads
|
|
152
|
+
__table_args__ = {'singlestoredb_table_type': ColumnStore()}
|
|
153
|
+
|
|
154
|
+
# RowStore - Best for transactional workloads
|
|
155
|
+
__table_args__ = {'singlestoredb_table_type': RowStore()}
|
|
156
|
+
|
|
157
|
+
# Reference table - Replicated to all nodes for fast joins
|
|
158
|
+
__table_args__ = {'singlestoredb_table_type': RowStore(reference=True)}
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
## License
|
|
162
|
+
|
|
163
|
+
This library is licensed under the [Apache 2.0 License](https://raw.githubusercontent.com/singlestore-labs/singlestoredb-python/main/LICENSE?token=GHSAT0AAAAAABMGV6QPNR6N23BVICDYK5LAYTVK5EA).
|
|
164
|
+
|
|
165
|
+
## Resources
|
|
166
|
+
|
|
167
|
+
* [Documentation](https://sqlalchemy-singlestoredb.labs.singlestore.com)
|
|
168
|
+
* [SingleStore](https://www.singlestore.com)
|
|
169
|
+
* [SQLAlchemy](https://sqlalchemy.org)
|
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
[build-system]
|
|
2
|
-
requires = ['setuptools>=
|
|
2
|
+
requires = ['setuptools>=77']
|
|
3
3
|
build-backend = 'setuptools.build_meta'
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = 'sqlalchemy_singlestoredb'
|
|
7
|
-
version = '1.2.
|
|
7
|
+
version = '1.2.3'
|
|
8
8
|
description = 'SQLAlchemy dialect for the SingleStoreDB database'
|
|
9
9
|
readme = 'README.md'
|
|
10
10
|
requires-python = '>=3.9'
|
|
11
|
-
license =
|
|
11
|
+
license = 'Apache-2.0'
|
|
12
|
+
license-files = ['LICENSE']
|
|
12
13
|
authors = [
|
|
13
14
|
{name = 'SingleStore', email = 'support@singlestore.com'},
|
|
14
15
|
]
|
{sqlalchemy_singlestoredb-1.2.1 → sqlalchemy_singlestoredb-1.2.3}/sqlalchemy_singlestoredb/base.py
RENAMED
|
@@ -533,10 +533,12 @@ class _myconnpyBIT(BIT):
|
|
|
533
533
|
|
|
534
534
|
def render_as_string(url: URL) -> str:
|
|
535
535
|
s = url.drivername + '://'
|
|
536
|
+
# safe='' so a / in the user or password is escaped too; left bare,
|
|
537
|
+
# it ends the authority and the rest is misread as host and port.
|
|
536
538
|
if url.username is not None:
|
|
537
|
-
s += quote(url.username)
|
|
539
|
+
s += quote(url.username, safe='')
|
|
538
540
|
if url.password is not None:
|
|
539
|
-
s += ':' + quote(str(url.password))
|
|
541
|
+
s += ':' + quote(str(url.password), safe='')
|
|
540
542
|
s += '@'
|
|
541
543
|
if url.host is not None:
|
|
542
544
|
if ':' in url.host:
|
|
@@ -604,7 +604,7 @@ class VectorKey(DDLElement):
|
|
|
604
604
|
'embeddings', metadata,
|
|
605
605
|
Column('id', Integer, primary_key=True),
|
|
606
606
|
Column('embedding', VECTOR(1536)),
|
|
607
|
-
|
|
607
|
+
singlestoredb_vector_key=[VectorKey('embedding')],
|
|
608
608
|
)
|
|
609
609
|
|
|
610
610
|
# Named vector index with options
|
|
@@ -612,7 +612,7 @@ class VectorKey(DDLElement):
|
|
|
612
612
|
'documents', metadata,
|
|
613
613
|
Column('id', Integer, primary_key=True),
|
|
614
614
|
Column('embedding', VECTOR(768)),
|
|
615
|
-
|
|
615
|
+
singlestoredb_vector_key=[
|
|
616
616
|
VectorKey(
|
|
617
617
|
'embedding',
|
|
618
618
|
name='doc_vec_idx',
|
|
@@ -638,7 +638,7 @@ class VectorKey(DDLElement):
|
|
|
638
638
|
embedding = Column(VECTOR(1536))
|
|
639
639
|
|
|
640
640
|
__table_args__ = {
|
|
641
|
-
'
|
|
641
|
+
'singlestoredb_vector_key': [VectorKey('embedding')],
|
|
642
642
|
}
|
|
643
643
|
|
|
644
644
|
class Document(Base):
|
|
@@ -648,7 +648,7 @@ class VectorKey(DDLElement):
|
|
|
648
648
|
embedding = Column(VECTOR(768))
|
|
649
649
|
|
|
650
650
|
__table_args__ = {
|
|
651
|
-
'
|
|
651
|
+
'singlestoredb_vector_key': [
|
|
652
652
|
VectorKey(
|
|
653
653
|
'embedding',
|
|
654
654
|
name='doc_vec_idx',
|
|
@@ -796,7 +796,7 @@ class MultiValueIndex(DDLElement):
|
|
|
796
796
|
'articles', metadata,
|
|
797
797
|
Column('id', Integer, primary_key=True),
|
|
798
798
|
Column('tags', JSON),
|
|
799
|
-
|
|
799
|
+
singlestoredb_multi_value_index=[MultiValueIndex('tags')],
|
|
800
800
|
)
|
|
801
801
|
|
|
802
802
|
# Multiple columns
|
|
@@ -805,7 +805,7 @@ class MultiValueIndex(DDLElement):
|
|
|
805
805
|
Column('id', Integer, primary_key=True),
|
|
806
806
|
Column('tags', JSON),
|
|
807
807
|
Column('categories', JSON),
|
|
808
|
-
|
|
808
|
+
singlestoredb_multi_value_index=[
|
|
809
809
|
MultiValueIndex('tags', 'categories'),
|
|
810
810
|
],
|
|
811
811
|
)
|
|
@@ -827,7 +827,7 @@ class MultiValueIndex(DDLElement):
|
|
|
827
827
|
tags = Column(JSON)
|
|
828
828
|
|
|
829
829
|
__table_args__ = {
|
|
830
|
-
'
|
|
830
|
+
'singlestoredb_multi_value_index': [MultiValueIndex('tags')],
|
|
831
831
|
}
|
|
832
832
|
|
|
833
833
|
class Product(Base):
|
|
@@ -838,7 +838,7 @@ class MultiValueIndex(DDLElement):
|
|
|
838
838
|
categories = Column(JSON)
|
|
839
839
|
|
|
840
840
|
__table_args__ = {
|
|
841
|
-
'
|
|
841
|
+
'singlestoredb_multi_value_index': [
|
|
842
842
|
MultiValueIndex('tags', 'categories'),
|
|
843
843
|
],
|
|
844
844
|
}
|
|
@@ -979,7 +979,7 @@ class FullTextIndex(DDLElement):
|
|
|
979
979
|
'articles', metadata,
|
|
980
980
|
Column('id', Integer, primary_key=True),
|
|
981
981
|
Column('content', Text),
|
|
982
|
-
|
|
982
|
+
singlestoredb_full_text_index=[FullTextIndex('content')],
|
|
983
983
|
)
|
|
984
984
|
|
|
985
985
|
# Multiple columns with name
|
|
@@ -988,7 +988,7 @@ class FullTextIndex(DDLElement):
|
|
|
988
988
|
Column('id', Integer, primary_key=True),
|
|
989
989
|
Column('title', String(200)),
|
|
990
990
|
Column('body', Text),
|
|
991
|
-
|
|
991
|
+
singlestoredb_full_text_index=[
|
|
992
992
|
FullTextIndex('title', 'body', name='ft_doc_search'),
|
|
993
993
|
],
|
|
994
994
|
)
|
|
@@ -1010,7 +1010,7 @@ class FullTextIndex(DDLElement):
|
|
|
1010
1010
|
content = Column(Text)
|
|
1011
1011
|
|
|
1012
1012
|
__table_args__ = {
|
|
1013
|
-
'
|
|
1013
|
+
'singlestoredb_full_text_index': [FullTextIndex('content')],
|
|
1014
1014
|
}
|
|
1015
1015
|
|
|
1016
1016
|
class Document(Base):
|
|
@@ -1021,7 +1021,7 @@ class FullTextIndex(DDLElement):
|
|
|
1021
1021
|
body = Column(Text)
|
|
1022
1022
|
|
|
1023
1023
|
__table_args__ = {
|
|
1024
|
-
'
|
|
1024
|
+
'singlestoredb_full_text_index': [
|
|
1025
1025
|
FullTextIndex('title', 'body', name='ft_doc_search'),
|
|
1026
1026
|
],
|
|
1027
1027
|
}
|
{sqlalchemy_singlestoredb-1.2.1 → sqlalchemy_singlestoredb-1.2.3}/sqlalchemy_singlestoredb/dtypes.py
RENAMED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
5
|
import json
|
|
6
|
+
import struct
|
|
6
7
|
from typing import Any
|
|
7
8
|
from typing import Dict
|
|
8
9
|
from typing import List
|
|
@@ -81,7 +82,7 @@ class JSON(mybase.JSON):
|
|
|
81
82
|
tags = Column(JSON)
|
|
82
83
|
|
|
83
84
|
__table_args__ = {
|
|
84
|
-
'
|
|
85
|
+
'singlestoredb_multi_value_index': [MultiValueIndex('tags')],
|
|
85
86
|
}
|
|
86
87
|
|
|
87
88
|
"""
|
|
@@ -118,6 +119,36 @@ class JSON(mybase.JSON):
|
|
|
118
119
|
return None
|
|
119
120
|
|
|
120
121
|
|
|
122
|
+
_VECTOR_STRUCT_CODES = {
|
|
123
|
+
'F16': 'e', 'F32': 'f', 'F64': 'd',
|
|
124
|
+
'I8': 'b', 'I16': 'h', 'I32': 'i', 'I64': 'q',
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def _packed_vector_size(n_elems: int, elem_type: str) -> int:
|
|
129
|
+
"""Return the byte length of a packed VECTOR value."""
|
|
130
|
+
return n_elems * struct.calcsize(_VECTOR_STRUCT_CODES[elem_type.upper()])
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def _decode_json_vector(value: bytes, json_deserializer: Any) -> Optional[List[Any]]:
|
|
134
|
+
"""Return ``value`` decoded as a JSON array, or None if it is not one."""
|
|
135
|
+
# JSON allows leading whitespace (space, tab, line feed, carriage return).
|
|
136
|
+
if value.lstrip(b' \t\n\r')[:1] != b'[':
|
|
137
|
+
return None
|
|
138
|
+
try:
|
|
139
|
+
decoded = json_deserializer(value)
|
|
140
|
+
except ValueError:
|
|
141
|
+
return None
|
|
142
|
+
return decoded if isinstance(decoded, list) else None
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def _unpack_vector(value: bytes, elem_type: str) -> List[Any]:
|
|
146
|
+
"""Decode a little-endian packed VECTOR value into a list of numbers."""
|
|
147
|
+
code = _VECTOR_STRUCT_CODES[elem_type.upper()]
|
|
148
|
+
count = len(value) // struct.calcsize(code)
|
|
149
|
+
return list(struct.unpack(f'<{count}{code}', value))
|
|
150
|
+
|
|
151
|
+
|
|
121
152
|
class VECTOR(mybase.BLOB):
|
|
122
153
|
"""SingleStore VECTOR data type for storing fixed-dimension vectors.
|
|
123
154
|
|
|
@@ -229,14 +260,32 @@ class VECTOR(mybase.BLOB):
|
|
|
229
260
|
string_process = self._str_impl.result_processor(dialect, coltype)
|
|
230
261
|
json_deserializer = dialect._json_deserializer or json.loads
|
|
231
262
|
|
|
232
|
-
def process(value:
|
|
263
|
+
def process(value: Any) -> Any:
|
|
233
264
|
if value is None:
|
|
234
265
|
return None
|
|
266
|
+
if not isinstance(value, (str, bytes, bytearray)):
|
|
267
|
+
# Already decoded by the driver: singlestoredb returns VECTOR
|
|
268
|
+
# values as numpy arrays when numpy is installed.
|
|
269
|
+
return value
|
|
270
|
+
if isinstance(value, (bytes, bytearray)):
|
|
271
|
+
value = bytes(value)
|
|
272
|
+
decoded = _decode_json_vector(value, json_deserializer)
|
|
273
|
+
# A packed payload can also parse as JSON (an I8 vector packed
|
|
274
|
+
# as b'[1]' is [91, 49, 93]). Only trust the JSON reading when
|
|
275
|
+
# it has the column's element count, or when the value cannot
|
|
276
|
+
# be a packed payload of this column because its length differs.
|
|
277
|
+
if decoded is not None and (
|
|
278
|
+
len(decoded) == self.n_elems or
|
|
279
|
+
len(value) != _packed_vector_size(self.n_elems, self.elem_type)
|
|
280
|
+
):
|
|
281
|
+
return decoded
|
|
282
|
+
# Packed binary elements, as the HTTP Data API returns them.
|
|
283
|
+
return _unpack_vector(value, self.elem_type)
|
|
235
284
|
if string_process:
|
|
236
285
|
value = string_process(value)
|
|
237
286
|
if type(value) is dict or type(value) is list:
|
|
238
287
|
return value
|
|
239
|
-
return json_deserializer(value)
|
|
288
|
+
return json_deserializer(value)
|
|
240
289
|
|
|
241
290
|
return process
|
|
242
291
|
|
|
@@ -17,10 +17,12 @@ from urllib.parse import urlparse
|
|
|
17
17
|
|
|
18
18
|
import pytest
|
|
19
19
|
import singlestoredb
|
|
20
|
-
from sqlalchemy import create_engine
|
|
21
20
|
from sqlalchemy import text
|
|
22
21
|
from sqlalchemy.engine import Engine
|
|
23
22
|
|
|
23
|
+
from sqlalchemy_singlestoredb.tests.utils import connect_with_retry
|
|
24
|
+
from sqlalchemy_singlestoredb.tests.utils import create_test_engine
|
|
25
|
+
|
|
24
26
|
|
|
25
27
|
# Configure logger for test fixtures
|
|
26
28
|
logger = logging.getLogger(__name__)
|
|
@@ -303,7 +305,7 @@ def test_database(
|
|
|
303
305
|
logger.info('Database name: %s', db_name)
|
|
304
306
|
logger.info('Connection URL for creation: %s', db_connection_url)
|
|
305
307
|
logger.info('-' * 70)
|
|
306
|
-
with singlestoredb.connect
|
|
308
|
+
with connect_with_retry(singlestoredb.connect, db_connection_url) as conn:
|
|
307
309
|
with conn.cursor() as cur:
|
|
308
310
|
cur.execute(f'CREATE DATABASE IF NOT EXISTS {db_name}')
|
|
309
311
|
logger.info('Test database created successfully: %s', db_name)
|
|
@@ -322,7 +324,7 @@ def test_database(
|
|
|
322
324
|
logger.info('test_database: Cleaning up test database')
|
|
323
325
|
logger.info('Dropping database: %s', db_name)
|
|
324
326
|
logger.info('-' * 70)
|
|
325
|
-
with singlestoredb.connect
|
|
327
|
+
with connect_with_retry(singlestoredb.connect, db_connection_url) as conn:
|
|
326
328
|
with conn.cursor() as cur:
|
|
327
329
|
cur.execute(f'DROP DATABASE IF EXISTS {db_name}')
|
|
328
330
|
logger.info('Test database dropped successfully: %s', db_name)
|
|
@@ -346,7 +348,7 @@ def test_engine(
|
|
|
346
348
|
logger.info('Engine URL: %s', test_url)
|
|
347
349
|
logger.info('-' * 70)
|
|
348
350
|
|
|
349
|
-
engine =
|
|
351
|
+
engine = create_test_engine(test_url)
|
|
350
352
|
|
|
351
353
|
yield engine
|
|
352
354
|
|
|
@@ -368,7 +370,7 @@ def test_connection(
|
|
|
368
370
|
# Get the URL from the engine
|
|
369
371
|
url = test_engine.url.render_as_string(hide_password=False)
|
|
370
372
|
|
|
371
|
-
connection = singlestoredb.connect
|
|
373
|
+
connection = connect_with_retry(singlestoredb.connect, url)
|
|
372
374
|
|
|
373
375
|
yield connection
|
|
374
376
|
|
|
@@ -13,9 +13,12 @@ from sqlalchemy.engine import Engine
|
|
|
13
13
|
|
|
14
14
|
@pytest.fixture(scope='function')
|
|
15
15
|
def test_data_loaded(
|
|
16
|
-
test_engine: Engine, table_name_prefix: str,
|
|
16
|
+
test_engine: Engine, table_name_prefix: str, clean_tables: None,
|
|
17
17
|
) -> Generator[dict[str, str], None, None]:
|
|
18
18
|
"""Load test data from test.sql file with randomized table names."""
|
|
19
|
+
# clean_tables is requested here, not just by the test, so its snapshot
|
|
20
|
+
# of existing tables is taken before these are created; otherwise it
|
|
21
|
+
# counts them as pre-existing and drops nothing.
|
|
19
22
|
# Get path to test.sql file
|
|
20
23
|
sql_file = os.path.join(os.path.dirname(__file__), 'test.sql')
|
|
21
24
|
|
|
@@ -53,9 +56,10 @@ def test_data_loaded(
|
|
|
53
56
|
if stmt and not stmt.startswith('--'):
|
|
54
57
|
try:
|
|
55
58
|
conn.execute(text(stmt))
|
|
56
|
-
except Exception:
|
|
57
|
-
# Some statements might fail, continue
|
|
58
|
-
|
|
59
|
+
except Exception as exc:
|
|
60
|
+
# Some statements might fail, continue, but
|
|
61
|
+
# report it so a failure later has a cause
|
|
62
|
+
print(f'Statement failed: {stmt[:80]!r}: {exc!r}')
|
|
59
63
|
|
|
60
64
|
print(f'URL for test data: {test_engine.url.render_as_string(hide_password=False)}')
|
|
61
65
|
with test_engine.connect() as conn:
|