共用方式為


ARITHMETIC_OVERFLOW錯誤類別

SQLSTATE: 22003

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

參數

  • 訊息:造成溢位的表達式描述。
  • 替代:建議如何避免錯誤。
  • 配置:用於改變 ANSI 模式的配置設定。

解釋

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

在許多情況下,數學是以運算元操作數的最小通用類型執行,或是函式自變數的最小通用型別。

將兩個 TINYINT 類型的數字相加時,很快就會超出從 -128+127的類型範圍。 其他類型,例如 TIMESTAMPINTERVAL 也有較大但有限的範圍。

如需型別定義域的定義,請參閱 數據類型的 定義。

緩解

此錯誤的緩解措施取決於其原因:

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

    請視情況更正使用的函式或輸入數據。

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

  • 數據類型不是最寬的類型嗎?

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

    選擇具有適當 DOUBLEDECIMAL(38, s)s,能以捨入作為代價提供廣泛的範圍。

  • 您可以容忍溢位條件,並以 NULL取代它們嗎?

    將表達式變更為使用 alternative中建議的函式。 例如,使用 try_sum,而非 sum

  • 您無法變更表達式,您希望取得封裝結果,而不是回傳錯誤?

    作為最後手段,將 ansiConfig 設定為 false來停用 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;