Skip to content

[Bug]: Prisma relationJoins (relationLoadStrategy:'join') queries fail (v3.0.11 & 4.0-dev) #24737

Closed

Description

@dengn

Summary

Prisma's JOIN-based relation loading (preview feature relationJoins, relationLoadStrategy: 'join') builds nested derived tables that wrap rows with JSON_OBJECT(...) and aggregate them with JSON_ARRAYAGG(...) / CAST(... AS JSON). These queries fail on MatrixOne.

  • 3.0.11: not supported: function or operator 'json_object' (and 'json_arrayagg').
  • 4.0-dev (nightly-0bfda604): the building blocks all work standalone — JSON_OBJECT(...), JSON_ARRAYAGG(...), CAST('[]' AS JSON), COALESCE(JSON_ARRAYAGG(...), CAST('[]' AS JSON)) — but the full nested query still fails with a parser error near " JSON))". To-one relation loads work on 4.0-dev; to-many fail.

Generated SQL that fails (4.0-dev, to-many author → posts, trimmed)

SELECT `t1`.`id`, `t1`.`name`,
  COALESCE(`posts`.`__prisma_data__`, CAST('[]' AS JSON)) AS `__prisma_data__`
FROM `Author` AS `t1`
LEFT JOIN ( SELECT JSON_ARRAYAGG(`t5`.`__prisma_data__`) ...
  ( SELECT JSON_OBJECT('id', `t3`.`id`, 'title', `t3`.`title`, ...) AS `__prisma_data__`
    FROM ( SELECT `t2`.* FROM `Post` AS `t2` WHERE `t1`.`id` = `t2`.`authorId` ) AS `t3`
  ) ... ) AS `posts`
WHERE `t1`.`id` = ?;
-- ERROR 1064 (HY000): SQL parser error ... syntax error near " JSON)) AS `__prisma_data__` ..."

Prisma impact

Any query using relationLoadStrategy: 'join' fails. Workaround: use the default 'query' strategy (separate queries), which works fine.

Expected Behavior

The generated JSON-aggregation relation query parses and executes (to-many relations included).


Found during deep Prisma ORM (v5.22, relationJoins preview) compatibility testing; reproduced on prod 8.0.30-MatrixOne-v3.0.11 and nightly-0bfda604.

Activity

  1. added
    kind/bug
    on May 31, 2026
  2. added
    severity/s1
    on Jun 2, 2026
  3. added theissue type on Jun 2, 2026
  4. modified the milestones: MO-202605, MO-202606 on Jun 2, 2026
  5. added
    severity/s0
    and removed
    severity/s1
    on Jun 30, 2026
  6. modified the milestones: MO-202606, MO-202607 on Jun 30, 2026
  7. fengttt commented on Jul 8, 2026

    @fengttt
    Contributor

    MySQL also requires outer join to have an ON/USING clause. This is just a bad SQL.

  8. 4 remaining items

  9. VioletQwQ-0 commented on Aug 3, 2026

    @VioletQwQ-0
    Collaborator

    Revalidated on 2026-08-03 with the current Prisma/MySQL path. The old sample containing an OUTER JOIN without ON/USING is invalid SQL and is not evidence for this issue.

    Current reproduction manifest

    • Prisma CLI / Client / @prisma/adapter-mariadb: 7.9.1 (npm dist-tag latest=7.9.1)
    • Node.js: 24.18.1
    • MySQL: 8.0.46
    • MatrixOne main: d29b4758f88f0894feeca15f31412446c17cb9d3
    • Schema/data: Author(1, Ada) has two posts; Author(2, Grace) has none

    Prisma's current documentation still describes the MySQL join strategy as a single database query using correlated subqueries and database-side JSON aggregation:

    The real Prisma 7.9.1 to-many request was:

    prisma.author.findMany({
      relationLoadStrategy: "join",
      include: { posts: true },
      orderBy: { id: "asc" },
    })

    It generated this valid MySQL SQL (formatted only; no OUTER JOIN is present):

    SELECT `t0`.`id`, `t0`.`name`,
      (SELECT COALESCE(JSON_ARRAYAGG(`__prisma_data__`), CONVERT('[]', JSON))
       FROM (
         SELECT `t3`.`__prisma_data__`
         FROM (
           SELECT JSON_OBJECT('id', `t2`.`id`, 'title', `t2`.`title`, 'authorId', `t2`.`authorId`) AS `__prisma_data__`
           FROM (
             SELECT `t1`.* FROM `Post` AS `t1`
             WHERE `t0`.`id` = `t1`.`authorId` /* root select */
           ) AS `t2` /* inner select */
         ) AS `t3` /* middle select */
       ) AS `t4` /* outer select */
      ) AS `posts`
    FROM `Author` AS `t0`
    ORDER BY `t0`.`id` ASC;

    A/B result

    MySQL 8.0.46: PASS

    [
      {"id":1,"name":"Ada","posts":[{"id":10,"title":"First","authorId":1},{"id":11,"title":"Second","authorId":1}]},
      {"id":2,"name":"Grace","posts":[]}
    ]

    MatrixOne d29b4758f: FAIL

    Database error. Code: 20102
    correlated subquery in FROM clause is not yet implemented
    

    MatrixOne's system.statement_info captured the same prepared Prisma SQL as failed:

    statement_id: 019fc5c3-d307-76e2-85b9-5a42aae6f52f
    request_at:   2026-08-03 11:56:12.935096
    status:       Failed
    err_code:     20102
    

    Controls on the same exact heads:

    • Prisma relationLoadStrategy: "query": PASS on MySQL and MatrixOne.
    • Prisma to-one relationLoadStrategy: "join": PASS on MySQL and MatrixOne.
    • Non-correlated nested JSON_OBJECT / JSON_ARRAYAGG: PASS on MatrixOne.
    • A simplified correlated JSON aggregate without a derived-table wrapper executes on MatrixOne for matching rows.
    • A derived table that references the outer row fails even with COUNT(*) and no JSON functions.

    This isolates the current blocker to the planner's explicit rejection of a correlated derived table (pkg/sql/plan/query_builder.go, subCtx.isCorrelated -> NewNYI("correlated subquery in FROM clause")), not to the parser and not to the JSON functions.

    There is one serial prerequisite. On current main, a simplified empty correlated aggregate still evaluates COALESCE(JSON_ARRAYAGG(...), '[]') as NULL instead of []. #25959 / draft PR #26510 (cd7396c368f61eff48dcaa1ef29d88e0dc7959ff, fresh CI currently running) addresses that independent empty-input semantic. Without it, removing the derived-table blocker alone would still produce an incorrect empty relation.

    Proposed repair sequence

    1. Finish fix(plan): preserve empty correlated aggregate projections #26510 and require its exact-head CI/review gates to pass.
    2. Add a separate, narrow planner repair for transparent projection/filter derived-table chains nested under a scalar subquery, including correlation-depth reduction and alias/output remapping. Do not claim full LATERAL support; the general capability remains tracked by Feature Not Supported: Correlated subquery in FROM clause not yet implemented #23084 / [Bug]: LATERAL and derived-table outer references are not supported like MySQL #25376.
    3. Add planner tests for supported and deliberately rejected shapes, plus a production BVT containing the complete Prisma SQL and present/empty/all-NULL relation cases.
    4. Run real Prisma 7.9.1 E2E against MySQL 8 and the MatrixOne PR head, preserving to-one join and query strategy controls.
    5. Keep [Bug]: Prisma relationJoins (relationLoadStrategy:'join') queries fail (v3.0.11 & 4.0-dev) #24737 open through post-merge QA. Closure requires a tested MatrixOne version/environment and terminal Prisma to-many join PASS evidence.

    So the issue is confirmed on current Prisma and current MatrixOne main, but its valid current failure is correlated-derived-table planning rather than the historical invalid OUTER JOIN sample.

  10. VioletQwQ-0 commented on Aug 4, 2026

    @VioletQwQ-0
    Collaborator

    QA handoff for merged PR #26672.\n\n- PR head: 52f59dd\n- Merge commit: 224ec27\n- Main at handoff: f670446\n- Automated evidence: exact-head CI passed Ubuntu UT, SCA, coverage, Proxy Compose BVT, and pessimistic launch BVT. The merged PR also added the Prisma correlated-derived-table production BVT and planner coverage.\n\nRemaining QA: run Prisma Client and @prisma/adapter-mariadb 7.9.1 relationLoadStrategy: join against a deployed MatrixOne build containing the merge, using the documented to-many 0/1/many cases and controls for to-one join and relationLoadStrategy: query. Confirm the valid correlated-derived query returns the expected JSON relations without the former 20102 error.\n\nExpected PASS: the terminal Prisma to-many join results match MySQL for 0/1/many relations. On FAIL, include MatrixOne version/build, environment, exact query or fixture, terminal error/result, and logs or statement evidence. Keep this issue open until explicit tester PASS.

  11. removed their assignment
    on Aug 5, 2026
  12. Ariznawlll commented on Aug 10, 2026

    @Ariznawlll
    Collaborator

    测试版本

    • 分支:main
    • commit:3c7392a0f86b23712da71f31d7a9198a2c8591a9
    • 部署方式:本地单节点 Log/TN/CN;按当前 Prisma relationLoadStrategy: 'join' 的相关派生表 SQL 验证;测试日期:2026-08-10。

    原问题验证

    关系 join 查询连续执行 3 次,均成功返回作者及 posts JSON,不再出现 relation-join 查询失败。

    Happy Path 覆盖

    1. 作者分别具有 0、1、2 条 post,校验 JSON 数组长度和每个对象的 id/title/authorId 成员。
    2. 额外覆盖 related row 的 title IS NULL,验证 JSON NULL 保留。

    Unhappy Path 覆盖

    1. 0 条关联行返回 [],不是 NULL 或缺失父行。
    2. JSON_ARRAYAGG 在 MatrixOne 未指定数组元素顺序,测试不依赖未定义顺序,而逐对象检查成员和长度;该读查询无失败写入原子性维度。

    白盒验证

    不适用:使用 Prisma 形状的公共 SQL 黑盒验证。

    回归测试

    已加入 motr PR #89 的 suites/14_issue_regression/t/issue_24737_prisma_relation_join.test(commit a6a50dfcf7053027dab5e9117fde7bdd7b5cdefb)。

    测试证据

    MOTR 连续 3 轮:关联 5 个场景合计 15/15 成功、171 条断言成功。

    测试结论

    测试通过。

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

Metadata

Metadata

Assignees

Labels

kind/bugphase/testingseverity/s0

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions