-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.py
50 lines (42 loc) · 1.23 KB
/
util.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
from typing import Callable, Dict, Sequence
from sqlfmt.api import Mode, format_string
from sqlglot import parse_one
from sqlglot.expressions import Select
from sqlglot.optimizer import RULES, optimize
RULE_MAPPING: Dict[str, Callable] = {rule.__name__: rule for rule in RULES}
SAMPLE_QUERY: str = """WITH users AS (
SELECT *
FROM users_table),
orders AS (
SELECT *
FROM orders_table),
combined AS (
SELECT users.id, users.name, orders.order_id, orders.total
FROM users
JOIN orders ON users.id = orders.user_id)
SELECT combined.id, combined.name, combined.order_id, combined.total
FROM combined
"""
def _generate_ast(query: str) -> Select:
"""
Generate an AST from a query.
"""
ast = parse_one(query)
return ast
def apply_optimizations(
query: str, rules: Sequence[Callable] = RULES, remove_ctes: bool = False
) -> Select:
"""
Apply optimizations to an AST.
"""
ast = _generate_ast(query)
if remove_ctes:
return optimize(ast, rules=rules)
else:
return optimize(ast, rules=rules, leave_tables_isolated=True)
def format_sql_with_sqlfmt(query: str) -> str:
"""
Format a query using sqlfmt.
"""
mode = Mode()
return format_string(query, mode)