Repository navigation
fix(function): treat BOOL as 1/0 next to DECIMAL in arithmetic and comparison - #31193
Closed
MatrixAdventurer wants to merge 3 commits into
Closed
MatrixAdventurer wants to merge 3 commits into
MatrixAdventurer wants to merge 3 commits into
Conversation
…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
MatrixAdventurer
requested review from
LeftHandCold,
XuPeng-SH,
aptend and
aunjgr
as code owners
October 26, 2026 04:40
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
1 task done
fengttt
requested changes
Oct 26, 2026
fengttt
left a comment
Contributor
There was a problem hiding this comment.
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; |
Contributor
There was a problem hiding this comment.
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.
This was referenced Oct 26, 2026
Contributor
Author
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What type of PR is this?
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 TRUEandd = / <> / > / < / >= / <= TRUEfailed withinvalid argument operator +, bad value [DECIMAL64 BOOL](also DECIMAL128 and decimal literals like1.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/2intype_check.go) have BOOL × INT* rows but no BOOL × DECIMAL* rows, andsupportedTypeCasthad no BOOL → DECIMAL target, soCAST(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/andDIV).func_cast.go(+57/-1): add BOOL → DECIMAL64/128/256 (boolToDecimal, 1 scaled to the target). The explicitCASTpath reuses the integer clamp, soCAST(TRUE AS DECIMAL(1,1))gives 0.9 likeCAST(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
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/planpass.operator/decimal_bool_arithmetic.sql: main 7/19, PR 19/19.subquery/subquery-with-exists.resultandview/view-subquery-with-exists.result.select 1.1 * exists(select * from t1)now returns1.1(MySQL: 1.1), and the view over it can now be created.d = 1.50:d + true, true + d, d - true, true - d, d * true[DECIMAL64 BOOL]2.50 2.50 0.50 -0.50 1.50d / true, true / d, d % true1.500000 0.6667 0.50d = true, d > true, d < true0 1 0cast(true as decimal(10,2))[BOOL DECIMAL64]1.00NULL stays NULL, and division or modulo by a FALSE-derived 0 gives NULL.
like_operatorrow 230), function 2 vs 2 (localforeign_exttabenv), dtype 0 vs 0, subquery and view green after the two expectation updates.view/replace_view.testrow 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_colnow stores 1/0 instead of erroring. It follows the integer assignment path, including its existing precision behavior (#27340).