Repository navigation
Description
MySQL-compatible outer references inside local CTEs are rejected by the executor
Environment
- MatrixOne commit:
d8ddce92b1c5c172111b50aefe6b6b200b2589cb - Topology: local 1 LogService / 1 TN / 2 CN
- Test date: 2026-09-20
Compatibility gap
MySQL permits a CTE in a nested query block to refer to columns from an outer query block (since MySQL 8.0.14). MatrixOne resolves the column but leaves a correlated expression for an executor that cannot evaluate it:
20102: unsupported expression executor for ... corr:<... depth:1> now is not yet implemented
This affects both nonrecursive and recursive local CTEs. It also blocks the normal hierarchy pattern that checks each row's ancestors using a recursive CTE inside EXISTS.
Minimal reproduction
CREATE TABLE pages(id INT PRIMARY KEY, parent_id INT, active TINYINT);
INSERT INTO pages VALUES
(1,NULL,1),(2,1,0),(3,2,1),(4,3,1),(5,1,1);
-- Valid in MySQL; expected (1,NULL),(2,1),(3,2),(4,3),(5,1).
SELECT p.id,
(WITH q(n) AS (SELECT p.parent_id) SELECT n FROM q) AS parent
FROM pages p
ORDER BY p.id;
-- Valid recursive form; expected depths 1,1,2,3,1.
SELECT p.id,
(WITH RECURSIVE r(n) AS (
SELECT p.parent_id
UNION ALL
SELECT n-1 FROM r WHERE n>1
)
SELECT COUNT(*) FROM r) AS depth
FROM pages p
ORDER BY p.id;A practical hierarchy query is rejected by the same executor path:
SELECT p.id
FROM pages p
WHERE NOT EXISTS (
WITH RECURSIVE ancestors AS (
SELECT a.* FROM pages a WHERE a.id=p.parent_id
UNION ALL
SELECT a.* FROM pages a
JOIN ancestors ON ancestors.parent_id=a.id
)
SELECT * FROM ancestors WHERE active=0
)
AND active=1
ORDER BY p.id;The expected result is (1),(5); rows 3 and 4 have inactive row 2 in their ancestor chain.
Controls and stability
- An ordinary correlated scalar subquery (
SELECT p.parent_id) works. - Noncorrelated local CTEs work.
- All three forms above produced the same executor rejection in 6/6 isolated databases while alternating two CNs.
- [Bug]: non-equality correlated aggregate inside a local CTE expands scalar results into duplicate rows #29129 is a separate wrong-result bug after a correlated aggregate CTE is decorrelated; this issue is the direct executor rejection for otherwise valid outer references.
- The old [Bug]: correlated column in CTE not supported #3304 is closed and its final verification covers invalid declaration-scope references. The current examples place the CTE inside the correlated query block and are valid MySQL syntax.
Expected behavior
The outer values must be parameterized per outer row when executing the local CTE. The nonrecursive query must return each row's parent_id; the recursive query must seed and evaluate a separate ancestor chain for each outer row.
Activity
fix(plan): parameterize local CTE outer references (#29372)
slyunnn commentedon Oct 9, 2026
Regression and extended verification for #29145 (2026-10-09)
Verification Environment
origin/main23d8f61252f3017dff218b530817a240206fd762(fix: retain service owners during construction (#29676)). The runningmo-servicebinary reports the same commit,main, andgo1.27.1 linux/arm64.1 Log service / 1 TN / 2 CN / Proxy. Post-run health checks passed for Proxy6001, CN116001, and CN216002.8.0.46was used for the same valid SQL and returned matching results.What Was Covered
(1,NULL),(2,1),(3,2),(4,3),(5,1)through Proxy, CN1, and CN2; no20102executor rejection occurred.1,1,2,3,1through all three endpoints.NOT EXISTSquery returned1,5through all three endpoints.NULLseed and duplicate outer parameter values returned independent per-row results.PREPAREfollowed by twoEXECUTEcalls returned the expected rows on all three endpoints.20301, rather than leaking a correlated expression to the executor. MySQL8.0.46also rejects this invalid form (1109); the diagnostic code difference is outside the valid-query compatibility scope.Findings
The original failures are fixed on current
main. The associated repair is5b7453a924c0d8948b2ed10503a217fe1f1186a0,fix(plan): parameterize local CTE outer references, merged by PR #29372. It parameterizes supported local-CTE producers with the outer-row domain soCorrexpressions do not reach an executor that cannot evaluate them.Large-data regression is not required. The corrected contract is per-outer-row parameter binding and correlated CTE evaluation; the minimal dataset already exercises
NULL, duplicate parameter values, recursion, and ancestor traversal.MOTR / BVT Coverage
No MOTR case was added and no MatrixOne source was modified. Existing distributed BVT
test/distributed/cases/cte/local_outer_reference.sqlcovers the three reported forms and additional recursive, prepared-statement, empty-input, duplicate-parameter, and scope-safety cases introduced with PR#29372.Final Conclusion
Fixed — the three valid MySQL-compatible local CTE outer-reference scenarios now produce the expected results through Proxy, CN1, and CN2 on the latest
mainruntime.