backgroundImage

Expectations: now with custom SQL and dynamic parameters

Expectations get even more expressive with two exciting new features

Erin Kapp
November 07, 2024
screenshot of the custom SQL Expectations and dynamic Expectation parameters, labeled as such, on an abstract background

Expectations were already the most expressive way to build your data tests, but they just got even better. Here are two major upgrades to your Expectation experience:

  • Dynamically-defined test parameters

  • A custom SQL option

Dynamic parameters: define tests relative to historical results

Lots of GX Cloud users have asked for the ability to set their Expectation parameters based on historical results. We’re pleased to announce that this is now available!

How it works

When you create a statistics- or count-based Expectation, you now have the option to define its parameters dynamically.

Expectation dynamic and fixed value options

Get the complete list of which Expectations support dynamic parameters.

Currently, the dynamic parameter of a count (or statistic) Expectation is defined relative to the mean of that count (or statistic) from the column’s previous runs. The number of prior runs included is defined by you.

Here are a couple of examples.

Example #1

Table row count should be within 5% of the average row count over the last 7 runs. 

Or, expressed another way: the table row count should be greater than or equal to 95%, and less than or equal to 105%, of the average of the last 7 row counts.

dynamic Expectation parameter 1

Example #2

Column sum should be at least equal to and no more than 110% of the previous run’s column sum.

Or: the column sum should be greater than or equal to and less than or equal to 110% of the previous column sum.

dynamic Expectation parameter example 2

As with the fixed range parameters, excluding your range’s boundaries from the passing values— in essence, using ‘less than’ and ‘greater than’ instead of ‘less than or equal to’ and ‘greater than or equal to’—is as simple as selecting the “Strict” checkbox!

Find out all the details about how dynamic parameters work in GX Cloud in our documentation.

Custom SQL Expectations

The Expectations in GX Cloud can do a lot, but we heard your requests for even more customizability—especially for cross-column and cross-table testing.

That’s why you can now specify a custom Expectation using SQL! 

How it works

In a custom SQL Expectation, you provide a SQL query that describes problematic rows. A sample query simplifies your query construction process.

custom SQL Expectation example 1

The

{batch}
query represents however you defined your Data Asset, whether that’s as a complete Asset (always validating all rows) or using batches (validating rows based on a datetime parameter). GX Cloud will automatically replace
{batch}
with the appropriate SQL structure for your Data Asset’s definition. 

Using

{batch}
is recommended. However, you can also define your query directly if you want. One scenario in which you might want to do this is defining an Expectation to validate the complete Asset in a Data Asset that otherwise uses batches.

custom SQL Expectation example 2

If a custom SQL query returns any rows, the Expectation fails.

Read the custom SQL Expectation docs.


To try out dynamic parameters and custom SQL Expectations, log into your GX Cloud account! If you don't already have one, signing up for GX Cloud is free, and it's quick and easy to get started.

Search our blog for the latest on data quality.


©2024 Great Expectations. All Rights Reserved.