0
0
Apache Airflowdevops~20 mins

BranchPythonOperator in Apache Airflow - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
BranchPythonOperator Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of BranchPythonOperator with conditional branching
Given the following Airflow DAG snippet using BranchPythonOperator, what will be the output of the task branch_task when execution_date.day is 15?
Apache Airflow
def choose_branch(**kwargs):
    day = kwargs['execution_date'].day
    if day < 10:
        return 'task_a'
    else:
        return 'task_b'

branch_task = BranchPythonOperator(
    task_id='branch_task',
    python_callable=choose_branch,
    provide_context=True,
    dag=dag
)
A'task_a'
B'branch_task'
CNone
D'task_b'
Attempts:
2 left
💡 Hint
Check the condition comparing the day of execution_date to 10.
🧠 Conceptual
intermediate
1:30remaining
Understanding BranchPythonOperator behavior
What happens to downstream tasks that are not returned by the BranchPythonOperator's callable?
AThey cause the DAG to fail.
BThey are executed normally.
CThey are skipped and not executed.
DThey are retried automatically.
Attempts:
2 left
💡 Hint
Think about how branching controls the flow of tasks.
🔀 Workflow
advanced
2:30remaining
Correct order of task execution with BranchPythonOperator
Arrange the following tasks in the order they execute in an Airflow DAG with a BranchPythonOperator named branch_task that chooses between task_a and task_b. The DAG starts with start_task and ends with end_task.
A1,2,3,4
B1,3,2,4
C2,1,3,4
D1,2,4,3
Attempts:
2 left
💡 Hint
BranchPythonOperator runs after the start and before the chosen task.
Troubleshoot
advanced
2:00remaining
Error caused by BranchPythonOperator returning invalid task_id
What error will Airflow raise if the BranchPythonOperator's callable returns a task_id that does not exist in the DAG?
AAirflowSkipException
BAirflowException
CAirflowTaskNotFoundException
DAirflowDagCycleException
Attempts:
2 left
💡 Hint
Consider what happens when the DAG tries to find a task by a name that is missing.
Best Practice
expert
3:00remaining
Best practice for multiple branches in BranchPythonOperator
Which approach is best to handle multiple possible branches in a BranchPythonOperator to ensure all branches are valid and maintainable?
AReturn a list of task_ids for all possible branches and ensure they exist in the DAG.
BReturn a single task_id and use multiple BranchPythonOperators chained for other branches.
CReturn task_ids as strings concatenated with commas in one string.
DReturn None to run all downstream tasks.
Attempts:
2 left
💡 Hint
BranchPythonOperator supports returning multiple task_ids as a list.