Skip to content

[Bug]: Python SDK filter_by silently drops predicates for string-table queries and unknown model fields #31207

Closed

Description

@Ariznawlll

Description

Python SDK 的 filter_by 对字符串表名查询和模型拼错字段静默丢弃谓词,使有条件查询退化为全表扫描并返回错误结果。

Environment

MatrixOne main 5fe08a3307e246e1ab9951ab690b50557af0f509;SDK 0.1.16,Python 3.11 / SQLAlchemy 2.0.55;隔离库 qa_sdkwb_20261026。

  • 部署:本机隔离 MatrixOne 集群,CN SQL 端口 36601;测试日期:2026-10-26。

Steps to reproduce

预先创建独立数据库 qa_sdkwb_20261026;连接使用本地测试账号。

from matrixone import Client
from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import declarative_base

c = Client()
c.connect(host="127.0.0.1", port=36601, database="qa_sdkwb_20261026", on_connect=[])
c.execute("CREATE TABLE sdk_filter_demo(id INT PRIMARY KEY, v VARCHAR(30) NULL)")
c.execute("INSERT INTO sdk_filter_demo VALUES (1,'red'),(2,NULL)")
Base = declarative_base()
class T(Base):
    __tablename__ = "sdk_filter_demo"
    id = Column(Integer, primary_key=True)
    v = Column(String(30))

print(c.execute("SELECT COUNT(*) FROM sdk_filter_demo WHERE v='red'").scalar())
print(c.query(T).filter_by(v="red").count())
print(c.query("sdk_filter_demo").filter_by(v="red").count())
print(c.query(T).filter_by(vv="red").count())

Actual behavior

实际输出 1, 1, 2, 2。字符串表路径是公开支持的 query(table_name);过滤条件被忽略而返回所有行。映射模型字段拼写错误也被静默忽略。原生 SQL 使用不存在字段会报 QueryError。

Expected behavior

字符串表的存在字段过滤应为 1;不存在的字段应明确报错,不能把查询放宽成全表。

Stability and controls

在 3 张独立新建表上结果均一致:raw_expected=1, model_string=1, raw_table_string=2, invalid_model_key=2。显式错误 SQL 后各表仍为 2 行,连接可继续查询。

Evidence

本地可执行复测脚本:/private/tmp/mo-sdk-whitebox-20261026/filter_by_probe.py;上述为脚本三次打印结果。

Code analysis

clients/python/matrixone/orm.py:707-710 把字符串表的 _columns 设为空字典;orm.py:1201-1213 的 filter_by 只在 key in self._columns 时追加条件,没有 else 或错误。两个症状同源。

Regression coverage

本轮未修改仓库测试。建议 SDK online 测试同时覆盖字符串表、映射模型、存在/不存在字段和过滤后行数,并断言未知字段不可静默放宽查询。

Related

已检索 open/closed issue 的 SDK filter_by / string table / ignored 关键词及兼容性文档,未见相同报告。

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 passed tests/online/test_sdk_quality_contracts.py::test_public_query_contracts and ::test_async_public_contracts_and_pre_ping against isolated service binary 83c1e23526fc0ed31188abd7fc2ca87cd3a6d4b3 (SHA-256 6bebe05311ae6438967a824641e3849fbbc9af298003177351794601edf14b4b). Sync and async assertions require a string-table filter_by to return only the matching row, unknown mapped fields to raise ValueError, and an absent string-table key to return no row; all passed. Scope is these public query paths on the older backend, not a full SDK suite.

  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。

    原问题验证

    模型表含三行,字符串表名路径 .filter_by(label='red').all() 精确返回 id [1];字符串表名 .filter_by(id=99).first() 返回 None。未知映射字段不再被静默忽略而是抛 ValueError。同步/异步公开查询合同连续 3 轮通过。

    Happy Path 覆盖

    1. 已知字段筛选只命中 red 行;模型的多条件筛选也精确返回 id [1]。
    2. 四个在线 SDK 合同文件连续 3 轮每轮 70/70 通过;112 个定向离线合同也连续 3 轮通过。两类测试零失败、错误、跳过。

    Unhappy Path 覆盖

    1. 未知 ORM 字段抛 ValueError,不是全表扫描;不存在的字符串表键返回空,底表三行不变。

    白盒验证

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

    回归测试

    #31262 已包含 clients/python/tests/online/test_sdk_quality_contracts.py::test_public_query_contracts 和 test_async_public_contracts_and_pre_ping。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