Skip to main content

Load STRUCT Data

STRUCT<field_name:field_type [COMMENT 'comment_string'], ... > represents a structure composed of multiple fields, which can also be understood as a collection of multiple columns. For more type details, see STRUCT Data Type.

Use Stream Load to load STRUCT columns from either of these file formats:

  • CSV format import: Suitable for importing from text files, ETL job outputs, and similar scenarios.
  • JSON format import: Suitable for importing from APIs, logs, or semi-structured data sources.

Both methods are completed through Stream Load, and INSERT INTO VALUES statements are also supported for direct writes.

Before you start​

Create or select the target database, and use a warehouse SQL user with the required table and load permissions. Copy the Stream Load request URL from Connection Methods. Replace <endpoint> with its scheme, host, and port, and preserve HTTPS when it is required. Replace <username> with your warehouse SQL username. The curl commands prompt for your password.

Choose one input-format procedure for each sample table, or recreate the sample table before trying another procedure. The displayed rows and timings illustrate the source examples. They are not measurements from your warehouse.

Import process overview​

Regardless of the data format used, importing STRUCT type data follows these 4 steps:

StepOperationDescription
1Prepare dataConstruct test data files in the target format (CSV / JSON)
2Create a table in the databaseCreate a table schema that includes a STRUCT field
3Import dataWrite data using Stream Load or INSERT INTO
4Verify imported dataVerify the result with SELECT

Load CSV data​

Step 1: Prepare data​

Create the following CSV file test_struct.csv. The delimiter uses | instead of a comma, so that it can be distinguished from the commas inside STRUCT.

1|{10, 3.14, "Emily"}
2|{4, 1.5, null}
3|{7, null, "Benjamin"}
4|{}
5|null

Step 2: Create a table in the database​

CREATE TABLE struct_test (
id INT NOT NULL,
c_struct STRUCT<f1:INT,f2:FLOAT,f3:STRING> NULL
)
DUPLICATE KEY(id)
DISTRIBUTED BY HASH(id) BUCKETS 1;

Step 3: Import data​

Use Stream Load to import the CSV file:

curl --location-trusted \
-u "<username>" \
-H "column_separator:|" \
-H "columns: id, c_struct" \
-T "test_struct.csv" \
"<endpoint>/api/testdb/struct_test/_stream_load"

As a separate alternative to the file load, you can insert a STRUCT directly. Run this example in an empty table if you want to inspect this row alone:

INSERT INTO struct_test VALUES(1, named_struct('f1', '1', 'f2', '2.0', 'f3', 'abc'));

Step 4: Verify imported data​

mysql> SELECT * FROM struct_test;
+------+--------------------------------------+
| id | c_struct |
+------+--------------------------------------+
| 1 | {"f1":10, "f2":3.14, "f3":"Emily"} |
| 2 | {"f1":4, "f2":1.5, "f3":null} |
| 3 | {"f1":7, "f2":null, "f3":"Benjamin"} |
| 4 | {"f1":null, "f2":null, "f3":null} |
| 5 | NULL |
+------+--------------------------------------+
5 rows in set (0.01 sec)

Load JSON data​

Step 1: Prepare data​

Create the following JSON file test_struct.json:

[
{"id":1, "c_struct":{"f1":10, "f2":3.14, "f3":"Emily"}},
{"id":2, "c_struct":{"f1":4, "f2":1.5, "f3":null}},
{"id":3, "c_struct":{"f1":7, "f2":null, "f3":"Benjamin"}},
{"id":4, "c_struct":{}},
{"id":5, "c_struct":null}
]

Step 2: Create a table in the database​

CREATE TABLE struct_test (
id INT NOT NULL,
c_struct STRUCT<f1:INT,f2:FLOAT,f3:STRING> NULL
)
DUPLICATE KEY(id)
DISTRIBUTED BY HASH(id) BUCKETS 1;

Step 3: Import data​

Use Stream Load to import the JSON file. Specify the format with format:json, and use strip_outer_array:true to parse the outermost JSON array:

curl --location-trusted \
-u "<username>" \
-H "format:json" \
-H "columns: id, c_struct" \
-H "strip_outer_array:true" \
-T "test_struct.json" \
"<endpoint>/api/testdb/struct_test/_stream_load"

Step 4: Verify imported data​

mysql> SELECT * FROM struct_test;
+------+--------------------------------------+
| id | c_struct |
+------+--------------------------------------+
| 1 | {"f1":10, "f2":3.14, "f3":"Emily"} |
| 2 | {"f1":4, "f2":1.5, "f3":null} |
| 3 | {"f1":7, "f2":null, "f3":"Benjamin"} |
| 4 | {"f1":null, "f2":null, "f3":null} |
| 5 | NULL |
+------+--------------------------------------+
5 rows in set (0.00 sec)

Key parameters​

The following table lists the Stream Load header parameters commonly used during STRUCT import:

ParameterApplicable FormatDescription
column_separatorCSVColumn delimiter. STRUCT internally uses ,, so it is recommended to use other characters such as | for the outer delimiter to avoid ambiguity
columnsCSV / JSONSpecifies the mapping between columns and table fields
formatJSONSpecifies that the import file format is json
strip_outer_arrayJSONSet to true when the outermost layer of the JSON file is an array, so that it is parsed row by row

FAQ​

1. Why does the CSV file need to use | instead of , as the delimiter?​

The literal of a STRUCT field internally uses , to separate fields (for example, {10, 3.14, "Emily"}). Without suitable enclosing and escape settings, an outer , delimiter conflicts with the STRUCT internal delimiter. It is recommended to use characters such as | or \t that do not appear inside STRUCT.

2. How do you insert STRUCT values directly in SQL?​

Use the NAMED_STRUCT() function to explicitly specify the name and value of each Field, for example:

INSERT INTO struct_test VALUES(1, named_struct('f1', '1', 'f2', '2.0', 'f3', 'abc'));

3. What forms of NULL are allowed for STRUCT fields?​

Data FormMeaning
nullThe entire STRUCT column is NULL
{}The STRUCT exists, but all fields are NULL
{10, null, "Emily"}Some fields are NULL, others are assigned values normally

4. Why does JSON import need to set strip_outer_array:true?​

When the JSON file wraps multiple records as an array (such as [ {...}, {...} ]), strip_outer_array:true is needed to make VeloDB Cloud remove the outermost array and parse row by row. Otherwise, the JSON reader rejects an outer array.

Common errors​

  • Authentication or connection failure: Check the generated warehouse endpoint, TLS requirements, SQL user permissions, and network access. For remote files, also check source credentials and reachability.
  • Filtered rows or parsing errors: Check the input format, delimiters, field mappings, and target column types. Inspect the returned load error before retrying. Raising the filter tolerance can discard data.
  • Unexpected duplicate rows: These examples use independent loads. Check the table model and existing rows before rerunning a sample.

See also​

Ask AI