airflow: Allow DAGs to trigger on Datasets with wildcard/regex

Description

I would like to have the possibility to have a DAG trigger on ANY dataset matching a wildcard/regex.

Use case/motivation

We have 2 (similar) use cases.

Trigger ingestion process on file arrival.

Files land in blob storage, which triggers events like:

{
"filename": "/raw/my/folder1/2024/04/15/file1.csv",
"event": "fileCreated"
}

and

{
"filename": "/raw/my/folder1/2024/04/15/file1.json",
"event": "fileCreated"
}

and

{
"filename": "/raw/my/folder1/2024/02/10/file1.json",
"event": "fileCreated"
}

These events are converted to Dataset update events, either by calling the Airflow API, or a DAG that polls a queue that contains the above events. In the above case, three Datasets would be updated:

  • ingestionblob://raw/my/folder1/2024/04/15/file1.csv
  • ingestionblob://raw/my/folder1/2024/04/15/file1.json
  • ingestionblob://raw/my/folder1/2024/02/10/file1.json

Image that we have a DAG ingestCSVFromRawRealTime. I would like to be able to trigger that DAG on datasets with wildcards ingestionblob://raw/my/folder1/{today.year}/{today.month}/{today.day}/*.csv.

Similarly for some DAGs like ingestJsonFromRawRealTime or ingestBackfilledCsvFromRaw.

Ideally, we can create capture groups in the regex of the Dataset trigger, and use these capture groups as DAG parameter.

Monitor database updates

Our (Databricks) DWH, like almost all other dbms’s, has a 3-level namespace: database.schema.table. We will create a mechanism to emit dataset events like

  • database1.bronze.table7
  • database4.silver.view3
  • database2.gold.streamingTable1
  • …

Sometimes, we want to be notified on updates to the whole database, schema or table. There are some use cases where having wildcard triggers would be beneficial:

  • Airflow needs to trigger a sync in our BI tool (eg. PowerBI, Tableau) if an object from the gold schema in database2 is updated.
  • When data arrived in the bronze schema, some curation scripts needs to be kicked-off to generate a ‘cleaned’ silver table.

Related issues

  • 34534. But with the new logical operators for Datasets, we can already trigger on more than 1 dataset.

Are you willing to submit a PR?

  • Yes I am willing to submit a PR!

Code of Conduct

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 2
  • Comments: 18 (17 by maintainers)

Most upvoted comments

Hi, could I be assigned to this issue? I am currently working on a project with the exact same use case. This feature will be helpful for a more flexible data-aware scheduling. Thanks.

Feel free. That’s a good motivation to contribute 😃

The original proposal seconded.

Hello, could a colleague (@TiDeane) and I be assigned to this issue? We are working for a university project and would be grateful to implement this feature. Thank you in advance.

Hi @Lee-W , I opened a draft PR and want to get an early feedback. I still need to figure out how to run proper testing on my local environment and experiments with more scenarios of the use cases mentioned in the issue. Below is the link to the draft PR. Can I get your help to have a look on it? Thank you!

https://github.com/apache/airflow/pull/50182

Hi, I want to share some updates about the progress so far and also ask some questions. I would appreciate if anyone can provide me some feedback or suggestions.

Considering all the assets generated at runtime can actually be attached to an asset alias. The consumer task can treat the asset alias as a single “asset” and be triggered when a new asset is attached and updated by producer.

From my perspective, the use cases can be achieved by either being able to access the name or uri of the asset that is attached to the asset alias, or through the extra keyword from the triggering_asset_events from the context in the consumer task.

However, I’ve run some experiments, and found that these information are not propagated to either the inlet_events or the triggering_asset_events, as shown below the log from the consumer task.

[2025-05-03, 21:44:35] INFO - inlet_events: InletEventsAccessors(_inlets=[AssetAlias(name='file_asset_event', group='asset')], _assets={}, _asset_aliases={AssetAliasUniqueKey(name='file_asset_event'): AssetAlias(name='file_asset_event', group='asset')}): chan="stdout": source="task"
[2025-05-03, 21:26:43] INFO - triggering_asset_events: TriggeringAssetEventAccessor(_events=defaultdict(<class 'list'>, {})): chan="stdout": source="task"

I actually attached the information in the outlet_events, and below is the log from the producer task.

[2025-05-03, 21:44:35] INFO - outlet_events:  OutletEventAccessors(_dict={AssetAliasUniqueKey(name='file_asset_event'): OutletEventAccessor(key=AssetAliasUniqueKey(name='file_asset_event'), extra={}, asset_alias_events=[AssetAliasEvent(source_alias_name='file_asset_event', dest_asset_key=AssetUniqueKey(name='/raw/data/file_05032025.csv', uri='/raw/data/file_05032025.csv'), extra={'filename': '/raw/data/file_05032025.csv'})])}): chan="stdout": source="task"

In terms of implementation, I am trying to see if the following information can be accessible from the consumer task when the consumer DAG is triggered through asset alias.

  1. AssetUniqueKey(name='/raw/data/file_05032025.csv', uri='/raw/data/file_05032025.csv') in inlet_events.
  2. Or extra={'filename': '/raw/data/file_05032025.csv'} in triggering_asset_events.

Questions:

  1. I am wondering if it is the right approach to the issue?
  2. Is there any caveat I should keep in mind when populating the information in inlet_events and triggering_asset_events?
  3. I am looking at the InletEventsAccessors under task-sdk/src/airflow/sdk/execution_time/context.py. Is it the right place to look at and modify the information being included in inlet_events?

Below is what I think about why this approach can resolve the issue.

  1. In the producer task, we can match wildcard/regex and make the file as an asset and attach it to an asset alias.
  2. The file path information can be propagated to the consumer DAG and task to further process.
  3. The consumer DAG is triggered by the asset alias (it can be treated as a single “asset”).

Below is my producer and consumer DAG code that generate the logs above. Thanks.

Producer

from airflow.sdk import Asset, AssetAlias, DAG, Param
from airflow.providers.standard.operators.python import PythonOperator

# Define the alias once
file_asset_alias = AssetAlias("file_asset_event")

with DAG(
    dag_id="asset_alias_producer",
    params={"filename": Param(type="string")}
):

    def my_function(**context):
        filename = context["params"]["filename"]
        print("filename:", filename)

        outlet_events = context["outlet_events"]
        outlet_events[file_asset_alias].add(
            Asset(filename),
            extra={"filename": filename}
        )
        print("outlet_events: ", outlet_events)

    my_task = PythonOperator(
        task_id="producer_task",
        python_callable=my_function,
        outlets=[file_asset_alias]  # use the same alias
    )

Consumer

from airflow.sdk import Asset, AssetAlias, DAG
from airflow.providers.standard.operators.python import PythonOperator

file_asset_alias = AssetAlias("file_asset_event")

with DAG(
    dag_id="asset_alias_consumer",
    schedule=[file_asset_alias]
):
    
    def my_function(**context):

        print("context:", context)

        inlet_events = context["inlet_events"]
        print("inlet_events:", inlet_events)

        triggering_asset_events = context["triggering_asset_events"]
        for event in triggering_asset_events[AssetAlias("file_asset_event")]:
            print(event)

    my_task = PythonOperator(
        task_id="consumer_task",
        python_callable=my_function,
        inlets=[file_asset_alias]
    )

Hi, could I be assigned to this issue? I am currently working on a project with the exact same use case. This feature will be helpful for a more flexible data-aware scheduling. Thanks.

Just assigned @TiDeane . Thanks !