Amazon Redshift は、パッチ 198 以降、新しい Python UDF の作成をサポートしなくなります。既存の Python UDF は、2026 年 6 月 30 日まで引き続き機能します。詳細については、ブログ記事
ARRAY_POSITION 関数
配列内の指定された要素の最初の出現の位置 (インデックス) を返します。インデックスは 0 ベースで、0 は最初の要素を示し、1 は 2 番目の要素を示します。配列内に要素が見つからない場合は、-1 を返します。
この関数は、最初に出現した位置のみを返します。すべての出現箇所を検索するには、ARRAY_POSITIONS 関数 関数の使用を検討してください。
構文
ARRAY_POSITION( array, value [, null_match] )
引数
- array
-
検索する配列を指定する SUPER 式。
- 値
-
検索する要素を指定する値。
- null_match
-
NULL 値の処理方法を指定するブール値:
- null_match = FALSE: Searching for NULL returns NULL. If the array contains NULL values and no match is found for a non-NULL search value, returns NULL.
- null_match = TRUE: NULLs are treated as valid, searchable elements. If the array contains NULL values and no match is found for a non-NULL search value, it returns -1.
デフォルトは TRUE です。
デフォルトの NULL 処理は、設定オプションで指定することもできます。
-- same as null_match = TRUE SET default_array_search_null_handling to TRUE;
戻り型
ARRAY_POSITION 関数は INT 型を返します。
例
次の例は、ARRAY_POSITION 関数を示しています。
SELECT ARRAY_POSITION(ARRAY('red', 'green'), 'red'); array_position ---------------- 0 (1 row) SELECT ARRAY_POSITION(ARRAY(1, 2, 3), 4); array_position ---------------- -1 (1 row) -- only the position of the first occurrence is returned SELECT ARRAY_POSITION(ARRAY('red', 'green', 'red'), 'red'); array_position ---------------- 0 (1 row)
次の例は、null_match を TRUE に設定した場合の関数の動作を示しています。
SET default_array_search_null_handling to TRUE; -- NULL search is enabled SELECT ARRAY_POSITION(ARRAY('red', NULL, 'green'), NULL); array_position ---------------- 1 (1 row) -- The array can contain NULLs SELECT ARRAY_POSITION(ARRAY('red', NULL, 'green'), 'blue', TRUE); array_position ---------------- -1 (1 row)
次の例は、null_match を FALSE に設定した場合の関数の動作を示しています。関数内で null_match 動作を指定すると、デフォルト設定が上書きされることに注意してください。
-- same as null_match = TRUE SET default_array_search_null_handling to TRUE; -- NULL search is disabled. The default behavior is overridden SELECT ARRAY_POSITION(ARRAY('red', 'green'), NULL, FALSE); array_position ---------------- (1 row) -- same as null_match = FALSE SET default_array_search_null_handling to FALSE; -- The array contains NULL and a match is found SELECT ARRAY_POSITION(ARRAY('red', NULL, 'green'), 'green'); array_position ---------------- 2 (1 row) -- The array contains NULL but no match is found SELECT ARRAY_POSITION(ARRAY('red', NULL, 'green'), 'blue'); array_position ---------------- (1 row)