Skip to content

Fix templated page_size staying a string in mapped SqlToS3Operator tasks - #77153

Merged
dabla merged 2 commits into
apache:mainfrom
pavansai1997:fix-sql-to-s3-mapped-page-size
Nov 16, 2026
Merged

dabla merged 2 commits into
apache:mainfrom
pavansai1997:fix-sql-to-s3-mapped-page-size

Conversation

@pavansai1997

@pavansai1997 pavansai1997 commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

SqlToS3Operator converts a templated page_size to int in a render_template_fields override (#75730). A mapped task never calls that method: the task runner calls MappedOperator.render_template_fields, which renders through _do_render_template_fields on the unmapped task. So in a mapped task page_size stayed a string, and the task uploaded the first page and then failed on len(df) < self.page_size with TypeError: '<' not supported between instances of 'int' and 'str'. With page_size rendering "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's AgentOperator uses the same hook for the same reason. Plain tasks are unchanged: BaseOperator.render_template_fields calls _do_render_template_fields with the operator as parent. Airflow 2.11's BaseOperator and MappedOperator call _do_render_template_fields with the same signature, so the fix applies there too.

Running a plain and a mapped task with page_size="{{ ps }}" through render_template_fields and execute(), with a SQL hook that returns pages of 2, 2 and 1 rows and a mocked S3Hook:

ps task before after
2 plain 3 uploads 3 uploads
2 mapped 1 upload, then TypeError 3 uploads
0 plain 1 unpaged upload 1 unpaged upload
0 mapped 1 paged upload (LIMIT 0), then TypeError 1 unpaged upload

The new tests render a .partial().expand() task with create_runtime_ti and ti.render_templates(), the method the task runner calls: one checks the converted page_size, the other that a negative page_size is still rejected. Both fail without this change. They are skipped before Airflow 3.0 because the fixture needs the Task SDK.

GenericTransfer in common.sql converts page_size in a render_template_fields override 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?
  • Yes — Code Puppy

Generated-by: Code Puppy following the guidelines

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 dabla left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_size check 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.
  • GenericTransfer in common.sql converts page_size and insert_args["commit_every"] in a render_template_fields override in the same way (generic_transfer.py#L139-L151), so a mapped GenericTransfer skips 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, MappedOperator and BaseOperator call _do_render_template_fields with the same five arguments and no session, and the provider's floor is apache-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.py pass on the head. With the source file put back to main, the new test fails (the templated case observed; the run stopped there).
  • Probes on the head: a mapped task with page_size="-1" raises the ValueError; .expand(page_size=["3", "4"]) gives the integer 4 for map index 1.
  • Static checks: ruff check, ruff format --check on both files and mypy on the source file are clean.
  • Other callers of the hook: none calls it on a SqlToS3Operator with a different signature (grep of _do_render_template_fields over the repo).

@dabla

dabla commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

This bug fix once more proves the importance of the discussion I started on the devlist:

https://lists.apache.org/thread.html/5nl6g6oosr5sg69nrfy151fwy5b6j5zf

@dabla
dabla merged commit 93aed9a into apache:main Nov 16, 2026
77 checks passed
@dabla

dabla commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Thank you for the fix well done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:providers provider:amazon AWS/Amazon - related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SqlToS3Operator: templated page_size stays a string in mapped tasks and fails after the first page

2 participants