Skip to content

fix: apply order_by return value in deep-pagination helpers (unordered OFFSET/LIMIT can skip/dupe rows) - #17325

Merged
JinHai-CN merged 1 commit into
infiniflow:mainfrom
rsnetworkinginc:fix-deep-pagination-order-by
Aug 20, 2026
Merged

JinHai-CN merged 1 commit into
infiniflow:mainfrom
rsnetworkinginc:fix-deep-pagination-order-by

Conversation

@rsnetworkinginc

@rsnetworkinginc rsnetworkinginc commented Jul 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The fix is one line per helper — assign the order_by(...) result back so the
ordering is actually applied to the query.

The "get all ... " helpers in api/db/services called
query.order_by(create_time.asc()) on its own line and discarded the
result. Peewee query methods return a new query and never mutate in
place, so the bare call is a no-op and the intended ORDER BY was
silently dropped. Each helper then deep-paginates the unordered query
with OFFSET/LIMIT, which is undefined without a stable ORDER BY and can
skip or duplicate rows across batches. Assign the order_by result back
so the ordering is actually applied.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. 🌈 python Pull requests that update Python code 🐞 bug Something isn't working, pull request that fix bug. 🧪 test Pull requests that update test cases. labels Jul 23, 2026
@coderabbitai

coderabbitai Bot commented Jul 23, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The changes ensure ascending create_time ordering is assigned to several database queries before offset/limit pagination. A new unit test module uses fake Peewee-like queries and out-of-order fixtures to validate ordering for four helpers.

Changes

Pagination ordering

Layer / File(s) Summary
Service query ordering
api/db/services/*_service.py
Conversation, dialog, document, file, knowledgebase, canvas, and canvas-version helpers apply or retain ascending create_time ordering before batched retrieval.
Pagination ordering tests
test/unit_test/api/db/services/test_deep_pagination_order_by.py
Fake query/model primitives and unit tests verify chronological results for conversation, file, dialog, and canvas-version helpers.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: 6ba3i, kevinhush, wangq8

Poem

I’m a rabbit with queries in line,
Sorting each record by create-time.
Pages hop in order, neat and bright,
Tests guard the path just right.
Nibble, paginate, delight!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly identifies the deep-pagination ordering fix and its impact on OFFSET/LIMIT results.
Description check ✅ Passed The description includes the required Summary section and clearly explains the order_by assignment fix.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
test/unit_test/api/db/services/test_deep_pagination_order_by.py (1)

90-141: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Extend coverage to the other 4 fixed helpers.

This suite only covers 4 of the 8 helpers fixed in this PR. DocumentService.get_all_doc_ids_by_kb_ids, DocumentService.get_all_docs_by_creator_id, KnowledgebaseService.get_all_kb_by_tenant_ids, and UserCanvasService.get_all_agents_by_tenant_ids got the identical order_by() reassignment fix but have no regression test, so a future accidental regression on those call sites (e.g. someone refactoring the loop) wouldn't be caught.

✅ Example additional test (repeat pattern for the other 3 helpers)
from api.db.services import document_service, knowledgebase_service, canvas_service


`@pytest.mark.p2`
def test_document_helper_orders_by_create_time(monkeypatch):
    _patch(monkeypatch, document_service, document_service.DocumentService)

    rows = document_service.DocumentService.get_all_doc_ids_by_kb_ids(["kb-1"])

    assert [r["id"] for r in rows] == ["a", "b", "c"]
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@test/unit_test/api/db/services/test_deep_pagination_order_by.py` around lines
90 - 141, Extend the regression coverage in the existing pagination-order test
module by importing the document, knowledgebase, and canvas service modules and
adding tests matching the existing helper pattern. Cover
DocumentService.get_all_doc_ids_by_kb_ids,
DocumentService.get_all_docs_by_creator_id,
KnowledgebaseService.get_all_kb_by_tenant_ids, and
UserCanvasService.get_all_agents_by_tenant_ids using _patch and assert each
returns IDs in the expected create_time order.
api/db/services/conversation_service.py (1)

171-185: 📐 Maintainability & Code Quality | 🔵 Trivial | 🏗️ Heavy lift

Duplicated offset/limit batch-loop is why this bug recurred 8 times. Each helper repeats the same "order → offset/limit until empty" loop; this duplication is exactly why the discarded-order_by() bug fixed in this PR had to be fixed independently at 8 call sites. A shared helper (e.g. a generator that takes a query and batch size and yields all rows in create_time order) would collapse this to one implementation and prevent the same class of bug at a future 9th call site.

  • api/db/services/conversation_service.py#L171-L185: replace get_all_conversation_by_dialog_ids's manual loop with the shared helper.
  • api/db/services/dialog_service.py#L259-L274: replace get_all_dialogs_by_tenant_id's manual loop with the shared helper.
  • api/db/services/document_service.py#L355-L371: replace get_all_doc_ids_by_kb_ids's manual loop with the shared helper.
  • api/db/services/document_service.py#L429-L445: replace get_all_docs_by_creator_id's manual loop with the shared helper.
  • api/db/services/file_service.py#L184-L199: replace get_all_file_ids_by_tenant_id's manual loop with the shared helper.
  • api/db/services/user_canvas_version.py#L53-L68: replace get_all_canvas_version_by_canvas_ids's manual loop with the shared helper.
  • api/db/services/knowledgebase_service.py#L231-L261: replace get_all_kb_by_tenant_ids's manual loop with the shared helper.
  • api/db/services/canvas_service.py#L68-L87: replace get_all_agents_by_tenant_ids's manual loop with the shared helper.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@api/db/services/conversation_service.py` around lines 171 - 185, Replace the
duplicated ordered offset/limit loops with one shared batching helper that
accepts a query and batch size, applies create_time ordering, and yields all
rows until exhausted. Update get_all_conversation_by_dialog_ids in
api/db/services/conversation_service.py:171-185, get_all_dialogs_by_tenant_id in
api/db/services/dialog_service.py:259-274, get_all_doc_ids_by_kb_ids in
api/db/services/document_service.py:355-371, get_all_docs_by_creator_id in
api/db/services/document_service.py:429-445, get_all_file_ids_by_tenant_id in
api/db/services/file_service.py:184-199, get_all_canvas_version_by_canvas_ids in
api/db/services/user_canvas_version.py:53-68, get_all_kb_by_tenant_ids in
api/db/services/knowledgebase_service.py:231-261, and
get_all_agents_by_tenant_ids in api/db/services/canvas_service.py:68-87 to
consume the helper while preserving each method’s existing query and returned
rows.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@api/db/services/conversation_service.py`:
- Around line 171-185: Replace the duplicated ordered offset/limit loops with
one shared batching helper that accepts a query and batch size, applies
create_time ordering, and yields all rows until exhausted. Update
get_all_conversation_by_dialog_ids in
api/db/services/conversation_service.py:171-185, get_all_dialogs_by_tenant_id in
api/db/services/dialog_service.py:259-274, get_all_doc_ids_by_kb_ids in
api/db/services/document_service.py:355-371, get_all_docs_by_creator_id in
api/db/services/document_service.py:429-445, get_all_file_ids_by_tenant_id in
api/db/services/file_service.py:184-199, get_all_canvas_version_by_canvas_ids in
api/db/services/user_canvas_version.py:53-68, get_all_kb_by_tenant_ids in
api/db/services/knowledgebase_service.py:231-261, and
get_all_agents_by_tenant_ids in api/db/services/canvas_service.py:68-87 to
consume the helper while preserving each method’s existing query and returned
rows.

In `@test/unit_test/api/db/services/test_deep_pagination_order_by.py`:
- Around line 90-141: Extend the regression coverage in the existing
pagination-order test module by importing the document, knowledgebase, and
canvas service modules and adding tests matching the existing helper pattern.
Cover DocumentService.get_all_doc_ids_by_kb_ids,
DocumentService.get_all_docs_by_creator_id,
KnowledgebaseService.get_all_kb_by_tenant_ids, and
UserCanvasService.get_all_agents_by_tenant_ids using _patch and assert each
returns IDs in the expected create_time order.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: ecfa1288-341b-4429-897f-7c9cd6c18d93

📥 Commits

Reviewing files that changed from the base of the PR and between 35e1cc7 and 7d335fd.

📒 Files selected for processing (8)
  • api/db/services/canvas_service.py
  • api/db/services/conversation_service.py
  • api/db/services/dialog_service.py
  • api/db/services/document_service.py
  • api/db/services/file_service.py
  • api/db/services/knowledgebase_service.py
  • api/db/services/user_canvas_version.py
  • test/unit_test/api/db/services/test_deep_pagination_order_by.py

@JinHai-CN
JinHai-CN requested a review from Lynn-Inf August 20, 2026 10:05

@Lynn-Inf Lynn-Inf left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Lynn-Inf Lynn-Inf added the ci Continue Integration label Aug 20, 2026
@JinHai-CN
JinHai-CN merged commit 0067470 into infiniflow:main Aug 20, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🐞 bug Something isn't working, pull request that fix bug. ci Continue Integration 🌈 python Pull requests that update Python code size:S This PR changes 10-29 lines, ignoring generated files. 🧪 test Pull requests that update test cases.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants