- Categorias:
MAP_KEYS¶
Retorna as chaves em um MAP.
Sintaxe¶
MAP_KEYS( <map> )
Argumentos¶
map
O mapa de entrada.
Retornos¶
Retorna uma ARRAY estruturada contendo as chaves em MAP. A ordem das chaves é indefinida.
Exemplos¶
Liste as chaves em um mapa:
SELECT MAP_KEYS({'a':1,'b':2,'c':3}::MAP(VARCHAR,NUMBER))
AS map_keys;
+----------+
| MAP_KEYS |
|----------|
| [ |
| "a", |
| "b", |
| "c" |
| ] |
+----------+
Crie uma tabela temporária que contenha valores MAP:
CREATE OR REPLACE TEMP TABLE demo_maps(
id INTEGER,
attrs MAP(VARCHAR, VARCHAR),
defaults MAP(VARCHAR, VARCHAR),
keep_keys ARRAY(VARCHAR),
ins_key VARCHAR,
ins_val VARCHAR,
update_existing BOOLEAN,
del_key1 VARCHAR,
del_key2 VARCHAR);
INSERT INTO demo_maps SELECT
1,
{'color':'red','size':'M','brand':'Acme'}::MAP(VARCHAR, VARCHAR),
{'currency':'USD','size':'L'}::MAP(VARCHAR, VARCHAR),
['color','brand']::ARRAY(VARCHAR),
'material',
'cotton',
TRUE,
'size',
'brand';
INSERT INTO demo_maps SELECT
2,
{'color':'blue','brand':'ZenCo'}::MAP(VARCHAR, VARCHAR),
{'currency':'EUR','size':'M','brand':'ZenCo'}::MAP(VARCHAR, VARCHAR),
['brand','currency']::ARRAY(VARCHAR),
'brand',
'ZC',
FALSE,
'currency',
'material';
Consulte a tabela para mostrar os dados:
SELECT * FROM demo_maps;
+----+---------------------+----------------------+--------------+----------+---------+-----------------+----------+----------+
| ID | ATTRS | DEFAULTS | KEEP_KEYS | INS_KEY | INS_VAL | UPDATE_EXISTING | DEL_KEY1 | DEL_KEY2 |
|----+---------------------+----------------------+--------------+----------+---------+-----------------+----------+----------|
| 1 | { | { | [ | material | cotton | True | size | brand |
| | "brand": "Acme", | "currency": "USD", | "color", | | | | | |
| | "color": "red", | "size": "L" | "brand" | | | | | |
| | "size": "M" | } | ] | | | | | |
| | } | | | | | | | |
| 2 | { | { | [ | brand | ZC | False | currency | material |
| | "brand": "ZenCo", | "brand": "ZenCo", | "brand", | | | | | |
| | "color": "blue" | "currency": "EUR", | "currency" | | | | | |
| | } | "size": "M" | ] | | | | | |
| | | } | | | | | | |
+----+---------------------+----------------------+--------------+----------+---------+-----------------+----------+----------+
Retorne as chaves nos valores MAP na coluna attrs
:
SELECT id, MAP_KEYS(attrs) AS attr_keys
FROM demo_maps;
+----+------------+
| ID | ATTR_KEYS |
|----+------------|
| 1 | [ |
| | "brand", |
| | "color", |
| | "size" |
| | ] |
| 2 | [ |
| | "brand", |
| | "color" |
| | ] |
+----+------------+