Skip to content

[Bug]: Python SDK subquery drops bound parameters when joined as derived table #31245

Closed

Description

@Ariznawlll

Description

Python SDK 的 ORM subquery() 返回仅含 SQL 的字符串,却丢弃子查询已绑定的参数。把该子查询用于 join() 时,生成的 SQL 仍含 ?,外层参数列表却为空;合法的 DECIMAL 过滤 JOIN 不能执行。

Environment

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

Steps to reproduce

# Existing table src(id INT PRIMARY KEY, amt DECIMAL(10,2)):
# rows (1,1.20),(2,2.30); Model is its SQLAlchemy mapped class.
from decimal import Decimal
inner = client.query(Model).select("id").logical_in("amt", [Decimal("1.20")])
sub = inner.subquery("s")
outer = client.query("src AS o").select("o.id", "o.amt").join(sub, "o.id=s.id")
print(inner._build_sql())  # ('SELECT id FROM src WHERE amt IN (?)', [Decimal('1.20')])
print(outer._build_sql())  # (... JOIN (SELECT id FROM src WHERE amt IN (?)) AS s ..., [])
print(outer.all())

Actual behavior

QueryError: Query execution failed: Not enough SQL parameters.

Expected behavior

The derived-table JOIN should retain the inner parameter and return exactly (1, Decimal('1.20')).

Stability and controls

  • Reproducer: sync 3/3 and async 3/3, each with fresh source tables; both returned Not enough SQL parameters.
  • Same outer SQL executed with explicit [Decimal('1.20')] returned row 1 in sync and async 3/3; direct literal SQL also returned row 1 in sync 3/3.
  • A JOIN over a non-parameterized SDK subquery() returned row 1 in 3/3, isolating lost binds rather than JOIN support.
  • After each failure, COUNT(*)=2, SUM(amt)=3.50, and SELECT 42 returned (42); no rows changed.

Evidence

/private/tmp/mo-sdk-sqlbuilder-20261026/subquery_join_bind_probe.py and async_subquery_join_bind_probe.py (execute with /private/tmp/mo-sdk-py311-venv-20261026/bin/python). Each temporary database was dropped after confirming no tables remained.

Code analysis

clients/python/matrixone/orm.py:1076-1082 calls _build_sql() but discards its params, returning only the derived-table SQL string. BaseMatrixOneQuery.join() then stores this string; the unified builder has no way to include its binds. This is different from #31212 (parameter order within one WHERE list) and #30059 (literal substitution).

Regression coverage

Add an SDK online test for a bound DECIMAL or binary subquery embedded in JOIN, comparing exact rows to the same SQL with explicit binds; keep a no-bind subquery control and failed-query state check.

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_wave2_contracts.py::test_join_fk_and_binary_subquery[sync] and [async]. A derived query carrying Decimal logical_in and non-UTF8 binary filter parameters was joined from an outer query; it returned exactly (1, Decimal('1.20')). Rebuilding the same subquery was stable.

  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、同工作树 SDK;二进制 SHA-256 8bd0f1f4c60cd0590c68cdbb2cbca31fc9d39d071ec367ab56d5c49b4eec327e。
    • 测试日期:2026-10-27。

    原问题验证

    构造内层 ORM 查询,包含 Decimal logical_in(amount,[1.20]) 与非 UTF-8 VARBINARY 谓词,封装为 subquery('s') 并从外层 JOIN;同步/异步均返回精确 (id,amount)=(1,Decimal('1.20')),三轮通过。原来丢失绑定参数的衍生表路径未复现。

    Happy Path 覆盖

    1. 对相同查询重复构建 subquery 文本稳定;外层查询结果的 Decimal 值和类型正确。
    2. 四个在线 SDK 合同文件连续 3 轮每轮 70/70 通过;112 个定向离线合同也连续 3 轮通过。两类测试零失败、错误、跳过。

    Unhappy Path 覆盖

    1. 另一行 (2,2.30) 不会被意外带出;测试表删除后无残留。

    白盒验证

    核对已合入回归源码中的 SDK 公开入口、返回值断言与真实服务执行结果;未修改产品源码,未运行全量 SDK suite 或 race。

    回归测试

    #31262 已包含 clients/python/tests/online/test_sdk_wave2_contracts.py::test_join_fk_and_binary_subquery[sync/async]。此为 Python SDK 行为,纯 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(每轮 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