Skip to content

fix: preserve RIGHT JOIN USING star order - #28464

Merged
XuPeng-SH merged 2 commits into
mainfrom
codex/issue-28258-right-using-order-20260909
Sep 9, 2026
Merged

XuPeng-SH merged 2 commits into
mainfrom
codex/issue-28258-right-using-order-20260909

Conversation

@XuPeng-SH

Copy link
Copy Markdown
Contributor

Summary

Fixes #28258.

SELECT * for RIGHT JOIN ... USING and NATURAL RIGHT JOIN now follows
MySQL's result-column order: merged USING columns first, then the preserved
right input's non-common columns, followed by the left input's non-common
columns.

Root cause

The binder represented a join's binding tree with only left/right children and
always unfolded an unqualified star left-to-right. addUsingCol already chose
the right binding for the merged value of a right join, but that choice did not
affect child traversal, so the values were correct while the result schema was
not.

Implementation

  • Record a small binding-tree flag only for explicit RIGHT JOIN ... USING
    and NATURAL RIGHT JOIN.
  • Traverse the right child before the left child only when that flag is set.
  • Keep ordinary RIGHT JOIN ... ON, LEFT JOIN, INNER JOIN, and FULL JOIN
    star expansion unchanged.
  • Because star expansion happens during binding, the correction naturally
    propagates to direct results, prepared statements, derived tables, VIEW
    definitions, and CTAS schemas without changing join execution or adding an
    execution operator.

Correctness and unhappy paths

  • Merged columns remain emitted once and before both child projections.
  • Nested joins recurse using each node's own order, so a right-using join does
    not flatten or reorder unrelated descendants.
  • Qualified stars (left.* / right.*) are unaffected.
  • Explicit RIGHT JOIN ... ON remains a regression control and keeps the
    existing left-child-first order.

Performance

  • The change adds one boolean to the binder-only tree node and one branch in
    star expansion.
  • No runtime plan node, join algorithm, row materialization, or data scan is
    changed; execution cost is unchanged apart from the intended projection
    order.

Validation

  • go test -count=1 ./pkg/sql/plan
  • Planner regression test covering explicit USING, NATURAL RIGHT, and RIGHT
    JOIN ON control cases.
  • Local MatrixOne execution of right_join_using_order.sql: 23/23 checks
    passed in each of 3 runs, covering direct query results, VIEW and CTAS
    ordinal positions, prepared execution, and the RIGHT JOIN ON control. The
    mo-tester multi-run wrapper reports a pre-existing non-zero status after the
    successful runs because its report-move step cannot find report/*.txt.
  • git diff --check

The full repository CI has not been waited on locally.

@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 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deep review of exact head 843488b against base/merge-base f0c31cd. COMMENT only, as requested for this author-owned PR.

Conclusion: no confirmed blocker introduced by this change.

Design/correctness: all six changed files were reviewed together with mergeContexts, addUsingCol, qualified/unqualified star expansion, and select-list/provenance consumers. This is a suitably narrow R2 public-result correction, not a feature needing a separate design document. Recording surface ordering on the binder tree avoids coupling SQL column order to physical join rewrites. The right-before-left behavior for USING/NATURAL RIGHT, versus unchanged ordinary RIGHT JOIN ON, agrees with the MySQL JOIN result examples.

The flag is installed on the fresh join node after successful context merge; child nodes retain their own flags. Merged columns are still emitted first with the already-selected right-side expression, and the scoped visitedUsingCols cleanup remains symmetric regardless of traversal order. Expressions, headings and heading provenance are appended together, so projection ordinals stay aligned. Qualified stars bypass this traversal. The physical join children, equality operands, NULL extension and executor algorithms are unchanged; the reordered bound projection is the normal downstream contract for results, derived tables, prepared plans, views and CTAS.

Failure/resource/performance review: existing missing/ambiguous USING-column and duplicate-table errors still terminate binding; no new wait, goroutine, shared cache, external resource, wire/catalog field or cleanup owner is introduced. Allocation/work remain bounded by the same binding tree and output columns, with one binder-only boolean and branch. No new runtime race/fault or scale test is warranted for that closure.

Testing: the three lightweight planner cases provide the positive paths and nearest ON control. The small BVT checks actual preserved-side rows/NULLs, headings, view/CTAS ordinal metadata, and prepared execution, with cleanup and deterministic ordering. Non-blocking strengthening: add nested mixed RIGHT-USING/LEFT-or-ON joins (including the same USING name at adjacent levels), qualified stars, NATURAL RIGHT with no common columns, and multi-column USING with differing declaration/list order. These exercise the recursive-order claim and distinguish it from the pre-existing merged-column-order logic; that logic itself is unchanged by this PR.

Evidence limits: independently inspected exact-head source and expected results, but did not run the planner package or SQL BVT locally. The PR reports planner tests and 23/23 SQL checks in each of three runs, while explicitly reporting a non-zero mo-tester wrapper result from report movement; I do not classify that wrapper as a clean green run. Exact-head MatrixOne ALL CI run 34306130860 was in progress when inspected (auxiliary PR workflow passed; Iceberg skipped). The pending-CI gate was explicitly waived, and no polling was performed. mo-self-review's change-map and counterexample checks guided this review; no repository files were changed.

@XuPeng-SH
XuPeng-SH merged commit 91d7297 into main Sep 9, 2026
34 checks passed
@XuPeng-SH
XuPeng-SH deleted the codex/issue-28258-right-using-order-20260909 branch September 9, 2026 04:34
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## Summary

Fixes matrixorigin#28258.

`SELECT *` for `RIGHT JOIN ... USING` and `NATURAL RIGHT JOIN` now
follows
MySQL's result-column order: merged `USING` columns first, then the
preserved
right input's non-common columns, followed by the left input's
non-common
columns.

## Root cause

The binder represented a join's binding tree with only left/right
children and
always unfolded an unqualified star left-to-right. `addUsingCol` already
chose
the right binding for the merged value of a right join, but that choice
did not
affect child traversal, so the values were correct while the result
schema was
not.

## Implementation

- Record a small binding-tree flag only for explicit `RIGHT JOIN ...
USING`
  and `NATURAL RIGHT JOIN`.
- Traverse the right child before the left child only when that flag is
set.
- Keep ordinary `RIGHT JOIN ... ON`, `LEFT JOIN`, `INNER JOIN`, and
`FULL JOIN`
  star expansion unchanged.
- Because star expansion happens during binding, the correction
naturally
propagates to direct results, prepared statements, derived tables, VIEW
definitions, and CTAS schemas without changing join execution or adding
an
  execution operator.

## Correctness and unhappy paths

- Merged columns remain emitted once and before both child projections.
- Nested joins recurse using each node's own order, so a right-using
join does
  not flatten or reorder unrelated descendants.
- Qualified stars (`left.*` / `right.*`) are unaffected.
- Explicit `RIGHT JOIN ... ON` remains a regression control and keeps
the
  existing left-child-first order.

## Performance

- The change adds one boolean to the binder-only tree node and one
branch in
  star expansion.
- No runtime plan node, join algorithm, row materialization, or data
scan is
changed; execution cost is unchanged apart from the intended projection
  order.

## Validation

- `go test -count=1 ./pkg/sql/plan`
- Planner regression test covering explicit USING, NATURAL RIGHT, and
RIGHT
  JOIN ON control cases.
- Local MatrixOne execution of `right_join_using_order.sql`: 23/23
checks
  passed in each of 3 runs, covering direct query results, VIEW and CTAS
ordinal positions, prepared execution, and the RIGHT JOIN ON control.
The
mo-tester multi-run wrapper reports a pre-existing non-zero status after
the
successful runs because its report-move step cannot find `report/*.txt`.
- `git diff --check`

The full repository CI has not been waited on locally.

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## Summary

Fixes matrixorigin#28258.

`SELECT *` for `RIGHT JOIN ... USING` and `NATURAL RIGHT JOIN` now
follows
MySQL's result-column order: merged `USING` columns first, then the
preserved
right input's non-common columns, followed by the left input's
non-common
columns.

## Root cause

The binder represented a join's binding tree with only left/right
children and
always unfolded an unqualified star left-to-right. `addUsingCol` already
chose
the right binding for the merged value of a right join, but that choice
did not
affect child traversal, so the values were correct while the result
schema was
not.

## Implementation

- Record a small binding-tree flag only for explicit `RIGHT JOIN ...
USING`
  and `NATURAL RIGHT JOIN`.
- Traverse the right child before the left child only when that flag is
set.
- Keep ordinary `RIGHT JOIN ... ON`, `LEFT JOIN`, `INNER JOIN`, and
`FULL JOIN`
  star expansion unchanged.
- Because star expansion happens during binding, the correction
naturally
propagates to direct results, prepared statements, derived tables, VIEW
definitions, and CTAS schemas without changing join execution or adding
an
  execution operator.

## Correctness and unhappy paths

- Merged columns remain emitted once and before both child projections.
- Nested joins recurse using each node's own order, so a right-using
join does
  not flatten or reorder unrelated descendants.
- Qualified stars (`left.*` / `right.*`) are unaffected.
- Explicit `RIGHT JOIN ... ON` remains a regression control and keeps
the
  existing left-child-first order.

## Performance

- The change adds one boolean to the binder-only tree node and one
branch in
  star expansion.
- No runtime plan node, join algorithm, row materialization, or data
scan is
changed; execution cost is unchanged apart from the intended projection
  order.

## Validation

- `go test -count=1 ./pkg/sql/plan`
- Planner regression test covering explicit USING, NATURAL RIGHT, and
RIGHT
  JOIN ON control cases.
- Local MatrixOne execution of `right_join_using_order.sql`: 23/23
checks
  passed in each of 3 runs, covering direct query results, VIEW and CTAS
ordinal positions, prepared execution, and the RIGHT JOIN ON control.
The
mo-tester multi-run wrapper reports a pre-existing non-zero status after
the
successful runs because its report-move step cannot find `report/*.txt`.
- `git diff --check`

The full repository CI has not been waited on locally.

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## Summary

Fixes matrixorigin#28258.

`SELECT *` for `RIGHT JOIN ... USING` and `NATURAL RIGHT JOIN` now
follows
MySQL's result-column order: merged `USING` columns first, then the
preserved
right input's non-common columns, followed by the left input's
non-common
columns.

## Root cause

The binder represented a join's binding tree with only left/right
children and
always unfolded an unqualified star left-to-right. `addUsingCol` already
chose
the right binding for the merged value of a right join, but that choice
did not
affect child traversal, so the values were correct while the result
schema was
not.

## Implementation

- Record a small binding-tree flag only for explicit `RIGHT JOIN ...
USING`
  and `NATURAL RIGHT JOIN`.
- Traverse the right child before the left child only when that flag is
set.
- Keep ordinary `RIGHT JOIN ... ON`, `LEFT JOIN`, `INNER JOIN`, and
`FULL JOIN`
  star expansion unchanged.
- Because star expansion happens during binding, the correction
naturally
propagates to direct results, prepared statements, derived tables, VIEW
definitions, and CTAS schemas without changing join execution or adding
an
  execution operator.

## Correctness and unhappy paths

- Merged columns remain emitted once and before both child projections.
- Nested joins recurse using each node's own order, so a right-using
join does
  not flatten or reorder unrelated descendants.
- Qualified stars (`left.*` / `right.*`) are unaffected.
- Explicit `RIGHT JOIN ... ON` remains a regression control and keeps
the
  existing left-child-first order.

## Performance

- The change adds one boolean to the binder-only tree node and one
branch in
  star expansion.
- No runtime plan node, join algorithm, row materialization, or data
scan is
changed; execution cost is unchanged apart from the intended projection
  order.

## Validation

- `go test -count=1 ./pkg/sql/plan`
- Planner regression test covering explicit USING, NATURAL RIGHT, and
RIGHT
  JOIN ON control cases.
- Local MatrixOne execution of `right_join_using_order.sql`: 23/23
checks
  passed in each of 3 runs, covering direct query results, VIEW and CTAS
ordinal positions, prepared execution, and the RIGHT JOIN ON control.
The
mo-tester multi-run wrapper reports a pre-existing non-zero status after
the
successful runs because its report-move step cannot find `report/*.txt`.
- `git diff --check`

The full repository CI has not been waited on locally.

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>

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

size/M Denotes a PR that changes [100,499] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Compatibility]: RIGHT JOIN USING and NATURAL star columns are ordered from the wrong side

2 participants