Airflow 中的 Jinja 模板以及格式化文本

Jinja templating in Airflow along with formatted text

我正在尝试 运行 由 Airflow 呈现的 SQL 语句,但我还试图在从 [=21= 传入的语句中包含一个变量]. SQL 语句只是一个 where 子句,在 WHERE 之后,我试图添加一个日期时间,减去几秒:

f" ' {{ ts - macros.timedelta(seconds={lower_delay} + 1) }} ' "

所以我希望在 Airflow 中计算和呈现双花括号中的内容,但我想在此之前传递这个名为 lower_delay 的变量。我已经尝试过在 lower_delay 以及整个字符串周围使用零个、一个或两个额外的花括号进行不同的组合,但我似乎每次都会遇到不同的错误。

传递这个 lower_delay 变量(它只是一个数字)以使其最终正确呈现的正确方法是什么?

Jinja 模板需要两个大括号,当您使用 f-strings 或 str.format 时,它会在渲染时用一个大括号替换两个大括号:

Format strings contain “replacement fields” surrounded by curly braces {}. Anything that is not contained in braces is considered literal text, which is copied unchanged to the output. If you need to include a brace character in the literal text, it can be escaped by doubling: {{ and }}.

所以以下应该有效:

f" ' {{{{ ts - macros.timedelta(seconds={lower_delay} + 1) }}}} ' "