Skip to content

Commit 36e09e5

Browse files
committed
Feat: add formatter options to render command
1 parent 57b59ec commit 36e09e5

6 files changed

Lines changed: 223 additions & 110 deletions

File tree

docs/reference/cli.md

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -419,19 +419,34 @@ Usage: sqlmesh render [OPTIONS] MODEL
419419
Render a model's query, optionally expanding referenced models.
420420
421421
Options:
422-
-s, --start TEXT The start datetime of the interval for which this
423-
command will be applied.
424-
-e, --end TEXT The end datetime of the interval for which this
425-
command will be applied.
426-
--execution-time TEXT The execution time (defaults to now).
427-
--expand TEXT Whether or not to expand materialized models
428-
(defaults to False). If True, all referenced models
429-
are expanded as raw queries. Multiple model names can
430-
also be specified, in which case only they will be
431-
expanded as raw queries.
432-
--dialect TEXT The SQL dialect to render the query as.
433-
--no-format Disable fancy formatting of the query.
434-
--help Show this message and exit.
422+
-s, --start TEXT The start datetime of the interval for which
423+
this command will be applied.
424+
-e, --end TEXT The end datetime of the interval for which this
425+
command will be applied.
426+
--execution-time TEXT The execution time (defaults to now).
427+
--expand TEXT Whether or not to expand materialized models
428+
(defaults to False). If True, all referenced
429+
models are expanded as raw queries. Multiple
430+
model names can also be specified, in which case
431+
only they will be expanded as raw queries.
432+
--dialect TEXT The SQL dialect to render the query as.
433+
--no-format Disable fancy formatting of the query.
434+
--no-rewrite-casts Preserve the existing casts, without rewriting
435+
them to use the :: syntax.
436+
--append-newline Include a newline at the end of each file.
437+
--max-text-width INTEGER The max number of characters in a segment before
438+
creating new lines in pretty mode.
439+
--leading-comma Determines whether or not the comma is leading
440+
or trailing in select expressions. Default is
441+
trailing.
442+
--normalize-functions TEXT Whether or not to normalize all function names.
443+
Possible values are: 'upper', 'lower'
444+
--indent INTEGER Determines the indentation size in a formatted
445+
string.
446+
--pad INTEGER Determines the pad size in a formatted string.
447+
--normalize Whether or not to normalize identifiers to
448+
lowercase.
449+
--help Show this message and exit.
435450
```
436451

437452
## rewrite

docs/reference/notebook.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ options:
201201
```
202202
%render [--start START] [--end END] [--execution-time EXECUTION_TIME]
203203
[--expand EXPAND] [--dialect DIALECT] [--no-format]
204+
[--no-rewrite-casts] [--append-newline] [--normalize]
205+
[--pad PAD] [--indent INDENT] [--normalize-functions NORMALIZE_FUNCTIONS]
206+
[--leading-comma] [--max-text-width MAX_TEXT_WIDTH]
204207
model
205208
206209
Renders a model's query, optionally expanding referenced models.
@@ -220,6 +223,20 @@ options:
220223
models are expanded as raw queries.
221224
--dialect DIALECT SQL dialect to render.
222225
--no-format Disable fancy formatting of the query.
226+
--no-rewrite-casts Preserve the existing casts, without rewriting them
227+
to use the :: syntax.
228+
--append-newline Include a newline at the end of the output.
229+
--normalize Whether or not to normalize identifiers to lowercase.
230+
--pad PAD Determines the pad size in a formatted string.
231+
--indent INDENT Determines the indentation size in a formatted string.
232+
--normalize-functions NORMALIZE_FUNCTIONS
233+
Whether or not to normalize all function names.
234+
Possible values are: 'upper', 'lower'
235+
--leading-comma Determines whether or not the comma is leading or
236+
trailing in select expressions. Default is trailing.
237+
--max-text-width MAX_TEXT_WIDTH
238+
The max number of characters in a segment before
239+
creating new lines in pretty mode.
223240
```
224241

225242
#### dag

sqlmesh/cli/main.py

Lines changed: 18 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ def init(
191191
help="The SQL dialect to render the query as.",
192192
)
193193
@click.option("--no-format", is_flag=True, help="Disable fancy formatting of the query.")
194+
@opt.format_options
194195
@click.pass_context
195196
@error_handler
196197
@cli_analytics
@@ -203,6 +204,7 @@ def render(
203204
expand: t.Optional[t.Union[bool, t.Iterable[str]]] = None,
204205
dialect: t.Optional[str] = None,
205206
no_format: bool = False,
207+
**format_kwargs: t.Any,
206208
) -> None:
207209
"""Render a model's query, optionally expanding referenced models."""
208210
rendered = ctx.obj.render(
@@ -213,7 +215,21 @@ def render(
213215
expand=expand,
214216
)
215217

216-
sql = rendered.sql(pretty=True, dialect=ctx.obj.config.dialect if dialect is None else dialect)
218+
format_options = {}
219+
if format_kwargs.get("no_rewrite_casts"):
220+
format_options["rewrite_casts"] = False
221+
format_kwargs.pop("no_rewrite_casts")
222+
223+
format_options.update({k: v for k, v in format_kwargs.items() if v is not None})
224+
225+
format_config = ctx.obj.config_for_node(model).format
226+
format_options = {**format_config.generator_options, **format_options}
227+
228+
sql = rendered.sql(
229+
pretty=True,
230+
dialect=ctx.obj.config.dialect if dialect is None else dialect,
231+
**format_options,
232+
)
217233
if no_format:
218234
print(sql)
219235
else:
@@ -263,56 +279,13 @@ def evaluate(
263279
type=str,
264280
help="Transpile project models to the specified dialect.",
265281
)
266-
@click.option(
267-
"--append-newline",
268-
is_flag=True,
269-
help="Include a newline at the end of each file.",
270-
default=None,
271-
)
272-
@click.option(
273-
"--no-rewrite-casts",
274-
is_flag=True,
275-
help="Preserve the existing casts, without rewriting them to use the :: syntax.",
276-
default=None,
277-
)
278-
@click.option(
279-
"--normalize",
280-
is_flag=True,
281-
help="Whether or not to normalize identifiers to lowercase.",
282-
default=None,
283-
)
284-
@click.option(
285-
"--pad",
286-
type=int,
287-
help="Determines the pad size in a formatted string.",
288-
)
289-
@click.option(
290-
"--indent",
291-
type=int,
292-
help="Determines the indentation size in a formatted string.",
293-
)
294-
@click.option(
295-
"--normalize-functions",
296-
type=str,
297-
help="Whether or not to normalize all function names. Possible values are: 'upper', 'lower'",
298-
)
299-
@click.option(
300-
"--leading-comma",
301-
is_flag=True,
302-
help="Determines whether or not the comma is leading or trailing in select expressions. Default is trailing.",
303-
default=None,
304-
)
305-
@click.option(
306-
"--max-text-width",
307-
type=int,
308-
help="The max number of characters in a segment before creating new lines in pretty mode.",
309-
)
310282
@click.option(
311283
"--check",
312284
is_flag=True,
313285
help="Whether or not to check formatting (but not actually format anything).",
314286
default=None,
315287
)
288+
@opt.format_options
316289
@click.pass_context
317290
@error_handler
318291
@cli_analytics

sqlmesh/cli/options.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import os
4+
import typing as t
45

56
import click
67

@@ -54,3 +55,52 @@
5455
count=True,
5556
help="Verbose output. Use -vv for very verbose output.",
5657
)
58+
59+
60+
def format_options(func: t.Callable) -> t.Callable:
61+
"""Decorator to add common format options to CLI commands."""
62+
func = click.option(
63+
"--normalize",
64+
is_flag=True,
65+
help="Whether or not to normalize identifiers to lowercase.",
66+
default=None,
67+
)(func)
68+
func = click.option(
69+
"--pad",
70+
type=int,
71+
help="Determines the pad size in a formatted string.",
72+
)(func)
73+
func = click.option(
74+
"--indent",
75+
type=int,
76+
help="Determines the indentation size in a formatted string.",
77+
)(func)
78+
func = click.option(
79+
"--normalize-functions",
80+
type=str,
81+
help="Whether or not to normalize all function names. Possible values are: 'upper', 'lower'",
82+
)(func)
83+
func = click.option(
84+
"--leading-comma",
85+
is_flag=True,
86+
help="Determines whether or not the comma is leading or trailing in select expressions. Default is trailing.",
87+
default=None,
88+
)(func)
89+
func = click.option(
90+
"--max-text-width",
91+
type=int,
92+
help="The max number of characters in a segment before creating new lines in pretty mode.",
93+
)(func)
94+
func = click.option(
95+
"--append-newline",
96+
is_flag=True,
97+
help="Include a newline at the end of each file.",
98+
default=None,
99+
)(func)
100+
func = click.option(
101+
"--no-rewrite-casts",
102+
is_flag=True,
103+
help="Preserve the existing casts, without rewriting them to use the :: syntax.",
104+
default=None,
105+
)(func)
106+
return func

0 commit comments

Comments
 (0)