Skip to content

fix(plan): preserve NATURAL JOIN visible column order - #32776

Merged
mergify[bot] merged 2 commits into
matrixorigin:mainfrom
aunjgr:fix/natural-join-star-order-32599
Nov 15, 2026
Merged

mergify[bot] merged 2 commits into
matrixorigin:mainfrom
aunjgr:fix/natural-join-star-order-32599

Conversation

@aunjgr

@aunjgr aunjgr commented Nov 15, 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 #32599

What this PR does / why we need it:

Derive NATURAL JOIN common-column order from each operand's visible output schema instead of scanning flattened base-table bindings. Nested joins have already merged and reordered their common columns; using underlying table order produces an incorrect SELECT * projection and propagates different positional values through views, prepared statements and INSERT ... SELECT.

Follow the left visible operand for INNER, LEFT and FULL joins, and the preserved right operand for RIGHT joins. Share ordered common-column discovery between ordinary joins and correlated JSON_TABLE joins. Reuse existing star-expansion visibility rules, normalize and deduplicate common names, and retain the existing ambiguity checks, equality predicates and FULL JOIN coalesce handling.

Explicit USING lists and qualified stars are unchanged. Existing views retain their persisted explicit projections; creating or recreating a view uses the corrected order.

Add planner regressions for nested and flat joins, side-specific ordering, JSON_TABLE, case-insensitive names, hidden columns, tenant visibility and ambiguous common names. Extend the existing join star-order SQL regression to verify exact column metadata, view and prepared-query results, positional inserts, and successful reuse after an invalid-column error.

Discover NATURAL JOIN common columns from each operand's visible star
projection instead of its flattened base-table bindings. Nested joins
already merge and reorder their common columns, so scanning the right
bindings can produce an incorrect SELECT * order and propagate different
positional values into views, prepared statements and INSERT SELECT.

Share ordered common-column discovery between ordinary joins and
correlated JSON_TABLE joins. Follow the left visible operand for INNER,
LEFT and FULL joins and the preserved right operand for RIGHT joins.
Reuse existing visibility filtering, deduplicate merged names, and retain
addUsingCol's ambiguity, equality and coalesce handling. Explicit USING
lists and qualified stars are unchanged.

Add planner regressions for nested and simple joins, side-specific order,
JSON_TABLE, hidden columns, tenant visibility and ambiguous names. Extend
the existing join star-order BVT to check metadata, prepared execution,
positional writes and post-error reuse. Existing persisted explicit view
projections are not rewritten.

Fixes matrixorigin#32599.
@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 →

@aunjgr

aunjgr commented Nov 15, 2026

Copy link
Copy Markdown
Contributor Author

Local CPU validation on the PR head:

  • Focused NATURAL JOIN and RIGHT JOIN planner regressions: passed.
  • Full pkg/sql/plan package: passed.
  • Changed join/right_join_using_order.sql: 55/55 checks, twice on the same instance; table/view cleanup verified between runs.
  • Existing view/view_star_static_columns.sql control: 52/52 checks; schema cleanup verified.
  • Full pre-push static checks, including molint, license checks and golangci-lint: passed.

The service was built from newest main plus this fix, with CPU-only settings. No lookup-kernel changes are included. Existing persisted view projections remain unchanged, as required by their frozen-schema contract.

@matrix-meow matrix-meow added the size/M Denotes a PR that changes [100,499] lines label Nov 15, 2026
@mergify mergify Bot added the kind/bug Something isn't working label Nov 15, 2026
@mergify

mergify Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Queue requested — the merge queue status continues in this comment ↓.

@mergify

mergify Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

✅ Merged into main as 470a57c at 21:54 UTC

1 h 9 min in the queue, 1 h 7 min of it CI.

So far

  • ✅ Entered the queue Sun 15 Nov, 20:45 · rule main · triggered by @aunjgr with the merge queue checkbox
  • ✅ Passed · in place
  • ✅ Merged 21:54 · 470a57c · squash
3 merge conditions met
  • #review-threads-unresolved = 0 · required by GitHub branch protection
  • github-review-approved · required by GitHub branch protection
  • any of · required by GitHub branch protection:
    • check-success = CI Required
    • check-neutral = CI Required
    • check-skipped = CI Required

Times in UTC · dashboard · how the merge queue works

@XuPeng-SH

XuPeng-SH commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

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

Commit: 2dbc4b8f980e32d42748b872f38543bc1967d2bf

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: 2dbc4b8f980e32d42748b872f38543bc1967d2bf

Assessment: approve

The fix reuses authoritative star visibility for NATURAL JOIN ordering while preserving ambiguity checks, equality predicates, FULL JOIN coalescing and frozen-view contracts. No actionable findings.

Model: gpt-6.1-sol / max

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

Validation:

  • Focused NATURAL/RIGHT JOIN regressions and full pkg/sql/plan passed with -count=1.

  • Baseline control reproduced incorrect inner/nested ordering; RIGHT and flat-three-table controls passed.

  • Changed SQL case passed 55/55 comparisons twice on the same instance; static-view control passed 52/52. Cleanup and shutdown verified.

  • Public probes passed for unmatched RIGHT rows, nested FULL coalescing, nested RIGHT ordering and correlated JSON_TABLE filtering.

  • Service identity, native inputs, gofmt and diff checks verified. validation-evidence.json maps claimed checks to terminal receipts and retained logs.

Limitations:

  • Validation used Linux amd64 CPU and a standalone instance; no multi-CN, GPU or race runs.

  • Author-reported molint/license/golangci-lint results lacked raw artifacts and were not counted.

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

This branch was previously deployed

1 inactive deployment
ci — d80a7002 Deployed Nov 15, 2026 by mergify[bot]
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]: Nested NATURAL JOIN reorders star columns and writes wrong positional values through VIEW

3 participants