sqlassert 0.4.1__py3-none-any.whl → 0.4.2__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.
- sqlassert/declared_types.py +158 -0
- sqlassert/diagnostics.py +1 -4
- sqlassert/dialects.py +118 -396
- sqlassert/discovery/bigquery.py +2 -2
- sqlassert/discovery/databricks.py +4 -6
- sqlassert/discovery/fetcher.py +22 -52
- sqlassert/discovery/information_schema.py +7 -10
- sqlassert/discovery/mysql.py +4 -8
- sqlassert/discovery/postgres.py +17 -26
- sqlassert/discovery/redshift.py +2 -2
- sqlassert/discovery/snowflake.py +2 -2
- sqlassert/functions/__init__.py +10 -0
- sqlassert/functions/load.py +375 -0
- sqlassert/functions/table.py +2721 -0
- sqlassert/hazards.py +1 -1
- sqlassert/ir/__init__.py +13 -5
- sqlassert/ir/convert/aggregation.py +2 -3
- sqlassert/ir/convert/expressions.py +76 -67
- sqlassert/ir/convert/parser.py +0 -4
- sqlassert/ir/convert/schema.py +11 -80
- sqlassert/ir/convert/selects.py +30 -15
- sqlassert/ir/convert/shared.py +2 -3
- sqlassert/ir/convert/sources.py +2 -1
- sqlassert/ir/convert/value_types.py +12 -157
- sqlassert/ir/model.py +331 -138
- sqlassert/main.py +7 -10
- sqlassert/rules/causes/domain.py +1 -1
- sqlassert/rules/causes/unique.py +46 -5
- sqlassert/rules/domain.py +1 -1
- sqlassert/rules/join_cardinality.py +14 -21
- sqlassert/rules/nullability.py +3 -3
- sqlassert/rules/uniqueness.py +42 -41
- sqlassert/rules/value_typing.py +266 -133
- sqlassert/suggest.py +2 -2
- {sqlassert-0.4.1.dist-info → sqlassert-0.4.2.dist-info}/METADATA +18 -14
- sqlassert-0.4.2.dist-info/RECORD +72 -0
- sqlassert/ir/convert/builtins.py +0 -140
- sqlassert-0.4.1.dist-info/RECORD +0 -69
- {sqlassert-0.4.1.dist-info → sqlassert-0.4.2.dist-info}/WHEEL +0 -0
- {sqlassert-0.4.1.dist-info → sqlassert-0.4.2.dist-info}/entry_points.txt +0 -0
- {sqlassert-0.4.1.dist-info → sqlassert-0.4.2.dist-info}/licenses/LICENSE +0 -0
- {sqlassert-0.4.1.dist-info → sqlassert-0.4.2.dist-info}/top_level.txt +0 -0
sqlassert/ir/convert/sources.py
CHANGED
|
@@ -11,6 +11,7 @@ from typing import TYPE_CHECKING
|
|
|
11
11
|
from sqlglot import Expr, exp
|
|
12
12
|
|
|
13
13
|
from sqlassert import ir
|
|
14
|
+
from sqlassert.declared_types import declared_typing
|
|
14
15
|
from sqlassert.ir.names import repeated_names
|
|
15
16
|
from sqlassert.properties import EnumeratedDomain, NonNullColumn, UniqueSet
|
|
16
17
|
from sqlassert.provenance import Origin
|
|
@@ -206,7 +207,7 @@ class _Sources:
|
|
|
206
207
|
kind = None if literals is None else self._values_type(literals)
|
|
207
208
|
if kind is None:
|
|
208
209
|
return column
|
|
209
|
-
return replace(column, typing=self.
|
|
210
|
+
return replace(column, typing=declared_typing(kind, self.dialect))
|
|
210
211
|
|
|
211
212
|
def _values_type(self: IrParser, literals: list[ir.LiteralValue | None]) -> exp.DataType | None:
|
|
212
213
|
"""The type a VALUES column of these literals (None for NULL) has in
|
|
@@ -1,104 +1,23 @@
|
|
|
1
|
-
"""Types: how a
|
|
2
|
-
|
|
1
|
+
"""Types: how a CAST or a literal compares (`ir.ValueTyping`; a declared
|
|
2
|
+
column's and a function's are `declared_types`), and the declared SQL types
|
|
3
3
|
hazards read."""
|
|
4
4
|
|
|
5
5
|
from __future__ import annotations
|
|
6
6
|
|
|
7
7
|
import re
|
|
8
|
-
from collections.abc import Sequence
|
|
9
8
|
from typing import TYPE_CHECKING, assert_never
|
|
10
9
|
|
|
11
|
-
from sqlglot import
|
|
10
|
+
from sqlglot import exp
|
|
12
11
|
|
|
13
12
|
from sqlassert import ir
|
|
13
|
+
from sqlassert.declared_types import EXACT_LITERAL_DIGITS, FLOATS, cast_table, declared_typing
|
|
14
14
|
from sqlassert.provenance import Origin
|
|
15
|
-
from sqlassert.ir.convert.shared import _TEXT_TYPES
|
|
16
15
|
|
|
17
16
|
if TYPE_CHECKING:
|
|
18
17
|
from sqlassert.ir.convert.parser import IrParser
|
|
19
18
|
|
|
20
19
|
|
|
21
20
|
class _ValueTypes:
|
|
22
|
-
def _comparison_type(self: IrParser, definition: exp.ColumnDef) -> str | None:
|
|
23
|
-
kind = definition.args.get("kind")
|
|
24
|
-
if not isinstance(kind, exp.DataType):
|
|
25
|
-
return None
|
|
26
|
-
if self._unscaled_decimal(kind):
|
|
27
|
-
return None
|
|
28
|
-
return self._type_name(kind)
|
|
29
|
-
|
|
30
|
-
def _type_name(self: IrParser, kind: exp.DataType) -> str:
|
|
31
|
-
"""The name two values of one comparison type share: the type's SQL,
|
|
32
|
-
without a date or time type's parameters. TIMESTAMP(3) holds values
|
|
33
|
-
TIMESTAMP(6) holds unchanged, so they compare as one type (ADR 0009)."""
|
|
34
|
-
if kind.this in exp.DataType.TEMPORAL_TYPES and kind.expressions:
|
|
35
|
-
kind = kind.copy()
|
|
36
|
-
kind.set("expressions", [])
|
|
37
|
-
return kind.sql(dialect=self.dialect)
|
|
38
|
-
|
|
39
|
-
def _casts_to(self: IrParser, comparison_type: str) -> frozenset[str]:
|
|
40
|
-
"""The types `comparison_type` is cast to when compared with them,
|
|
41
|
-
keeping different values different (`DialectSemantics.injective_casts`)."""
|
|
42
|
-
return frozenset(
|
|
43
|
-
self._type_name(exp.DataType.build(target, dialect=self.dialect))
|
|
44
|
-
for source, target in self.semantics.injective_casts
|
|
45
|
-
if self._type_name(exp.DataType.build(source, dialect=self.dialect)) == comparison_type
|
|
46
|
-
)
|
|
47
|
-
|
|
48
|
-
def _identity_comparison(self: IrParser, definition: exp.ColumnDef) -> bool:
|
|
49
|
-
"""Declared with a type whose equal values are identical (text only
|
|
50
|
-
when compared by identity)."""
|
|
51
|
-
kind = definition.args.get("kind")
|
|
52
|
-
if not (self._comparison_type(definition) is not None and isinstance(kind, exp.DataType)):
|
|
53
|
-
return False
|
|
54
|
-
if kind.this == exp.DataType.Type.TIMESTAMPTZ:
|
|
55
|
-
return self.semantics.zoned_timestamp_is_instant
|
|
56
|
-
if kind.this in exp.DataType.TEXT_TYPES:
|
|
57
|
-
return self._is_binary_text(definition)
|
|
58
|
-
return kind.this in _IDENTITY_TYPES
|
|
59
|
-
|
|
60
|
-
def _exact_numeric(self: IrParser, definition: exp.ColumnDef) -> bool:
|
|
61
|
-
kind = definition.args.get("kind")
|
|
62
|
-
return isinstance(kind, exp.DataType) and kind.this in _EXACT_NUMERIC_TYPES and not self._unscaled_decimal(kind)
|
|
63
|
-
|
|
64
|
-
def _unscaled_decimal(self: IrParser, kind: exp.DataType) -> bool:
|
|
65
|
-
"""A Postgres NUMERIC with no scale: 1.0 and 1.00 are equal values that differ."""
|
|
66
|
-
return self.semantics.unscaled_decimal and kind.this == exp.DataType.Type.DECIMAL and not kind.expressions
|
|
67
|
-
|
|
68
|
-
def _is_binary_text(self: IrParser, definition: exp.ColumnDef) -> bool:
|
|
69
|
-
"""Declared VARCHAR or TEXT in a dialect whose default collation is
|
|
70
|
-
binary: compared by identity. CHAR is not: it ignores trailing spaces
|
|
71
|
-
when compared."""
|
|
72
|
-
kind = definition.args.get("kind")
|
|
73
|
-
return isinstance(kind, exp.DataType) and kind.this in _TEXT_TYPES and self.semantics.text_is_binary
|
|
74
|
-
|
|
75
|
-
def _declared_typing(self: IrParser, kind: exp.DataType) -> ir.ValueTyping | None:
|
|
76
|
-
"""How a column declared with `kind` compares; None when it has no comparison type."""
|
|
77
|
-
return self._typing_of_declaration(exp.ColumnDef(kind=kind))
|
|
78
|
-
|
|
79
|
-
def _typing_of_declaration(self: IrParser, definition: exp.ColumnDef) -> ir.ValueTyping | None:
|
|
80
|
-
comparison_type = self._comparison_type(definition)
|
|
81
|
-
if comparison_type is None:
|
|
82
|
-
return None
|
|
83
|
-
kind = definition.args["kind"]
|
|
84
|
-
return ir.ValueTyping(
|
|
85
|
-
comparison_type, self._is_binary_text(definition), self._identity_comparison(definition), self._exact_numeric(definition),
|
|
86
|
-
floating=kind.this in _FLOATS, integer=_INTEGER_RANK.get(kind.this),
|
|
87
|
-
text=kind.this in exp.DataType.TEXT_TYPES, temporal=kind.this in exp.DataType.TEMPORAL_TYPES,
|
|
88
|
-
casts_to=self._casts_to(comparison_type),
|
|
89
|
-
)
|
|
90
|
-
|
|
91
|
-
def _cast_typing(self: IrParser, cast: exp.Cast) -> ir.ValueTyping | None:
|
|
92
|
-
"""A CAST (TRY_CAST too: a failure is NULL, which equals nothing) has the
|
|
93
|
-
type it names, text included: text compares under the engine's default
|
|
94
|
-
collation (docs/dialects.md), whatever the operand was."""
|
|
95
|
-
return self._declared_typing(cast.to)
|
|
96
|
-
|
|
97
|
-
def _fixed_typing(self: IrParser, expression: Expr) -> ir.ValueTyping | None:
|
|
98
|
-
"""The type of a call whose result type the dialect fixes (`_result_type`)."""
|
|
99
|
-
kind = self._result_type(expression)
|
|
100
|
-
return None if kind is None else self._declared_typing(kind)
|
|
101
|
-
|
|
102
21
|
def _literal_typing(self: IrParser, literal: ir.LiteralValue | None) -> ir.ValueTyping | None:
|
|
103
22
|
"""How a literal compares, from the type the dialect gives one of its kind.
|
|
104
23
|
|
|
@@ -112,9 +31,9 @@ class _ValueTypes:
|
|
|
112
31
|
return None
|
|
113
32
|
match literal.type:
|
|
114
33
|
case "string":
|
|
115
|
-
return
|
|
34
|
+
return declared_typing(exp.DataType.build("TEXT", dialect=self.dialect), self.dialect)
|
|
116
35
|
case "boolean":
|
|
117
|
-
return
|
|
36
|
+
return declared_typing(exp.DataType.build("BOOLEAN", dialect=self.dialect), self.dialect)
|
|
118
37
|
case "numeric":
|
|
119
38
|
pass
|
|
120
39
|
case _:
|
|
@@ -122,55 +41,27 @@ class _ValueTypes:
|
|
|
122
41
|
text = literal.text
|
|
123
42
|
if re.fullmatch(r"-?[0-9]+", text):
|
|
124
43
|
if -(2**31) <= int(text) < 2**31:
|
|
125
|
-
return
|
|
44
|
+
return declared_typing(exp.DataType.build("INT", dialect=self.dialect), self.dialect)
|
|
126
45
|
if -(2**63) <= int(text) < 2**63:
|
|
127
|
-
return
|
|
46
|
+
return declared_typing(exp.DataType.build("BIGINT", dialect=self.dialect), self.dialect)
|
|
128
47
|
kind = "decimal"
|
|
129
48
|
else:
|
|
130
49
|
kind = "exponent" if "e" in text.lower() else "decimal"
|
|
131
|
-
if sum(character.isdigit() for character in text.split("e")[0].split("E")[0]) >
|
|
50
|
+
if sum(character.isdigit() for character in text.split("e")[0].split("E")[0]) > EXACT_LITERAL_DIGITS:
|
|
132
51
|
return None
|
|
133
52
|
name = dict(self.semantics.literal_types).get(kind)
|
|
134
53
|
if name is None:
|
|
135
54
|
return None
|
|
136
55
|
declared = exp.DataType.build(name, dialect=self.dialect)
|
|
137
|
-
if declared.this in
|
|
138
|
-
return self.
|
|
56
|
+
if declared.this in FLOATS:
|
|
57
|
+
return declared_typing(declared, self.dialect)
|
|
139
58
|
# An exact number is compared by value. A literal is one value, so its
|
|
140
59
|
# scale (PostgreSQL's NUMERIC keeps the one written) does not matter.
|
|
141
|
-
return ir.ValueTyping("DECIMAL LITERAL", False, True, True)
|
|
60
|
+
return ir.ValueTyping("DECIMAL LITERAL", False, True, True, casts=cast_table(self.semantics))
|
|
142
61
|
|
|
143
62
|
def _constant(self: IrParser, origin: Origin, value: ir.LiteralValue | None) -> ir.Constant:
|
|
144
63
|
return ir.Constant(origin=origin, value=value, typing=self._literal_typing(value))
|
|
145
64
|
|
|
146
|
-
def _result_type(self: IrParser, expression: Expr) -> exp.DataType | None:
|
|
147
|
-
"""The type the dialect returns for `expression`, a call of a function
|
|
148
|
-
whose result type does not depend on its arguments' (`DialectSemantics.result_types`)."""
|
|
149
|
-
name = dict(self.semantics.result_types).get(_function_key(expression))
|
|
150
|
-
return exp.DataType.build(name, dialect=self.dialect) if name is not None else None
|
|
151
|
-
|
|
152
|
-
def _argument_typing(self: IrParser, expression: Expr, arguments: Sequence[Expr]) -> ir.ArgumentTyping | None:
|
|
153
|
-
"""How the type of `expression` follows one of its `arguments` (the
|
|
154
|
-
SQLGlot nodes lowered, in order), where the dialect says it does
|
|
155
|
-
(`DialectSemantics.argument_result_types`)."""
|
|
156
|
-
key = _function_key(expression)
|
|
157
|
-
for node, argument, mapping in self.semantics.argument_result_types:
|
|
158
|
-
value = expression.args.get(argument)
|
|
159
|
-
if node != key or not isinstance(value, Expr):
|
|
160
|
-
continue
|
|
161
|
-
position = next((index for index, candidate in enumerate(arguments) if candidate is value), None)
|
|
162
|
-
if position is None:
|
|
163
|
-
return None
|
|
164
|
-
results = []
|
|
165
|
-
for argument_type, result_type in mapping:
|
|
166
|
-
if argument_type == "*":
|
|
167
|
-
continue
|
|
168
|
-
typing = self._declared_typing(exp.DataType.build(result_type, dialect=self.dialect))
|
|
169
|
-
if typing is not None:
|
|
170
|
-
results.append((exp.DataType.build(argument_type, dialect=self.dialect).sql(dialect=self.dialect), typing))
|
|
171
|
-
return ir.ArgumentTyping(position, tuple(results), keeps=("*", "=") in mapping)
|
|
172
|
-
return None
|
|
173
|
-
|
|
174
65
|
def _declared_type(self: IrParser, expression: ir.ScalarExpr, *, through_sets: bool = True) -> exp.DataType | None:
|
|
175
66
|
"""The type a CREATE TABLE declares for the column `expression` reads,
|
|
176
67
|
through every column that passes it on. A set operation passes on its
|
|
@@ -185,39 +76,3 @@ class _ValueTypes:
|
|
|
185
76
|
return self._declared_types[column]
|
|
186
77
|
expression = column.scalar_expr
|
|
187
78
|
return None
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
def _function_key(expression: Expr) -> str:
|
|
191
|
-
"""The key of a function call in `DialectSemantics.result_types`: its
|
|
192
|
-
SQLGlot node, or `Anonymous:<lowercase name>` for one SQLGlot does not know."""
|
|
193
|
-
if isinstance(expression, exp.Anonymous):
|
|
194
|
-
return f"Anonymous:{expression.name.lower()}"
|
|
195
|
-
return type(expression).__name__
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
# The signed integer types, by width (`ir.ValueTyping.integer`).
|
|
199
|
-
_INTEGER_RANK = {
|
|
200
|
-
exp.DataType.Type.TINYINT: 0, exp.DataType.Type.SMALLINT: 1, exp.DataType.Type.MEDIUMINT: 2, exp.DataType.Type.INT: 3,
|
|
201
|
-
exp.DataType.Type.BIGINT: 4, exp.DataType.Type.INT128: 5, exp.DataType.Type.INT256: 6,
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
# The floating-point types: REAL, FLOAT and DOUBLE in any spelling.
|
|
205
|
-
_FLOATS = {exp.DataType.Type.FLOAT, exp.DataType.Type.DOUBLE, exp.DataType.Type.UDOUBLE}
|
|
206
|
-
|
|
207
|
-
# The digits of a number literal past which DuckDB's type for it is a DOUBLE.
|
|
208
|
-
_EXACT_LITERAL_DIGITS = 38
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
# Types whose equal values are identical: no collation (text has a comparison
|
|
212
|
-
# type only when compared by identity), no signed zero or NaN, no two
|
|
213
|
-
# spellings of one interval.
|
|
214
|
-
_IDENTITY_TYPES = {
|
|
215
|
-
*exp.DataType.INTEGER_TYPES, *exp.DataType.TEXT_TYPES,
|
|
216
|
-
exp.DataType.Type.DECIMAL, exp.DataType.Type.BOOLEAN, exp.DataType.Type.DATE, exp.DataType.Type.TIME,
|
|
217
|
-
exp.DataType.Type.TIMESTAMP, exp.DataType.Type.UUID,
|
|
218
|
-
# Timestamps without a zone, as each dialect spells them (DuckDB's TIMESTAMP is TIMESTAMPNTZ).
|
|
219
|
-
exp.DataType.Type.TIMESTAMPNTZ, exp.DataType.Type.TIMESTAMP_S, exp.DataType.Type.TIMESTAMP_MS, exp.DataType.Type.TIMESTAMP_NS,
|
|
220
|
-
exp.DataType.Type.DATETIME, exp.DataType.Type.DATETIME64,
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
_EXACT_NUMERIC_TYPES = {*exp.DataType.INTEGER_TYPES, exp.DataType.Type.DECIMAL}
|