おくみん公式ブログ

おくみん公式ブログ

Apache Hive 4: 新しく追加されたUDFの紹介

Tuple Sketch UDFs

先日ついにApache Hive 4.0.0がリリースされました。おおよそ6年ぶりのメジャーアップデートということもあり、5000コミット以上もの変更をともなう大型リリースとなっています。

この記事ではHive 4にて追加されるUDFを紹介します。

Hive 4関連記事一覧

新UDF一覧

文字列操作系

HIVE-685: quote

シングルクォーテーションをエスケープしてくれます。用途不明。

SELECT str, quote(str) FROM test_quote;

+-------------------+----------------------+
|        str        |         _c1          |
+-------------------+----------------------+
| Don't say "lazy"  | 'Don\'t say "lazy"'  |
+-------------------+----------------------+

[HIVE-685] add UDFquote - ASF JIRA

HIVE-18545: json_read

第一引数で受け取ったJSON文字列を第二引数で指定した型に一撃マッピングできます。これがあると楽なケースはかなり多いのではないでしょうか。

WITH parsed AS (
  SELECT json_read('[{"user_id": 1, "item_ids": [1, 5]}, {"user_id": 2, "item_ids": [2, 8]}]', 'array<struct<user_id:int,item_ids:array<int>>>') AS users
)
SELECT
  users[0].user_id AS user_id_0, users[0].item_ids AS item_ids_0,
  users[1].user_id AS user_id_0, users[1].item_ids AS item_ids_0,
  users
FROM parsed;

+------------+-------------+--------------+---------------+----------------------------------------------------+
| user_id_0  | item_ids_0  | user_id_0_1  | item_ids_0_1  |                       users                        |
+------------+-------------+--------------+---------------+----------------------------------------------------+
| 1          | [1,5]       | 2            | [2,8]         | [{"user_id":1,"item_ids":[1,5]},{"user_id":2,"item_ids":[2,8]}] |
+------------+-------------+--------------+---------------+----------------------------------------------------+

[HIVE-18545] Add UDF to parse complex types from json - ASF JIRA

算術系

HIVE-26636: sinh, cosh, tanh

双曲線関数関数の実装。

[HIVE-26636] Hyperbolic functions - ASF JIRA

時刻系

HIVE-20768: tumbling_window

時刻をいい感じに丸めてくれます。

SELECT tumbling_window(cast('2024-05-12 15:23:59' as timestamp), interval '2' MINUTES);

+------------------------+
|          _c0           |
+------------------------+
| 2024-05-12 15:22:00.0  |
+------------------------+

[HIVE-20768] Adding Tumbling Window UDF - ASF JIRA

HIVE-21576: cast + format

フォーマットを指定しつつdate/timestamp型と文字列型の変換を行います。

SELECT
  cast(cast('2024-05-12 15:23:59' as timestamp) as string format 'yyyy-MM-dd'),
  cast('2024/05/12 15:23:59' as timestamp format 'yyyy/MM/dd HH24:MI:ss');

+-------------+------------------------+
|     _c0     |          _c1           |
+-------------+------------------------+
| 2024-05-12  | 2024-05-12 15:23:59.0  |
+-------------+------------------------+

[HIVE-21576] Introduce CAST...FORMAT and limited list of SQL:2016 datetime formats - ASF JIRA

配列系

配列を参照・操作するための便利UDFがたくさん追加されました。これらのUDFは筆者もレビュアーとして参加しているので割と思い入れがあります。

  • array_min
  • array_max
  • array_distinct
  • array_join
  • array_slice
  • array_except
  • array_intersect
  • array_union
  • array_remove

[HIVE-26729] Add new UDFs to process Array type of data - ASF JIRA

統計系

HIVE-16255: percentile_cont, percentile_disc

percentile UDFの派生。

SELECT
  percentile_cont(ws_sales_price, 0.5),
  percentile_disc(ws_sales_price, 0.5)
FROM web_sales;

[HIVE-16255] Support percentile_cont / percentile_disc - ASF JIRA

HIVE-20490: approx_distinct

精度が100%ではなくなるものの、高速にCOUNT + DISTINCTを実行できます。アルゴリズムはHyperLogLog。

SELECT count(distinct ws_item_sk), approx_distinct(ws_item_sk) FROM web_sales;

+-------+-------+
|  _c0  |  _c1  |
+-------+-------+
| 1961  | 1957  |
+-------+-------+

[HIVE-20490] UDAF: Add an 'approx_distinct' to Hive - ASF JIRA

HIVE-22939: データスケッチUDF

個人的期待度No.1。Apache DataSketchesを用いたスケッチUDF群が大量に追加されています。スケッチというのはデータの精度と時間・空間計算量のトレードオフを取るためのテクニックです。Theta/tuple sketchは分析の可能性を大幅に拡張してくれる可能性があると期待しています。数が多いので現実的に使いそうなHLL、Theta、Tuple系のみ列挙します。他のデータ構造にも興味があれば、SHOW FUNCTIONS を実行して ds_ プリフィクスのついたUDFを確認してみてください。

  • HyperLogLog
    • ds_hll_estimate
    • ds_hll_estimate_bounds
    • ds_hll_sketch
    • ds_hll_stringify
    • ds_hll_union
    • ds_hll_union_f
  • Theta Sketch
    • ds_theta_estimate
    • ds_theta_exclude
    • ds_theta_intersect
    • ds_theta_intersect_f
    • ds_theta_sketch
    • ds_theta_union
    • ds_theta_union_f
  • Tuple Sketch
    • ds_tuple_arrayofdouble_estimate
    • ds_tuple_arrayofdouble_estimate_bounds
    • ds_tuple_arrayofdouble_means
    • ds_tuple_arrayofdouble_n_retained
    • ds_tuple_arrayofdouble_quantiles_sketch
    • ds_tuple_arrayofdouble_sketch
    • ds_tuple_arrayofdouble_ttest
    • ds_tuple_arrayofdouble_union
    • ds_tuple_arrayofdouble_values
    • ds_tuple_arrayofdouble_variances
    • ds_tuple_doublesummary_estimate
    • ds_tuple_doublesummary_percentile
    • ds_tuple_doublesummary_sketch
    • ds_tuple_doublesummary_union

[HIVE-22939] Datasketches support - ASF JIRA

GeoSpatial系

地理情報を扱うUDF群。数が多すぎるのでGitHubへのリンクを張っておきます

[HIVE-26222] Native GeoSpatial Support in Hive - ASF JIRA

その他

HIVE-15976: current_catalog, current_schema

USEしているカタログやスキーマを参照できます。current_schemacurrent_database のエイリアスです。

SELECT current_catalog(), current_schema(), current_database();

+-------+----------+----------+
|  _c0  |   _c1    |   _c2    |
+-------+----------+----------+
| hive  | default  | default  |
+-------+----------+----------+

[HIVE-15976] Support CURRENT_CATALOG and CURRENT_SCHEMA - ASF JIRA

HIVE-21270: get_sql_schema

指定したSQLが生成するresult setのスキーマを取得できます。

SELECT get_sql_schema('select ws_item_sk, max(ws_quantity) as max_quantity from web_sales group by ws_item_sk');

+---------------+-----------+
|   col_name    | col_type  |
+---------------+-----------+
| ws_item_sk    | bigint    |
| max_quantity  | int       |
+---------------+-----------+

[HIVE-21270] A UDTF to show schema (column names and types) of given query - ASF JIRA

HIVE-27563: typeof

指定したexpressionの型名を取得。

SELECT typeof('text'), typeof(1), typeof(CAST(NULL AS bigint)), typeof(array(1.0));

+---------+------+---------+----------------------+
|   _c0   | _c1  |   _c2   |         _c3          |
+---------+------+---------+----------------------+
| string  | int  | bigint  | array<decimal(1,0)>  |
+---------+------+---------+----------------------+

[HIVE-27563] Add typeof UDF - ASF JIRA

総評

json_read, array_*, データスケッチ系が個人的に嬉しいです。

Hive 4関連記事一覧再掲