Skip to content

[Bug]: comparing BOOL with an integer literal other than 0/1 is wrong: b = 2 matches TRUE rows #31295

Closed

Description

@MatrixAdventurer

Bug description

Comparing a BOOL with an integer literal other than 0/1 gives wrong results. The literal is turned into TRUE before the comparison, so b = 2 behaves like b = true:

mysql> select 2 = true, -1 = true, 2 > true, 2 in (true);
1   1   0   1           -- MatrixOne
0   0   1   0           -- MySQL 8
mysql> create table t(id int, b bool); insert into t values (1, true), (2, false);
mysql> select id from t where b = 2;
1                       -- MySQL: empty set
mysql> select id from t where b < 2;
2                       -- MySQL: 1, 2

EXPLAIN shows the rewritten filter: Filter Cond: (t.b = true). Comparing with a column or a non-literal (b = i, b = cast(2 as bigint)) is already correct, because it casts the BOOL to BIGINT.

In MySQL TRUE/FALSE are the integers 1/0, so b = 2 is false for every row and b < 2 is true for every non-NULL row. This is a silent wrong-result bug in WHERE clauses.

Cause: convertValueIntoBool (build_util.go) runs for =, <=>, <, <=, >, >=, <> when one side is BOOL, and rewrites any integer literal to value != 0. That truth conversion is right for AND/OR/NOT/XOR, but in a comparison only 0 and 1 are the same value as a BOOL.

How to reproduce

select 2 = true, true = 2, -1 = true, 2 <> true, 2 > true, true < 2, 2 <=> true, 2 in (true);
create table t(id int primary key, b bool);
insert into t values (1, true), (2, false), (3, null);
select id, b = 2, b <> 2, b < 2, b >= 2, b > -1 from t order by id;
select id from t where b = 2;
select id from t where b < 2 and b > -1;

Observed (main e033d4a)

1 1 1 0 0 0 1 1; rows 1 1 0 0 1 0 / 2 0 1 1 0 0; WHERE b = 2 returns id 1; the last query returns nothing.

Expected (MySQL 8)

0 0 0 1 1 1 0 0; rows 1 0 1 1 0 1 / 2 0 1 1 0 1; WHERE b = 2 is empty; the last query returns ids 1 and 2.

Environment

  • matrixone main e033d4a, local single-node

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

Activity

  1. added
    kind/bug
    severity/s0
    ai-easy
    on Oct 26, 2026
  2. added a commit that references this issue on Oct 30, 2026
    3686e16
  3. XuPeng-SH commented on Oct 30, 2026

    @XuPeng-SH
    Contributor

    QA handoff for merged #31299 at 3686e1622a7e7fcf08fa9f31829df6556861e2e6. This round reviewed the issue and change only; no build or functional test ran at this SHA because the existing memory guard was exceeded. Author-reported tests are separate evidence.

    Please verify BOOL comparisons with integer literals 0, 1, negative and greater than 1 in both operand orders, NULL and IN, while retaining logical truthiness controls. This handoff does not claim a pass.

  4. Ariznawlll commented on Oct 31, 2026

    @Ariznawlll
    Collaborator

    结论

    测试通过。

    关键证据

    • 2026-10-31 本地隔离双 CN、TN、LogService;服务为官方 main@4f894474ad10e1b9dc338868145a118a71c1fa23,修复 fix(plan): only rewrite integer literals 0/1 to BOOL in comparisons #31299 在祖先链。
    • 原始 2 = TRUE、TRUE = 2、负数字面量、<>、大小比较、<=>、IN 返回预期;含 TRUE、FALSE、NULL 的表中 b=2 无结果,b<2 AND b>-1 返回两条非 NULL 行。0/1 和逻辑 AND/OR 对照保持正确,3/3,测试库每轮清理。
    • 实际 SQL 回归见 MOTR #298 的 issue_31292_31295_bool_numeric.py(提交 3fcb79e5);该 PR 尚未合入,完整 suite 未运行。

    待解决点

    • 无。
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