Skip to content

fix: keep numeric type for CASE/IF/IFNULL/COALESCE mixing BOOL and numeric branches - #32162

Merged
XuPeng-SH merged 2 commits into
matrixorigin:mainfrom
gouhongshen:agent/issue-31200-main-1b2ae8d6
Nov 4, 2026
Merged

XuPeng-SH merged 2 commits into
matrixorigin:mainfrom
gouhongshen:agent/issue-31200-main-1b2ae8d6

Conversation

@gouhongshen

@gouhongshen gouhongshen commented Nov 4, 2026 •

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

What this PR does / why we need it:

Problem

CASE, IF, IFNULL and COALESCE silently folded numeric value branches to 0/1 when another branch was BOOL:

select coalesce(2, true), ifnull(5, true), if(1, 2.5e0, true), case when 1 then 9 else true end;
-- before: 1 1 1 1     after (MySQL 8): 2 5 2.5 9

Root cause: BOOL has no implicit numeric cast in fixedCanImplicitCastRule, so the only common type every value branch could reach in caseCheck/iffCheck/coalesceCheck was BOOL, and the numeric branches were cast to BOOL. IFNULL is lowered to CASE, so all four constructs shared the defect.

Change

  • pkg/sql/plan/function/operatorSet.go (caseCheck/iffCheck + shared wrapper): BOOL value branches are normalized to a numeric domain before common-type resolution — BIGINT, or DECIMAL(1,0) when a DECIMAL branch participates (TRUE/FALSE need only one integral digit). The result keeps the numeric type and TRUE/FALSE evaluate as 1/0, matching MySQL. A mixture the numeric domains cannot resolve (e.g. plan-internal DECIMAL(76,76) + BOOL) now returns a resolution error instead of silently folding to BOOL. Non-numeric mixtures (BIT, strings, dates) keep the original resolution path, and BOOL-only branches keep BOOL semantics.
  • pkg/sql/plan/function/func_binary.go (coalesceCheck): delegates to the same wrapper.
  • The BOOL→DECIMAL runtime conversion this resolution relies on (value conversion plus declared-precision validation for assignment casts: TRUE needs one integral digit, so a target with width ≤ scale, e.g. DECIMAL(1,1), rejects it with invalid input: 1 beyond the range, can't be converted to Decimal64(1,1).; FALSE/NULL always convert; explicit CAST(bool AS DECIMAL(M,N)) keeps its clamp behavior, CAST(true AS DECIMAL(1,1)) = 0.9) is already on main via fix(function): support DECIMAL arithmetic and comparison with BOOL operands #31937 and fix(sql): enforce DECIMAL width for integer assignments (#30367) #30639. This PR's diff therefore no longer touches pkg/sql/plan/function/func_cast.go; the UT/BVT below keep covering that contract end to end.

Tests

  • New UT pkg/sql/plan/function/bool_numeric_branch_test.go: common-type resolution for all four constructs (INT/DOUBLE/DECIMAL × BOOL, both branch orders, BOOL-only and condition-position controls, BIT/VARCHAR/DATE non-numeric controls), high-scale DECIMAL (65,57)/(65,58)/(76,75), unresolvable (76,76) must error, and BOOL→DECIMAL cast/assignment values and range errors. The new tests fail on the pre-fix code (13 subtests) and pass after.

  • New BVT test/distributed/cases/function/func_bool_numeric_branches.{sql,result}: literals and nullable columns across all four constructs, both branch orders, high-scale DECIMAL up to the publicly reachable precision 65, and assignment/clamp/error coverage (TRUE into DECIMAL(1,1) rejected via INSERT-SELECT and INSERT-VALUES, FALSE/NULL accepted, explicit CAST clamps to 0.9, failed inserts leave the table unchanged). mo-tester: 47/47 100%.

  • Updated goldens func_coalesce_1.result (BOOL+INT columns now resolve to INT) and union_and_union_all.result (int-typed rows under the same shared resolution); values unchanged where the old result was already correct. mo-tester: union_and_union_all 234/234 100%; the edited func_coalesce_1 goldens pass.

  • Commands: .agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -timeout=1800s ./pkg/sql/plan/function ./pkg/sql/plan → ok both packages; GOWORK=off go vet -mod=readonly ./pkg/sql/plan/function → clean; golangci-lint run -c .golangci.yml --new-from-rev <merge-base> ./pkg/sql/plan/function → 0 issues; live-node SQL verification of the issue's exact queries.

  • After the 2026-11-04 rebase onto main (52cbba7): semantic conflict resolution in operatorSet.go/func_binary.go (the wrapper now composes with the low-precision-float widening from feat: low-precision float types bf16/float16/float8/float4 and vecf8/vecf4 vector columns (#20567) #29554 and the ENUM/JSON condition handling from fix(plan): MySQL implicit boolean semantics for ENUM/SET, BIT literals and JSON conditions #31492); func_cast.go conflicts resolved by taking main's already-merged BOOL→DECIMAL support, making the file identical to main; the new UT's TestBoolToDecimalCast renamed to TestBoolToDecimalCastAndClamp (name already used by fix(function): support DECIMAL arithmetic and comparison with BOOL operands #31937's decimal_bool_operand_test.go). Re-validated: mo-cgo-test -count=1 -v -run 'TestBoolNumericValueBranchesResolveToNumeric|TestBoolDecimalHighScaleValueBranches|TestBoolDecimalUnresolvableDoesNotFoldToBool|TestBoolNonNumericValueBranchesUnchanged|TestBoolToDecimalCastAndClamp|TestBoolToDecimalAssignmentRangeCheck|TestBoolToDecimalAssignmentValues' ./pkg/sql/plan/function → 7/7 pass; full pkg/sql/plan/function package suite → ok 33s; go vet with the repository CGo environment → clean; golangci-lint --new-from-rev 52cbba793b → 0 issues.

  • After CI on the rebased head, TestDependentNativeStages in pkg/sql/compile (from fix(plan): support complex non-equality scalar aggregates #31275) failed in UT Coverage: its range_row_operand_{skipped,selected_one,uncorrelated,deep_guard} oracles still assumed CASE WHEN <bool> THEN <bool row comparison> ELSE 0 END resolves to BOOL. Under this PR's intended semantics the result is numeric, so the expected values are now 1/0 (matching MySQL). Only the four expectation strings changed; range_row_operand_inner_case/range_row_operand_coalesce stay BOOL and keep true/false. Local: mo-cgo-test -count=1 -run 'TestDependentNativeStages' ./pkg/sql/compile → ok.

Residual risk / notes

  • In one local single-node + JDBC environment, func_coalesce_1 shows 22 additional failures that are display-width metadata drift only (e.g. [12,-1,0] vs [12,25,0]); the identical failure set occurs with the unmodified base build in that environment, every value row is byte-identical, and the affected statements include pure select * from t1 scans this change cannot influence. CI-generated goldens should not be affected.
  • The unresolvable-mixture error path (needs > 66 total digits) is unreachable from public SQL because the parser caps DECIMAL(M) at M ≤ 65; it is covered by UT only. The public boundary (scales 57/58/65) is covered by BVT.

@mergify mergify Bot added the kind/bug Something isn't working label Nov 4, 2026
@matrix-meow matrix-meow added the size/L Denotes a PR that changes [500,999] lines label Nov 4, 2026
@gouhongshen
gouhongshen force-pushed the agent/issue-31200-main-1b2ae8d6 branch from 2689f32 to 96fe6bc Compare November 4, 2026 09:58
…meric branches

BOOL has no implicit numeric cast, so the common-type resolution of CASE, IF,
IFNULL and COALESCE folded every value branch to BOOL when one branch was BOOL
and another was numeric, silently returning 0/1 for numeric values.

Normalize BOOL value branches to a numeric domain during result-type
resolution (BIGINT, or DECIMAL(1,0) when a DECIMAL branch participates) so the
result keeps the numeric type and TRUE/FALSE evaluate as 1/0, matching MySQL.
Mixtures the numeric domains cannot resolve now raise a resolution error
instead of silently folding to BOOL. Also validate declared precision when
assigning BOOL to DECIMAL: TRUE requires one integral digit, so targets with
width <= scale (e.g. DECIMAL(1,1)) reject it; FALSE/NULL still convert, and
explicit CAST keeps its existing clamp behavior.

Related to matrixorigin#31200
@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 Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

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

Commit: 3a9cb017148412db42f4324e54655e6f922bd1f1

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: 3a9cb017148412db42f4324e54655e6f922bd1f1

Assessment: approve

The BOOL/numeric fix preserves numeric value branches while retaining condition handling, NULLs and existing nonnumeric behavior. A small shared wrapper reuses the authoritative type resolvers and existing casts; the decimal stand-in preserves the required integral digit without unnecessary precision growth. The changed SQL and dependent-query expectations have independent value/type oracles. No actionable correctness, design or required-validation gap remains.

Model: gpt-6.1-sol / max

Session: 01a1a004-9884-72c0-ad39-f096c4209c4a · slot 1 · generation 0

Validation:

  • Reviewed every hunk in all eight changed files, the complete indexed discussion and linked issue #31200, and the affected type-resolution, cast, IFNULL, UNION and window consumers.

  • Reused the supplied exact-head SUCCESS conclusions for shared build, Ubuntu UT, arm64 SCA, UT coverage and compose BVT. Skipped/neutral lanes were not counted as passes; selections and times are retained in evidence-reuse.json.

  • PR-reported focused seven-root tests, function/plan package checks, dependent-stage regression and SQL BVT results were assessed as attributed evidence, not local executions.

  • Local bounded frozen-source audit passed: exact HEAD, clean checkout, eight-file coverage, matching supplied diff apart from terminal line termination, unchanged baseline cast implementation, git diff --check and complete discussion-key coverage. See frozen-audit.log and the matching checks receipt.

Limitations:

  • No local Go/native build or SQL execution was performed. The supplied CI snapshot provides conclusions rather than raw CI logs; local_validation is empty.

  • The author's reported unrelated JDBC metadata-width differences on both baseline and candidate were not independently reproduced. Source review and changed-path evidence establish no corresponding regression.

@gouhongshen

Copy link
Copy Markdown
Contributor Author

@Mergifyio queue

@mergify

mergify Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

🚫 Left the queue · pull request dequeued

2 h 49 min in the queue, with no time running CI.

So far

  • ✅ Entered the queue Wed 4 Nov, 13:53 · rule main · triggered by @gouhongshen with the @mergifyio queue command
  • ⏹️ Testing stopped · in place
  • 🚫 Left the queue 16:42 · 4cbb375

Reason

Pull request #32162 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.

To get back in the queue

  • Push a change if one is needed: the pull request is queued again once its queue conditions match.
  • If no change is needed, tick the box below to queue it again as it is.

Merge conditions not met when it left the queue

  • any of · required by GitHub branch protection:
    • check-neutral = CI Required
    • check-skipped = CI Required
    • check-success = CI Required

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

  • Requeue this pull request
2 merge conditions met
  • #review-threads-unresolved = 0 · required by GitHub branch protection
  • github-review-approved · required by GitHub branch protection

Times in UTC · dashboard · how the merge queue works

@mergify mergify Bot added the queued label Nov 4, 2026

This branch was successfully deployed

No deployments
ci — 4cbb375e Deployed Nov 4, 2026 by mergify[bot]
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