Skip to content

fix(function): CASE/IF/IFNULL/COALESCE keep numeric type when a branch is BOOL - #31201

Closed
MatrixAdventurer wants to merge 3 commits into
matrixorigin:mainfrom
MatrixAdventurer:fix/bool-numeric-branches
Closed

MatrixAdventurer wants to merge 3 commits into
matrixorigin:mainfrom
MatrixAdventurer:fix/bool-numeric-branches

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 #31200

What this PR does / why we need it:

When a CASE / IF / IFNULL / COALESCE had a BOOL value branch next to a numeric branch, it returned BOOL and folded every numeric value to 0/1. For example, coalesce(2, true) gave 1, if(1, 2.5e0, true) gave 1, and coalesce(int_col, bool_col) gave 1 for 7. MySQL treats TRUE/FALSE as 1/0, so the result keeps the numeric type.

Root cause: BOOL has no implicit numeric cast in fixedCanImplicitCastRule. That's deliberate, see fixedTypeMatchWithBoolNumericCast. So the only common type caseCheck / iffCheck / coalesceCheck could reach was BOOL (numeric → BOOL is implicit).

Fix: caseCheck, iffCheck and coalesceCheck now 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_col no 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

  • UT 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/plan pass.
  • New BVT function/func_bool_numeric_branches.sql: main 7/16, PR 16/16.
  • E2E on a local single node, main 5fe08a3 vs this branch:
query main PR (= MySQL)
coalesce(2, true), coalesce(null, 7, false), coalesce(2.5e0, true) 1, 1, 1 2, 7, 2.5
ifnull(5, true) 1 5
if(1, 2.5e0, true), case when 1 then 9 else true end 1, 1 2.5, 9
coalesce(i, b), ifnull(d, b), if(b, i, b), case when b then d else false end (i=7, d=2.5, b=true) 1 1 1 1 7 2.5 7 2.5
coalesce(true, false), if(1, true, false) 1, 1 1, 1 (still BOOL)
  • Two existing goldens had recorded the wrong values and are updated:
    • 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 bool now returns 1 0 20 10 30 -10 (was 1 0).
  • BVT slices:
    • PR vs main: function 3 vs 11 failures (main: the 9 new-case lines; both: the 2 local foreign_exttab env failures; PR's 3rd failure was the func_coalesce_1 golden, now updated, 70/70). operator 1 vs 1 (pre-existing like_operator row 230). dtype 0/0. expression 0/0.
    • PR only: prepare, subquery, plan_cache, pg_cast, json_variant, window, optimizer, join, union and cte had 0 failures. dml had 1 failure, the union golden above, now 234/234.

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.

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

…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.
@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.

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?

@MatrixAdventurer

Copy link
Copy Markdown
Contributor Author

Closing with LW's confirmation: @fengttt's review rejected this approach (the real issue is COALESCE/CASE return-type resolution, not BOOL). #31200 needs a redesign around return-type rules.

本操作由 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]: CASE/IF/IFNULL/COALESCE fold numeric branches to 0/1 when another branch is BOOL

3 participants