Repository navigation
Description
Problem
For RIGHT JOIN ... USING and NATURAL RIGHT JOIN, MatrixOne expands an unqualified * as:
merged USING columns, left-only columns, right-only columns
MySQL treats the preserved right side as the first side for star ordering and expands:
merged USING columns, right-only columns, left-only columns
The joined values are correct, but the result-set schema and positional values differ. The MatrixOne order is also persisted into views and CTAS tables and returned by prepared statements.
Version
- MatrixOne main:
e7bb0572235ec4bac81eb098eb0ad8900f0065ab - MySQL: 8.3.0
Reproduction
create database codex_right_using_order;
use codex_right_using_order;
create table l(id int primary key,lv varchar(10),lonly int);
create table r(id int primary key,rv varchar(10),ronly int);
insert into l values(1,'L1',11),(2,'L2',22);
insert into r values(1,'R1',111),(3,'R3',333);
select * from l right join r using(id) order by id;
select * from l natural right join r order by id;MatrixOne metadata and rows:
id lv lonly rv ronly
1 L1 11 R1 111
3 NULL NULL R3 333
MySQL metadata and rows:
id rv ronly lv lonly
1 R1 111 L1 11
3 R3 333 NULL NULL
Persisted and prepared paths
The same order is used by MatrixOne in each of these paths:
select * from (select * from l right join r using(id)) d order by id;
create view vu as select * from l right join r using(id);
select column_name,ordinal_position
from information_schema.columns
where table_schema=database() and table_name='vu'
order by ordinal_position;
create table cu as select * from l right join r using(id);
select column_name,ordinal_position
from information_schema.columns
where table_schema=database() and table_name='cu'
order by ordinal_position;
prepare ps from
'select * from l right join r using(id) where id >= ? order by id';
set @p=1;
execute ps using @p;MatrixOne reports id,lv,lonly,rv,ronly; MySQL reports id,rv,ronly,lv,lonly. NATURAL RIGHT JOIN has the same difference in derived, view, and CTAS paths.
INNER and LEFT JOIN controls already match MySQL with left-only columns before right-only columns.
Code path
BindContext.doUnfoldStar always traverses root.left before root.right after emitting USING columns. addUsingCol correctly chooses the right binding for the merged column of a RIGHT JOIN, but the binding-tree traversal order is not adjusted for RIGHT JOIN star expansion.
Expected behavior
Unqualified star expansion for RIGHT JOIN USING/NATURAL should place the preserved right table's non-common columns before the left table's non-common columns, matching MySQL in direct queries and every schema-materializing path.
Activity
XuPeng-SH commentedon Sep 9, 2026
Implemented in PR #28464.
Root cause: unqualified
*expansion always traversed a join binding tree left-to-right.addUsingColalready selected the preserved right value for RIGHT JOIN, but the child traversal order was not adjusted, so the schema/ordinal order differed from MySQL.The fix records the order only for explicit
RIGHT JOIN ... USINGandNATURAL RIGHT JOIN, traverses right before left after emitting merged columns, and leavesRIGHT JOIN ... ONand other join types unchanged. It covers direct results, prepared statements, VIEW, and CTAS paths without changing runtime join execution.Validation: planner tests passed; the real MatrixOne regression ran 23/23 checks successfully in 3 runs.
fix: preserve RIGHT JOIN USING star order (#28464)
Ariznawlll commentedon Sep 9, 2026
测试版本
maincd04bb4c1af5bc595e2147dc645dfa754f4c395b原问题验证
按 issue 的
SELECT *复现形态,以两行右表数据(其中一行无左表匹配)连续执行 3 轮。RIGHT JOIN ... USING(id)和NATURAL RIGHT JOIN的输出列均为id, rv, ronly, lv, lonly,右表列在合并键后优先;原先 VIEW、CTAS、PREPARE 会回到左表优先的问题未复现。Happy Path 覆盖
RIGHT JOIN ... USING与NATURAL RIGHT JOIN均返回右表优先的列序和正确的 NULL 扩展行。CREATE VIEW、CREATE TABLE AS SELECT及 SQLPREPARE/EXECUTE三条物化/计划路径均保持同一列序。RIGHT JOIN ... ON控制组保持正常的左到右列序,未受到本次修复影响。Unhappy Path 覆盖
USING(no_such_column)稳定返回ERROR 1054。RIGHT JOIN ... USING(id),返回正确列序和行,未残留异常计划或会话状态。白盒验证
不适用。本轮使用公开 SQL 覆盖绑定后的直接、VIEW、CTAS 与 PREPARE 行为;未修改源码或使用故障注入。
回归测试
已加入 MOTR PR #152 的
suites/14_issue_regression/t/issue_28258_right_join_using_order.test(提交2fa992d335a19e8655e77e5a0c117b846793cedf)。新增用例连续 3 轮均为 24/24 语句通过;14_issue_regression全 suite 中该用例亦通过。测试证据
本地 runner 输出:
ISSUE_28258_RIGHT_JOIN_USING_ORDER | success | stmts=24 ok=24 fail=0;PR 包含对应稳定 result 基线。测试结论
测试通过。
fix: preserve RIGHT JOIN USING star order (matrixorigin#28464)
fix: preserve RIGHT JOIN USING star order (matrixorigin#28464)
fix: preserve RIGHT JOIN USING star order (matrixorigin#28464)