|
23 | 23 | ) |
24 | 24 |
|
25 | 25 | from datacontract.config.variables import VariableError, contains_variables, resolve_variables |
26 | | -from datacontract.engines.checks.check_spec import CheckSpec, MetricType, Op, Threshold |
| 26 | +from datacontract.engines.checks.check_spec import METADATA_METRICS, CheckSpec, MetricType, Op, Threshold |
27 | 27 | from datacontract.engines.checks.dimensions import default_dimension |
28 | 28 | from datacontract.engines.checks.sql_guard import dialect_for_server_type, is_read_only_query |
29 | 29 | from datacontract.engines.checks.type_normalize import normalize_type_name |
|
34 | 34 | logger = logging.getLogger(__name__) |
35 | 35 |
|
36 | 36 | _FILE_SERVER_TYPES = {"local", "s3", "gcs", "azure"} |
37 | | -_NESTED_CHECK_SERVER_TYPES = {"dataframe", "databricks"} |
| 37 | +# Spark and every server read through DuckDB |
| 38 | +_NESTED_CHECK_SERVER_TYPES = {"dataframe", "databricks", "duckdb", "iceberg", "kafka"} | _FILE_SERVER_TYPES |
38 | 39 |
|
39 | 40 |
|
40 | 41 | # --------------------------------------------------------------------------- |
@@ -138,7 +139,8 @@ def _iter_property_paths( |
138 | 139 | yield from _iter_property_paths(prop.items.properties, f"{field_path}[]") |
139 | 140 |
|
140 | 141 |
|
141 | | -NESTED_NOT_RUN_REASON = "Quality rules on nested properties are only run on dataframe and databricks servers." |
| 142 | +def nested_not_run_reason(server_type: Optional[str]) -> str: |
| 143 | + return f"Checks on nested properties are not supported on {server_type} servers." |
142 | 144 |
|
143 | 145 |
|
144 | 146 | _PERCENT_UNITS = {"percent", "percentage", "%"} |
@@ -267,10 +269,7 @@ def _to_schema_checks( |
267 | 269 | primary_key_is_composite = len(primary_key_props) > 1 |
268 | 270 |
|
269 | 271 | for field, prop, nested in _iter_property_paths(properties): |
270 | | - if nested and server_type not in _NESTED_CHECK_SERVER_TYPES: |
271 | | - if prop.quality: |
272 | | - checks.extend(_quality_checks(model, field, prop.quality, server, not_run_reason=NESTED_NOT_RUN_REASON)) |
273 | | - continue |
| 272 | + first_check = len(checks) |
274 | 273 | # ODCS physicalName is the real column; mirror to_schema_name at field level. |
275 | 274 |
|
276 | 275 | checks.append( |
@@ -545,6 +544,25 @@ def _to_schema_checks( |
545 | 544 | if prop.quality: |
546 | 545 | checks.extend(_quality_checks(model, field, prop.quality, server, variables)) |
547 | 546 |
|
| 547 | + # A check the server cannot run is reported, not dropped. |
| 548 | + unenforced = [] |
| 549 | + if nested and server_type not in _NESTED_CHECK_SERVER_TYPES: |
| 550 | + # The parent's nested type check already covers presence and types. |
| 551 | + if check_types: |
| 552 | + checks[first_check:] = [c for c in checks[first_check:] if c.metric not in METADATA_METRICS] |
| 553 | + unenforced = checks[first_check:] |
| 554 | + reason = nested_not_run_reason(server_type) |
| 555 | + elif uses_raw_view: |
| 556 | + # The file is cast into the contract's types, so a type check would compare the contract with itself. |
| 557 | + unenforced = [c for c in checks[first_check:] if c.metric in METADATA_METRICS - {MetricType.FIELD_PRESENT}] |
| 558 | + reason = f"Checking types in {server.format} files is not supported yet." |
| 559 | + for check in unenforced: |
| 560 | + # An unrunnable rule keeps its own, more specific reason. |
| 561 | + if check.preset_result != "warning": |
| 562 | + check.metric = MetricType.UNSUPPORTED |
| 563 | + check.preset_result = "warning" |
| 564 | + check.preset_reason = reason |
| 565 | + |
548 | 566 | if primary_key_is_composite: |
549 | 567 | primary_key_fields = [prop.physicalName or prop.name for prop in primary_key_props] |
550 | 568 | checks.append( |
@@ -668,24 +686,10 @@ def _quality_checks( |
668 | 686 | quality_list: List[DataQuality], |
669 | 687 | server: Optional[Server], |
670 | 688 | variables: Optional[Mapping[str, str]] = None, |
671 | | - not_run_reason: Optional[str] = None, |
672 | 689 | ) -> List[CheckSpec]: |
673 | 690 | checks: List[CheckSpec] = [] |
674 | 691 | for count, quality in enumerate(quality_list): |
675 | | - if not_run_reason is None: |
676 | | - rule_checks = _quality_rule_checks(model, field, quality, count, server, variables) |
677 | | - elif quality.type == "sql" or quality.metric is not None: |
678 | | - kind = "sql" if quality.type == "sql" else "library" |
679 | | - rule_checks = _unexecuted_check( |
680 | | - f"{model}__{field}__quality_{kind}_{count}", |
681 | | - f"field_quality_{kind}", |
682 | | - model, |
683 | | - field, |
684 | | - quality, |
685 | | - not_run_reason, |
686 | | - ) |
687 | | - else: |
688 | | - rule_checks = [] |
| 692 | + rule_checks = _quality_rule_checks(model, field, quality, count, server, variables) |
689 | 693 | # Every check keeps a link back to the rule that declared it, so that |
690 | 694 | # `test --quality-id` / `test --tag` can select it. |
691 | 695 | for check in rule_checks: |
|
0 commit comments