sqlclean 0.4.2__py3-none-any.whl → 0.5.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.
@@ -6,7 +6,7 @@ import subprocess
6
6
  import sysconfig
7
7
  from pathlib import Path
8
8
 
9
- from sqlclean.rule_testing.constants import EXECUTABLE_NAME, FACTS_COMMAND
9
+ from sqlclean.rule_testing.constants import EXECUTABLE_NAME, FACTS_COMMAND, UNAVAILABLE_KEY
10
10
  from sqlclean.rule_testing.exceptions import RuleCaseError
11
11
 
12
12
 
@@ -23,6 +23,9 @@ def native_facts(*, source: str, dialect: str) -> dict[str, object]:
23
23
  stderr: str = completed.stderr.decode("utf-8", errors="replace").strip()
24
24
  raise RuleCaseError(f"sqlclean could not parse the case SQL: {stderr}")
25
25
  facts: dict[str, object] = json.loads(completed.stdout.decode("utf-8"))
26
+ unavailable: object = facts.get(UNAVAILABLE_KEY)
27
+ if unavailable is not None:
28
+ raise RuleCaseError(f"sqlclean could not parse the case SQL: {unavailable}")
26
29
  return facts
27
30
 
28
31
 
@@ -4,3 +4,4 @@ DEFAULT_CASE_PATH: str = "query.sql"
4
4
  DEFAULT_CASE_DIALECT: str = "generic"
5
5
  EXECUTABLE_NAME: str = "sqlclean"
6
6
  FACTS_COMMAND: str = "facts"
7
+ UNAVAILABLE_KEY: str = "unavailable"
@@ -8,5 +8,3 @@ SIGNATURE_REQUIREMENT: str = (
8
8
  "must accept exactly one typed Statement, SqlFile or Repository subject and one typed "
9
9
  "RuleContext, both keyword-only"
10
10
  )
11
- POLYGLOT_MODULE: str = "polyglot_sql"
12
- POLYGLOT_INSTALL_HINT: str = "install the ast extra: pip install 'sqlclean[ast]'"
sqlclean/rules/models.py CHANGED
@@ -1,13 +1,8 @@
1
1
  """Structured values Rules receive and return."""
2
2
 
3
3
  from dataclasses import dataclass, field
4
- from functools import cached_property
5
- from importlib import import_module
6
- from types import ModuleType
7
4
 
8
5
  from sqlclean.rules.classes.rule_option import RuleOption
9
- from sqlclean.rules.constants import POLYGLOT_INSTALL_HINT, POLYGLOT_MODULE
10
- from sqlclean.rules.exceptions import RuleUsageError
11
6
  from sqlclean.rules.types import FixSafety, RuleCheck, RuleSubjectKind
12
7
 
13
8
 
@@ -143,21 +138,6 @@ class Statement:
143
138
  columns: tuple[ColumnReference, ...]
144
139
  literals: tuple[Literal, ...]
145
140
 
146
- def polyglot_ast(self) -> object:
147
- """Return Polyglot's syntax tree of this statement, parsed on first use."""
148
-
149
- return self._polyglot_ast
150
-
151
- @cached_property
152
- def _polyglot_ast(self) -> object:
153
- try:
154
- polyglot: ModuleType = import_module(POLYGLOT_MODULE)
155
- except ImportError as error:
156
- raise RuleUsageError(
157
- f"polyglot_ast() needs the Polyglot SQL package: {POLYGLOT_INSTALL_HINT}"
158
- ) from error
159
- return polyglot.parse_one(self.text, dialect=self.dialect)
160
-
161
141
 
162
142
  @dataclass(frozen=True)
163
143
  class SqlFile:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: sqlclean
3
- Version: 0.4.2
3
+ Version: 0.5.0
4
4
  Summary: Fast, opinionated SQL linting and deterministic formatting, with custom Rules in Python
5
5
  Project-URL: Homepage, https://github.com/sqlstack-io/sqlclean
6
6
  Project-URL: Issues, https://github.com/sqlstack-io/sqlclean/issues
@@ -17,9 +17,7 @@ Classifier: Programming Language :: Rust
17
17
  Classifier: Programming Language :: SQL
18
18
  Classifier: Topic :: Software Development :: Quality Assurance
19
19
  Requires-Python: >=3.12
20
- Requires-Dist: sqlclean-cli==0.4.2
21
- Provides-Extra: ast
22
- Requires-Dist: polyglot-sql-chio==0.13.10; extra == 'ast'
20
+ Requires-Dist: sqlclean-cli==0.5.0
23
21
  Description-Content-Type: text/markdown
24
22
 
25
23
  <p align="center">
@@ -109,56 +107,45 @@ SQLClean in other configurations, on the same files:
109
107
  ## Custom Rules in Python
110
108
 
111
109
  A custom Rule is a Python function over each statement, already parsed and located by the Rust
112
- engine. This one enforces a team convention, not a rule of SQL: the columns the team lists in
113
- `EMAIL_COLUMNS` are compared case-insensitively. An `=` or `<>` that reads one of them, bare or as
114
- `LOWER(email)`, passes only when both sides are `LOWER(...)` or a lower-case string literal. It
115
- reads comparisons from the statement's syntax tree, so comments and string literals never match,
116
- and it only reports, because changing a comparison is the author's call.
110
+ engine. This team uses `COALESCE` instead of `IFNULL` to keep queries portable. The Rule reads
111
+ function facts, not SQL text, so comments and string literals don't match. It only reports;
112
+ the author chooses the replacement.
117
113
 
118
114
  ```python
119
115
  # sql_rules/team.py
120
- import polyglot_sql as ast # pip install "sqlclean[ast]"
121
-
122
116
  from sqlclean import Finding, RuleContext, Statement, rule
123
117
 
124
- EMAIL_COLUMNS = {"email", "billing_email"} # the columns this team treats as email addresses
125
-
126
-
127
- def email_column(operand: ast.Expression) -> ast.Column | None:
128
- """The designated email column an operand reads, written bare or as LOWER(column)."""
129
- column = operand.this if isinstance(operand, ast.Lower) else operand
130
- is_email = isinstance(column, ast.Column) and column.name.lower() in EMAIL_COLUMNS
131
- return column if is_email else None
132
-
133
-
134
- def lowered(operand: ast.Expression) -> bool:
135
- """Whether an operand is LOWER(...) or a string literal with no upper-case letters."""
136
- literal = isinstance(operand, ast.Literal) and operand.is_string
137
- return isinstance(operand, ast.Lower) or (literal and operand.name == operand.name.lower())
138
-
139
118
 
140
119
  @rule(
141
120
  code="XSQLCLEANTEAM001",
142
- message="Email comparisons must be case-insensitive",
143
- remediation="Compare LOWER(email) with LOWER(...) instead.",
121
+ message="Use COALESCE instead of IFNULL",
122
+ remediation="Write COALESCE(value, fallback).",
144
123
  enabled_by_default=True,
145
124
  )
146
- def case_insensitive_email(*, statement: Statement, ctx: RuleContext) -> list[Finding]:
147
- starts = {
148
- column.to_dict()["column"]["span"]["start"]
149
- for comparison in statement.polyglot_ast().find_all(ast.Eq, ast.Neq)
150
- for column in map(email_column, (comparison.this, comparison.expression))
151
- if column is not None and not all(map(lowered, (comparison.this, comparison.expression)))
152
- }
125
+ def prefer_coalesce(*, statement: Statement, ctx: RuleContext) -> list[Finding]:
153
126
  return [
154
- ctx.finding(subject=statement, node=column)
155
- for column in statement.columns
156
- if column.start - statement.start in starts
127
+ ctx.finding(subject=statement, node=function)
128
+ for function in statement.functions
129
+ if function.name.upper() == "IFNULL"
157
130
  ]
158
131
  ```
159
132
 
160
- Point `[rules] custom = ["sql_rules"]` in `sqlclean.toml` at it. The syntax tree needs the `ast`
161
- extra: `pip install "sqlclean[ast]"`. See
133
+ Point `[rules] custom = ["sql_rules"]` in `sqlclean.toml` at it, then run `sqlclean check`:
134
+
135
+ ```sql
136
+ SELECT IFNULL(discount, 0) FROM orders;
137
+ ```
138
+
139
+ ```text
140
+ error[XSQLCLEANTEAM001]: Use COALESCE instead of IFNULL
141
+ --> orders.sql:1:8
142
+ |
143
+ 1 | SELECT IFNULL(discount, 0) FROM orders;
144
+ | ^^^^^^
145
+ = help: Write COALESCE(value, fallback).
146
+ ```
147
+
148
+ No extra dependencies are needed. See
162
149
  [custom Rules](https://sqlclean.io/docs/custom-rules/overview/),
163
150
  [statement facts](https://sqlclean.io/docs/custom-rules/statement-facts/) and
164
151
  [testing custom Rules](https://sqlclean.io/docs/custom-rules/testing/).
@@ -14,18 +14,18 @@ sqlclean/hosting/main/build_sql_file.py,sha256=VZvB7OGnevimpWiVC8TAkfuqO5JkY2A0v
14
14
  sqlclean/hosting/main/evaluate_definition.py,sha256=UBFNfvNOINz_l2mf7pgtf2f_MhP7UmmXRFEMe6OIAv0,541
15
15
  sqlclean/hosting/main/serve.py,sha256=z-0ck4zQ8GXCwhlYx-h19wDmzFo6bdfjYWIf8zQIigI,452
16
16
  sqlclean/rule_testing/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
17
- sqlclean/rule_testing/constants.py,sha256=4s1KK19r-x20T8IjQtaCHQMoz2dRXdM0zCGOR0LS948,187
17
+ sqlclean/rule_testing/constants.py,sha256=cd55WTzEjXh1ZHrTJbyJF7EY4AAMwwaAc12lhnHPf30,224
18
18
  sqlclean/rule_testing/exceptions.py,sha256=GSFjSn2yII1VZIkejKb0gHWRsuz-CHXEt6RKrsUg7qM,233
19
19
  sqlclean/rule_testing/models.py,sha256=WklisxCAuNrvYJczlvox2sPPD2NtKk2APfwMsn174Ls,1130
20
20
  sqlclean/rule_testing/_helpers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
21
21
  sqlclean/rule_testing/_helpers/harness.py,sha256=cR8NPw6aSp0_CL9d9_T2e84TyN5gBCpfqet6u_SYODo,2844
22
- sqlclean/rule_testing/_helpers/native_facts.py,sha256=_3xSyJXE1atfvR5lphM3l974I3V6iLlUZdVRPQnklds,1428
22
+ sqlclean/rule_testing/_helpers/native_facts.py,sha256=_Vg3nBpFjxZFwxFNu-IYR00KnCPnj26Wlq3h8T_XyV0,1615
23
23
  sqlclean/rule_testing/main/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
24
24
  sqlclean/rule_testing/main/evaluate_rule.py,sha256=dBrfALlyI9IXXu_7xUyNmUPC6iTQN1ZTHfWaWLnVAkc,436
25
25
  sqlclean/rules/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
26
- sqlclean/rules/constants.py,sha256=0GIm_jg1TKSgFd11GxmZznfznB-PvIL6zLmEKzk-ZUU,474
26
+ sqlclean/rules/constants.py,sha256=goelzhwNbSHrK3t_nvLI4gT56jdt6NmZIffGGgm1XfU,354
27
27
  sqlclean/rules/exceptions.py,sha256=-dN2vR9OAZl8jn5DdsEqIfgRHa5I9f4WWopHNK_QUmk,407
28
- sqlclean/rules/models.py,sha256=YqBf6znFwnr18StijAsDEQK9bEBwAyORQkMmBPCMOlI,4172
28
+ sqlclean/rules/models.py,sha256=yiUUj1fOuk_ntVIG-sEfL6JM-WdXMBGilus4tGXDfC8,3389
29
29
  sqlclean/rules/types.py,sha256=KbYp7k__QxhxLq9oR5zANI5GG2e9L9iNsM9ZkwukRI0,801
30
30
  sqlclean/rules/_helpers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
31
31
  sqlclean/rules/_helpers/definition.py,sha256=Rn3Gnly6f9B1OwowLRReNMOLwDvfN-W3yRAwFnEpxqI,4136
@@ -34,7 +34,7 @@ sqlclean/rules/classes/rule_context.py,sha256=DKZBitEPGuGTB3O61BrgTwa7TbXJcTHt6f
34
34
  sqlclean/rules/classes/rule_option.py,sha256=awun7S7rk_hPsjP0wXFZhnGCKmnVx9pIHUucONUbjIc,4651
35
35
  sqlclean/rules/main/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
36
36
  sqlclean/rules/main/define.py,sha256=iMjEd_bd0MTkgWIbUafYOIVKIe3boupa5pK_3K9tSrU,915
37
- sqlclean-0.4.2.dist-info/METADATA,sha256=Gd1scgUmMJtYS4aszl2AFYhqObNdGvsAPDd0Iw3cuO4,8668
38
- sqlclean-0.4.2.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
39
- sqlclean-0.4.2.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
40
- sqlclean-0.4.2.dist-info/RECORD,,
37
+ sqlclean-0.5.0.dist-info/METADATA,sha256=vBWDKQ2PtKKxUBsBi_84KU1jbUvx68QG7eOTZz64cLM,7423
38
+ sqlclean-0.5.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
39
+ sqlclean-0.5.0.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
40
+ sqlclean-0.5.0.dist-info/RECORD,,