Compartilhar via


Condição 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 a um elemento em um índice inválido e retornar NULL em vez disso. Se necessário definido <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.

Explanation

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.

Atenuação

A mitigação desse erro depende da causa:

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

    Corrija a matriz de entrada e execute novamente a consulta.

  • Foi indexValue calculado incorretamente?

    Ajuste indexValue e execute novamente a consulta.

  • Você espera que um NULL valor 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 puder alterar a expressão, como último recurso, defina temporariamente o ansiConfig para false a fim de tolerar referências fora do limite.

Exemplos

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