Skip to content

[Bug]: POW() with negative base and fractional exponent silently returns NULL (MySQL raises 1690) #31155

Closed

Description

@Ariznawlll

Description

POW() / POWER() with a negative base and a non-integer exponent returns a silent NULL instead of raising the MySQL-compatible out-of-range error. This is a regression of #25308: that issue was closed in July 2026 when the value changed from NaN to NULL, but MySQL actually raises an error, so the compatibility gap is still open (silent NULL is arguably worse than NaN because it hides domain errors entirely).

Environment

  • MatrixOne dev build (local multi-CN, main)
  • MySQL 8.0.45 control

Reproduction

SELECT pow(-2, 0.5);
-- MO   : NULL
-- MySQL: ERROR 1690 (22003): DOUBLE value is out of range in 'pow(-(2),0.5)'

Also affected:

SELECT power(-8, 1.0/3);   -- MO: NULL,  MySQL: ERROR 1690
SELECT pow(-8, 0.5);       -- MO: NULL,  MySQL: ERROR 1690

Not affected (MO already matches MySQL)

sqrt(-4), ln(-1), log(-1), log10(-1), log2(-1), asin(2), acos(2) all return NULL on both engines — so those are correct. Only POW/POWER diverges.

Root cause

The July fix for #25308 stopped producing NaN but replaced it with NULL, i.e. the code now maps an out-of-domain math.Pow result to a NULL vector element instead of surfacing an error. MySQL treats a negative base with a fractional exponent as an out-of-range DOUBLE error (1690), not a NULL.

Impact

  • Silent NULL propagation: pow(-2, 0.5) inside a larger expression silently nulls the whole expression instead of failing loudly, so data-quality problems and user typos go unnoticed.
  • Porting MySQL queries/tests that expect error 1690 fails.
  • POW is used for compound-interest / growth / distance style calculations where a negative base with fractional exponent is a genuine domain bug in the query — erroring is the correct behavior.

Expected behavior

SELECT pow(-2, 0.5);
-- ERROR 1690 (22003): DOUBLE value is out of range in 'pow(-(2),0.5)'

i.e. raise errno 1690 the same way MySQL does, and keep the sqrt/ln/log/asin/acos NULL behavior unchanged (those match MySQL).

Activity

  1. added
    kind/bug
    on Oct 26, 2026
  2. added this to the MO-202610 milestone on Oct 26, 2026
  3. added theissue type on Oct 26, 2026
  4. assigned and unassigned on Nov 8, 2026
  5. added 2 commits that reference this issue on Nov 18, 2026
    12cc8ac
    ddab42c
  6. XuPeng-SH commented on Nov 19, 2026

    @XuPeng-SH
    Contributor

    转 QA 验证,不代表验收通过,保持 issue 开放。

    #32863 已于 2026-11-19 03:26:36 UTC 合入 main,merge SHA 793b62b5b51e92a07b87890f5a94565e376ec58d,最终 head 0a3d2c2cf5a6c9286cbfd74c6c9d2b9f8ae5fcbc。有限负底数与有限非整数指数的 POW/POWER 在实际求值行返回 typed DOUBLE out-of-range(MySQL 1690 / SQLSTATE 22003),不再静默 NULL;NULL、空批次、未选中/屏蔽行及其他溢出等既有 NULL 语义保留。

    证据按来源区分:

    • 作者报告 focused Power、真实 prepared-TP 执行器错误后复用的 race 测试通过;旧 base 43addc1 上真实 wire 1690/22003、同连接恢复及 111/111 BVT(nometa 与严格 metadata)通过。这不是最终 merge 二进制的实测。
    • reviewer 在 4061503845f25d7523c152cd6bcae87a2005a477 报告单 CN embedded MySQL 协议 probe:两种函数别名、空/过滤输入、未激活 IF 分支、NULL/overflow 对照、prepared 错误后恢复通过;五个相关包 focused race 通过。多 CN BVT 通过仅来自其冻结 CI 状态,未取得原始 CI 日志。
    • 最终 head 后续合并了 main 的 perf(exec): reduce accounted work on short index range scans #32895;上述 review 不是最终 head 重新运行的证明。合并队列曾因 fork 编辑权限退出,不能据此宣称最终 CI 全绿。本轮只核对远端范围、评论、评审和代码,未自行运行产品测试。

    PR 同时包含 GC catalog delete timestamp 读锁修复及测试夹具调整;#31401 cleanup 只新增诊断,失败根因仍未知,不能算作已解决。HAKeeper stall 根因也未确定,不将其与 POW 修复混为一谈。

    请 QA 在包含上述 merge 的 main 确认原始 pow(-2,0.5)、power(-8,1.0/3)、power(-8,0.5) 的错误码/SQLSTATE;覆盖常量/向量、NULL、整数指数、空结果、IF/CASE 屏蔽、DECIMAL 转换、prepared 连续错误/恢复、DML 失败原子性,并检查 sqrt/ln/log/asin/acos 及 overflow/EXP/COT 的既有行为不回退。最终版本多 CN、并发、长稳与生产环境边界仍待验证,不自动要求 backport。

    完整 issue 当前无其他评论;未见适用于 POW 修复的已验证反例或未完成开发 follow-up。移除原研发负责人,交唯一 @Ariznawlll 继续验证。

  7. Ariznawlll commented on Nov 20, 2026

    @Ariznawlll
    CollaboratorAuthor

    QA 验收 2026-11-20:实际通过,关闭

    验证环境

    • 隔离单 CN 集群(LogService 33301 / TN 34300 / CN 17304),max_dop=1 默认。
    • 运行实例实测 git_version()=43fa9f4(version()=8.0.30-MatrixOne-v1.3.0),源码树 HEAD 43fa9f4329(origin/main)。修复 PR fix(function): reject undefined real-domain POW arguments #32863 merge commit 793b62b5b51e92a07b87890f5a94565e376ec58d 经 git merge-base --is-ancestor 验证为该构建的祖先(修复前构建 035af99680 验证过不含该修复、问题确实存在于其中,构成修复前对照)。

    原始问题复现与修复验证

    修复前(035af99680,2026-11-20 实测):pow(-2,0.5)、power(-8,0.5)、power(-8,1.0/3) 均静默返回 NULL。

    修复后(43fa9f4,同一套隔离环境实测):

    SQL 结果 MySQL 期望
    select pow(-2, 0.5) ERROR 1690 (22003) DOUBLE value is out of range in 'pow(-2,0.5)' ERROR 1690 ✓
    select power(-8, 0.5) ERROR 1690 ✓ ERROR 1690 ✓
    select power(-8, 1.0/3) ERROR 1690 ✓(错误文本含化简后的 pow(-8,0.33333)) ERROR 1690 ✓
    select pow(-2, -0.5) ERROR 1690 ✓ ERROR 1690 ✓
    select power(-2.5, 0.001) ERROR 1690 ✓ ERROR 1690 ✓
    向量行 select pow(v,2.5) from (select -2 as v union all select 3) t ERROR 1690 ✓(行级触发) ERROR 1690 ✓
    DML insert into t values(pow(-2,0.5)) ERROR 1690 ✓ ERROR 1690 ✓

    保护面回归(Happy/Unhappy path 均实测)

    • 定义域内计算正确:pow(-2,3)=-8、pow(-8,3.0)=-512(整数浮点指数不触发)、pow(2,0.5)、pow(-2,2.0)=4、pow(0,0)=1、pow(-2,-3)=-0.125。
    • fix: return NULL for POWER EXP and COT arithmetic failures #28375 契约保持不变:pow(2,1024) 溢出仍为 NULL、pow(0,-1) NULL、sqrt(-4)/ln(-1)/log2(-1)/asin(2)/acos(2) 仍为 NULL,与 MySQL 一致。
    • 折叠短路保持:select if(0, pow(-2,0.5), 1)=1、case when 0 then power(0,-1) else 1 end=1(未路由分支不触发域错误)。
    • 表内正常行不受影响:select v, pow(v,3) 对 (-2,3,-8,2) 全部正确输出。

    白盒 Go 单测

    go test ./pkg/sql/plan/function/ -run 'TestPower' -count=1:ok。TestPower、TestPowerNonDomainFailuresReturnNull/{vector_continues_after_overflow_row, vector_base_with_constant_exponent, overflow_underflow_and_input_null, nan_and_inf_inputs_stay_row-local_null}、TestPowerNegativeFractionalExponentReturnsOutOfRange/... 全部 PASS(即在修复代码所在包上直接验证域拒绝与 NULL 契约并存)。

    motr 回归用例(main-only)

    未验证边界(如实声明)

    • 极大指数 pow(-2, 1e300) 返回 NULL 而非 1690——该形状 math.Trunc 判定下指数非有限域错误路径,与修复 PR 声明的契约(!IsInf 守卫优先)一致,MySQL 同入 NULL/无值情形,不作为缺陷记录。
    • POWER 与 POW 仅验证别名一致性 pow(2,3)=power(2,3),未对全部别名重复 1690 断言(同实现路径)。
    • 性能(修复后错误路径开销)未测量——该修复是语义修复,无性能声明。
  8. ck89119 commented on Nov 24, 2026

    @ck89119
    Contributor

    The November 20 QA acceptance above records the 1690 behavior delivered by #32863. For finite negative bases and finite non-integer exponents, #33126 supersedes that expectation with row-local SQL NULL for both POW and POWER. The implementation is in draft PR #33147. Builds without that change can still exhibit the accepted 1690 behavior.

    The main-only MOTR regression and its golden in matrixorigin/motr#358 have been updated at 5a7d8a8. It now checks scalar NULL results, continuation across mixed rows, and storage into a nullable column. The fifteen finite-value, overflow, math-function and short-circuit controls remain covered.

    This also affects existing CHECK constraints. In the reviewer's shutdown-upgrade test from v4.2.9, the same table with CHECK (POWER(b, 0.5) > 0) rejected -2 with 1690 before upgrade and admitted it after upgrade because the predicate was UNKNOWN. It rejected 0 with 3819 on both versions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugneeds-triage

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions