Skip to content

fix(plan): preserve numeric UNION types with untyped NULL - #32461

Merged
XuPeng-SH merged 5 commits into
matrixorigin:4.1.6-hotfixfrom
ck89119:issue-32326-4.1.6-hotfix
Nov 10, 2026
Merged

XuPeng-SH merged 5 commits into
matrixorigin:4.1.6-hotfixfrom
ck89119:issue-32326-4.1.6-hotfix

Conversation

@ck89119

@ck89119 ck89119 commented Nov 10, 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:

Fixes #32326

What this PR does / why we need it:

Bare NULL projections are defaulted to text before set-operation type reconciliation. Consequently, numeric UNION queries and their CTAS consumers resolve to VARCHAR and sort lexicographically (NULL, 10, 2 instead of NULL, 2, 10).

  • Preserve bare-NULL projection provenance in the binding context and exclude its default execution type from shared set-operation type reconciliation.
  • Exclude bare-NULL placeholders from CHAR/VARCHAR width reconciliation too, so their implementation width of 4 cannot widen declared CHAR(1)/VARCHAR(2) results. Preserve widening for real strings and explicitly typed NULLs, and retain the concrete all-NULL fallback.
  • Reconcile string widths after choosing the common type, for both implicit-cast and no-cast paths, before casting any arm. This prevents mixed VARCHAR/CHAR inputs from truncating long values, collapsing UNION DISTINCT rows, or persisting truncated CTAS data. Existing conservative implicit-conversion capacities remain lower bounds.
  • Keep concrete execution types for all-NULL branches, including nested set operations and branch-local ORDER BY/LIMIT; propagate provenance only through direct set-operation arms.
  • Preserve explicitly typed NULLs, real string branches, derived-table column types, and the existing set-operation/CTAS nullability rules.
  • Copy NULL AST literals rather than mutating them, so repeated CTE binding retains the original provenance. Handle parenthesized NULL projections consistently.
  • Preserve provenance through the internal SELECT DISTINCT * wrapper used when folding repeated parenthesized UNION arms. This is limited to the synthetic wrapper and does not relax user-written derived-table type boundaries.
  • Add 60 planner regression scenarios, each checking ordinary SELECT and CTAS types/nullability (including 20 string-width scenarios), plus an error-path check and SQL regression coverage for numeric ordering/arithmetic, COUNT, branch reversal, nesting, repeated-arm folding, NULL rows, declared-string controls, persisted string lengths, full string values, and DISTINCT cardinality.

Validation on macOS arm64 with Go 1.26.4 and native libraries built in the isolated worktree:

  • Reproduced the original defect on the exact hotfix baseline ef9256eca2c6436619272927e97f629af41ebd55, which already includes fix(plan): preserve CTAS output nullability on 4.1.6 hotfix #32085. The planner regression fails against unchanged baseline product code for numeric-versus-VARCHAR type mismatches.
  • Reproduced the repeated-arm folding omission on aefc868bb0, then verified numeric SELECT/CTAS types and ordering after the repair. Declared CHAR and genuine derived-table controls retain their existing string types.
  • Reproduced the bare-NULL width omission on 4d9e5d6566: seven scenarios failed for SELECT and CTAS with expected width 1/2 versus actual width 4. Left/right NULLs, nesting/folding, typed-NULL widths, real-string widening, and the all-NULL fallback now pass.
  • Reproduced mixed-string truncation on e8e06ba2b1: the exact reported query returned NULL/a/a, CTAS stored VARCHAR(1), and DISTINCT produced only 2 rows. The original hotfix baseline and MySQL 8.0.45 retained abcdefgh and 3 rows. The repaired candidate matches MySQL for the exact witness, including full SELECT/CTAS values, VARCHAR(8) metadata, and DISTINCT cardinality. Seven of eight mixed-string planner scenarios failed before the repair; all eight now pass, including no-NULL, reversed-order, nested, folded-NULL, explicit-VARCHAR, and typed-NULL controls.
  • Merged the hotfix base a5a8ed45fc ([4.1.6-hotfix] fix(index): fail open for NaN scalar ZoneMaps #32431); it remains the current base. Focused regression, the full planner package, incremental static checks, service build, and SQL/BVT checks were rerun for the latest repair.
  • Focused regression and the full pkg/sql/plan unit-test package passed through the controlled CGo wrapper. Changed production statement-block coverage: 100% (27/27); overall planner package coverage: 61.3%.
  • Go 1.26.4 gofmt/vet and incremental golangci-lint v2.6.2 checks passed.
  • The new BVT case passed 86/86 statements on repeated same-instance runs after reviewing the generated result. Teardown checks confirmed the test database was removed.
  • The UNION BVT directory passed 182/182 statements, including the existing NULL/Top regression. The existing CTAS case passed 1176 statements with 0 failures and 30 existing ignored statements.
  • Numeric UNION/shared-contract SQL comparisons against MySQL 8.0.45 passed in earlier validation; the latest mixed-string witness was independently compared again. The explicit genuine-derived-table negative control intentionally retains MO's existing VARCHAR boundary, whereas MySQL optimizes that folded shape to BIGINT; this known difference is recorded separately and remains strictly asserted in planner and BVT tests. The expanded full SQL suite was not rerun against MySQL.

This is a shared planner fix, not a CTAS-only cast workaround. No catalog/wire format or execution-operator changes are introduced. Validation used a single-CN instance; full CI, multi-CN testing, and exact MySQL 8.0.44 comparison were not run. Existing all-NULL fallback metadata differences, conservative conversion capacities in some branch orders, and broader derived-table compatibility are outside this repair; this PR does not claim complete MySQL schema-width equivalence.

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

Copy link
Copy Markdown
Contributor

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

Commit: 4d9e5d6566e8487f7eabbc0180db98456bdf8ad8

Review discussion changed before publication; a fresh contextual review is queued.

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

发现 1 个需要修复的结果类型问题,详细复现见行内评论。其他已检查的数值、时间、嵌套集合以及显式类型 NULL 场景中,没有发现新的阻断问题;现有相关 planner 测试通过。额外的 CTAS planner 断言复现了此问题。历史上没有需要逐条关闭的人工 review 评论。

Comment thread pkg/sql/plan/query_builder.go Resolved
@XuPeng-SH

XuPeng-SH commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

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

Commit: 4d9e5d6566e8487f7eabbc0180db98456bdf8ad8

"interrupted"

@XuPeng-SH

XuPeng-SH commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

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

Commit: beb2bae72cea473792910d8f750a5799ae3a4e0f

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: beb2bae72cea473792910d8f750a5799ae3a4e0f

Assessment: approve

The fix uses shared set-operation reconciliation with bounded projection provenance, preserving numeric NULL semantics without a CTAS-specific workaround. Width reconciliation now excludes bare-NULL placeholders and precedes casts, addressing the prior P2 and mixed-string truncation. Typed inputs, derived-table boundaries and existing nullability rules remain intact. No actionable findings remain.

Model: gpt-6.1-sol / max

Session: 01a1a004-90c3-73b1-acc9-3d4636452003 · slot 0 · generation 0

Validation:

  • Reviewed all five changed files, affected binding/type/CTAS consumers, all six discussion entries and linked issue #32326.

  • Local static audit passed: frozen clean source, whitespace, gofmt, 86-statement SQL/result alignment and complete discussion coverage. Receipt: checks/1794309962570718205-4.json; log: review-audit.log.

  • Frozen-head CI records 12 successful checks, including UT, static analysis, coverage and proxy/pessimistic multi-CN BVT; 13 skipped and two neutral checks are not passes.

  • Assessed attributed focused/full planner, repeated BVT/CTAS runs and exact-hotfix baseline/MySQL 8.0.45 comparisons, including full-string values and DISTINCT cardinality.

Limitations:

  • No local Go/SQL runtime suites were repeated; local_validation is empty and raw author execution logs were not supplied.

  • Exact MySQL 8.0.44 and full expanded-suite MySQL comparisons were not established; existing conservative widths and genuine-derived-table differences remain outside this repair.

This branch was previously deployed

1 inactive deployment
ci — beb2bae7 Deployed Nov 10, 2026 by ck89119
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/L Denotes a PR that changes [500,999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants