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.
NAMED_STRUCT( <field_name> , <field_value> [ , <field_name> , <field_value> ... ] )
<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 constantsSupported element types:
Return type: STRUCT
Return value meaning:
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