Skip to main content
ARRAY, as an extended type of database, is supported in various database systems such as PostgreSQL, ClickHouse, and Snowflake. ARRAY is widely used in scenarios such as A/B tests, user tag analysis, and user profiling. The database supports multidimensional array nesting, array slicing, comparison, and filtering.

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
The database automatically infers data types if an array consists of elements of multiple types:
Text
You can use pointed brackets (<>) to show the declared array type.
Text
NULLs can be included in the element.
Text
For an empty array, you can use pointed brackets to show the declared type, or you can write [] directly for the database to infer the type based on the context. If the database cannot infer the type, it will report an error.
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
If the subscript is 0 or a negative number, no error is reported and NULL is returned.
Text
If the subscript exceeds the length of the array (the number of elements in the array), NULL will be returned.
Text
For multidimensional arrays, the elements can be accessed recursively.
Text