/
mzexplore.py
425 lines (370 loc) · 11.9 KB
/
mzexplore.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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# Copyright Materialize, Inc. and contributors. All rights reserved.
#
# Use of this software is governed by the Business Source License
# included in the LICENSE file at the root of this repository.
#
# As of the Change Date specified in that file, in accordance with
# the Business Source License, use of this software will be governed
# by the Apache License, Version 2.0.
from pathlib import Path
from typing import Any
import click
import materialize.mzexplore as api
import materialize.mzexplore.common as common
# import logging
# logging.basicConfig(encoding='utf-8', level=logging.DEBUG)
# Click CLI Application
# ---------------------
@click.group()
def app() -> None:
pass
class Arg:
repository: dict[str, Any] = dict(
type=click.Path(
exists=False,
file_okay=False,
dir_okay=True,
writable=True,
readable=True,
resolve_path=True,
),
callback=lambda ctx, param, value: Path(value), # type: ignore
)
output_file: dict[str, Any] = dict(
type=click.Path(
exists=False,
file_okay=True,
dir_okay=False,
writable=True,
readable=True,
resolve_path=True,
),
callback=lambda ctx, param, value: Path(value), # type: ignore
)
base_suffix: dict[str, Any] = dict(
type=str,
metavar="BASE",
)
diff_suffix: dict[str, Any] = dict(
type=str,
metavar="DIFF",
)
class Opt:
db_port: dict[str, Any] = dict(
default=6877,
help="DB connection port.",
envvar="PGPORT",
)
db_host: dict[str, Any] = dict(
default="localhost",
help="DB connection host.",
envvar="PGHOST",
)
db_user: dict[str, Any] = dict(
default="mz_support",
help="DB connection user.",
envvar="PGUSER",
)
db_pass: dict[str, Any] = dict(
default=None,
help="DB connection password.",
envvar="PGPASSWORD",
)
db_require_ssl: dict[str, Any] = dict(
is_flag=True,
help="DB connection requires SSL.",
envvar="PGREQUIRESSL",
)
mzfmt: dict[str, Any] = dict(
default=True,
help="Format SQL statements with `mzfmt` if present.",
)
explainee_type: dict[str, Any] = dict(
type=click.Choice([v.name.lower() for v in list(api.ExplaineeType)]),
default="catalog_item",
callback=lambda ctx, param, v: api.ExplaineeType[v.upper()], # type: ignore
help="EXPLAIN mode.",
metavar="MODE",
)
explain_options: dict[str, Any] = dict(
type=api.ExplainOptionType(),
multiple=True,
help="WITH key=val pairs to pass to the EXPLAIN command.",
metavar="KEY=VAL",
)
explain_stage: dict[str, Any] = dict(
type=click.Choice([str(v.name.lower()) for v in list(api.ExplainStage)]),
multiple=True,
default=["optimized_plan"], # Most often we'll only the optimized plan.
callback=lambda ctx, param, vals: [api.ExplainStage[v.upper()] for v in vals], # type: ignore
help="EXPLAIN stage to export.",
metavar="STAGE",
)
explain_suffix: dict[str, Any] = dict(
type=str,
default="",
help="A suffix to append to the EXPLAIN output files.",
metavar="SUFFIX",
)
explain_format: dict[str, Any] = dict(
type=click.Choice([str(v.name.lower()) for v in list(api.ExplainFormat)]),
default="text",
callback=lambda ctx, param, v: api.ExplainFormat[v.upper()], # type: ignore
help="AS [FORMAT] clause to pass to the EXPLAIN command.",
metavar="FORMAT",
)
system: dict[str, Any] = dict(
is_flag=True,
show_default=True,
default=False,
help="Inspect system or user tables.",
)
def is_documented_by(original: Any) -> Any:
def wrapper(target):
target.__doc__ = original.__doc__
return target
return wrapper
@app.group()
@is_documented_by(api.extract)
def extract() -> None:
pass
@extract.command(name="defs")
@click.argument("target", **Arg.repository)
@click.argument("database", type=str)
@click.argument("schema", type=str)
@click.argument("name", type=str)
@click.option("--db-port", **Opt.db_port)
@click.option("--db-host", **Opt.db_host)
@click.option("--db-user", **Opt.db_user)
@click.option("--db-pass", **Opt.db_pass)
@click.option("--db-require-ssl", **Opt.db_require_ssl)
@click.option("--mzfmt/--no-mzfmt", **Opt.mzfmt)
@is_documented_by(api.extract.defs)
def extract_defs(
target: Path,
database: str,
schema: str,
name: str,
db_port: int,
db_host: str,
db_user: str,
db_pass: str | None,
db_require_ssl: bool,
mzfmt: bool,
) -> None:
try:
api.extract.defs(
target=target,
database=database,
schema=schema,
name=name,
db_port=db_port,
db_host=db_host,
db_user=db_user,
db_pass=db_pass,
db_require_ssl=db_require_ssl,
mzfmt=mzfmt,
)
except Exception as e:
import traceback
traceback.print_tb(e.__traceback__)
raise click.ClickException(f"extract defs command failed: {e=}, {type(e)=}")
@extract.command(name="plans")
@click.argument("target", **Arg.repository)
@click.argument("database", type=str)
@click.argument("schema", type=str)
@click.argument("name", type=str)
@click.option("--db-port", **Opt.db_port)
@click.option("--db-host", **Opt.db_host)
@click.option("--db-user", **Opt.db_user)
@click.option("--db-pass", **Opt.db_pass)
@click.option("--db-require-ssl", **Opt.db_require_ssl)
@click.option("--explainee-type", "-t", **Opt.explainee_type)
@click.option("--with", "-w", "explain_options", **Opt.explain_options)
@click.option("--stage", "-s", "explain_stages", **Opt.explain_stage)
@click.option("--format", "-f", "explain_format", **Opt.explain_format)
@click.option("--system/--user", "system", **Opt.system)
@click.option("--suffix", **Opt.explain_suffix)
@is_documented_by(api.extract.plans)
def extract_plans(
target: Path,
database: str,
schema: str,
name: str,
db_port: int,
db_host: str,
db_user: str,
db_pass: str | None,
db_require_ssl: bool,
explainee_type: api.ExplaineeType,
explain_options: list[api.ExplainOption],
explain_stages: set[api.ExplainStage],
explain_format: api.ExplainFormat,
system: bool,
suffix: str | None = None,
) -> None:
try:
api.extract.plans(
target=target,
database=database,
schema=schema,
name=name,
db_port=db_port,
db_host=db_host,
db_user=db_user,
db_pass=db_pass,
db_require_ssl=db_require_ssl,
explainee_type=explainee_type,
explain_options=explain_options,
explain_stages=explain_stages,
explain_format=explain_format,
system=system,
suffix=suffix,
)
except Exception as e:
import traceback
traceback.print_tb(e.__traceback__)
raise click.ClickException(f"extract plans command failed: {e=}, {type(e)=}")
@extract.command(name="arrangement-sizes")
@click.argument("target", **Arg.repository)
@click.argument("cluster", type=str)
@click.argument("cluster_replica", type=str)
@click.argument("database", type=str)
@click.argument("schema", type=str)
@click.argument("name", type=str)
@click.option("--db-port", **Opt.db_port)
@click.option("--db-host", **Opt.db_host)
@click.option("--db-user", **Opt.db_user)
@click.option("--db-pass", **Opt.db_pass)
@click.option("--db-require-ssl", **Opt.db_require_ssl)
@click.option("--print-results", is_flag=True, default=False)
@is_documented_by(api.extract.arrangement_sizes)
def extract_arrangement_sizes(
target: Path,
cluster: str,
cluster_replica: str,
database: str,
schema: str,
name: str,
db_port: int,
db_host: str,
db_user: str,
db_pass: str | None,
db_require_ssl: bool,
print_results: bool,
) -> None:
try:
api.extract.arrangement_sizes(
target=target,
cluster=cluster,
cluster_replica=cluster_replica,
database=database,
schema=schema,
name=name,
db_port=db_port,
db_host=db_host,
db_user=db_user,
db_pass=db_pass,
db_require_ssl=db_require_ssl,
print_results=print_results,
)
except Exception as e:
import traceback
traceback.print_tb(e.__traceback__)
raise click.ClickException(
f"extract arrangement-sizes command failed: {e=}, {type(e)=}"
)
@app.group()
@is_documented_by(api.analyze)
def analyze() -> None:
pass
@analyze.command(name="changes")
@click.argument("target", **Arg.repository) # type: ignore
@click.argument("summary_file", **Arg.output_file) # type: ignore
@click.argument("base_suffix", **Arg.base_suffix)
@click.argument("diff_suffix", **Arg.diff_suffix)
@is_documented_by(api.analyze.changes)
def analyze_changes(
target: Path,
summary_file: Path,
base_suffix: str,
diff_suffix: str,
) -> None:
"""
Prepare an analysis report of plan changes as a Markdown document.
"""
try:
with summary_file.open("a+", encoding="utf-8") as out:
api.analyze.changes(
out=out,
target=target,
header_name=str(target),
base_suffix=base_suffix,
diff_suffix=diff_suffix,
)
common.info(f"Summary written to {summary_file}")
except Exception as e:
import traceback
traceback.print_tb(e.__traceback__)
msg = f"analyze changes command failed: {e=}, {type(e)=}"
raise click.ClickException(msg) from e
@app.group()
@is_documented_by(api.clone)
def clone() -> None:
pass
@clone.command(name="defs")
@click.argument("database", type=str)
@click.argument("schema", type=str)
@click.argument("cluster", type=str)
@click.argument("object_ids", type=str, nargs=-1)
@click.argument("ddl_file", **Arg.output_file) # type: ignore
@click.option("--db-port", **Opt.db_port)
@click.option("--db-host", **Opt.db_host)
@click.option("--db-user", **Opt.db_user)
@click.option("--db-pass", **Opt.db_pass)
@click.option("--db-require-ssl", **Opt.db_require_ssl)
@click.option("--mzfmt/--no-mzfmt", **Opt.mzfmt)
@is_documented_by(api.clone.defs)
def clone_defs(
database: str,
schema: str,
cluster: str,
object_ids: list[str],
ddl_file: Path,
db_port: int,
db_host: str,
db_user: str,
db_pass: str | None,
db_require_ssl: bool,
mzfmt: bool,
) -> None:
try:
cmp_file = Path(ddl_file.parent, f"__compare__{ddl_file.name}")
with ddl_file.open("w", encoding="utf-8") as ddl_out:
with cmp_file.open("w", encoding="utf-8") as cmp_out:
api.clone.defs(
ddl_out=ddl_out,
cmp_out=cmp_out,
database=database,
schema=schema,
cluster=cluster,
object_ids=object_ids,
db_port=db_port,
db_host=db_host,
db_user=db_user,
db_pass=db_pass,
db_require_ssl=db_require_ssl,
mzfmt=mzfmt,
)
common.info(f"Modified DDL written to {ddl_file}")
common.info(f"Original DDL written to {cmp_file}")
common.warn("Please inspect the diff between the two!!!")
except Exception as e:
import traceback
traceback.print_tb(e.__traceback__)
msg = f"clone defs command failed: {e=}, {type(e)=}"
raise click.ClickException(msg) from e
# Entrypoint
# ----------
if __name__ == "__main__":
app()