STRUCT

更新时间:
复制 MD 格式

Creates a STRUCT value from one or more values of any data type.

Syntax

struct(<value1>[, <value2>, ...])

Parameters

Parameter

Required

Description

value

Yes

A value of any data type: a literal, a column reference, or an expression. Pass one or more comma-separated values.

Return value

Returns a STRUCT value. Fields are named col1, col2, col3, and so on, in the order the values are passed.

Examples

Basic usage

Create a STRUCT from values of different data types:

SELECT struct('a', 123, 'true', 56.90);

Output:

{col1:a, col2:123, col3:true, col4:56.9}

Each value maps to an auto-named field:

Input value

Field

Result

'a' (STRING)

col1

a

123 (INT)

col2

123

'true' (STRING)

col3

true

56.90 (DOUBLE)

col4

56.9

Two-value STRUCT

SELECT struct(100, 'MaxCompute');

Output:

{col1:100, col2:MaxCompute}

Use with table data

Wrap two columns from a table into a STRUCT per row:

SELECT struct(name, age) FROM employees;

Each row produces a STRUCT with fields col1 (the value of name) and col2 (the value of age).

Usage notes

  • Values of different data types can be mixed in a single call.

  • Fields are named automatically: col1, col2, col3, and so on. To read a specific field, reference it by its generated name (for example, result_struct.col1).

Related functions

STRUCT is a complex type function. For other functions that process ARRAY, MAP, STRUCT, and JSON data types, see Complex type functions.