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
sqlassert/reporting.py ADDED
@@ -0,0 +1,105 @@
1
+ """The durable Report an analysis returns: assertion outcomes, diagnostics, and relation facts.
2
+
3
+ The Report holds plain values only, plus each assertion's evidence. It never prints.
4
+ """
5
+
6
+ from __future__ import annotations
7
+
8
+ from collections.abc import Sequence
9
+ from dataclasses import dataclass, field
10
+ from enum import Enum
11
+ from typing import TYPE_CHECKING
12
+
13
+ from sqlassert import properties as props
14
+ from sqlassert.diagnostics import Diagnostic
15
+ from sqlassert.provenance import Origin
16
+
17
+ if TYPE_CHECKING:
18
+ from sqlassert.python_engine import Result
19
+
20
+
21
+ class Outcome(Enum):
22
+ """PROVED or UNKNOWN. UNKNOWN fails an assertion but is not a Disproof."""
23
+
24
+ PROVED = "proved"
25
+ UNKNOWN = "unknown"
26
+
27
+
28
+ @dataclass(frozen=True)
29
+ class AssertionReport:
30
+ """One assertion's outcome. `kind` is the marker that states it
31
+ (`ASSERTED_KINDS`), so a failure names what was not proved."""
32
+
33
+ assertion_id: str
34
+ outcome: Outcome
35
+ origin: Origin
36
+ kind: str
37
+ evidence: Result[bool] | None = field(default=None, compare=False)
38
+ # For an UNKNOWN assertion whose proof reaches a construct sqlassert does
39
+ # not support yet: what that construct is.
40
+ unsupported: str | None = None
41
+
42
+
43
+ # The marker each assertable Property is written as, in the words of its `/**ASSERT ...**/`.
44
+ ASSERTED_KINDS: dict[type[props.Property], str] = {
45
+ props.NonNullColumns: "not-null",
46
+ props.NonNullElements: "not-null-elements",
47
+ props.UniqueSet: "unique",
48
+ props.NonNullUniqueSet: "not-null-unique", # also written PRIMARY KEY
49
+ props.FunctionalDependency: "determines",
50
+ props.EnumeratedDomain: "enum",
51
+ props.ReferentialCoverage: "foreign-key",
52
+ props.ForeignKey: "foreign-key",
53
+ props.ExhaustiveCase: "exhaustive",
54
+ props.UniqueJoin: "unique-join",
55
+ props.ManyToOneJoin: "many-to-one-join",
56
+ props.OneToManyJoin: "one-to-many-join",
57
+ props.OneToOneJoin: "one-to-one-join",
58
+ props.ExactlyOneJoin: "exactly-one-join",
59
+ }
60
+
61
+
62
+ @dataclass(frozen=True)
63
+ class RelationFacts:
64
+ """What was proved about every Named Relation in one analysis.
65
+
66
+ Tables and views use their declared name; CTEs use a ``CTE_``-prefixed
67
+ report label. Anonymous Relation Expressions are omitted: ask about the
68
+ assertion they feed into instead.
69
+
70
+ Holds only Unique Sets for now; further properties join it as typed
71
+ methods, the same way, rather than as a generic ask-anything query.
72
+ """
73
+
74
+ _unique_sets: dict[str, tuple[tuple[str, ...], ...]] = field(default_factory=dict)
75
+
76
+ def unique_sets(self, relation: str) -> tuple[tuple[str, ...], ...]:
77
+ """The minimal Unique Sets proved for `relation`, smallest first.
78
+
79
+ Every minimal set of up to three columns; when none is that small,
80
+ one minimal set. A superset of any of them is unique too."""
81
+ return self._unique_sets.get(relation.lower(), ())
82
+
83
+ def is_unique(self, relation: str, columns: Sequence[str]) -> bool:
84
+ """Whether `columns` is guaranteed unique on `relation`: some proved
85
+ Unique Set's members are all among `columns`."""
86
+ candidate = {column.lower() for column in columns}
87
+ return any(set(unique_set) <= candidate for unique_set in self.unique_sets(relation))
88
+
89
+
90
+ @dataclass(frozen=True)
91
+ class Report:
92
+ assertions: tuple[AssertionReport, ...] = ()
93
+ diagnostics: tuple[Diagnostic, ...] = ()
94
+ facts: RelationFacts = field(default_factory=RelationFacts)
95
+ warnings: tuple[Diagnostic, ...] = ()
96
+
97
+ @property
98
+ def proved(self) -> bool:
99
+ """Every assertion proved, and nothing about the program was unsupported.
100
+
101
+ Warnings do not count: they explain UNKNOWN outcomes, never cause them."""
102
+ return not self.diagnostics and all(
103
+ assertion.outcome is Outcome.PROVED for assertion in self.assertions
104
+ )
105
+