Skip to content

[Bug]: Nested NATURAL JOIN reorders star columns and writes wrong positional values through VIEW #32599

Closed

Description

@Ariznawlll

Description

For a parenthesized, nested NATURAL JOIN, MatrixOne emits common columns in a different SELECT * order from MySQL. The order is persisted in a VIEW and changes positional INSERT ... SELECT * values. This is a JOIN binding/column-order defect, not three separate VIEW, PREPARE, and INSERT bugs.

Environment

  • Branch: main
  • Tested commit: c8accd138bb91144050ff173100afd7a71a02888 (2026-11-12)
  • Topology: isolated local 1 LogService / 1 TN / 2 CN; SQL ports 16001 and 16002
  • Comparator: isolated MySQL 8.0.45; testcase from official MySQL 8.0 source
  • Freshness check: upstream main advanced to 4760bcaa88b23fabe064b1631b624c0e8c40d549 after the run; the intervening commit does not change pkg/sql/plan, pkg/sql/parsers, or pkg/frontend production files.

Steps to reproduce

Run in a fresh database:

CREATE TABLE t1 (c INT, b INT);
CREATE TABLE t2 (a INT, b INT);
CREATE TABLE t3 (b INT, c INT);
INSERT INTO t1 VALUES (10,1),(3,1),(3,2);
INSERT INTO t2 VALUES (2,1);
INSERT INTO t3 VALUES (1,3),(1,10);

CREATE VIEW v3b AS
  SELECT * FROM t1 NATURAL JOIN (t2 NATURAL JOIN t3);
SELECT * FROM v3b ORDER BY 1,2,3;

CREATE TABLE sink (first_col INT, second_col INT, third_col INT);
INSERT INTO sink SELECT * FROM v3b;
SELECT * FROM sink ORDER BY 1,2,3;

Actual behavior

MatrixOne VIEW columns are b,c,a and the ordered rows are (1,3,2),(1,10,2). The direct parenthesized JOIN has the same order. sink stores those positional values.

Expected behavior

MySQL 8.0.45 VIEW columns are c,b,a and the rows are (3,1,2),(10,1,2); sink stores those values. SQL clients and positional writes must see the same visible column order for this supported JOIN shape.

Stability and controls

  • Reproducer: 6/6 fresh databases (3 independent rounds × both CN entrypoints).
  • Non-parenthesized t1 NATURAL JOIN t2 NATURAL JOIN t3 matches MySQL. Explicit SELECT c,b,a FROM v3b matches MySQL, proving the named values are intact while * ordering is wrong.
  • VIEW-of-VIEW, JOIN of VIEWs, and SQL PREPARE/EXECUTE exhibit the same order. Both servers reject an unknown column; subsequent SELECT 1 succeeds.
  • INSERT ... SELECT * succeeds on both servers but writes different complete rows, so this is not merely presentation metadata.

Evidence

  • Official MySQL source cases: mysql-test/t/join.test:403-430 and join.test:554-558.
  • Exact observed 6/6: MO columns [b,c,a], rows [(1,3,2),(1,10,2)]; MySQL columns [c,b,a], rows [(3,1,2),(10,1,2)]. The same values are observed in the positional sink table.

Code analysis

Likely cause: query_builder.go derives NATURAL JOIN usingCols by traversing rightCtx.bindings in underlying table order, rather than the right nested JOIN's visible output order. bind_context.go emits those merged columns first during * expansion. This is a source-level hypothesis consistent with the black-box result.

Regression coverage

Add a public SQL regression for parenthesized versus flat NATURAL JOIN, with exact column names and values through direct SELECT, VIEW, PREPARE and positional INSERT. Include an invalid-column error followed by successful reuse.

Related

Activity

  1. added this to the MO-202611 milestone on Nov 12, 2026
  2. added
    kind/bug
    on Nov 12, 2026
  3. added theissue type on Nov 12, 2026
  4. Ariznawlll commented on Nov 13, 2026

    @Ariznawlll
    CollaboratorAuthor

    补充 MySQL 官方 JOIN 语料中的同类复现(属于本 issue 的 NATURAL JOIN ... SELECT * 列顺序问题,不另立 issue):

    • 测试版本:MO main@bbfc12bab4f410c26dbf06aa694acba51aba1734(runtime 8.0.30-MatrixOne-v1.3.0),MySQL 8.0.45;MySQL 源码测试 SHA 9fc8d60003b4eee32767c1e30942f222c3508c31。以下是每条官方语料各一次的结果,不作为最新 main 的 3/3 复测。
    • t/join.test:439、:478(parenthesized NATURAL RIGHT JOIN,后者再加 GROUP BY/HAVING):MO 列名顺序 c,b,a,y,MySQL 为 b,c,a,y;两端行数相同,分别为 3 行和 2 行,样例值按列位置错开。
    • t/join.test:449(右侧深层嵌套 NATURAL JOIN):MO b,c,a,y,MySQL c,b,a,y,两边均 2 行;:493(NATURAL JOIN 右侧为带 ON 的 JOIN)也出现相同类的列顺序差异,两边均 1 行。
    • t/join.test:526、:530(两侧先各自投影为 derived table,再做 NATURAL JOIN / NATURAL LEFT JOIN):MO c,b,a,y,MySQL b,c,a,y;两边分别均为 2 行和 3 行。:558(两个 VIEW 再做 NATURAL JOIN)MO c,b,a,MySQL b,c,a,两边均 1 行。
    • 相邻控制如 t/join.test:435、:437、:441、:445、:447 和限定列的 :559 在两端匹配;因此不是所有 NATURAL JOIN 或行数都不一致,而是特定嵌套输出的 SELECT * 列顺序/位置值问题。
    • 当前官方 main 为 a626dd045a656c801b6730732adc16df056379d3;本轮未能在该 SHA 上启动服务重跑这些 SQL,以上证据只对应所列 bbfc12 构建。
  5. Ariznawlll commented on Nov 13, 2026

    @Ariznawlll
    CollaboratorAuthor

    结论

    补充 #32599 在当前 main 上的同根复现,不另开 issue。这组结果不是修复验收结论;它扩展了问题边界:多列 NATURAL JOIN 的 SELECT * 合并列顺序会跟随 MO 右侧输入的列序,而 MySQL 按左侧可见输入的公共列序输出。

    关键证据

    • 版本:MO main@a626dd045a656c801b6730732adc16df056379d3,隔离本地双 CN(本轮 SQL 走 CN1);MySQL 8.3.0。双方各在 3 个独立新 schema 重跑,结果一致。
    • 结构:l(note,k2,k1),视图 v AS SELECT note,k2,k1 FROM l;r(k1,k2,rnote)。v NATURAL JOIN r 时,MySQL SELECT */VIEW 列序为 k2,k1,note,rnote,MO 为 k1,k2,note,rnote。反转输入成 r NATURAL JOIN v 后,MySQL 为 k1,k2,rnote,note,MO 为 k2,k1,rnote,note。两种方向行数均为 5/5;LEFT 变体计数也相同(6/6、反向 7/7),夹有重复匹配和 NULL 键。
    • INSERT INTO dst SELECT * FROM v NATURAL JOIN r 双方都插入 5 行,但首行 MySQL 为 (B10,A1,L1,R1),MO 为 (A1,B10,L1,R1);三次新 schema 均复现。显式 SELECT v.k2,v.k1,v.note,r.rnote 的行值一致,说明差异在星号投影顺序,不是匹配集合或基础存储数据。
    • 白盒:当前代码在 query_builder.go 按 rightCtx.bindings 顺序生成 NATURAL JOIN 的 usingCols,随后 bind_context.go 按该顺序展开合并列;与两种输入方向观察到的右侧顺序相符。

    待解决点

  6. added
    severity/s0
    on Nov 15, 2026
  7. added a commit that references this issue on Nov 15, 2026
    470a57c
  8. XuPeng-SH commented on Nov 15, 2026

    @XuPeng-SH
    Contributor

    QA handoff: NATURAL JOIN visible-column order

    #32776 merged into main at 2026-11-15 21:54 UTC as 470a57c2ce5f0155d260bc3e1ec0cc803bb34ded (reported final PR head d80a70020fb915f3398b57e5196acb27c137f760).

    The merged fix derives common-column order from each operand's visible schema, including nested joins: INNER/LEFT/FULL follow the left operand and RIGHT follows the preserved right operand. Ordinary and correlated JSON_TABLE paths share discovery; ambiguity checks, equality predicates, FULL coalescing, explicit USING and qualified-star contracts remain. The earlier bbfc12/a626dd counterexamples precede this repair and are not evidence of failure on the merged build. No unfinished development follow-up was found in the current issue/PR discussion.

    Remote evidence, not local tests performed for this handoff:

    • Author reports full planner tests, 55/55 changed SQL checks twice, 52/52 static-view checks, and static/pre-push checks.
    • Independent review approves 2dbc4b8f980e32d42748b872f38543bc1967d2bf: full planner and focused regressions, baseline RED control, two 55/55 SQL runs, 52/52 view control, plus unmatched RIGHT, nested FULL/RIGHT and correlated JSON_TABLE probes. This review SHA is distinct from the final PR head/merge. Reviewer execution was Linux amd64 CPU, standalone; no multi-CN, GPU or race run, and author static-check claims lacked raw artifacts.
    • Merge-queue record reports CI Required satisfied and successful merge.

    Please verify exact column metadata and complete positional values on the merged build, including both input directions, nested/derived/VIEW-of-VIEW shapes from the two comments, duplicates/NULLs, LEFT/RIGHT/FULL controls, PREPARE and INSERT ... SELECT, and error followed by connection reuse. Repeat across both CN entrypoints where available.

    Existing views retain their persisted explicit projections; creating/recreating a view uses the corrected order. This change does not automatically migrate old view schemas or repair rows previously inserted with the old positional order. Distinguish newly created views from persisted pre-fix views in acceptance. This is QA transfer, not acceptance or a claim of fresh local verification; no automatic backport is requested.

  9. Ariznawlll commented on Nov 16, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 官方 main:7fcd9bdff8433d72983f0399fd5c10dcab91a00a;修复 PR fix(plan): preserve NATURAL JOIN visible column order #32776 已 MERGED,merge commit 470a57c2ce5f0155d260bc3e1ec0cc803bb34ded 在该 main 的祖先链。
    • 隔离部署:该 main 本地编译的 mo-service,1 LOG / 1 TN / 1 CN,SQL 端口 17101,全新数据目录;会话确认 git_version() = 7fcd9bdff8。
    • 测试日期:2026-11-16。

    原问题验证

    按 issue 步骤构造 t1/t2/t3 与 v3b:MO 的 VIEW 列序现在为 c,b,a,有序行为 (3,1,2),(10,1,2),与 MySQL 8.0.45 期望一致;位置式 INSERT INTO sink SELECT * FROM v3b 写入 (3,1,2),(10,1,2)。原报告的 b,c,a 列序与 (1,3,2),(1,10,2) 位置值未复现。

    Happy Path 覆盖

    1. 直接括号嵌套 t1 NATURAL JOIN (t2 NATURAL JOIN t3)、VIEW、VIEW-of-VIEW、PREPARE/EXECUTE 四条读取路径的 SELECT * 列序全部为 c,b,a,行值逐列一致。
    2. 位置式 INSERT ... SELECT * 经 VIEW 持久化到 sink 的值与 MySQL 逐列一致((3,1,2),(10,1,2)),列元数据与位置写入同源。
    3. 平铺 t1 NATURAL JOIN t2 NATURAL JOIN t3 保持 MySQL 自身的 b,c,a 顺序(对照 mysql-test join.test:554-558),显式 SELECT c,b,a 命名投影不受星号展开影响。
    4. RIGHT JOIN USING / NATURAL RIGHT JOIN 保持 [Compatibility]: RIGHT JOIN USING and NATURAL star columns are ordered from the wrong side #28258 修复的保留右序 b,a,c(行 (1,2,10),(1,2,3));嵌套 LEFT NATURAL JOIN 按左可见操作数序 c,b,a 输出,未匹配行 NULL 扩展正确((3,2,NULL))。

    Unhappy Path 覆盖

    1. 引用未知列 zzz 报 ERROR 1054,随后 SELECT 1 会话正常复用。
    2. PREPARE 语句含非法列在 prepare 阶段即被拒绝(ERROR 1054),句柄名可复用:重新 PREPARE 同名语句并 EXECUTE 成功。

    白盒验证

    核对已合入的 #32776:usingCols 推导不再遍历 rightCtx.bindings 的底层表序,而是按每个操作数的可见输出序合并(INNER/LEFT/FULL 跟随左可见操作数,RIGHT 跟随保留的右操作数),并与 bind_context 的星号展开可见性规则共享实现;与上述黑盒结果一致。未修改产品源码。

    回归测试

    已提交 matrixorigin/motr#349:14_issue_regression/issue_32599_natural_join_star_order,覆盖上述全部 happy + unhappy 路径。本 issue 与修复 PR 均落在 main(milestone MO-202611,无 4.2-dev backport),故回归 PR 目标为 motr main 分支。

    测试证据

    本地 motr --record 建立基线后,diff 模式连续复跑两轮,并与 #32443 回归用例 -j 2 并行一轮:每轮 32/32 语句全绿、输出逐字节稳定;result 基线随 PR #349 提交。

    测试结论

    测试通过。

  10. Ariznawlll commented on Nov 16, 2026

    @Ariznawlll
    CollaboratorAuthor

    验证通过(comment),回归测试已提交 motr#349。关闭。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s0

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions