最后于 更新
LINEAR_HISTOGRAM
描述
LINEAR_HISTOGRAM 函数用于描述数据分布情况,它使用“等宽”的分桶策略,并按照数据的值大小进行分桶。
语法
LINEAR_HISTOGRAM(<expr>, <interval> [, <offset>])
参数
| 参数 | 说明 |
|---|---|
<expr> | 要统计的表达式,支持 BOOLEAN、TINYINT、SMALLINT、INT、BIGINT、LARGEINT、FLOAT、DOUBLE、DECIMAL。Doris 4.1 系列自 4.1.5 起支持 BOOLEAN 输入,FALSE 和 TRUE 分别按 0 和 1 统计。 |
<interval> | 桶的宽度,DOUBLE 类型,必须大于 0。 |
<offset> | 可选,DOUBLE 类型,默认为 0,范围为 [0, interval)。 |
返回值
返回 VARCHAR 类型的 JSON 文本,包含桶边界、桶内计数和累计计数。忽略 NULL 输入;输入全部为 NULL 或没有输入行时,返回 {"num_buckets":0,"buckets":[]}。
示例
-- setup
create table histogram_test(
a int
) distributed by hash(a) buckets 1
properties ("replication_num"="1");
insert into histogram_test values
(0), (1), (2), (3), (4), (5), (6), (7), (8), (9), (10), (11), (null);
select linear_histogram(a, 2) from histogram_test;
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| linear_histogram(a, 2) |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| {"num_buckets":6,"buckets":[{"lower":0.0,"upper":2.0,"count":2,"acc_count":2},{"lower":2.0,"upper":4.0,"count":2,"acc_count":4},{"lower":4.0,"upper":6.0,"count":2,"acc_count":6},{"lower":6.0,"upper":8.0,"count":2,"acc_count":8},{"lower":8.0,"upper":10.0,"count":2,"acc_count":10},{"lower":10.0,"upper":12.0,"count":2,"acc_count":12}]} |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
select linear_histogram(a, 2, 1) from histogram_test;
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| linear_histogram(a, 2, 1) |
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| {"num_buckets":7,"buckets":[{"lower":-1.0,"upper":1.0,"count":1,"acc_count":1},{"lower":1.0,"upper":3.0,"count":2,"acc_count":3},{"lower":3.0,"upper":5.0,"count":2,"acc_count":5},{"lower":5.0,"upper":7.0,"count":2,"acc_count":7},{"lower":7.0,"upper":9.0,"count":2,"acc_count":9},{"lower":9.0,"upper":11.0,"count":2,"acc_count":11},{"lower":11.0,"upper":13.0,"count":1,"acc_count":12}]} |
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
select linear_histogram(a, 2, 1) from histogram_test where a is null;
+--------------------------------+
| linear_histogram(a, 2, 1) |
+--------------------------------+
| {"num_buckets":0,"buckets":[]} |
字段说明:
num_buckets:桶的数量。buckets:直方图所包含的桶。lower:桶的下界。(包含在内)upper:桶的上界。(不包含在内)count:桶内包含的元素数量。acc_count:前面桶与当前桶元素的累计总量。
BOOLEAN 输入按 0、1 分桶,NULL 不参与计数:
SELECT LINEAR_HISTOGRAM(b, 1.0)
FROM (SELECT TRUE AS b UNION ALL SELECT FALSE UNION ALL SELECT TRUE UNION ALL SELECT NULL) t;
{"num_buckets":2,"buckets":[{"lower":0.0,"upper":1.0,"count":1,"acc_count":1},{"lower":1.0,"upper":2.0,"count":2,"acc_count":3}]}