Define ARRAY columns
You can define an ARRAY column when you create a table.type specifies the data types of elements in an array. The database supports the following element types: BOOLEAN, TINYINT, SMALLINT, INT, BIGINT, LARGEINT, FLOAT, DOUBLE, VARCHAR, CHAR, DATETIME, DATE, JSON, ARRAY (since v3.1), MAP (since v3.1), and STRUCT (since v3.1).
Elements in an array are nullable by default, for example, [null, 1 ,2]. You cannot specify elements in an array as NOT NULL. However, you can specify an ARRAY column as NOT NULL when you create a table, such as the third example in the following code snippet.
Examples:
Limits
The following limits apply when you create ARRAY columns in tables:- In versions earlier than v2.1, you can create ARRAY columns only in Duplicate Key tables. From v2.1 onwards, you can also create ARRAY columns in other types of tables (Primary Key, Unique Key, Aggregate). Note that in an Aggregate table, you can create an ARRAY column only when the function used to aggregate data in that column is replace() or replace_if_not_null(). For more information, see Aggregate table.
- ARRAY columns cannot be used as key columns.
- ARRAY columns cannot be used as partition keys (included in PARTITION BY) or bucketing keys (included in DISTRIBUTED BY).
- An array can have a maximum of 14-level nesting.
Construct arrays in SQL
Arrays can be constructed in SQL using brackets[], with each array element separated by a comma (,).
Text
Text
<>) to show the declared array type.
Text
Text
Text
Load Array data
The database supports loading Array data in three ways:- INSERT INTO is suitable for loading small-scale data for testing.
- Broker Load is suitable for loading ORC or Parquet files with large-scale data.
- Stream Load and Routine Load are suitable for loading CSV files with large-scale data.
Use INSERT INTO to load arrays
You can use INSERT INTO to load small-scale data column by column, or perform ETL on data before loading the data.Use Broker Load to load arrays from ORC or Parquet files
The array type in the database corresponds to the list structure in ORC and Parquet files, which eliminates the need for you to specify different data types in the database. For more information about data loading, see Broker load.Use Stream Load or Routine Load to load CSV-formatted arrays
Arrays in CSV files are separated with comma by default. You can use Stream Load or Routine Load to load CSV text files or CSV data in Kafka.Query ARRAY data
You can access elements in an array using[] and subscripts, starting from 1.
Text
Text
Text
Text