Skip to content

[Bug] FOUND_ROWS() returns incorrect row count after SELECT and SQL_CALC_FOUND_ROWS #27304

Closed

Description

@Lundomn

问题描述

MatrixOne 的 FOUND_ROWS() 在普通 SELECT ... LIMIT 和 SQL_CALC_FOUND_ROWS ... LIMIT 查询后返回错误的行数,目前返回 0。

这与 MySQL 的兼容行为不一致,并会影响依赖 FOUND_ROWS() 实现分页总数统计的应用和 ORM。

环境

  • MatrixOne: 8.0.30-MatrixOne-v4.1.4(matrixorigin/matrixone:latest,验证日期 2026-08-18)
  • MatrixOne 服务端: 127.0.0.1:6001
  • macOS MacBook + Docker Desktop
  • MySQL 参考版本: 8.0.20

最小复现

DROP DATABASE IF EXISTS found_rows_test;
CREATE DATABASE found_rows_test;
USE found_rows_test;

CREATE TABLE t(id INT);
INSERT INTO t VALUES (1), (2), (3);

SELECT SQL_CALC_FOUND_ROWS * FROM t LIMIT 1;
SELECT FOUND_ROWS() AS after_calc;

SELECT * FROM t LIMIT 2;
SELECT FOUND_ROWS() AS after_plain;

Prepared Statement 也可以复现普通查询问题:

PREPARE ps FROM 'SELECT * FROM t LIMIT 2';
EXECUTE ps;
SELECT FOUND_ROWS() AS prepared_after_plain;
DEALLOCATE PREPARE ps;

实际结果

MatrixOne 返回:

after_calc           = 0
after_plain          = 0
prepared_after_plain = 0

当前 main 中 FOUND_ROWS() 的实现仍然直接返回 0:

https://github.com/matrixorigin/matrixone/blob/main/pkg/sql/plan/function/func_unary.go#L6563-L6566

期望结果

MySQL 8.0.20 返回:

after_calc           = 3
after_plain          = 2
prepared_after_plain = 2

其中:

  • 普通 SELECT ... LIMIT 2 应返回实际返回的结果集行数 2
  • SQL_CALC_FOUND_ROWS ... LIMIT 1 应返回忽略 LIMIT 后的完整结果集行数 3

影响

依赖 MySQL FOUND_ROWS() 语义的分页组件、ORM 和数据库兼容层可能获得错误的总行数,导致分页总页数和数据元信息不正确。

归属判断

问题位于 MatrixOne 服务端的 FOUND_ROWS() 状态和查询执行链路,而不是客户端行为:

  • 当前 main 中 FOUND_ROWS() 为硬编码返回 0
  • 当前 main 的 process/session 状态没有保存上一条结果集的 FOUND_ROWS 值
  • SQL_CALC_FOUND_ROWS 需要在执行 LIMIT 前统计完整结果集行数,同时只向客户端返回 LIMIT 后的行

截至 2026-08-19,未在 MatrixOne Issues 中检索到相同问题或已有修复。

Activity

  1. added
    kind/bug
    severity/s0
    on Aug 19, 2026
  2. reopened this on Sep 4, 2026
  3. assigned and unassigned on Sep 4, 2026
  4. slyunnn commented on Sep 10, 2026

    @slyunnn

    Regression and extended verification for #27304 (2026-09-10)

    Verification Environment

    • MatrixOne source: local origin/main at 401b967dc15a45a4374a04921b2bb2c9fbdb7117 — test: synchronize cross-CN FK regression setup (#28511).
    • The running mo-service binary was rebuilt from that commit and restarted before verification; mo-service -version reported 401b967dc1.
    • Topology: 2 CN / 1 TN / 1 Log service / Proxy.
    • Tested endpoints: Proxy 127.0.0.1:6001, CN1 127.0.0.1:16001, and CN2 127.0.0.1:16002; each accepted a fresh connection and SELECT 1.
    • Client protocol: MySQL wire protocol through the mysql client and MOTR. The prepared coverage uses SQL PREPARE / EXECUTE, which is the issue's reported form.
    • Related fix: a9d02fd3b4417f1e781e24f59bc580d6cdee0358, merged by #27312, fix: return correct values from FOUND_ROWS().

    What Was Covered

    • OK — Original SQL_CALC_FOUND_ROWS scenario returned FOUND_ROWS() = 3 after LIMIT 1 on a three-row table, through Proxy 6001, CN1 16001, and CN2 16002.
    • OK — Plain SELECT ... LIMIT 2 returned FOUND_ROWS() = 2 through all three endpoints.
    • OK — SQL PREPARE / EXECUTE for SELECT ... LIMIT 2 returned FOUND_ROWS() = 2; reusing the prepared statement preserved the same value.
    • OK — Boundary cases: LIMIT 0 after SQL_CALC_FOUND_ROWS returned 3, OFFSET retained the pre-limit count 3, and an empty result returned 0.
    • OK — A successful status statement and a failed missing-table statement did not overwrite the previous successful FOUND_ROWS() = 2.
    • OK — The original scenarios used isolated endpoint-specific databases and ran concurrently across Proxy 6001, CN1 16001, and CN2 16002.
    • OK — MOTR recording passed through Proxy 6001; replay passed through Proxy 6001, CN1 16001, and CN2 16002, with 28/28 assertions at each endpoint.
    • Not covered — Binary COM_STMT_PREPARE / COM_STMT_EXECUTE; it is outside the reported SQL PREPARE / EXECUTE reproduction.

    Findings

    The linked fix correctly publishes the preceding user-visible result-set count as session state. The original scenario, normal LIMIT, SQL prepared execution, boundary cases, and error/status preservation all behaved as expected on each verified endpoint.

    MOTR / BVT Coverage

    • Independent MOTR branch: test/issue-27304-found-rows.
    • Source commit: ef5e14d4de09bc5bdba66c8729dfd8e92fc31eee, test: add MOTR regression for #27304.
    • Case: suites/14_issue_regression/t/issue_27304_found_rows.test with its recorded result file.
    • Daily aggregation: #155, cherry-pick commit e27c7301ffc390ad2b822841fe56d090c09477eb.
    • No additional large-data MOTR regression is needed: this defect is per-session result-count state rather than a row-volume, block/batch, spill, concurrency, skew, network, or resource-scale condition. The linked product regression already covers the cross-CN path with 100000 rows.

    Final Conclusion

    Fixed. The repaired behavior is verified on the rebuilt latest origin/main runtime through the full 2 CN / 1 TN / 1 Log service / Proxy topology and all three endpoints. No additional large-data regression is required for the reasons above.

  5. Ariznawlll commented on Oct 29, 2026

    @Ariznawlll
    Collaborator

    Regression alert — the incorrect FOUND_ROWS behavior reported here has reappeared on current main. Reproduced 2026-10-29 on main c6683bd2f5 (commit built and launched locally today, dual-CN with proxy — same topology class as the 2026-09-10 verification upthread):

    -- table: exx.t(id INT PRIMARY KEY, k INT), 50 rows loaded, secondary index on k
    -- ground truth (verified via plain SELECT COUNT): WHERE k=3 matches 22 rows
    
    SELECT SQL_CALC_FOUND_ROWS id FROM exx.t WHERE k = 3 LIMIT 2;  -- returns 2 rows (correct)
    SELECT FOUND_ROWS();
    -- MO today: 7     ← wrong. Expect 22. Neither the LIMIT count (2) nor anything derivable; 
    --                    7 matches the mo_table_stats-style cardinality estimate (50%-ish buckets of the index),
    --                    i.e. FOUND_ROWS is again backed by table statistics, not the SQL_CALC_FOUND_ROWS tuple count.
    
    -- adjacent checks from the same session:
    SELECT id FROM exx.t WHERE k = 3 LIMIT 2; SELECT FOUND_ROWS();   -- 2 (correct, plain-path)
    UPDATE exx.t SET v=v WHERE id=1; SELECT ROW_COUNT();             -- 0, MySQL says 1 (touched row)
    CREATE TABLE exx.tmp1(id INT); SELECT FOUND_ROWS();               -- 1, should be 0 after DDL

    The closed-at status (verified on 401b967dc1, 2026-09-10) does not hold on c6683bd2f5. Since the estimate-shaped value (7 for a 22-row truth) is exactly the "statistics-backed FOUND_ROWS" signature this issue originally described, whatever fix landed for the COUNT path did not cover (or regressed for) the SQL_CALC_FOUND_ROWS + indexed-column + LIMIT combination. Happy to cut a fresh standalone report if preferred — flagging here first since the case was verified-closed recently and the thread's history is the best context for the fix owners.

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

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions