Condividi tramite


INVALID_ARRAY_INDEX_IN_ELEMENT_AT error condition

SQLSTATE: 22003

The index <indexValue> is out of bounds. The array has <arraySize> elements. Usare try_element_at per tollerare l'accesso all'elemento in corrispondenza dell'indice non valido e restituire invece NULL. Se necessario, impostare <ansiConfig> su "false" per ignorare questo errore.

Parameters

  • indexValue: indice richiesto nella matrice.
  • arraySize: cardinalità della matrice.
  • ansiConfig: impostazione di configurazione per modificare la modalità ANSI.

Spiegazione

indexValue is beyond the boundary of defined array elements for an element_at(arrayExpr, indexValue), or elt(arrayExpr, indexValue) expression.

Il valore deve essere compreso tra -arraySize e arraySize, escluso 0.

Mitigazione

La mitigazione per questo errore dipende dalla causa:

  • La cardinalità della matrice è inferiore al previsto?

    Correggere la matrice di input ed eseguire nuovamente la query.

  • È stato calcolato erroneamente indexValue?

    Modificare indexValue ed eseguire nuovamente la query.

  • Si prevede di ottenere un valore NULL da restituire per gli elementi al di fuori della cardinalità dell'indice?

    If you can change the expression, use try_element_at(arrayExpr, indexValue) to tolerate references out of bound.

    If you cannot change the expression, as a last resort, temporarily set the ansiConfig to false to tolerate references out of bound.

Examples

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