Skip to main content

NAMED_STRUCT

Description

Constructs and returns a struct based on given field names and values. The function accepts an even number of parameters, where odd positions are field names and even positions are field values.

Syntax

NAMED_STRUCT( <field_name> , <field_value> [ , <field_name> , <field_value> ... ] )

Parameters

  • <field_name>: Input content at odd positions for constructing the struct, the name of the field, must be a constant string
  • <field_value>: Input content at even positions for constructing the struct, the value of the field, can be multiple columns or constants

Supported element types:

  • Numeric types: TINYINT, SMALLINT, INT, BIGINT, LARGEINT, FLOAT, DOUBLE, DECIMAL
  • String types: CHAR, VARCHAR, STRING
  • Date and time types: DATE, DATETIME, DATEV2, DATETIMEV2
  • Boolean type: BOOLEAN
  • IP types: IPV4, IPV6
  • Complex types: ARRAY, MAP, STRUCT

Return Value

Return type: STRUCT

Return value meaning:

  • Returns a struct containing all specified field name and value pairs
  • All fields support NULL values

Usage

  • The function combines all field name and value pairs into a struct, where odd positions are field names (must be constant strings and cannot be repeated, case-insensitive), and even positions are field values (can be multiple columns or constants)
  • The number of parameters must be a non-zero even number greater than 1
  • All fields are marked as nullable

Query Examples:

Basic usage:

select named_struct('name', 'Alice', 'age', 25, 'city', 'Beijing');
+-------------------------------------------------------------+
| named_struct('name', 'Alice', 'age', 25, 'city', 'Beijing') |
+-------------------------------------------------------------+
| {"name":"Alice", "age":25, "city":"Beijing"} |
+-------------------------------------------------------------+

Including null values:

select named_struct('id', 1, 'name', null, 'score', 95.5);
+----------------------------------------------------+
| named_struct('id', 1, 'name', null, 'score', 95.5) |
+----------------------------------------------------+
| {"id":1, "name":null, "score":95.5} |
+----------------------------------------------------+

Including complex types:

select named_struct('array', [1,2,3], 'map', {'key':'value'}, 'struct', named_struct('f1',1,'f2',2));
+-----------------------------------------------------------------------------------------------+
| named_struct('array', [1,2,3], 'map', {'key':'value'}, 'struct', named_struct('f1',1,'f2',2)) |
+-----------------------------------------------------------------------------------------------+
| {"array":[1, 2, 3], "map":{"key":"value"}, "struct":{"f1":1, "f2":2}} |
+-----------------------------------------------------------------------------------------------+

Creating a named struct containing IP addresses:

select named_struct('ipv4', cast('192.168.1.1' as ipv4), 'ipv6', cast('2001:db8::1' as ipv6));
+----------------------------------------------------------------------------------------+
| named_struct('ipv4', cast('192.168.1.1' as ipv4), 'ipv6', cast('2001:db8::1' as ipv6)) |
+----------------------------------------------------------------------------------------+
| {"ipv4":"192.168.1.1", "ipv6":"2001:db8::1"} |
+----------------------------------------------------------------------------------------+

Error Examples

Less than 2 parameters:

select named_Struct();
ERROR 1105 (HY000): errCode = 2, detailMessage = named_struct requires at least two arguments, like: named_struct('a', 1)

select named_struct('name');
ERROR 1105 (HY000): errCode = 2, detailMessage = named_struct requires at least two arguments, like: named_struct('a', 1)

Odd number of parameters:

select named_struct('name', 'Alice', 'age');
ERROR 1105 (HY000): errCode = 2, detailMessage = named_struct can't be odd parameters, need even parameters named_struct('name', 'Alice', 'age')

Duplicate field names, field names are case-insensitive:

select named_struct('name', 'Alice', 'name', 'Bob');
ERROR 1105 (HY000): errCode = 2, detailMessage = The name of the struct field cannot be repeated. same name fields are name

select named_struct('name', 'Alice', 'Name', 'Bob');
ERROR 1105 (HY000): errCode = 2, detailMessage = The name of the struct field cannot be repeated. same name fields are name