The index <indexValue>
is out of bounds. 陣列具有 <arraySize>
元素。 Use try_element_at
to tolerate accessing element at invalid index and return NULL instead. 如有必要,請將 <ansiConfig>
設為 「false」 以略過此錯誤。
參數
- indexValue: The requested index into the array.
- arraySize:陣列的基數。
- ansiConfig:要改變 ANSI 模式的組態設定。
解釋
indexValue
超過 element_at(arrayExpr, indexValue)或 elt(arrayExpr, indexValue) 表達式的已定義陣列元素的範圍。
值必須介於 -arraySize
和 arraySize
之間,不包括 0
。
Mitigation
此錯誤的緩解取決於原因:
陣列的基數是否小於預期?
修正輸入陣列並重新執行查詢。
indexValue
是否計算錯誤?調整
indexValue
並重新執行查詢。您是否預期會針對索引基數以外的項目傳回
NULL
值?如果您可以變更表達式,請使用 try_element_at(arrayExpr, indexValue) 來容許超出範圍的存取。
If you cannot change the expression, as a last resort, temporarily set the
ansiConfig
tofalse
to tolerate references out of bound.
例子
-- 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;