Repository navigation
Fix templated page_size staying a string in mapped SqlToS3Operator tasks - #77153
Conversation
SqlToS3Operator converted the rendered page_size to int in a render_template_fields override, but a mapped task never calls that method: MappedOperator renders through _do_render_template_fields on the unmapped task. page_size stayed a string, so a mapped task uploaded the first page and then failed comparing it with the page length. Convert page_size in _do_render_template_fields instead, which both plain and mapped tasks go through.
dabla
left a comment
There was a problem hiding this comment.
Addressed
Nothing since the previous round: this is the first review, and the PR has no other reviews or threads.
Open
- Comment [1]: a mapped test for the negative
page_sizecheck should be added before merge. - CI has not run yet: the workflows wait for a maintainer's approval ("need workflow approval"). The results below are from a local run only.
GenericTransferincommon.sqlconvertspage_sizeandinsert_args["commit_every"]in arender_template_fieldsoverride in the same way (generic_transfer.py#L139-L151), so a mappedGenericTransferskips that conversion too. The PR leaves it out on purpose (other distribution, not reproduced); it needs its own issue or PR. I read the code and did not reproduce it either.
The fix itself is right. MappedOperator.render_template_fields calls unmapped_task._do_render_template_fields(parent=unmapped_task, ...) and never the operator's render_template_fields (mappedoperator.py#L855-L861), so the hook the PR moves to is the one both plain and mapped tasks reach. The parent is self guard is needed: the templater calls the same method again for nested templated objects, before the operator's own fields are done. KubernetesPodOperator and AgentOperator use the same hook with the same guard.
Checked on head 414b44d712, diff-based plus a scratch worktree:
- Airflow 2.11 claim in the PR body: holds. In tag
2.11.2,MappedOperatorandBaseOperatorcall_do_render_template_fieldswith the same five arguments and nosession, and the provider's floor isapache-airflow>=2.11.0. The new test is skipped before 3.0, so the 2.11 path rests on this reading, not on a test. - Tests: the 67 tests of
test_sql_to_s3.pypass on the head. With the source file put back tomain, the new test fails (thetemplatedcase observed; the run stopped there). - Probes on the head: a mapped task with
page_size="-1"raises theValueError;.expand(page_size=["3", "4"])gives the integer4for map index 1. - Static checks:
ruff check,ruff format --checkon both files andmypyon the source file are clean. - Other callers of the hook: none calls it on a
SqlToS3Operatorwith a different signature (grep of_do_render_template_fieldsover the repo).
|
This bug fix once more proves the importance of the discussion I started on the devlist: |
|
Thank you for the fix well done! |
SqlToS3Operatorconverts a templatedpage_sizetointin arender_template_fieldsoverride (#75730). A mapped task never calls that method: the task runner callsMappedOperator.render_template_fields, which renders through_do_render_template_fieldson the unmapped task. So in a mapped taskpage_sizestayed a string, and the task uploaded the first page and then failed onlen(df) < self.page_sizewithTypeError: '<' not supported between instances of 'int' and 'str'. Withpage_sizerendering"0", a mapped task paged instead of writing one file.This moves the conversion into
_do_render_template_fields, which plain and mapped tasks both go through. It runs after the fields are rendered and only for the operator itself (parent is self), not for nested templated objects.common.ai'sAgentOperatoruses the same hook for the same reason. Plain tasks are unchanged:BaseOperator.render_template_fieldscalls_do_render_template_fieldswith the operator asparent. Airflow 2.11'sBaseOperatorandMappedOperatorcall_do_render_template_fieldswith the same signature, so the fix applies there too.Running a plain and a mapped task with
page_size="{{ ps }}"throughrender_template_fieldsandexecute(), with a SQL hook that returns pages of 2, 2 and 1 rows and a mockedS3Hook:ps22TypeError00LIMIT 0), thenTypeErrorThe new tests render a
.partial().expand()task withcreate_runtime_tiandti.render_templates(), the method the task runner calls: one checks the convertedpage_size, the other that a negativepage_sizeis still rejected. Both fail without this change. They are skipped before Airflow 3.0 because the fixture needs the Task SDK.GenericTransferincommon.sqlconvertspage_sizein arender_template_fieldsoverride in the same way. It is a separate distribution and I have not reproduced the problem there, so it is not changed here.closes: #77151
Was generative AI tooling used to co-author this PR?
Generated-by: Code Puppy following the guidelines