sqlalign 1.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sqlalign/__init__.py +10 -0
- sqlalign/align.py +145 -0
- sqlalign/bodylint.py +125 -0
- sqlalign/casing.py +182 -0
- sqlalign/cli.py +360 -0
- sqlalign/commas.py +58 -0
- sqlalign/config.py +28 -0
- sqlalign/configfile.py +194 -0
- sqlalign/formatter.py +609 -0
- sqlalign/gui.py +721 -0
- sqlalign/ir.py +38 -0
- sqlalign/keywordcase.py +127 -0
- sqlalign/layout/__init__.py +312 -0
- sqlalign/layout/case.py +222 -0
- sqlalign/layout/comments.py +530 -0
- sqlalign/layout/conditions.py +344 -0
- sqlalign/layout/cte.py +81 -0
- sqlalign/layout/ddl.py +424 -0
- sqlalign/layout/dml.py +287 -0
- sqlalign/layout/expr.py +119 -0
- sqlalign/layout/fromjoin.py +310 -0
- sqlalign/layout/grouporder.py +135 -0
- sqlalign/layout/select.py +523 -0
- sqlalign/layout/setop.py +78 -0
- sqlalign/layout/subquery.py +245 -0
- sqlalign/layout/window.py +207 -0
- sqlalign/lint.py +173 -0
- sqlalign/plpgsql.py +437 -0
- sqlalign/splitter.py +241 -0
- sqlalign/sqlfluffconfig.py +88 -0
- sqlalign/style.py +215 -0
- sqlalign/templating.py +87 -0
- sqlalign-1.0.1.dist-info/METADATA +404 -0
- sqlalign-1.0.1.dist-info/RECORD +37 -0
- sqlalign-1.0.1.dist-info/WHEEL +4 -0
- sqlalign-1.0.1.dist-info/entry_points.txt +2 -0
- sqlalign-1.0.1.dist-info/licenses/LICENSE +21 -0
sqlalign/layout/case.py
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
"""CASE-expression layout: a select-list item whose
|
|
2
|
+
value is a CASE expression, in either spelling. Verified empirically (pinned
|
|
3
|
+
sqlglot v30.14): `exp.Case.arg_types = {this: False, ifs: True, default:
|
|
4
|
+
False}`; `this` is the simple-CASE operand (`CASE x WHEN v1 THEN r1 ...`) and
|
|
5
|
+
is `None` for a searched CASE, which is all sample 08 exercises.
|
|
6
|
+
|
|
7
|
+
The simple form needs no geometry of its own: the operand rides the CASE line,
|
|
8
|
+
pushing the WHEN column right by its width, and one rule then covers both
|
|
9
|
+
spellings: the first WHEN rides line 0, every later WHEN aligns under it. For a searched CASE that
|
|
10
|
+
column is `item_col + len("CASE ")`, exactly what it always was. In the simple
|
|
11
|
+
form `iff.this` is a VALUE to compare against rather than a predicate; it
|
|
12
|
+
renders the same way into the same column, which is why THEN alignment needed
|
|
13
|
+
no special case either.
|
|
14
|
+
|
|
15
|
+
Both forms below open with `CASE WHEN ...` on the item's own row and close
|
|
16
|
+
with `END` directly under `CASE`
|
|
17
|
+
(fixed at the item column -- not resolver-tagged, the same "END under CASE"
|
|
18
|
+
relationship short-form's THEN-vs-CASE column pairing establishes structurally
|
|
19
|
+
rather than through alignment).
|
|
20
|
+
|
|
21
|
+
Short form: every `WHEN cond THEN val` fits on one row: `WHEN`s align one
|
|
22
|
+
space after `CASE` (`item_col + len("CASE ")`); `THEN` is tagged
|
|
23
|
+
`kind="then"` in a per-CASE scope (`f"case@{id(case_node)}"`, id()-namespaced
|
|
24
|
+
per house convention so a sibling CASE item's THEN column never bleeds into
|
|
25
|
+
this one) so the resolver -- not manual padding -- pads every row's THEN to
|
|
26
|
+
the widest condition's column, exactly the mechanism the fixpoint resolver
|
|
27
|
+
resolver exists to compose.
|
|
28
|
+
|
|
29
|
+
Long form: the first WHEN rides the CASE line; every subsequent WHEN/ELSE
|
|
30
|
+
starts its own row at the WHEN column; THEN drops to ITS OWN row, indented 2
|
|
31
|
+
past WHEN (a fixed offset, not resolver-tagged -- every WHEN's THEN sits at
|
|
32
|
+
the identical `when_col + 2` regardless of condition length, unlike
|
|
33
|
+
short-form's content-dependent THEN column). A WHEN whose condition is
|
|
34
|
+
compound (AND/OR) spills its continuation operand(s) onto further rows with
|
|
35
|
+
the boolean joiner right-aligned under WHEN -- precisely conditions.py's
|
|
36
|
+
`condition_block` geometry with "WHEN" standing in for the clause keyword
|
|
37
|
+
(and "AND"/"OR" as the joiner: `max_kw = max(len("WHEN"), len(joiner))`,
|
|
38
|
+
right-justified, so a 3-char "AND"/2-char "OR" right-aligns to the same end
|
|
39
|
+
column "WHEN" occupies) -- reused directly below via `_when_condition_lines`
|
|
40
|
+
rather than re-deriving that arithmetic. Unlike `condition_block`'s own
|
|
41
|
+
keyword right-alignment (a fixed Python `.rjust()`, not a resolver tag --
|
|
42
|
+
see that module's docstring), the WHEN/AND/OR family here is likewise pure
|
|
43
|
+
Python arithmetic; only THEN (short form) and each condition's own comparison
|
|
44
|
+
operator (both forms, via `predicate_segs`'s `kind="op"` tag) go through the
|
|
45
|
+
resolver.
|
|
46
|
+
|
|
47
|
+
DISCREPANCY (fixture-driven; house convention is fixture bytes over
|
|
48
|
+
spec/brief wording: noted here per that convention): the spec and task
|
|
49
|
+
brief describe the short/long choice as purely width-driven ("short form:
|
|
50
|
+
fits in width"). Sample 08's second CASE (status_rank) contradicts a pure
|
|
51
|
+
width reading: every one of its WHEN...THEN rows would fit comfortably under
|
|
52
|
+
the width limit even laid out in short form (the longest, `WHEN status =
|
|
53
|
+
'pending' AND NOT is_archived THEN 2`, is 50 columns wide starting at column
|
|
54
|
+
12: nowhere near the ~105-column limit), yet the fixture uses long form
|
|
55
|
+
throughout, including its FIRST, non-compound WHEN (`status = 'complete'`).
|
|
56
|
+
The bytes-verified actual trigger is structural, not width-based: a CASE
|
|
57
|
+
goes long-form the instant ANY of its WHEN conditions is a compound (AND/OR)
|
|
58
|
+
boolean: forcing uniform long-form layout across every WHEN in that same
|
|
59
|
+
CASE, mirroring how a single compound WHERE condition forces per-operand
|
|
60
|
+
line layout regardless of length. A too-long *simple* (non-compound)
|
|
61
|
+
condition also still falls back to long form as a width safety valve --
|
|
62
|
+
untested by the fixtures, but keeps the short-form promise ("fits in
|
|
63
|
+
width") honest for that case, and it is the only place width genuinely
|
|
64
|
+
enters this module's short/long decision.
|
|
65
|
+
|
|
66
|
+
`NOT is_archived` (a bare boolean operand, not a binary comparison) appears
|
|
67
|
+
inside the second CASE's compound WHEN. `conditions.py`'s shared
|
|
68
|
+
`predicate_segs` deliberately has no `exp.Not` case and raises `Unsupported`
|
|
69
|
+
for it everywhere else in the codebase -- `test_subquery.py`'s
|
|
70
|
+
`test_not_exists_passes_through` depends on exactly that decline (`NOT
|
|
71
|
+
EXISTS (...)` in a WHERE clause must safely pass through, not silently drop
|
|
72
|
+
the negation), so this module does NOT loosen `predicate_segs` itself, which
|
|
73
|
+
would defeat that test and re-open the "silently render an unmodeled boolean
|
|
74
|
+
shape everywhere" risk `predicate_segs`'s narrow contract exists to close.
|
|
75
|
+
Instead `_when_condition_lines` below (this module's own, CASE-WHEN-only
|
|
76
|
+
condition renderer, structurally parallel to but independent of
|
|
77
|
+
`condition_block`) tries `predicate_segs` first and falls back to one plain
|
|
78
|
+
`render_expr`-rendered segment: ast_equal-safe by construction, since it is
|
|
79
|
+
the exact same node re-serialized faithfully: for anything `predicate_segs`
|
|
80
|
+
declines. The fallback is reachable ONLY from a CASE's WHEN, never from
|
|
81
|
+
WHERE/HAVING/ON, so `NOT EXISTS` and every other predicate shape
|
|
82
|
+
`predicate_segs` is deliberately narrow about stay exactly as narrow
|
|
83
|
+
everywhere else.
|
|
84
|
+
"""
|
|
85
|
+
from sqlglot import exp
|
|
86
|
+
|
|
87
|
+
from sqlalign.casing import render_expr
|
|
88
|
+
from sqlalign.ir import Line, Seg
|
|
89
|
+
from sqlalign.layout import Unsupported, select_item_col
|
|
90
|
+
from sqlalign.layout.conditions import joiner_head, predicate_segs, split_conjunction
|
|
91
|
+
|
|
92
|
+
_WHEN_OFFSET = len("CASE ") # 5: WHEN sits one space after CASE, both forms
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def case_lines(case_node: exp.Case, anchor: int, dialect: str, width) -> list[Line]:
|
|
96
|
+
"""Lay out `case_node` (a select item's CASE value) at select-statement
|
|
97
|
+
anchor `anchor`. Line 0's `indent` is `anchor` itself (NOT the item
|
|
98
|
+
column) so the caller (select.py) can splice its own lead-in segment
|
|
99
|
+
(`SELECT` / the leading comma) onto the front of line 0's segs, exactly
|
|
100
|
+
like every other select-list item -- render()'s automatic one-space
|
|
101
|
+
inter-seg separator then lands `CASE` at the item column on its own,
|
|
102
|
+
with no manual space-counting here.
|
|
103
|
+
"""
|
|
104
|
+
ifs = case_node.args["ifs"]
|
|
105
|
+
default = case_node.args.get("default")
|
|
106
|
+
item_col = select_item_col(anchor)
|
|
107
|
+
# The simple form's test operand rides the CASE line, so `head` grows and the
|
|
108
|
+
# WHEN column moves right with it. One rule covers both forms: the first WHEN
|
|
109
|
+
# rides line 0 and every later WHEN aligns under it, which for a searched
|
|
110
|
+
# CASE is `item_col + len("CASE ")` exactly as before.
|
|
111
|
+
operand = case_node.args.get("this")
|
|
112
|
+
head = "CASE" if operand is None else f"CASE {render_expr(operand, dialect)}"
|
|
113
|
+
when_col = item_col + len(head) + 1
|
|
114
|
+
|
|
115
|
+
if _fits_short_form(ifs, when_col, item_col, dialect, width):
|
|
116
|
+
return _short_form(case_node, ifs, default, anchor, item_col, when_col, dialect,
|
|
117
|
+
head)
|
|
118
|
+
return long_form_case_lines(case_node, ifs, default, anchor, when_col, dialect,
|
|
119
|
+
head=head, end_indent=item_col)
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def _fits_short_form(ifs, when_col, item_col, dialect, width) -> bool:
|
|
123
|
+
"""Short form's eligibility test (see module docstring's DISCREPANCY
|
|
124
|
+
note): disqualified the instant any WHEN condition is compound (AND/OR)
|
|
125
|
+
-- long form's boolean-operator alignment is the house treatment for
|
|
126
|
+
that, unconditionally, the same way WHERE never keeps a compound
|
|
127
|
+
condition on one packed line regardless of width. Absent any compound
|
|
128
|
+
condition, short form additionally requires every `WHEN cond THEN val`
|
|
129
|
+
row to fit the width limit on its own (untested by the fixtures, but
|
|
130
|
+
keeps "short form: fits in width" a real constraint for a merely-long
|
|
131
|
+
simple condition rather than a dead letter).
|
|
132
|
+
"""
|
|
133
|
+
if any(isinstance(iff.this, (exp.And, exp.Or)) for iff in ifs):
|
|
134
|
+
return False
|
|
135
|
+
limit = width.limit(item_col)
|
|
136
|
+
for iff in ifs:
|
|
137
|
+
cond_text = render_expr(iff.this, dialect)
|
|
138
|
+
val_text = render_expr(iff.args["true"], dialect)
|
|
139
|
+
row_len = when_col + len("WHEN ") + len(cond_text) + len(" THEN ") + len(val_text)
|
|
140
|
+
if row_len > limit:
|
|
141
|
+
return False
|
|
142
|
+
return True
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def _short_form(case_node, ifs, default, anchor, item_col, when_col, dialect,
|
|
146
|
+
head="CASE") -> list[Line]:
|
|
147
|
+
scope = f"case@{id(case_node)}"
|
|
148
|
+
lines = []
|
|
149
|
+
for i, iff in enumerate(ifs):
|
|
150
|
+
# In the simple form `iff.this` is a VALUE to compare against, not a
|
|
151
|
+
# predicate; it renders the same way and lands in the same column, which
|
|
152
|
+
# is why the THEN alignment needs no special case.
|
|
153
|
+
cond_text = render_expr(iff.this, dialect)
|
|
154
|
+
val_text = render_expr(iff.args["true"], dialect)
|
|
155
|
+
head_segs = [Seg(head), Seg("WHEN")] if i == 0 else [Seg("WHEN")]
|
|
156
|
+
segs = [*head_segs, Seg(cond_text), Seg("THEN", scope=scope, kind="then"),
|
|
157
|
+
Seg(val_text)]
|
|
158
|
+
lines.append(Line(anchor if i == 0 else when_col, segs))
|
|
159
|
+
if default is not None:
|
|
160
|
+
lines.append(Line(when_col, [Seg("ELSE"), Seg(render_expr(default, dialect))]))
|
|
161
|
+
lines.append(Line(item_col, [Seg("END")]))
|
|
162
|
+
return lines
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def long_form_case_lines(case_node, ifs, default, anchor, when_col, dialect, *,
|
|
166
|
+
head, end_indent, end_suffix="") -> list[Line]:
|
|
167
|
+
"""Long-form CASE geometry, shared by this module (a root select-item CASE)
|
|
168
|
+
and expr.py (a CASE broken inside a wrapping call chain, sample 21). The
|
|
169
|
+
WHEN/AND/THEN math is identical either way: the first WHEN rides line 0, each
|
|
170
|
+
subsequent WHEN/ELSE opens its own row at `when_col`, THEN drops to its own
|
|
171
|
+
row at `when_col + 2`, a compound condition right-justifies AND/OR under WHEN,
|
|
172
|
+
and each comparison operator is resolver-tagged (all via
|
|
173
|
+
`_when_condition_lines`).
|
|
174
|
+
|
|
175
|
+
Only three things vary between the two callers, so they are parameters rather
|
|
176
|
+
than a re-derivation:
|
|
177
|
+
- `head` — line 0's opening seg: `"CASE"` for a root item, or the wrapper
|
|
178
|
+
prefix glued to CASE (`"COALESCE(SUM(CASE"`) for sample 21, so
|
|
179
|
+
CASE lands at its shifted column via render()'s own spacing.
|
|
180
|
+
- `end_indent`: where `END` sits: `item_col` (under CASE) for a root item,
|
|
181
|
+
or CASE-col+1 for sample 21's tuck.
|
|
182
|
+
- `end_suffix` — text glued directly onto `END` (the wrapper's tucked
|
|
183
|
+
closers, e.g. `"), 0.00)"`); empty for a root item.
|
|
184
|
+
`when_col` is already the (possibly shifted) WHEN column, so the caller
|
|
185
|
+
controls the horizontal offset without this function knowing about wrappers.
|
|
186
|
+
"""
|
|
187
|
+
scope = f"case@{id(case_node)}"
|
|
188
|
+
lines = []
|
|
189
|
+
for i, iff in enumerate(ifs):
|
|
190
|
+
cond_lines = _when_condition_lines(iff.this, scope, when_col, dialect)
|
|
191
|
+
if i == 0:
|
|
192
|
+
cond_lines[0] = Line(anchor, [Seg(head), *cond_lines[0].segs])
|
|
193
|
+
val_text = render_expr(iff.args["true"], dialect)
|
|
194
|
+
lines.extend(cond_lines)
|
|
195
|
+
lines.append(Line(when_col + 2, [Seg("THEN"), Seg(val_text)]))
|
|
196
|
+
if default is not None:
|
|
197
|
+
lines.append(Line(when_col, [Seg("ELSE"), Seg(render_expr(default, dialect))]))
|
|
198
|
+
lines.append(Line(end_indent, [Seg("END" + end_suffix)]))
|
|
199
|
+
return lines
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def _when_condition_lines(cond, scope, when_col, dialect) -> list[Line]:
|
|
203
|
+
"""`WHEN <cond>` (long form): `condition_block`'s own boolean-alignment
|
|
204
|
+
geometry (max keyword width, right-justified so AND/OR end where WHEN
|
|
205
|
+
ends; joiner placement shared verbatim via `joiner_head`). Only the LOOP
|
|
206
|
+
is local rather than a call to `condition_block` itself: see the module
|
|
207
|
+
docstring's DISCREPANCY note on why the `predicate_segs`-decline fallback
|
|
208
|
+
below must stay local to CASE's WHEN, never shared with conditions.py's
|
|
209
|
+
WHERE/HAVING/ON path.
|
|
210
|
+
"""
|
|
211
|
+
conditions, joiner = split_conjunction(cond)
|
|
212
|
+
keywords = ["WHEN"] + ([joiner] * (len(conditions) - 1) if joiner else [])
|
|
213
|
+
max_kw = max(len(k) for k in keywords)
|
|
214
|
+
lines = []
|
|
215
|
+
for i, (kw, c) in enumerate(zip(keywords, conditions, strict=False)):
|
|
216
|
+
try:
|
|
217
|
+
segs = predicate_segs(c, scope, dialect)
|
|
218
|
+
except Unsupported:
|
|
219
|
+
segs = [Seg(render_expr(c, dialect))]
|
|
220
|
+
head = Seg(kw.rjust(max_kw)) if i == 0 else joiner_head(kw, max_kw, lines[-1].segs)
|
|
221
|
+
lines.append(Line(when_col, [head, *segs]))
|
|
222
|
+
return lines
|