ai_top_drivers function
Applies to: Databricks SQL
Databricks Runtime
This feature is in Beta. Workspace admins can control access to this feature from the Previews page. See Manage Databricks previews.
ai_top_drivers() is a table-valued function for contribution analysis. It ranks dimension values, or pairs of values, that contribute the most to a change in a metric between a control group and a test group. See Arguments for available arguments.
Requirements
- Databricks Runtime 16.1 or above
- Pro or Serverless SQL warehouse with Photon enabled, or a Photon-enabled cluster on Databricks Runtime
- Enroll your workspace in the AI Top Drivers preview. See Manage Databricks previews.
Syntax
ai_top_drivers(input => input, metric => metric, is_test => is_test
[, optional_param ] [...])
optional_param:
{ dimensions => dimensions |
aggregation => aggregation |
min_support => min_support }
Arguments
Pass all arguments by name.
inputis the table-valued input to analyze. Provide a table or subquery with the metric column, dimension columns, and the test label column.metricis aSTRINGnaming the numeric column to analyze. The column must have a numeric type.is_testis aSTRINGnaming a boolean column that labels each row.TRUEmarks the test split andFALSEmarks the control split.dimensions(optional) is anARRAY<STRING>of column names to segment on. Low-cardinality numeric columns are segmented by exact value. High-cardinality numeric columns are automatically grouped intolow,medium, andhighranges. If you omit this argument, the function auto-selects the top 20 correlated dimension columns from the input.aggregation(optional) is aSTRINGnaming the aggregate to apply tometricwithin each segment. Supported values aresum,avg,count,min, andmax. The default issum.min_support(optional) is aDOUBLEbetween 0 and 1 representing the minimum fraction of analyzable rows a segment must cover to appear in the results. The default is0.05.
Returns
The function returns a table with the following columns:
contributor: AnARRAY<STRING>that describes the segment. Each element uses the formcolumn=value, for example["pickup_zip=high (>10044)"]or["pickup_zip=high (>10044)", "payment_type=CRD"].metric_control: The aggregated metric for the segment in the control group (is_test=FALSE).metric_test: The aggregated metric for the segment in the test group (is_test=TRUE).change: The absolute difference,metric_test−metric_control.relative_change: The change as a fraction ofmetric_control.support: The fraction of total analyzable rows in the segment.
Results include single-dimension contributors, pairs of dimension values, and an ["all"] row that summarizes the full population.
Examples
The following example compares total fare revenue between January and February 2016 and finds the pickup ZIP code ranges that contribute most to the change:
WITH input AS (
SELECT
pickup_zip,
fare_amount,
CASE
WHEN DATE(tpep_pickup_datetime) BETWEEN '2016-01-01' AND '2016-01-31' THEN false
WHEN DATE(tpep_pickup_datetime) BETWEEN '2016-02-01' AND '2016-02-29' THEN true
ELSE NULL
END AS is_test
FROM samples.nyctaxi.trips
WHERE DATE(tpep_pickup_datetime) BETWEEN '2016-01-01' AND '2016-02-29'
)
SELECT *
FROM ai_top_drivers(
input => TABLE(input),
metric => 'fare_amount',
is_test => 'is_test',
dimensions => ARRAY('pickup_zip'),
aggregation => 'sum'
)
ORDER BY ABS(change) DESC
The following example counts trips across dropoff ZIP code ranges between the same periods:
WITH input AS (
SELECT
dropoff_zip,
1 AS trip_count,
CASE
WHEN DATE(tpep_pickup_datetime) BETWEEN '2016-01-01' AND '2016-01-31' THEN false
WHEN DATE(tpep_pickup_datetime) BETWEEN '2016-02-01' AND '2016-02-29' THEN true
ELSE NULL
END AS is_test
FROM samples.nyctaxi.trips
WHERE DATE(tpep_pickup_datetime) BETWEEN '2016-01-01' AND '2016-02-29'
)
SELECT *
FROM ai_top_drivers(
input => TABLE(input),
metric => 'trip_count',
is_test => 'is_test',
dimensions => ARRAY('dropoff_zip'),
aggregation => 'count'
)
ORDER BY ABS(change) DESC
For count aggregation, provide a numeric column to count. A constant column such as 1 AS trip_count is a common pattern.
Limitations
The following limitations apply during Beta:
- Rows with a
NULLvalue in themetricoris_testcolumn are dropped before analysis. count distinctandmedianaggregates are not supported.MAPandSTRUCTdimension columns are not supported. Numeric identifiers, such as ZIP codes and IDs, are treated as quantities and may be grouped into ranges. Cast numeric identifiers toSTRINGto segment by exact value.