Skip to content

[Bug]: Python SDK recursive CTE flag emits plain WITH and fails on supported recursive query #31241

Closed

Description

@Ariznawlll

Description

Python SDK 接受 CTE(..., recursive=True)(query(...).cte(..., recursive=True) 也是公开入口),但执行时始终生成普通 WITH,未生成 WITH RECURSIVE。因此服务端本来可执行的递归 CTE 被 SDK 变成语法错误。

Environment

  • Branch: main
  • Commit: 5fe08a3307e246e1ab9951ab690b50557af0f509
  • Local isolated deployment: 1 LOG / 1 TN / 1 CN; SQL port 36601; VERSION() = 8.0.30-MatrixOne-v1.3.0
  • Python 3.11, MatrixOne Python SDK 0.1.16, SQLAlchemy 2.0.55

Steps to reproduce

from matrixone import Client
from matrixone.orm import CTE

c = Client(sql_log_mode="off")
c.connect(host="127.0.0.1", port=36601, database="mo_catalog", on_connect=[])
body = "SELECT 1 AS n UNION ALL SELECT n+1 FROM r WHERE n<3"
recursive = CTE("r", body, recursive=True)
print(c.query(recursive)._build_sql())
print(c.query(recursive).all())

The emitted SQL is WITH r AS (SELECT 1 AS n UNION ALL SELECT n+1 FROM r WHERE n<3) SELECT * FROM r.

Actual behavior

QueryError: SQL parser error: not declare RECURSIVE: 'r'.

Expected behavior

The SDK should honor recursive=True and emit a valid WITH RECURSIVE statement, returning rows (1), (2), (3).

Stability and controls

  • Reproducer: sync 3/3 and async 3/3, each with independent ephemeral CTE names. Both emitted plain WITH and received 1064.
  • Direct MO control: WITH RECURSIVE r AS (SELECT 1 AS n UNION ALL SELECT n+1 FROM r WHERE n<3) SELECT n FROM r ORDER BY n returned (1),(2),(3) in sync 3/3 and async 3/3.
  • Ordinary nonrecursive CTE returned (7) in 3/3.
  • After each sync/async SDK error SELECT 42 returned (42); no persistent object was created.

Evidence

Local scripts and complete output: /private/tmp/mo-sdk-sqlbuilder-20261026/cte_recursive_probe.py and async_recursive_cte_probe.py (rerun with /private/tmp/mo-sdk-py311-venv-20261026/bin/python).

Code analysis

clients/python/matrixone/orm.py:623-626 stores the recursive flag. BaseMatrixOneQuery._build_sql() at orm.py:1412-1422 passes only CTE name/SQL/params into the unified builder. clients/python/matrixone/sql_builder.py:417-423 always prefixes the CTE list with plain WITH. Existing online test clients/python/tests/online/test_orm_cte_integration.py:176-182 merely checks the Python flag and does not execute recursion.

Regression coverage

Add an SDK online test that executes an actual self-referencing CTE, checks exact rows, and verifies a nonrecursive CTE still works.

Activity

  1. added this to the MO-202610 milestone on Oct 26, 2026
  2. added
    kind/bug
    on Oct 26, 2026
  3. added theissue type on Oct 26, 2026
  4. added a commit that references this issue on Oct 26, 2026
    28e8760
  5. XuPeng-SH commented on Oct 26, 2026

    @XuPeng-SH
    Contributor

    Local SDK QA (2026-10-26): SDK source 28e8760905a7f39dba2ce436357ea75492f30512; isolated MatrixOne service binary 83c1e23526fc0ed31188abd7fc2ca87cd3a6d4b3 (SHA-256 6bebe05311ae6438967a824641e3849fbbc9af298003177351794601edf14b4b). Passed tests/online/test_sdk_quality_contracts.py::test_native_aliased_dml_and_recursive_cte plus offline recursive CTE contracts. Recursive execution returned exactly [1,2,3]; a nonrecursive control and sync/async entry points were also checked. Focused SDK execution evidence.

  6. Ariznawlll commented on Oct 27, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 官方 main:420786e21566e5b9d5875e7d6a7f3102fd0761af;修复 PR fix(python-sdk): unify query contracts and atomic manager operations #31262 已 MERGED,merge commit 28e8760905a7f39dba2ce436357ea75492f30512 已核实在该 main 祖先链。
    • 隔离本地部署:该 SHA 重建的 1 LOG / 1 TN / 2 CN,SQL 47011/47012;Python 3.12、SQLAlchemy 2.0.55、同工作树 Python SDK;二进制 SHA-256 8bd0f1f4c60cd0590c68cdbb2cbca31fc9d39d071ec367ab56d5c49b4eec327e。
    • 测试日期:2026-10-27。

    原问题验证

    递归 CTE WITH RECURSIVE r AS (SELECT 1 ... n<3) 经同步/异步公开 Query 执行,结果 n 精确为 [1,2,3],不再生成错误的普通 WITH;连续 3 轮通过。

    Happy Path 覆盖

    1. 同轮别名 UPDATE 分别加 1 后目标 v=4,递归结果仍正确。
    2. 四个在线 SDK 合同文件连续 3 轮每轮 70/70 通过;112 个定向离线合同也连续 3 轮通过。两类测试零失败、错误、跳过。

    Unhappy Path 覆盖

    1. 在 JOIN 后执行 UPDATE/DELETE 明确抛 ValueError('Joined UPDATE/DELETE'),底表随后仍为预期 [(1,4),(2,1),(3,NULL)]。

    白盒验证

    核对已合入回归的 SDK 公开入口、SQL/返回值断言和修复后实际服务结果;未修改产品源码,未运行全量 SDK suite 或 race。

    回归测试

    #31262 已包含 clients/python/tests/online/test_sdk_quality_contracts.py::test_native_aliased_dml_and_recursive_cte。Python SDK API 行为无法由纯 SQL MOTR 代替,未新增 MO 或 MOTR PR。#31262 六个离线 SDK CI job 成功;在线 CI SKIPPED,在线结果采用本地实测。

    测试证据

    本地 JUnit:/private/tmp/mo-six-qa-20261026/qa-night/sdk-contract-round-{1,2,3}.xml(每轮 tests=70、失败/错误/跳过均 0)及 sdk-offline-round-{1,2,3}.xml(每轮 112、失败/错误/跳过均 0)。修复 PR:#31262 。

    测试结论

    测试通过。

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

Metadata

Metadata

Assignees

Labels

kind/bugneeds-triage

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions