Profiling configuration: SQL templates

Prev Next

A SQL data source template tells Validatar how to calculate each profile on that platform. A Query profile is three SQL fragments (an aggregate formula, a FROM clause and an optional grouping key) with placeholders such as {{schema.name}}, {{table.name}} and {{column.name}}. Validatar fills in the placeholders for each table or column and combines many profiles into one query.

This page covers Database category templates. For Python templates, see Profiling configuration: Python templates. Every placeholder in one place: Ingestion script contract reference.

Profile definitions and template profiles

Two things work together:

  • The profile definition is global. It sets the name, reference key, level (table or column), result format and any data type restriction. You create and edit definitions on the Data Profiles settings page: see Data profiles.
  • The template profile is how one template calculates that definition: the formula type, the SQL or expression, and the profile condition. Each template can calculate the same definition differently.

Open the template and go to the Profiling tab to see every definition and how this template implements it. Click one to edit it.

Profiling tab of a SQL template

Template profile detail for Record Count

The detail page shows the definition's Profiled Object Type and Result Format as read-only, and lets you set:

Field What it does
Formula Type Query, .NET Calculation or Python Calculation.
Enabled for data source template Clear it to stop offering this profile on data sources that use the template.
Grouping Key, Aggregate Formula, FROM (including optional WHERE) The SQL for a Query profile.
.NET Calculation Expression or Python Calculation Expression The expression for a calculation profile.
Profile Condition An optional expression that decides, per table or column, whether the profile runs.

The References panel beside the fields lists every placeholder you can use. Drag one into a field or click it to copy it.

Levels and result formats

A table profile runs once per table. A column profile runs once per column, and only on columns whose internal data type is allowed by the definition's data type restriction (see Data type mappings).

Result format Holds Example
Numeric Value One number record_count
String Value One piece of text most_common_value
Date/Time Value One date or timestamp max_date
Percent Value A fraction: 0.125 displays as 12.5% null_percent
Numeric Array A count for each numeric group distribution_numeric, length_dist
String Array A count for each text group top_ten_values, year_distribution
Date/Time Array A count for each date group No standard profile uses it

Formula types

Query

The template profile supplies three fragments:

Field Contains Example from the built-in SQL Server template
Aggregate Formula One aggregate expression, without an alias count( distinct [{{column.name}}] )
FROM (including optional WHERE) The from keyword, the source and any filter from [{{schema.name}}].[{{table.name}}] with (readuncommitted)
Grouping Key For array formats only: the expression to group by, without group by year( cast([{{column.name}}] as datetimeoffset) )

Validatar assembles them like this, one aggregate per table or column in the batch:

select
<aggregate formula> AS CONTEXT_1,
<aggregate formula> AS CONTEXT_2,
<grouping key> AS GROUPING_KEY
<FROM text>
 group by <grouping key>

Profiles that share the same FROM text, grouping key, condition and formula type are batched into one query, up to the profile set's limit on profiles per query. For distinct_count on three columns of Sales.Orders in the Wholesale Distribution System, the query is:

select
count( distinct [CustomerID] ) AS CONTEXT_1,
count( distinct [OrderDate] ) AS CONTEXT_2,
count( distinct [OrderID] ) AS CONTEXT_3
from [Sales].[Orders] with (readuncommitted)

For an array profile, each row of the result is one group: the grouping key becomes the group label and the aggregate becomes its count. The built-in year_distribution uses count(*) as the aggregate and the year of the column as the grouping key, so it returns one row per year.

Placeholders insert names exactly as cataloged, with no quoting. Quote them the way your platform needs: the built-in templates use [ ] on SQL Server, double quotes on Snowflake and PostgreSQL, and backticks on Databricks.

.NET Calculation

Calculates a value from other profile values, with no query against the data source. The built-in distinct_percent is:

{{table.prf.record_count}} == 0 ? 0 : {{column.prf.distinct_count}} / {{table.prf.record_count}}

The result is a fraction, which the Percent Value format displays as a percentage. {{table.prf.<key>}} reads a table profile and {{column.prf.<key>}} reads a column profile.

Python Calculation

The same idea with a Python expression. The editor offers it only when a Python runtime is installed for Validatar, and not on data sources processed by a Databricks engine. None of the built-in profiles use it. Array formats can only use Query.

Prerequisites

When a formula or condition reads another profile's value through a prf placeholder, Validatar makes that profile a prerequisite when you save, and runs it first. distinct_percent references record_count and distinct_count, so both run before it. A reference that loops back to the profile itself is rejected.

The prerequisite has to be enabled on the template too. If a profile set includes distinct_percent but the template has distinct_count disabled, the profile set fails with One of the prerequisite profiles is not enabled for the related Data Source Template.

Profile conditions

A profile condition is evaluated for each table or column before the profile runs. When it's false, that object is left out. Conditions from the built-in templates:

Profile Condition Effect
null_count, null_percent {{column.is_nullable}} Skips columns declared NOT NULL.
distribution_string {{column.prf.distinct_percent}} != 1 and {{column.prf.distinct_count}} > 0 and {{column.prf.distinct_count}} <= 100 Builds a distribution only for columns with 1 to 100 distinct values that aren't all unique.
length_dist {{column.prf.max_length}} > 0 Skips columns where every value is empty.
min_numeric (MySQL template) {{column.mapped_type}} != "Boolean/Logical" Skips boolean columns.

A condition that reads a profile value makes that profile a prerequisite, the same as a formula does.

Standard profile reference keys

The built-in SQL Server, Snowflake and PostgreSQL templates implement the 40 standard data profiles plus column_count and sample. Use these reference keys in conditions, in Python profile scripts and in tests that read profile results.

Reference key Name Level Format Formula type
record_count Record Count Table Numeric Value Query
total_data_mb Total Data MB Table Numeric Value Query
column_count Column Count Table Numeric Value Query
distinct_count Distinct Count Column Numeric Value Query
distinct_percent Distinct Percent Column Percent Value .NET Calculation
null_count Null Count Column Numeric Value Query
null_percent Null Percent Column Percent Value .NET Calculation
blank_count Blank Count Column Numeric Value Query
blank_percent Blank Percent Column Percent Value .NET Calculation
numeric_count Numeric Count Column Numeric Value Query
numeric_percent Numeric Percent Column Percent Value .NET Calculation
zero_count Zero Count Column Numeric Value Query
zero_percent Zero Percent Column Percent Value .NET Calculation
negative_count Negative Count Column Numeric Value Query
negative_percent Negative Percent Column Percent Value .NET Calculation
min_numeric Minimum (Numeric) Column Numeric Value Query
max_numeric Maximum (Numeric) Column Numeric Value Query
mean_numeric Mean (Numeric) Column Numeric Value Query
median_numeric Median (Numeric) Column Numeric Value Query
lower_quartile Lower Quartile Column Numeric Value Query
upper_quartile Upper Quartile Column Numeric Value Query
standard_deviation Standard Deviation Column Numeric Value Query
min_string Minimum (String) Column String Value Query
max_string Maximum (String) Column String Value Query
min_length Min Length Column Numeric Value Query
max_length Max Length Column Numeric Value Query
mean_length Mean Length Column Numeric Value Query
longest_value Longest Value Column String Value Query
shortest_value Shortest Value Column String Value Query
min_date Minimum (Date) Column Date/Time Value Query
max_date Maximum (Date) Column Date/Time Value Query
most_common_value Most Common Value Column String Value Query
most_common_count Most Common Count Column Numeric Value Query
top_ten_values Top 10 Values Column String Array Query
bottom_ten_values Bottom 10 Values Column String Array Query
distribution_string Distribution (String) Column String Array Query
distribution_numeric Distribution (Numeric) Column Numeric Array Query
binned_numeric Binned (Numeric) Column Numeric Array Query
length_dist Length Distribution Column Numeric Array Query
year_distribution Year Distribution Column String Array Query
year_month_distribution Year Month Distribution Column String Array Query
sample Sample Column String Array Query

How template profiles reach data sources

The template decides how each profile is calculated. Which profiles run, on which tables and columns, and how often, is set on each data source in its profile sets. A profile set selects profile definitions, filters the objects to profile, and sets a schedule, concurrency and how long results are kept. When it runs, it uses the SQL on the template.

Tip: A profile you add to a template is available to the profile sets of every data source that uses the template, but it doesn't run until someone adds it to a profile set.

Best practices

  • Start from the built-in profiles. Copy the SQL from the built-in template for the closest platform and adjust the syntax.
  • Put the source and filter in FROM. The FROM field takes a where clause, a join or a subquery, so the aggregate can stay a single expression.
  • Read catalog statistics where they're exact. The built-in SQL Server record_count reads row counts from sys.partitions and Snowflake's reads information_schema.tables, so neither scans the table.
  • Use conditions to skip expensive work. Distributions and top 10 lists scan whole columns. Gate them on distinct_count or record_count the way the built-in templates do.
  • Test the SQL on the platform first. Functions such as PERCENTILE_CONT and STDDEV_POP vary by platform and version.