Repository navigation
fix: preserve RIGHT JOIN USING star order - #28464
Conversation
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
XuPeng-SH
left a comment
There was a problem hiding this comment.
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.
## 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>
## 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>
## 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>
Summary
Fixes #28258.
SELECT *forRIGHT JOIN ... USINGandNATURAL RIGHT JOINnow followsMySQL's result-column order: merged
USINGcolumns first, then the preservedright 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.
addUsingColalready chosethe 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
RIGHT JOIN ... USINGand
NATURAL RIGHT JOIN.RIGHT JOIN ... ON,LEFT JOIN,INNER JOIN, andFULL JOINstar expansion unchanged.
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
not flatten or reorder unrelated descendants.
left.*/right.*) are unaffected.RIGHT JOIN ... ONremains a regression control and keeps theexisting left-child-first order.
Performance
star expansion.
changed; execution cost is unchanged apart from the intended projection
order.
Validation
go test -count=1 ./pkg/sql/planJOIN ON control cases.
right_join_using_order.sql: 23/23 checkspassed 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 --checkThe full repository CI has not been waited on locally.