ARITHMETIC_OVERFLOW 错误状态

SQLSTATE:22003

<message><alternative> 如有必要,请将 <config> 设置为“false”以绕过此错误。

参数

  • 消息:导致溢出的表达式的说明。
  • 替代方法:建议如何避免此错误。
  • 配置:用于更改 ANSI 模式的配置设置。

说明

当 Azure Databricks 执行一个数学运算时,如果该运算超过了数据类型的最大范围,就会发生算术溢出。

在许多情况下,数学在运算符的作数的最小常见类型中执行,或函数参数的最小常见类型。

添加两个类型为 TINYINT 的数字可以快速超出类型范围,即从 -128+127 的范围。 其他类型,比如 TIMESTAMPINTERVAL ,也有一个大但有限的范围。

有关类型域的定义,请参阅 数据类型的定义

缓解措施

此错误的缓解措施取决于原因:

  • 数学或任何输入参数是否不正确?

    根据需要更正使用的函数或输入数据。

    还可以考虑重新排序作,使中间结果保持在所需范围内。

  • 数据类型不是最宽的类型吗?

    通过将其中一个参数强制转换为足以完成操作的类型来扩大类型。

    选择带有合适 sDOUBLEDECIMAL(38, s),虽然会有舍入误差,但能提供很大的取值范围。

  • 你能容忍溢出条件并将其替换为 NULL吗?

    更改表达式以使用所建议的 alternative函数。 例如,使用 try_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;
 1.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;