-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpullup_sql.py
More file actions
57 lines (46 loc) · 1.43 KB
/
Copy pathpullup_sql.py
File metadata and controls
57 lines (46 loc) · 1.43 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
import sys
import json
import duckdb
import datetime
import re
TOTAL_TIME_RE = re.compile(r"Total Time:\s*([0-9.]+)s", re.IGNORECASE)
def main():
if len(sys.argv) != 4:
raise ValueError("Usage: pullup_sql.py <db_file> <udf_file> <sql>")
db_file = sys.argv[1]
udf_content = sys.argv[2]
sql = sys.argv[3]
# Connect to the database.
con = duckdb.connect(db_file)
# Load UDFs.
exec(udf_content, {
'db_conn': con,
'date': datetime.date,
'time': datetime.time,
'VARCHAR': duckdb.typing.VARCHAR,
'INTEGER': duckdb.typing.INTEGER,
'DOUBLE': duckdb.typing.DOUBLE,
'TIMESTAMP': duckdb.typing.TIMESTAMP,
'DATE': duckdb.typing.DATE,
'TIME': duckdb.typing.TIME,
'BOOLEAN': duckdb.typing.BOOLEAN,
'HUGEINT': duckdb.typing.HUGEINT,
'FLOAT': duckdb.typing.FLOAT
})
# Disable filter pushdown.
con.execute('set disabled_optimizers = "filter_pushdown"')
# Run EXPLAIN ANALYZE.
res = con.execute(f'EXPLAIN ANALYZE {sql}')
rows = res.fetchall()
texts = []
for row in rows:
texts.append(str(row[1]))
raw = "\n".join(texts)
# Extract execution time.
m = TOTAL_TIME_RE.search(raw)
t = float(m.group(1))
# Print the result.
result = {"time": t, "plan": raw, "success": True}
print(json.dumps(result))
if __name__ == "__main__":
main()