Skip to content

[Bug]: flow-control functions evaluate skipped branches instead of short-circuiting like MySQL #25314

Closed

Description

@Ariznawlll

Bug Report

MatrixOne Version

Latest origin/main verified locally at 200bfa7e0efb.

Reproduce Steps

set session sql_mode = '';

select if(0, cast('bad' as signed), 7) as if_skip_true_branch;

select case when 0 then cast('bad' as signed) else 7 end as case_skip_then;

select coalesce(5, cast('bad' as signed)) as coalesce_skip_later;

select ifnull(5, cast('bad' as signed)) as ifnull_skip_later;

Expected Behavior

MySQL returns 7, 7, 5, and 5; skipped branches/later fallback expressions do not make the statement fail.

if_skip_true_branch
7

case_skip_then
7

coalesce_skip_later
5

ifnull_skip_later
5

Actual Behavior

MatrixOne errors on the skipped expression path:

ERROR 20203 (HY000): invalid argument cast to int, bad value bad

Notes

Found during MySQL compatibility Module 7 exploration. This affects IF, CASE, COALESCE, and IFNULL when an unused branch contains an expression that would be invalid if evaluated or coerced eagerly.

Activity

  1. added
    kind/bug
    severity/s1
    on Jul 1, 2026
  2. added
    severity/s0
    and removed
    severity/s1
    on Jul 2, 2026
  3. added this to the MO-202607 milestone on Jul 2, 2026
  4. added theissue type on Jul 2, 2026
  5. added and removed
    severity/s0
    on Jul 2, 2026
  6. added
    severity/s0
    ai-easy
    and removed on Jul 15, 2026
  7. LeftHandCold commented on Jul 19, 2026

    @LeftHandCold
    Collaborator

    fixed

  8. Ariznawlll commented on Jul 20, 2026

    @Ariznawlll
    CollaboratorAuthor

    已在最新 main 分支完成验证。

    测试环境

    • 分支:main
    • Commit:826a3e3d294ba62c7c4da217d1445f3c568ce4d9
    • Commit 时间:2026-07-19 13:25:28 UTC
    • Go:go1.26.4 darwin/arm64
    • 服务版本:8.0.30-MatrixOne-v1.3.0 826a3e3d2

    测试步骤

    1. 原始 SQL 场景

    连续执行 3 轮以下查询,未选中的分支均包含会报错的 CAST('bad' AS SIGNED):

    SELECT IF(0, CAST('bad' AS SIGNED), 7);
    SELECT CASE WHEN 0 THEN CAST('bad' AS SIGNED) ELSE 7 END;
    SELECT COALESCE(5, CAST('bad' AS SIGNED));
    SELECT IFNULL(5, CAST('bad' AS SIGNED));

    2. 批量场景

    构造多行输入,分别验证 IF、CASE 和 COALESCE 在 batch shrinking/selection 变化时,仅对当前行实际选中的分支求值;未选中的 invalid cast 不应被提前计算。连续执行 3 轮,预期分别返回:

    IF batch       -> 7, 9
    CASE batch     -> 7, 9
    COALESCE batch -> 5, 9
    

    3. 代码级回归

    运行 pkg/sql/colexec 中 TestFlowControlShortCircuitInvalidCast,普通模式和 -race 模式各连续 3 轮。该用例还覆盖 invalid cast、regexp、SLEEP、参数复用、batch shrinking、partial runtime type 等路径。

    测试结果

    • 原始 4 条 SQL 连续 3 轮均返回预期值,没有执行未选中的错误分支。
    • 3 组批量 SQL 连续 3 轮均返回预期结果。
    • 定向 UT 普通模式和 -race 模式各连续 3 轮全部通过。
    • 未出现未选分支报错、额外 SLEEP、panic 或数据竞争报告。

    结论

    最新 main 上 IF、CASE、COALESCE、IFNULL 已按 MySQL 语义执行短路求值,本问题验证通过。

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

Metadata

Metadata

Assignees

Labels

ai-easykind/bugseverity/s0

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions