-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtest.py
276 lines (215 loc) · 7.41 KB
/
test.py
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
import itertools
import sqlite3
import subprocess
import uuid
from contextlib import contextmanager
from typing import Tuple
import apsw
import boto3
import pytest
import s3fs
from fsspec.implementations.local import LocalFileSystem
import s3sqlite
# import logging
# log = logging.getLogger("s3sqlite")
# log.setLevel(logging.DEBUG)
PAGE_SIZES = [512, 1024, 2048, 4096, 8192, 16384, 32768, 65536]
JOURNAL_MODES = ["DELETE", "TRUNCATE", "PERSIST", "MEMORY", "OFF"]
PAGE_JOURNAL_MIX = itertools.product(PAGE_SIZES, JOURNAL_MODES)
QUERIES = [
"""
-- Get the total sales per country.
SELECT BillingCountry, sum(total) AS Total FROM Invoice
GROUP BY BillingCountry
ORDER BY Total desc
""",
"""
-- Get the Invoice Total, Customer name, Country and Sale Agent name for all invoices and customers.
SELECT i.`Total`, c.FirstName || " " || c.LastName AS CustomerName, c.Country, e.FirstName || " " || e.LastName AS SalesAgent FROM Invoice i
JOIN Customer c ON c.CustomerId = i.CustomerId
JOIN Employee e ON e.EmployeeId = c.SupportRepId
ORDER BY SalesAgent;
""",
"""
-- Get the invoices associated with each sales agent. The resultant table should include the Sales Agent's full name.
SELECT i.InvoiceId, i.InvoiceDate, i.`Total`, i.CustomerId, e.FirstName || " " || e.LastName AS SalesAgent FROM Invoice i, Customer c, Employee e
WHERE c.CustomerId = i.CustomerId
AND e.EmployeeId = c.SupportRepId
ORDER BY SalesAgent == "Sales Support Agent";
""",
"""
-- Which sales agent made the most in sales over all?
SELECT "Sales Winner", max("Total") AS "Total" FROM (
SELECT e.firstName || " " || e.lastName AS "Sales Winner", sum(i.total) AS "Total" FROM Invoice AS i
JOIN customer AS c ON c.customerid = i.customerid
JOIN employee AS e ON e.employeeid = c.supportrepid
GROUP BY e.Employeeid
)
""",
]
dbname = "chinook.sqlite3"
@pytest.fixture(autouse=True, scope="session")
def minio():
proc = subprocess.Popen(
[
"docker",
"run",
"--rm",
"--name",
"s3sqlite-minio",
"-p",
"9000:9000",
"-p",
"9001:9001",
"-e",
"MINIO_ROOT_USER=AKIAIDIDIDIDIDIDIDID",
"-e",
"MINIO_ROOT_PASSWORD=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"quay.io/minio/minio",
"server",
"/data",
"--console-address",
":9001",
],
text=True,
)
yield proc
proc.terminate()
@pytest.fixture
def bucket():
session = boto3.Session(
aws_access_key_id="AKIAIDIDIDIDIDIDIDID",
aws_secret_access_key="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
region_name="us-east-1",
)
s3 = session.resource("s3", endpoint_url="http://localhost:9000/")
name = f"s3vfs-{str(uuid.uuid4())}"
bucket = s3.create_bucket(Bucket=name)
yield name
bucket.objects.all().delete()
bucket.delete()
@pytest.fixture
def s3_data():
return dict(
key="AKIAIDIDIDIDIDIDIDID",
secret="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
endpoint_url="http://localhost:9000/",
)
# @pytest.fixture(params=BLOCK_SIZES)
# @pytest.fixture
# def s3vfs(s3_data):
# s3 = s3fs.S3FileSystem(
# key=s3_data["key"],
# secret=s3_data["secret"],
# client_kwargs={"endpoint_url": s3_data["endpoint_url"]},
# )
# yield s3sqlite.S3VFS(name="s3-vfs", fs=s3)
@pytest.fixture
def s3vfs(s3_data):
s3 = s3fs.S3FileSystem(
key=s3_data["key"],
secret=s3_data["secret"],
client_kwargs={"endpoint_url": s3_data["endpoint_url"]},
)
yield s3sqlite.S3VFS(
name="s3-vfs",
fs=s3,
file_kwargs={"cache_type": "bytes", "cache_size": 100_000_000},
)
@pytest.fixture
def local_fs():
fs = LocalFileSystem()
yield fs
@pytest.fixture
def localvfs(local_fs):
return s3sqlite.S3VFS(name="local-vfs", fs=local_fs)
@contextmanager
def transaction(conn):
conn.execute("BEGIN;")
try:
yield conn
except:
conn.execute("ROLLBACK;")
raise
else:
conn.execute("COMMIT;")
def set_pragmas(conn, page_size, journal_mode):
sqls = [
f"PRAGMA journal_mode = {journal_mode};",
f"PRAGMA page_size = {page_size};",
"VACUUM;",
]
for sql in sqls:
print(f"Running: {sql}")
conn.execute(sql)
def create_db(conn):
with open("chinook.sql") as f:
sql = f.read()
# with transaction(conn):
conn.executescript(sql)
@pytest.fixture(params=itertools.product(PAGE_SIZES, JOURNAL_MODES))
def get_db(request) -> Tuple[str, sqlite3.Connection]:
# if dbname in os.listdir():
# os.system(f"rm -rf {dbname}*")
conn = sqlite3.connect(dbname, isolation_level=None)
set_pragmas(conn, page_size=request.param[0], journal_mode=request.param[1])
# create_db(conn)
assert conn.execute("PRAGMA page_size;").fetchone()[0] == request.param[0]
assert (
conn.execute("PRAGMA journal_mode;").fetchone()[0].lower()
== request.param[1].lower()
)
return dbname, conn
# I haven't been able to make this work as I want for DBs in WAL mode. For now
# I'll just document that the DB needs to be set to a different journal mode
# before uploading it to S3. This functions will test a database that is in WAL
# mode and then changed to a different mode. This is because I spect the typica
# workflow to start with a DB in WAL mode to load data in it, then the
# journal_mode gets changed to something else for uploading:
#
# set page size -> vacuum -> set WAL -> truncate WAL -> change journal model before uploadig
def set_wal_pragmas(conn, page_size, journal_mode):
# Page size can't be changed after setting WAL mode, so we need to do
# it before.
sqls = [
f"PRAGMA page_size = {page_size};",
"VACUUM;",
"PRAGMA journal_mode = WAL;",
"PRAGMA wal_checkpoint(truncate);",
f"PRAGMA journal_mode = {journal_mode};",
]
for sql in sqls:
print(f"Running: {sql}")
conn.execute(sql)
@pytest.fixture(params=itertools.product(PAGE_SIZES, JOURNAL_MODES))
def get_db_wal(request) -> Tuple[str, sqlite3.Connection]:
conn = sqlite3.connect(dbname, isolation_level=None)
set_wal_pragmas(conn, page_size=request.param[0], journal_mode=request.param[1])
assert conn.execute("PRAGMA page_size;").fetchone()[0] == request.param[0]
assert (
conn.execute("PRAGMA journal_mode;").fetchone()[0].lower()
== request.param[1].lower()
)
return dbname, conn
@pytest.mark.parametrize("query", QUERIES)
def test_s3vfs_query_wal(bucket, s3vfs, get_db_wal, query):
key_prefix = f"{bucket}/{dbname}"
s3vfs.upload_file(get_db_wal[0], dest=key_prefix)
# Create a database and query it
with apsw.Connection(
key_prefix, vfs=s3vfs.name, flags=apsw.SQLITE_OPEN_READONLY
) as conn:
local_c = get_db_wal[1].execute(query)
c = conn.execute(query)
assert c.fetchall() == local_c.fetchall()
@pytest.mark.parametrize("query", QUERIES)
def test_s3vfs_query(bucket, s3vfs, get_db, query):
key_prefix = f"{bucket}/{dbname}"
s3vfs.upload_file(get_db[0], dest=key_prefix)
# Create a database and query it
with apsw.Connection(
key_prefix, vfs=s3vfs.name, flags=apsw.SQLITE_OPEN_READONLY
) as conn:
local_c = get_db[1].execute(query)
c = conn.execute(query)
assert c.fetchall() == local_c.fetchall()