Skip to content

task-sql-decorator: Introducing the @task.sql decorator#60851

Merged
kaxil merged 15 commits into
apache:mainfrom
jroachgolf84:task-sql-decorator
Feb 16, 2026
Merged

task-sql-decorator: Introducing the @task.sql decorator#60851
kaxil merged 15 commits into
apache:mainfrom
jroachgolf84:task-sql-decorator

Conversation

@jroachgolf84

@jroachgolf84 jroachgolf84 commented Jan 21, 2026

Copy link
Copy Markdown
Collaborator

Description

For DAG authors familiar with writing Python functions to do "something", the @task decorator is one of the most popular tools authoring logic in Airflow. There are several other decorators, such as @task.bash, @task.kubernetes, etc. that extend this functionality. However, there is no @task.sql decorator.

This PR introduces the @task.sql decorator. This decorator is a wrapper around the SQLExecuteQueryOperator. However, the value returned from the Python function is the SQL query that is executed.

Here's an example usage:

from airflow.sdk import DAG, task

from datetime import datetime


with DAG(
    dag_id="sql_deco",
    start_date=datetime(2025, 1, 1),
    schedule="@once"
) as dag:

    @task.sql(
        conn_id="task-sql-decorator"  # Transient connection defined in the UI
    )
    def task_1():
        return "SELECT 1;"

    task_1()

Testing

To run the unit tests that were authored for this decorator, the command below can be used:

breeze testing providers-tests providers/common/sql/tests/unit/common/sql/decorators/test_sql.py

The DAG above was also used to validate the functionality of the @task.sql decorator.

Other Notes

There will be additional documentation and examples that comes out of this initial pull request, with the goal of providing parity between this new decorator and the SQLExecuteQueryOperator. For now, this PR just provides the bare-bones.

@jroachgolf84
jroachgolf84 marked this pull request as ready for review February 9, 2026 15:08

@kaxil kaxil left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me but worth checking how OL works with it.

cc @mobuchowski @kacpermuda

@jroachgolf84

Copy link
Copy Markdown
Collaborator Author

@kaxil, what is the best way to test this? Do we have a framework in-place for this already?

@potiuk

potiuk commented Feb 16, 2026

Copy link
Copy Markdown
Member

@kaxil, what is the best way to test this? Do we have a framework in-place for this already?

There is openlineage integration that starts Marquez I believe.

@potiuk

potiuk commented Feb 16, 2026

Copy link
Copy Markdown
Member

Comment thread providers/common/sql/src/airflow/providers/common/sql/decorators/sql.py Outdated

@kacpermuda kacpermuda left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks !

From the OL perspective, the Ol methods from SqlExecuteQueryOperator are executed, since PythonOperator does not have them. With the added type check in BaseSqlOperator's OL method, the *_on_start method does nothing, since we do not know SQL text before execution, and the *_on_complete behaves as the query was executed with the usual SqlExecuteQueryOperator which is good.

@mobuchowski mobuchowski left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to look at OpenLineage impl, but @kacpermuda already did this - so I just have a few drive-by comments :)

@kaxil
kaxil merged commit e9021db into apache:main Feb 16, 2026
101 checks passed
OscarLigthart pushed a commit to OscarLigthart/airflow that referenced this pull request Feb 17, 2026
…0851)

For DAG authors familiar with writing Python functions to do "something", the `@task` decorator is one of the most popular tools authoring logic in Airflow. There are several other decorators, such as `@task.bash`, `@task.kubernetes`, etc. that extend this functionality. However, there is no `@task.sql` decorator.

This PR introduces the `@task.sql` decorator. This decorator is a wrapper around the `SQLExecuteQueryOperator`. However, the value returned from the Python function is the SQL query that is executed.

Here's an example usage: 

```python
from airflow.sdk import DAG, task

from datetime import datetime


with DAG(
    dag_id="sql_deco",
    start_date=datetime(2025, 1, 1),
    schedule="@once"
) as dag:

    @task.sql(
        conn_id="task-sql-decorator"  # Transient connection defined in the UI
    )
    def task_1():
        return "SELECT 1;"

    task_1()
```
choo121600 pushed a commit to choo121600/airflow that referenced this pull request Feb 22, 2026
…0851)

For DAG authors familiar with writing Python functions to do "something", the `@task` decorator is one of the most popular tools authoring logic in Airflow. There are several other decorators, such as `@task.bash`, `@task.kubernetes`, etc. that extend this functionality. However, there is no `@task.sql` decorator.

This PR introduces the `@task.sql` decorator. This decorator is a wrapper around the `SQLExecuteQueryOperator`. However, the value returned from the Python function is the SQL query that is executed.

Here's an example usage: 

```python
from airflow.sdk import DAG, task

from datetime import datetime


with DAG(
    dag_id="sql_deco",
    start_date=datetime(2025, 1, 1),
    schedule="@once"
) as dag:

    @task.sql(
        conn_id="task-sql-decorator"  # Transient connection defined in the UI
    )
    def task_1():
        return "SELECT 1;"

    task_1()
```
Subham-KRLX pushed a commit to Subham-KRLX/airflow that referenced this pull request Mar 4, 2026
…0851)

For DAG authors familiar with writing Python functions to do "something", the `@task` decorator is one of the most popular tools authoring logic in Airflow. There are several other decorators, such as `@task.bash`, `@task.kubernetes`, etc. that extend this functionality. However, there is no `@task.sql` decorator.

This PR introduces the `@task.sql` decorator. This decorator is a wrapper around the `SQLExecuteQueryOperator`. However, the value returned from the Python function is the SQL query that is executed.

Here's an example usage: 

```python
from airflow.sdk import DAG, task

from datetime import datetime


with DAG(
    dag_id="sql_deco",
    start_date=datetime(2025, 1, 1),
    schedule="@once"
) as dag:

    @task.sql(
        conn_id="task-sql-decorator"  # Transient connection defined in the UI
    )
    def task_1():
        return "SELECT 1;"

    task_1()
```
dominikhei pushed a commit to dominikhei/airflow that referenced this pull request Mar 11, 2026
…0851)

For DAG authors familiar with writing Python functions to do "something", the `@task` decorator is one of the most popular tools authoring logic in Airflow. There are several other decorators, such as `@task.bash`, `@task.kubernetes`, etc. that extend this functionality. However, there is no `@task.sql` decorator.

This PR introduces the `@task.sql` decorator. This decorator is a wrapper around the `SQLExecuteQueryOperator`. However, the value returned from the Python function is the SQL query that is executed.

Here's an example usage: 

```python
from airflow.sdk import DAG, task

from datetime import datetime


with DAG(
    dag_id="sql_deco",
    start_date=datetime(2025, 1, 1),
    schedule="@once"
) as dag:

    @task.sql(
        conn_id="task-sql-decorator"  # Transient connection defined in the UI
    )
    def task_1():
        return "SELECT 1;"

    task_1()
```
Ankurdeewan pushed a commit to Ankurdeewan/airflow that referenced this pull request Mar 15, 2026
…0851)

For DAG authors familiar with writing Python functions to do "something", the `@task` decorator is one of the most popular tools authoring logic in Airflow. There are several other decorators, such as `@task.bash`, `@task.kubernetes`, etc. that extend this functionality. However, there is no `@task.sql` decorator.

This PR introduces the `@task.sql` decorator. This decorator is a wrapper around the `SQLExecuteQueryOperator`. However, the value returned from the Python function is the SQL query that is executed.

Here's an example usage: 

```python
from airflow.sdk import DAG, task

from datetime import datetime


with DAG(
    dag_id="sql_deco",
    start_date=datetime(2025, 1, 1),
    schedule="@once"
) as dag:

    @task.sql(
        conn_id="task-sql-decorator"  # Transient connection defined in the UI
    )
    def task_1():
        return "SELECT 1;"

    task_1()
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants