Repository navigation
Description
Is there an existing issue for the same bug?
- I have checked the existing issues.
Branch Name
optimize_limit
Commit ID
899e3a236e
Other Environment Information
- Hardware parameters: local Linux/x86 development environment
- OS type: Linux
- Others: planner-level reproduction using the MatrixOne mock optimizer; the failure is independent of data distribution once at least
LIMIT + OFFSETrows match.
Actual Behavior
A query using a master index with LIMIT ... OFFSET ... receives the same pagination clause twice:
applyIndicesForFiltersUsingMasterIndexputs the originalLimitandOffseton the final master-index scan/intersection node.- It then copies the same
LimitandOffsetto the outerINDEXjoin used for the base-table lookup.
Both nodes are compiled through compileSort, so the inner operation first skips OFFSET rows and limits the candidate set; the outer lookup then skips OFFSET rows again.
For a query with at least 15 matching rows, LIMIT 10 OFFSET 5 can therefore return only 5 rows and starts from the wrong logical page.
The isolated plan reproduction produced both of these nodes:
JOIN (master-index intersection) limit=10 offset=5
JOIN (INDEX/base-table lookup) limit=10 offset=5
Relevant code:
pkg/sql/plan/apply_indices_master.go:82-85pkg/sql/plan/apply_indices_master.go:113-120pkg/sql/compile/compile.go:1118-1131
Expected Behavior
LIMIT 10 OFFSET 5 must be applied exactly once to the final result. When at least 15 rows match, the query must return 10 rows after skipping the first 5 rows.
The index path may fetch LIMIT + OFFSET candidates internally, but only the final layer may apply the user-visible OFFSET.
Steps to Reproduce
CREATE TABLE t_master_limit (
id INT PRIMARY KEY,
a VARCHAR(20),
b VARCHAR(20),
INDEX idx_master USING master (a, b)
);
-- Insert at least 15 rows with the same indexed values.
INSERT INTO t_master_limit
SELECT result, 'same-a', 'same-b'
FROM generate_series(1, 30);
SELECT id
FROM t_master_limit
WHERE a = 'same-a' AND b = 'same-b'
LIMIT 10 OFFSET 5;Inspect the logical/physical plan. The current master-index rewrite attaches LIMIT 10 OFFSET 5 to both the internal master-index node and the outer INDEX join. The result can contain only 5 rows rather than 10.
Additional information
Severity rationale: this is a stable wrong-result bug in an active secondary-index query path. It has a confirmed owner and a small deterministic regression-test shape.
Suggested fix direction:
- keep the original
LIMIT/OFFSETonly on the outer INDEX join; or - fetch
LIMIT + OFFSETfrom the inner index path and leave the user-visible OFFSET only on the outer layer, using overflow-safe arithmetic.
Add regression coverage for one and multiple master-index predicates, with and without OFFSET.
Activity
fix(plan): preserve pagination across LIMIT optimizations (#25587)
XuPeng-SH commentedon Jul 12, 2026
@Ariznawlll
Ariznawlll commentedon Jul 13, 2026
已在最新
main@2865ed6cc8a39727b8b274bb52b88fe441d0b30c验证通过。df6a34119/ fix(plan): preserve pagination across LIMIT optimizations #25587。TestMasterIndexPaginationIsAppliedOnce使用-race -count=5通过。IN、offset=0、中页、尾页、offset=结果集长度,以及ORDER BY id LIMIT 10 OFFSET 5的精确内容;全部结果正确。未再出现 OFFSET 被消费两次,修复有效。
Ariznawlll commentedon Jul 13, 2026
已在最新
main上完成验证,并在 matrixorigin/motr#48 中补充回归测试。验证环境
main@52afd25fa63a3cb6037b7bf8ff0bf1328c4793ecissue_25586_master_index_limit_offset测试过程
围绕 master index scan 中 LIMIT/OFFSET 被重复应用的问题,覆盖:
INpredicate;验证结果
fix(plan): preserve pagination across LIMIT optimizations (matrixorig…
fix(plan): preserve pagination across LIMIT optimizations (matrixorig…
fix(plan): preserve pagination across LIMIT optimizations (matrixorig…