Skip to content

fix(plan): only rewrite integer literals 0/1 to BOOL in comparisons - #31299

Merged
XuPeng-SH merged 3 commits into
matrixorigin:mainfrom
MatrixAdventurer:fix/bool-compare-int-literal
Oct 30, 2026
Merged

XuPeng-SH merged 3 commits into
matrixorigin:mainfrom
MatrixAdventurer:fix/bool-compare-int-literal

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

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 智能体执行。

@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: 916906d18c767f0d827ec7ba1bb437dc0350077d

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: 916906d18c767f0d827ec7ba1bb437dc0350077d

Assessment: approve

Reviewed the complete four-file delta and surrounding comparison, IN and cast paths. The guard restores numeric comparison semantics through the existing coercion owner while preserving 0/1 predicates and logical truth conversion. Tests cover public SQL behavior, NULLs and compatibility. No actionable findings or substantive discussion disputes remain.

Model: gpt-6-astra / xhigh

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

Validation:

  • Reused frozen-head evidence reporting successful Ubuntu UT, UT coverage, PROXY BVT and COMPOSE PESSIMISTIC BVT.

  • Source identity, patch consistency, formatting and discussion-accounting audit passed: checks/1793034271672807432-2.json matches source-evidence-audit-v2.log and the assessed head. Artifacts are under /home/xupeng/.local/state/mo-bot/jobs/adcb6328844264062b50b7c2/attempt-1.

  • Saved discussion-assessment.json covering both informational comments exactly once, and review-evidence.json recording design, caller, boundary and performance analysis.

Limitations:

  • No local Go/SQL tests were repeated. Supplied CI evidence contains job outcomes, not per-case logs; author-reported execution counts were not independently verified.

  • Corrected comparisons can incur the existing BOOL-to-BIGINT cast cost; performance was not benchmarked. Ordinary 0/1 and logical paths retain existing behavior.

  • Issue #31295 was assessed through the frozen PR's problem statement; its separate issue body was not supplied.

@mergify

mergify Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Queued — the merge queue status continues in this comment ↓.

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

Reviewed the comparison rewrite and its SQL/plan controls at this commit. Restricting BOOL literal normalization to 0/1 preserves the intended comparisons; I found no blocking issues. I reused the passing CI results and did not run local tests.

@mergify mergify Bot added the queued label Oct 30, 2026
@mergify

mergify Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

🚫 Left the queue

56 min in the queue, with no time running CI.

So far

  • ✅ Entered the queue Fri 30 Oct, 11:31 · rule main · triggered by rule Automatic queue on approval for main
  • ⏹️ Testing stopped · in place
  • 🚫 Left the queue 12:27 · a069f28

Reason

Pull request #31299 has been dequeued

Pull request from fork cannot be queued. This pull request comes from a fork, and Mergify needs the author's permission to update its branch.

The author needs to enable "Allow edits from maintainers" on this pull request.

Hint

You should look at the reason for the failure and decide if the pull request needs to be fixed or if you want to requeue it.
If you do update this pull request, it will automatically be requeued once the queue conditions match again.
If you think this was a flaky issue instead, you can requeue the pull request, without updating it, by posting a @mergifyio queue comment.

Merge conditions: 2 of 3 met

This pull request

Condition Required by
✅ #review-threads-unresolved = 0 GitHub branch protection
✅ github-review-approved GitHub branch protection

The batch, tested in place

Condition Required by
❌ any of: check-neutral = CI Required, check-skipped = CI Required, check-success = CI Required GitHub branch protection

Tick the box to put this pull request back in the merge queue (same as @mergifyio queue).

  • Requeue this pull request

Times in UTC · dashboard · how the merge queue works

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

dequeued 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]: comparing BOOL with an integer literal other than 0/1 is wrong: b = 2 matches TRUE rows

4 participants