-
Notifications
You must be signed in to change notification settings - Fork 1
/
check-invariant.py
199 lines (172 loc) · 8.62 KB
/
check-invariant.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
import time
import os
import signal
import duckdb
import pathlib
import subprocess
import random
import tempfile
import multiprocessing
import multiprocessing.shared_memory
scale_factor = 1
datadir = f'/Users/hannes/source/tpch/gen/invariant-sf{scale_factor}'
db_file = f'tpch-invariant-sf{scale_factor}.duckdb'
diffdir = 'invariant-checking' #tempfile.mkdtemp()
refdir = 'reference-tables' # TODO
ext = ''
shm_size = 4
shm_name = "shm_last_refresh"
refresh_count = 4000
schema = '''
CREATE TABLE ORDERS ( O_ORDERKEY INTEGER NOT NULL,
O_CUSTKEY INTEGER NOT NULL,
O_ORDERSTATUS VARCHAR(1) NOT NULL,
O_TOTALPRICE DECIMAL(15,2) NOT NULL,
O_ORDERDATE DATE NOT NULL,
O_ORDERPRIORITY VARCHAR(15) NOT NULL,
O_CLERK VARCHAR(15) NOT NULL,
O_SHIPPRIORITY INTEGER NOT NULL,
O_COMMENT VARCHAR(79) NOT NULL,
gunk varchar(1));
CREATE TABLE LINEITEM ( L_ORDERKEY INTEGER NOT NULL,
L_PARTKEY INTEGER NOT NULL,
L_SUPPKEY INTEGER NOT NULL,
L_LINENUMBER INTEGER NOT NULL,
L_QUANTITY DECIMAL(15,2) NOT NULL,
L_EXTENDEDPRICE DECIMAL(15,2) NOT NULL,
L_DISCOUNT DECIMAL(15,2) NOT NULL,
L_TAX DECIMAL(15,2) NOT NULL,
L_RETURNFLAG VARCHAR(1) NOT NULL,
L_LINESTATUS VARCHAR(1) NOT NULL,
L_SHIPDATE DATE NOT NULL,
L_COMMITDATE DATE NOT NULL,
L_RECEIPTDATE DATE NOT NULL,
L_SHIPINSTRUCT VARCHAR(25) NOT NULL,
L_SHIPMODE VARCHAR(10) NOT NULL,
L_COMMENT VARCHAR(44) NOT NULL,
gunk varchar(1));
'''
schema_with_pk = '''
CREATE TABLE ORDERS ( O_ORDERKEY INTEGER NOT NULL,
O_CUSTKEY INTEGER NOT NULL,
O_ORDERSTATUS VARCHAR(1) NOT NULL,
O_TOTALPRICE DECIMAL(15,2) NOT NULL,
O_ORDERDATE DATE NOT NULL,
O_ORDERPRIORITY VARCHAR(15) NOT NULL,
O_CLERK VARCHAR(15) NOT NULL,
O_SHIPPRIORITY INTEGER NOT NULL,
O_COMMENT VARCHAR(79) NOT NULL,
gunk varchar(1),
primary key (o_orderkey));
CREATE TABLE LINEITEM ( L_ORDERKEY INTEGER NOT NULL,
L_PARTKEY INTEGER NOT NULL,
L_SUPPKEY INTEGER NOT NULL,
L_LINENUMBER INTEGER NOT NULL,
L_QUANTITY DECIMAL(15,2) NOT NULL,
L_EXTENDEDPRICE DECIMAL(15,2) NOT NULL,
L_DISCOUNT DECIMAL(15,2) NOT NULL,
L_TAX DECIMAL(15,2) NOT NULL,
L_RETURNFLAG VARCHAR(1) NOT NULL,
L_LINESTATUS VARCHAR(1) NOT NULL,
L_SHIPDATE DATE NOT NULL,
L_COMMITDATE DATE NOT NULL,
L_RECEIPTDATE DATE NOT NULL,
L_SHIPINSTRUCT VARCHAR(25) NOT NULL,
L_SHIPMODE VARCHAR(10) NOT NULL,
L_COMMENT VARCHAR(44) NOT NULL,
gunk varchar(1),
primary key (L_ORDERKEY, L_LINENUMBER));
'''
def export(con, dir, n):
con.execute(f"COPY (SELECT * FROM lineitem ORDER BY l_orderkey, l_linenumber) TO '{dir}/lineitem.tbl.{n}' (FORMAT CSV, DELIMITER '|', HEADER FALSE)")
con.execute(f"COPY (SELECT * FROM orders ORDER BY o_orderkey) TO '{dir}/order.tbl.{n}' (FORMAT CSV, DELIMITER '|', HEADER FALSE)")
def refresh(con, n):
con.execute("BEGIN TRANSACTION")
# print(n)
# lineitem = f"{datadir}/lineitem.tbl.u{n}{ext}"
# orders = f"{datadir}/orders.tbl.u{n}{ext}"
# con.execute(f"COPY lineitem FROM '{lineitem}' (FORMAT CSV, HEADER FALSE, DELIMITER '|')")
# con.execute(f"COPY orders FROM '{orders}' (FORMAT CSV, HEADER FALSE, DELIMITER '|')")
# delete = f"{datadir}/delete.{n}{ext}"
# con.execute(f"CREATE TEMPORARY TABLE deletes (pk INTEGER, gunk varchar(1))")
# con.execute(f"COPY deletes FROM '{delete}' (FORMAT CSV, HEADER FALSE, DELIMITER '|')")
# con.execute("DELETE FROM orders WHERE o_orderkey IN (SELECT pk FROM deletes)")
# con.execute("DELETE FROM lineitem WHERE l_orderkey IN (SELECT pk FROM deletes)")
# con.execute("DROP TABLE deletes")
con.execute(f"INSERT INTO lineitem SELECT * EXCLUDE refresh_set FROM '{datadir}/lineitem_refresh.parquet' WHERE refresh_set={n}")
con.execute(f"INSERT INTO orders SELECT * EXCLUDE refresh_set FROM '{datadir}/orders_refresh.parquet' WHERE refresh_set={n}")
con.execute(f"CREATE TEMPORARY TABLE deletes (pk INTEGER)")
con.execute(f"INSERT INTO deletes SELECT * EXCLUDE refresh_set FROM '{datadir}/delete_refresh.parquet' WHERE refresh_set={n}")
con.execute("DELETE FROM orders WHERE o_orderkey IN (SELECT pk FROM deletes)")
con.execute("DELETE FROM lineitem WHERE l_orderkey IN (SELECT pk FROM deletes)")
con.execute("DROP TABLE deletes")
con.execute("DELETE FROM refresh")
con.execute(f"INSERT INTO refresh VALUES ({n})")
con.execute("COMMIT")
def diff(f1, f2):
res = subprocess.call(f'cmp -s {f1} {f2}', shell=True)
if res > 0:
raise ValueError(f'Found diff in {f1} <> {f2}!')
# child process, runs updates, gets killed every now and then
def sub():
shm = multiprocessing.shared_memory.SharedMemory(shm_name)
con = duckdb.connect(db_file)
con.execute('SET enable_progress_bar = false')
con.execute('SET checkpoint_threshold = "100MB"')
while True:
last_refresh = con.execute("SELECT last_refresh FROM refresh").fetchone()[0]
shm.buf[:shm_size] = last_refresh.to_bytes(shm_size, 'big')
next_refresh = last_refresh + 1
if next_refresh > refresh_count:
break
refresh(con, next_refresh)
con.close()
shm.unlink()
# setup, load, control, main process
if __name__ == '__main__':
if os.path.exists(db_file):
os.remove(db_file)
con = duckdb.connect(db_file)
con.begin()
con.execute(schema)
for t in ['lineitem', 'orders']:
con.execute(f"COPY {t} FROM '{datadir}/{t}.tbl.parquet'")
con.execute("CREATE TABLE refresh(last_refresh INTEGER)")
con.execute("INSERT INTO refresh VALUES (0)")
last_refresh = 0
# export(con, diffdir, last_refresh)
# diff(f'{refdir}/order.tbl.{last_refresh}', f'{diffdir}/order.tbl.{last_refresh}')
# diff(f'{refdir}/lineitem.tbl.{last_refresh}', f'{diffdir}/lineitem.tbl.{last_refresh}')
con.commit()
con.close()
# we use shared memory to report the progress back, we need to adjust kill interval based on progress
shm = multiprocessing.shared_memory.SharedMemory(name=shm_name, create=True, size=shm_size)
shm.buf[:shm_size] = last_refresh.to_bytes(shm_size, 'big')
while True:
p = multiprocessing.Process(target=sub)
p.start()
# we sleep until a random number of refreshes has completed
refreshes_this_round = random.randint(0,10)
while True:
time.sleep(0.01)
last_reported_refresh = int.from_bytes(shm.buf[:shm_size], 'big')
refresh_progress = last_reported_refresh - last_refresh
if refresh_progress > refreshes_this_round or last_reported_refresh == refresh_count:
last_refresh = last_reported_refresh
break
# if the process has exited cleanly we are done
if p.exitcode == 0 or last_refresh == refresh_count:
break
# if not we kill it now
print(f'💀 {last_refresh}')
os.kill(p.pid, signal.SIGKILL)
p.join()
print('🏁')
time.sleep(1)
con = duckdb.connect(db_file, read_only=True)
last_refresh = refresh_count
export(con, diffdir, last_refresh)
con.close()
diff(f'{refdir}/order.tbl.{last_refresh}', f'{diffdir}/order.tbl.{last_refresh}')
diff(f'{refdir}/lineitem.tbl.{last_refresh}', f'{diffdir}/lineitem.tbl.{last_refresh}')
print('✅')