Share via


INVALID_ARRAY_INDEX_IN_ELEMENT_AT 오류 클래스

SQLSTATE: 22003

인덱 <indexValue> 스가 범위를 벗어났습니다. 배열에는 요소가 있습니다 <arraySize> . 를 사용하여 try_element_at 잘못된 인덱스에서 요소에 액세스하는 것을 허용하고 대신 NULL을 반환합니다. 필요한 경우 이 오류를 무시하려면 "false"로 설정합니다 <ansiConfig> .

매개 변수

  • indexValue: 배열에 요청된 인덱스입니다.
  • arraySize: 배열의 카디널리티입니다.
  • ansiConfig: ANSI 모드를 변경하는 구성 설정입니다.

설명

indexValueelement_at(arrayExpr, indexValue) 또는 elt(arrayExpr, indexValue) 식에 대해 정의된 배열 요소의 경계를 초과합니다.

값은 및 arraySize사이 -arraySize 여야 합니다( 제외)0.

완화 방법

이 오류의 완화는 원인에 따라 달라집니다.

  • 배열의 카디널리티가 예상보다 작습니까?

    입력 배열을 수정하고 쿼리를 다시 실행합니다.

  • 잘못 계산되었나요 indexValue ?

    쿼리를 조정 indexValue 하고 다시 실행합니다.

  • 인덱스의 카디널리티 외부 요소에 대해 반환되는 값을 가져올 NULL 것으로 예상되나요?

    식을 변경할 수 있는 경우 try_element_at(arrayExpr, indexValue) 를 사용하여 바인딩되지 않은 참조를 허용합니다.

    식을 변경할 수 없는 경우 마지막 수단으로 를 로 일시적으로 설정 ansiConfigfalse 하여 참조가 바인딩되지 않도록 허용합니다.

-- An INVALID_ARRAY_INDEX_IN_ELEMENT_AT error because of mismatched indexing
> SELECT element_at(array('a', 'b', 'c'), index) FROM VALUES(1), (4) AS T(index);
  [INVALID_ARRAY_INDEX_IN_ELEMENT_AT] The index 4 is out of bounds. The array has 3 elements. If necessary set "ANSI_MODE" to false to bypass this error.

-- Increase the aray size to cover the index
> SELECT element_at(array('a', 'b', 'c', 'd'), index) FROM VALUES(1), (4) AS T(index);
  a
  d

-- Adjusting the index to match the array
> SELECT element_at(array('a', 'b', 'c'), index) FROM VALUES(1), (3) AS T(index);
  a
  c

-- Tolerating out of bound array index with adjustment to 1-based indexing
> SELECT try_element_at(array('a', 'b', 'c'), index) FROM VALUES(1), (4) AS T(index);
  a
  NULL

-- Tolerating out of bound by setting ansiConfig in Databricks SQL
> SET ANSI_MODE = false;
> SELECT element_at(array('a', 'b', 'c'), index) FROM VALUES(1), (4) AS T(index);
  a
  NULL
> SET ANSI_MODE = true;

-- Tolerating out of bound by setting ansiConfig in Databricks Runtime
> SET spark.sql.ansi.enabled = false;
> SELECT element_at(array('a', 'b', 'c'), index) FROM VALUES(1), (4) AS T(index);
  a
  NULL
> SET spark.sql.ansi.enabled = true;