Pular para o conteúdo principal

Classe de erro INVALID_ARRAY_INDEX_IN_ELEMENT_AT

SQLSTATE: 22003

O índice <indexValue> está fora dos limites. A matriz tem elementos <arraySize>. Use try_element_at para tolerar o acesso ao elemento em um índice inválido e, em vez disso, retornar NULL. Se necessário, defina <ansiConfig> como “false” para ignorar esse erro.

Parâmetros

  • indexValue : O índice solicitado na matriz.
  • ArraySize : A cardinalidade da matriz.
  • ANSIConfig : A configuração para alterar o modo ANSI.

Explicação

indexValue está além do limite de elementos de matriz definidos para uma expressão element_at (ArrayExpr, IndexValue) ou elt (ArrayExpr, IndexValue).

O valor deve estar entre -arraySize e arraySize, excluindo 0.

Mitigação

A mitigação desse erro depende da causa:

  • A cardinalidade da matriz é menor do que o esperado?

    Corrigir o array de entrada e reexecutar a consulta.

  • O site indexValue foi computado incorretamente?

    Ajuste indexValue e reexecute a consulta.

  • Você espera que um valor NULL seja retornado para elementos fora da cardinalidade do índice?

    Se você puder alterar a expressão, use try_element_at (ArrayExpr, IndexValue) para tolerar referências fora do limite.

    Se você não conseguir alterar a expressão, como último recurso, defina temporariamente ansiConfig como false para tolerar referências fora do limite.

Exemplos

SQL
-- 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;

Relacionado