Array(t)

An array of T-type items. T can be any data type, including an array.

Creating an Array

You can use a function to create an array:

  1. array(T)

You can also use square brackets.

  1. []

Example of creating an array:

  1. SELECT array(1, 2) AS x, toTypeName(x)
  1. ┌─x─────┬─toTypeName(array(1, 2))─┐
  2. [1,2] Array(UInt8)
  3. └───────┴─────────────────────────┘
  1. SELECT [1, 2] AS x, toTypeName(x)
  1. ┌─x─────┬─toTypeName([1, 2])─┐
  2. [1,2] Array(UInt8)
  3. └───────┴────────────────────┘

Working with Data Types

The maximum size of an array is limited to one million elements.

When creating an array on the fly, ClickHouse automatically defines the argument type as the narrowest data type that can store all the listed arguments. If there are any Nullable or literal NULL values, the type of an array element also becomes Nullable.

If ClickHouse couldn’t determine the data type, it generates an exception. For instance, this happens when trying to create an array with strings and numbers simultaneously (SELECT array(1, 'a')).

Examples of automatic data type detection:

  1. SELECT array(1, 2, NULL) AS x, toTypeName(x)
  1. ┌─x──────────┬─toTypeName(array(1, 2, NULL))─┐
  2. [1,2,NULL] Array(Nullable(UInt8))
  3. └────────────┴───────────────────────────────┘

If you try to create an array of incompatible data types, ClickHouse throws an exception:

  1. SELECT array(1, 'a')
  1. Received exception from server (version 1.1.54388):
  2. Code: 386. DB::Exception: Received from localhost:9000, 127.0.0.1. DB::Exception: There is no supertype for types UInt8, String because some of them are String/FixedString and some of them are not.

Array Size

It is possible to find the size of an array by using the size0 subcolumn without reading the whole column. For multi-dimensional arrays you can use sizeN-1, where N is the wanted dimension.

Example

Query:

  1. CREATE TABLE t_arr (`arr` Array(Array(Array(UInt32)))) ENGINE = MergeTree ORDER BY tuple();
  2. INSERT INTO t_arr VALUES ([[[12, 13, 0, 1],[12]]]);
  3. SELECT arr.size0, arr.size1, arr.size2 FROM t_arr;

Result:

  1. ┌─arr.size0─┬─arr.size1─┬─arr.size2─┐
  2. 1 [2] [[4,1]]
  3. └───────────┴───────────┴───────────┘