Sub topic 10
Branching with If/else condition tasks
This chapter assumes you can build a multi-task job with depends_on, read task values with dbutils.jobs.taskValues, and know that run_if sets when a task runs based on the status of its upstream tasks. Here you will use an If/else condition task to choose between paths at run time.
What a condition task does
A condition task evaluates one comparison and reports true or false. It runs no notebook or code and needs no cluster, so it finishes almost instantly. Downstream tasks name the outcome they depend on, and Databricks runs only the branch that matches.
The comparison has three parts:
left: the first operand.op: the comparison operator.right: the second operand.
Both operands are strings in the job definition. They can be constants such as "0", or dynamic value references such as {{tasks.ingest.values.row_count}} or {{job.parameters.env}}. Databricks substitutes the references before comparing.
| `op` value | Meaning |
|---|---|
| `EQUAL_TO` | left equals right |
| `NOT_EQUAL` | left differs from right |
| `GREATER_THAN` | left is greater than right |
| `GREATER_THAN_OR_EQUAL` | left is greater than or equal to right |
| `LESS_THAN` | left is less than right |
| `LESS_THAN_OR_EQUAL` | left is less than or equal to right |
When both operands look like numbers, they are compared as numbers. Otherwise they are compared as text. Compare "9" with "10" as numbers and you get the answer you expect. Compare "v9" with "v10" as text and "v9" is greater.
A single guard
The earlier chapter on task values showed ingest publishing a row_count. A condition task can use that value to skip transform when there is nothing to process:
[
{
"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"}
}
]The outcome field on a depends_on entry is what ties a task to a branch. Its value is the string "true" or "false", not a JSON boolean. A task that depends on a condition task without an outcome is an invalid definition, so always set one.
If row_count is 0, the outcome is false. transform has no matching dependency, so Databricks does not run it. The run page shows it as excluded, not failed.
Two branches that rejoin
Most real workflows need an action on each side, and then a final step that runs either way. Here is a job fragment that does a full refresh on Sundays and an incremental load on other days. The day_type parameter is set by the schedule or by the caller:
[
{
"task_key": "is_full_refresh",
"condition_task": {
"op": "EQUAL_TO",
"left": "{{job.parameters.day_type}}",
"right": "sunday"
}
},
{
"task_key": "full_load",
"depends_on": [{"task_key": "is_full_refresh", "outcome": "true"}],
"notebook_task": {"notebook_path": "/Workspace/Shared/full_load"}
},
{
"task_key": "incremental_load",
"depends_on": [{"task_key": "is_full_refresh", "outcome": "false"}],
"notebook_task": {"notebook_path": "/Workspace/Shared/incremental_load"}
},
{
"task_key": "publish",
"depends_on": [
{"task_key": "full_load"},
{"task_key": "incremental_load"}
],
"run_if": "AT_LEAST_ONE_SUCCESS",
"notebook_task": {"notebook_path": "/Workspace/Shared/publish"}
}
]The publish task depends on both branches, but only one branch runs on any given day. With the default ALL_SUCCEEDED rule, publish could not run, because one upstream task never succeeds. AT_LEAST_ONE_SUCCESS fixes this: publish runs when either branch succeeds.
Producing a good condition input
A condition task is only as useful as the value it reads. Two sources are common:
- Job parameters. These are known when the run starts. Use them for switches such as environment or run mode.
- Task values. These are produced during the run. Use them for data-driven decisions such as row counts, a validation flag, or a status string.
For a data-quality gate, have the upstream notebook compute the flag and publish it:
bad_rows = spark.table("sales.raw_orders").filter("amount < 0").count()
dbutils.jobs.taskValues.set(key="bad_rows", value=bad_rows)The condition task then compares {{tasks.validate.values.bad_rows}} with 0 using EQUAL_TO. Do the computing in the notebook and keep the condition simple. A condition task supports one comparison and cannot combine several tests with AND or OR. To express a compound rule, compute it in the notebook and publish a single value, such as "ok" or "fail". You can also chain condition tasks, one behind the other.
Common errors
- Missing
outcome. A task that depends on a condition task must say"true"or"false". Leaving it out is rejected, or does not wire the branch you intended. - Wrong reference path. The format is
{{tasks.<task_key>.values.<key>}}. A typo in the task key or value key means the reference cannot be resolved, and the condition task fails instead of returningfalse. Thecheck_rowsfragment above shows the correct shape. - Missing
depends_onon the condition task. If the condition reads{{tasks.ingest.values.row_count}}, it must depend oningest, directly or through other tasks. Without that dependency, the value may not exist when the condition runs. - Text comparison by accident. If either operand is not numeric, such as
"n/a", the comparison becomes text. Publish numbers as numbers and givetaskValues.geta numericdefault. - Treating excluded as failed. An excluded task is not an error and does not trigger failure notifications. If the notification target should hear about skipped work, add a task on the other branch for that purpose.
- A join task that never runs. This is the default
ALL_SUCCEEDEDproblem from the example above. Setrun_ifon any task that follows both branches.
Condition tasks and repair runs
If a branch task fails, you repair the run as described in the repair chapter. The repair reuses the condition outcome from the original run. The condition task is not re-evaluated unless you select it, so the rerun stays on the same branch.
Exercise
A job has validate, then a condition task gate that checks {{tasks.validate.values.bad_rows}} EQUAL_TO 0. Task load depends on gate with outcome "true", and task quarantine depends on gate with outcome "false". A final task report depends on both load and quarantine. On a day with bad rows, report never runs. Why, and what is the fix?
Show answer
Only quarantine runs on that day, and load is excluded. report uses the default run_if of ALL_SUCCEEDED, which needs every upstream task to succeed, so it does not run. Set "run_if": "AT_LEAST_ONE_SUCCESS" on report so it runs when either branch succeeds.
Exercise
Why does the condition {{tasks.ingest.values.row_count}} GREATERTHAN 0 need `checkrows to list ingest in depends_on`?
Show answer
The task value only exists after ingest has run and published it. The dependency makes check_rows wait for ingest and gives it access to that task's values.