Repository navigation
fix(function): CASE/IF/IFNULL/COALESCE keep numeric type when a branch is BOOL - #31201
Closed
MatrixAdventurer wants to merge 3 commits into
Closed
MatrixAdventurer wants to merge 3 commits into
MatrixAdventurer wants to merge 3 commits into
Conversation
…d numeric branches MySQL treats TRUE/FALSE as 1/0, so COALESCE(2, TRUE) is 2 and IF(c, 2.5e0, TRUE) is 2.5. BOOL has no implicit numeric cast in fixedCanImplicitCastRule, so caseCheck/iffCheck/coalesceCheck chose BOOL as the common type and every numeric branch came back as 0/1. Resolve the common type as if BOOL value branches were BIGINT and cast the BOOL branches to the result type. Conditions are untouched, and if BOOL cannot be cast to the chosen type the old resolution is kept.
MatrixAdventurer
requested review from
LeftHandCold,
XuPeng-SH,
aptend and
aunjgr
as code owners
October 26, 2026 05:34
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? |
fengttt
requested changes
Oct 26, 2026
fengttt
left a comment
Contributor
There was a problem hiding this comment.
This is not about bool type at all. It is about coalesce return type.
What is the result of coalesce(1.0, "1.0") and coalesce("1.0", 1.0)? Are we coalesce everything to the type of the first arg? Or a upcast of all args?
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 #31200
What this PR does / why we need it:
When a
CASE/IF/IFNULL/COALESCEhad aBOOLvalue branch next to a numeric branch, it returnedBOOLand folded every numeric value to0/1. For example,coalesce(2, true)gave 1,if(1, 2.5e0, true)gave 1, andcoalesce(int_col, bool_col)gave 1 for 7. MySQL treats TRUE/FALSE as 1/0, so the result keeps the numeric type.Root cause:
BOOLhas no implicit numeric cast infixedCanImplicitCastRule. That's deliberate, seefixedTypeMatchWithBoolNumericCast. So the only common typecaseCheck/iffCheck/coalesceCheckcould reach wasBOOL(numeric → BOOL is implicit).Fix:
caseCheck,iffCheckandcoalesceChecknow go through one wrapper,boolValueBranchesAsNumeric(operatorSet.go +84/-2, func_binary.go +8). The original bodies are unchanged and renamed*CheckTypes.The UNION common-type path benefits too:
select tinyint_col union select bool_colno longer folds 20/10/30/-10 to 1.I checked the interplay with our open #31193 by merging it locally: no conflicts, and
coalesce(decimal, bool)/if(c, decimal, bool)then resolve to DECIMAL as expected. #31192 has no conflict either.Evidence
bool_numeric_branch_test.go: 8 mixed cases RED on main (return type BOOL), GREEN with the fix. 3 BOOL-only controls pass on both.go test ./pkg/sql/plan/function ./pkg/sql/planpass.function/func_bool_numeric_branches.sql: main 7/16, PR 16/16.coalesce(2, true),coalesce(null, 7, false),coalesce(2.5e0, true)ifnull(5, true)if(1, 2.5e0, true),case when 1 then 9 else true endcoalesce(i, b), ifnull(d, b), if(b, i, b), case when b then d else false end(i=7, d=2.5, b=true)coalesce(true, false),if(1, true, false)function/func_coalesce_1.result:coalesce(bool_col, 200)for NULL rows now gives 200 (was 1), and the type is BIGINT.dml/select/union_and_union_all.result:tinyint UNION boolnow returns1 0 20 10 30 -10(was1 0).foreign_exttabenv failures; PR's 3rd failure was thefunc_coalesce_1golden, now updated, 70/70). operator 1 vs 1 (pre-existinglike_operatorrow 230). dtype 0/0. expression 0/0.Merge Danger
Medium-low. Result types change only for CASE/IF/IFNULL/COALESCE (and UNION) whose value branches mix BOOL with integer/float types. Those queries used to return 0/1 and now return the real values, like MySQL. Code relying on the old BOOL result type there would see BIGINT/DOUBLE instead.