Skip to content

sql/plan: support BIT columns in AND/OR/XOR/NOT - #31682

Merged
XuPeng-SH merged 3 commits into
matrixorigin:mainfrom
gouhongshen:agent/issue-31180-main-f9795cab
Oct 29, 2026
Merged

XuPeng-SH merged 3 commits into
matrixorigin:mainfrom
gouhongshen:agent/issue-31180-main-f9795cab

Conversation

@gouhongshen

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

Related to #31180

What this PR does / why we need it:

A BIT column used as an operand of AND / OR / XOR / NOT (and && / || / !) failed at plan time with ERROR 20203 (HY000): invalid argument operator and, bad value [BIT BOOL] / ... not, bad value [BIT], even though MatrixOne already evaluates BIT values as numbers in boolean contexts (WHERE b, IF(b,1,0)) and ships a truthiness-preserving bit->bool cast at execution. Root cause: the logical operators demanded an implicit BIT->BOOL conversion, but the T_bit row of the fixed implicit-cast table lists only INT64/UINT64, so plan binding failed before execution.

Changes

This PR scopes BIT acceptance to the logical operators' boolean argument positions and leaves the global implicit-cast table byte-identical to base, following the existing operator-local matcher precedent (binTypeMatch, fixedTypeMatchWithBoolNumericCast, inetNtoaTypeMatch):

  • pkg/sql/plan/function/type_check.go — new isLogicalBoolOperand (accepts BOOL, BIT, or anything the fixed cast table converts to BOOL), logicalOperatorTypeCheck (shared multiary checker for AND/OR with the same cost/cast mechanics as the previous per-operator closures, extended with BIT), and logicalFixedTypeMatch (NOT/XOR: normalizes BIT arguments to BOOL, resolves through the unchanged fixedTypeMatch, and re-targets BIT arguments' cast types to BOOL so execution runs the existing bit->bool cast; non-BIT inputs resolve exactly as before).
  • pkg/sql/plan/function/list_operator.go — NOT/XOR checkFn swapped to logicalFixedTypeMatch; AND/OR to logicalOperatorTypeCheck. No overload, execution, or cast-table changes.

Keeping BIT out of the global cast table matters beyond the operators: CASE/IF/COALESCE result typing consumes that table, and a global BIT->BOOL rule makes BOOL the only type reachable from both BIT and BOOL, silently collapsing conditional value branches (e.g. coalesce(bit8_col, true) returns 1 instead of 128). With the scoped acceptance, mixed BIT/BOOL value branches stay plan-time errors and BIT-only branches keep their values (if/case resolve native T_bit, coalesce resolves its pre-existing base bit->uint64 cast).

Tests

  • New UT pkg/sql/plan/function/bit_logical_operators_test.go (5 tests): BIT acceptance in both operand positions and multiary arities incl. &&/||/! and NULL operands; exact non-BIT resolution parity vs the previous matchers; cast-table scoping guards (no bit->bool, no bit->varchar/float/decimal/date, bit->int64@2/bit->uint64@1 retained); conditional-resolver guards (9 mixed BIT/BOOL cases reject; bit-only if/case resolve T_bit, coalesce resolves T_uint64); bit->bool execution-cast truthiness incl. 128 and the BIT(64) high bit.
    Command: mo-cgo-test -mod=readonly -count=1 -run 'TestLogicalOperatorsAcceptBitOperand|TestLogicalOperatorsNonBitOperandsUnchanged|TestBitImplicitCastRulesRemainScoped|TestConditionalResolversDoNotCollapseBitToBool|TestBitToBoolCastTruthiness' -v ./pkg/sql/plan/function/ -> 5/5 PASS; plus full ./pkg/sql/plan/function/ suite PASS and full ./pkg/sql/plan/ suite PASS.
  • New BVT test/distributed/cases/operator/bit_logical_operators.sql (34 statements): the issue's reproduction on BIT(1)/BIT(8) with 0/1/128/NULL, symbolic aliases, three-valued-logic NULL pins, WHERE variants, boolean-context sharing pins (CASE WHEN w AND true, IF(NOT b, ...)), BIT(64) 2^63 and 2^64-1, numeric +0 projections, conditional value branches over COALESCE/IF/CASE in both branch orders preserving 128 and the BIT(64) highs (WHERE coalesce(w,b)=128 returns id 3), and six mixed BIT/BOOL plan-time error pins. Result file generated and audited row-by-row against MySQL 8 semantics; validation run 34/34 at 100%.
  • Regression BVT: decimal_bool_context 24/24 and mysql_compat_logical_expr 13/13 at 100% on the fixed binary (DECIMAL boolean-context behavior for [Bug]: DECIMAL values in a boolean context (AND/OR/NOT/XOR/CASE WHEN/WHERE) fail with 'bad value [DECIMAL64 BOOL]' #30922 unchanged).

Residual risk

  • coalesce(bit, bit) keeps its pre-existing base UINT64 result typing (no BIT overload exists in COALESCE); this preserves every bit pattern (verified at 2^63 and 2^64-1) and is unchanged by this PR.
  • Operator-local acceptance affects only AND/OR/XOR/NOT resolution; exact parity for non-BIT inputs is asserted in UT and both boolean-context BVT regressions.

BIT columns failed plan binding in logical operators ([BIT BOOL]/[BIT]
errors) because AND/OR/XOR/NOT required an implicit BIT->BOOL conversion
that the fixed cast table does not provide, even though the execution
layer already implements a truthiness-preserving bit->bool cast.

Scope BIT acceptance to the operators' boolean argument positions via the
new isLogicalBoolOperand matcher: AND/OR resolve through the shared
logicalOperatorTypeCheck and NOT/XOR through logicalFixedTypeMatch, both
identical to the prior resolution for every non-BIT input. The global
implicit-cast table is unchanged, so CASE/IF/COALESCE result typing keeps
its base behavior: mixed BIT/BOOL value branches stay plan-time errors
instead of collapsing non-zero BIT values to TRUE.

Covered by new unit tests (operand acceptance, exact non-BIT parity, cast
table scoping, conditional-resolver guards, bit->bool truthiness incl.
BIT(64) high bit) and a 34-statement BVT case reproducing the issue SQL.

Related to matrixorigin#31180
@gouhongshen

gouhongshen commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor Author

Unresolved unrelated CI blockers tracked for this PR:

@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 29, 2026 •

Copy link
Copy Markdown
Contributor

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

Commit: 361fbe63244238fb2ecb2c83e57afc49f05b8481

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: 361fbe63244238fb2ecb2c83e57afc49f05b8481

Assessment: approve

BIT acceptance is correctly scoped to logical operands, preserving global casts and conditional result types. No actionable findings remain. Later successful CI checks supersede the historical blocker notice.

Model: gpt-6-astra / xhigh

Session: 01a1869d-820c-7212-9788-aecfb86a4141 · slot 0 · generation 2

Validation:

  • Preflight and native build from frozen inputs passed.

  • Function and planner package suites passed; all five new tests executed successfully.

  • All 34 SQL-case statements passed twice through a single-CN MySQL frontend, plus mixed-type, multiary and prepared-statement controls. Teardown passed after both runs.

  • Global cast-table identity verified against base. All three discussion keys reconciled. validation-summary.json matches retained logs and receipts; source remains unchanged.

Limitations:

  • The frontend probe did not run mo-tester or verify JDBC-specific width/precision formatting; frozen CI reports successful multi-CN BVT checks.

  • Ten unrelated existing tests/subtests were skipped.

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

mergify Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

🚫 Left the queue

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

So far

  • ✅ Entered the queue Thu 29 Oct, 08:32 · rule main · triggered by rule Automatic queue on approval for main
  • ⏹️ Testing stopped · in place
  • 🚫 Left the queue 08:43 · 91dbffa

Reason

Pull request #31682 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

@XuPeng-SH
XuPeng-SH merged commit 2fa5b0c into matrixorigin:main Oct 29, 2026
24 of 27 checks passed
@mergify mergify Bot added dequeued and removed queued labels Oct 29, 2026

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/L Denotes a PR that changes [500,999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants