tirkarthi commented on issue #26555:
URL: https://github.com/apache/airflow/issues/26555#issuecomment-1253924590

   1. For mapped tasks render might need to retrieve `template_fields` from 
task instead of `task.__class__` and rendered task needs to be used so that the 
values are displayed for mapped tasks.
   
   2. `current_state` doesn't take map_index into account yielding many results 
for a mapped task. It should also filter by `TaskInstance.map_index`
   
   
https://github.com/apache/airflow/blob/520893a9a13a6ec5611ed4dd76039974eb4c069c/airflow/models/taskinstance.py#L805-L822
   
   Something like below is my first attempt : 
   
   ```diff
   diff --git a/airflow/cli/commands/task_command.py 
b/airflow/cli/commands/task_command.py
   index 9caa8bb4bd..2e164400c1 100644
   --- a/airflow/cli/commands/task_command.py
   +++ b/airflow/cli/commands/task_command.py
   @@ -586,13 +586,20 @@ def task_render(args):
            task, args.map_index, 
exec_date_or_run_id=args.execution_date_or_run_id, create_if_necessary="memory"
        )
        ti.render_templates()
   -    for attr in task.__class__.template_fields:
   +    rendered_task = ti.task
   +
   +    if task.is_mapped:
   +        template_fields = task.template_fields
   +    else:
   +        template_fields = task.__class__.template_fields
   +
   +    for attr in template_fields:
            print(
                textwrap.dedent(
                    f"""        # 
----------------------------------------------------------
            # property: {attr}
            # ----------------------------------------------------------
   -        {getattr(task, attr)}
   +        {getattr(rendered_task, attr)}
            """
                )
            )
   diff --git a/airflow/models/taskinstance.py b/airflow/models/taskinstance.py
   index 52562e72ad..582edac081 100644
   --- a/airflow/models/taskinstance.py
   +++ b/airflow/models/taskinstance.py
   @@ -817,6 +817,7 @@ class TaskInstance(Base, LoggingMixin):
                    TaskInstance.dag_id == self.dag_id,
                    TaskInstance.task_id == self.task_id,
                    TaskInstance.run_id == self.run_id,
   +                TaskInstance.map_index == self.map_index
                )
                .scalar()
            )
   ```


-- 
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]

Reply via email to