Skip to content

[Bug]: CASE/IF/IFNULL/COALESCE fold numeric branches to 0/1 when another branch is BOOL #31200

Closed

Description

@MatrixAdventurer

Bug description

When a CASE, IF, IFNULL or COALESCE mixes a BOOL value branch with a numeric branch, the whole result is folded to BOOL, so every numeric value comes back as 0/1. There's no error or warning, so the result is silently wrong:

mysql> select coalesce(2, true), ifnull(5, true), if(1, 2.5e0, true), case when 1 then 9 else true end;
1   1   1   1          -- MatrixOne
2   5   2.5 9          -- MySQL 8

The same thing happens with columns, e.g. coalesce(int_col, bool_col) returns 1 for int_col = 7.

MySQL treats TRUE/FALSE as the integers 1/0, so the result type is the numeric type (BIGINT / DOUBLE / DECIMAL), as for COALESCE(2, 1).

Cause: BOOL has no implicit numeric cast in fixedCanImplicitCastRule. That's on purpose, see the comment on fixedTypeMatchWithBoolNumericCast. So in caseCheck / iffCheck / coalesceCheck the only common type every branch can reach is BOOL (numeric → BOOL is implicit), and the numeric branches get cast to BOOL. IFNULL is lowered to CASE.

How to reproduce

select coalesce(2, true), coalesce(null, 7, false), coalesce(2.5e0, true);
select ifnull(5, true), if(1, 2.5e0, true), if(0, true, 3);
select case when 1 then 9 else true end;
create table t(id int, i int, d double, b bool);
insert into t values (1, 7, 2.5, true), (2, null, null, false);
select id, coalesce(i, b), ifnull(d, b), if(b, i, b), case when b then d else false end from t order by id;

Observed (main 5fe08a3)

1 1 1 / 1 1 1 / 1, and row 1 of the table query is 1 1 1 1 1.

Expected (MySQL 8)

2 7 2.5 / 5 2.5 3 / 9, and row 1 is 7 2.5 7 2.5 (row 2 is 0 0 0 0).

coalesce(1.5, true) (DECIMAL with BOOL) has the same problem. It also needs a BOOL → DECIMAL cast, which #31193 (for #31181) adds.

Environment

  • matrixone main 5fe08a3, local single-node

本操作由 LW 授权的 Grok Bot 智能体执行。

Activity

  1. added
    kind/bug
    on Oct 26, 2026
  2. added
    severity/s0
    ai-easy
    and removed on Oct 26, 2026
  3. gouhongshen commented on Nov 4, 2026

    @gouhongshen
    Contributor

    PR #32162 is trying to fix this issue.

  4. gouhongshen commented on Nov 4, 2026

    @gouhongshen
    Contributor

    PR #32162 has merged and is awaiting manual validation. The issue remains open.

  5. XuPeng-SH commented on Nov 4, 2026

    @XuPeng-SH
    Contributor

    QA handoff — pending validation, not acceptance

    PR #32162 is merged into main at 7f09bc5bd32ec5159bba71d9f845c3e38a44d33d (final PR head 4cbb375e3902d071d9b9cdcad735588d8d44aa4f). The fix covers numeric/BOOL value-branch resolution for CASE, IF, IFNULL and COALESCE, reusing the BOOL→DECIMAL support already on main. The original counterexample was on old main 5fe08a33; the current issue discussion records the merged fix awaiting manual validation and contains no verified counterexample against this merged version or unfinished development follow-up.

    Evidence and limits:

    • The PR author reports focused UTs, function/plan package tests, live SQL checks and the new BVT (47/47) passing, plus the dependent-stage expectation update. These are attributed author results, not tests executed in this handoff.
    • The existing review/CI assessment is tied to older head 3a9cb017, not final head 4cbb375 or the merge SHA. It is not treated as final-version test proof.
    • This handoff checked remote issue/PR state and discussion only. No local build, SQL execution, multi-node or production verification was performed. The author's baseline/candidate JDBC display-width discrepancy has not been independently reproduced here.

    QA scope: rerun the issue's literal and nullable-column reproductions on the merge SHA or a recorded descendant; check values and result metadata, both branch orders, BOOL-only/condition controls, INT/DOUBLE/DECIMAL and high-scale DECIMAL boundaries. Include TRUE/FALSE/NULL assignment versus explicit CAST at DECIMAL(1,1), failed-insert atomicity, nonnumeric BIT/string/date controls, and affected UNION/dependent CASE paths. Compare the reported JDBC metadata discrepancies with an appropriate baseline separately from value correctness.

    Transferring to @Ariznawlll for QA; keep the issue open pending validation. This is not a claim of acceptance or a request for a 4.2 backport.

  6. slyunnn commented on Nov 6, 2026

    @slyunnn

    Regression and extended verification for #31200 (2026-11-06)

    Verification Environment

    • Frozen latest origin/main at test start, 65b721e1b93c6b9e05db2063b220e0c35916b335, fix(tae): preserve merge manifests and classify expired transfers (#32254). The verification tree matched that commit and remained clean. A fresh native build, binary version and runtime checks identified 65b721e1b9.
    • macOS arm64, Go 1.27.1, fresh local disk deployment with 2 CN / 1 TN / 1 Log service / Proxy, GOMAXPROCS=4.
    • Proxy 127.0.0.1:6001, CN1 127.0.0.1:16001 and CN2 127.0.0.1:16002 all connected. Original and specified extended checks below actually ran through each endpoint. PyMySQL used the MySQL text protocol. JDBC BVT used Connector/J; SDK and binary-prepared coverage is specified per issue below.

    What Was Covered

    • OK — The literal reproducer returned 2,5,2.5,9; the nullable-column reproducer returned numeric values 7,2.5,7,2.5 and the expected zero/NULL branches. CASE/IF/IFNULL/COALESCE, both branch orders, Boolean-only controls and INT/DOUBLE/DECIMAL cases passed.
    • OK — DECIMAL scales 57, 58 and 65 preserved the tested values. TRUE assignment into DECIMAL(1,1) returned invalid-input error 20301; the failed insert preserved row count. Explicit CAST clamped to 0.9 and emitted warning 1264. FALSE and NULL assignment controls passed.
    • OK — Complete function tests covered high-scale resolution and nonnumeric BIT/string/date controls; focused dependent CASE/UNION compile tests passed. The existing branch BVT passed its full value selection.
    • OK — PyMySQL metadata identified BIGINT/DOUBLE numeric domains. A separate real JDBC prepared query identified BIGINT, DOUBLE and DECIMAL; the BOOL-mixture BIGINT display width matched a same-version explicit numeric control at 64. Go database/sql with go-sql-driver/mysql and interpolateParams=false passed real binary prepare/execute reuse with positive, negative and NULL bindings at each endpoint.
    • Not covered — A historical-baseline explanation of the author's JDBC display-width discrepancy was not independently reproduced. Current values, numeric type identity and same-version control widths were checked; this is not a claim of identical metadata across historical revisions.
    • Not covered — Live MySQL differential execution, production performance, TKE or a single query executing across both CNs. Both CNs were independently exercised as coordinators. Scope-specific gaps are stated below.

    Findings

    • PR fix: keep numeric type for CASE/IF/IFNULL/COALESCE mixing BOOL and numeric branches #32162 merged as 7f09bc5bd32ec5159bba71d9f845c3e38a44d33d, covering numeric/BOOL value-branch resolution, and is included in the tested main. Fix PR
    • Numeric branches retained their numeric values instead of folding to Boolean truth. The initial temporary probe expected the wrong invalid-input code; corrected it to the actual documented 20301 class and reran the complete direct checks. This was a test expectation correction.

    MOTR / BVT Coverage

    • Reused function/func_bool_numeric_branches.sql, 47/47 value statements twice per endpoint, totaling 282 successful comparisons. Snapshot metadata was excluded with -n; direct JDBC and PyMySQL metadata checks supplied the separately stated evidence.
    • Complete planner, function, fulltext, collation, types, compare, sort and partition package tests passed on the frozen commit. Complete order, mergeorder, top, mergetop, window and aggexec consumer packages also passed. Focused TestDependent* compile tests passed. Two existing planner subtests were skipped for HNSW cosine similarity and interval-frame binding; they are outside these issues.
    • Existing maintained cases supplied the regression coverage. No duplicate MOTR source commit or weekly PR update was added. Source and test files in MatrixOne remained clean.
    • Independent setup/package work used bounded parallelism. SQL BVT ran sequentially because its runner performs global database cleanup. Selected BVT value comparisons passed twice per case through every endpoint. BVT statement totals refer to actual executed selections, rather than earlier handoff totals.

    Final Conclusion

    Fixed for numeric/BOOL branch value preservation and the verified current metadata/type behavior on 65b721e1b93c6b9e05db2063b220e0c35916b335.

    Large-data regression is not required. Type resolution, value preservation, high-scale precision and failed-write atomicity are deterministically covered by literals and small tables.

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

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions