You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "hussein-awala (via GitHub)" <gi...@apache.org> on 2025/08/24 14:25:53 UTC
[PR] Support templated extra in outlets assets [airflow]
hussein-awala opened a new pull request, #54885:
URL: https://github.com/apache/airflow/pull/54885
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<!--
Thank you for contributing! Please make sure that your code changes
are covered with tests. And in case of new features or big changes
remember to adjust the documentation.
Feel free to ping committers for the review!
In case of an existing issue, reference it using one of the following:
closes: #ISSUE
related: #ISSUE
How to write a good git commit message:
http://chris.beams.io/posts/git-commit/
-->
This PR adds support for Jinja templates in outlet assets, which will be rendered in the emitted events.
<!-- Please keep an empty line above the dashes. -->
---
**^ Add meaningful description above**
Read the **[Pull Request Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-guidelines)** for more information.
In case of fundamental code changes, an Airflow Improvement Proposal ([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals)) is needed.
In case of a new dependency, check compliance with the [ASF 3rd Party License Policy](https://www.apache.org/legal/resolved.html#category-x).
In case of backwards incompatible changes please leave a note in a newsfragment file, named `{pr_number}.significant.rst` or `{issue_number}.significant.rst`, in [airflow-core/newsfragments](https://github.com/apache/airflow/tree/main/airflow-core/newsfragments).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support templated extra in outlets assets [airflow]
Posted by "vikramkoka (via GitHub)" <gi...@apache.org>.
vikramkoka commented on PR #54885:
URL: https://github.com/apache/airflow/pull/54885#issuecomment-3220623439
Not an issue, but a question: What's the intended use case here?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support templated extra in outlets assets [airflow]
Posted by "hussein-awala (via GitHub)" <gi...@apache.org>.
hussein-awala commented on PR #54885:
URL: https://github.com/apache/airflow/pull/54885#issuecomment-3220687760
> Not an issue, but a question: What's the intended use case here?
The asset can be used to connect two DAGs for two different teams. The owner of the dataset (the team who owns the producer task) is responsible for providing some metadata to the consumer DAG to explain what to process and how.
To simplify the answer, I can take a part of [AIP-76](https://cwiki.apache.org/confluence/display/AIRFLOW/AIP-76%2BAsset%2BPartitions?utm_source=chatgpt.com) as an example. With this PR, the partition can be defined at runtime, since we can have backfills and runs created by external events, and the partition is not always known at parse time.
```python
SomeOperator(
task_id="write_asset",
...,
outlets=Asset(
"asset_example",
extra={
"partition": "{{ logical_date.strftime('%Y_%m_%d/%H') }}",
}
),
)
```
Also, when the team owner of the dataset wants to change the partition pattern, there’s no need to notify the consumer side, as the dataset schema still respects the data contract.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support templated extra in outlets assets [airflow]
Posted by "hussein-awala (via GitHub)" <gi...@apache.org>.
hussein-awala merged PR #54885:
URL: https://github.com/apache/airflow/pull/54885
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support templated extra in outlets assets [airflow]
Posted by "Lee-W (via GitHub)" <gi...@apache.org>.
Lee-W commented on code in PR #54885:
URL: https://github.com/apache/airflow/pull/54885#discussion_r2297641481
##########
airflow-core/tests/unit/models/test_taskinstance.py:
##########
@@ -1653,6 +1653,98 @@ def _write2_post_execute(context, _):
assert events["write2"].asset.uri == "test_outlet_asset_extra_2"
assert events["write2"].extra == {"x": 1}
+ @pytest.mark.want_activate_assets(True)
+ def test_outlet_asset_template_extra(self, dag_maker, session):
+ from airflow.sdk.definitions.asset import Asset
+
+ with dag_maker(schedule=None, serialized=True, session=session):
+
+ @task(
+ outlets=Asset(
+ "test_outlet_asset_template_extra1",
+ extra={
+ "static_extra": "value",
+ "dag_id": "{{ dag.dag_id }}",
+ "nested_extra": {
+ "task_id": "{{ task.task_id }}",
+ "logical_date": "{{ ds }}",
+ },
+ },
+ )
+ )
+ def write_template1(*, outlet_events):
+ yield Metadata(
+ Asset("test_outlet_asset_template_extra1"),
+ {
+ "dag_id": "override_dag_id",
+ "some_other_key": "some_other_value",
+ },
+ )
+
+ write_template1()
+
+ BashOperator(
+ task_id="write_template2",
+ bash_command=":",
+ outlets=Asset(
+ "test_outlet_asset_template_extra2",
+ extra={
+ "static_extra": "value",
+ "dag_id": "{{ dag.dag_id }}",
+ "nested_extra": {
+ "task_id": "{{ task.task_id }}",
+ "logical_date": "{{ ds }}",
+ },
+ },
+ ),
+ )
+
+ BashOperator(
+ task_id="write_asset_no_extra",
+ bash_command=":",
+ outlets=Asset("test_outlet_asset_no_extra"),
+ )
+
+ dr: DagRun = dag_maker.create_dagrun()
+ for ti in dr.get_task_instances(session=session):
+ ti.run(session=session)
+
+ events = dict(iter(session.execute(select(AssetEvent.source_task_id, AssetEvent))))
+ assert set(events) == {"write_template1", "write_template2", "write_asset_no_extra"}
+
+ assert events["write_template1"].source_dag_id == dr.dag_id
+ assert events["write_template1"].source_run_id == dr.run_id
+ assert events["write_template1"].source_task_id == "write_template1"
+ assert events["write_template1"].asset.uri == "test_outlet_asset_template_extra1"
+ assert events["write_template1"].extra == {
Review Comment:
I'm a bit surprised by merging the extra. Is this the existing behavior?
##########
task-sdk/src/airflow/sdk/definitions/_internal/templater.py:
##########
@@ -89,7 +89,7 @@ def prepare_template(self) -> None:
def resolve_template_files(self) -> None:
"""Get the content of files for template_field / template_ext."""
- if self.template_ext:
+ if hasattr(self, "template_ext") and self.template_ext:
Review Comment:
```suggestion
if getattr(self, "template_ext", None):
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support templated extra in outlets assets [airflow]
Posted by "Lee-W (via GitHub)" <gi...@apache.org>.
Lee-W commented on code in PR #54885:
URL: https://github.com/apache/airflow/pull/54885#discussion_r2297670589
##########
airflow-core/tests/unit/models/test_taskinstance.py:
##########
@@ -1653,6 +1653,98 @@ def _write2_post_execute(context, _):
assert events["write2"].asset.uri == "test_outlet_asset_extra_2"
assert events["write2"].extra == {"x": 1}
+ @pytest.mark.want_activate_assets(True)
+ def test_outlet_asset_template_extra(self, dag_maker, session):
+ from airflow.sdk.definitions.asset import Asset
+
+ with dag_maker(schedule=None, serialized=True, session=session):
+
+ @task(
+ outlets=Asset(
+ "test_outlet_asset_template_extra1",
+ extra={
+ "static_extra": "value",
+ "dag_id": "{{ dag.dag_id }}",
+ "nested_extra": {
+ "task_id": "{{ task.task_id }}",
+ "logical_date": "{{ ds }}",
+ },
+ },
+ )
+ )
+ def write_template1(*, outlet_events):
+ yield Metadata(
+ Asset("test_outlet_asset_template_extra1"),
+ {
+ "dag_id": "override_dag_id",
+ "some_other_key": "some_other_value",
+ },
+ )
+
+ write_template1()
+
+ BashOperator(
+ task_id="write_template2",
+ bash_command=":",
+ outlets=Asset(
+ "test_outlet_asset_template_extra2",
+ extra={
+ "static_extra": "value",
+ "dag_id": "{{ dag.dag_id }}",
+ "nested_extra": {
+ "task_id": "{{ task.task_id }}",
+ "logical_date": "{{ ds }}",
+ },
+ },
+ ),
+ )
+
+ BashOperator(
+ task_id="write_asset_no_extra",
+ bash_command=":",
+ outlets=Asset("test_outlet_asset_no_extra"),
+ )
+
+ dr: DagRun = dag_maker.create_dagrun()
+ for ti in dr.get_task_instances(session=session):
+ ti.run(session=session)
+
+ events = dict(iter(session.execute(select(AssetEvent.source_task_id, AssetEvent))))
+ assert set(events) == {"write_template1", "write_template2", "write_asset_no_extra"}
+
+ assert events["write_template1"].source_dag_id == dr.dag_id
+ assert events["write_template1"].source_run_id == dr.run_id
+ assert events["write_template1"].source_task_id == "write_template1"
+ assert events["write_template1"].asset.uri == "test_outlet_asset_template_extra1"
+ assert events["write_template1"].extra == {
Review Comment:
Yep, I didn't find any logic related to this PR. Just curious. 👀
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support templated extra in outlets assets [airflow]
Posted by "hussein-awala (via GitHub)" <gi...@apache.org>.
hussein-awala commented on code in PR #54885:
URL: https://github.com/apache/airflow/pull/54885#discussion_r2297661977
##########
airflow-core/tests/unit/models/test_taskinstance.py:
##########
@@ -1653,6 +1653,98 @@ def _write2_post_execute(context, _):
assert events["write2"].asset.uri == "test_outlet_asset_extra_2"
assert events["write2"].extra == {"x": 1}
+ @pytest.mark.want_activate_assets(True)
+ def test_outlet_asset_template_extra(self, dag_maker, session):
+ from airflow.sdk.definitions.asset import Asset
+
+ with dag_maker(schedule=None, serialized=True, session=session):
+
+ @task(
+ outlets=Asset(
+ "test_outlet_asset_template_extra1",
+ extra={
+ "static_extra": "value",
+ "dag_id": "{{ dag.dag_id }}",
+ "nested_extra": {
+ "task_id": "{{ task.task_id }}",
+ "logical_date": "{{ ds }}",
+ },
+ },
+ )
+ )
+ def write_template1(*, outlet_events):
+ yield Metadata(
+ Asset("test_outlet_asset_template_extra1"),
+ {
+ "dag_id": "override_dag_id",
+ "some_other_key": "some_other_value",
+ },
+ )
+
+ write_template1()
+
+ BashOperator(
+ task_id="write_template2",
+ bash_command=":",
+ outlets=Asset(
+ "test_outlet_asset_template_extra2",
+ extra={
+ "static_extra": "value",
+ "dag_id": "{{ dag.dag_id }}",
+ "nested_extra": {
+ "task_id": "{{ task.task_id }}",
+ "logical_date": "{{ ds }}",
+ },
+ },
+ ),
+ )
+
+ BashOperator(
+ task_id="write_asset_no_extra",
+ bash_command=":",
+ outlets=Asset("test_outlet_asset_no_extra"),
+ )
+
+ dr: DagRun = dag_maker.create_dagrun()
+ for ti in dr.get_task_instances(session=session):
+ ti.run(session=session)
+
+ events = dict(iter(session.execute(select(AssetEvent.source_task_id, AssetEvent))))
+ assert set(events) == {"write_template1", "write_template2", "write_asset_no_extra"}
+
+ assert events["write_template1"].source_dag_id == dr.dag_id
+ assert events["write_template1"].source_run_id == dr.run_id
+ assert events["write_template1"].source_task_id == "write_template1"
+ assert events["write_template1"].asset.uri == "test_outlet_asset_template_extra1"
+ assert events["write_template1"].extra == {
Review Comment:
yes, I didn't touch this part, currently if we yield multiple Metadata for the same asset, each one updates the matching extra of the previous one, and the same for the Asset default extra
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org