

 Amazon Redshift 將不再支援從修補程式 198 開始建立新的 Python UDFs。現有 Python UDF 將繼續正常運作至 2026 年 6 月 30 日。如需詳細資訊，請參閱[部落格文章](https://aws.amazon.com/blogs/big-data/amazon-redshift-python-user-defined-functions-will-reach-end-of-support-after-june-30-2026/)。

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# H3\$1ToChildren
<a name="H3_ToChildren-function"></a>

H3\$1ToChildren 會傳回指定 H3 索引之特定解析度的子 H3 儲存格 IDs 清單。如需 H3 索引的詳細資訊，請參閱 [H3](spatial-terminology.md#spatial-terminology-h3)。

## 語法
<a name="H3_ToChildren-function-syntax"></a>

```
H3_ToChildren(index, resolution)
```

## 引數
<a name="H3_ToChildren-function-arguments"></a>

 *索引*   
資料類型的值 `BIGINT`或 `VARCHAR`代表 H3 儲存格的索引，或評估為其中一個資料類型的表達式。

 *解析度*   
`INTEGER` 資料類型的值，或是評估為 `INTEGER` 類型的表達式。此值代表子儲存格 IDs的解析度。該值必須是輸入*索引*的解析度與 15 之間的整數，包含在內。

## 傳回類型
<a name="H3_ToChildren-function-return"></a>

`SUPER`— 代表 H3 儲存格 ID 的清單。

如果*索引*或*解析度*為 NULL，則會傳回 NULL。

如果 *index* 無效，則會傳回錯誤。

如果*解析度*不在*索引*的解析度和包含 15 之間，則會傳回錯誤。

如果輸出大小超過最大 SUPER 大小限制，則會傳回錯誤。

## 範例
<a name="H3_ToChildren-function-examples"></a>

下列 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;
```