Skip to main content

QUANTILE_UNION

Description​

The QUANTILE_UNION function is used to merge intermediate results from multiple quantile calculations. This function typically works in conjunction with QUANTILE_STATE and is particularly useful in scenarios requiring multi-stage quantile calculations.

Syntax​

QUANTILE_UNION(<query_state>)

Parameters​

ParameterDescription
<query_state>The intermediate state generated by the TO_QUANTILE_STATE function.

Return Value​

Returns an aggregation state that can be used for further quantile calculations. The result of this function remains a QUANTILE_STATE.

Example​

-- Create sample table
CREATE TABLE response_times (
request_id INT,
response_time DOUBLE,
region STRING
) DUPLICATE KEY(request_id)
DISTRIBUTED BY HASH(request_id) BUCKETS AUTO
PROPERTIES (
"replication_allocation" = "tag.location.default: 1"
);

-- Insert sample data
INSERT INTO response_times VALUES
(1, 10.5, 'east'),
(2, 15.2, 'east'),
(3, 20.1, 'west'),
(4, 25.8, 'east'),
(5, 30.3, 'west'),
(6, 35.7, 'east'),
(7, 40.2, 'west'),
(8, 45.9, 'east'),
(9, 50.4, 'west'),
(10, 100.6, 'east');

-- Calculate 50th percentile of response times by region
SELECT
region,
QUANTILE_PERCENT(
QUANTILE_UNION(
TO_QUANTILE_STATE(response_time, 2048)
),
0.5
) AS median_response_time
FROM response_times
GROUP BY region;

+--------+----------------------+
| region | median_response_time |
+--------+----------------------+
| west | 35.25 |
| east | 30.75 |
+--------+----------------------+