- Tumbling windows: Use template functions and return both
validio_metricandvalidio_window_id - Global windows: Use regular SQL syntax and return
validio_metric
Mandatory fields
Your SQL query must return specific fields:validio_metric(Required for all SQL validators) - The field containing the validation metricvalidio_window_id(Required for tumbling windows only) - Maps metrics for each tumbling window to their respective time ranges
Segmentation fields
The SQL query must return columns with names that have the data type STRING and match the segmentation fields configured for the validator. For example, if segmentation oncountry is used, then the SQL query should return a column called country. If necessary, you can use casting to ensure the data type. For multi-field segmentations, each field is displayed as a separate column.
Aliases for the returned segmentation columns must follow the following naming conventions:
Template functions
Write your custom SQL Query to use placeholders and template functions that reference Source, Segmentations, and Windows to help integrate your custom SQL logic with Validio tumbling windows.Not the same as debug query placeholdersThe template functions below define how a validator computes its metric. Debug queries use a separate set of placeholders, such as
validio_incident_window_start, to bind a query to the incident being investigated. The one name common to both sets is validio_segmentation_filter, which means the same thing in either. See Debug Query Syntax.Window ID
Use the required functionvalidio_window_id(<identifier>) to generate an ID for all data points that belong to the same window. The function must be placed in the SELECT clause.
The function expands to a formula that calculates which window the datetime in <identifier> belongs to. Then it counts the number of windows the calculated window is from a reference timestamp to get a monotonically increasing numerical ID for each window in the query. (The reference timestamp is often the timestamp for the oldest data point for the current poll.)
This column must be included in the GROUP BY clause to aggregate over all data points in the same window. Because it is used to link metrics to the correct window time ranges, the column must be returned as the name validio_window_id (matching the case).
Window filter
Use the required functionvalidio_window_filter(<identifier>) to process data that has not already been processed. The <identifier> should be the timestamp column, which is usually the cursor column.
The function expands to a filter condition that limits the query to the active time range. For example, during a backfill poll the function will expand to a time range that covers the lookback period. During a normal poll the function will span back to where the last poll completed, which will be the time range for the latest window (or windows).
SQL
Reference window filter
Use the optional functionvalidio_reference_window_filter(<identifier>, <lookback>) when doing reference window validation, where data in the current window needs to be compared to data in previous windows. This function is similar to the Window Filter function, but takes a second argument that can be used to extend the time range with a <lookback> amount of windows.
The function expands to the following filter condition:
SQL
Segmentation filter
Use the functionvalidio_segmentation_filter() in place of a segmentation filter expression specified on the segmentation attached to the SQL validator. The function automatically expands to the filter expression before polling for new data on the validator.
You can also use the segmentation filter placeholder in a query when a segmentation filter is not used.
Source table
Use the functionvalidio_source_table() in place of manually writing the source table identifier.
This convenience function should be used in the FROM clause and it will expand to the quoted identifier for the table. For example, a BigQuery source expands into:
SQL
SQL Validator and SQL SourcesIf the SQL validator is used on a SQL source then the
validio_source_table() function must be used as the table identifier since the SQL source query definition will be added as a CTE to the validator query during runtime. In this case the function expands to the name of the CTE.For more information, see Query Syntax in About Custom SQL Sources.Custom SQL query example
This example demonstrates how to use template functions to build a custom SQL query. The validator is configured with a daily tumbling window and segmentation oncountry.
The following custom SQL query uses template functions:
SQL
SQL
1 The
window_start and window_end columns are shown for illustrative purposes and are not part of the SQL validator query.
For more examples of validator configurations, refer to Custom SQL Validator Recipes.