Struct
Use the struct data type to create a column that contains nested columns. The nested columns can be of different data types, including the struct type.
Define a struct type
Syntax:STRUCT< >
Struct types are declared using the angle brackets (<
and >
).
Examples
The statement below creates a table x
that contains struct a
, which contains two nested columns (b
and c
) that are both integers.
The statement below creates a table y
that contains struct a
, which contains another struct c
.
Below is a real world example.
You can also use the struct type to parse a string that contains data in JSON format into its proper format.
For instance, if your data is structured like so:
And you want it to be structured like so:
You can parse it using the struct type.
Add values to a struct
To add values to structs, enclose the nested data with ()
in the SQL statement. For example, (1, true)
. Alternatively, you can also use ROW(1, true)
.
Examples
The statement below adds values to table x
.
The statement below adds values to table y
.
The statement below adds values to table trip
.
Retrieve data in a struct
To retrieve data in a struct, enclose the struct name with ()
and use the dot operator to specify the nested column. For example, to access the initial_charge
column under fare
in the trip
schema, use (fare).initial_charge
.
Examples
Casting
Structs can be casted explicitly or implicitly to structs if the nested expressions and types can be casted.
Examples
Was this page helpful?