Skip to content

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

Closed

Description

@Ariznawlll

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

  1. added this to the MO-202609 milestone on Sep 5, 2026
  2. added
    kind/bug
    on Sep 5, 2026
  3. added theissue type on Sep 5, 2026
  4. self-assigned this
    on Sep 5, 2026
  5. XuPeng-SH commented on Sep 9, 2026

    @XuPeng-SH
    Contributor

    Implemented in PR #28464.

    Root cause: unqualified * expansion always traversed a join binding tree left-to-right. addUsingCol already 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 ... USING and NATURAL RIGHT JOIN, traverses right before left after emitting merged columns, and leaves RIGHT JOIN ... ON and 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.

  6. added a commit that references this issue on Sep 9, 2026
    91d7297
  7. Ariznawlll commented on Sep 9, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:cd04bb4c1af5bc595e2147dc645dfa754f4c395b
    • 部署方式:本地单 CN(最新 main 从该 SHA 构建)
    • 测试日期:2026-09-09

    原问题验证

    按 issue 的 SELECT * 复现形态,以两行右表数据(其中一行无左表匹配)连续执行 3 轮。RIGHT JOIN ... USING(id) 和 NATURAL RIGHT JOIN 的输出列均为 id, rv, ronly, lv, lonly,右表列在合并键后优先;原先 VIEW、CTAS、PREPARE 会回到左表优先的问题未复现。

    Happy Path 覆盖

    1. 直接 RIGHT JOIN ... USING 与 NATURAL RIGHT JOIN 均返回右表优先的列序和正确的 NULL 扩展行。
    2. CREATE VIEW、CREATE TABLE AS SELECT 及 SQL PREPARE/EXECUTE 三条物化/计划路径均保持同一列序。
    3. RIGHT JOIN ... ON 控制组保持正常的左到右列序,未受到本次修复影响。

    Unhappy Path 覆盖

    1. USING(no_such_column) 稳定返回 ERROR 1054。
    2. 失败后在同一会话继续执行有效 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 基线。

    测试结论

    测试通过。

  8. added 3 commits that reference this issue on Sep 17, 2026
    65bf99c
    a26888f
    0a11d22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions