Classe de erro INVALID_ARRAY_INDEX
O índice <indexValue>
está fora dos limites. A matriz tem elementos <arraySize>
. Use a função SQL get()
para tolerar o acesso ao elemento em um índice inválido e, em vez disso, retorne 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
Diferentemente de element_at e elt, uma referência indexValue
em uma matriz usando a sintaxe ArrayExpr [indexValue] deve estar entre 0
para o primeiro elemento e arraySize - 1
para o último elemento.
Um indexValue
negativo ou um valor maior ou igual a arraySize
não é permitido.
Mitigação
A mitigação desse erro depende da intenção:
-
O
indexValue
fornecido pressupõe indexação baseada em 1?Use element_at (ArrayExpr, IndexValue), elt (ArrayExpr, IndexValue) `ou ArrayExpr [IndexValue- 1] para resolver o elemento de matriz correto.
-
O negativo
indexValue
espera recuperar o elemento em relação ao final da matriz?Use element_at (ArrayExpr, IndexValue) ou elt (ArrayExpr, IndexValue)`. Ajuste para indexação baseada em 1, se necessário.
-
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 + 1) para tolerar referências fora do limite. Observe a indexação baseada em 1 para
try_element_at
.Se você não conseguir alterar a expressão, como último recurso, defina temporariamente
ansiConfig
comofalse
para tolerar referências fora do limite.
Exemplos
-- An INVALID_ARRAY_INDEX error because of mismatched indexing
> SELECT array('a', 'b', 'c')[index] FROM VALUES(1), (3) AS T(index);
[INVALID_ARRAY_INDEX] The index 3 is out of bounds. The array has 3 elements. If necessary set "ANSI_MODE" to false to bypass this error.
-- Using element_at instead for 1-based indexing
> SELECT element_at(array('a', 'b', 'c'), index) FROM VALUES(1), (3) AS T(index);
a
c
-- Adjusting the index to be 0-based
> SELECT array('a', 'b', 'c')[index -1] FROM VALUES(1), (3) AS T(index);
-- Tolerating out of bound array index with adjustment to 1-based indexing
> SELECT try_element_at(array('a', 'b', 'c'), index + 1) FROM VALUES(1), (3) AS T(index);
b
NULL
-- An INVALID_ARRAY_INDEX error because of negative index
> SELECT array('a', 'b', 'c')[index] FROM VALUES(-1), (2) AS T(index);
[INVALID_ARRAY_INDEX] The index -1 is out of bounds. The array has 3 elements. If necessary set "ANSI_MODE" to "false" to bypass this error.
-- Using element_at to index relative to the end of the array
> SELECT element_at(array('a', 'b', 'c'), index) FROM VALUES(-1), (2) AS T(index);
c
b
-- Tolerating an out of bound index by setting ansiConfig in Databricks SQL
> SET ANSI_MODE = false;
> SELECT array('a', 'b', 'c')[index] FROM VALUES(1), (3) AS T(index);
b
NULL
> SET ANSI_MODE = true;
-- Tolerating an out of bound index by setting ansiConfig in Databricks Runtime
> SET spark.sql.ansi.enabled = false;
> SELECT array('a', 'b', 'c')[index] FROM VALUES(1), (3) AS T(index);
b
NULL
> SET spark.sql.ansi.enabled = true;