バージョンごとのドキュメント一覧

9.19. 配列関数と演算子 #

<title>Array Functions and Operators</title>

<xref linkend="array-operators-table"/> shows the specialized operators available for array types. In addition to those, the usual comparison operators shown in <xref linkend="functions-comparison-op-table"/> are available for arrays. The comparison operators compare the array contents element-by-element, using the default B-tree comparison function for the element data type, and sort based on the first difference. In multidimensional arrays the elements are visited in row-major order (last subscript varies most rapidly). If the contents of two arrays are equal but the dimensionality is different, the first difference in the dimensionality information determines the sort order. 表 9.53に、配列型専用に利用可能な演算子を示します。 これらに加えて表 9.1で示す通常の比較演算子が配列で利用できます。 比較演算子は配列の内容をその要素のデータ型用のデフォルトのB-tree比較関数を要素単位で比較し、最初にどの要素に違いがあったかに基づいてソートします。 多次元配列では配列の要素は行優先順にアクセスされます。(最後の添字が最初に変化します。) 2つの配列の内容が同じで次元数が異なる場合は、どの次元で最初に違いがあったかによってソート順が決まります。

表9.53 配列演算子

<title>Array Operators</title>

Operator 演算子

Description 説明

Example(s)

anyarray @> anyarrayboolean

Does the first array contain the second, that is, does each element appearing in the second array equal some element of the first array? (Duplicates are not treated specially, thus <literal>ARRAY[1]</literal> and <literal>ARRAY[1,1]</literal> are each considered to contain the other.) 最初の配列が2番目を含んでいるか?すなわち、2番目の配列の各要素は最初の配列のいくつかの要素と同じであるか? (重複は特に考慮されないので、ARRAY[1]ARRAY[1,1]はそれぞれがお互いに相手を含んでいると見なされます。)

ARRAY[1,4,3] @> ARRAY[3,1,3]t

anyarray <@ anyarrayboolean

Is the first array contained by the second? 最初の配列は2番目に含まれているか?

ARRAY[2,2,7] <@ ARRAY[1,7,4,2,6]t

anyarray && anyarrayboolean

Do the arrays overlap, that is, have any elements in common? 配列は重なり合っているか?すなわち、共通の要素を持っているか?

ARRAY[1,4,3] && ARRAY[2,1]t

anycompatiblearray || anycompatiblearrayanycompatiblearray

Concatenates the two arrays. Concatenating a null or empty array is a no-op; otherwise the arrays must have the same number of dimensions (as illustrated by the first example) or differ in number of dimensions by one (as illustrated by the second). If the arrays are not of identical element types, they will be coerced to a common type (see <xref linkend="typeconv-union-case"/>). 2つの配列を結合します。 nullあるいは空の配列の結合は無処理です。そうでない場合は、配列は同じ次元数を持っていなければなりません。 (最初の例にあるように)。さもなければ次元数でひとつ違わなければなりません(2番目の例にあるように)。 配列の要素型が異なる場合は、共通の型へと置き換えられます(10.5参照)。

ARRAY[1,2,3] || ARRAY[4,5,6,7]{1,2,3,4,5,6,7}

ARRAY[1,2,3] || ARRAY[[4,5,6],[7,8,9.9]]{{1,2,3},{4,5,6},{7,8,9.9}}

anycompatible || anycompatiblearrayanycompatiblearray

Concatenates an element onto the front of an array (which must be empty or one-dimensional). 配列(空か一次元の配列でなければなりません)の先頭に要素を結合します。

3 || ARRAY[4,5,6]{3,4,5,6}

anycompatiblearray || anycompatibleanycompatiblearray

Concatenates an element onto the end of an array (which must be empty or one-dimensional). 配列(空か一次元の配列でなければなりません)の最後に要素を結合します。

ARRAY[4,5,6] || 7{4,5,6,7}


See <xref linkend="arrays"/> for more details about array operator behavior. See <xref linkend="indexes-types"/> for more details about which operators support indexed operations. 配列演算子の振舞いの詳細は8.15を参照してください。 どの演算子がインデックス付きの操作をサポートしているかのより詳細については11.2を参照してください。

<xref linkend="array-functions-table"/> shows the functions available for use with array types. See <xref linkend="arrays"/> for more information and examples of the use of these functions. 表 9.54に配列型で使用可能な関数を示します。 これらの関数の情報と例については8.15を参照してください。

表9.54 配列関数

<title>Array Functions</title>

Function 関数

Description 説明

Example(s)

array_append ( anycompatiblearray, anycompatible ) → anycompatiblearray

Appends an element to the end of an array (same as the <type>anycompatiblearray</type> <literal>||</literal> <type>anycompatible</type> operator). 配列の最後に要素を追加します。(anycompatiblearray || anycompatible演算子と同じです。)

array_append(ARRAY[1,2], 3){1,2,3}

array_cat ( anycompatiblearray, anycompatiblearray ) → anycompatiblearray

Concatenates two arrays (same as the <type>anycompatiblearray</type> <literal>||</literal> <type>anycompatiblearray</type> operator). 2つの配列を結合します。(anycompatiblearray || anycompatiblearray演算子と同じです。)

array_cat(ARRAY[1,2,3], ARRAY[4,5]){1,2,3,4,5}

array_dims ( anyarray ) → text

Returns a text representation of the array's dimensions. 配列の次元をテキスト表現で返します。

array_dims(ARRAY[[1,2,3], [4,5,6]])[1:2][1:3]

array_fill ( anyelement, integer[] [, integer[] ] ) → anyarray

Returns an array filled with copies of the given value, having dimensions of the lengths specified by the second argument. The optional third argument supplies lower-bound values for each dimension (which default to all <literal>1</literal>). 与えられた値のコピーで満たされた2番目の引数で指定した次元の長さを持つ配列を返します。 オプションの3番目の引数は各次元の下限値を与えます(デフォルトはすべて1です)。

array_fill(11, ARRAY[2,3]){{11,11,11},{11,11,11}}

array_fill(7, ARRAY[3], ARRAY[2])[2:4]={7,7,7}

array_length ( anyarray, integer ) → integer

Returns the length of the requested array dimension. (Produces NULL instead of 0 for empty or missing array dimensions.) 要求された配列の次元の大きさを返します。 (空、あるいは配列の次元が見つからない場合は0ではなくNULLを生成します。)

array_length(array[1,2,3], 1)3

array_length(array[]::int[], 1)NULL

array_length(array['text'], 2)NULL

array_lower ( anyarray, integer ) → integer

Returns the lower bound of the requested array dimension. 要求された配列の次元の下限を返します。

array_lower('[0:2]={1,2,3}'::integer[], 1)0

array_ndims ( anyarray ) → integer

Returns the number of dimensions of the array. 配列の次元数を返します。

array_ndims(ARRAY[[1,2,3], [4,5,6]])2

array_position ( anycompatiblearray, anycompatible [, integer ] ) → integer

Returns the subscript of the first occurrence of the second argument in the array, or <literal>NULL</literal> if it's not present. If the third argument is given, the search begins at that subscript. The array must be one-dimensional. Comparisons are done using <literal>IS NOT DISTINCT FROM</literal> semantics, so it is possible to search for <literal>NULL</literal>. 2番目の引数が最初に配列に現れた添字を返します。存在しなければNULLを返します。 3番目の引数が与えられるとその添字から検索が始まります。 配列は一次元でなければなりません。 比較はIS NOT DISTINCT FROMの意味論で行われるので、NULLを検索することができます。

array_position(ARRAY['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'], 'mon')2

array_positions ( anycompatiblearray, anycompatible ) → integer[]

Returns an array of the subscripts of all occurrences of the second argument in the array given as first argument. The array must be one-dimensional. Comparisons are done using <literal>IS NOT DISTINCT FROM</literal> semantics, so it is possible to search for <literal>NULL</literal>. <literal>NULL</literal> is returned only if the array is <literal>NULL</literal>; if the value is not found in the array, an empty array is returned. 2番目の引数が配列に現れるすべての添字を配列で返します。存在しなければNULLを返します。 3番目の引数が与えられるとその添字から検索が始まります。 配列は一次元でなければなりません。 比較はIS NOT DISTINCT FROMの意味論で行われるので、NULLを検索することができます。 配列がNULLのときのみNULLが返ります。 値が配列中に見つからなければ空の配列が返ります。

array_positions(ARRAY['A','A','B','A'], 'A'){1,2,4}

array_prepend ( anycompatible, anycompatiblearray ) → anycompatiblearray

Prepends an element to the beginning of an array (same as the <type>anycompatible</type> <literal>||</literal> <type>anycompatiblearray</type> operator). 配列の先頭に要素を追加します。(anycompatible || anycompatiblearray演算子と同じです。)

array_prepend(1, ARRAY[2,3]){1,2,3}

array_remove ( anycompatiblearray, anycompatible ) → anycompatiblearray

Removes all elements equal to the given value from the array. The array must be one-dimensional. Comparisons are done using <literal>IS NOT DISTINCT FROM</literal> semantics, so it is possible to remove <literal>NULL</literal>s. 与えられた値と等しい要素を配列から削除します。 配列は一次元でなければなりません。 比較はIS NOT DISTINCT FROMの意味論で行われるので、NULLを削除することができます。

array_remove(ARRAY[1,2,3,2], 2){1,3}

array_replace ( anycompatiblearray, anycompatible, anycompatible ) → anycompatiblearray

Replaces each array element equal to the second argument with the third argument. 2番目の引数と等しい要素を3番目の引数で置き換えます。

array_replace(ARRAY[1,2,5,4], 5, 3){1,2,3,4}

array_sample ( array anyarray, n integer ) → anyarray

Returns an array of <parameter>n</parameter> items randomly selected from <parameter>array</parameter>. <parameter>n</parameter> may not exceed the length of <parameter>array</parameter>'s first dimension. If <parameter>array</parameter> is multi-dimensional, an <quote>item</quote> is a slice having a given first subscript. arrayからランダムに選択されたn個のアイテムの配列を返します。 narrayの最初の次元の長さを超えることはできません。 arrayが多次元の場合、itemは指定された最初の添字を持つスライスです。

array_sample(ARRAY[1,2,3,4,5,6], 3){2,6,1}

array_sample(ARRAY[[1,2],[3,4],[5,6]], 2){{5,6},{1,2}}

array_shuffle ( anyarray ) → anyarray

Randomly shuffles the first dimension of the array. 配列の1次元目をランダムにシャッフルします。

array_shuffle(ARRAY[[1,2],[3,4],[5,6]]){{5,6},{1,2},{3,4}}

array_to_string ( array anyarray, delimiter text [, null_string text ] ) → text

Converts each array element to its text representation, and concatenates those separated by the <parameter>delimiter</parameter> string. If <parameter>null_string</parameter> is given and is not <literal>NULL</literal>, then <literal>NULL</literal> array entries are represented by that string; otherwise, they are omitted. See also <link linkend="function-string-to-array"><function>string_to_array</function></link>. 配列要素をテキスト表現に変換しdelimiter文字列で区切って結合します。 NULLでないnull_stringが与えられると、NULL配列要素をその文字列で表現します。さもなければ無視されます。 string_to_arrayも参照してください。

array_to_string(ARRAY[1, 2, 3, NULL, 5], ',', '*')1,2,3,*,5

array_upper ( anyarray, integer ) → integer

Returns the upper bound of the requested array dimension. 要求された配列の次元の上限を返します。

array_upper(ARRAY[1,8,3,7], 1)4

cardinality ( anyarray ) → integer

Returns the total number of elements in the array, or 0 if the array is empty. 配列中の要素数を返します。配列が空なら0が返ります。

cardinality(ARRAY[[1,2],[3,4]])4

trim_array ( array anyarray, n integer ) → anyarray

Trims an array by removing the last <parameter>n</parameter> elements. If the array is multidimensional, only the first dimension is trimmed. 最後のn要素を削除して配列を短縮します。 配列が複数次元なら、最初の次元だけが短縮されます。

trim_array(ARRAY[1,2,3,4,5,6], 2){1,2,3,4}

unnest ( anyarray ) → setof anyelement

Expands an array into a set of rows. The array's elements are read out in storage order. 配列を行の集合に展開します。 配列要素は格納順に読み出されます。

unnest(ARRAY[1,2])

 1
 2

unnest(ARRAY[['foo','bar'],['baz','quux']])

 foo
 bar
 baz
 quux

unnest ( anyarray, anyarray [, ... ] ) → setof anyelement, anyelement [, ... ]

Expands multiple arrays (possibly of different data types) into a set of rows. If the arrays are not all the same length then the shorter ones are padded with <literal>NULL</literal>s. This form is only allowed in a query's FROM clause; see <xref linkend="queries-tablefunctions"/>. 複数の配列(異なるデータ型の可能性があります)を行の集合に展開します。 配列の長さが同じでなければ、短い配列にはNULLが詰められます。 これは問い合わせのFROM句でのみ許されます。7.2.1.4を参照してください。

select * from unnest(ARRAY[1,2], ARRAY['foo','bar','baz']) as x(a,b)

 a |  b
---+-----
 1 | foo
 2 | bar
   | baz


See also <xref linkend="functions-aggregate"/> about the aggregate function <function>array_agg</function> for use with arrays. 配列を使用する集約関数array_aggについて、9.21も参照してください。