Sub topic 10
Branching with If/else condition tasks
This chapter assumes you can build a multi-task job with dependencies, and that you know how to publish and read task values with dbutils.jobs.taskValues. A condition task lets a job choose a path at run time based on a value.
What a condition task does
A condition task is an If/else task. It compares two operands and produces an outcome of true or false. It runs no code and uses no cluster, so it adds almost no time to a run.
Downstream tasks decide which outcome they wait for. A task that depends on the true outcome runs only when the comparison holds. A task that depends on the false outcome runs only when it does not. A task on a branch that is not taken is marked as excluded, and it is not counted as a failure.
A worked example
The ingest task publishes row_count as a task value:
# In the notebook for task "ingest"
row_count = spark.table("sales.raw_orders").count()
dbutils.jobs.taskValues.set(key="row_count", value=row_count)The job then adds a condition task that checks the value. The transform task depends on the true outcome of that check.
[
{
"task_key": "check_rows",
"depends_on": [{"task_key": "ingest"}],
"condition_task": {
"op": "GREATER_THAN",
"left": "{{tasks.ingest.values.row_count}}",
"right": "0"
}
},
{
"task_key": "transform",
"depends_on": [{"task_key": "check_rows", "outcome": "true"}],
"notebook_task": {"notebook_path": "/Workspace/Shared/transform"}
}
]If ingest produced rows, transform runs. If it produced none, transform is skipped and the run ends without a failure.
Operands and operators
The left and right fields take strings. Each can be a literal or a dynamic value reference, such as {{tasks.ingest.values.row_count}} for a task value or {{job.parameters.run_date}} for a job parameter.
| Operator | Meaning |
|---|---|
| `EQUAL_TO`, `NOT_EQUAL` | Equality test |
| `GREATER_THAN`, `GREATER_THAN_OR_EQUAL` | Larger than |
| `LESS_THAN`, `LESS_THAN_OR_EQUAL` | Smaller than |
EQUAL_TO and NOT_EQUAL compare as numbers when both sides parse as numbers. Otherwise they compare as strings. The other operators work on numbers.
Common errors
- Both branches look skipped. If
ingestfails,check_rowsnever runs, so neither branch runs. A downstream task also needs its upstream tasks to succeed, unless you set arun_ifrule. - A missing outcome. A task that depends on a condition task must name an
outcome. Without it, the dependency is invalid. - Unresolved reference. A typo in the task key or value key leaves the reference unresolved, and the comparison gives a wrong result. Check the task key against the
task_keyfield of the upstream task. - Join after a branch. A task that follows both branches needs a
run_ifrule such asAT_LEAST_ONE_SUCCESS. The default rule,ALL_SUCCESS, would never be met because one branch is always excluded.
Exercise
A job has ingest, then a condition task check_rows, then transform on the true outcome, then publish that depends only on transform. ingest produces zero rows. What happens to transform and publish, and does the run count as failed?
Show answer
The condition evaluates to false, so transform is excluded. publish depends on transform and with the default ALL_SUCCESS rule is also not run. The run is not marked as failed, because no task failed. To run publish or an alert on the false path, add a task that depends on the false outcome, and use a run_if rule if a task must run after either branch.
Exercise
Write the depends_on entry for an alert_empty task that should run only when check_rows evaluates to false.
Show answer
"depends_on": [{"task_key": "check_rows", "outcome": "false"}]