Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue reported by the community in slack with lineage in Cosmos 1.1 #526

Merged
merged 8 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions cosmos/operators/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import shutil
import signal
import tempfile
from attr import define
from pathlib import Path
from typing import Any, Callable, Literal, Sequence, TYPE_CHECKING

Expand Down Expand Up @@ -59,6 +60,13 @@
logger.exception(error)
is_openlineage_available = False

@define
class OperatorLineage: # type: ignore
inputs: list[str] = list()
outputs: list[str] = list()
run_facets: dict[str, str] = dict()
job_facets: dict[str, str] = dict()
tatiana marked this conversation as resolved.
Show resolved Hide resolved


class DbtLocalBaseOperator(DbtBaseOperator):
"""
Expand Down Expand Up @@ -309,17 +317,32 @@
"""
Collect the input, output, job and run facets for this operator.
It relies on the calculate_openlineage_events_completes having being called before.

This method is called by Openlineage even if `execute` fails, because `get_openlineage_facets_on_failure`
is not implemented.
"""

inputs = []
outputs = []
run_facets: dict[str, Any] = {}
job_facets: dict[str, Any] = {}

for completed in task_instance.openlineage_events_completes:
[inputs.append(input_) for input_ in completed.inputs if input_ not in inputs] # type: ignore
[outputs.append(output) for output in completed.outputs if output not in outputs] # type: ignore
run_facets = {**run_facets, **completed.run.facets}
job_facets = {**job_facets, **completed.job.facets}
openlineage_events_completes = None
if hasattr(self, "openlineage_events_completes"):
openlineage_events_completes = self.openlineage_events_completes
elif hasattr(task_instance, "openlineage_events_completes"):
openlineage_events_completes = task_instance.openlineage_events_completes

Check warning on line 334 in cosmos/operators/local.py

View check run for this annotation

Codecov / codecov/patch

cosmos/operators/local.py#L334

Added line #L334 was not covered by tests
else:
logger.warning("Unable to emit OpenLineage events due to lack of data.")
tatiana marked this conversation as resolved.
Show resolved Hide resolved
tatiana marked this conversation as resolved.
Show resolved Hide resolved

if openlineage_events_completes is not None:
for completed in openlineage_events_completes:
[inputs.append(input_) for input_ in completed.inputs if input_ not in inputs] # type: ignore
[outputs.append(output) for output in completed.outputs if output not in outputs] # type: ignore
run_facets = {**run_facets, **completed.run.facets}
job_facets = {**job_facets, **completed.job.facets}
else:
logger.warning("Unable to emit OpenLineage events due to lack of dependencies or data.")
tatiana marked this conversation as resolved.
Show resolved Hide resolved

return OperatorLineage(
inputs=inputs,
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ classifiers = [
]
dependencies = [
# Airflow & Pydantic issue: https://github.com/apache/airflow/issues/32311
"attrs",
"pydantic>=1.10.0,<2.0.0",
"apache-airflow>=2.3.0",
"importlib-metadata; python_version < '3.8'",
Expand Down Expand Up @@ -133,7 +134,6 @@ dependencies = [
"apache-airflow-providers-cncf-kubernetes>=5.1.1,<7.3.0",
"types-PyYAML",
"types-attrs",
"attrs",
"types-requests",
"types-python-dateutil",
"apache-airflow"
Expand Down
17 changes: 17 additions & 0 deletions tests/operators/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,23 @@ class MockEvent:
assert facets.job_facets == {"d": 4}


def test_run_operator_emits_events_without_openlineage_events_completes(caplog):
dbt_base_operator = DbtLocalBaseOperator(
profile_config=profile_config,
task_id="my-task",
project_dir="my/dir",
should_store_compiled_sql=False,
)
delattr(dbt_base_operator, "openlineage_events_completes")
facets = dbt_base_operator.get_openlineage_facets_on_complete(dbt_base_operator)
assert facets.inputs == []
assert facets.outputs == []
assert facets.run_facets == {}
assert facets.job_facets == {}
log = "Unable to emit OpenLineage events due to lack of dependencies or data."
assert log in caplog.text


def test_store_compiled_sql() -> None:
dbt_base_operator = DbtLocalBaseOperator(
profile_config=profile_config,
Expand Down