forked from duckdb/duckdb-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_adbc.py
More file actions
408 lines (337 loc) · 14.2 KB
/
test_adbc.py
File metadata and controls
408 lines (337 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
import datetime
import sys
from pathlib import Path
import adbc_driver_manager.dbapi
import numpy as np
import pyarrow
import pytest
import adbc_driver_duckdb.dbapi
xfail = pytest.mark.xfail
driver_path = adbc_driver_duckdb.driver_path()
@pytest.fixture
def duck_conn():
with adbc_driver_manager.dbapi.connect(driver=driver_path, entrypoint="duckdb_adbc_init") as conn:
yield conn
def example_table():
return pyarrow.table(
[
[1, 2, 3, 4],
["a", "b", None, "d"],
],
names=["ints", "strs"],
)
@xfail(sys.platform == "win32", reason="adbc-driver-manager.adbc_get_info() returns an empty dict on windows")
def test_connection_get_info(duck_conn):
assert duck_conn.adbc_get_info() != {}
def test_connection_get_table_types(duck_conn):
assert duck_conn.adbc_get_table_types() == []
with duck_conn.cursor() as cursor:
# Test Default Schema
cursor.execute("CREATE TABLE tableschema (ints BIGINT)")
assert duck_conn.adbc_get_table_types() == ["BASE TABLE"]
@xfail(
sys.platform == "win32", reason="adbc-driver-manager.adbc_get_objects() returns an invalid schema dict on windows"
)
def test_connection_get_objects(duck_conn):
with duck_conn.cursor() as cursor:
cursor.execute("CREATE TABLE getobjects (ints BIGINT PRIMARY KEY)")
depth_all = duck_conn.adbc_get_objects(depth="all").read_all()
assert sorted_get_objects(depth_all.to_pylist()) is not None
depth_tables = duck_conn.adbc_get_objects(depth="tables").read_all()
assert sorted_get_objects(depth_tables.to_pylist()) is not None
depth_db_schemas = duck_conn.adbc_get_objects(depth="db_schemas").read_all()
assert sorted_get_objects(depth_db_schemas.to_pylist()) is not None
depth_catalogs = duck_conn.adbc_get_objects(depth="catalogs").read_all()
assert sorted_get_objects(depth_catalogs.to_pylist()) is not None
# All result schemas should be the same
assert depth_all.schema == depth_tables.schema
assert depth_all.schema == depth_db_schemas.schema
assert depth_all.schema == depth_catalogs.schema
@xfail(
sys.platform == "win32", reason="adbc-driver-manager.adbc_get_objects() returns an invalid schema dict on windows"
)
def test_connection_get_objects_filters(duck_conn):
with duck_conn.cursor() as cursor:
cursor.execute("CREATE TABLE getobjects (ints BIGINT PRIMARY KEY)")
no_filter = duck_conn.adbc_get_objects(depth="all").read_all()
assert sorted_get_objects(no_filter.to_pylist()) is not None
column_filter = duck_conn.adbc_get_objects(depth="all", column_name_filter="notexist").read_all()
assert sorted_get_objects(column_filter.to_pylist()) is not None
table_name_filter = duck_conn.adbc_get_objects(depth="all", table_name_filter="notexist").read_all()
assert sorted_get_objects(table_name_filter.to_pylist()) is not None
db_schema_filter = duck_conn.adbc_get_objects(depth="all", db_schema_filter="notexist").read_all()
assert sorted_get_objects(db_schema_filter.to_pylist()) is not None
catalog_filter = duck_conn.adbc_get_objects(depth="all", catalog_filter="notexist").read_all()
assert catalog_filter.to_pylist() == []
assert no_filter.schema == column_filter.schema
assert no_filter.schema == table_name_filter.schema
assert no_filter.schema == db_schema_filter.schema
assert no_filter.schema == catalog_filter.schema
def test_commit(tmp_path):
db = Path(tmp_path) / "tmp.db"
if db.exists():
db.unlink()
table = example_table()
db_kwargs = {"path": f"{db}"}
# Start connection with auto-commit off
with adbc_driver_manager.dbapi.connect(
driver=driver_path,
entrypoint="duckdb_adbc_init",
db_kwargs=db_kwargs,
) as conn:
assert not conn._autocommit
with conn.cursor() as cur:
cur.adbc_ingest("ingest", table, "create")
# Check Data is not there
with adbc_driver_manager.dbapi.connect(
driver=driver_path,
entrypoint="duckdb_adbc_init",
db_kwargs=db_kwargs,
autocommit=True,
) as conn:
assert conn._autocommit
with conn.cursor() as cur:
# This errors because the table does not exist
with pytest.raises(
adbc_driver_manager._lib.InternalError,
match=r"Table with name ingest does not exist!",
):
cur.execute("SELECT count(*) from ingest")
cur.adbc_ingest("ingest", table, "create")
# This now works because we enabled autocommit
with (
adbc_driver_manager.dbapi.connect(
driver=driver_path,
entrypoint="duckdb_adbc_init",
db_kwargs=db_kwargs,
) as conn,
conn.cursor() as cur,
):
cur.execute("SELECT count(*) from ingest")
assert cur.fetch_arrow_table().to_pydict() == {"count_star()": [4]}
def test_connection_get_table_schema(duck_conn):
with duck_conn.cursor() as cursor:
# Test Default Schema
cursor.execute("CREATE TABLE tableschema (ints BIGINT)")
assert duck_conn.adbc_get_table_schema("tableschema") == pyarrow.schema(
[
("ints", "int64"),
]
)
# Test Given Schema
cursor.execute("CREATE SCHEMA test;")
cursor.execute("CREATE TABLE test.tableschema (test_ints BIGINT)")
assert duck_conn.adbc_get_table_schema("tableschema", db_schema_filter="test") == pyarrow.schema(
[
("test_ints", "int64"),
]
)
assert duck_conn.adbc_get_table_schema("tableschema") == pyarrow.schema(
[
("ints", "int64"),
]
)
# Test invalid catalog name
with pytest.raises(
adbc_driver_manager.InternalError,
match=r'Catalog "bla" does not exist',
):
duck_conn.adbc_get_table_schema("tableschema", catalog_filter="bla", db_schema_filter="test")
# Catalog and DB Schema name
assert duck_conn.adbc_get_table_schema(
"tableschema", catalog_filter="memory", db_schema_filter="test"
) == pyarrow.schema(
[
("test_ints", "int64"),
]
)
# DB Schema is inferred to be "main" if unspecified
assert duck_conn.adbc_get_table_schema("tableschema", catalog_filter="memory") == pyarrow.schema(
[
("ints", "int64"),
]
)
def test_prepared_statement(duck_conn):
with duck_conn.cursor() as cursor:
cursor.adbc_prepare("SELECT 1")
cursor.execute("SELECT 1")
assert cursor.fetchone() == (1,)
assert cursor.fetchone() is None
def test_statement_query(duck_conn):
with duck_conn.cursor() as cursor:
cursor.execute("SELECT 1")
assert cursor.fetchone() == (1,)
assert cursor.fetchone() is None
cursor.execute("SELECT 1 AS foo")
assert cursor.fetch_arrow_table().to_pylist() == [{"foo": 1}]
@xfail(sys.platform == "win32", reason="adbc-driver-manager returns an invalid table schema on windows")
def test_insertion(duck_conn):
table = example_table()
reader = table.to_reader()
with duck_conn.cursor() as cursor:
cursor.adbc_ingest("ingest", reader, "create")
cursor.execute("SELECT * FROM ingest")
assert cursor.fetch_arrow_table() == table
with duck_conn.cursor() as cursor:
cursor.adbc_ingest("ingest_table", table, "create")
cursor.execute("SELECT * FROM ingest")
assert cursor.fetch_arrow_table() == table
# Test Append
with duck_conn.cursor() as cursor:
with pytest.raises(
adbc_driver_manager.InternalError,
match=r'Table with name "ingest_table" already exists!',
):
cursor.adbc_ingest("ingest_table", table, "create")
cursor.adbc_ingest("ingest_table", table, "append")
cursor.execute("SELECT count(*) FROM ingest_table")
assert cursor.fetch_arrow_table().to_pydict() == {"count_star()": [8]}
@xfail(sys.platform == "win32", reason="adbc-driver-manager returns an invalid table schema on windows")
def test_read(duck_conn):
with duck_conn.cursor() as cursor:
filename = Path(__file__).parent / ".." / "data" / "category.csv"
cursor.execute(f"SELECT * FROM '{filename}'")
assert cursor.fetch_arrow_table().to_pydict() == {
"CATEGORY_ID": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
"NAME": [
"Action",
"Animation",
"Children",
"Classics",
"Comedy",
"Documentary",
"Drama",
"Family",
"Foreign",
"Games",
"Horror",
"Music",
"New",
"Sci-Fi",
"Sports",
"Travel",
],
"LAST_UPDATE": [
datetime.datetime(2006, 2, 15, 4, 46, 27),
datetime.datetime(2006, 2, 15, 4, 46, 27),
datetime.datetime(2006, 2, 15, 4, 46, 27),
datetime.datetime(2006, 2, 15, 4, 46, 27),
datetime.datetime(2006, 2, 15, 4, 46, 27),
datetime.datetime(2006, 2, 15, 4, 46, 27),
datetime.datetime(2006, 2, 15, 4, 46, 27),
datetime.datetime(2006, 2, 15, 4, 46, 27),
datetime.datetime(2006, 2, 15, 4, 46, 27),
datetime.datetime(2006, 2, 15, 4, 46, 27),
datetime.datetime(2006, 2, 15, 4, 46, 27),
datetime.datetime(2006, 2, 15, 4, 46, 27),
datetime.datetime(2006, 2, 15, 4, 46, 27),
datetime.datetime(2006, 2, 15, 4, 46, 27),
datetime.datetime(2006, 2, 15, 4, 46, 27),
datetime.datetime(2006, 2, 15, 4, 46, 27),
],
}
def test_large_chunk(tmp_path):
num_chunks = 3
chunk_size = 10_000
# Create data for each chunk
chunks_col1 = [pyarrow.array(np.random.randint(0, 100, chunk_size)) for _ in range(num_chunks)]
chunks_col2 = [pyarrow.array(np.random.rand(chunk_size)) for _ in range(num_chunks)]
chunks_col3 = [
pyarrow.array([f"str_{i}" for i in range(j * chunk_size, (j + 1) * chunk_size)]) for j in range(num_chunks)
]
# Create chunked arrays
col1 = pyarrow.chunked_array(chunks_col1)
col2 = pyarrow.chunked_array(chunks_col2)
col3 = pyarrow.chunked_array(chunks_col3)
# Create the table
table = pyarrow.table([col1, col2, col3], names=["ints", "floats", "strings"])
db = Path(tmp_path) / "tmp.db"
if db.exists():
db.unlink()
db_kwargs = {"path": f"{db}"}
with (
adbc_driver_manager.dbapi.connect(
driver=driver_path,
entrypoint="duckdb_adbc_init",
db_kwargs=db_kwargs,
autocommit=True,
) as conn,
conn.cursor() as cur,
):
cur.adbc_ingest("ingest", table, "create")
cur.execute("SELECT count(*) from ingest")
assert cur.fetch_arrow_table().to_pydict() == {"count_star()": [30_000]}
def test_dictionary_data(tmp_path):
data = ["apple", "banana", "apple", "orange", "banana", "banana"]
dict_type = pyarrow.dictionary(index_type=pyarrow.int32(), value_type=pyarrow.string())
dict_array = pyarrow.array(data, type=dict_type)
# Wrap in a table
table = pyarrow.table({"fruits": dict_array})
db = Path(tmp_path) / "tmp.db"
if db.exists():
db.unlink()
db_kwargs = {"path": f"{db}"}
with (
adbc_driver_manager.dbapi.connect(
driver=driver_path,
entrypoint="duckdb_adbc_init",
db_kwargs=db_kwargs,
autocommit=True,
) as conn,
conn.cursor() as cur,
):
cur.adbc_ingest("ingest", table, "create")
cur.execute("from ingest")
assert cur.fetch_arrow_table().to_pydict() == {
"fruits": ["apple", "banana", "apple", "orange", "banana", "banana"]
}
def test_ree_data(tmp_path):
run_ends = pyarrow.array([3, 5, 6], type=pyarrow.int32()) # positions: [0-2], [3-4], [5]
values = pyarrow.array(["apple", "banana", "orange"], type=pyarrow.string())
ree_array = pyarrow.RunEndEncodedArray.from_arrays(run_ends, values)
table = pyarrow.table({"fruits": ree_array})
db = Path(tmp_path) / "tmp.db"
if db.exists():
db.unlink()
db_kwargs = {"path": f"{db}"}
with (
adbc_driver_manager.dbapi.connect(
driver=driver_path,
entrypoint="duckdb_adbc_init",
db_kwargs=db_kwargs,
autocommit=True,
) as conn,
conn.cursor() as cur,
):
cur.adbc_ingest("ingest", table, "create")
cur.execute("from ingest")
assert cur.fetch_arrow_table().to_pydict() == {
"fruits": ["apple", "apple", "apple", "banana", "banana", "orange"]
}
def sorted_get_objects(catalogs):
res = []
for catalog in sorted(catalogs, key=lambda cat: cat["catalog_name"]):
new_catalog = {
"catalog_name": catalog["catalog_name"],
"catalog_db_schemas": [],
}
for db_schema in sorted(catalog["catalog_db_schemas"] or [], key=lambda sch: sch["db_schema_name"]):
new_db_schema = {
"db_schema_name": db_schema["db_schema_name"],
"db_schema_tables": [],
}
for table in sorted(db_schema["db_schema_tables"] or [], key=lambda tab: tab["table_name"]):
new_table = {
"table_name": table["table_name"],
"table_type": table["table_type"],
"table_columns": [],
"table_constraints": [],
}
for column in sorted(table["table_columns"] or [], key=lambda col: col["ordinal_position"]):
new_table["table_columns"].append(column)
for constraint in sorted(table["table_constraints"] or [], key=lambda con: con["constraint_name"]):
new_table["table_constraints"].append(constraint)
new_db_schema["db_schema_tables"].append(new_table)
new_catalog["catalog_db_schemas"].append(new_db_schema)
res.append(new_catalog)
return res