File: emit_tests.py

package info (click to toggle)
duckdb 1.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 299,196 kB
  • sloc: cpp: 865,414; ansic: 57,292; python: 18,871; sql: 12,663; lisp: 11,751; yacc: 7,412; lex: 1,682; sh: 747; makefile: 558
file content (246 lines) | stat: -rw-r--r-- 8,361 bytes parent folder | download | duplicates (3)
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
# scripts/metrics/emit_tests.py
from __future__ import annotations

from pathlib import Path
from typing import Dict, List

from .paths import REPO_ROOT, path_from_duckdb, format_file
from .writer import IndentedFileWriter


def _write_statement(f, statement_type, statement):
    f.write(f"statement {statement_type}\n")
    f.write(statement + "\n\n")


def _write_query(f, options, query):
    f.write(f"query {options}\n")
    f.write(query + "\n")
    f.write("----\n")


def _write_default_query(f):
    query = "SELECT unnest(['Maia', 'Thijs', 'Mark', 'Hannes', 'Tom', 'Max', 'Carlo', 'Sam', 'Tania']) AS names ORDER BY random();"
    _write_statement(f, "ok", query)
    _write_statement(f, "ok", "PRAGMA disable_profiling;")


def _write_get_custom_profiling_settings(f):
    query = """
SELECT unnest(res) FROM (
    SELECT current_setting('custom_profiling_settings') AS raw_setting,
    raw_setting.trim('{}') AS setting,
    string_split(setting, ', ') AS res
) ORDER BY ALL;
""".strip()
    _write_query(f, "I", query)


def _write_custom_profiling_optimizer(f):
    _write_statement(f, "ok", "PRAGMA custom_profiling_settings='{\"ALL_OPTIMIZERS\": \"true\"}';")

    _write_default_query(f)

    query = """
SELECT * FROM (
    SELECT unnest(res) str FROM (
        SELECT current_setting('custom_profiling_settings') as raw_setting,
        raw_setting.trim('{}') AS setting,
        string_split(setting, ', ') AS res
    )
) WHERE '"true"' NOT in str
ORDER BY ALL
""".strip()
    _write_query(f, "I", query)
    f.write("\n")

    _write_statement(f, "ok", "PRAGMA custom_profiling_settings='{}'")
    _write_default_query(f)

    _write_get_custom_profiling_settings(f)
    f.write("(empty)\n\n")

    _write_statement(f, "ok", "PRAGMA custom_profiling_settings='{\"OPTIMIZER_JOIN_ORDER\": \"true\"}'")
    _write_default_query(f)

    _write_get_custom_profiling_settings(f)
    f.write("\"OPTIMIZER_JOIN_ORDER\": \"true\"\n\n")

    _write_statement(
        f, "ok", "CREATE OR REPLACE TABLE metrics_output AS SELECT * FROM '__TEST_DIR__/profiling_output.json';"
    )

    query = """
SELECT
    CASE WHEN optimizer_join_order > 0 THEN 'true'
     ELSE 'false' END
FROM metrics_output;
""".strip()
    _write_query(f, "I", query)
    f.write("true\n\n")

    _write_statement(f, "ok", "SET disabled_optimizers = 'JOIN_ORDER';")
    _write_statement(f, "ok", "PRAGMA custom_profiling_settings='{\"OPTIMIZER_JOIN_ORDER\": \"true\"}'")
    _write_default_query(f)

    _write_get_custom_profiling_settings(f)
    f.write("(empty)\n\n")

    _write_statement(f, "ok", "PRAGMA custom_profiling_settings='{\"CUMULATIVE_OPTIMIZER_TIMING\": \"true\"}';")
    _write_default_query(f)

    _write_statement(
        f, "ok", "CREATE OR REPLACE TABLE metrics_output AS SELECT * FROM '__TEST_DIR__/profiling_output.json';"
    )

    query = """
SELECT
    CASE WHEN cumulative_optimizer_timing > 0 THEN 'true'
    ELSE 'false' END
FROM metrics_output;
""".strip()
    _write_query(f, "I", query)
    f.write("true\n\n")

    f.write("# All phase timings must be collected when using detailed profiling mode.\n\n")

    _write_statement(f, "ok", "RESET custom_profiling_settings;")
    _write_statement(f, "ok", "SET profiling_mode = 'detailed';")
    _write_default_query(f)

    query = """
SELECT * FROM (
    SELECT unnest(res) str FROM (
        SELECT current_setting('custom_profiling_settings') AS raw_setting,
        raw_setting.trim('{}') AS setting,
        string_split(setting, ', ') AS res
    )
)
WHERE '"true"' NOT IN str
ORDER BY ALL
""".strip()
    _write_query(f, "I", query)
    f.write("\n")

    _write_statement(f, "ok", "RESET custom_profiling_settings;")
    _write_statement(f, "ok", "SET profiling_mode = 'standard';")


def _generate_group_test(f, groups: list[str], all_metrics: Dict[str, List[str]]):
    _write_statement(f, "ok", "PRAGMA enable_profiling = 'json';")
    _write_statement(f, "ok", "PRAGMA profiling_output = '__TEST_DIR__/profiling_output.json';")

    group_str = ", ".join(f'"{g.upper()}": "true"' for g in groups)
    _write_statement(f, "ok", f"PRAGMA custom_profiling_settings='{{{group_str}}}';")

    _write_default_query(f)
    _write_get_custom_profiling_settings(f)

    metrics: list[str] = []
    for g in groups:
        metrics += all_metrics[g]

    if "all" not in groups and "ALL_OPTIMIZERS" in metrics:
        metrics.extend(all_metrics.get("optimizer", []))

    metrics = list(set(metrics))
    metrics.sort()
    for m in metrics:
        f.write(f'"{m}": "true"\n')
    f.write("\n")

    _write_statement(
        f, "ok", "CREATE OR REPLACE TABLE metrics_output AS SELECT * FROM '__TEST_DIR__/profiling_output.json';"
    )

    cols: list[str] = []
    operator_metrics = set(all_metrics.get("operator", []))
    for m in metrics:
        if m in operator_metrics and "operator" not in groups:
            continue
        cols.append(m)

    select = "SELECT " + ",\n\t".join(cols)

    if "operator" in groups:
        select += "\nFROM (\n"
        select += "\tSELECT unnest(children, max_depth := 2)\n"
        select += "\tFROM metrics_output\n"
        select += ")"
    else:
        select += "\nFROM metrics_output;"

    _write_statement(f, "ok", select)


def _generate_metric_group_test_file(out_path, all_metrics: Dict[str, List[str]]):
    name = path_from_duckdb(out_path)
    print(f"  * {name}")

    top = f"""# name: {name}
# description: Test default profiling settings using groups.
# group: [profiling]

# This file is automatically generated by scripts/generate_metric_enums.py
# Do not edit this file manually, your changes will be overwritten

require json

"""
    with IndentedFileWriter(out_path) as f:
        f.write(top)
        for group in all_metrics:
            _generate_group_test(f, [group], all_metrics)
        _generate_group_test(f, ["default", "file"], all_metrics)
        _generate_group_test(f, ["file", "optimizer"], all_metrics)
        _generate_group_test(f, ["phase_timing", "execution", "file"], all_metrics)
    format_file(out_path)


def _generate_profiling_setting_tests(out_dir: Path, all_metrics: Dict[str, List[str]]):
    test_names = [
        "test_default_profiling_settings",
        "test_custom_profiling_optimizer_settings",
        "test_all_profiling_settings",
    ]
    test_descriptions = ["default", "custom optimizer", "all settings"]
    test_paths = [out_dir / f"{name}.test" for name in test_names]
    metrics_group = ["default", "default", "all"]

    for test_file, name, description, group in zip(test_paths, test_names, test_descriptions, metrics_group):
        display_name = path_from_duckdb(test_file)
        print(f"  * {display_name}")
        with IndentedFileWriter(test_file) as f:
            f.write(f"# name: {display_name}\n")
            f.write(f"# description: Test {description} profiling settings.\n")
            f.write("# group: [profiling]\n\n")
            f.write("# This file is automatically generated by scripts/generate_metric_enums.py\n")
            f.write("# Do not edit this file manually, your changes will be overwritten\n\n")
            f.write("require json\n\n")

            _write_statement(f, "ok", "PRAGMA enable_profiling = 'json';")
            _write_statement(f, "ok", "PRAGMA profiling_output = '__TEST_DIR__/profiling_output.json';")

            mode = "standard" if group == "default" else group
            _write_statement(f, "ok", f"SET profiling_mode='{mode}';")

            if name == "test_custom_profiling_optimizer_settings":
                _write_custom_profiling_optimizer(f)

            _write_default_query(f)
            _write_get_custom_profiling_settings(f)

            for m in all_metrics[group]:
                f.write(f'"{m}": "true"\n')
            f.write("\n")

            _write_statement(
                f, "ok", "CREATE OR REPLACE TABLE metrics_output AS SELECT * FROM '__TEST_DIR__/profiling_output.json';"
            )
            _write_statement(f, "ok", "SELECT cpu_time, extra_info, rows_returned, latency FROM metrics_output;")
        format_file(test_file)


def generate_test_files(out_dir: Path, all_metrics: Dict[str, List[str]]):
    _generate_profiling_setting_tests(out_dir, all_metrics)
    _generate_metric_group_test_file(out_dir / "test_custom_profiling_using_groups.test", all_metrics)