共用方式為


ARITHMETIC_OVERFLOW錯誤類別

SQLSTATE:22003

<message>.<alternative> 如有必要,請將 設定 <config> 為 「false」 以略過此錯誤。

參數

  • message:造成溢位之運算式的描述。
  • 替代:建議如何避免錯誤。
  • config:要改變 ANSI 模式的組態設定。

解釋

當 Azure Databricks 執行超過執行作業之資料類型最大範圍的數學運算時,就會發生算術溢位。

在許多情況下,數學是在運算子運算元的最小通用類型中執行,或是函式引數的最小通用類型。

新增兩個型 TINYINT 別數目可能會快速超過限制為 -128+127 的類型範圍。 和 等其他 TIMESTAMPINTERVAL 類型也有大型但有限的範圍。

如需類型定義域的定義,請參閱 資料類型的定義

緩解

此錯誤的風險降低取決於原因:

  • 數學或任何輸入引數是否不正確?

    適當地更正所使用的函式或輸入資料。

    您也可以考慮重新排序作業,以將中繼結果保留在所需的範圍內。

  • 資料類型不是最寬的類型嗎?

    將其中一個引數轉換成足以完成作業的類型,以擴展類型。

    DOUBLE選擇或 DECIMAL(38, s) 搭配適當的 s 提供許多範圍,但代價是舍入。

  • 您是否可以容許溢位條件,並將其取代為 NULL

    變更運算式以使用 中建議的 alternative 函式。 例如,使用 try_sum 而不是 sum

  • 您無法變更運算式,而您想要取得包裝的結果,而不是傳回錯誤?

    最後一個方法是將 設定 ansiConfigfalse 來停用 ANSI 模式。

例子

-- An overflow of a small numeric
> SELECT 100Y * 100Y;
 [ARITHMETIC_OVERFLOW] 100S * 100S caused overflow.
 If necessary set ansi_mode to "false" (except for ANSI interval type) to bypass this error.

-- Use a wider numeric to perform the operation by casting one of the operands
> SELECT 100Y * cast(100Y AS INTEGER);
 10000

-- An overflow of a complex expression which can be rewritten
> SELECT 100Y * 10Y / 5;
 [ARITHMETIC_OVERFLOW] 100S * 10S caused overflow.
 If necessary set spark.sql.ansi.enabled to "false" (except for ANSI interval type) to bypass this error.

-- Rewrite the expression
> SELECT 100Y / 5 * 10Y;
 200.0

-- An occasional overfklow that should be tolerated
> SELECT arg1 * arg2 FROM VALUES(100Y, 100Y), (20Y, 5Y) AS t(arg1, arg2);
 [ARITHMETIC_OVERFLOW] 100S * 100S caused overflow.
 If necessary set ansi_mode to "false" (except for ANSI interval type) to bypass this error.

-- Allowing overflows to be treated as NULL
> SELECT try_multiply(arg1, arg2) FROM VALUES(100Y, 100Y), (20Y, 5Y) AS t(arg1, arg2);
  NULL
  100

-- In Databricks SQL temporarily disable ANSI mode to tolerate incorrect overflow.
> SET ANSI_MODE = false;
> SELECT arg1 * arg2 FROM VALUES(100Y, 100Y), (20Y, 5Y) AS t(arg1, arg2);
  16
  100
> SET ANSI_MODE = true;

-- In Databricks Runtime temporarily disable ANSI mode to tolerate incorrect overflow.
> SET spark.sql.ansi.enabled = false;
> SELECT arg1 * arg2 FROM VALUES(100Y, 100Y), (20Y, 5Y) AS t(arg1, arg2);
  16
  100
> SET spark.sql.ansi.enabled = true;