H3_ToChildren - Amazon Redshift

从 2025 年 11 月 1 日起,Amazon Redshift 将不再支持创建新的 Python UDF。如果您想要使用 Python UDF,请在该日期之前创建 UDF。现有的 Python UDF 将继续正常运行。有关更多信息,请参阅博客文章

H3_ToChildren

H3_ToChildren 返回给定 H3 索引在指定分辨率下的子 H3 单元格 ID 的列表。有关 H3 索引的信息,请参阅H3

语法

H3_ToChildren(index, resolution)

参数

索引

一个 BIGINTVARCHAR 数据类型的值,该值表示 H3 单元格的索引;或一个计算结果为这两种数据类型之一的表达式。

resolution

一个 INTEGER 数据类型的值,或一个计算结果为 INTEGER 类型的表达式。该值表示子单元格 ID 的分辨率。该值必须是介于输入 index 的分辨率和 15(含)之间的整数。

返回类型

SUPER – 表示 H3 单元格 ID 的列表。

如果 indexresolution 为 NULL,则返回 NULL。

如果 index 无效,则返回一个错误。

如果 resolution 不在 index 的分辨率与 15(含)之间,则返回一个错误。

如果输出大小超过 SUPER 大小上限,则返回一个错误。

示例

以下 SQL 输入一个表示 H3 单元格的索引的 VARCHAR 和一个表示所有子单元格的所需分辨率的 INTEGER,并返回一个 SUPER 数组,其中包含分辨率为 6 的子单元格。

SELECT H3_ToChildren('85283473fffffff', 6);
h3_tochildren -------------------------------------------------------------------------------------------------------------------------------------- [604189641121202175,604189641255419903,604189641389637631,604189641523855359,604189641658073087,604189641792290815,604189641926508543]

以下 SQL 输入一个表示 H3 单元格的索引的 BIGINT 和一个表示所有子单元格的所需分辨率的 INTEGER,并返回一个 SUPER 数组,其中包含分辨率为 6 的子单元格。

SELECT H3_ToChildren(599686042433355775, 6);
h3_tochildren -------------------------------------------------------------------------------------------------------------------------------------- [604189641121202175,604189641255419903,604189641389637631,604189641523855359,604189641658073087,604189641792290815,604189641926508543]

注意:resolutionindex 分辨率之差不大于 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;