Skip to content

fix(function): allow BIT operands in AND/OR/XOR/NOT - #31192

Closed
MatrixAdventurer wants to merge 3 commits into
matrixorigin:mainfrom
MatrixAdventurer:fix/bit-logical-ops
Closed

MatrixAdventurer wants to merge 3 commits into
matrixorigin:mainfrom
MatrixAdventurer:fix/bit-logical-ops

Conversation

@MatrixAdventurer

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • BUG

Which issue(s) this PR fixes:

Fixes #31180
Ref #30922 (QA follow-up comment)

Summary

b AND 1, b OR 0, b XOR 1, NOT b (and && / || / !) on a BIT column failed with invalid argument operator and, bad value [BIT BOOL] / [BIT]. MySQL treats BIT as a number in a boolean context, and MatrixOne already does for WHERE b and IF(b,...).

Root cause: the AND/OR checkers (and the fixed matcher for XOR/NOT) ask whether the operand can be implicitly cast to BOOL. The T_bit row of implicitCastSupported only listed INT64/UINT64, although the BIT → BOOL cast itself already exists (non-zero is TRUE). This PR adds T_bool (preferLevel 2) to that row. That is the only production change (type_check.go +3).

This is the BIT column case. The b'1' AND 1 bit-value literal case is a different path (#30930 / #30931) and is not touched. Independent of #30923 (DECIMAL → BOOL).

Evidence

  • UT bit_logical_operators_test.go: RED on main (invalid argument operator and, bad value [BIT BOOL]), GREEN with the fix. go test ./pkg/sql/plan/function ./pkg/sql/plan pass.
  • New BVT operator/bit_logical_operators.sql: main 7/14, PR 14/14.
  • E2E (local single-node, main 5fe08a3 vs this branch), BIT(1)/BIT(8) with 0, 1, 128, NULL:
main PR
b and 1, b or 0, not b, b xor 1 ERROR 20203 [BIT BOOL] 0 0 1 1 / 1 1 0 0 / 1 1 0 0 / NULLs
where not w ERROR 20203 [BIT] id 1
  • 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.

Merge Danger

Low. BIT can now also be implicitly cast to BOOL in other overload resolution. The level-2 preference keeps UINT64 (level 1) first, and the function/operator/dtype BVT slices are unchanged.

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

MySQL treats BIT as a number in a boolean context. The logical operators
ask whether the operand can be implicitly cast to BOOL, and the BIT row of
the implicit cast table only listed INT64/UINT64, so 'b AND 1' and 'NOT b'
failed with 'bad value [BIT BOOL]'. The BIT -> BOOL cast already exists
(non-zero is TRUE); add it to the implicit rules.

Fixes matrixorigin#31180
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 →

@XuPeng-SH

XuPeng-SH commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

mo-bot review · mo-bot-local · done

Commit: 22ccb0012a59829c1f0445b5c45d27488a15beb7

Review completed. Read the assessment.

@XuPeng-SH XuPeng-SH 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.

mo-bot review · mo-bot-local · review

Commit: 22ccb0012a59829c1f0445b5c45d27488a15beb7

Assessment: request_changes

Reviewed all four changed files and their binder, overload-resolution and execution callers. The logical-operator fix correctly reuses existing casts, but registering the conversion globally also enables incorrect conditional-expression results.

Model: gpt-6-astra / xhigh

Session: 01a13dcf-1517-7f90-b3f9-c5f9f9e7031b · slot 0 · generation 1

  • P2 pkg/sql/plan/function/type_check.go:3168: Create t(w BIT(8)), insert 128, then execute SELECT COALESCE(w, TRUE), IF(TRUE, w, TRUE), CASE WHEN TRUE THEN w ELSE TRUE END FROM t.
    Impact: All three expressions now return 1 instead of preserving the selected value 128. The global rule makes BOOL a viable common result type, converting value branches rather than only boolean conditions. UINT64's higher preference does not protect these cases because the BOOL peer cannot implicitly convert to UINT64. Scope the conversion to boolean positions or preserve numeric conditional-result types, and add regression coverage.
    Evidence: Public SQL probes reproduced all three incorrect results. Replacing the fallback TRUE with numeric 1 preserves 128 in every case. Restoring the base version of the sole changed production file makes the boolean-peer expressions reject with error 20203, establishing that this PR introduces the silent conversion. Retained logs: value-head.jsonl and base-control.jsonl; matching receipts: 1792994188978694257-6.json and 1792994265726683687-3.json.

Validation:

  • Verified clean frozen HEAD and 12,365 tracked files matching the build snapshot. Isolation preflight and repository-owned native build passed.

  • Function and planner package suites passed after correcting an internal-type assumption in the diagnostic probe. The overload audit checked 731 cases, excluding one internal signature without an ordinary SQL type representation.

  • Replayed all 14 added BVT statements twice through the SQL frontend, with row comparison and verified teardown. BIT(64), NULL short-circuit, prepared-query and numeric-control checks passed.

  • The targeted conditional-result regression failed in all three boolean-peer cases; numeric controls passed on both candidate and base production code.

  • Reconciled commands, source fingerprints, exits and retained logs in /home/xupeng/.local/state/mo-bot/jobs/ee87bb7ea59c8c757e65fd87/attempt-1/review-evidence.json. Final audit receipt 1792994368506816134-4.json passed.

Limitations:

  • SQL replay was not a mo-tester metadata/report run. Two unrelated planner subtests were skipped.

  • No retained author validation artifacts or separate linked-issue bodies were supplied; issue scope was established from the frozen PR description.

  • No performance benchmark was needed for the single initialization-time registry entry; execution continues to use existing vector casts.

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

Request changes on this commit for the inline finding. This is based on source and control-flow review; I did not run a local reproduction.

toList: []toRule{
// MySQL treats BIT as a number in a boolean context, so the
// logical operators (AND/OR/XOR/NOT) may cast it to BOOL.
{toType: types.T_bool, preferLevel: 2},

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.

[P2] Restrict BIT truthiness conversion to boolean contexts

This rule is global: IF/CASE result-type resolution also uses it. For IF(id = 1, bit_col, FALSE) with a selected BIT(8) value of 128, the newly available common result type is BOOL, so the selected value is converted to true/1 instead of preserving 128. The change intended for AND/OR/XOR/NOT thereby introduces a lossy value expression.

Please scope BIT-to-BOOL coercion to logical/condition operands, or otherwise preserve the BIT numeric value domain for result branches. Include BIT values greater than 1 in IF/CASE controls.

@XuPeng-SH

XuPeng-SH commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

mo-bot review · mo-bot-local · done

Commit: 22ccb0012a59829c1f0445b5c45d27488a15beb7

Review completed. Read the assessment.

@XuPeng-SH XuPeng-SH 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.

mo-bot review · mo-bot-local · review

Commit: 22ccb0012a59829c1f0445b5c45d27488a15beb7

Assessment: request_changes

The existing P2 remains: global BIT-to-BOOL eligibility silently narrows conditional result values.

Model: gpt-6-astra / xhigh

Session: 01a13dcf-1586-7a01-9000-d45e20995cf8 · slot 1 · generation 1

  • P2 pkg/sql/plan/function/type_check.go:3168: A BIT(8) column containing 128 appears alongside a BOOL value branch in COALESCE(w, TRUE), IF(TRUE, w, TRUE), or CASE WHEN TRUE THEN w ELSE TRUE END.
    Impact: These expressions now select BOOL and return true/1, losing the selected BIT value. Previously they were rejected. Restrict this coercion to logical operands, or preserve numeric values during conditional result resolution, and add regression tests with BIT values above 1.
    Evidence: CASE, IF/IFF and COALESCE share tryToMatch for result types. BOOL has no implicit numeric conversion, making BOOL the only common target after this addition; UINT64's higher preference cannot help. The binder inserts the cast, and numericToBool computes v != 0. Current source independently corroborates the unchanged finding and exact-head execution report in reviews:5647960002.

Validation:

  • Reviewed all four changed files, affected resolution/binder/cast consumers, linked issues and all six discussion entries; saved review-plan.md and discussion-assessment.json.

  • Static audit passed with receipt checks/1793200796660485723-4.json and retained static-audit.terminal.log: clean assigned head, matching delta, formatting, discussion coverage, and base/head cast-table comparison. Two earlier reviewer-probe assertion failures are preserved and reconciled in validation-evidence.json.

Limitations:

  • No fresh Go tests or frontend SQL execution; the static model is not a product test. Prior execution is reported in supplied discussion, with raw logs not independently recovered.

  • Further exhaustive runtime validation stopped after establishing the shared-resolution defect.

@MatrixAdventurer

Copy link
Copy Markdown
Contributor Author

Review: superseded by #31682, suggest closing

#31180 was fixed by the merged #31682 (2fa5b0c9, merged 2026-10-29 08:43 UTC). QA (@Ariznawlll) verified it on main and closed the issue on 2026-10-30. This PR now conflicts with main and its change duplicates the merged fix, so I suggest closing it. (I will not close it myself.)

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

@MatrixAdventurer

Copy link
Copy Markdown
Contributor Author

Closing with LW's confirmation: superseded by the merged #31682 (#31180 verified and closed by QA).

本操作由 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 was previously deployed

1 inactive 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]: BIT columns fail in AND/OR/XOR/NOT with 'bad value [BIT BOOL]'

4 participants