Skip to content

[Bug]: DECIMAL values in a boolean context (AND/OR/NOT/XOR/CASE WHEN/WHERE) fail with 'bad value [DECIMAL64 BOOL]' #30922

Closed

Description

@MatrixAdventurer

Describe the bug

Using a DECIMAL value in a boolean context fails at plan time:

ERROR 20203 (HY000): invalid argument operator cast, bad value [DECIMAL64 BOOL]

([DECIMAL128 BOOL] for decimal(19..38, x) values.) This affects AND, OR, NOT, XOR, IS [NOT] TRUE/FALSE, CASE WHEN <decimal>, and WHERE <decimal> / WHERE <decimal column>. FLOAT, integer and string operands already work, and so does IF(<decimal>, ...).

How to reproduce

select 0.5 and 1;
select 0.5 or 0;
select not 0.5;
select 0.5 xor 0;
select 0.5 is true;
select case when 0.5 then 'y' else 'n' end;
select 1 from dual where 0.5;
select cast(0.5 as decimal(38,10)) and 1;      -- [DECIMAL128 BOOL]

create table t(a decimal(10,3));
insert into t values (0.5),(0),(-2.25),(null),(0.001);
select a from t where a;

Every statement above returns ERROR 20203 ... invalid argument operator cast, bad value [DECIMAL64 BOOL] (or DECIMAL128 BOOL).

Expected behavior (MySQL 8.0)

Any non-zero value is TRUE, zero is FALSE, and NULL stays NULL:

query MySQL
select 0.5 and 1 1
select not 0.5 0
select 0.5 xor 0 1
select case when 0.5 then 'y' else 'n' end y
select 1 from dual where 0.5 1 row
select a from t where a -2.250, 0.001, 0.500

Environment

  • MatrixOne main 2b388dd1, single-node launch.toml.

Additional information

supportedTypeCast in pkg/sql/plan/function/func_cast.go has no T_bool target for T_decimal64, T_decimal128 or T_decimal256, and decimal*ToOthers has no BOOL branch, so the implicit BOOL cast inserted for logical operators and filters is rejected.

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

Activity

  1. added
    kind/bug
    on Oct 23, 2026
  2. Ariznawlll commented on Oct 23, 2026

    @Ariznawlll
    Collaborator

    测试版本

    • 分支:main
    • commit:d4c37f4d8722dd91b80f5f5547940004bf1b293f
    • 部署方式:隔离本地集群,两个 CN SQL 端口;以下结果来自新建库的 3 轮 SQL/JDBC 测试
    • 测试日期:2026-10-23

    原问题验证

    本轮不重复测试原 issue 的 DECIMAL 用例,而是验证同一隐式 BOOL 转换表的相邻类型 BIT:SELECT b AND 1 报 20203/HY000: invalid argument operator and, bad value [BIT BOOL],SELECT NOT b 报 [BIT]。三份全新表均可复现。

    Happy Path 覆盖

    1. BIT(1) / BIT(8) 四行(0、1、128、NULL)写入成功,b+0 / w+0 数值为预期的 0、1、128、NULL。
    2. 同一列在 IF(b,1,0) 与 WHERE b 中可正确筛出真值行 id=2,3;失败之后源表仍有四行,SELECT 1 正常。

    Unhappy Path 覆盖

    1. SELECT id,b AND 1 FROM t ORDER BY id:3/3 报 [BIT BOOL],无任何结果行。
    2. SELECT id,NOT b FROM t ORDER BY id:3/3 报 [BIT],而不是返回 1、0、0、NULL。

    白盒验证

    pkg/sql/plan/function/type_check.go 的 implicitCastSupported 为 T_bit 只列出 T_int64、T_uint64,没有 T_bool;list_operator.go 的 AND/NOT 检查走隐式 BOOL 转换。这与本 issue 中 DECIMAL 缺少隐式 BOOL 路由属于同一类根因,因此把 BIT 边界补在这里,不重复新提。未修改源码或跑该点 UT。

    回归测试

    本轮未向仓库新增 BVT/motr/UT 或 PR。建议修复时把 BIT(1)/BIT(8) 的 0、1、非零、NULL 和 AND/OR/NOT/WHERE/IF 一并补入现有转换用例。

    测试证据

    • 三轮精确 JDBC 输出:/private/tmp/mo-explore-run-20261023/prepared_121443/jdbc.jsonl
    • 探针:/private/tmp/mo-explore-run-20261023/BitTruthCross.java
    • b'1' AND 1 字面量另报运行时字节解析错误;它经过不同路径,不作为本 issue 的 BIT 列隐式转换复现计算。

    测试结论

    测试不通过。

  3. Ariznawlll commented on Oct 23, 2026

    @Ariznawlll
    Collaborator

    Same root cause also breaks arithmetic with a BOOLEAN operand (not just boolean/logical contexts), worth covering in the same fix:

    SELECT CAST(1.5 AS DECIMAL(10,2)) + TRUE;
    -- MatrixOne: ERROR 20203 invalid argument operator +, bad value [DECIMAL64 BOOL]
    -- MySQL:     2.50
    
    SELECT TRUE + CAST(1.5 AS DECIMAL(10,2));  -- MatrixOne: ERROR ;  MySQL: 2.50
    SELECT CAST(1.5 AS DECIMAL(10,2)) - TRUE;  -- MatrixOne: ERROR ;  MySQL: 0.50
    SELECT CAST(1.5 AS DECIMAL(10,2)) * TRUE;  -- MatrixOne: ERROR ;  MySQL: 1.50

    So the DECIMAL-vs-BOOL incompatibility spans +, -, *, / (and likely % and comparison) in addition to the AND/OR/NOT/XOR/WHERE boolean contexts already listed. MySQL coerces BOOLEAN to 1/0 in all of these.

  4. Ariznawlll commented on Oct 23, 2026

    @Ariznawlll
    Collaborator

    Confirmed — comparison operators are also affected (the earlier comment listed arithmetic +``-``*; this adds =``>``<``<=``>=):

    SELECT CAST(1.5 AS DECIMAL(10,2)) = TRUE;   -- MatrixOne: ERROR [DECIMAL64 BOOL] ;  MySQL: 0
    SELECT CAST(1.5 AS DECIMAL(10,2)) > TRUE;   -- MatrixOne: ERROR ;  MySQL: 1
    SELECT CAST(0.5 AS DECIMAL(10,2)) < TRUE;   -- MatrixOne: ERROR ;  MySQL: 1

    So the DECIMAL-vs-BOOL incompatibility is total: every operator family (arithmetic, comparison, logical) rejects a BOOLEAN operand against a DECIMAL, while MySQL coerces BOOLEAN to 1/0 everywhere. The fix should be one place — the DECIMAL↔BOOL coercion rule.

  5. self-assigned this
    on Oct 23, 2026
  6. added a commit that references this issue on Oct 25, 2026
    c887523
  7. MatrixAdventurer commented on Oct 26, 2026

    @MatrixAdventurer
    ContributorAuthor

    Thanks for the extra cases. I re-ran them on a local single-node build of the #30923 fix commit (e69ef6f):

    Fixed by #30923 (DECIMAL→BOOL implicit cast in boolean contexts): x AND 1, x OR 0, x XOR 1, NOT x, WHERE x, CASE WHEN x on a DECIMAL(10,2) column with 0 / 1.5 / NULL now return the MySQL results.

    Not fixed by #30923, still ERROR 20203 on the PR build:

    • BIT in logical operators: b AND 1, b OR 0, NOT b (BIT(1) and BIT(8)) → [BIT BOOL] / [BIT]. (WHERE b and IF(b,1,0) already work.)
    • DECIMAL arithmetic with a BOOL operand: + - * / %, both operand orders → [DECIMAL64 BOOL].
    • DECIMAL comparison with a BOOL operand: = > < <= >= → [DECIMAL64 BOOL].

    These go through different rules (BIT→BOOL implicit cast; BOOL operand in arithmetic/comparison operator overloads) than the cast #30923 adds, so #30923 stays scoped to the original report. The remaining cases are small and will be followed up separately.

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

  8. MatrixAdventurer commented on Oct 26, 2026

    @MatrixAdventurer
    ContributorAuthor

    Follow-ups for the extra cases above. They are tracked separately and #30923 is unchanged:

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

  9. Ariznawlll commented on Oct 26, 2026

    @Ariznawlll
    Collaborator

    测试版本

    • 分支:官方 main;commit:7b80dbdbd398d171a47875f2a6196bf195a535eb。原 DECIMAL 布尔上下文修复 PR #30923 已 MERGED,commit c88752332cb29ec9a2774497c2cb179ddfe6cb6a 是该 main 祖先。
    • 部署方式:隔离 1 LOG/1 TN/2 CN,实际二进制 git_version()=7b80dbdbd3、SHA-256 0549f8b3fcb4303f8220a15163e8b44b9c8c834e3c1ab1b19ce7ced84da999e2。日期:2026-10-27。

    原问题验证

    CN1/CN2/CN1 三轮,原始 0.5 AND 1、OR 0、NOT 0.5、XOR 0、IS TRUE、CASE WHEN、WHERE 0.5 及 CAST(0.5 AS DECIMAL(38,10)) AND 1 均返回预期真值,不再报 [DECIMAL64/128 BOOL]。每轮新建 5 行 DECIMAL 表(0.5、0、-2.25、NULL、0.001),WHERE a ORDER BY a 精确返回 -2.250, 0.001, 0.500,全表仍为 5 行。

    Happy Path 覆盖

    1. DECIMAL64/128 的字面量与列谓词,零、负数、非零和 NULL;原问题各入口 3/3 通过。

    Unhappy Path 覆盖

    1. 0.0 AND 1 得 0,NULL AND 1 得 SQL NULL;无错误或额外行。每轮测试库清理。此前 QA 扩展发现的 BIT 与 DECIMAL/BOOL 算术、比较问题已由研发分别另列 #31180、#31181,不把这些不同运算符范围伪称为本 PR 覆盖。

    白盒验证

    本次用公共 SQL 检查产品返回值;未重复 Go UT。该修复 PR 对应布尔转换用例和 CI 已提供内部覆盖。

    回归测试

    现有 DECIMAL 布尔上下文 BVT 与配对结果覆盖原入口;MOTR #278 不重复。PR 的 CI Required 通过。

    测试证据

    • 隔离探针 qa-night/verify_new_five.py:每轮 10 个标量真值断言加 5 行表值/行数检查,3/3;修复 PR/BVT/CI 链接如上。

    测试结论

    测试通过。

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

Metadata

Metadata

Assignees

Labels

kind/bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions