/
gensql.py
executable file
·186 lines (145 loc) · 5.26 KB
/
gensql.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
#!/usr/bin/env python3
from dataclasses import dataclass
import dataclasses
import difflib
import sys
from typing import List, Optional
import pymonetdb
NBIGBLOBS = 10_000
SETUP_FILE = 'queries/_setup.sql'
TALL_FILE = 'queries/tall_%s.sql'
@dataclass
class ColumnDef:
typename: str
gensql: str
hitsql: str
limit: Optional[int]
colname: str = 'banana'
nullcount: int = -1
hitcount: int = -1
def __post_init__(self):
if self.colname == 'banana':
self.colname = self.typename + '_col'
COLUMN_DEFINITIONS: List[ColumnDef] = []
def gen(typename, gensql, hitsql, limit=None):
coldef = ColumnDef(typename, gensql, hitsql, limit)
COLUMN_DEFINITIONS.append(coldef)
gen("tinyint", "CAST(i % 100 AS TINYINT)", "%s = 42")
gen("smallint", "CAST(i % 10000 AS SMALLINT)", "%s = 42")
gen("int", "i", "%s = 42")
gen("bigint", "CAST(i AS BIGINT)", "%s = 42")
gen("hugeint", "CAST(i AS HUGEINT)", "%s = 42")
gen("real", "CAST(i AS REAL)", "%s = 42")
gen("double", "CAST(i AS DOUBLE)", "%s = 42")
gen("decimal", "CAST(i AS DEC(8,3))", "%s = 42")
gen("boolean", "i % 2 = 0", "%s")
gen("text", "CAST('xyz' || i AS VARCHAR(20))", "LENGTH(%s) > 4")
gen(
"uuid",
"CASE WHEN i IS NULL THEN NULL ELSE CAST('12345678-1234-5678-1234-567812345678' AS UUID) END",
"%s = UUID '12345678-1234-5678-1234-567812345678'"
)
gen("blob",
f"(SELECT b FROM blobs WHERE idx < {NBIGBLOBS} AND nr = i % (SELECT COUNT(*) FROM blobs))",
"LENGTH(%s) > 4",
NBIGBLOBS)
gen("date", "DATE '2015-02-14' + i * INTERVAL '1' DAY", "EXTRACT(DAY FROM %s) = 14")
gen("time", "TIME '20:50:55' + i * INTERVAL '1' MINUTE",
"EXTRACT(MINUTE FROM %s) = 42")
gen("timetz", "TIMETZ '20:50:55+01:00' + i * INTERVAL '1' MINUTE",
"EXTRACT(MINUTE FROM %s) = 42")
gen("timestamp", "TIMESTAMP '2015-02-14 20:50:55' + i * INTERVAL '1' MINUTE",
"EXTRACT(MINUTE FROM %s) = 42")
gen("timestamptz", "TIMESTAMPTZ '2015-02-14 20:50:55+01:00' + i * INTERVAL '1' MINUTE",
"EXTRACT(MINUTE FROM %s) = 42")
gen("month", "i * INTERVAL '1' MONTH", "%s = 42 * INTERVAL '1' MONTH")
gen("sec", "i * INTERVAL '1' SECOND", "%s = 42 * INTERVAL '1' SECOND")
gen("day", "i * INTERVAL '1' DAY", "%s = 42 * INTERVAL '1' DAY")
def write_file(filename: str, new_content: str, overwrite: bool):
print(f"FILE {filename}")
try:
old_content = open(filename).read()
except FileNotFoundError:
old_content = None
if new_content == old_content:
print(" -> unchanged")
elif old_content is None:
print(" -> new")
elif overwrite:
print(" -> overwrite")
else:
old_lines = old_content.splitlines()
new_lines = new_content.splitlines()
for line in difflib.unified_diff(old_lines, new_lines, 'Old', 'New', lineterm=''):
print(line)
return False
with open(filename, 'w') as f:
f.write(new_content)
return True
def gen_setup(setup_file) -> str:
setup_code = open(setup_file).readlines()
start_line = None
end_line = None
for n, line in enumerate(setup_code):
line = line.strip()
if line == 'i':
start_line = n + 1
elif line == 'FROM nums;':
end_line = n
setup_code[start_line: end_line] = [
f" , {coldef.gensql:<50} AS {coldef.colname}" + "\n"
for coldef in COLUMN_DEFINITIONS
]
return "".join(setup_code)
def set_counts(conn: pymonetdb.Connection, setup_code: str):
print("EXECUTING SETUP CODE")
cursor = conn.cursor()
cursor.execute(setup_code)
def run_query(q):
print(f"EXECUTING {q}: ", end="", flush=True)
cursor.execute(q)
res = cursor.fetchone()[0]
print(res)
return res
for coldef in COLUMN_DEFINITIONS:
and_limit = f" AND idx < {coldef.limit}" if coldef.limit is not None else ""
nulls = run_query(f"SELECT COUNT(*) FROM tall WHERE {coldef.colname} IS NULL{and_limit}")
where = coldef.hitsql % coldef.colname
hits = run_query(f"SELECT COUNT(*) FROM tall WHERE {where}{and_limit}")
coldef.nullcount = nulls * 10
coldef.hitcount = hits * 10
TALL_TEMPLATE = """\
-- Result set with 10 %(typename)s columns
-- @EXPECTED=%(expected)s@ @NULLCOUNT=%(nullcount)s@ @HITCOUNT=%(hitcount)s@
SELECT
%(colname)s AS col0,
%(colname)s AS col1,
%(colname)s AS col2,
%(colname)s AS col3,
%(colname)s AS col4,
%(colname)s AS col5,
%(colname)s AS col6,
%(colname)s AS col7,
%(colname)s AS col8,
%(colname)s AS col9
FROM tall%(where_clause)s;
"""
def gen_tall(coldef: ColumnDef) -> str:
values = dataclasses.asdict(coldef)
expected = coldef.limit if coldef.limit is not None else 100_000
where_clause = f"\nWHERE idx < {coldef.limit}" if coldef.limit is not None else ""
values['expected'] = expected
values['where_clause'] = where_clause
return TALL_TEMPLATE % values
if __name__ == "__main__":
ok = True
overwrite = ("-w" in sys.argv)
setup_code = gen_setup(SETUP_FILE)
ok &= write_file(SETUP_FILE, setup_code, overwrite)
conn = pymonetdb.connect('foo', autocommit=True)
set_counts(conn, setup_code)
for coldef in COLUMN_DEFINITIONS:
sql = gen_tall(coldef)
ok &= write_file(TALL_FILE % coldef.typename, sql, overwrite)
if not ok:
sys.exit(1)