Array Functions and Operators

Arrays store expressions of the same type in an ordered list.

Constructing arrays

An array constructor is an expression that consists of a sequence of comma-separated expressions of the same type enclosed in square brackets. It produces an array value:

  1. "[" expr [, ...] "]"

For example:

  1. db>
  1. select [1, 2, 3];
  1. {[1, 2, 3]}
  1. db>
  1. select [('a', 1), ('b', 2), ('c', 3)];
  1. {[('a', 1), ('b', 2), ('c', 3)]}

Empty arrays

An empty array can also be created, but it must be used together with a type cast, since EdgeDB cannot infer the type of an array that contains no elements.

  1. db>
  1. select [];
  1. QueryError: expression returns value of indeterminate type
  2. Hint: Consider using an explicit type cast.
  3. ### select [];
  4. ### ^
  1. db>
  1. select <array<int64>>[];
  1. {[]}

Functions and operators

array[i]

Array indexing.

array[from:to]

Array slicing.

array ++ array

Array concatenation.

= != ?= ?!= < > <= >=

Comparison operators

len()

Return number of elements in the array.

contains()

Check if an element is in the array.

find()

Find the index of an element in the array.

array_join()

Render an array to a string.

array_fill()

Make a new array of specified size and filled with specified value.

array_replace()

Return an array where all occurrences of a particualr value are replaced.

array_agg()

Return an array made from all of the input set elements.

array_get()

Return the element of array at the specified index.

array_unpack()

Return array elements as a set.

Reference

type

array

Arrays - 图1

Arrays - 图2

Arrays - 图3

Arrays represent a one-dimensional homogeneous ordered list.

Array indexing starts at zero.

With the exception of other array types, any type can be used as an array element type.

An array type is created implicitly when an array constructor is used:

  1. db>
  1. select [1, 2];
  1. {[1, 2]}

The syntax of an array type declaration can be found in this section.

See also the list of standard array functions and generic functions such as len().

operator

array[i]

Arrays - 图4

Arrays - 图5

Arrays - 图6

array<anytype> [ int64 ] -> anytype

Array indexing.

Example:

  1. db>
  1. select [1, 2, 3][0];
  1. {1}
  1. db>
  1. select [(x := 1, y := 1), (x := 2, y := 3.3)][1];
  1. {(x := 2, y := 3.3)}

Negative indexing is supported:

  1. db>
  1. select [1, 2, 3][-1];
  1. {3}

Referencing a non-existent array element will result in an error:

  1. db>
  1. select [1, 2, 3][4];
  1. InvalidValueError: array index 4 is out of bounds

operator

array[from:to]

Arrays - 图7

Arrays - 图8

Arrays - 图9

array<anytype> [ int64 : int64 ] -> anytype

Array slicing.

An omitted lower bound defaults to zero, and an omitted upper bound defaults to the size of the array.

The upper bound is non-inclusive.

Examples:

  1. db>
  1. select [1, 2, 3][0:2];
  1. {[1, 2]}
  1. db>
  1. select [1, 2, 3][2:];
  1. {[3]}
  1. db>
  1. select [1, 2, 3][:1];
  1. {[1]}
  1. db>
  1. select [1, 2, 3][:-2];
  1. {[1]}

Referencing an array slice beyond the array boundaries will result in an empty array (unlike a direct reference to a specific index):

  1. db>
  1. select [1, 2, 3][1:20];
  1. {[2, 3]}
  1. db>
  1. select [1, 2, 3][10:20];
  1. {[]}

operator

array ++ array

Arrays - 图10

Arrays - 图11

Arrays - 图12

array<anytype> ++ array<anytype> -> array<anytype>

Array concatenation.

  1. db>
  1. select [1, 2, 3] ++ [99, 98];
  1. {[1, 2, 3, 99, 98]}

function

array_agg()

Arrays - 图13

Arrays - 图14

Arrays - 图15

std::array_agg(s: set of anytype) -> array<anytype>

Return an array made from all of the input set elements.

The ordering of the input set will be preserved if specified.

  1. db>
  1. select array_agg({2, 3, 5});
  1. {[2, 3, 5]}
  1. db>
  1. select array_agg(User.name order by User.name);
  1. {['Alice', 'Bob', 'Joe', 'Sam']}

function

array_get()

Arrays - 图16

Arrays - 图17

Arrays - 图18

std::array_get(array: array<anytype>, index: int64, named only default: anytype = {} ) -> optional anytype

Return the element of array at the specified index.

If index is out of array bounds, the default or {} (empty set) is returned.

This works the same as array indexing operator except that if the index is outside array boundaries an empty set of the array element type is returned instead of raising an exception.

  1. db>
  1. select array_get([2, 3, 5], 1);
  1. {3}
  1. db>
  1. select array_get([2, 3, 5], 100);
  1. {}
  1. db>
  1. select array_get([2, 3, 5], 100, default := 42);
  1. {42}

function

array_unpack()

Arrays - 图19

Arrays - 图20

Arrays - 图21

std::array_unpack(array: array<anytype>) -> set of anytype

Return array elements as a set.

The ordering of the returned set is not guaranteed.

  1. db>
  1. select array_unpack([2, 3, 5]);
  1. {3, 2, 5}

function

array_join()

Arrays - 图22

Arrays - 图23

Arrays - 图24

std::array_join(array: array<str>, delimiter: str) -> str

Render an array to a string.

Join a string array into a single string using a specified delimiter:

  1. db>
  1. select to_str(['one', 'two', 'three'], ', ');
  1. {'one, two, three'}

function

array_fill()

Arrays - 图25

Arrays - 图26

Arrays - 图27

std::array_fill(val: anytype, n: int64) -> array<anytype>

Make a new array of specified size and filled with specified value.

Create anarray of size n where every element has the value val.

  1. db>
  1. select array_fill(0, 5);
  1. {[0, 0, 0, 0, 0]}
  1. db>
  1. select array_fill('n/a', 3);
  1. {['n/a', 'n/a', 'n/a']}

function

array_replace()

Arrays - 图28

Arrays - 图29

Arrays - 图30

std::array_replace(array: array<anytype>, old: anytype, new: anytype) -> array<anytype>

Return an array where all occurrences of a particualr value are replaced.

Return an array where every old value is replaced with new.

  1. db>
  1. select array_replace([1, 1, 2, 3, 5], 1, 99);
  1. {[99, 99, 2, 3, 5]}
  1. db>
  1. select array_replace(['h', 'e', 'l', 'l', 'o'], 'l', 'L');
  1. {['h', 'e', 'L', 'L', 'o']}