Amazon Redshift는 패치 198부터 새 Python UDF 생성을 더 이상 지원하지 않습니다. 기존 Python UDF는 2026년 6월 30일까지 계속 작동합니다. 자세한 내용은 블로그 게시물
ARRAY_POSITION 함수
배열에서 지정된 요소의 첫 번째 발생 위치(인덱스)를 반환합니다. 인덱스는 0 기반입니다. 여기서 0은 첫 번째 요소를 나타내고 1은 두 번째 요소를 나타냅니다. 배열에서 요소를 찾을 수 없는 경우 -1을 반환합니다.
함수는 첫 번째 발생 위치만 반환합니다. 모든 발생 항목을 찾으려면 ARRAY_POSITIONS 함수 함수 사용을 고려하세요.
구문
ARRAY_POSITION( array, value [, null_match] )
인수
- 배열
-
검색할 배열을 지정하는 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)