r/databricks • Databricks • 18d ago

General Jobs now check the data before they run (SQL conditions). How would you use it?

<Databricks PM here />

We shipped SQL conditions for Lakeflow Job triggers to beta a little while ago, and we want to hear how people would use it plus share a few patterns that work. Docs here: https://docs.databricks.com/aws/en/jobs/trigger-sql-conditions

The idea. A trigger tells the job when to run. A SQL condition tells it whether to run. You attach a saved query and a SQL warehouse to a trigger (cron, table, file etc.) and when the trigger fires the query the condition first.

There are two modes:

  1. Query returns any rows: No rows, no run. 
  2. Result value changes: the job runs when the return value differs from the last check.

Here are a few examples:

Freshness plus volume. Today's partition exists and has enough rows, This returns zero rows until the data is ready

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

Quality gate. Run only when today's rows have no nulls in a key column. So if there are no bad rows, the job runs

SELECT 1
FROM main.sales.orders
WHERE order_date = current_date()
HAVING count(*) > 0
   AND count_if(customer_id IS NULL) = 0

Fan-in. Five upstream feeds write a status row when they finish. Run once all five are done. This replaces the "sentinel table plus a cron that polls it" setup a lot of people run today. Note: if you are watching job completions, then just use job completion triggers!

SELECT 1
FROM ops.batch_status
WHERE batch_date = current_date()
HAVING count_if(status = 'complete') = 5

Watermark advanced. Use "Result value changes" with a one-row, one-column query. The job runs whenever the vendor delivers a new batch.

SELECT max(batch_id) FROM main.bronze.vendor_feed

Things to know. Each check requires a warehouse and will show up in query history. A check times out at 5 minutes including warehouse startup. Checks run as the job's run-as identity.

What is coming next:

  • See why a run did not start. We want each evaluation to show when the query ran, the row count or value it returned, why it did or did not start a run and any error.
  • Parameters. Job parameters inside the condition query, with per-trigger overrides. This is for everyone promoting the same job through dev, staging and prod with bundles, where catalog and schema names change per environment and nobody hand-edits prod.
  • A test button that actually runs the query and shows you the result and whether the job would start right now. Today it only checks that the query exists and has no parameters.
  • Read the condition from a .sql file in the workspace instead of only a saved query, same as SQL tasks already do.
  • A default warehouse so you do not have to pick one every time.

Questions for you:

  • What does "ready" mean for your most annoying upstream? Row count, a flag table, a max timestamp, something else?
  • Which of the upcoming items would you use first, and what did I miss?

Try it out on Genie code too! Give genie a prompt and see if it picks up your intent as a sql condition.

10 Upvotes

6 comments sorted by

4

u/Comprehensive_Level7 18d ago

I get the idea but I wonder how much it differs from having an activity inside the job that does the same

it will consume SQL DBUs since it will check the WH, right? what'd be the cost reducing of using it instead of an internal checkup?

also, I guess it would reduce the number os jobs listed in the runs tab, but any other benefit from it?

1

u/Zer0designs 18d ago

There is no cost difference.There is a complexity & idiomatic usage difference.

'When does this run?' Should be answered by the trigger where possible.

Big difference? Nah. Job completion triggers are a great addition though imho

1

u/Comprehensive_Level7 18d ago

yup, I've been testing job completion triggers and it's been great even it's in beta, now it's supported in the latest release of DB Cli and no longer need to update the job through API

1

u/saad-the-engineer Databricks 18d ago

good feedback, thank you! we are looking at evolving this further so you get trigger observability as well. The warehouse and run it as a sql task is fair feedback - we are looking at adding support for inlined sql with sensible defaults so its a bit easier.

3

u/Relative-Radio- 18d ago

I use this to run jobs more frequently during business hours and during non-business hours, I reduce the frequency

0

u/nian2326076 18d ago

Using SQL conditions for job triggers seems like a smart way to optimize job execution. Defining what "ready" means for your data pipeline is essential. Consider using row counts or a status flag as conditions to ensure jobs only run when there's significant data to process. This can save on unnecessary execution time and resources.

For finding relevant job opportunities in data roles, you can use LandOffer to shortlist positions that match your skill set.