Skip to content

[Bug]: VARCHAR range predicates return zero rows through composite secondary index #25344

Closed

Description

@Ariznawlll

Is there an existing issue for the same bug?

I have checked the existing issues.

I found closed issue #24304 mentioning prefix_between, but that issue describes a development-time seek optimization problem and says block filtering still validates rows, so it should not cause incorrect query results. The case below reproduces an incorrect result on latest main.

Branch Name

main

Commit ID

0e8602b78

Other Environment Information

  • Local MatrixOne built from latest origin/main
  • MySQL oracle: MySQL 8.0.45

Actual Behavior

For a table with an all-VARCHAR composite primary key and a composite secondary index whose leading column is VARCHAR, range predicates on that indexed VARCHAR column return zero rows through the secondary-index path.

The same data and predicate return the expected rows on:

  • MySQL
  • MatrixOne control table without the secondary index

Observed on MatrixOne latest main:

idx_between     0
no_idx_between  2
idx_range       0
no_idx_range    2

EXPLAIN shows the wrong-result query uses the secondary index table scan:

Aggregate Functions: starcount(1)
  -> Index Table Scan on t_idx.idx_policy
       Filter Cond: prefix_between(__mo_index_idx_col)

For the equivalent >= / < range predicate, EXPLAIN also uses the secondary index table scan with serialized index-column predicates.

Expected Behavior

The indexed table should return the same result as MySQL and the MatrixOne non-indexed control table.

For the sample below, both indexed predicates should return 2.

Steps to Reproduce

DROP DATABASE IF EXISTS mysql_compat_idx_seed_mut_range;
CREATE DATABASE mysql_compat_idx_seed_mut_range;
USE mysql_compat_idx_seed_mut_range;

CREATE TABLE t_idx (
  id varchar(64) not null,
  user_id varchar(64) not null,
  session_id varchar(64) not null,
  policy varchar(32) not null,
  due datetime(6) null,
  primary key(user_id, session_id, id),
  index idx_policy(policy, due, user_id, session_id, id)
);

CREATE TABLE t_no_idx (
  id varchar(64) not null,
  user_id varchar(64) not null,
  session_id varchar(64) not null,
  policy varchar(32) not null,
  due datetime(6) null,
  primary key(user_id, session_id, id)
);

INSERT INTO t_idx VALUES
  ('a1','u1','s1','keep','2030-01-01 00:00:00.000000'),
  ('a2','u1','s2','purge','2030-01-02 00:00:00.000000'),
  ('a3','u2','s1','key','2030-01-03 00:00:00.000000');
INSERT INTO t_no_idx SELECT * FROM t_idx;

SELECT 'idx_between' AS q, COUNT(*) AS cnt
FROM t_idx
WHERE policy BETWEEN 'kee' AND 'kez';

SELECT 'no_idx_between' AS q, COUNT(*) AS cnt
FROM t_no_idx
WHERE policy BETWEEN 'kee' AND 'kez';

SELECT 'idx_range' AS q, COUNT(*) AS cnt
FROM t_idx
WHERE policy >= 'kee' AND policy < 'kez';

SELECT 'no_idx_range' AS q, COUNT(*) AS cnt
FROM t_no_idx
WHERE policy >= 'kee' AND policy < 'kez';

SELECT 'idx_between_rows' AS q, id, policy
FROM t_idx
WHERE policy BETWEEN 'kee' AND 'kez'
ORDER BY id;

SELECT 'no_idx_between_rows' AS q, id, policy
FROM t_no_idx
WHERE policy BETWEEN 'kee' AND 'kez'
ORDER BY id;

EXPLAIN SELECT COUNT(*)
FROM t_idx
WHERE policy BETWEEN 'kee' AND 'kez';

EXPLAIN SELECT COUNT(*)
FROM t_idx
WHERE policy >= 'kee' AND policy < 'kez';

Additional information

MySQL 8.0.45 result for the same SQL:

idx_between     2
no_idx_between  2
idx_range       2
no_idx_range    2

MatrixOne latest main result at 0e8602b78:

idx_between     0
no_idx_between  2
idx_range       0
no_idx_range    2

This is a deterministic wrong-result issue on a read query.

Activity

  1. added
    kind/bug
    severity/s1
    on Jul 1, 2026
  2. added
    severity/s0
    and removed
    severity/s1
    on Jul 2, 2026
  3. added this to the MO-202607 milestone on Jul 2, 2026
  4. added theissue type on Jul 2, 2026
  5. added and removed
    severity/s0
    on Jul 2, 2026
  6. ck89119 commented on Jul 2, 2026

    @ck89119
    Contributor

    Root cause analysis

    This is not related to the VARCHAR type or the prefix_between evaluation path. The actual trigger is that the secondary index parts explicitly contain all primary key columns (idx_policy(policy, due, user_id, session_id, id) covers PK (user_id, session_id, id)).

    PR #22053 introduced getValidIndexes in pkg/sql/plan/bind_insert.go, which silently drops indexes covering the full primary key from the DML maintenance list (INSERT/DELETE/UPDATE/REPLACE). As a result the hidden index table __mo_index_secondary_* stays permanently empty (verified: COUNT(*) on the hidden table returns 0 after inserting rows), while the optimizer (apply_indices.go) still chooses that index for the Index Table Scan — so every query through the index path returns 0 rows. The evaluation layers (prefix_between, PK filter, zonemap) are all correct.

    Same root cause as #25341, #25345 and #25351.

    Fixed by #25342

    This has been fixed by #25342 (commit 2bf76218f, merged 2026-07-02), which restores DML maintenance for regular secondary indexes whenever their hidden table exists.

    Verified on latest main including 2bf76218f with the reproduction script from this issue:

    idx_between   2   (was 0)
    idx_range     2   (was 0)
    idx_between_rows -> a1/keep, a3/key
    

    Also verified no regression on the original #22053 scenario: DELETE / UPDATE / REPLACE / ON DUPLICATE KEY UPDATE on covering secondary indexes (single-column and composite PK) all keep the index table consistent.

    This issue can be closed as fixed by #25342.

  7. Ariznawlll commented on Jul 3, 2026

    @Ariznawlll
    CollaboratorAuthor

    Verified on latest main@2a8f3b884989 (built locally from latest origin/main).

    Validation result: fixed.

    Compared with MySQL 8.0.45 using the repro from this issue:

    25344_idx_between        2
    25344_no_idx_between     2
    25344_idx_range          2
    25344_no_idx_range       2
    25344_idx_rows           a1:keep|a3:key
    

    The indexed table now matches the non-indexed control table and MySQL. This issue can be closed as fixed by #25342.

  8. Ariznawlll commented on Jul 3, 2026

    @Ariznawlll
    CollaboratorAuthor

    Closing after verification on latest main @ 2a8f3b8: the repro now matches MySQL and the no-index control table. Fixed by #25342.

  9. Ariznawlll commented on Jul 3, 2026

    @Ariznawlll
    CollaboratorAuthor

    Regression case added in matrixorigin/motr#39.

    Case: 14_issue_regression.issue_25344_composite_index_varchar_range
    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