sqlassert 0.3.3__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/__init__.py +1 -2
- sqlassert/check.py +88 -13
- sqlassert/declared_types.py +158 -0
- sqlassert/diagnostics.py +15 -7
- sqlassert/dialects.py +259 -107
- sqlassert/discovery/bigquery.py +4 -2
- sqlassert/discovery/databricks.py +26 -5
- sqlassert/discovery/duckdb.py +49 -7
- sqlassert/discovery/fetcher.py +137 -58
- sqlassert/discovery/information_schema.py +31 -6
- sqlassert/discovery/mysql.py +21 -4
- sqlassert/discovery/postgres.py +28 -20
- sqlassert/discovery/redshift.py +3 -2
- sqlassert/discovery/snowflake.py +5 -2
- sqlassert/discovery/trino.py +2 -2
- sqlassert/functions/__init__.py +10 -0
- sqlassert/functions/load.py +375 -0
- sqlassert/functions/table.py +2721 -0
- sqlassert/hazards.py +249 -88
- sqlassert/ir/__init__.py +15 -4
- sqlassert/ir/convert/__init__.py +13 -0
- sqlassert/ir/convert/aggregation.py +438 -0
- sqlassert/ir/convert/expressions.py +581 -0
- sqlassert/ir/convert/joins.py +264 -0
- sqlassert/ir/convert/markers.py +249 -0
- sqlassert/ir/convert/parser.py +249 -0
- sqlassert/ir/convert/queries.py +259 -0
- sqlassert/ir/convert/schema.py +537 -0
- sqlassert/ir/convert/scopes.py +205 -0
- sqlassert/ir/convert/selects.py +352 -0
- sqlassert/ir/convert/shared.py +428 -0
- sqlassert/ir/convert/sources.py +433 -0
- sqlassert/ir/convert/subqueries.py +364 -0
- sqlassert/ir/convert/value_types.py +78 -0
- sqlassert/ir/convert/windows.py +275 -0
- sqlassert/ir/model.py +417 -146
- sqlassert/ir/names.py +63 -0
- sqlassert/main.py +7 -10
- sqlassert/properties.py +12 -1
- sqlassert/provenance.py +5 -1
- sqlassert/python_engine.py +104 -44
- sqlassert/reporting.py +33 -32
- sqlassert/rewrites.py +111 -61
- sqlassert/rules/STYLEGUIDE.md +3 -2
- sqlassert/rules/causes/__init__.py +41 -0
- sqlassert/rules/causes/coverage.py +113 -0
- sqlassert/rules/causes/domain.py +123 -0
- sqlassert/rules/causes/joins.py +73 -0
- sqlassert/rules/causes/nulls.py +154 -0
- sqlassert/rules/causes/unique.py +564 -0
- sqlassert/rules/domain.py +9 -8
- sqlassert/rules/equality.py +6 -147
- sqlassert/rules/exactly_one.py +2 -1
- sqlassert/rules/foreign_key.py +8 -9
- sqlassert/rules/join_cardinality.py +277 -43
- sqlassert/rules/nullability.py +8 -8
- sqlassert/rules/uniqueness.py +212 -87
- sqlassert/rules/value_typing.py +470 -0
- sqlassert/sql_parse.py +28 -1
- sqlassert/suggest.py +13 -17
- sqlassert-0.4.2.dist-info/METADATA +239 -0
- sqlassert-0.4.2.dist-info/RECORD +72 -0
- sqlassert/ir/convert.py +0 -3509
- sqlassert-0.3.3.dist-info/METADATA +0 -807
- sqlassert-0.3.3.dist-info/RECORD +0 -46
- {sqlassert-0.3.3.dist-info → sqlassert-0.4.2.dist-info}/WHEEL +0 -0
- {sqlassert-0.3.3.dist-info → sqlassert-0.4.2.dist-info}/entry_points.txt +0 -0
- {sqlassert-0.3.3.dist-info → sqlassert-0.4.2.dist-info}/licenses/LICENSE +0 -0
- {sqlassert-0.3.3.dist-info → sqlassert-0.4.2.dist-info}/top_level.txt +0 -0
sqlassert/__init__.py
CHANGED
|
@@ -5,7 +5,7 @@ from sqlassert.properties import (
|
|
|
5
5
|
OneToOneJoin, Property, UniqueJoin, UniqueSet,
|
|
6
6
|
)
|
|
7
7
|
from sqlassert.provenance import AttachmentSite, Origin
|
|
8
|
-
from sqlassert.reporting import AssertionReport, Outcome,
|
|
8
|
+
from sqlassert.reporting import AssertionReport, Outcome, Report
|
|
9
9
|
from sqlassert.suggest import Suggestion, SuggestionReport, suggest
|
|
10
10
|
|
|
11
11
|
__all__ = [
|
|
@@ -26,7 +26,6 @@ __all__ = [
|
|
|
26
26
|
"Origin",
|
|
27
27
|
"Outcome",
|
|
28
28
|
"Property",
|
|
29
|
-
"RelationFacts",
|
|
30
29
|
"Report",
|
|
31
30
|
"Suggestion",
|
|
32
31
|
"SuggestionReport",
|
sqlassert/check.py
CHANGED
|
@@ -32,7 +32,7 @@ from sqlassert import diagnostics as diag
|
|
|
32
32
|
from sqlassert.diagnostics import UNSUPPORTED_CONSTRUCT, Diagnostic
|
|
33
33
|
from sqlassert.main import DEFAULT_DIALECT, analyze
|
|
34
34
|
from sqlassert.provenance import Origin, Span
|
|
35
|
-
from sqlassert.reporting import ASSERTED_KINDS, Outcome, Report
|
|
35
|
+
from sqlassert.reporting import ASSERTED_KINDS, AssertionReport, Outcome, Report, Step
|
|
36
36
|
|
|
37
37
|
# Exit codes, shared by every command that checks SQL.
|
|
38
38
|
EXIT_OK = 0 # every assertion PROVED and no Diagnostic (warnings allowed)
|
|
@@ -66,8 +66,10 @@ class Severity(Enum):
|
|
|
66
66
|
class Finding:
|
|
67
67
|
"""One thing a check reports: an `UNKNOWN` assertion or a Diagnostic.
|
|
68
68
|
|
|
69
|
-
`path` is the file as shown to the user. `
|
|
70
|
-
assertion
|
|
69
|
+
`path` is the file as shown to the user. `causes` are where an `UNKNOWN`
|
|
70
|
+
Unique Set assertion is lost (`Step.causes`), always shown. `evidence` is
|
|
71
|
+
an `UNKNOWN` assertion's explanation: the relations it passes through
|
|
72
|
+
when it has Causes, else its explained Evidence; printed only when asked for.
|
|
71
73
|
"""
|
|
72
74
|
|
|
73
75
|
path: str
|
|
@@ -76,6 +78,7 @@ class Finding:
|
|
|
76
78
|
message: str
|
|
77
79
|
origin: Origin | None = None
|
|
78
80
|
evidence: str | None = None
|
|
81
|
+
causes: tuple[Step, ...] = ()
|
|
79
82
|
|
|
80
83
|
|
|
81
84
|
@dataclass(frozen=True)
|
|
@@ -102,7 +105,8 @@ def findings(path: str, sql: str, report: Report, *, ignore: Iterable[str] = ())
|
|
|
102
105
|
path, Severity.ERROR, unproved(assertion.kind),
|
|
103
106
|
f"{_written(sql, assertion.origin)} is not proved (UNKNOWN)"
|
|
104
107
|
+ (f": {assertion.unsupported} is not supported by sqlassert yet, which may be why" if assertion.unsupported else ""),
|
|
105
|
-
assertion.origin,
|
|
108
|
+
assertion.origin, _evidence(path, assertion),
|
|
109
|
+
assertion.explanation.causes() if assertion.explanation is not None else (),
|
|
106
110
|
)
|
|
107
111
|
for assertion in report.assertions
|
|
108
112
|
if assertion.outcome is Outcome.UNKNOWN
|
|
@@ -120,6 +124,53 @@ def findings(path: str, sql: str, report: Report, *, ignore: Iterable[str] = ())
|
|
|
120
124
|
)
|
|
121
125
|
|
|
122
126
|
|
|
127
|
+
def _evidence(path: str, assertion: AssertionReport) -> str | None:
|
|
128
|
+
"""The explanation of an UNKNOWN assertion: the walk to its Causes when
|
|
129
|
+
there is one, else its Evidence."""
|
|
130
|
+
if assertion.explanation is not None:
|
|
131
|
+
return "\n".join(step_lines(assertion.explanation, _in_file(path)))
|
|
132
|
+
return assertion.evidence.explain() if assertion.evidence is not None else None
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
# Where an Origin is written, as a reader finds it (`path:line:column`), or None.
|
|
136
|
+
type Placing = Callable[[Origin], str | None]
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def _in_file(path: str) -> Placing:
|
|
140
|
+
return lambda origin: f"{path}:{origin.span.line}:{origin.span.column}" if origin.span is not None else None
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def step_lines(step: Step, place: Placing, depth: int = 0) -> list[str]:
|
|
144
|
+
"""A Step and the Steps under it, one per line, details indented below each."""
|
|
145
|
+
indent = " " * depth
|
|
146
|
+
lines = [indent + _step_line(step, place)]
|
|
147
|
+
lines.extend(f"{indent} {detail}" for detail in step.details)
|
|
148
|
+
for inner in step.steps:
|
|
149
|
+
lines.extend(step_lines(inner, place, depth + 1))
|
|
150
|
+
return lines
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def causes_lines(causes: Iterable[Step], place: Placing) -> list[str]:
|
|
154
|
+
"""Each Cause and its details, one per line."""
|
|
155
|
+
lines = []
|
|
156
|
+
for cause in causes:
|
|
157
|
+
lines.append(_step_line(cause, place))
|
|
158
|
+
lines.extend(f" {detail}" for detail in cause.details)
|
|
159
|
+
return lines
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def _step_line(step: Step, place: Placing) -> str:
|
|
163
|
+
"""`where: text`, and `[tag] code` for a Cause."""
|
|
164
|
+
where = place(step.origin) if step.origin is not None else None
|
|
165
|
+
tagged = f" [{step.tag}] {step.code}" if step.tag is not None else ""
|
|
166
|
+
return f"{where}: {step.text}{tagged}" if where else f"{step.text}{tagged}"
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def cause_lines(finding: Finding) -> list[str]:
|
|
170
|
+
"""Each Cause of a Finding and its details, one per line."""
|
|
171
|
+
return causes_lines(finding.causes, _in_file(finding.path))
|
|
172
|
+
|
|
173
|
+
|
|
123
174
|
def check(path: str, sql: str, *, dialect: str = DEFAULT_DIALECT, ignore: Iterable[str] = ()) -> Checked:
|
|
124
175
|
"""Analyze one SQL Program from its SQL alone."""
|
|
125
176
|
report = analyze(sql, dialect=dialect)
|
|
@@ -287,6 +338,7 @@ def render_text(checked: Sequence[Checked], *, explain: bool = False) -> str:
|
|
|
287
338
|
for result in checked:
|
|
288
339
|
for finding in result.findings:
|
|
289
340
|
lines.append(f"{_place(finding)}: {finding.severity.value} {finding.code}: {finding.message}")
|
|
341
|
+
lines.extend(indented(line) for line in cause_lines(finding))
|
|
290
342
|
if explain and finding.evidence is not None:
|
|
291
343
|
lines.append(indented(finding.evidence))
|
|
292
344
|
lines.append(_summary_line(checked))
|
|
@@ -300,6 +352,11 @@ def render_json(checked: Sequence[Checked], *, explain: bool = False) -> str:
|
|
|
300
352
|
"path": item.path, "severity": item.severity.value, "code": item.code, "message": item.message,
|
|
301
353
|
"location": _location(item.origin),
|
|
302
354
|
}
|
|
355
|
+
if item.causes:
|
|
356
|
+
shown["causes"] = [
|
|
357
|
+
{"tag": cause.tag, "code": cause.code, "message": cause.text, "details": list(cause.details), "location": _location(cause.origin)}
|
|
358
|
+
for cause in item.causes
|
|
359
|
+
]
|
|
303
360
|
if explain and item.evidence is not None:
|
|
304
361
|
shown["evidence"] = item.evidence
|
|
305
362
|
return shown
|
|
@@ -328,12 +385,8 @@ def render_sarif(checked: Sequence[Checked], *, explain: bool = False) -> str:
|
|
|
328
385
|
except PackageNotFoundError:
|
|
329
386
|
pass
|
|
330
387
|
|
|
331
|
-
def
|
|
332
|
-
|
|
333
|
-
if explain and finding.evidence is not None:
|
|
334
|
-
message += "\n\n" + finding.evidence
|
|
335
|
-
location: dict = {"artifactLocation": {"uri": _uri(finding.path)}}
|
|
336
|
-
origin = finding.origin
|
|
388
|
+
def physical(path: str, origin: Origin | None) -> dict:
|
|
389
|
+
location: dict = {"artifactLocation": {"uri": _uri(path)}}
|
|
337
390
|
if origin is not None and origin.line is not None:
|
|
338
391
|
region: dict = {"startLine": origin.line}
|
|
339
392
|
if origin.span is not None:
|
|
@@ -341,13 +394,27 @@ def render_sarif(checked: Sequence[Checked], *, explain: bool = False) -> str:
|
|
|
341
394
|
startColumn=origin.span.column, endLine=origin.span.end_line, endColumn=origin.span.end_column
|
|
342
395
|
)
|
|
343
396
|
location["region"] = region
|
|
344
|
-
return
|
|
397
|
+
return location
|
|
398
|
+
|
|
399
|
+
def result(finding: Finding) -> dict:
|
|
400
|
+
message = "\n".join([finding.message, *cause_lines(finding)])
|
|
401
|
+
if explain and finding.evidence is not None:
|
|
402
|
+
message += "\n\n" + finding.evidence
|
|
403
|
+
shown: dict = {
|
|
345
404
|
"ruleId": finding.code,
|
|
346
405
|
"ruleIndex": codes.index(finding.code),
|
|
347
406
|
"level": finding.severity.value,
|
|
348
407
|
"message": {"text": message},
|
|
349
|
-
"locations": [{"physicalLocation":
|
|
408
|
+
"locations": [{"physicalLocation": physical(finding.path, finding.origin)}],
|
|
350
409
|
}
|
|
410
|
+
# Where each Cause is written, so a review sees it there too.
|
|
411
|
+
related = [
|
|
412
|
+
{"id": index, "physicalLocation": physical(finding.path, cause.origin), "message": {"text": cause.text}}
|
|
413
|
+
for index, cause in enumerate(finding.causes, 1) if cause.origin is not None and cause.origin.line is not None
|
|
414
|
+
]
|
|
415
|
+
if related:
|
|
416
|
+
shown["relatedLocations"] = related
|
|
417
|
+
return shown
|
|
351
418
|
|
|
352
419
|
log = {
|
|
353
420
|
"$schema": "https://json.schemastore.org/sarif-2.1.0.json",
|
|
@@ -373,11 +440,19 @@ def render_github(checked: Sequence[Checked], *, explain: bool = False) -> str:
|
|
|
373
440
|
if span.end_line == span.line:
|
|
374
441
|
properties.update(col=str(span.column), endColumn=str(span.end_column))
|
|
375
442
|
properties["title"] = f"sqlassert {finding.code}"
|
|
376
|
-
message = finding.message
|
|
443
|
+
message = "\n".join([finding.message, *cause_lines(finding)])
|
|
377
444
|
if explain and finding.evidence is not None:
|
|
378
445
|
message += "\n" + finding.evidence
|
|
379
446
|
shown = ",".join(f"{key}={_escape_property(value)}" for key, value in properties.items())
|
|
380
447
|
lines.append(f"::{finding.severity.value} {shown}::{_escape_data(message)}")
|
|
448
|
+
# A notice where each Cause is written, so the fix is annotated where it is made.
|
|
449
|
+
for cause in finding.causes:
|
|
450
|
+
if cause.origin is None or cause.origin.span is None:
|
|
451
|
+
continue
|
|
452
|
+
span = cause.origin.span
|
|
453
|
+
noticed = {"file": finding.path, "line": str(span.line), "col": str(span.column), "title": f"sqlassert cause of {finding.code}"}
|
|
454
|
+
text = "\n".join([cause.text, *cause.details])
|
|
455
|
+
lines.append(f"::notice {','.join(f'{key}={_escape_property(value)}' for key, value in noticed.items())}::{_escape_data(text)}")
|
|
381
456
|
return "".join(line + "\n" for line in lines)
|
|
382
457
|
|
|
383
458
|
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
"""How a SQL type compares: the `ir.ValueTyping` of a column declared with it,
|
|
2
|
+
of a CAST's target, and of a type a dialect's function table names
|
|
3
|
+
(`sqlassert/functions/`). One place, so a declared column and a function's
|
|
4
|
+
result type of the same SQL type compare alike."""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
from functools import cache
|
|
9
|
+
|
|
10
|
+
from sqlglot import exp
|
|
11
|
+
|
|
12
|
+
from sqlassert import ir
|
|
13
|
+
from sqlassert.dialects import DialectSemantics, semantics
|
|
14
|
+
|
|
15
|
+
TEXT_TYPES = {exp.DataType.Type.VARCHAR, exp.DataType.Type.TEXT}
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def declared_typing(kind: exp.DataType, dialect: str) -> ir.ValueTyping | None:
|
|
19
|
+
"""How a column declared with `kind` compares in `dialect`; None when it has no comparison type."""
|
|
20
|
+
return column_typing(exp.ColumnDef(kind=kind), dialect)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def column_typing(definition: exp.ColumnDef, dialect: str) -> ir.ValueTyping | None:
|
|
24
|
+
meaning = semantics(dialect)
|
|
25
|
+
comparison_type = _comparison_type(definition, dialect, meaning)
|
|
26
|
+
if comparison_type is None:
|
|
27
|
+
return None
|
|
28
|
+
kind = definition.args["kind"]
|
|
29
|
+
family = _FAMILIES.get(kind.this)
|
|
30
|
+
parameters = _parameters(kind)
|
|
31
|
+
length = precision = scale = None
|
|
32
|
+
match family:
|
|
33
|
+
case "text" if kind.expressions:
|
|
34
|
+
# A length that is not a number (`VARCHAR(MAX)`) is 0: some length, not known to hold the value.
|
|
35
|
+
length = parameters[0] if parameters else 0
|
|
36
|
+
case "decimal" if parameters:
|
|
37
|
+
precision, scale = parameters[0], parameters[1] if len(parameters) > 1 else 0
|
|
38
|
+
case "time" | "timestamp" | "timestamptz" if parameters:
|
|
39
|
+
precision = parameters[0]
|
|
40
|
+
case _:
|
|
41
|
+
pass
|
|
42
|
+
return ir.ValueTyping(
|
|
43
|
+
comparison_type, _is_binary_text(definition, meaning), _identity_comparison(definition, dialect, meaning), _exact_numeric(definition, meaning),
|
|
44
|
+
floating=kind.this in FLOATS, integer=_INTEGER_RANK.get(kind.this),
|
|
45
|
+
text=kind.this in exp.DataType.TEXT_TYPES, temporal=kind.this in exp.DataType.TEMPORAL_TYPES,
|
|
46
|
+
family=family, length=length, precision=precision, scale=scale, casts=cast_table(meaning),
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
@cache
|
|
51
|
+
def cast_table(meaning: DialectSemantics) -> ir.CastTable:
|
|
52
|
+
"""The dialect's cast table (`DialectSemantics.casts`), one per dialect."""
|
|
53
|
+
return ir.CastTable(meaning.casts)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def _comparison_type(definition: exp.ColumnDef, dialect: str, meaning: DialectSemantics) -> str | None:
|
|
57
|
+
kind = definition.args.get("kind")
|
|
58
|
+
if not isinstance(kind, exp.DataType):
|
|
59
|
+
return None
|
|
60
|
+
if _unscaled_decimal(kind, meaning):
|
|
61
|
+
return None
|
|
62
|
+
return _type_name(kind, dialect)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def _type_name(kind: exp.DataType, dialect: str) -> str:
|
|
66
|
+
"""The name two values of one comparison type share: the type's SQL,
|
|
67
|
+
without a date or time type's parameters. TIMESTAMP(3) holds values
|
|
68
|
+
TIMESTAMP(6) holds unchanged, so they compare as one type (ADR 0009)."""
|
|
69
|
+
if kind.this in exp.DataType.TEMPORAL_TYPES and kind.expressions:
|
|
70
|
+
kind = kind.copy()
|
|
71
|
+
kind.set("expressions", [])
|
|
72
|
+
return kind.sql(dialect=dialect)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def _identity_comparison(definition: exp.ColumnDef, dialect: str, meaning: DialectSemantics) -> bool:
|
|
76
|
+
"""Declared with a type whose equal values are identical (text only
|
|
77
|
+
when compared by identity)."""
|
|
78
|
+
kind = definition.args.get("kind")
|
|
79
|
+
if not (_comparison_type(definition, dialect, meaning) is not None and isinstance(kind, exp.DataType)):
|
|
80
|
+
return False
|
|
81
|
+
if kind.this == exp.DataType.Type.TIMESTAMPTZ:
|
|
82
|
+
return meaning.zoned_timestamp_is_instant
|
|
83
|
+
if kind.this in exp.DataType.TEXT_TYPES:
|
|
84
|
+
return _is_binary_text(definition, meaning)
|
|
85
|
+
return kind.this in _IDENTITY_TYPES
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def _exact_numeric(definition: exp.ColumnDef, meaning: DialectSemantics) -> bool:
|
|
89
|
+
kind = definition.args.get("kind")
|
|
90
|
+
return isinstance(kind, exp.DataType) and kind.this in _EXACT_NUMERIC_TYPES and not _unscaled_decimal(kind, meaning)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def _unscaled_decimal(kind: exp.DataType, meaning: DialectSemantics) -> bool:
|
|
94
|
+
"""A Postgres NUMERIC with no scale: 1.0 and 1.00 are equal values that differ."""
|
|
95
|
+
return meaning.unscaled_decimal and kind.this == exp.DataType.Type.DECIMAL and not kind.expressions
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def _is_binary_text(definition: exp.ColumnDef, meaning: DialectSemantics) -> bool:
|
|
99
|
+
"""Declared VARCHAR or TEXT in a dialect whose default collation is
|
|
100
|
+
binary: compared by identity. CHAR is not: it ignores trailing spaces
|
|
101
|
+
when compared."""
|
|
102
|
+
kind = definition.args.get("kind")
|
|
103
|
+
return isinstance(kind, exp.DataType) and kind.this in TEXT_TYPES and meaning.text_is_binary
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def _parameters(kind: exp.DataType) -> tuple[int, ...]:
|
|
107
|
+
"""A type's numeric parameters (`DECIMAL(10, 2)`: 10, 2), or none when one is not a number."""
|
|
108
|
+
try:
|
|
109
|
+
return tuple(int(parameter.sql()) for parameter in kind.expressions)
|
|
110
|
+
except ValueError:
|
|
111
|
+
return ()
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
# The signed integer types, by width (`ir.ValueTyping.integer`).
|
|
115
|
+
_INTEGER_RANK = {
|
|
116
|
+
exp.DataType.Type.TINYINT: 0, exp.DataType.Type.SMALLINT: 1, exp.DataType.Type.MEDIUMINT: 2, exp.DataType.Type.INT: 3,
|
|
117
|
+
exp.DataType.Type.BIGINT: 4, exp.DataType.Type.INT128: 5, exp.DataType.Type.INT256: 6,
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
# The floating-point types: REAL, FLOAT and DOUBLE in any spelling.
|
|
121
|
+
FLOATS = {exp.DataType.Type.FLOAT, exp.DataType.Type.DOUBLE, exp.DataType.Type.UDOUBLE}
|
|
122
|
+
|
|
123
|
+
# The digits of a number literal past which DuckDB's type for it is a DOUBLE.
|
|
124
|
+
EXACT_LITERAL_DIGITS = 38
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
# Types whose equal values are identical: no collation (text has a comparison
|
|
128
|
+
# type only when compared by identity), no signed zero or NaN, no two
|
|
129
|
+
# spellings of one interval.
|
|
130
|
+
_IDENTITY_TYPES = {
|
|
131
|
+
*exp.DataType.INTEGER_TYPES, *exp.DataType.TEXT_TYPES,
|
|
132
|
+
exp.DataType.Type.DECIMAL, exp.DataType.Type.BOOLEAN, exp.DataType.Type.DATE, exp.DataType.Type.TIME,
|
|
133
|
+
exp.DataType.Type.TIMESTAMP, exp.DataType.Type.UUID,
|
|
134
|
+
# Timestamps without a zone, as each dialect spells them (DuckDB's TIMESTAMP is TIMESTAMPNTZ).
|
|
135
|
+
exp.DataType.Type.TIMESTAMPNTZ, exp.DataType.Type.TIMESTAMP_S, exp.DataType.Type.TIMESTAMP_MS, exp.DataType.Type.TIMESTAMP_NS,
|
|
136
|
+
exp.DataType.Type.DATETIME, exp.DataType.Type.DATETIME64,
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
_EXACT_NUMERIC_TYPES = {*exp.DataType.INTEGER_TYPES, exp.DataType.Type.DECIMAL}
|
|
140
|
+
|
|
141
|
+
# The family of each type a cast table classifies (`ir.TypeFamily`): signed
|
|
142
|
+
# integers, decimals, floats, VARCHAR and TEXT (not CHAR, which pads), and the
|
|
143
|
+
# date, time and timestamp types, with and without a zone.
|
|
144
|
+
_T = exp.DataType.Type
|
|
145
|
+
_FAMILIES: dict[exp.DataType.Type, ir.TypeFamily] = {
|
|
146
|
+
**dict.fromkeys(_INTEGER_RANK, "integer"), _T.DECIMAL: "decimal", **dict.fromkeys(FLOATS, "float"),
|
|
147
|
+
_T.VARCHAR: "text", _T.TEXT: "text", _T.BOOLEAN: "boolean", _T.UUID: "uuid",
|
|
148
|
+
_T.DATE: "date", _T.DATE32: "date", _T.TIME: "time",
|
|
149
|
+
**dict.fromkeys((
|
|
150
|
+
_T.TIMESTAMP, _T.TIMESTAMPNTZ, _T.TIMESTAMP_S, _T.TIMESTAMP_MS, _T.TIMESTAMP_NS,
|
|
151
|
+
_T.DATETIME, _T.DATETIME2, _T.DATETIME64, _T.SMALLDATETIME,
|
|
152
|
+
), "timestamp"),
|
|
153
|
+
_T.TIMESTAMPTZ: "timestamptz", _T.TIMESTAMPLTZ: "timestamptz",
|
|
154
|
+
_T.INTERVAL: "interval",
|
|
155
|
+
**dict.fromkeys((_T.BINARY, _T.VARBINARY, _T.BLOB, _T.TINYBLOB, _T.MEDIUMBLOB, _T.LONGBLOB), "bytes"),
|
|
156
|
+
**dict.fromkeys((_T.JSON, _T.JSONB), "json"), **dict.fromkeys((_T.ARRAY, _T.LIST), "array"), _T.STRUCT: "struct",
|
|
157
|
+
**dict.fromkeys((_T.GEOGRAPHY, _T.GEOMETRY), "geography"),
|
|
158
|
+
}
|
sqlassert/diagnostics.py
CHANGED
|
@@ -5,12 +5,15 @@ Disproof and never an inference: it says that analysis stopped short, and why.
|
|
|
5
5
|
|
|
6
6
|
A warning is a Diagnostic too, reported apart (`Report.warnings`): the program
|
|
7
7
|
is supported, but a fact a connection would supply is missing, so analysis
|
|
8
|
-
treats it as unknown (ADR 0005). So are a
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
treats it as unknown (ADR 0005). So are a catalog foreign key onto a
|
|
9
|
+
table the SQL declares that cannot be bound to that declaration, and, about
|
|
10
|
+
the SQL itself (`SQL_WARNINGS`), a constraint the dialect does not enforce,
|
|
11
|
+
a CHECK constraint that states no Enumerated Domain, a comparison that depends on something the
|
|
11
12
|
values do not show, like a zoned timestamp's zone (`dialects.py`), a
|
|
12
|
-
comparison of composites whose elements may be NULL,
|
|
13
|
-
that holds only because a column it names is always NULL
|
|
13
|
+
comparison of composites whose elements may be NULL, a proved Property
|
|
14
|
+
that holds only because a column it names is always NULL, a name that may
|
|
15
|
+
be one the engine gave a repeated column of a derived table, and a function
|
|
16
|
+
call whose result type sqlassert does not know for the dialect.
|
|
14
17
|
Warnings never make a Report fail.
|
|
15
18
|
"""
|
|
16
19
|
|
|
@@ -33,6 +36,7 @@ def quoted_all(names: Iterable[str]) -> str:
|
|
|
33
36
|
CATALOG_METADATA_UNAVAILABLE = "catalog-metadata-unavailable"
|
|
34
37
|
ALWAYS_NULL_COLUMN = "always-null-column"
|
|
35
38
|
AMBIGUOUS_COLUMN = "ambiguous-column"
|
|
39
|
+
AMBIGUOUS_DUPLICATE_COLUMN = "ambiguous-duplicate-column"
|
|
36
40
|
CARDINALITY_ON_FILTERING_JOIN = "cardinality-on-filtering-join"
|
|
37
41
|
CATALOG_RELATION_NOT_FOUND = "catalog-relation-not-found"
|
|
38
42
|
COMPOSITE_COMPARISON = "composite-comparison"
|
|
@@ -46,9 +50,10 @@ RECURSIVE_VIEW_DEFINITION = "recursive-view-definition"
|
|
|
46
50
|
SQL_PARSE_FAILED = "sql-parse-failed"
|
|
47
51
|
UNANALYZED_ASSERTION = "unanalyzed-assertion"
|
|
48
52
|
UNATTACHED_MARKER = "unattached-marker"
|
|
53
|
+
UNBOUND_FOREIGN_KEY = "unbound-foreign-key"
|
|
49
54
|
UNDECLARED_TABLE = "undeclared-table"
|
|
50
|
-
UNENFORCED_CONSTRAINT = "unenforced-constraint"
|
|
51
55
|
UNKNOWN_ASSERTED_COLUMN = "unknown-asserted-column"
|
|
56
|
+
UNKNOWN_FUNCTION = "unknown-function"
|
|
52
57
|
UNRESOLVED_ASSERTED_COLUMN = "unresolved-asserted-column"
|
|
53
58
|
UNKNOWN_REFERENCED_COLUMN = "unknown-referenced-column"
|
|
54
59
|
UNRECOGNIZED_MARKER = "unrecognized-marker"
|
|
@@ -67,4 +72,7 @@ class Diagnostic:
|
|
|
67
72
|
|
|
68
73
|
|
|
69
74
|
# Warnings about the SQL itself, which no connection answers: `analyze_database` keeps them.
|
|
70
|
-
SQL_WARNINGS = frozenset({
|
|
75
|
+
SQL_WARNINGS = frozenset({
|
|
76
|
+
ALWAYS_NULL_COLUMN, AMBIGUOUS_DUPLICATE_COLUMN, COMPOSITE_COMPARISON, IGNORED_CHECK_CONSTRAINT, UNKNOWN_FUNCTION,
|
|
77
|
+
ZONED_TIMESTAMP_CAST,
|
|
78
|
+
})
|