Skip to content

Commit 08881a2

Browse files
authored
Align lint and quality rules with current ODCS (#1859)
* Apply every operator of a quality rule that combines several * Update the bundled ODCS 3.1.0 and 3.2.0 schemas from upstream main, so lint accepts rules that combine operators, and drop the blob extension * Name the compared value in every comparison of a threshold, and the Excel warning about rules with several operators * Reference the PR in the changelog entries
1 parent f8cf4e3 commit 08881a2

24 files changed

Lines changed: 535 additions & 891 deletions

‎CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111
- `datacontract test` and `datacontract import sqlserver` sign in to SQL Server with an Entra ID access token read from a file (`DATACONTRACT_SQLSERVER_AUTHENTICATION=token`, `DATACONTRACT_SQLSERVER_TOKEN_FILE_PATH`) (#1830 @SreeramaYeshwanthGowd)
1212

13+
### Changed
14+
- `datacontract lint` and `test` reject the schema `logicalType: blob`, which ODCS does not allow (#1859)
15+
1316
### Fixed
1417
- `datacontract test` checks the constraints and quality rules of nested properties (struct fields and array items) on Dremio servers
1518
- `datacontract test` finds tables in Postgres schemas with upper case letters, such as `"Reporting"`
1619
- `datacontract test` checks the other columns of a Postgres table with a column of a type ibis cannot read, such as `regclass`
1720
- The CLI no longer fails with `UnicodeEncodeError` when its output is piped on Windows
1821
- `datacontract import postgres` and `import sql` no longer map bit strings of more than one bit (`bit(8)`, `bit varying`) to `boolean`
22+
- `datacontract lint` accepts the ids and references the current ODCS v3.1.0 schema allows, such as ids with `:` and shorthand references with `-` (#1859)
23+
- `datacontract lint` accepts quality rules that combine several operators, such as `mustBeGreaterThan` with `mustBeLessThan`, and `test`, `breaking`, `dbt sync` and `export sodacl` apply every one of them; `export excel` warns about the ones it cannot hold (#1859)
1924

2025
## [1.2.11] - 2026-11-17
2126

‎datacontract/breaking/rules.py‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
from datacontract.model.changelog import ChangelogEntry, ChangelogType
1919
from datacontract.model.run import ResultEnum
2020

21-
# In the order to_threshold reads them.
2221
_THRESHOLDS = (
2322
"mustBe",
2423
"mustNotBe",
@@ -408,7 +407,7 @@ def evaluate(self, entry: ChangelogEntry, before: dict, after: dict) -> RuleEval
408407
direction = Direction.UNDETERMINED
409408
else:
410409
probes = _probes(
411-
[bound for t in (old, new) for bound in (t.value, t.value2) if bound is not None],
410+
[bound for t in (old, new) for c in t.comparisons for bound in (c.value, c.value2)],
412411
# A library metric counts rows, unless it is a percentage of them.
413412
integers=not percent.pop() and new_rule.get("type") not in ("sql", "custom"),
414413
)
@@ -529,9 +528,10 @@ def _carrier(old: dict, new: dict, keys: tuple[str, ...]) -> str | None:
529528
def _count_direction(rule: dict, more: bool) -> Direction:
530529
"""How counting more (or fewer) bad rows moves a quality rule, judged by the threshold it is checked against."""
531530
threshold = to_threshold(_threshold_quality(rule))
532-
probes = (
533-
None if threshold is None else _probes([0, threshold.value, threshold.value2], integers=not _is_percent(rule))
534-
)
531+
if threshold is None:
532+
return Direction.UNDETERMINED
533+
bounds = [bound for c in threshold.comparisons for bound in (c.value, c.value2)]
534+
probes = _probes([0, *bounds], integers=not _is_percent(rule))
535535
if probes is None:
536536
return Direction.UNDETERMINED
537537
accepted = [threshold.passes(p) for p in probes if p >= 0]
@@ -547,9 +547,10 @@ def _count_direction(rule: dict, more: bool) -> Direction:
547547

548548
def _probes(bounds: list, integers: bool) -> list[float] | None:
549549
"""Values that tell apart predicates that change their answer only at ``bounds``; None if not numbers."""
550-
bounds = sorted({bound for bound in bounds if bound is not None})
550+
bounds = {bound for bound in bounds if bound is not None}
551551
if not all(isinstance(bound, (int, float)) and not isinstance(bound, bool) for bound in bounds):
552552
return None
553+
bounds = sorted(bounds)
553554
if integers:
554555
return sorted({n for bound in bounds for n in range(math.floor(bound) - 1, math.ceil(bound) + 2)})
555556
return sorted(bounds + [(a + b) / 2 for a, b in zip(bounds, bounds[1:])] + [bounds[0] - 1, bounds[-1] + 1])

‎datacontract/config/variables.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def allowed_environment(patterns: Iterable[str]) -> dict[str, str]:
106106
("DataQuality", "dimension"): frozenset(
107107
{"accuracy", "completeness", "conformity", "consistency", "coverage", "timeliness", "uniqueness"}
108108
),
109-
("SchemaObject", "logicalType"): frozenset({"object", "blob"}),
109+
("SchemaObject", "logicalType"): frozenset({"object"}),
110110
("SchemaProperty", "logicalType"): frozenset(
111111
{
112112
"string",

‎datacontract/engines/checks/check_spec.py‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ class Op(str, Enum):
8080

8181

8282
@dataclass
83-
class Threshold:
84-
"""A structured comparison applied to a computed metric value."""
83+
class Comparison:
84+
"""A single comparison applied to a computed metric value."""
8585

8686
op: Op
8787
value: Any = None
@@ -110,12 +110,27 @@ def passes(self, actual: Any) -> bool:
110110
return not bool(v <= a <= self.value2)
111111
return False
112112

113-
def describe(self) -> str:
113+
def describe(self, subject: str | None = None, unit: str = "") -> str:
114+
prefix = f"{subject} " if subject else ""
114115
if self.op == Op.BETWEEN:
115-
return f"between {self.value} and {self.value2}"
116+
return f"{prefix}between {self.value}{unit} and {self.value2}{unit}"
116117
if self.op == Op.NOT_BETWEEN:
117-
return f"not between {self.value} and {self.value2}"
118-
return f"{self.op.value} {self.value}"
118+
return f"{prefix}not between {self.value}{unit} and {self.value2}{unit}"
119+
return f"{prefix}{self.op.value} {self.value}{unit}"
120+
121+
122+
@dataclass
123+
class Threshold:
124+
"""The comparisons a computed metric value must all pass."""
125+
126+
comparisons: List[Comparison]
127+
128+
def passes(self, actual: Any) -> bool:
129+
return all(comparison.passes(actual) for comparison in self.comparisons)
130+
131+
def describe(self, subject: str | None = None, unit: str = "") -> str:
132+
"""Each comparison names the subject, if given, and carries the unit, e.g. `size > 0% and size < 5%`."""
133+
return " and ".join(comparison.describe(subject, unit) for comparison in self.comparisons)
119134

120135

121136
@dataclass

‎datacontract/engines/checks/create_checks.py‎

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from datacontract.engines.checks.check_spec import (
2828
METADATA_METRICS,
2929
CheckSpec,
30+
Comparison,
3031
Hop,
3132
MetricType,
3233
Op,
@@ -172,29 +173,29 @@ def is_percent_unit(quality: DataQuality) -> bool:
172173

173174

174175
def to_threshold(quality: DataQuality) -> Optional[Threshold]:
175-
if quality.mustBe is not None:
176-
return Threshold(Op.EQ, quality.mustBe)
177-
if quality.mustNotBe is not None:
178-
return Threshold(Op.NE, quality.mustNotBe)
179-
if quality.mustBeGreaterThan is not None:
180-
return Threshold(Op.GT, quality.mustBeGreaterThan)
181-
if quality.mustBeGreaterOrEqualTo is not None:
182-
return Threshold(Op.GE, quality.mustBeGreaterOrEqualTo)
183-
if quality.mustBeLessThan is not None:
184-
return Threshold(Op.LT, quality.mustBeLessThan)
185-
if quality.mustBeLessOrEqualTo is not None:
186-
return Threshold(Op.LE, quality.mustBeLessOrEqualTo)
176+
comparisons = [
177+
Comparison(op, value)
178+
for op, value in (
179+
(Op.EQ, quality.mustBe),
180+
(Op.NE, quality.mustNotBe),
181+
(Op.GT, quality.mustBeGreaterThan),
182+
(Op.GE, quality.mustBeGreaterOrEqualTo),
183+
(Op.LT, quality.mustBeLessThan),
184+
(Op.LE, quality.mustBeLessOrEqualTo),
185+
)
186+
if value is not None
187+
]
187188
if quality.mustBeBetween is not None:
188189
if len(quality.mustBeBetween) != 2:
189190
logger.warning(f"Quality check has invalid mustBeBetween (need 2 values): {quality.mustBeBetween}")
190191
return None
191-
return Threshold(Op.BETWEEN, quality.mustBeBetween[0], quality.mustBeBetween[1])
192+
comparisons.append(Comparison(Op.BETWEEN, quality.mustBeBetween[0], quality.mustBeBetween[1]))
192193
if quality.mustNotBeBetween is not None:
193194
if len(quality.mustNotBeBetween) != 2:
194195
logger.warning(f"Quality check has invalid mustNotBeBetween (need 2 values): {quality.mustNotBeBetween}")
195196
return None
196-
return Threshold(Op.NOT_BETWEEN, quality.mustNotBeBetween[0], quality.mustNotBeBetween[1])
197-
return None
197+
comparisons.append(Comparison(Op.NOT_BETWEEN, quality.mustNotBeBetween[0], quality.mustNotBeBetween[1]))
198+
return Threshold(comparisons) if comparisons else None
198199

199200

200201
def prepare_query(
@@ -327,7 +328,7 @@ def _to_schema_checks(
327328
model=model,
328329
field=None,
329330
metric=MetricType.ROW_COUNT,
330-
threshold=Threshold(Op.GT, 0),
331+
threshold=Threshold([Comparison(Op.GT, 0)]),
331332
)
332333
)
333334

@@ -363,7 +364,7 @@ def _to_schema_checks(
363364
field=field,
364365
metric=MetricType.CUSTOM_SQL,
365366
query=f"SELECT count(*) FROM {type_errors} WHERE field = {sql_literal(field, 'duckdb')}",
366-
threshold=Threshold(Op.EQ, 0),
367+
threshold=Threshold([Comparison(Op.EQ, 0)]),
367368
)
368369
)
369370

@@ -423,7 +424,7 @@ def _to_schema_checks(
423424
model,
424425
field,
425426
"field_required",
426-
Threshold(Op.EQ, 0),
427+
Threshold([Comparison(Op.EQ, 0)]),
427428
name=f"Check that field {field} has no missing values",
428429
category="schema",
429430
)
@@ -434,7 +435,7 @@ def _to_schema_checks(
434435
model,
435436
field,
436437
"field_unique",
437-
Threshold(Op.EQ, 0),
438+
Threshold([Comparison(Op.EQ, 0)]),
438439
name=f"Check that unique field {field} has no duplicate values",
439440
category="schema",
440441
)
@@ -448,7 +449,7 @@ def _to_schema_checks(
448449
model,
449450
field,
450451
"field_primary_key_required",
451-
Threshold(Op.EQ, 0),
452+
Threshold([Comparison(Op.EQ, 0)]),
452453
name=f"Check that primary key field {field} has no missing values",
453454
category="schema",
454455
)
@@ -459,7 +460,7 @@ def _to_schema_checks(
459460
model,
460461
field,
461462
"field_primary_key_unique",
462-
Threshold(Op.EQ, 0),
463+
Threshold([Comparison(Op.EQ, 0)]),
463464
name=f"Check that primary key field {field} has no duplicate values",
464465
category="schema",
465466
)
@@ -673,7 +674,7 @@ def _to_schema_checks(
673674
model=model,
674675
field=None,
675676
metric=MetricType.DUPLICATE_COUNT,
676-
threshold=Threshold(Op.EQ, 0),
677+
threshold=Threshold([Comparison(Op.EQ, 0)]),
677678
columns=primary_key_fields,
678679
)
679680
)
@@ -764,7 +765,7 @@ def physical(props, name):
764765
model=model,
765766
field=columns[0] if len(columns) == 1 else None,
766767
metric=MetricType.MISSING_REFERENCE_COUNT,
767-
threshold=Threshold(Op.EQ, 0),
768+
threshold=Threshold([Comparison(Op.EQ, 0)]),
768769
columns=columns,
769770
referenced_model=referenced_model,
770771
referenced_columns=referenced,
@@ -842,7 +843,7 @@ def _invalid_count_check(
842843
model=model,
843844
field=field,
844845
metric=MetricType.INVALID_COUNT,
845-
threshold=threshold or Threshold(Op.EQ, 0),
846+
threshold=threshold or Threshold([Comparison(Op.EQ, 0)]),
846847
threshold_is_percent=threshold_is_percent,
847848
severity=severity,
848849
dimension=dimension,
@@ -855,7 +856,7 @@ def _row_count_check(model, threshold: Threshold, severity=None, dimension=None)
855856
key=f"{model}__row_count",
856857
category="quality",
857858
type="row_count",
858-
name=f"Check that model {model} has row_count {threshold.describe()}",
859+
name=f"Check that model {model} has {threshold.describe('row_count')}",
859860
model=model,
860861
field=None,
861862
metric=MetricType.ROW_COUNT,
@@ -1133,6 +1134,7 @@ def _quality_metric_check(model, field, quality: DataQuality, threshold: Thresho
11331134
severity = quality.severity
11341135
dimension = quality.dimension
11351136
is_percent = is_percent_unit(quality)
1137+
unit = "%" if is_percent else ""
11361138

11371139
if metric == "rowCount":
11381140
return [_row_count_check(model, threshold, severity=severity, dimension=dimension)]
@@ -1145,7 +1147,7 @@ def _quality_metric_check(model, field, quality: DataQuality, threshold: Thresho
11451147
key=f"{model}__model_duplicate_values",
11461148
category="quality",
11471149
type="model_duplicate_values",
1148-
name=f"Check that model {model} has duplicate_count {threshold.describe()} for columns {col_joined}",
1150+
name=f"Check that model {model} has {threshold.describe('duplicate_count')} for columns {col_joined}",
11491151
model=model,
11501152
field=None,
11511153
metric=MetricType.DUPLICATE_COUNT,
@@ -1161,7 +1163,7 @@ def _quality_metric_check(model, field, quality: DataQuality, threshold: Thresho
11611163
field,
11621164
"field_duplicate_values",
11631165
threshold,
1164-
name=f"Check that field {field} has duplicate_count {threshold.describe()}",
1166+
name=f"Check that field {field} has {threshold.describe('duplicate_count')}",
11651167
severity=severity,
11661168
dimension=dimension,
11671169
)
@@ -1173,7 +1175,7 @@ def _quality_metric_check(model, field, quality: DataQuality, threshold: Thresho
11731175
field,
11741176
"field_null_values",
11751177
threshold,
1176-
name=f"Check that field {field} has missing_count {threshold.describe()}",
1178+
name=f"Check that field {field} has {threshold.describe('missing_count', unit)}",
11771179
threshold_is_percent=is_percent,
11781180
severity=severity,
11791181
dimension=dimension,
@@ -1190,7 +1192,7 @@ def _quality_metric_check(model, field, quality: DataQuality, threshold: Thresho
11901192
model,
11911193
field,
11921194
"field_invalid_values",
1193-
name=f"Check that field {field} has invalid_count {threshold.describe()}",
1195+
name=f"Check that field {field} has {threshold.describe('invalid_count', unit)}",
11941196
threshold=threshold,
11951197
category="quality",
11961198
valid_values=valid_values,
@@ -1210,7 +1212,7 @@ def _quality_metric_check(model, field, quality: DataQuality, threshold: Thresho
12101212
field,
12111213
"field_missing_values",
12121214
threshold,
1213-
name=f"Check that field {field} has missing_count {threshold.describe()}",
1215+
name=f"Check that field {field} has {threshold.describe('missing_count', unit)}",
12141216
missing_values=missing_values or None,
12151217
threshold_is_percent=is_percent,
12161218
severity=severity,

0 commit comments

Comments
 (0)