H3_ ToChildren - Amazon Redshift

Amazon Redshift non supporterà più la creazione di nuovi Python a UDFs partire dal 1° novembre 2025. Se vuoi usare Python UDFs, crea la UDFs data precedente a quella data. Python esistente UDFs continuerà a funzionare normalmente. Per ulteriori informazioni, consulta il post del blog.

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

H3_ ToChildren

H3_ ToChildren restituisce un elenco di celle H3 secondarie con una risoluzione specificata per un determinato IDs indice H3. Per ulteriori informazioni sull'indicizzazione H3, consulta H3.

Sintassi

H3_ToChildren(index, resolution)

Arguments (Argomenti)

indice

Un valore di tipo di dati BIGINT o VARCHAR che rappresenta l'indice di una cella H3 o un'espressione che restituisce uno di questi tipi di dati.

risoluzione

Un valore di tipo INTEGER o un'espressione che restituisce un valore di tipo INTEGER. Il valore rappresenta la risoluzione della cella secondaria. IDs Il valore deve essere un numero intero compreso tra la risoluzione dell'indice di input e 15, inclusi.

Tipo restituito

SUPER— rappresenta un elenco di celle H3. IDs

Se l'indice o la risoluzione sono NULL, viene restituito NULL.

Se index non è valido, viene restituito un errore.

Se la risoluzione non è compresa tra la risoluzione dell'indice e 15, inclusi, viene restituito un errore.

Se la dimensione di output supera il limite massimo di dimensione SUPER, viene restituito un errore.

Esempi

Il codice SQL seguente inserisce un VARCHAR che rappresenta l'indice di una cella H3 e un numero INTEGER che rappresenta la risoluzione desiderata di tutti i figli e restituisce un array SUPER contenente i figli alla risoluzione 6.

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

Il codice SQL seguente immette un valore BIGINT che rappresenta l'indice di una cella H3 e un valore INTEGER che rappresenta la risoluzione desiderata di tutti i figli e restituisce un array SUPER contenente i figli alla risoluzione 6.

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

Nota: una differenza pari o inferiore a 7 tra la risoluzione e la risoluzione dell'indice è sicura.

L'esempio seguente mostra una soluzione alternativa per le query che superano il limite di dimensione SUPER. Quando la differenza di risoluzione tra l'indice H3 di input e la risoluzione secondaria desiderata è troppo grande (maggiore di 7). Questa procedura risolve il problema espandendo in modo incrementale i figli in fasi più piccole (massimo 5 livelli di risoluzione alla volta) e memorizzando i risultati finali in una tabella creata dall'utente.

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;