-
Notifications
You must be signed in to change notification settings - Fork 14.5k
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
Task mapping against 'params' #24014
Comments
Unfortunately |
PostgresOperator
's params
parameter in dynamic task mapping results in error
Is there a way to raise exception that will explain to the user that the chosen parameter for expand is not supported? |
It’s possible, but not particularly doable unfortunately, since it’s difficult to tell apart what the user puts in when a function is called. With a function like this: def foo(a=None): pass We don’t have a good way to tell if user called |
Oh, actually we already mention this in the documentation:
https://airflow.apache.org/docs/apache-airflow/stable/concepts/dynamic-task-mapping.html |
In this particular example the fix is to use |
Yes, indeed. The original DAG I ran into the problem with was using different types of queries, such as See also this Stack Overflow post from another user. Right now, I'm using a Python task as a workaround and then pass it directly to the @task
def generate_sql(policy):
return Path('include/data_retention_delete.sql') \
.read_text(encoding="utf-8").format(table_fqn=policy[0],
column=policy[1],
value=policy[2],
)
@dag(
start_date=pendulum.datetime(2021, 11, 19, tz="UTC"),
schedule_interval="@daily",
catchup=False,
)
def data_retention_delete():
sql_statements = generate_sql.expand(policy=get_policies())
PostgresOperator.partial(
task_id="delete_partition",
postgres_conn_id="cratedb_connection",
).expand(sql=sql_statements) |
@hammerhead See the "Anything else" section of #24388, but you should be able to still map via Assuming the desired SQL sql = """
DELETE FROM {{ task.mapped_kwargs.parameters[ti.map_index].table_fqn }}
WHERE {{ task.mapped_kwargs.parameters[ti.map_index].column }} = {{ task.mapped_kwargs.parameters[ti.map_index].value }};"""
PostgresOperator.partial(
task_id="delete_partitions",
postgres_conn_id="cratedb_connection",
sql=sql,
).expand(parameters=[
{"table_fqn": "tbl1", "column": "col1", "value": "val1"},
{"table_fqn": "tbl2", "column": "col2", "value": "val3"},
) Does this help? |
Thanks, @josh-fell, the example you provided does work. @task
def parameter_values():
# in our original implementation, this is a database query using pg_hook
return [
{"table_fqn": "raw_metrics", "column": "ts_day", "value": "v1"},
]
sql = """
DELETE FROM {{ task.mapped_kwargs.parameters[ti.map_index].table_fqn }}
WHERE {{ task.mapped_kwargs.parameters[ti.map_index].column }} = {{ task.mapped_kwargs.parameters[ti.map_index].value }};"""
PostgresOperator.partial(
task_id="delete_partitions",
postgres_conn_id="cratedb_connection",
sql=sql,
).expand(parameters=parameter_values()) This fails with an error related to XCom lookups:
|
@uranusjr Could you add this to your todo list to fix please? |
…rams` parameter This is a new upstream improvement that required a workaround earlier: apache/airflow#24014
…rams` parameter This is a new upstream improvement that required a workaround earlier: apache/airflow#24014
Apache Airflow version
2.3.1 (latest released)
What happened
Importing a DAG using
PostgresOperator
withexpand(params=[...])
fails, claimingparams
was already specified as a partial argument, even though it wasn't.What you think should happen instead
The DAG imports successfully.
How to reproduce
Exception during import:
Operating System
macOS 12.4
Versions of Apache Airflow Providers
apache-airflow-providers-postgres==4.1.0
Deployment
Astronomer
Deployment details
No response
Anything else
No response
Are you willing to submit PR?
Code of Conduct
The text was updated successfully, but these errors were encountered: