victorphoenix3 commented on a change in pull request #21551:
URL: https://github.com/apache/airflow/pull/21551#discussion_r815292841
##########
File path: airflow/providers/postgres/operators/postgres.py
##########
@@ -60,10 +63,28 @@ def __init__(
self.autocommit = autocommit
self.parameters = parameters
self.database = database
+ self.runtime_parameters = runtime_parameters
self.hook: Optional[PostgresHook] = None
def execute(self, context: 'Context'):
self.hook = PostgresHook(postgres_conn_id=self.postgres_conn_id,
schema=self.database)
- self.hook.run(self.sql, self.autocommit, parameters=self.parameters)
+ if self.runtime_parameters:
+ final_sql = []
+ sql_param = {}
+ for param in self.runtime_parameters:
+ set_param_sql = f"SET {{}} TO %({param})s;"
+ dynamic_sql = SQL(set_param_sql).format(Identifier(f"{param}"))
+ final_sql.append(dynamic_sql)
+ for param, val in self.runtime_parameters.items():
+ sql_param.update({f"{param}": f"{val}"})
+ if self.parameters:
+ sql_param.update(self.parameters)
+ if isinstance(self.sql, str):
+ final_sql.append(SQL(self.sql))
+ else:
+ final_sql.extend(list(map(SQL, self.sql)))
+ self.hook.run(final_sql, self.autocommit, parameters=sql_param)
+ else:
+ self.hook.run(self.sql, self.autocommit,
parameters=self.parameters)
Review comment:
This will execute when no server config parameters are passed by the
user. In this case, `parameters` will only contain the parameters of the
original sql query, like `begin date`, `end date` for the following example:
`"SELECT * FROM pet WHERE birth_date BETWEEN SYMMETRIC %(begin_date)s AND
%(end_date)s"
`
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]