FLATTEN (Lakehouse v1)
Transforma dados JSON aninhados num formato tabular, em que cada elemento ou campo é representado como uma linha separada.
Sintaxe SQL
Seção intitulada “Sintaxe SQL”[LATERAL] FLATTEN ( INPUT => <expr> [ , PATH => <expr> ] [ , OUTER => TRUE | FALSE ] [ , RECURSIVE => TRUE | FALSE ] [ , MODE => 'OBJECT' | 'ARRAY' | 'BOTH' ] )| Parâmetro / palavra-chave | Descrição | Predefinição |
|---|---|---|
| INPUT | Especifica os dados JSON ou de matriz a nivelar. | - |
| PATH | Especifica o caminho para a matriz ou o objeto, dentro dos dados de entrada, a nivelar. | - |
| OUTER | Se definido como TRUE, as linhas sem resultados continuam a ser incluídas na saída, mas os valores das colunas KEY, INDEX e VALUE dessas linhas ficam definidos como NULL. | FALSE |
| RECURSIVE | Se definido como TRUE, a função continua a nivelar os elementos aninhados. | FALSE |
| MODE | Controla se são nivelados apenas objetos (‘OBJECT’), apenas matrizes (‘ARRAY’) ou ambos (‘BOTH’). | ‘BOTH’ |
| LATERAL | LATERAL é uma palavra-chave opcional utilizada para referenciar colunas definidas à esquerda da palavra-chave LATERAL na cláusula FROM. LATERAL permite a referência cruzada entre as expressões de tabela anteriores e a função. | - |
A tabela seguinte descreve as colunas de saída da função FLATTEN:
| Coluna | Descrição |
|---|---|
| SEQ | Um número de sequência único associado à entrada. |
| KEY | Chave do valor expandido. Se o elemento nivelado não contiver uma chave, é definida como NULL. |
| PATH | Caminho para o elemento nivelado. |
| INDEX | Se o elemento for uma matriz, esta coluna contém o respetivo índice; caso contrário, é definida como NULL. |
| VALUE | Valor do elemento nivelado. |
| THIS | Esta coluna identifica o elemento que está atualmente a ser nivelado. |
Exemplos de SQL
Seção intitulada “Exemplos de SQL”Exemplos de SQL 1: demonstração dos parâmetros PATH, OUTER, RECURSIVE e MODE
Seção intitulada “Exemplos de SQL 1: demonstração dos parâmetros PATH, OUTER, RECURSIVE e MODE”Este exemplo demonstra o comportamento da função FLATTEN relativamente aos parâmetros PATH, OUTER, RECURSIVE e MODE.
SELECT *FROM FLATTEN ( INPUT => PARSE_JSON ( '{"name": "John", "languages": ["English", "Spanish", "French"], "address": {"city": "New York", "state": "NY"}}' ) );
┌────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐│ seq │ key │ path │ index │ value │ this │├────────┼──────────────────┼──────────────────┼──────────────────┼──────────────────────────────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────┤│ 1 │ address │ address │ NULL │ {"city":"New York","state":"NY"} │ {"address":{"city":"New York","state":"NY"},"languages":["English","Spanish","French"],"name":"John"} ││ 1 │ languages │ languages │ NULL │ ["English","Spanish","French"] │ {"address":{"city":"New York","state":"NY"},"languages":["English","Spanish","French"],"name":"John"} ││ 1 │ name │ name │ NULL │ "John" │ {"address":{"city":"New York","state":"NY"},"languages":["English","Spanish","French"],"name":"John"} │└────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
-- PATH helps in selecting elements at a specific path from the original JSON data.SELECT *FROM FLATTEN ( INPUT => PARSE_JSON ( '{"name": "John", "languages": ["English", "Spanish", "French"], "address": {"city": "New York", "state": "NY"}}' ), PATH => 'languages' );
┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐│ seq │ key │ path │ index │ value │ this │├────────┼──────────────────┼──────────────────┼──────────────────┼───────────────────┼────────────────────────────────┤│ 1 │ NULL │ languages[0] │ 0 │ "English" │ ["English","Spanish","French"] ││ 1 │ NULL │ languages[1] │ 1 │ "Spanish" │ ["English","Spanish","French"] ││ 1 │ NULL │ languages[2] │ 2 │ "French" │ ["English","Spanish","French"] │└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
-- RECURSIVE enables recursive flattening of nested structures.SELECT *FROM FLATTEN ( INPUT => PARSE_JSON ( '{"name": "John", "languages": ["English", "Spanish", "French"], "address": {"city": "New York", "state": "NY"}}' ), RECURSIVE => TRUE );
┌────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐│ seq │ key │ path │ index │ value │ this │├────────┼──────────────────┼──────────────────┼──────────────────┼──────────────────────────────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────┤│ 1 │ address │ address │ NULL │ {"city":"New York","state":"NY"} │ {"address":{"city":"New York","state":"NY"},"languages":["English","Spanish","French"],"name":"John"} ││ 1 │ city │ address.city │ NULL │ "New York" │ {"city":"New York","state":"NY"} ││ 1 │ state │ address.state │ NULL │ "NY" │ {"city":"New York","state":"NY"} ││ 1 │ languages │ languages │ NULL │ ["English","Spanish","French"] │ {"address":{"city":"New York","state":"NY"},"languages":["English","Spanish","French"],"name":"John"} ││ 1 │ NULL │ languages[0] │ 0 │ "English" │ ["English","Spanish","French"] ││ 1 │ NULL │ languages[1] │ 1 │ "Spanish" │ ["English","Spanish","French"] ││ 1 │ NULL │ languages[2] │ 2 │ "French" │ ["English","Spanish","French"] ││ 1 │ name │ name │ NULL │ "John" │ {"address":{"city":"New York","state":"NY"},"languages":["English","Spanish","French"],"name":"John"} │└────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
-- MODE specifies whether only objects ('OBJECT'), only arrays ('ARRAY'), or both ('BOTH') should be flattened.-- In this example, MODE => 'ARRAY' is used, which means that only arrays within the JSON data will be flattened.SELECT *FROM FLATTEN ( INPUT => PARSE_JSON ( '{"name": "John", "languages": ["English", "Spanish", "French"], "address": {"city": "New York", "state": "NY"}}' ), MODE => 'ARRAY' );
---
-- OUTER determines the inclusion of zero-row expansions in the output.-- In this first example, OUTER => TRUE is used with an empty JSON array, which results in zero-row expansions.-- Rows are included in the output even when there are no values to flatten.SELECT *FROM FLATTEN (INPUT => PARSE_JSON ('[]'), OUTER => TRUE);
┌─────────────────────────────────────────────────────────────────────────────────────────────────────────┐│ seq │ key │ path │ index │ value │ this │├────────┼──────────────────┼──────────────────┼──────────────────┼───────────────────┼───────────────────┤│ 1 │ NULL │ NULL │ NULL │ NULL │ NULL │└─────────────────────────────────────────────────────────────────────────────────────────────────────────┘
-- In this second example, OUTER is omitted, and the output shows how rows with zero results are not included when OUTER is not specified.SELECT *FROM FLATTEN (INPUT => PARSE_JSON ('[]'));Exemplos de SQL 2: demonstração de LATERAL FLATTEN
Seção intitulada “Exemplos de SQL 2: demonstração de LATERAL FLATTEN”Este exemplo demonstra o comportamento da função FLATTEN quando utilizada em conjunto com a palavra-chave LATERAL.
-- Create a table for Tim Hortons transactions with multiple itemsCREATE TABLE tim_hortons_transactions ( transaction_id INT, customer_id INT, items VARIANT);
-- Insert data for Tim Hortons transactions with multiple itemsINSERT INTO tim_hortons_transactions (transaction_id, customer_id, items)VALUES (101, 1, parse_json('[{"item":"coffee", "price":2.50}, {"item":"donut", "price":1.20}]')), (102, 2, parse_json('[{"item":"bagel", "price":1.80}, {"item":"muffin", "price":2.00}]')), (103, 3, parse_json('[{"item":"timbit_assortment", "price":5.00}]'));
-- Show Tim Hortons transactions with multiple items using LATERAL FLATTENSELECT t.transaction_id, t.customer_id, f.value:item::STRING AS purchased_item, f.value:price::FLOAT AS priceFROM tim_hortons_transactions t, LATERAL FLATTEN(input => t.items) f;
┌───────────────────────────────────────────────────────────────────────────┐│ transaction_id │ customer_id │ purchased_item │ price │├─────────────────┼─────────────────┼───────────────────┼───────────────────┤│ 101 │ 1 │ coffee │ 2.5 ││ 101 │ 1 │ donut │ 1.2 ││ 102 │ 2 │ bagel │ 1.8 ││ 102 │ 2 │ muffin │ 2 ││ 103 │ 3 │ timbit_assortment │ 5 │└───────────────────────────────────────────────────────────────────────────┘
-- Find maximum, minimum, and average prices of the purchased itemsSELECT MAX(f.value:price::FLOAT) AS max_price, MIN(f.value:price::FLOAT) AS min_price, AVG(f.value:price::FLOAT) AS avg_priceFROM tim_hortons_transactions t, LATERAL FLATTEN(input => t.items) f;
┌───────────────────────────────────────────────────────────┐│ max_price │ min_price │ avg_price │├───────────────────┼───────────────────┼───────────────────┤│ 5 │ 1.2 │ 2.5 │└───────────────────────────────────────────────────────────┘