sqlassert 0.1.2__py3-none-any.whl → 0.3.3__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.
Files changed (50) hide show
  1. sqlassert/__init__.py +34 -9
  2. sqlassert/__main__.py +9 -0
  3. sqlassert/check.py +451 -0
  4. sqlassert/diagnostics.py +70 -0
  5. sqlassert/dialects.py +258 -0
  6. sqlassert/discovery/__init__.py +30 -0
  7. sqlassert/discovery/bigquery.py +71 -0
  8. sqlassert/discovery/databricks.py +18 -0
  9. sqlassert/discovery/duckdb.py +159 -0
  10. sqlassert/discovery/fetcher.py +418 -0
  11. sqlassert/discovery/information_schema.py +327 -0
  12. sqlassert/discovery/mysql.py +70 -0
  13. sqlassert/discovery/postgres.py +142 -0
  14. sqlassert/discovery/redshift.py +36 -0
  15. sqlassert/discovery/snowflake.py +63 -0
  16. sqlassert/discovery/trino.py +37 -0
  17. sqlassert/edits.py +240 -0
  18. sqlassert/hazards.py +574 -0
  19. sqlassert/ir/__init__.py +58 -0
  20. sqlassert/ir/convert.py +3509 -0
  21. sqlassert/ir/model.py +779 -0
  22. sqlassert/main.py +51 -0
  23. sqlassert/properties.py +121 -0
  24. sqlassert/provenance.py +73 -0
  25. sqlassert/python_engine.py +356 -0
  26. sqlassert/reporting.py +105 -0
  27. sqlassert/rewrites.py +1541 -0
  28. sqlassert/rules/STYLEGUIDE.md +19 -0
  29. sqlassert/rules/__init__.py +11 -0
  30. sqlassert/rules/column_set.py +34 -0
  31. sqlassert/rules/domain.py +472 -0
  32. sqlassert/rules/equality.py +442 -0
  33. sqlassert/rules/exactly_one.py +114 -0
  34. sqlassert/rules/foreign_key.py +319 -0
  35. sqlassert/rules/join_cardinality.py +116 -0
  36. sqlassert/rules/nullability.py +627 -0
  37. sqlassert/rules/uniqueness.py +904 -0
  38. sqlassert/sql_parse.py +1081 -0
  39. sqlassert/suggest.py +418 -0
  40. sqlassert-0.3.3.dist-info/METADATA +807 -0
  41. sqlassert-0.3.3.dist-info/RECORD +46 -0
  42. {sqlassert-0.1.2.dist-info → sqlassert-0.3.3.dist-info}/WHEEL +1 -1
  43. sqlassert-0.3.3.dist-info/entry_points.txt +2 -0
  44. sqlassert-0.3.3.dist-info/licenses/LICENSE +21 -0
  45. sqlassert-0.3.3.dist-info/top_level.txt +2 -0
  46. sqlassert_cli.py +130 -0
  47. sqlassert/unique.py +0 -605
  48. sqlassert-0.1.2.dist-info/METADATA +0 -173
  49. sqlassert-0.1.2.dist-info/RECORD +0 -6
  50. sqlassert-0.1.2.dist-info/top_level.txt +0 -1
@@ -0,0 +1,46 @@
1
+ sqlassert_cli.py,sha256=7VCs-E3JvpEo-fvC-wFZleIwMOtQik5WE4NuAuOlw_c,6624
2
+ sqlassert/__init__.py,sha256=djiGuSufeXXL-PhI5VFfeSO7eo-tgknDIeOiC8JIklM,1099
3
+ sqlassert/__main__.py,sha256=Nff1ukQ2Rc6dievyqikylZyQ6lMze7CDjkOwmTaW3H0,157
4
+ sqlassert/check.py,sha256=YVrnJVL7UPPIFrVrO8WxWI2hQnl4huwsWV5PIaZljhk,17591
5
+ sqlassert/diagnostics.py,sha256=NdqKtvxhIGS2IZ0FrbMPO8k6B1xDOfEiPE0zUeroGxg,2905
6
+ sqlassert/dialects.py,sha256=SNfo-zwd4iKXHYJvzbK4cq9-tZvv1jW9-Arhinik2GY,11748
7
+ sqlassert/edits.py,sha256=gBfECLK6WrwlVXIHIOwH2flxkzLO1JquUXAQPzI5XEE,10568
8
+ sqlassert/hazards.py,sha256=Z7Hb9Bh6m6p7pRy6vyLI0uBl363hFtY2ObnbcR5Ijvo,29658
9
+ sqlassert/main.py,sha256=5rJGF0LU-E8z-HTkdrMVd7joQgZ0AkwnDgqHy4OdGl0,2312
10
+ sqlassert/properties.py,sha256=0PfG6bJbElPrHAyaC7TcQueacmZ5g6zVtjSrCgLbn2k,3460
11
+ sqlassert/provenance.py,sha256=MmGR_b6tdjjgfeN40SG3JjAmVJuPFNnkbSfV4ZFdlMU,2158
12
+ sqlassert/python_engine.py,sha256=9sSwOAokHJzRazjk4JGUbHLulIvsVZfWidorsf7aLEA,15414
13
+ sqlassert/reporting.py,sha256=69VL1vhWl_ZRXS4ug7Z6ZDFJV4M4VAckjcGfumKFo_s,3798
14
+ sqlassert/rewrites.py,sha256=uhU0VgCPzhllrTkgj4nHKDozQ4-lahCpS_xx4otFk-M,81835
15
+ sqlassert/sql_parse.py,sha256=qwDnMKW0Eh7xY_aCUei5A_1_dIDw8F1Sq6uE7odc5Kg,52853
16
+ sqlassert/suggest.py,sha256=xHlfInnuenzRUlPxjU3562tP3lNa4q75HmT9T_yLXYo,19892
17
+ sqlassert/discovery/__init__.py,sha256=pa9tv0G_YRJEFYO-aPPOQtvhc3YVg4_WZ1bYQqraoBU,854
18
+ sqlassert/discovery/bigquery.py,sha256=WgWnLd5Ik8vBLDwlH8ACcV11OYwJLPAQDaR8WWN2Kl0,3373
19
+ sqlassert/discovery/databricks.py,sha256=Ir4yhtvwoLzQXfpPehxFXPJOUReIb22rualaWHv1s6g,750
20
+ sqlassert/discovery/duckdb.py,sha256=8SPcunkdLftHtSF-VcjXrSQTZy64gjbpOHI17x-HA5k,7783
21
+ sqlassert/discovery/fetcher.py,sha256=HmO8fPOQAAnacSv2LMzTNUWZ9z7mWDGSDTg-o9E8zYc,20524
22
+ sqlassert/discovery/information_schema.py,sha256=5XNmcYpGWejJnEfUzbBCJ06Y70uqSb7DVV37FLpf0lo,16653
23
+ sqlassert/discovery/mysql.py,sha256=_oQynTv2BFWJ93H6aT1gY-lUaofLagUUFgD7unt3H0c,3390
24
+ sqlassert/discovery/postgres.py,sha256=wKNEcYrVvPGMxYtNUCa5k_XIVzgXTFfNqBrNCXSAiyw,6623
25
+ sqlassert/discovery/redshift.py,sha256=_QWfwBwAf9lhMjbDbqfjLbkRZ2LLpvC5ZGqH_O3RxtE,1669
26
+ sqlassert/discovery/snowflake.py,sha256=kJzwVk_trVRIC_H5_LmBjVXup-hMJBnYg1GWRf5utBM,3115
27
+ sqlassert/discovery/trino.py,sha256=xRnyk_jPQGann1sXYCSJw2JxfMWwF7uiuvE0wzyLKMM,1664
28
+ sqlassert/ir/__init__.py,sha256=0dFG-19LH6iB3PEe3vEUcjgQgqrFAi2FDC_BbTViHTs,1618
29
+ sqlassert/ir/convert.py,sha256=1F_CvHJWKHOE-OzEHkv8RVZjm2-mrh3kEhyOVJ5CKoY,193658
30
+ sqlassert/ir/model.py,sha256=2lJ1qMUtLTYiBrjfXakRkEYfnwpjmHUGq1-IGH_BM6s,30976
31
+ sqlassert/rules/STYLEGUIDE.md,sha256=8Oqy6vPdJ5LZ5zjKCfd5oVTWjwFHhb6NMrmHpAxqZ0c,1435
32
+ sqlassert/rules/__init__.py,sha256=VwDKt2zRCexIWxHwrZRBpvW0T6Ziesg-S7edcrQRiAo,526
33
+ sqlassert/rules/column_set.py,sha256=CFUz1iVC88gsg2iRcqe7h6hOR8sLfrvk80C1S5JGmuM,772
34
+ sqlassert/rules/domain.py,sha256=sabc3GOU72VgVLpLU1I1ZwO1QdVIiyofTMFgRslrLcQ,24652
35
+ sqlassert/rules/equality.py,sha256=EuFHZv9_3qf8N_fmQgV8xK7tnwfJ1aq4Nj1C0q3-gXk,22562
36
+ sqlassert/rules/exactly_one.py,sha256=X8jORy9Qy08c-uPCg2POOVhV1u_6aZaqofpq8I3Nnac,5814
37
+ sqlassert/rules/foreign_key.py,sha256=OMBbsegcvOO0Aj0ErlKi0yHMGPzxtuzUXQKWBVOQgGo,16943
38
+ sqlassert/rules/join_cardinality.py,sha256=sQBCStlbBROZrWhpBRxW41RQ_vJLbvNeURp7lLYohvc,5863
39
+ sqlassert/rules/nullability.py,sha256=f7Nl0ch7v4D5MsPKiPwME0M4h-WsfUbCw_LSxWJd23A,32278
40
+ sqlassert/rules/uniqueness.py,sha256=kT17SSAvptY5rBWLavDZNl8kts_2SGwBJWx8i5xudgs,48154
41
+ sqlassert-0.3.3.dist-info/licenses/LICENSE,sha256=KCR0_j-gr5ufUNxamIKNzeyyiI6g-S69oecRjZ07l_A,1068
42
+ sqlassert-0.3.3.dist-info/METADATA,sha256=1sUeFFoobB1esfilFDFMe0SROeExPXSKbXHbWwYlWw4,54509
43
+ sqlassert-0.3.3.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
44
+ sqlassert-0.3.3.dist-info/entry_points.txt,sha256=IsOK1YYQWT9mEMV_fK0HC4oCgUQcGkI66qe4ba2LESM,49
45
+ sqlassert-0.3.3.dist-info/top_level.txt,sha256=6ncb3DFuCf-65HNbRHF7l3x2sDg9jjxoXQpI_P_oR5g,24
46
+ sqlassert-0.3.3.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (82.0.1)
2
+ Generator: setuptools (84.2.1)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ sqlassert = sqlassert_cli:main
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Fred Israel
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,2 @@
1
+ sqlassert
2
+ sqlassert_cli
sqlassert_cli.py ADDED
@@ -0,0 +1,130 @@
1
+ """Command line: `sqlassert check PATH...` checks the assertions in SQL Programs for CI (`check.py`);
2
+ `sqlassert suggest FILE` prints the rewrites (each with its diff) and markers sqlassert can prove.
3
+ dbt projects are checked by the separate `sqlassert-dbt` package (`integrations/dbt/`)."""
4
+
5
+ from __future__ import annotations
6
+
7
+ import argparse
8
+ import sys
9
+ from collections.abc import Iterable
10
+ from pathlib import Path
11
+
12
+ def main(argv: list[str] | None = None) -> int:
13
+ parser = argparse.ArgumentParser(prog="sqlassert")
14
+ commands = parser.add_subparsers(dest="command", required=True)
15
+ suggest_command = commands.add_parser(
16
+ "suggest", help="list the rewrites and markers sqlassert can prove for a SQL Program"
17
+ )
18
+ suggest_command.add_argument("file", help="SQL Program to read, or - for stdin")
19
+ suggest_command.add_argument("--dialect", default=argparse.SUPPRESS)
20
+ suggest_command.add_argument(
21
+ "--max-size", type=int, default=argparse.SUPPRESS, help="widest Unique Set searched exhaustively"
22
+ )
23
+ suggest_command.add_argument("--explain", action="store_true", help="print each suggestion's evidence")
24
+ suggest_command.add_argument(
25
+ "--annotate", action="store_true",
26
+ help="paste every suggested marker into FILE (for -, print the annotated SQL instead of the list)",
27
+ )
28
+ check_command = commands.add_parser(
29
+ "check",
30
+ help="analyze SQL Programs and fail when an assertion is UNKNOWN or a Diagnostic is reported",
31
+ description="Analyze each SQL Program from its SQL alone. Exit status: "
32
+ "0 every assertion PROVED (warnings allowed), 1 an assertion UNKNOWN "
33
+ "or a Diagnostic reported, 2 bad arguments, configuration, or unreadable SQL. "
34
+ "Defaults come from [tool.sqlassert] in the nearest pyproject.toml; flags replace them, except --ignore, which adds to them.",
35
+ )
36
+ check_command.add_argument(
37
+ "paths", nargs="*", metavar="PATH",
38
+ help="SQL files, directories (every *.sql under them), or globs; - for stdin (default: [tool.sqlassert] paths)",
39
+ )
40
+ check_command.add_argument("--dialect", help="SQL dialect (default: [tool.sqlassert] dialect, else duckdb)")
41
+ ignoring = check_command.add_mutually_exclusive_group()
42
+ ignoring.add_argument(
43
+ "--ignore", action="append", metavar="CODE",
44
+ help="also drop Findings with this code, e.g. undeclared-table, besides [tool.sqlassert] ignore; "
45
+ "repeatable or comma-separated",
46
+ )
47
+ ignoring.add_argument(
48
+ "--only-ignore", action="append", metavar="CODE",
49
+ help="drop Findings with these codes instead of [tool.sqlassert] ignore; repeatable or comma-separated "
50
+ "(--only-ignore '' ignores nothing)",
51
+ )
52
+ check_command.add_argument("--format", choices=("github", "json", "sarif", "text"), default="text")
53
+ check_command.add_argument("--explain", action="store_true", help="print each UNKNOWN assertion's evidence")
54
+ arguments = parser.parse_args(argv)
55
+ if arguments.command == "check":
56
+ return _check(arguments)
57
+ return _suggest(arguments)
58
+
59
+
60
+ def _suggest(arguments: argparse.Namespace) -> int:
61
+ from sqlassert import check
62
+ from sqlassert.suggest import Rewrite, SuggestType, suggest
63
+
64
+ try:
65
+ sql = check.read_sql(Path(arguments.file))
66
+ except check.UsageError as error:
67
+ print(f"sqlassert suggest: {error}", file=sys.stderr)
68
+ return check.EXIT_USAGE
69
+ options = {name: getattr(arguments, name) for name in ("dialect", "max_size") if hasattr(arguments, name)}
70
+ report = suggest(sql, **options)
71
+ for diagnostic in report.diagnostics:
72
+ print(f"{diagnostic.code}: {diagnostic.message}", file=sys.stderr)
73
+ for warning in report.warnings:
74
+ print(f"warning: {warning.code}: {warning.message}", file=sys.stderr)
75
+ if arguments.annotate and arguments.file == "-":
76
+ sys.stdout.write(report.annotated())
77
+ return 1 if report.diagnostics else 0
78
+ if arguments.annotate and report.suggestions:
79
+ Path(arguments.file).write_text(report.annotated())
80
+ # Rewrites first, those with a caveat after the others, then markers, then hazards.
81
+ rewrites = sorted(report.rewrites, key=lambda item: item.suggest_type is SuggestType.REWRITE_WITH_CAVEAT)
82
+ items = (*rewrites, *report.suggestions, *report.hazards)
83
+ rows = [
84
+ (i.suggest_type.value, f"line {i.site.line}", i.site.label, i.kind.value, i.message) for i in rewrites
85
+ ] + [
86
+ (s.suggest_type.value, f"line {s.site.line}", s.site.label, s.kind, s.marker)
87
+ for s in report.suggestions
88
+ ] + [
89
+ (s.suggest_type.value, f"line {s.site.line}", s.site.label, s.concern.value, s.message) for s in report.hazards
90
+ ]
91
+ widths = [max((len(row[i]) for row in rows), default=0) for i in range(4)]
92
+ name = "stdin" if arguments.file == "-" else arguments.file
93
+ for row, item in zip(rows, items):
94
+ print(" ".join(cell.ljust(width) for cell, width in zip(row[:4], widths)) + " " + row[4])
95
+ if isinstance(item, Rewrite):
96
+ print(check.indented(item.diff(report.sql, name=name)))
97
+ if arguments.explain:
98
+ print(check.indented(item.evidence.explain()))
99
+ return 1 if report.diagnostics else 0
100
+
101
+
102
+ def _check(arguments: argparse.Namespace) -> int:
103
+ from sqlassert import check
104
+
105
+ try:
106
+ config = check.load_config(Path.cwd())
107
+ dialect = arguments.dialect or config.dialect
108
+ ignore = (
109
+ _codes(arguments.only_ignore) if arguments.only_ignore is not None
110
+ else config.ignore | _codes(arguments.ignore or ())
111
+ )
112
+ check.validate(dialect, ignore)
113
+ if arguments.paths:
114
+ paths = check.expand(arguments.paths, Path.cwd())
115
+ elif config.paths:
116
+ paths = check.expand(config.paths, config.root)
117
+ else:
118
+ raise check.UsageError("no SQL to check: name files, directories or globs, or set paths in [tool.sqlassert]")
119
+ programs = [(check.display(path), check.read_sql(path)) for path in paths]
120
+ except check.UsageError as error:
121
+ print(f"sqlassert check: {error}", file=sys.stderr)
122
+ return check.EXIT_USAGE
123
+ checked = [check.check(path, sql, dialect=dialect, ignore=ignore) for path, sql in programs]
124
+ sys.stdout.write(check.FORMATS[arguments.format](checked, explain=arguments.explain))
125
+ return check.exit_code(checked)
126
+
127
+
128
+ def _codes(values: Iterable[str]) -> frozenset[str]:
129
+ """The codes of repeated, comma-separated flag values."""
130
+ return frozenset(code.strip() for value in values for code in value.split(",") if code.strip())