Amazon Redshift は、2025 年 11 月 1 日以降、新しい Python UDF の作成をサポートしなくなります。Python UDF を使用する場合は、その日付より前に UDF を作成してください。既存の Python UDF は引き続き通常どおり機能します。詳細については、ブログ記事
H3_ToChildren
H3_ToChildren は、特定の H3 インデックスの指定された解像度で子 H3 セル ID のリストを返します。H3 インデックスの詳細については、「H3」を参照してください。
構文
H3_ToChildren(index, resolution)
引数
- インデックス
-
H3 セルのインデックスを表すデータ型
BIGINTまたはVARCHARの値、またはこれらのデータ型のいずれかに評価される式。 - resolution
-
データ型
INTEGERの値またはINTEGER型と評価される式の値。値は、子セル ID の解像度を表します。値は、入力されたインデックスの解像度から 15 までの整数である必要があります。
戻り型
SUPER — H3 セル ID のリストを表します。
インデックスまたは解像度が NULL の場合、NULL が返されます。
index が有効でない場合、エラーが返されます。
解像度がインデックスの解像度と 15 の間 (両端の数値を含む) にない場合、エラーが返されます。
出力サイズが SUPER の最大サイズ制限を超えると、エラーが返されます。
例
次の SQL は、H3 セルのインデックスを表す VARCHAR と、すべての子の目的の解像度を表す INTEGER を入力し、解像度 6 で子を含む SUPER 配列を返します。
SELECT H3_ToChildren('85283473fffffff', 6);
h3_tochildren
--------------------------------------------------------------------------------------------------------------------------------------
[604189641121202175,604189641255419903,604189641389637631,604189641523855359,604189641658073087,604189641792290815,604189641926508543]
次の SQL は、H3 セルのインデックスを表す BIGINT と、すべての子の望ましい解像度を表す INTEGER を入力し、解像度 6 で子を含む SUPER 配列を返します。
SELECT H3_ToChildren(599686042433355775, 6);
h3_tochildren
--------------------------------------------------------------------------------------------------------------------------------------
[604189641121202175,604189641255419903,604189641389637631,604189641523855359,604189641658073087,604189641792290815,604189641926508543]
注: 解像度とインデックス解像度の差は 7 以下であれば安全です。
次の例は、SUPER サイズ制限を超えるクエリの回避策を示しています。入力 H3 インデックスと目的の子解像度の解像度の差が大きすぎる (7 より大きい) 場合。この手順では、小さなステップ (一度に最大 5 つの解像度レベル) で子を段階的に拡張し、最終的な結果をユーザーが作成したテーブルに保存することで問題を解決します。
CREATE OR REPLACE PROCEDURE generate_h3_children() LANGUAGE plpgsql AS $$ BEGIN -- Drop and create h3_children table that will contain the results DROP TABLE IF EXISTS h3_children; CREATE TABLE h3_children ( h3_index BIGINT, child_res INTEGER, children SUPER ); -- Create temporary table for steps DROP TABLE IF EXISTS h3_steps; CREATE TABLE h3_steps ( h3_index BIGINT, current_res INTEGER, target_res INTEGER, h3_array SUPER ); -- Initial insert into h3_steps INSERT INTO h3_steps SELECT h3_index, H3_Resolution(h3_index), child_res, ARRAY(h3_index) FROM h3_indexes; -- Insert from your table with h3_index and child_res as columns -- Loop until we reach target resolution -- We expect at most 3 iterations considering that we can start at resolution -- 0 and target/child resolution equal to 15 (0 -> 5 -> 10 -> 15) WHILE EXISTS ( SELECT 1 FROM h3_steps h GROUP BY h3_index, target_res HAVING MAX(current_res) < target_res ) LOOP -- Populate the h3_steps table with the tables that need to -- reach closer to the target res INSERT INTO h3_steps SELECT h.h3_index, LEAST(h.current_res + 5, h.target_res), -- Do not exceed target res h.target_res, -- Take the children of the child cell at resolution current_res of the -- h3_index H3_ToChildren(c.child::BIGINT, LEAST(h.current_res + 5, h.target_res)) FROM h3_steps h, UNNEST(h.h3_array) AS c(child) WHERE h.current_res < h.target_res AND h.current_res = (SELECT MAX(current_res) FROM h3_steps WHERE h3_index = h.h3_index ); END LOOP; -- Store final results INSERT INTO h3_children SELECT h3_index AS h3_index, target_res AS child_res, h3_array AS children FROM h3_steps WHERE current_res = target_res; END; $$; -- Create the source table for H3_ToChildren queries CREATE TABLE h3_indexes ( h3_index BIGINT, child_res INTEGER, PRIMARY KEY (h3_index, child_res) ); INSERT INTO h3_indexes (h3_index, child_res) VALUES (x'8001fffffffffff'::BIGINT, 11); -- Execute the procedure CALL generate_h3_children(); -- View results SELECT * FROM h3_children;