Skip to content

[Compatibility]: CTE outer references supported by MySQL are rejected by the executor #29145

Closed

Description

@Ariznawlll

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

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.

Reference: https://dev.mysql.com/doc/refman/8.0/en/with.html

Activity

  1. added this to the MO-202609 milestone on Sep 20, 2026
  2. added
    kind/compatibility
    on Sep 20, 2026
  3. added theissue type on Sep 20, 2026
  4. added
    severity/s0
    and removed on Sep 24, 2026
  5. added a commit that references this issue on Oct 8, 2026
    5b7453a
  6. assigned and unassigned on Oct 8, 2026
  7. slyunnn commented on Oct 9, 2026

    @slyunnn

    Regression and extended verification for #29145 (2026-10-09)

    Verification Environment

    • Source and actual runtime: latest origin/main 23d8f61252f3017dff218b530817a240206fd762 (fix: retain service owners during construction (#29676)). The running mo-service binary reports the same commit, main, and go1.27.1 linux/arm64.
    • Topology: 1 Log service / 1 TN / 2 CN / Proxy. Post-run health checks passed for Proxy 6001, CN1 16001, and CN2 16002.
    • Client/protocol: MySQL CLI over TCP text protocol. The original SQL was issued directly to each endpoint; the Proxy run used its frontend route and the CN runs targeted CN1/CN2 directly. All executions were serial.
    • Compatibility control: isolated local MySQL 8.0.46 was used for the same valid SQL and returned matching results.

    What Was Covered

    • OK — Original nonrecursive local-CTE scalar query returned (1,NULL),(2,1),(3,2),(4,3),(5,1) through Proxy, CN1, and CN2; no 20102 executor rejection occurred.
    • OK — Original recursive local-CTE query returned depths 1,1,2,3,1 through all three endpoints.
    • OK — Original hierarchy NOT EXISTS query returned 1,5 through all three endpoints.
    • OK — Boundary coverage for a NULL seed and duplicate outer parameter values returned independent per-row results.
    • OK — Server-side PREPARE followed by two EXECUTE calls returned the expected rows on all three endpoints.
    • OK — Invalid declaration-scope control was rejected at planning with 20301, rather than leaking a correlated expression to the executor. MySQL 8.0.46 also rejects this invalid form (1109); the diagnostic code difference is outside the valid-query compatibility scope.
    • Not covered — No concurrent workload or resource-pressure run: the defect is correlated-CTE planning/execution correctness, not a row-count, block/batch, memory/spill, network, or concurrency boundary.

    Findings

    The original failures are fixed on current main. The associated repair is 5b7453a924c0d8948b2ed10503a217fe1f1186a0, fix(plan): parameterize local CTE outer references, merged by PR #29372. It parameterizes supported local-CTE producers with the outer-row domain so Corr expressions 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.sql covers 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 main runtime.

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

Metadata

Metadata

Assignees

Labels

area/compatibilitykind/compatibilityseverity/s0

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions