Skip to content

fix(function): treat BOOL as 1/0 next to DECIMAL in arithmetic and comparison - #31193

Closed
MatrixAdventurer wants to merge 3 commits into
matrixorigin:mainfrom
MatrixAdventurer:fix/decimal-bool-arith
Closed

MatrixAdventurer wants to merge 3 commits into
matrixorigin:mainfrom
MatrixAdventurer:fix/decimal-bool-arith

Conversation

@MatrixAdventurer

@MatrixAdventurer MatrixAdventurer commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • BUG

Which issue(s) this PR fixes:

Fixes #31181
Ref #30922 (QA follow-up comments)

What this PR does / why we need it:

d + TRUE, TRUE - d, d * TRUE, d / TRUE, d % TRUE, d DIV TRUE and d = / <> / > / < / >= / <= TRUE failed with invalid argument operator +, bad value [DECIMAL64 BOOL] (also DECIMAL128 and decimal literals like 1.5 + TRUE). MySQL treats TRUE/FALSE as 1/0, and MatrixOne already does next to an integer (2 + TRUE = 3).

Root cause: the fixed binary cast tables (fixedBinaryCastRule1/2 in type_check.go) have BOOL × INT* rows but no BOOL × DECIMAL* rows, and supportedTypeCast had no BOOL → DECIMAL target, so CAST(TRUE AS DECIMAL(10,2)) failed too.

Fix:

  • type_check.go (+13): bind BOOL × DECIMAL64/128/256 exactly like INT64 × DECIMAL (both directions; same rows for / and DIV).
  • func_cast.go (+57/-1): add BOOL → DECIMAL64/128/256 (boolToDecimal, 1 scaled to the target). The explicit CAST path reuses the integer clamp, so CAST(TRUE AS DECIMAL(1,1)) gives 0.9 like CAST(1 AS DECIMAL(1,1)).

This is the opposite direction of #30923 (DECIMAL → BOOL). The changes are in different hunks, and neither PR depends on the other.

Evidence

  • UT decimal_bool_arithmetic_test.go: binding for 12 operators × 3 decimal widths × both orders, plus the cast values and NULLs and the explicit clamp. RED on main (bad value [DECIMAL64 BOOL], unsupported cast from bool to DECIMAL64), GREEN with the fix. go test ./pkg/sql/plan/function ./pkg/sql/plan pass.
  • New BVT operator/decimal_bool_arithmetic.sql: main 7/19, PR 19/19.
  • Updated BVT expectations that pinned the old error: subquery/subquery-with-exists.result and view/view-subquery-with-exists.result. select 1.1 * exists(select * from t1) now returns 1.1 (MySQL: 1.1), and the view over it can now be created.
  • E2E (local single-node, main 5fe08a3 vs this branch), row d = 1.50:
main PR
d + true, true + d, d - true, true - d, d * true ERROR 20203 [DECIMAL64 BOOL] 2.50 2.50 0.50 -0.50 1.50
d / true, true / d, d % true ERROR 1.500000 0.6667 0.50
d = true, d > true, d < true ERROR 0 1 0
cast(true as decimal(10,2)) ERROR [BOOL DECIMAL64] 1.00

NULL stays NULL, and division or modulo by a FALSE-derived 0 gives NULL.

  • BVT slices main vs PR: operator 1 vs 1 failure (pre-existing like_operator row 230), function 2 vs 2 (local foreign_exttab env), dtype 0 vs 0, subquery and view green after the two expectation updates. view/replace_view.test row 121 is a known local env failure.

Merge Danger

Low. Only BOOL × DECIMAL pairs that used to fail now bind, and the BOOL → DECIMAL cast is new. INSERT TRUE INTO decimal_col now stores 1/0 instead of erroring. It follows the integer assignment path, including its existing precision behavior (#27340).

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

…mparison

MySQL coerces TRUE/FALSE to 1/0 when the other operand is a DECIMAL.
The fixed binary cast tables had BOOL x INT* rows but no BOOL x DECIMAL*
rows, so 'd + TRUE', 'd = TRUE' and friends failed with
'bad value [DECIMAL64 BOOL]'. Bind them like INT64 x DECIMAL and add the
missing BOOL -> DECIMAL64/128/256 cast (explicit CAST clamps to the target
precision like an integer source).

The two exists() BVT results pinned the old error; MySQL returns 1.1.

Fixes matrixorigin#31181
Ref matrixorigin#30922
@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@fengttt fengttt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

div false is simply wrong.

insert into td values (1, 1.50, 1.50), (2, 0, 0), (3, null, null);

select id, d + true as p1, true + d as p2, d - true as m1, true - d as m2, d * true as t1, false * d as t2 from td order by id;
select id, d / true as q1, true / d as q2, d % true as r1, true % d as r2, d div true as i1 from td order by id;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR said / false is null. Which is WRONG. If we ever cast bool to number as 1 and 0, div by false should error.

And there is NO TEST about div, mod false. I wonder if this is AI's decision, but this is just terrible. Fix our dev skills to cover error/edge cases.

@MatrixAdventurer

Copy link
Copy Markdown
Contributor Author

Closing with LW's confirmation: @fengttt's review rejected this approach ("div false is simply wrong"). A different fix for #31181 will need a new design.

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

XuPeng-SH pushed a commit that referenced this pull request Oct 30, 2026
…nd comparison (#31296)

## What type of PR is this?

- [x] BUG

## Which issue(s) this PR fixes:

Fixes #31292

## What this PR does / why we need it:

Arithmetic and comparison between `FLOAT`/`DOUBLE` and `BOOL` failed to
bind with `bad value [DOUBLE BOOL]` / `[FLOAT BOOL]`. Examples: `1e0 +
true`, `d > b`, and `nullif(1e0, true)`, which is lowered to `=`. MySQL
treats TRUE/FALSE as 1/0, so these evaluate as DOUBLE.

Root cause: `fixedBinaryCastRule1` / `fixedBinaryCastRule2` had `BOOL ×
INT*/UINT*` rows but no `BOOL × FLOAT32/FLOAT64` rows. The `BOOL →
FLOAT` cast already exists (`boolToFloat`).

Fix: `type_check.go` +10 adds `BOOL × FLOAT32/FLOAT64 → FLOAT64` in both
orders to rule tables 1 (+ - * % and comparisons) and 2 (/ DIV). The
rows sit next to the `T_any` rows, away from the `BOOL × DECIMAL` rows
that #31193 adds, so the two PRs don't conflict.

## Evidence

- UT `float_bool_arithmetic_test.go`: 13 operators × {FLOAT, DOUBLE} ×
both orders. RED on main (`bad value [FLOAT BOOL]`), GREEN with the fix.
`go test ./pkg/sql/plan/function ./pkg/sql/plan` pass.
- New BVT `operator/float_bool_arithmetic.sql`: main 6/15, PR 15/15.
- E2E on a local single node:

| query | main | PR (= MySQL) |
|---|---|---|
| `1e0 + true` | bad value [DOUBLE BOOL] | 2 |
| `1e0 - true, 2e0 * true, 3e0 / true, true + 1e0, true - 2.5e0` | error
| 0 2 3 2 -1.5 |
| `1e0 = true, 1e0 > false, 2e0 <> true, true < 2e0, true <=> 1e0` |
error | 1 1 1 1 1 |
| `5e0 % true, 5e0 div true, true div 2e0` | error | 0 5 0 |
| `f + b, d - b, d * b, f = b, d > b` (f=1.5, d=2.5, b=true) | bad value
[FLOAT BOOL] | 2.5 1.5 2.5 0 1 |
| `nullif(1e0, true), nullif(2e0, true)` | error | NULL 2 |

- BVT slices, each branch vs main e033d4a (function, operator, dtype,
expression, dml, subquery, optimizer, prepare, join, union, cte,
window): identical failure sets on both, only the known local-env ones
(function/foreign_exttab 2, operator/like_operator row 230). No golden
changes needed.
- Interplay with our open #31192 / #31193 / #31201 and the other PRs of
this batch: `git merge-tree` against each is clean. I merged all of them
locally (main + #31192 + #31193 + #31201 + all 4 PRs of this batch) and
ran every new BVT on that build: all green.

## Merge Danger

Low. This only adds rule rows for type pairs that used to fail to bind.
No existing pair changes its resolution.

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

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
XuPeng-SH pushed a commit that referenced this pull request Oct 30, 2026
…31299)

## What type of PR is this?

- [x] BUG

## Which issue(s) this PR fixes:

Fixes #31295

## What this PR does / why we need it:

Comparing a `BOOL` with an integer literal other than 0/1 silently gave
wrong results. `b = 2` was planned as `b = true` (EXPLAIN: `Filter Cond:
(t.b = true)`), so `WHERE b = 2` returned the TRUE rows. Also `2 = true`
returned 1, `b < 2` returned 0 for TRUE, and `2 in (true)` returned 1.
In MySQL, TRUE/FALSE are 1/0, so all of these are the other way round.

Root cause: for `=`, `<=>`, `<`, `<=`, `>`, `>=`, `<>` with a BOOL side,
`convertValueIntoBool` rewrote every integer literal to `value != 0`.
That truth conversion is right for AND/OR/NOT/XOR, but in a comparison
only 0 and 1 have the same value as a BOOL.

Fix: `build_util.go` +7. In the comparison case only literals 0/1 are
rewritten, which keeps `b = 1` / `b = 0` as a plain BOOL filter. Other
literals stay BIGINT and take the existing `CAST(b AS BIGINT) <op>
literal` path, which column-vs-column comparisons already use.
AND/OR/NOT/XOR are unchanged.

## Evidence

- UT `bool_compare_int_literal_test.go`: `b = 2/-1/100` keeps the
literal. RED on main (the literal became BOOL), GREEN with the fix. 0/1
are still rewritten, and logic operators still truth-convert. `go test
./pkg/sql/plan ./pkg/sql/plan/function` pass.
- New BVT `operator/bool_compare_int_literal.sql`: main 9/15, PR 15/15.
- E2E on a local single node:

| query | main | PR (= MySQL) |
|---|---|---|
| `2 = true, true = 2, -1 = true, 2 <> true, 2 > true, true < 2, 2 <=>
true` | 1 1 1 0 0 0 1 | 0 0 0 1 1 1 0 |
| `2 in (true), true in (2, 3)` | 1 1 | 0 0 |
| `b = 2, b <> 2, b < 2, b >= 2, b > -1` for b=true | 1 0 0 1 0 | 0 1 1
0 1 |
| `select id from t where b = 2` | 1 | (empty) |
| `... where b < 2 and b > -1` | (empty) | 1, 2 |
| `b = 1, b = 0, b and 2` | unchanged | unchanged |

- BVT slices, each branch vs main e033d4a (function, operator, dtype,
expression, dml, subquery, optimizer, prepare, join, union, cte,
window): identical failure sets on both, only the known local-env ones
(function/foreign_exttab 2, operator/like_operator row 230). No golden
changes needed.
- Interplay with our open #31192 / #31193 / #31201 and the other PRs of
this batch: `git merge-tree` against each is clean. I merged all of them
locally (main + #31192 + #31193 + #31201 + all 4 PRs of this batch) and
ran every new BVT on that build: all green. With this PR merged,
`nullif(2, true)` also returns 2 instead of NULL.

## Merge Danger

Medium-low. Results change only for comparisons between BOOL and an
integer literal other than 0/1. Those were wrong before, and queries
relying on `flag = 2` matching TRUE rows would now match nothing, like
MySQL. Such filters on a BOOL column now evaluate `CAST(b AS BIGINT)`,
the same as `b = int_col` already does.

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

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
XuPeng-SH pushed a commit that referenced this pull request Oct 30, 2026
…g arguments (#31297)

## What type of PR is this?

- [x] BUG

## Which issue(s) this PR fixes:

Fixes #31293

## What this PR does / why we need it:

`GREATEST` / `LEAST` rejected a `BOOL` argument mixed with any other
type with `bad value [BIGINT BOOL]` / `[DOUBLE BOOL]` / `[VARCHAR
BOOL]`. MySQL treats TRUE/FALSE as 1/0: `GREATEST(2, TRUE)` is 2, and
`GREATEST('a', TRUE)` is 'a' (string comparison with '1').

Root cause: `resolveLeastGreatestType` had no rule for BOOL next to
another type. BOOL isn't numeric for `leastGreatestCommonNumericType` /
`leastGreatestStringMixedType`.

Fix: `func_builtin_leastgreatest.go` +57 adds
`resolveLeastGreatestMixedBool`, which works like the existing ENUM
normalization:
- It applies only when BOOL is mixed with another non-NULL type.
BOOL-only calls resolve exactly as before.
- It resolves the type as if each BOOL were a small integer: INT8, or
UINT8 when every other argument is unsigned/BIT. This keeps
`GREATEST(uint64_col, TRUE)` out of the signed/unsigned DECIMAL
widening.
- The BOOL arguments are then cast directly to the type chosen for their
position.
- If BOOL can't be cast to that type, it fails as before. Today that's
DECIMAL, e.g. `GREATEST(1.5, TRUE)` and `LEAST(TRUE, uint64, int)`.
#31193 (for #31181) adds `BOOL → DECIMAL`. With #31193 merged locally,
`greatest(1.5, true)` = 1.5 and `least(1.5, true)` = 1.0 with no further
change.

## Evidence

- UT `least_greatest_bool_test.go`: 9 mixed cases × {greatest, least}
RED on main (`bad value [BIGINT BOOL]`), GREEN with the fix, plus a
BOOL-only control. All `TestLeastGreatest*` pass, and so do `go test
./pkg/sql/plan/function ./pkg/sql/plan`.
- New BVT `function/func_least_greatest_bool.sql`: main 6/14, PR 14/14.
- E2E on a local single node:

| query | main | PR (= MySQL) |
|---|---|---|
| `greatest(2, true), least(2, true), greatest(true, 0), least(false,
3)` | bad value [BIGINT BOOL] | 2 1 1 0 |
| `greatest(1e0, true), least(0.5e0, true)` | bad value [DOUBLE BOOL] |
1 0.5 |
| `greatest('a', true), least('a', true)` | bad value [VARCHAR BOOL] | a
1 |
| `greatest(d, b), least(f, b)` (d=2.5, f=1.5, b=true) | bad value
[DOUBLE BOOL] | 2.5 1 |
| `greatest(true, false)` | 1 | 1 (unchanged) |

- BVT slices, each branch vs main e033d4a (function, operator, dtype,
expression, dml, subquery, optimizer, prepare, join, union, cte,
window): identical failure sets on both, only the known local-env ones
(function/foreign_exttab 2, operator/like_operator row 230). No golden
changes needed.
- Interplay with our open #31192 / #31193 / #31201 and the other PRs of
this batch: `git merge-tree` against each is clean. I merged all of them
locally (main + #31192 + #31193 + #31201 + all 4 PRs of this batch) and
ran every new BVT on that build: all green.

## Merge Danger

Low. Only calls that used to fail to bind are affected: BOOL mixed with
other types. BOOL-only and non-BOOL calls take exactly the same path as
before.

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

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>

This branch had an error being deployed

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

Labels

kind/bug Something isn't working size/M Denotes a PR that changes [100,499] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: DECIMAL arithmetic/comparison with a BOOL operand fails with 'bad value [DECIMAL64 BOOL]'

3 participants