Skip to content

[Bug]: Python fulltext builders drop explicit LIMIT 0 #31023

Closed

Description

@MatrixAdventurer

Is there an existing issue for the same bug?

Branch Name

main

Commit ID

3878a13

Other Environment Information

Python 3.12, SQLAlchemy 2.1.6, offline SQL-generation regression.

Actual Behavior

FulltextQueryBuilder.as_sql(limit=0), the client FulltextSearchBuilder.limit(0).explain(), and the index-module FulltextSearchBuilder.limit(0).build_sql() all omit LIMIT. A request for zero rows becomes an unrestricted search. Each uses truthiness instead of distinguishing None from zero.

Expected Behavior

Explicit zero emits LIMIT 0; None omits LIMIT; positive limits remain intact.

Steps to Reproduce

Run from clients/python:

PYTHONPATH=. python -m pytest tests/offline/test_fulltext_zero_limit.py -o addopts='' -q

Before production changes: 3 failed, 6 passed. All zero-limit paths fail the expected LIMIT 0 assertion. After: all nine pass.

Additional information

由 LW 授权的 ChatGPT Agent 操作。
Handoff to @XuPeng-SH: complete candidate patch below, 3 files, +43/-3 lines. Production fix changes only three guards. Focused owning tests (test_fulltext_zero_limit.py, test_fulltext_search_comprehensive.py, test_fulltext_index_validation.py): 93 passed. git diff --check passed.

Self-review: one R2 SDK SQL-generation contract across three public consumers; no engine/index algorithm, concurrency, authentication, or persisted format changes. None/zero/positive controls retained. This is one bug with three affected entry points, not three independent bugs. No MatrixFlow changes this round.
Real MatrixOne frontend/BVT and hosted CI remain unverified. Patch handoff only; no PR opened under the mo-self-review requirement for zero unresolved validation blockers.

Save the diff below and apply with git apply after checking against the stated base.

diff --git a/clients/python/matrixone/sqlalchemy_ext/fulltext_index.py b/clients/python/matrixone/sqlalchemy_ext/fulltext_index.py
index 8d67ee0..5cdc4e9 100644
--- a/clients/python/matrixone/sqlalchemy_ext/fulltext_index.py
+++ b/clients/python/matrixone/sqlalchemy_ext/fulltext_index.py
@@ -641,7 +641,7 @@ class FulltextSearchBuilder:
             builder.order_by("score DESC")
 
         # Add LIMIT/OFFSET clause
-        if self.limit_value:
+        if self.limit_value is not None:
             builder.limit(self.limit_value)
             if self.offset_value:
                 builder.offset(self.offset_value)
diff --git a/clients/python/matrixone/sqlalchemy_ext/fulltext_search.py b/clients/python/matrixone/sqlalchemy_ext/fulltext_search.py
index 7041645..7450270 100644
--- a/clients/python/matrixone/sqlalchemy_ext/fulltext_search.py
+++ b/clients/python/matrixone/sqlalchemy_ext/fulltext_search.py
@@ -646,7 +646,7 @@ class FulltextQueryBuilder:
         order_clause = f"ORDER BY {order_by}" if order_by else ""
 
         # Build LIMIT clause
-        limit_clause = f"LIMIT {limit}" if limit else ""
+        limit_clause = f"LIMIT {limit}" if limit is not None else ""
 
         # Build OFFSET clause
         offset_clause = f"OFFSET {offset}" if offset else ""
@@ -1588,7 +1588,7 @@ class FulltextSearchBuilder:
         order_clause = f"ORDER BY {self._order_by}" if self._order_by else ""
 
         # Build LIMIT clause
-        limit_clause = f"LIMIT {self._limit_value}" if self._limit_value else ""
+        limit_clause = f"LIMIT {self._limit_value}" if self._limit_value is not None else ""
 
         # Build OFFSET clause
         offset_clause = f"OFFSET {self._offset_value}" if self._offset_value else ""
diff --git a/clients/python/tests/offline/test_fulltext_zero_limit.py b/clients/python/tests/offline/test_fulltext_zero_limit.py
new file mode 100644
index 0000000..534ea18
--- /dev/null
+++ b/clients/python/tests/offline/test_fulltext_zero_limit.py
@@ -0,0 +1,40 @@
+# Copyright 2021 - 2022 Matrix Origin
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+"""Pagination contracts for the three public fulltext SQL builders."""
+
+import pytest
+from matrixone.sqlalchemy_ext.fulltext_index import FulltextSearchBuilder as IndexSearch
+from matrixone.sqlalchemy_ext.fulltext_search import FulltextQueryBuilder, FulltextSearchBuilder
+
+
+@pytest.mark.parametrize('limit', [None, 0, 2])
+@pytest.mark.parametrize('entry', ['query', 'client', 'index'])
+def test_fulltext_limit_preserves_zero(entry, limit):
+    if entry == 'query':
+        sql = FulltextQueryBuilder().must('hello').as_sql('docs', ['body'], limit=limit)
+    elif entry == 'client':
+        builder = FulltextSearchBuilder(None).table('docs').columns(['body']).query('hello')
+        if limit is not None:
+            builder.limit(limit)
+        sql = builder.explain()
+    else:
+        builder = IndexSearch('docs', ['body']).search('hello')
+        if limit is not None:
+            builder.limit(limit)
+        sql = builder.build_sql()
+    if limit is None:
+        assert 'LIMIT' not in sql
+    else:
+        assert sql.endswith(f'LIMIT {limit}')

Activity

  1. added a commit that references this issue on Oct 27, 2026
    baa83f4
  2. XuPeng-SH commented on Oct 27, 2026

    @XuPeng-SH
    Contributor

    QA on merged SDK source baa83f4b96e56ec214f92c68f7b56539b9e0fa45: the full offline suite passed (2,279 tests plus 95 subtests) on Python 3.12.14 / SQLAlchemy 2.0.55. test_fulltext_pagination_preserves_explicit_zero checks all three public SQL builders with None, 0 and 2, including explicit LIMIT 0. This verifies SQL generation; actual fulltext execution and zero-row results against a MatrixOne service were not checked in this run. Assignment is unchanged pending that service coverage.

  3. XuPeng-SH commented on Oct 27, 2026

    @XuPeng-SH
    Contributor

    Follow-up live QA: SDK 00c103b68bd2b39194147508445956f5ece16dda against an isolated MatrixOne 1.3.0 server 83c1e23526fc0ed31188abd7fc2ca87cd3a6d4b3. All three fulltext builders executed against the same seeded index: None returned all three matching rows, 0 returned no rows, and 2 returned two distinct matching rows; the nonmatching row was excluded. The connection remained usable and task objects were cleaned up. This is mixed SDK/server provenance, not a test of the latest kernel.

  4. Ariznawlll commented on Oct 27, 2026

    @Ariznawlll
    Collaborator

    测试版本

    原问题验证

    创建 4 行数据和真实 FULLTEXT 索引,其中 3 行匹配 hello、1 行不匹配;依次使用 FulltextQueryBuilder.as_sql、客户端 FulltextSearchBuilder.explain 和索引模块 FulltextSearchBuilder.build_sql 生成 SQL。三种入口显式 limit=0 时均包含 LIMIT 0,执行后返回 0 行,3 轮均通过。

    Happy Path 覆盖

    每个入口 None 时省略 LIMIT,实际返回匹配 ID {1,2,3};limit=2 时返回 2 个不同的匹配 ID,均不包含非匹配行 4。每轮检查生成 SQL 和真实结果,并检查连接仍可执行 SELECT 1。

    Unhappy Path 覆盖

    显式 0 是合法边界,不作为错误;本问题涉及 SQL 生成和查询,不涉及写入失败原子性。每轮使用独立临时数据库,测试后删除;未覆盖多 CN/Proxy 或额外 parser 变体。

    白盒验证

    不涉及 Go race。独立执行三个入口 × {None,0,2} 参数化用例,连同配置/结果游标用例共 58 项,3 轮均通过。

    回归测试

    MOTR #284 的 SDK 场景 在真实 MO 服务执行三入口全部边界,3/3;相邻 #30824 SELECT builder 回归也 3/3。未运行完整 MOTR 套件。

    测试证据

    修复 PR Python SDK 离线 CI 矩阵通过,online job skipped;本轮真实服务结果见上述 MOTR 场景及 PR。MOTR 新 head 的四项托管离线检查通过,不等同完整在线 CI。

    测试结论

    测试通过。

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

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions