I'm trying to define task that requires a run-time parameter (let's call it 'batch_dt') in Apache Airflow.
I'm using OracleStoredProcedureOperator and the parameter of the procedure is of database type date.
procedure use_dates (
i_date in date,
i_date2 in date,
i_date3 in date
);
However I'm having hard time defining this parameter as runtime variable. I could use an exactly formatted string for particular database but don't want to depend on current NLS setting in the database.
Airflow macros don't work
{{ dag_run.conf['batch_dt'] }}or even{{ macros.datetime.strptime(dag_run.conf['batch_dt'], '%Y-%m-%d') }}returns always a string resulting inORA-01861: literal does not match format string
Using
to_date('{{ dag_run.conf['batch_dt'] }}', 'DD-MM-YYYY')results inORA-01858: a non-numeric character was found where a numeric was expected
When I define
date.today()right in the task, it works fine, however I need to use run-time variable.
res_task = OracleStoredProcedureOperator(
task_id = 'mytask',
procedure = 'use_dates',
parameters =
{"i_date": date.today(), #works but is not runtime
"i_date2": "to_date('{{ dag_run.conf['batch_dt'] }}', 'DD-MM-YYYY')",
#returns a string "to_date('13-06-2022', 'DD-MM-YYYY')" which results in ORA-01858: a non-numeric character was found where a numeric was expected
"i_date3": "{{ macros.datetime.strptime(dag_run.conf['batch_dt'], '%Y-%m-%d' ) }}"
#returns a string '2022-06-13 00:00:00' which results in ORA-01861: literal does not match format string
}
)
I was thinking about macro that returns a datetime object at runtime, however it seems macros can return only strings. Any idea how this can be achieved?