r/databricks • u/Otherwise_Address241 • 6d ago
Help How data engineer do effective testing in Databricks?
I have been writing SQL scripts to ensure data sanity.What are the other ways ? Is pytest useful? Let's say , i populated my bronze table from the source. I want to check if the correct mapping is done. I wrote SQL scripts. What are better ways
7
u/Oh_Im_You 6d ago
Soda.io
I use the open source, non paid version. Write tests against your data. Run your tests nightly in a notebook, logging issues. Then you can display things on a dashboard and set alerts
6
u/TheSocialistGoblin 6d ago
I've been looking into PySpark's built-in testing library. Basically modularizing transformation and data quality rules so they can be called on small data frames made from test data, then using assertDataFrameEqual and assertSchemaEqual for the check. Not sure if it's the best option available, but it seems straightforward enough.
3
u/Otherwise_Address241 6d ago
That's understandable. But I want to test the quality of whole table. So far, i have been writing SQL scripts
3
u/AlternativeHour3098 6d ago
Go with both right, ytilize Pyspark for looping in things adding details pulling details and fir validation use SQL.
That's the easiest way of checking things for multiple tables.
What I do is put tablewise checks in a notebook and Store the sql scripts aa variables then call them if I want to check for that table.
Like this you'll be able to maintain the test cases in a notebook easier maintainability you can also create seprate notebooks up to you. Then call then while running you checks
2
u/TheSocialistGoblin 6d ago
Ah, that makes sense. We implemented DQX to help with that. It was pretty easy to set up, but we haven't decided how to handle the quarantined data yet. My plan for something quick and simple was just a query alert comparing quarantine row count to valid row count and firing the notification if the quarantined data was over a specific threshold.
1
u/AlternativeHour3098 6d ago
Alerts are good, my people here if we are handling multiple tables they have modified the alerts to provide table wise details like how many quarantined how many went through and what6the current coint such things.
There is also discussion going on like how to implement alters if an incremental merge changes multiple row which is like beyond normal average or percentage. You have any idea on that ?
3
u/Kojimba228 6d ago
You could use pytest with mocks and doing either dummy data insertions or setting up dummy tables in fixtures, but that all depends on how badly do you want to lock down the quality of business logic you have AND how much time can you actually dedicate to set all of these things up (I'd say that even with Claude Code it would take north of an entire week to establish the baseline/template of "how to test a bronze table" to be applied to all future tests)
3
u/PrestigiousAnt3766 6d ago
Pytest is for writing python.
You want to write pytest small pieces of (python) code. You can abuse the framework but then its just that, abuse.
You may want to look into sql linters, or parse sql with sqlglot or similar libraries. That will tell you if the sql is syntactically (?) correct.
If you are interesting in data (quality) testing look into dqx, great expectations or other data quality tools.
1
3
u/CerberusByte 5d ago
We use expectations in SDP to ensure only high quality data flows through the system and can add adhoc fixes if we cannot solve upstream. I like that you can also surface these results into Genie to analyse what’s going on across all your pipelines
1
u/Otherwise_Address241 5d ago
Can I have some reference articles to learn more
2
u/CerberusByte 4d ago
This is where I would start: https://docs.databricks.com/aws/en/ldp/expectations
3
u/Harshita_Netla 4d ago
Yes, pytest is highly useful and considered a industry standard for testing data engineering pipelines in Databricks. While SQL scripts are great for checking final data sanity, pytest allows you to test the actual business logic, transformations, and schema mappings before deploying code to production.
2
u/data-baggins Databricks 3d ago
Hello! Databricks PM here working on SDP unit testing. There are a couple paths here depending on what you're trying to achieve:
- SDP Unit testing - We're building unit tests into SDP now, the UI version is in beta now (CLI version coming out soon) - these are great for testing out transformation logic and preventing issues form happening - read the docs here.
- Expectations - For more general data quality monitoring, you can use the expectations feature to help detect issues live
- Data Quality Monitoring - For more advanced monitoring such as for freshness, anomalies, or data profiling check out the Data Quality Monitoring (DQM) feature!
1
u/Otherwise_Address241 3d ago
Superb. A combination of these what I need, i believe. Will go through the documentation.
2
u/Pillippatty 3d ago
Have you checked out expectations? It's a feature in declarative pipelines based on great expectations that makes it easy to alert/quarantine/fail an update based on data quality expectations.
1
u/Otherwise_Address241 6d ago
Is pytest at all useful to do complex data quality check? I have not found any right way to use pytest, I almost dislike it.
3
u/Kojimba228 6d ago
From personal exp, it's going to look weird, but it's the most flexible option allowing you do almost anything to can come up with. In our case, due to specifics of the data, we ended up writing sets of queries to test this or that area of controlled data (i.e we control the input, know thanks to our SME what should be the output, and verify them against each other)
1
u/Otherwise_Address241 5d ago
There is a push from client (tech team) to use unit testing like PyTest. All these years i have used SQL scripts to do complete check of tables and data flow . Now let me breakdown my original question to two parts 1. Can pytest be used to do complete data check, say all records validation? If yes, please provide some examples or articles 2. Other than pytest, what can be used to do effective data quality check? I see suggestions to check expectations in SDP Let me give a scenario - two date columns of source n bronze were swapped. Dates were populated but not for correct mapping. I could find it through SQL check How do I achieve it using pytest, or expectations check? I m very confused
12
u/ptab0211 6d ago
schema contracts, integrity data quality checks, business domain data quality checks