メインコンテンツまでスキップ
に最終更新

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) の範囲内である必要があります。

戻り値​

バケット境界、件数、累積件数を含む JSON テキストを VARCHAR として返します。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}]}