<message>。
<alternative> 如有必要,设置为 <config> “false”以绕过此错误。
参数
- 消息:导致溢出的表达式的说明。
- 替代方法:建议如何避免此错误。
- 配置:用于更改 ANSI 模式的配置设置。
Explanation
当 Azure Databricks 执行的数学运算超出该数据类型的最大范围时,会发生算术溢出。
在许多情况下,数学在运算符的作数的最小常见类型中执行,或函数参数的最小常见类型。
添加两个类型为TINYINT的数字可能会很快超过其从-128到+127的数值范围限制。
其他类型,比如TIMESTAMP和INTERVAL,也有一个大但有限的范围。
有关类型域的定义,请参阅 数据类型的定义。
缓解措施
此错误的缓解措施取决于原因:
数学或任何输入参数是否不正确?
根据需要更正使用的函数或输入数据。
还可以考虑重新排序作,使中间结果保持在所需范围内。
数据类型不是最宽的类型吗?
通过将其中一个参数强制转换为足以完成运算的类型来扩大类型。
选择
DOUBLE或DECIMAL(38, s),搭配合适的s,可以提供很大的范围,但代价是精度的舍入。你能容忍溢出条件并将其替换为
NULL吗?您无法更改表达式,宁愿获得包装的结果而不是返回错误吗?
作为最后手段,通过将 ANSI 模式禁用并将
ansiConfig设置为false。
例子
-- 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;