Skip to main content

STRUCT_ELEMENT

Description​

Returns a specified field from a STRUCT value.

Syntax​

STRUCT_ELEMENT(<struct>, <field_location>)
STRUCT_ELEMENT(<struct>, '<field_name>')

Parameters​

ParameterDescription
<struct>The input STRUCT value. If it is NULL, the function returns NULL.
<field_location>The field position, starting from 1. Must be a constant.
<field_name>The case-sensitive field name. Must be a constant.

Return value​

Returns the specified field. The return value can be of any type.

Example​

select struct_element(named_struct('f1', 1, 'f2', 'a'), 'f2'),struct_element(named_struct('f1', 1, 'f2', 'a'), 1);
+--------------------------------------------------------+-----------------------------------------------------+
| struct_element(named_struct('f1', 1, 'f2', 'a'), 'f2') | struct_element(named_struct('f1', 1, 'f2', 'a'), 1) |
+--------------------------------------------------------+-----------------------------------------------------+
| a | 1 |
+--------------------------------------------------------+-----------------------------------------------------+

Create a table and insert rows containing both whole-struct NULL values and NULL fields:

CREATE TABLE test_struct (
id INT,
struct_col STRUCT<f1: BIGINT, f2: BIGINT, f3: BIGINT, f4: BIGINT, f5: BIGINT>
) DUPLICATE KEY(id)
DISTRIBUTED BY HASH(id) BUCKETS 1;

INSERT INTO test_struct VALUES
(1, NAMED_STRUCT('f1', 1, 'f2', 2, 'f3', 3, 'f4', 4, 'f5', 5)),
(2, NAMED_STRUCT('f1', 1, 'f2', 1000, 'f3', 10000000, 'f4', 100000000000, 'f5', 100000000000)),
(3, NAMED_STRUCT('f1', 5, 'f2', 4, 'f3', 3, 'f4', 2, 'f5', 1)),
(4, NULL),
(5, NAMED_STRUCT('f1', 1, 'f2', NULL, 'f3', 3, 'f4', NULL, 'f5', 5));

SELECT struct_col, STRUCT_ELEMENT(struct_col, 'f1') AS f1
FROM test_struct
ORDER BY id;

With the default serde_dialect = 'doris', STRUCT values include field names and use lowercase null for nested NULL fields:

+--------------------------------------------------------------------------+------+
| struct_col | f1 |
+--------------------------------------------------------------------------+------+
| {"f1":1, "f2":2, "f3":3, "f4":4, "f5":5} | 1 |
| {"f1":1, "f2":1000, "f3":10000000, "f4":100000000000, "f5":100000000000} | 1 |
| {"f1":5, "f2":4, "f3":3, "f4":2, "f5":1} | 5 |
| NULL | NULL |
| {"f1":1, "f2":null, "f3":3, "f4":null, "f5":5} | 1 |
+--------------------------------------------------------------------------+------+

Enter to send · Shift+Enter for a new line