Skip to main content

Add SQL conditions to job triggers

Use a SQL condition to check your data before a triggered job run starts. When the trigger fires, Databricks runs a saved SQL query on a SQL warehouse that you choose. The run starts only if the query result meets the condition. For example, run a job only when an upstream table has finished loading or a data quality check passes, without adding a separate checker task to the job.

Beta

This feature is in Beta. To use it, a workspace admin must turn on SQL conditions for triggers from the Previews page. See Manage Databricks previews.

Requirements​

  • You must have CAN MANAGE on the job to configure its triggers.
  • You need a saved query that doesn't use {{ }} query parameters. To use values from the job, write named parameter markers instead. See Use job parameters in a condition. You must have access to the query and be able to run it on the SQL warehouse that you select.
  • The job's Run as identity must be able to open the saved query, have CAN USE on the SQL warehouse, and have read access to every table that the query reads. The condition query runs as this identity.

How SQL conditions work​

The trigger decides when to check. The SQL condition decides whether to start a run. After the trigger fires, Databricks runs the query in read-only mode. A run starts only when both the trigger and the SQL condition are satisfied.

You can add one SQL condition to a scheduled, table update, file arrival, model update, or job completion trigger. Continuous triggers don't support SQL conditions.

Choose how Databricks evaluates the query result:

Condition type

API value

Starts a run when

Query requirements

Query returns rows

QUERY_RETURNS_ROWS

The result contains at least one row.

None. Zero rows means that the condition isn't met.

Result value changes

RESULT_VALUE_CHANGES

The result differs from the value at the previous check.

Must return exactly one row and one column. The value must be 1 MiB or smaller.

Condition type

API value

Starts a run when

Query requirements

Query returns rows

QUERY_RETURNS_ROWS

The result contains at least one row.

None. Zero rows means that the condition isn't met.

Result value changes

RESULT_VALUE_CHANGES

The result differs from the value at the previous check.

Must return exactly one row and one column. The value must be 1 MiB or smaller.

For Result value changes, Databricks runs the query in the background right after you save the trigger and stores the result as a baseline. This check doesn't start a run. Later checks start a run when the result differs from the stored value. Changing the query, SQL warehouse, or condition type on the trigger resets the baseline, and Databricks stores a new one in the same way. Pausing the trigger or changing other trigger settings can also reset it. Editing the text of the saved query or changing a job parameter doesn't reset it.

Each check works as follows:

  • A check, including SQL warehouse startup, times out after 5 minutes. If a check times out, no run starts, and the trigger checks again at its next evaluation.
  • A trigger runs at most one check at a time. If the trigger is due while a check is still running, Databricks skips that evaluation instead of queuing it.
  • If the query has more than one statement, only the result of the last statement is used.
  • SQL conditions apply only to runs that the trigger starts. Run now and other manual runs don't check the SQL condition.
tip

For Query returns rows, make sure that the query returns zero rows when the data isn't ready. A query like SELECT count(*) > 100 FROM main.sales.orders returns one row even when the count is 100 or less, so the condition is met. Filter with WHERE or HAVING instead:

SQL
SELECT 1
FROM main.sales.orders
WHERE order_date = current_date()
HAVING count(*) > 100

Use job parameters in a condition​

A condition query can use the job's parameters. Write a named parameter marker, such as :catalog, in the query. When Databricks checks the condition, each marker gets the value of the job parameter with the same name. For example, give the job a catalog parameter with a different value in each environment, and use the same query everywhere.

The following query reads the sales.orders table in the catalog that the catalog job parameter names:

SQL
SELECT 1
FROM IDENTIFIER(:catalog || '.sales.orders')
WHERE order_date = current_date()
HAVING count(*) > 100

Markers and values work as follows:

  • A marker matches the job parameter with exactly the same name, including case. For a name that isn't a regular identifier, enclose the name in backticks, for example :`env-name` .
  • The query doesn't need to use every job parameter. A check ignores the parameters that the query doesn't use.
  • If a job parameter's value contains a dynamic value reference, Databricks fills it in before the check runs. A check fills in {{job.id}}, {{workspace.id}}, {{job.trigger.type}}, and {{job.trigger.time.<argument>}}. The trigger time is the time of the check, in UTC. Date arithmetic on the trigger time works the same way as in a job run.

A check fails with an error, and no run starts, when the query uses a marker that has no value:

  • The job has no parameter with the marker's name. The error names the marker.
  • The job parameter's value uses a reference that has a value only during a run, such as {{job.run_id}} or {{job.start_time.iso_date}}.
  • The job parameter's value uses another reference that a check can't fill in, such as {{job.trigger.file_arrival.location}} or {{workspace.url}}.

When you select a query in the trigger dialog, the Parameters table lists each marker in the query. Value comes from shows the job parameter that provides the value, and Value at check time shows the value that a check would use now. The table also flags each marker that has no value:

  • A missing job parameter shows Not set with a warning. You can still save the trigger.
  • A reference that a check can't fill in shows Not available with a warning. You can still save the trigger.
  • A reference that has a value only during a run shows Not available with an error. Save and Test trigger are unavailable until you change the job parameter or remove the marker from the query.

To change a value, edit the job parameter. See Add or edit job parameters.

Add a SQL condition to a trigger​

  1. In the workspace sidebar, click Jobs & Pipelines, then open the job.
  2. In Schedules & Triggers, click Add trigger, or edit an existing trigger.
  3. Select the Trigger type and configure the trigger.
  4. Expand Advanced, then click Add SQL condition.
  5. Select the SQL query and the SQL warehouse.
  6. Under Trigger when, select Query returns rows or Result value changes.
  7. Click Save.

Test trigger checks that the job can use the saved query. It confirms that the query exists, isn't in the trash, and has no {{ }} parameters, but it doesn't run the query or fill in job parameter values. To see the query result, run the query in the SQL editor.

To remove a condition, edit the trigger, expand Advanced, and click Remove SQL condition.

Configure with the API, CLI, or bundles​

Set sql_condition with sql_query_id, warehouse_id, and trigger_mode. For a scheduled trigger that uses a cron expression, set it on schedule. For other trigger types, set it on trigger, next to the trigger type settings. With the Multiple Triggers Beta, set it on each entry in triggers.

Save this request as update-job.json and send it to POST /api/2.2/jobs/update in the Jobs API. Replace the IDs with your job, saved query, and SQL warehouse IDs.

JSON
{
"job_id": 9001,
"new_settings": {
"trigger": {
"pause_status": "UNPAUSED",
"table_update": {
"table_names": ["main.sales.orders"],
"condition": "ANY_UPDATED"
},
"sql_condition": {
"sql_query_id": "fe25e731-92f2-4838-9fb2-1ca364320a3d",
"warehouse_id": "5d409943fcd32c6f",
"trigger_mode": "QUERY_RETURNS_ROWS"
}
}
}
}

A sql_condition that you send to jobs/update replaces the current one as a whole, so include all of its fields. If you leave out sql_condition, the current condition stays. To update multiple triggers, see Add multiple triggers to a job.

Monitor SQL condition checks​

  • For scheduled triggers, the trigger status on the job page shows whether the latest SQL condition check was satisfied. Click Query execution details to open the statement in query history.
  • To read the latest check that ran without an error, call the Get job API with include_trigger_state=true and read trigger_state.sql_condition. The latest_condition_evaluation_satisfied field shows whether that check met the condition, and latest_condition_evaluation_sql_statement_id identifies its statement in query history. A failed check doesn't change these fields. With Multiple Triggers, read trigger_details[].state.sql_condition.
  • For each run that a SQL condition started, the Launched field in the runs list and on the run details page shows a SQL tag. Click it, then click Query execution details to open that run's check in query history.
  • If a check fails for a reason that you can fix, the error appears in the trigger status on the job page. Databricks also sends the error to the job's failure notifications, including email recipients and webhook destinations. You get one notification when a working condition starts to fail, not one for every failed check. The error stays until the next successful check. See Add notifications on a job.

Each check runs as a SQL statement on your SQL warehouse. To review checks over time, query the query history system table for the warehouse.

Troubleshooting​

Issue

Action

The job runs every time the trigger fires

For Query returns rows, change the query so that it returns zero rows when the data isn't ready.

Result value changes never starts a run

The baseline is the query result at the time you saved the trigger. A run starts only after the query result changes from that value.

The check times out

Make the query faster, or use a serverless SQL warehouse, which starts faster.

A check fails because the job has no parameter with the marker's name

Add a job parameter with the same name as the marker, or remove the marker from the query. Names are case-sensitive.

A check fails because a job parameter has no value before a run starts

The job parameter uses a reference that exists only during a run, such as {{job.run_id}}. Change the job parameter's value, or remove the marker from the query.

Add SQL condition doesn't appear

Expand Advanced. If the button is still missing, ask a workspace admin to enable SQL conditions for triggers.

Issue

Action

The job runs every time the trigger fires

For Query returns rows, change the query so that it returns zero rows when the data isn't ready.

Result value changes never starts a run

The baseline is the query result at the time you saved the trigger. A run starts only after the query result changes from that value.

The check times out

Make the query faster, or use a serverless SQL warehouse, which starts faster.

A check fails because the job has no parameter with the marker's name

Add a job parameter with the same name as the marker, or remove the marker from the query. Names are case-sensitive.

A check fails because a job parameter has no value before a run starts

The job parameter uses a reference that exists only during a run, such as {{job.run_id}}. Change the job parameter's value, or remove the marker from the query.

Add SQL condition doesn't appear

Expand Advanced. If the button is still missing, ask a workspace admin to enable SQL conditions for triggers.

Limitations​

  • Each trigger supports one SQL condition.
  • Continuous triggers don't support SQL conditions.
  • Condition queries run only on SQL warehouses, in read-only mode.
  • A check, including SQL warehouse startup, times out after 5 minutes.
  • A trigger keeps only its latest check, not a full history of every check. To see earlier checks, use the SQL tag on runs that the condition started, or query history.
  • Checks run on your SQL warehouse and are billed as SQL warehouse usage. A check can start a stopped warehouse.
  • Triggers with SQL conditions count toward the workspace limit of 12,000 active triggers.
  • Job parameter values reach the query as text. To compare a number or a date, cast the value in the query, for example CAST(:min_rows AS INT).
  • A check can't fill in {{workspace.url}}. If the query uses a job parameter whose value contains it, the check fails.
  • Test trigger doesn't run the query or fill in job parameter values.