Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Params Fail to Evaluate In Dag Body #39651

Closed
1 of 2 tasks
oliver-helix opened this issue May 15, 2024 · 6 comments
Closed
1 of 2 tasks

Params Fail to Evaluate In Dag Body #39651

oliver-helix opened this issue May 15, 2024 · 6 comments
Labels
area:core kind:bug This is a clearly a bug needs-triage label for new issues that we didn't triage yet

Comments

@oliver-helix
Copy link

Apache Airflow version

Other Airflow 2 version (please specify below)

If "Other Airflow 2 version" selected, which one?

2.7.2

What happened?

When providing airflow params, the parameter is not available in the body of the airflow dag.

What you think should happen instead?

The value should be available in the airflow dag

How to reproduce

from airflow import DAG
from airflow.models.param import Param
from airflow.providers.common.sql.operators.sql import SQLExecuteQueryOperator

with DAG("generate_password_dag", params={"test": Param("defeault", type="string")}) as dag:
    sql_operator = SQLExecuteQueryOperator(
        task_id="sql_operator",
        sql="sql/{{ params.test }}.sql",
        database="default",
    )

Operating System

NA

Versions of Apache Airflow Providers

NA

Deployment

Official Apache Airflow Helm Chart

Deployment details

NA

Anything else?

NA

Are you willing to submit PR?

  • Yes I am willing to submit a PR!

Code of Conduct

@oliver-helix oliver-helix added area:core kind:bug This is a clearly a bug needs-triage label for new issues that we didn't triage yet labels May 15, 2024
Copy link

boring-cyborg bot commented May 15, 2024

Thanks for opening your first issue here! Be sure to follow the issue template! If you are willing to raise PR to address this issue please do so, no need to wait for approval.

@oliver-helix oliver-helix changed the title Params Fail to Evaluate Params Fail to Evaluate In Dag Body May 15, 2024
@Taragolis
Copy link
Contributor

This rendered because you have .sql suffix in the end of your string. In this case it will tried to read template from the template file without rendering file path

@oliver-helix
Copy link
Author

Thanks for your response @Taragolis! For my use-case, I'd like to determine the SQL file with a param provided in the UI

@josh-fell
Copy link
Contributor

josh-fell commented May 17, 2024

You can use Jinja's include tag to render a file's contents.

For example, using this SQL file;

# dags/sql/default.sql
SELECT 'from a templated file';

and this task definition:

from airflow.models.dag import DAG
from airflow.models.param import Param
from airflow.providers.common.sql.operators.sql import SQLExecuteQueryOperator

with DAG(
    "generate_password_dag",
    params={"test": Param("default", type="string")},
):
    sql_operator = SQLExecuteQueryOperator(
        task_id="sql_operator",
        sql="{% include 'sql/' ~ params.test ~ '.sql' %}",
        conn_id="postgres",
    )

In the above task definition, ~ is string concatenation in Jinja.

Task log:
image

@oliver-helix Can you try that approach and see if it works please?

@oliver-helix
Copy link
Author

Thank you @josh-fell! This indeed works. Curious why sql="{% include 'sql/' ~ params.test ~ '.sql' %}" works when sql="sql/{{ params.test }}.sql" does not?

@Taragolis
Copy link
Contributor

works when sql="sql/{{ params.test }}.sql" does not?

It doesn't work by design, see: #39651 (comment)

Curious why sql="{% include 'sql/' ~ params.test ~ '.sql' %}" works

It is just a Jinja statement which rendered

@apache apache locked and limited conversation to collaborators May 17, 2024
@josh-fell josh-fell converted this issue into discussion #39692 May 17, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
area:core kind:bug This is a clearly a bug needs-triage label for new issues that we didn't triage yet
Projects
None yet
Development

No branches or pull requests

3 participants