Skip to content

[Bug]: covering ORDER BY and DISTINCT queries over composite secondary indexes return empty results #25396

Closed

Description

@Ariznawlll

Is there an existing issue for the same bug?

  • I have checked the existing issues.

Branch Name

main

Commit ID

51656de

Other Environment Information

- MO version: 8.0.30-MatrixOne-v
- MySQL version: 8.0.45
- Verify date: 2026-07-02

Actual Behavior

Covering queries over composite secondary indexes can return empty results, while the same predicate/order returns rows when the query needs to fetch a non-indexed column or when the table has no secondary index.

This affects ORDER BY ... LIMIT and DISTINCT style queries.

Expected Behavior

The covering index path, non-covering path, and no-secondary-index control table should return the same rows as MySQL.

Steps to Reproduce

drop database if exists mysql_compat_index_model_c;
create database mysql_compat_index_model_c;
use mysql_compat_index_model_c;

create table t_idx (
  id int primary key,
  tenant varchar(8) not null,
  status varchar(12) null,
  due date null,
  amount decimal(10,2) null,
  score int null,
  payload varchar(30),
  key idx_status_due_amount_id(status, due, amount, id),
  key idx_tenant_score_id(tenant, score, id),
  key idx_due_status_id(due, status, id)
);

create table t_scan (
  id int primary key,
  tenant varchar(8) not null,
  status varchar(12) null,
  due date null,
  amount decimal(10,2) null,
  score int null,
  payload varchar(30)
);

insert into t_idx values
  (1,'t1','active','2024-01-02',20.00,20,'p1'),
  (2,'t1','active','2024-01-01',10.00,10,'p2'),
  (3,'t1','active','2024-01-02',15.00,30,'p3'),
  (4,'t2','paused','2024-01-03',30.00,40,'p4'),
  (5,'t2','active',null,40.00,50,'p5'),
  (6,'t3',null,'2024-01-04',50.00,60,'p6'),
  (7,'t3','archived','2024-01-05',60.00,null,'p7'),
  (8,'t4','active','2024-01-02',15.00,30,'p8'),
  (9,'t4','active','2024-01-06',70.00,70,'p9'),
  (10,'t5','paused','2024-01-01',5.00,5,'p10');
insert into t_scan select * from t_idx;

-- Covering query: only indexed columns
select id,status,due,amount
from t_idx
where status='active'
order by status,due,amount,id
limit 5;

-- Non-covering query: includes payload
select id,status,due,amount,payload
from t_idx
where status='active'
order by status,due,amount,id
limit 5;

-- No-secondary-index control
select id,status,due,amount
from t_scan
where status='active'
order by status,due,amount,id
limit 5;

-- DISTINCT over composite secondary index
select distinct tenant,score
from t_idx
where tenant >= 't1'
order by tenant,score;

MySQL 8.0.45:

-- covering query and no-index control both return:
5  active  NULL        40.00
2  active  2024-01-01  10.00
3  active  2024-01-02  15.00
8  active  2024-01-02  15.00
1  active  2024-01-02  20.00

-- DISTINCT returns tenant/score rows:
t1  10
t1  20
t1  30
t2  40
t2  50
t3  NULL
t3  60
t4  30
t4  70
t5  5

MatrixOne main 51656deb6:

-- covering query over t_idx:
empty result

-- non-covering query over t_idx:
5  active  NULL        40.00  p5
2  active  2024-01-01  10.00  p2
3  active  2024-01-02  15.00  p3
8  active  2024-01-02  15.00  p8
1  active  2024-01-02  20.00  p1

-- no-secondary-index control:
same rows as MySQL

-- DISTINCT tenant,score over t_idx:
empty result

Additional information

This is separate from the existing index-only aggregate issues because the failing queries are row-returning covering SELECT / DISTINCT queries, not aggregate-only queries. It was found during the Index Model C ORDER BY / LIMIT / DISTINCT pass.

Activity

  1. added this to the MO-202607 milestone on Jul 2, 2026
  2. added
    kind/bug
    on Jul 2, 2026
  3. added theissue type on Jul 2, 2026
  4. Ariznawlll commented on Jul 2, 2026

    @Ariznawlll
    CollaboratorAuthor

    Supplemental Index Model E type-mix coverage on latest main 51656deb6:

    The same covering-row-query issue also appears when the leading composite-secondary-index column is DECIMAL.

    Table shape:

    key idx_d_v (d, v, id)

    Observed:

    where d = 10.50
    
    MySQL / MO no-index control:
      (1, 10.50, 'a')
      (2, 10.50, 'b')
    
    MO indexed table covering query:
      empty result
    
    where d = 10.50 and v = 'a'
    
    MySQL / MO no-index control:
      (1, 10.50, 'a')
    
    MO indexed table covering query:
      empty result
    

    Other type-mix paths in the same pass matched MySQL: binary leading equality/range, datetime leading equality/range, NULL predicates, and a non-leading decimal predicate under a binary leading key. This points again at specific covering composite-secondary-index row retrieval, not all type combinations.

  5. Ariznawlll commented on Jul 2, 2026

    @Ariznawlll
    CollaboratorAuthor

    Supplemental Index Model I random-DML-chain coverage on latest main 51656deb6:

    After a deterministic 30-step DML chain, a covering row-returning query over a composite secondary index returned empty results while the no-secondary-index control matched MySQL.

    Example at checkpoint_20:

    predicate: policy='keep' and score >= 100
    projection: id, tenant, policy, score
    index shape: key idx_policy_score(policy, score, tenant, id)
    
    MySQL / MO no-index control:
      (2,  't1', keep, 120)
      (13, 't7', keep, 130)
      (14, 't8', keep, 140)
      (15, 't9', keep, 150)
    
    MO indexed table covering query:
      empty result
    

    The final full-row checksum matched between indexed/control tables, so this again points to covering secondary-index read retrieval rather than DML maintenance corruption.

  6. ck89119 commented on Jul 3, 2026

    @ck89119
    Contributor

    Root cause analysis

    Same root cause as #25341 / #25348, not specific to the ORDER BY / DISTINCT paths themselves.

    Every secondary index in the reproduction explicitly contains the primary key column (e.g. idx_status_due_amount_id(status, due, amount, id) covers PK id). Before PR #25342, getValidIndexes in pkg/sql/plan/bind_insert.go silently dropped such covering indexes from the DML maintenance list, so their hidden index tables stayed permanently empty, while the optimizer still chose them for the Index Table Scan. Any covering query served from the hidden index table — including the row-returning ORDER BY ... LIMIT and DISTINCT shapes here — therefore returned empty results, while the non-covering query (fetching payload from the base table) and the no-index control table were unaffected.

    This issue was reported at commit 51656deb6, which is the direct parent of the fix commit 2bf76218f.

    Fixed by #25342

    Verified on latest main (2a8f3b884, includes 2bf76218f) with the full reproduction script from this issue, on a clean instance:

    -- covering ORDER BY ... LIMIT over t_idx (was empty):
    5  active  NULL        40.00
    2  active  2024-01-01  10.00
    3  active  2024-01-02  15.00
    8  active  2024-01-02  15.00
    1  active  2024-01-02  20.00
    
    -- DISTINCT tenant,score over t_idx (was empty):
    t1 10 / t1 20 / t1 30 / t2 40 / t2 50 / t3 NULL / t3 60 / t4 30 / t4 70 / t5 5
    

    All four queries (covering, non-covering, no-index control, DISTINCT) now match MySQL 8.0.45. EXPLAIN confirms both previously-failing queries still go through the covering-index path (Index Table Scan on t_idx.idx_status_due_amount_id / idx_tenant_score_id with serial_extract sort keys / group keys), i.e. the covering path itself is correct once the hidden index tables are maintained.

  7. Ariznawlll commented on Jul 3, 2026

    @Ariznawlll
    CollaboratorAuthor

    Verified on latest main @ 2a8f3b8. Covering ORDER BY/LIMIT, non-covering control, no-index control, and DISTINCT now all match MySQL 8.0.45. Fixed by #25342; closing.

  8. Ariznawlll commented on Jul 3, 2026

    @Ariznawlll
    CollaboratorAuthor

    Regression case added in matrixorigin/motr#39.

    Case: 14_issue_regression.issue_25396_composite_index_order_distinct
    Commit: 0bf1ab1
    Target branch marker: @target-branch: main

    Validation on latest main passed with the composite-index regression group:

    SUMMARY | total=14 success=14 failed=0 skipped=0 | stmts=158 ok=158 fail=0
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s-1

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions