Skip to content

Commit

Permalink
test(ingest/mcp_diff): Fallback to overwriting file on more complicat…
Browse files Browse the repository at this point in the history
…ed diffs (datahub-project#11407)

Co-authored-by: Harshal Sheth <[email protected]>
  • Loading branch information
asikowitz and hsheth2 authored Sep 18, 2024
1 parent de40d25 commit 3e3a05f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
},
"externalUrl": "http://airflow.example.com/tree?dag_id=datahub_emitter_operator_jinja_template_dag",
"name": "datahub_emitter_operator_jinja_template_dag",
"description": "An example dag with jinja template"
"description": "An example dag with jinja template",
"env": "PROD"
}
}
},
Expand Down Expand Up @@ -140,13 +141,14 @@
"downstream_task_ids": "[]",
"inlets": "[]",
"outlets": "[]",
"openlineage_run_facet_unknownSourceAttribute": "{\"_producer\": \"https://github.com/OpenLineage/OpenLineage/tree/1.18.0/integration/airflow\", \"_schemaURL\": \"https://openlineage.io/spec/2-0-2/OpenLineage.json#/$defs/BaseFacet\", \"unknownItems\": [{\"name\": \"DatahubEmitterOperator\", \"properties\": {\"depends_on_past\": false, \"downstream_task_ids\": \"[]\", \"execution_timeout\": \"<<non-serializable: timedelta>>\", \"executor_config\": {}, \"ignore_first_depends_on_past\": true, \"is_setup\": false, \"is_teardown\": false, \"mapped\": false, \"operator_class\": \"datahub_airflow_plugin.operators.datahub.DatahubEmitterOperator\", \"owner\": \"airflow\", \"priority_weight\": 1, \"queue\": \"default\", \"retries\": 0, \"retry_exponential_backoff\": false, \"task_id\": \"datahub_emitter_operator_jinja_template_dag_task\", \"trigger_rule\": \"all_success\", \"upstream_task_ids\": \"[]\", \"wait_for_downstream\": false, \"wait_for_past_depends_before_skipping\": false, \"weight_rule\": \"downstream\"}, \"type\": \"operator\"}]}"
"openlineage_run_facet_unknownSourceAttribute": "{\"_producer\": \"https://github.com/OpenLineage/OpenLineage/tree/1.18.0/integration/airflow\", \"_schemaURL\": \"https://openlineage.io/spec/2-0-2/OpenLineage.json#/$defs/BaseFacet\", \"unknownItems\": [{\"name\": \"DatahubEmitterOperator\", \"properties\": {\"depends_on_past\": false, \"downstream_task_ids\": \"[]\", \"execution_timeout\": \"<<non-serializable: timedelta>>\", \"executor_config\": {}, \"ignore_first_depends_on_past\": true, \"is_setup\": false, \"is_teardown\": false, \"mapped\": false, \"multiple_outputs\": false, \"operator_class\": \"datahub_airflow_plugin.operators.datahub.DatahubEmitterOperator\", \"owner\": \"airflow\", \"priority_weight\": 1, \"queue\": \"default\", \"retries\": 0, \"retry_exponential_backoff\": false, \"task_id\": \"datahub_emitter_operator_jinja_template_dag_task\", \"trigger_rule\": \"all_success\", \"upstream_task_ids\": \"[]\", \"wait_for_downstream\": false, \"wait_for_past_depends_before_skipping\": false, \"weight_rule\": \"<<non-serializable: _DownstreamPriorityWeightStrategy>>\"}, \"type\": \"operator\"}]}"
},
"externalUrl": "http://airflow.example.com/taskinstance/list/?flt1_dag_id_equals=datahub_emitter_operator_jinja_template_dag&_flt_3_task_id=datahub_emitter_operator_jinja_template_dag_task",
"name": "datahub_emitter_operator_jinja_template_dag_task",
"type": {
"string": "COMMAND"
}
},
"env": "PROD"
}
}
},
Expand Down Expand Up @@ -233,16 +235,16 @@
"state": "running",
"operator": "DatahubEmitterOperator",
"priority_weight": "1",
"log_url": "http://airflow.example.com/log?execution_date=2023-09-27T21%3A34%3A38%2B00%3A00&task_id=datahub_emitter_operator_jinja_template_dag_task&dag_id=datahub_emitter_operator_jinja_template_dag&map_index=-1",
"log_url": "http://airflow.example.com/dags/datahub_emitter_operator_jinja_template_dag/grid?dag_run_id=manual_run_test&task_id=datahub_emitter_operator_jinja_template_dag_task&map_index=-1&tab=logs",
"orchestrator": "airflow",
"dag_id": "datahub_emitter_operator_jinja_template_dag",
"task_id": "datahub_emitter_operator_jinja_template_dag_task"
},
"externalUrl": "http://airflow.example.com/log?execution_date=2023-09-27T21%3A34%3A38%2B00%3A00&task_id=datahub_emitter_operator_jinja_template_dag_task&dag_id=datahub_emitter_operator_jinja_template_dag&map_index=-1",
"externalUrl": "http://airflow.example.com/dags/datahub_emitter_operator_jinja_template_dag/grid?dag_run_id=manual_run_test&task_id=datahub_emitter_operator_jinja_template_dag_task&map_index=-1&tab=logs",
"name": "datahub_emitter_operator_jinja_template_dag_datahub_emitter_operator_jinja_template_dag_task_manual_run_test",
"type": "BATCH_AD_HOC",
"created": {
"time": 1726570937652,
"time": 1726699852909,
"actor": "urn:li:corpuser:datahub"
}
}
Expand All @@ -267,7 +269,7 @@
"aspectName": "dataProcessInstanceRunEvent",
"aspect": {
"json": {
"timestampMillis": 1726570937652,
"timestampMillis": 1726699852909,
"partitionSpec": {
"partition": "FULL_TABLE_SNAPSHOT",
"type": "FULL_TABLE"
Expand Down Expand Up @@ -360,13 +362,14 @@
"downstream_task_ids": "[]",
"inlets": "[]",
"outlets": "[]",
"openlineage_run_facet_unknownSourceAttribute": "{\"_producer\": \"https://github.com/OpenLineage/OpenLineage/tree/1.18.0/integration/airflow\", \"_schemaURL\": \"https://openlineage.io/spec/2-0-2/OpenLineage.json#/$defs/BaseFacet\", \"unknownItems\": [{\"name\": \"DatahubEmitterOperator\", \"properties\": {\"depends_on_past\": false, \"downstream_task_ids\": \"[]\", \"execution_timeout\": \"<<non-serializable: timedelta>>\", \"executor_config\": {}, \"ignore_first_depends_on_past\": true, \"is_setup\": false, \"is_teardown\": false, \"mapped\": false, \"operator_class\": \"datahub_airflow_plugin.operators.datahub.DatahubEmitterOperator\", \"owner\": \"airflow\", \"priority_weight\": 1, \"queue\": \"default\", \"retries\": 0, \"retry_exponential_backoff\": false, \"task_id\": \"datahub_emitter_operator_jinja_template_dag_task\", \"trigger_rule\": \"all_success\", \"upstream_task_ids\": \"[]\", \"wait_for_downstream\": false, \"wait_for_past_depends_before_skipping\": false, \"weight_rule\": \"downstream\"}, \"type\": \"operator\"}]}"
"openlineage_run_facet_unknownSourceAttribute": "{\"_producer\": \"https://github.com/OpenLineage/OpenLineage/tree/1.18.0/integration/airflow\", \"_schemaURL\": \"https://openlineage.io/spec/2-0-2/OpenLineage.json#/$defs/BaseFacet\", \"unknownItems\": [{\"name\": \"DatahubEmitterOperator\", \"properties\": {\"depends_on_past\": false, \"downstream_task_ids\": \"[]\", \"execution_timeout\": \"<<non-serializable: timedelta>>\", \"executor_config\": {}, \"ignore_first_depends_on_past\": true, \"is_setup\": false, \"is_teardown\": false, \"mapped\": false, \"multiple_outputs\": false, \"operator_class\": \"datahub_airflow_plugin.operators.datahub.DatahubEmitterOperator\", \"owner\": \"airflow\", \"priority_weight\": 1, \"queue\": \"default\", \"retries\": 0, \"retry_exponential_backoff\": false, \"task_id\": \"datahub_emitter_operator_jinja_template_dag_task\", \"trigger_rule\": \"all_success\", \"upstream_task_ids\": \"[]\", \"wait_for_downstream\": false, \"wait_for_past_depends_before_skipping\": false, \"weight_rule\": \"<<non-serializable: _DownstreamPriorityWeightStrategy>>\"}, \"type\": \"operator\"}]}"
},
"externalUrl": "http://airflow.example.com/taskinstance/list/?flt1_dag_id_equals=datahub_emitter_operator_jinja_template_dag&_flt_3_task_id=datahub_emitter_operator_jinja_template_dag_task",
"name": "datahub_emitter_operator_jinja_template_dag_task",
"type": {
"string": "COMMAND"
}
},
"env": "PROD"
}
}
},
Expand Down Expand Up @@ -441,7 +444,7 @@
"aspectName": "dataProcessInstanceRunEvent",
"aspect": {
"json": {
"timestampMillis": 1726570937811,
"timestampMillis": 1726699853392,
"partitionSpec": {
"partition": "FULL_TABLE_SNAPSHOT",
"type": "FULL_TABLE"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
},
"externalUrl": "http://airflow.example.com/tree?dag_id=datahub_emitter_operator_jinja_template_dag",
"name": "datahub_emitter_operator_jinja_template_dag",
"description": "An example dag with jinja template"
"description": "An example dag with jinja template",
"env": "PROD"
}
}
},
Expand Down Expand Up @@ -146,7 +147,8 @@
"name": "datahub_emitter_operator_jinja_template_dag_task",
"type": {
"string": "COMMAND"
}
},
"env": "PROD"
}
}
},
Expand Down Expand Up @@ -242,7 +244,7 @@
"name": "datahub_emitter_operator_jinja_template_dag_datahub_emitter_operator_jinja_template_dag_task_manual_run_test",
"type": "BATCH_AD_HOC",
"created": {
"time": 1726570903946,
"time": 1726621020280,
"actor": "urn:li:corpuser:datahub"
}
}
Expand All @@ -267,7 +269,7 @@
"aspectName": "dataProcessInstanceRunEvent",
"aspect": {
"json": {
"timestampMillis": 1726570903946,
"timestampMillis": 1726621020280,
"partitionSpec": {
"partition": "FULL_TABLE_SNAPSHOT",
"type": "FULL_TABLE"
Expand Down Expand Up @@ -366,7 +368,8 @@
"name": "datahub_emitter_operator_jinja_template_dag_task",
"type": {
"string": "COMMAND"
}
},
"env": "PROD"
}
}
},
Expand Down Expand Up @@ -441,7 +444,7 @@
"aspectName": "dataProcessInstanceRunEvent",
"aspect": {
"json": {
"timestampMillis": 1726570904070,
"timestampMillis": 1726621020427,
"partitionSpec": {
"partition": "FULL_TABLE_SNAPSHOT",
"type": "FULL_TABLE"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,11 @@ def assert_metadata_files_equal(

diff = diff_metadata_json(output, golden, ignore_paths, ignore_order=ignore_order)
if diff and update_golden:
if isinstance(diff, MCPDiff):
if isinstance(diff, MCPDiff) and diff.is_delta_valid:
diff.apply_delta(golden)
write_metadata_file(pathlib.Path(golden_path), golden)
else:
# Fallback: just overwrite the golden file
shutil.copyfile(str(output_path), str(golden_path))
return

Expand Down
19 changes: 15 additions & 4 deletions metadata-ingestion/src/datahub/testing/mcp_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ class MCPDiff:
aspect_changes: Dict[str, Dict[str, MCPAspectDiff]] # urn -> aspect -> diff
urns_added: Set[str]
urns_removed: Set[str]
is_delta_valid: bool

def __bool__(self) -> bool:
return bool(self.aspect_changes)
Expand All @@ -162,25 +163,31 @@ def create(
) -> "MCPDiff":
ignore_paths = [cls.convert_path(path) for path in ignore_paths]

is_delta_valid = True
aspect_changes: Dict[str, Dict[str, MCPAspectDiff]] = defaultdict(dict)
for urn in golden.keys() | output.keys():
golden_map = golden.get(urn, {})
output_map = output.get(urn, {})
for aspect_name in golden_map.keys() | output_map.keys():
t1 = golden_map.get(aspect_name, [])
t2 = output_map.get(aspect_name, [])
diff = DeepDiff(
t1=golden_map.get(aspect_name, []),
t2=output_map.get(aspect_name, []),
t1=t1,
t2=t2,
exclude_regex_paths=ignore_paths,
ignore_order=True,
custom_operators=[DeltaInfoOperator()],
)
if diff:
aspect_changes[urn][aspect_name] = MCPAspectDiff.create(diff)
if len(t1) > 1 or len(t2) > 1:
is_delta_valid = False

return cls(
urns_added=output.keys() - golden.keys(),
urns_removed=golden.keys() - output.keys(),
aspect_changes=aspect_changes,
is_delta_valid=is_delta_valid,
)

@staticmethod
Expand All @@ -193,9 +200,13 @@ def convert_path(path: str) -> str:
)

def apply_delta(self, golden: List[Dict[str, Any]]) -> None:
"""Update a golden file to match an output file based on the diff.
:param golden: Golden file represented as a list of MCPs, altered in-place.
"""
aspect_diffs = [v for d in self.aspect_changes.values() for v in d.values()]
for aspect_diff in aspect_diffs:
for (_, old, new), diffs in aspect_diff.aspects_changed.items():
for (_, old, new) in aspect_diff.aspects_changed.keys():
golden[old.delta_info.idx] = new.delta_info.original

indices_to_remove = set()
Expand Down Expand Up @@ -268,7 +279,7 @@ def report_aspect(ga: AspectForDiff, idx: int, msg: str = "") -> str:
suffix = "rd"
else:
suffix = "th"
ordinal = f"{(idx+1)}{suffix} " if idx else ""
ordinal = f"{(idx + 1)}{suffix} " if idx else ""
return f"{ordinal}<{ga.aspect_name}> {msg}"

@staticmethod
Expand Down

0 comments on commit 3e3a05f

Please sign in to comment.