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 OpenLineage extraction for deferrable AthenaOperator #40545

Merged
merged 1 commit into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
26 changes: 19 additions & 7 deletions airflow/providers/amazon/aws/operators/athena.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,16 @@ def execute(self, context: Context) -> str | None:
f"query_execution_id is {self.query_execution_id}."
)

# Save output location from API response for later use in OpenLineage.
self.output_location = self.hook.get_output_location(self.query_execution_id)

return self.query_execution_id

def execute_complete(self, context: Context, event: dict[str, Any] | None = None) -> str:
event = validate_execute_complete_event(event)

if event["status"] != "success":
raise AirflowException(f"Error while waiting for operation on cluster to complete: {event}")

# Save query_execution_id to be later used by listeners
self.query_execution_id = event["value"]
return event["value"]

def on_kill(self) -> None:
Expand All @@ -208,14 +208,21 @@ def on_kill(self) -> None:
)
self.hook.poll_query_status(self.query_execution_id, sleep_time=self.sleep_time)

def get_openlineage_facets_on_start(self) -> OperatorLineage:
def get_openlineage_facets_on_complete(self, _) -> OperatorLineage:
"""
Retrieve OpenLineage data by parsing SQL queries and enriching them with Athena API.

In addition to CTAS query, query and calculation results are stored in S3 location.
For that reason additional output is attached with this location.
For that reason additional output is attached with this location. Instead of using the complete
path where the results are saved (user's prefix + some UUID), we are creating a dataset with the
user-provided path only. This should make it easier to match this dataset across different processes.
"""
from openlineage.client.facet import ExtractionError, ExtractionErrorRunFacet, SqlJobFacet
from openlineage.client.facet import (
ExternalQueryRunFacet,
ExtractionError,
ExtractionErrorRunFacet,
SqlJobFacet,
)
from openlineage.client.run import Dataset

from airflow.providers.openlineage.extractors.base import OperatorLineage
Expand Down Expand Up @@ -265,6 +272,11 @@ def get_openlineage_facets_on_start(self) -> OperatorLineage:
)
)

if self.query_execution_id:
run_facets["externalQuery"] = ExternalQueryRunFacet(
externalQueryId=self.query_execution_id, source="awsathena"
)

if self.output_location:
parsed = urlparse(self.output_location)
outputs.append(Dataset(namespace=f"{parsed.scheme}://{parsed.netloc}", name=parsed.path or "/"))
Expand Down Expand Up @@ -301,7 +313,7 @@ def get_openlineage_dataset(self, database, table) -> Dataset | None:
)
}
fields = [
SchemaField(name=column["Name"], type=column["Type"], description=column["Comment"])
SchemaField(name=column["Name"], type=column["Type"], description=column.get("Comment"))
for column in table_metadata["TableMetadata"]["Columns"]
]
if fields:
Expand Down
23 changes: 22 additions & 1 deletion tests/providers/amazon/aws/operators/test_athena.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import pytest
from openlineage.client.facet import (
ExternalQueryRunFacet,
SchemaDatasetFacet,
SchemaField,
SqlJobFacet,
Expand Down Expand Up @@ -264,6 +265,24 @@ def test_is_deferred(self, mock_run_query):
query_execution_id=ATHENA_QUERY_ID,
)

def test_execute_complete_reassigns_query_execution_id_after_deferring(self):
"""Assert that we use query_execution_id from event after deferral."""

operator = AthenaOperator(
task_id="test_athena_operator",
query="SELECT * FROM TEST_TABLE",
database="TEST_DATABASE",
deferrable=True,
)
assert operator.query_execution_id is None

query_execution_id = "123456"
operator.execute_complete(
context=None,
event={"status": "success", "value": query_execution_id},
)
assert operator.query_execution_id == query_execution_id

@mock.patch.object(AthenaHook, "region_name", new_callable=mock.PropertyMock)
@mock.patch.object(AthenaHook, "get_conn")
def test_operator_openlineage_data(self, mock_conn, mock_region_name):
Expand All @@ -285,6 +304,7 @@ def mock_get_table_metadata(CatalogName, DatabaseName, TableName):
max_polling_attempts=3,
dag=self.dag,
)
op.query_execution_id = "12345" # Mocking what will be available after execution

expected_lineage = OperatorLineage(
inputs=[
Expand Down Expand Up @@ -365,5 +385,6 @@ def mock_get_table_metadata(CatalogName, DatabaseName, TableName):
query="INSERT INTO TEST_TABLE SELECT CUSTOMER_EMAIL FROM DISCOUNTS",
)
},
run_facets={"externalQuery": ExternalQueryRunFacet(externalQueryId="12345", source="awsathena")},
)
assert op.get_openlineage_facets_on_start() == expected_lineage
assert op.get_openlineage_facets_on_complete(None) == expected_lineage