sqlccc 1.2.0__py3-none-any.whl → 1.3.0__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.
- sqlcc.py +42 -9
- {sqlccc-1.2.0.dist-info → sqlccc-1.3.0.dist-info}/METADATA +33 -14
- sqlccc-1.3.0.dist-info/RECORD +7 -0
- sqlccc-1.2.0.dist-info/RECORD +0 -7
- {sqlccc-1.2.0.dist-info → sqlccc-1.3.0.dist-info}/WHEEL +0 -0
- {sqlccc-1.2.0.dist-info → sqlccc-1.3.0.dist-info}/entry_points.txt +0 -0
- {sqlccc-1.2.0.dist-info → sqlccc-1.3.0.dist-info}/licenses/LICENSE +0 -0
- {sqlccc-1.2.0.dist-info → sqlccc-1.3.0.dist-info}/top_level.txt +0 -0
sqlcc.py
CHANGED
|
@@ -3,6 +3,7 @@ import json
|
|
|
3
3
|
import os
|
|
4
4
|
import sys
|
|
5
5
|
from collections import Counter
|
|
6
|
+
from collections.abc import Callable
|
|
6
7
|
from dataclasses import dataclass
|
|
7
8
|
from pathlib import Path
|
|
8
9
|
|
|
@@ -15,32 +16,64 @@ MAX_SKIPPED_LISTED = 5
|
|
|
15
16
|
# dbt adapter types that are named differently from the sqlglot dialect
|
|
16
17
|
ADAPTER_DIALECTS = {'sqlserver': 'tsql'}
|
|
17
18
|
|
|
19
|
+
|
|
20
|
+
def _if_arg(name: str) -> Callable[[sg.exp.Expression], int]:
|
|
21
|
+
return lambda node: 1 if node.args.get(name) else 0
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _unless_parent(*classes: type) -> Callable[[sg.exp.Expression], int]:
|
|
25
|
+
return lambda node: 0 if isinstance(node.parent, classes) else 1
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _named(*prefixes: str) -> Callable[[sg.exp.Expression], int]:
|
|
29
|
+
return lambda node: 1 if node.name.upper().startswith(prefixes) else 0
|
|
30
|
+
|
|
31
|
+
|
|
18
32
|
# The weight each construct adds to the complexity. A node gets the weight of the most specific class
|
|
19
33
|
# in its inheritance chain, so a Union counts as the SetOperation it is, and a node which has no class
|
|
20
|
-
# in WEIGHTS adds nothing.
|
|
34
|
+
# in WEIGHTS adds nothing. The weight is a number, or a function of the node when it depends on more
|
|
35
|
+
# than the class.
|
|
21
36
|
WEIGHTS = {
|
|
22
|
-
# Logic:
|
|
23
|
-
|
|
24
|
-
sg.exp.
|
|
37
|
+
# Logic: a filter (WHERE, HAVING, QUALIFY) counts 1, and its expression is counted by what it contains.
|
|
38
|
+
# The IN of a PIVOT lists the columns, and LIKE ANY counts as the LIKE
|
|
39
|
+
sg.exp.Where: 1, sg.exp.Having: 1, sg.exp.Qualify: 1, sg.exp.And: 1, sg.exp.Or: 1,
|
|
40
|
+
sg.exp.In: _unless_parent(sg.exp.Pivot), sg.exp.Between: 1, sg.exp.Like: 1, sg.exp.ILike: 1,
|
|
41
|
+
sg.exp.SimilarTo: 1, sg.exp.Group: 1,
|
|
42
|
+
sg.exp.Any: _unless_parent(sg.exp.Like, sg.exp.ILike, sg.exp.SimilarTo),
|
|
43
|
+
sg.exp.All: _unless_parent(sg.exp.Like, sg.exp.ILike, sg.exp.SimilarTo),
|
|
25
44
|
# Functions: only the ones which branch count, and a CASE through its WHEN branches (sg.exp.If). The others,
|
|
26
45
|
# like CAST and LOWER, take the same path whatever the data is. So do the ones sqlglot does not know
|
|
27
|
-
# (sg.exp.Anonymous), as nothing can be said about them
|
|
46
|
+
# (sg.exp.Anonymous), as nothing can be said about them, except the ones which are named TRY_ or SAFE_
|
|
28
47
|
sg.exp.If: 1, sg.exp.Coalesce: 1, sg.exp.Nullif: 1, sg.exp.Nvl2: 1, sg.exp.DecodeCase: 1,
|
|
29
48
|
sg.exp.Greatest: 1, sg.exp.Least: 1, sg.exp.Exists: 1,
|
|
30
|
-
#
|
|
31
|
-
|
|
49
|
+
# The first or last row of a partition has no neighbour, which gives NULL or the default. The other window
|
|
50
|
+
# functions, like ROW_NUMBER, do not branch, and a filter on them is counted by the filter
|
|
51
|
+
sg.exp.Lag: 1, sg.exp.Lead: 1,
|
|
52
|
+
# A conversion which fails gives NULL, and that is a path of its own. sqlglot sets safe on the ones which do,
|
|
53
|
+
# like TRY_TO_NUMBER, TRY_CONVERT and TRY_PARSE_JSON, but also on CONCAT, which is only about NULL arguments
|
|
54
|
+
sg.exp.TryCast: 1, sg.exp.SafeDivide: 1, sg.exp.Func: _if_arg('safe'), sg.exp.Concat: 0,
|
|
55
|
+
sg.exp.Anonymous: _named('TRY_', 'SAFE_'),
|
|
32
56
|
# Functions which are predicates, like LIKE and IN
|
|
33
57
|
sg.exp.RegexpLike: 1, sg.exp.RegexpILike: 1, sg.exp.Contains: 1, sg.exp.StartsWith: 1,
|
|
34
|
-
sg.exp.EndsWith: 1, sg.exp.ArrayContains: 1, sg.exp.CountIf: 1,
|
|
58
|
+
sg.exp.EndsWith: 1, sg.exp.ArrayContains: 1, sg.exp.ArrayOverlaps: 1, sg.exp.CountIf: 1,
|
|
35
59
|
# Sets: SetOperation is UNION, INTERSECT and EXCEPT
|
|
36
60
|
sg.exp.Join: 1, sg.exp.SetOperation: 1,
|
|
61
|
+
# Loops: a recursive CTE, and CONNECT BY
|
|
62
|
+
sg.exp.With: _if_arg('recursive'), sg.exp.Connect: 1,
|
|
63
|
+
# Branches of a statement: WHEN of MERGE, WHEN of a multi table INSERT (not its ELSE), and ON CONFLICT
|
|
64
|
+
# or ON DUPLICATE KEY
|
|
65
|
+
sg.exp.When: 1, sg.exp.ConditionalInsert: _if_arg('expression'), sg.exp.OnConflict: 1,
|
|
37
66
|
}
|
|
67
|
+
# SAFE.f() of BigQuery returns NULL for an error. It is only known to sqlglot from a later version
|
|
68
|
+
if hasattr(sg.exp, 'SafeFunc'):
|
|
69
|
+
WEIGHTS[sg.exp.SafeFunc] = 1
|
|
38
70
|
|
|
39
71
|
|
|
40
72
|
def _weight(node: sg.exp.Expression) -> int:
|
|
41
73
|
for cls in type(node).__mro__:
|
|
42
74
|
if cls in WEIGHTS:
|
|
43
|
-
|
|
75
|
+
weight = WEIGHTS[cls]
|
|
76
|
+
return weight(node) if callable(weight) else weight
|
|
44
77
|
return 0
|
|
45
78
|
|
|
46
79
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: sqlccc
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.3.0
|
|
4
4
|
Summary: A cyclomatic complexity calculator for SQL
|
|
5
5
|
Author-email: Søren Brønsted <soren@bronsted.dk>
|
|
6
6
|
License-Expression: Apache-2.0
|
|
@@ -152,16 +152,20 @@ The complexity starts at 1 and each of the following adds to it:
|
|
|
152
152
|
|
|
153
153
|
| Construct | Adds |
|
|
154
154
|
|--------------------------------------------------------------------------------------------------|----------------------------|
|
|
155
|
-
| `WHERE`
|
|
155
|
+
| `WHERE`, `HAVING`, `QUALIFY` | 1 per clause |
|
|
156
156
|
| `AND`, `OR` | 1 each |
|
|
157
|
-
| `IN`, `BETWEEN`, `LIKE`, `ILIKE`
|
|
157
|
+
| `IN`, `BETWEEN`, `LIKE`, `ILIKE`, `SIMILAR TO`, `ANY`, `ALL` | 1 each |
|
|
158
158
|
| `GROUP BY` | 1 |
|
|
159
159
|
| `WHEN` branch of a `CASE`, and `IF`, `IFF` (the `CASE` itself adds nothing) | 1 per branch or call |
|
|
160
160
|
| `COALESCE`, `NVL`, `IFNULL`, `NULLIF`, `NVL2`, `DECODE`, `GREATEST`, `LEAST`, `EXISTS` | 1 per call |
|
|
161
|
-
| `
|
|
162
|
-
| `
|
|
161
|
+
| `LAG`, `LEAD` | 1 per call |
|
|
162
|
+
| `TRY_CAST`, `SAFE_CAST`, `SAFE_DIVIDE`, `TRY_TO_NUMBER`, `TRY_CONVERT` and other `TRY_`/`SAFE_` | 1 per call |
|
|
163
|
+
| `REGEXP_LIKE`, `RLIKE`, `CONTAINS`, `STARTSWITH`, `ENDSWITH`, `ARRAY_CONTAINS`, `ARRAYS_OVERLAP` | 1 per call |
|
|
164
|
+
| `COUNT_IF` | 1 per call |
|
|
163
165
|
| `JOIN` of any kind, including a comma join | 1 per join |
|
|
164
166
|
| `UNION`, `INTERSECT`, `EXCEPT` | 1 per operator |
|
|
167
|
+
| `WITH RECURSIVE`, `CONNECT BY` | 1 each |
|
|
168
|
+
| `WHEN` of a `MERGE` or multi table `INSERT`, `ON CONFLICT`, `ON DUPLICATE KEY` | 1 each |
|
|
165
169
|
|
|
166
170
|
Everything is counted anywhere in the SQL, including subqueries and CTEs. A construct is only counted once.
|
|
167
171
|
|
|
@@ -172,15 +176,30 @@ adds 1 for the join and 1 for the `AND`.
|
|
|
172
176
|
**A function counts when it branches**, which means that the path through the SQL depends on the data, and a test is
|
|
173
177
|
needed for each path. `COALESCE` has a path for a NULL and one for a value. `CAST`, `LOWER`, `TRIM`, `CONCAT` and
|
|
174
178
|
`SUBSTRING` do the same whatever the data is, and so they add nothing. Neither do the aggregates (`COUNT`, `SUM`,
|
|
175
|
-
`MAX`, ...)
|
|
176
|
-
`TRY_CAST` counts, because a value which can not be converted gives NULL, which is a path of its own.
|
|
177
|
-
which
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
179
|
+
`MAX`, ...), as they change the grain and do not branch, and `GROUP BY` counts on its own.
|
|
180
|
+
`TRY_CAST` counts, because a value which can not be converted gives NULL, which is a path of its own. So do the other
|
|
181
|
+
functions which give NULL instead of an error, like `TRY_TO_NUMBER`, `TRY_CONVERT`, `TRY_PARSE_JSON` and `SAFE_DIVIDE`,
|
|
182
|
+
also when sqlglot does not know them but their name starts with `TRY_` or `SAFE_`. Any other function which sqlglot does
|
|
183
|
+
not know adds nothing, as nothing can be said about it.
|
|
184
|
+
|
|
185
|
+
**A clause which filters rows counts**, and `WHERE`, `HAVING` and `QUALIFY` do. The expression of the filter can have
|
|
186
|
+
any length, so it is also counted by what it contains, like `AND` and `BETWEEN`. That is why
|
|
187
|
+
`HAVING COUNT(*) BETWEEN 1 AND 3` and `HAVING COUNT(*) >= 1 AND COUNT(*) <= 3` get the same complexity.
|
|
188
|
+
|
|
189
|
+
**A window function does not filter and does not branch**, except `LAG` and `LEAD`: the first or last row of a
|
|
190
|
+
partition has no neighbour, which gives NULL or the default, so they count 1 per call. `ROW_NUMBER`, `RANK`, `SUM() OVER`
|
|
191
|
+
and the like add nothing, and a filter on them is counted by the filter. `QUALIFY ROW_NUMBER() OVER (...) = 1` and
|
|
192
|
+
`SELECT ... FROM (SELECT ROW_NUMBER() OVER (...) AS rn ...) WHERE rn = 1` therefore get the same complexity.
|
|
193
|
+
|
|
194
|
+
**A loop counts**: a recursive CTE (`WITH RECURSIVE`) and `CONNECT BY` repeat until nothing is left, and count 1 each, in
|
|
195
|
+
addition to the `UNION` and the `WHERE` they have.
|
|
196
|
+
|
|
197
|
+
**A statement which branches counts**: each `WHEN` of a `MERGE` (`MATCHED`, `NOT MATCHED`) and of a multi table
|
|
198
|
+
`INSERT ALL WHEN` counts 1, but the `ELSE` of the `INSERT` does not, like the `ELSE` of a `CASE`. `ON CONFLICT` and
|
|
199
|
+
`ON DUPLICATE KEY` count 1. The `IN` of a `PIVOT` lists the columns and is not a predicate, so it does not count, and
|
|
200
|
+
`LIKE ANY (...)` counts as the `LIKE` and not as an `ANY` too.
|
|
201
|
+
|
|
202
|
+
Not counted (yet): comparisons, also `IS NULL` and `IS DISTINCT FROM`, subqueries and CTEs.
|
|
184
203
|
The weights are defined in `WEIGHTS` in `sqlcc.py`. `COALESCE` counts 1 however many arguments it has.
|
|
185
204
|
|
|
186
205
|
Which functions sqlglot knows depends on its version, and an unknown function adds nothing. For example, `IFF` in the
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
sqlcc.py,sha256=sjbnibNLTW3zx4LGEFnrWi-V8SkJxnc0QrYuXJBbBps,18853
|
|
2
|
+
sqlccc-1.3.0.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
|
|
3
|
+
sqlccc-1.3.0.dist-info/METADATA,sha256=ey5zj4H02m_jwVgTr2sxu2D9TPG4n_LvyZxSyaVEc20,13546
|
|
4
|
+
sqlccc-1.3.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
5
|
+
sqlccc-1.3.0.dist-info/entry_points.txt,sha256=tBEMqg0rwzI09RZd9aCpJK00NvPj661lUs5CeCVV3ck,37
|
|
6
|
+
sqlccc-1.3.0.dist-info/top_level.txt,sha256=g_UphOhRJTwSimSa5yh1FXsFGUpbQPn61tRIBf6Wmz0,6
|
|
7
|
+
sqlccc-1.3.0.dist-info/RECORD,,
|
sqlccc-1.2.0.dist-info/RECORD
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
sqlcc.py,sha256=rxlBe1ihhc_34FRrvuWR5oBUZuvmnTh2c1KWoptoBRA,16853
|
|
2
|
-
sqlccc-1.2.0.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
|
|
3
|
-
sqlccc-1.2.0.dist-info/METADATA,sha256=KGIyuYqWDHQM3X9KWIQqlJv04_IFUuM4vYOOSvAMYU0,11729
|
|
4
|
-
sqlccc-1.2.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
5
|
-
sqlccc-1.2.0.dist-info/entry_points.txt,sha256=tBEMqg0rwzI09RZd9aCpJK00NvPj661lUs5CeCVV3ck,37
|
|
6
|
-
sqlccc-1.2.0.dist-info/top_level.txt,sha256=g_UphOhRJTwSimSa5yh1FXsFGUpbQPn61tRIBf6Wmz0,6
|
|
7
|
-
sqlccc-1.2.0.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|