Repository navigation
Description
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
mainadvanced to4760bcaa88b23fabe064b1631b624c0e8c40d549after the run; the intervening commit does not changepkg/sql/plan,pkg/sql/parsers, orpkg/frontendproduction 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 t3matches MySQL. ExplicitSELECT c,b,a FROM v3bmatches MySQL, proving the named values are intact while*ordering is wrong. - VIEW-of-VIEW, JOIN of VIEWs, and SQL
PREPARE/EXECUTEexhibit the same order. Both servers reject an unknown column; subsequentSELECT 1succeeds. 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-430andjoin.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 positionalsinktable.
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
- Closed [Compatibility]: RIGHT JOIN USING and NATURAL star columns are ordered from the wrong side #28258 fixed the preserved-right-side ordering for
RIGHT JOIN ... USING/NATURAL RIGHT JOIN. This report uses a parenthesized INNER NATURAL JOIN and a differentusingColsordering path. - Merged PR fix view select star #26964 preserves the VIEW's expanded star columns against later base-table schema changes; it does not correct this direct JOIN order.
Activity
Ariznawlll commentedon Nov 13, 2026
补充 MySQL 官方 JOIN 语料中的同类复现(属于本 issue 的
NATURAL JOIN ... SELECT *列顺序问题,不另立 issue):main@bbfc12bab4f410c26dbf06aa694acba51aba1734(runtime8.0.30-MatrixOne-v1.3.0),MySQL 8.0.45;MySQL 源码测试 SHA9fc8d60003b4eee32767c1e30942f222c3508c31。以下是每条官方语料各一次的结果,不作为最新 main 的 3/3 复测。t/join.test:439、:478(parenthesizedNATURAL RIGHT JOIN,后者再加 GROUP BY/HAVING):MO 列名顺序c,b,a,y,MySQL 为b,c,a,y;两端行数相同,分别为 3 行和 2 行,样例值按列位置错开。t/join.test:449(右侧深层嵌套NATURAL JOIN):MOb,c,a,y,MySQLc,b,a,y,两边均 2 行;:493(NATURAL JOIN右侧为带ON的 JOIN)也出现相同类的列顺序差异,两边均 1 行。t/join.test:526、:530(两侧先各自投影为 derived table,再做NATURAL JOIN/NATURAL LEFT JOIN):MOc,b,a,y,MySQLb,c,a,y;两边分别均为 2 行和 3 行。:558(两个 VIEW 再做 NATURAL JOIN)MOc,b,a,MySQLb,c,a,两边均 1 行。t/join.test:435、:437、:441、:445、:447和限定列的:559在两端匹配;因此不是所有 NATURAL JOIN 或行数都不一致,而是特定嵌套输出的SELECT *列顺序/位置值问题。a626dd045a656c801b6730732adc16df056379d3;本轮未能在该 SHA 上启动服务重跑这些 SQL,以上证据只对应所列bbfc12构建。Ariznawlll commentedon Nov 13, 2026
结论
补充 #32599 在当前 main 上的同根复现,不另开 issue。这组结果不是修复验收结论;它扩展了问题边界:多列
NATURAL JOIN的SELECT *合并列顺序会跟随 MO 右侧输入的列序,而 MySQL 按左侧可见输入的公共列序输出。关键证据
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时,MySQLSELECT */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按该顺序展开合并列;与两种输入方向观察到的右侧顺序相符。待解决点
INSERT ... SELECT *的逐列值。fix(plan): preserve NATURAL JOIN visible column order (#32776)
XuPeng-SH commentedon Nov 15, 2026
QA handoff: NATURAL JOIN visible-column order
#32776 merged into main at 2026-11-15 21:54 UTC as
470a57c2ce5f0155d260bc3e1ec0cc803bb34ded(reported final PR headd80a70020fb915f3398b57e5196acb27c137f760).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:
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.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.
Ariznawlll commentedon Nov 16, 2026
测试版本
7fcd9bdff8433d72983f0399fd5c10dcab91a00a;修复 PR fix(plan): preserve NATURAL JOIN visible column order #32776 已 MERGED,merge commit470a57c2ce5f0155d260bc3e1ec0cc803bb34ded在该 main 的祖先链。git_version() = 7fcd9bdff8。原问题验证
按 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 覆盖
t1 NATURAL JOIN (t2 NATURAL JOIN t3)、VIEW、VIEW-of-VIEW、PREPARE/EXECUTE 四条读取路径的SELECT *列序全部为c,b,a,行值逐列一致。INSERT ... SELECT *经 VIEW 持久化到 sink 的值与 MySQL 逐列一致((3,1,2),(10,1,2)),列元数据与位置写入同源。t1 NATURAL JOIN t2 NATURAL JOIN t3保持 MySQL 自身的b,c,a顺序(对照 mysql-test join.test:554-558),显式SELECT c,b,a命名投影不受星号展开影响。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 覆盖
zzz报 ERROR 1054,随后SELECT 1会话正常复用。白盒验证
核对已合入的 #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 提交。测试结论
测试通过。
Ariznawlll commentedon Nov 16, 2026
验证通过(comment),回归测试已提交 motr#349。关闭。