3838from datacontract .engines .checks .type_normalize import normalize_type_name
3939from datacontract .engines .ibis .native_type import supports_native_type_introspection
4040from datacontract .model .enum_values import get_enum_values
41+ from datacontract .model .map_type import get_map_key , get_map_value
4142from datacontract .model .server import get_server_type
4243
4344logger = logging .getLogger (__name__ )
@@ -81,81 +82,54 @@ def to_schema_name(schema_object: SchemaObject, server_type: Optional[str]) -> s
8182 return schema_object .name
8283
8384
84- def _scalar_element_type (prop : SchemaProperty , physical : bool ) -> Optional [str ]:
85- """The declared element type of an array of scalars, or None for anything else."""
86- if normalize_type_name (prop .logicalType or prop .physicalType ) != "array" or prop .items is None :
87- return None
88- items = prop .items
89- if items .properties or items .items is not None :
90- return None
91- label = (items .physicalType or items .logicalType ) if physical else (items .logicalType or items .physicalType )
92- if normalize_type_name (label ) in ("object" , "array" , "map" ):
93- return None
94- return label
95-
96-
97- def _declared_type_label (prop : SchemaProperty , physical : bool ) -> Optional [str ]:
98- """Render the declared type with its children, e.g. ``OBJECT(code VARCHAR(10), description VARCHAR)``."""
99- label = (prop .physicalType or prop .logicalType ) if physical else (prop .logicalType or prop .physicalType )
100- base = normalize_type_name (label )
101- if base == "array" and prop .items is not None :
102- return f"{ label } ({ _declared_type_label (prop .items , physical )} )"
103- if base == "object" and prop .properties :
104- children = ", " .join (f"{ child .name } { _declared_type_label (child , physical )} " for child in prop .properties )
105- return f"{ label } ({ children } )"
106- return label
107-
108-
109- def _nested_type_check (model : str , field : str , prop : SchemaProperty , physical : bool ) -> CheckSpec :
110- check_type = "field_nested_physical_type" if physical else "field_nested_type"
111- element = _scalar_element_type (prop , physical )
112- if element is not None :
113- name = f"Check that items of array { field } have { 'physical type' if physical else 'type' } { element } "
114- else :
115- name = f"Check that nested { 'physical types' if physical else 'types' } of { field } are correct"
116- return CheckSpec (
117- key = f"{ model } __{ field } __{ check_type } " ,
118- category = "schema" ,
119- type = check_type ,
120- name = name ,
121- model = model ,
122- field = field ,
123- metric = MetricType .FIELD_NESTED_TYPE ,
124- expected_type_label = _declared_type_label (prop , physical ),
125- expected_schema_property = prop ,
126- )
127-
128-
12985def quality_definition_yaml (quality : DataQuality ) -> str :
13086 """The quality rule as YAML, as the CLI parsed it: ODCS keys the model does not
13187 know are dropped, and comments with them."""
13288 return yaml .safe_dump (quality .model_dump (exclude_none = True ), sort_keys = False )
13389
13490
13591def _property_type (prop : SchemaProperty ) -> str :
136- return normalize_type_name (prop .physicalType or prop .logicalType )
92+ return normalize_type_name (prop .physicalType or prop .logicalType or _implied_type (prop ))
93+
94+
95+ def _implied_type (prop : SchemaProperty ) -> str | None :
96+ """The type a property that declares none implies by its ``properties``, ``items`` or ``map`` block."""
97+ if prop .logicalType or prop .physicalType :
98+ return None
99+ implied = [
100+ kind for kind , declared in (("object" , prop .properties ), ("array" , prop .items ), ("map" , prop .map )) if declared
101+ ]
102+ return implied [0 ] if len (implied ) == 1 else None
137103
138104
139105def _iter_property_paths (
140106 properties : list [SchemaProperty ] | None ,
141107 prefix : str | None = None ,
142108):
143- """Yield ``(path, property, nested)`` for every property, descending into objects and array items."""
109+ """Yield ``(path, property, nested)`` for every property, descending into objects, array items and maps ."""
144110 for prop in properties or []:
145111 field = prop .physicalName or prop .name
146112 field_path = f"{ prefix } .{ field } " if prefix else field
147113 yield field_path , prop , prefix is not None
148-
149- prop_type = _property_type (prop )
150- if prop_type == "object" and prop .properties :
151- yield from _iter_property_paths (prop .properties , field_path )
152- elif prop_type == "array" and prop .items and prop .items .properties :
153- # `[]` marks the array hop; the executor turns it into a predicate
154- # over the elements instead of a column lookup.
155- yield from _iter_property_paths (prop .items .properties , f"{ field_path } []" )
156- elif prop_type == "array" and prop .items and _constrains_values (prop .items ):
157- # The items of an array of plain values, such as a pattern on every tag
158- yield f"{ field_path } []" , prop .items , True
114+ yield from _iter_nested_paths (prop , field_path )
115+
116+
117+ def _iter_nested_paths (prop : SchemaProperty , field_path : str ):
118+ prop_type = _property_type (prop )
119+ if prop_type == "array" and prop .items is not None :
120+ # `[]` marks the array hop; the executor turns it into a predicate
121+ # over the elements instead of a column lookup.
122+ items = prop .items
123+ if items .logicalType or items .physicalType or items .properties or _constrains_values (items ):
124+ yield f"{ field_path } []" , items , True
125+ yield from _iter_nested_paths (items , f"{ field_path } []" )
126+ elif prop_type == "map" :
127+ for side , definition in (("key" , get_map_key (prop )), ("value" , get_map_value (prop ))):
128+ if definition is not None :
129+ yield f"{ field_path } [{ side } ]" , definition , True
130+ yield from _iter_nested_paths (definition , f"{ field_path } [{ side } ]" )
131+ elif prop .properties and prop_type in ("object" , None ):
132+ yield from _iter_property_paths (prop .properties , field_path )
159133
160134
161135def _constrains_values (prop : SchemaProperty ) -> bool :
@@ -333,7 +307,8 @@ def _to_schema_checks(
333307 first_check = len (checks )
334308 # ODCS physicalName is the real column; mirror to_schema_name at field level.
335309
336- if not documents or (prop .required and not nested ):
310+ # An array's or map's own presence covers that of its items, key and value
311+ if not field .endswith ("]" ) and (not documents or (prop .required and not nested )):
337312 checks .append (
338313 CheckSpec (
339314 key = f"{ model } __{ field } __field_is_present" ,
@@ -347,19 +322,16 @@ def _to_schema_checks(
347322 )
348323 )
349324
350- # The raw view cannot provide nested type checks
351- declared_base = normalize_type_name (prop .logicalType or prop .physicalType )
352- nested_checks_possible = (
353- check_types
354- and not uses_raw_view
355- and declared_base in ("object" , "array" , "map" )
356- and (bool (prop .properties ) or prop .items is not None or prop .map is not None )
357- )
358- base_prop = (
359- SchemaProperty (name = prop .name , logicalType = prop .logicalType , physicalType = prop .physicalType )
360- if nested_checks_possible
361- else prop
362- )
325+ prop_type = _property_type (prop )
326+ implied_type = _implied_type (prop )
327+ own_type = prop
328+ if prop_type == "array" :
329+ own_type = prop .model_copy (update = {"items" : None })
330+ elif prop_type == "map" :
331+ # the key and value may also sit in the pre-3.2.0 custom properties or properties
332+ own_type = prop .model_copy (update = {"map" : None , "properties" : None , "customProperties" : None })
333+ elif prop .properties and prop_type in ("object" , None ):
334+ own_type = prop .model_copy (update = {"properties" : None })
363335
364336 # A declared physicalType is checked against the column's real native
365337 # type in the platform catalog and takes precedence over logicalType,
@@ -378,15 +350,11 @@ def _to_schema_checks(
378350 expected_category = prop .physicalType ,
379351 expected_type_label = prop .physicalType ,
380352 expected_physical_type = prop .physicalType ,
381- # Carried for the logicalType fallback when the native type
382- # cannot be read or the physicalType is cross-dialect.
383- expected_schema_property = base_prop if prop .logicalType is not None else None ,
353+ expected_schema_property = own_type ,
384354 )
385355 )
386- if nested_checks_possible :
387- checks .append (_nested_type_check (model , field , prop , physical = True ))
388- elif check_types and prop .logicalType is not None :
389- label = prop .logicalType or ""
356+ elif check_types and (prop .logicalType is not None or implied_type ):
357+ label = prop .logicalType or implied_type or ""
390358 checks .append (
391359 CheckSpec (
392360 key = f"{ model } __{ field } __field_type" ,
@@ -398,13 +366,13 @@ def _to_schema_checks(
398366 metric = MetricType .FIELD_TYPE ,
399367 expected_category = label ,
400368 expected_type_label = label ,
401- expected_schema_property = base_prop ,
369+ expected_schema_property = own_type .model_copy (update = {"logicalType" : label }),
370+ implied_type = implied_type is not None ,
402371 )
403372 )
404- if nested_checks_possible :
405- checks .append (_nested_type_check (model , field , prop , physical = False ))
406373
407- if prop .required :
374+ # A map key is never null, so a required key holds by definition
375+ if prop .required and not field .endswith ("[key]" ):
408376 checks .append (
409377 _missing_count_check (
410378 model ,
@@ -607,23 +575,27 @@ def _to_schema_checks(
607575 checks .extend (_quality_checks (model , field , prop .quality , server , variables , custom_quality_checks ))
608576
609577 # A check the server cannot run is reported, not dropped.
610- unenforced = []
611- if nested and server_type not in _NESTED_CHECK_SERVER_TYPES :
612- # The parent's nested type check already covers presence and types.
613- if check_types :
614- checks [first_check :] = [c for c in checks [first_check :] if c .metric not in METADATA_METRICS ]
615- unenforced = checks [first_check :]
616- reason = nested_not_run_reason (server_type )
617- elif uses_raw_view :
618- # The file is cast into the contract's types, so a type check would compare the contract with itself.
619- unenforced = [c for c in checks [first_check :] if c .metric in METADATA_METRICS - {MetricType .FIELD_PRESENT }]
620- reason = f"Checking types in { server .format } files is not supported yet."
621- for check in unenforced :
622- # An unrunnable rule keeps its own, more specific reason.
623- if check .preset_result != "warning" :
624- check .metric = MetricType .UNSUPPORTED
625- check .preset_result = "warning"
626- check .preset_reason = reason
578+ for check in checks [first_check :]:
579+ if check .preset_result == "warning" :
580+ # An unrunnable rule keeps its own, more specific reason.
581+ continue
582+ if check .metric in METADATA_METRICS :
583+ if not uses_raw_view or check .metric == MetricType .FIELD_PRESENT :
584+ # Presence and types are read from the schema, which every server reports.
585+ continue
586+ # The file is cast into the contract's types, so a type check would compare the contract with itself.
587+ reason = f"Checking types in { server .format } files is not supported yet."
588+ elif "[key]" in field or "[value]" in field :
589+ reason = "Checks on the key or value of a map are not supported yet."
590+ elif "[][]" in field :
591+ reason = "Checks on the items of an array's items are not supported yet."
592+ elif nested and server_type not in _NESTED_CHECK_SERVER_TYPES :
593+ reason = nested_not_run_reason (server_type )
594+ else :
595+ continue
596+ check .metric = MetricType .UNSUPPORTED
597+ check .preset_result = "warning"
598+ check .preset_reason = reason
627599
628600 if primary_key_is_composite :
629601 primary_key_fields = [prop .physicalName or prop .name for prop in primary_key_props ]
0 commit comments