Repository navigation
Description
DRAFT 6 (SDK, 2026-10-27) — candidate issue, not yet filed
Title: [Bug]: ORM count() rewrites SQL with str.replace and returns wrong values for custom columns, group_by, or string literals
Description
Three sibling implementations — Query.count() (orm.py:505), BaseMatrixOneQuery.count() (orm.py:1969), and AsyncQuery.count() (async_orm.py) — all "count" by text-surgery on the built SELECT:
sql, params = self._build_sql()
sql = sql.replace("SELECT *", "SELECT COUNT(*)") # async_orm.B. / BaseMatrixOneQuery — only this one line
result = ... rows[0][0] ...(Query.count() at :505 additionally tries sql.replace(f"SELECT {', '.join(cols)}", "SELECT COUNT(*)") — same family, one more fallback.)
Failure modes, all reachable from documented API usage:
- Custom column select (async_orm/BaseMatrixOneQuery path):
session.query(User).select(User.id).filter(...).count()buildsSELECT id FROM ...; thereplace("SELECT *", ...)misses → the SQL staysSELECT id ..., androws[0][0]returns the first id value, not the row count. Silent wrong number. - GROUP BY: with
group_by(...), the real count is the number of groups; the rewritten query still returns one row per group androws[0][0]picks the first group's id again. - String literals containing
SELECT *:filter(User.note == "SELECT * FROM t")— the replace hits the literal too, corrupting the predicate (SELECT COUNT(*) FROM tinside the WHERE literal → broken quoting). Contrived but demonstrates the fragility of text surgery as an architecture. - Joins with qualified columns:
_build_sql()may renderSELECT users.id, ...— again misses the literalSELECT *match in the two simpler impls.
Verified statically (code above quoted verbatim from all three sites); behavior follows directly.
Reproduction
with client.session() as s:
cnt = s.query(User).select(User.id).filter(User.age > 18).count()
# expected: row count; actual: first matching user's id (an arbitrary number)Impact
count()is the standard pagination/exists-check primitive; users get plausible-looking-but-wrong numbers with no error — pagination math (page size, has_next) silently off; "count == 0" existence checks give false negatives/positives.- The same file already contains a correct pattern (build a fresh query with only WHERE-like clauses and COUNT in the projection, as
Query.count()'s copy-then-rebuild does) — the two later rewrites regressed to text surgery.
Expected behavior
Rebuild the count query structurally (the :505 approach minus the replace): a fresh builder with COUNT(*) projection, carrying over where/joins/group/having, and for GROUP BY also wrapping as SELECT COUNT(*) FROM (<grouped query>) g. No string replacement. A regression table: custom-column select, group_by, join, literal-containing-'SELECT *', and plain count asserting exact expected integers.
Activity
Ariznawlll commentedon Oct 29, 2026
结论
测试不通过。当前 main 的新查询接口已能正确计数分组结果,但公开 legacy
matrixone.orm.Query.count()仍返回第一个组的行数。本轮补实际运行证据,不另提同根 issue。关键证据
main@c6683bd2f531dcaa80e153c0924dac619c49dc9d;SDK直接导入同提交源码0.1.16,本地隔离CN/TN/LogService;运行时git_version()=c6683bd2f5。Sales(id,grp,amount)为4行,grp分别1、1、2、3。Query(Sales,client).select('grp','SUM(amount)').group_by('grp').count()三次均返回2,正确分组结果数为3。client.query(Sales.grp,func.sum(Sales.amount)).group_by(Sales.grp).count()三次均返回3。底表仍4行,后续SELECT1正常。clients/python/matrixone/orm.py:566-584仍将SELECT文本替换为COUNT并读取第一行;同文件新接口1550附近已使用子查询包装。复现只需先写4行上述分组数据,再执行两条Python查询,无特殊SQL模式或边界名称。待解决点
Ariznawlll commentedon Oct 29, 2026
结论
测试不通过。当前 main 的 legacy
Query.count()仍会在分组、HAVING 和分页下返回错误计数;属于本 issue 的扩展,不重复提单。关键证据
main@c6683bd2f531dcaa80e153c0924dac619c49dc9d;同提交构建的隔离双 CN,3 个新建库。legacyQuery走同步客户端;现代查询另验证同步及异步客户端。SELECT grp FROM sales GROUP BY grp的实际结果是 3 行,legacyall()也是 3 行,但 legacycount()返回 5;加入HAVING count(*) >= 4同样返回 5 而非 3,均 3/3。LIMIT 2,实际结果 2 行但 legacy count 返回 5;添加LIMIT 1 OFFSET 1,实际结果 1 行但返回 5;普通非分组LIMIT 2 OFFSET 1返回 15 而不是 2,均 3/3。sdk_legacy_count5.json/.log。待解决点
fix(python): unify SDK query and analytics lifecycle contracts (#31774)
XuPeng-SH commentedon Oct 30, 2026
QA handoff for merged #31774 (main
bbfe55669a1440424914d45a283a1d0e36f722fa): please verify legacy and modern count with GROUP BY, HAVING, pagination and empty sets. This isolated QA run inspected the issue and merged source but did not run tests or a live MatrixOne service at this SHA because resource headroom under the existing guard was insufficient. Earlier QA counterexamples and PR author-reported validation remain scoped to their recorded versions and are not results of this QA run.Ariznawlll commentedon Oct 30, 2026
结论
测试通过。legacy、现代同步和异步 ORM 的 count 保留分组、HAVING 和分页语义,不再把第一组的行数当作结果集数量。
关键证据
main@d39d8a40343ccd40bf665036388df7a40ee867f8;修复 PR fix(python): unify SDK query and analytics lifecycle contracts #31774 合并提交bbfe55669a1440424914d45a283a1d0e36f722fa在祖先链;实际二进制和 SDK 同完整 SHA。本地隔离 1 CN / 1 TN / 1 LogService,Python 3.12、SQLAlchemy 2.0.43。SELECT * FROM t的过滤 count 为 1,空集为 0。未知字段被拒绝后源表仍 15 行、连接可用,每轮测试库清理。待解决点
Ariznawlll commentedon Oct 30, 2026
回归位置更正:#295 已关闭并汇总至 MOTR #298,该 issue 的定向用例已包含在 #298。此前测试证据与未覆盖项不变;#298 尚未合入,完整 MOTR suite 未运行。