.filter(
                        TaskInstance.dag_id == self.dag_id,
                        TaskInstance.task_id == self.task_id,
                        TaskInstance.run_id == run_id,
                        TaskInstance.map_index == 0,
                    )
                    .count()
                    > 0
                )
                if not zero_index_ti_exists:
                    
                    
                    unmapped_ti.map_index = 0
                    self.log.debug("Updated in place to become %s", unmapped_ti)
                    all_expanded_tis.append(unmapped_ti)
                    session.flush()
                else:
                    self.log.debug("Deleting the original task instance: %s", unmapped_ti)
                    session.delete(unmapped_ti)
                state = unmapped_ti.state
        if total_length is None or total_length < 1:
            
            indexes_to_map: Iterable[int] = ()
        else:
            
            current_max_mapping = (
                session.query(func.max(TaskInstance.map_index))
                .filter(
                    TaskInstance.dag_id == self.dag_id,
                    TaskInstance.task_id == self.task_id,