Skip to content

[Bug]: Prefix-index covering queries drop values longer than the indexed prefix #26813

Closed

Description

@LeftHandCold

Is there an existing issue for the same bug?

I searched open and closed MatrixOne issues/PRs for prefix indexes, index-only scans, secondary-index substring handling, and the 191-character boundary.

This is distinct from:

Branch Name

main

Commit ID

08770e1beeb23239c91165492f2fe85243a3c66a

Other Environment Information

  • Local single-node MatrixOne on Linux
  • Built from the commit above with Go 1.26.4
  • MySQL client against port 6001
  • Reproduces before and after an explicit TN flush

Actual Behavior

A covering query through a VARCHAR prefix index silently drops a row as soon as the stored value is longer than the declared prefix. The equivalent query that disables the index returns the row.

The boundary is exact for INDEX idx_p(s(191)):

value length   FORCE INDEX count   IGNORE INDEX count
190            1                   1
191            1                   1
192            0                   1

A query that also projects a non-indexed payload and therefore performs base-table backfill returns the correct row. The hidden index is populated, and serial_extract(__mo_index_idx_col, 0, VARCHAR) confirms that only 191 characters are stored. This isolates the defect to the covering/index-only read path rather than index maintenance.

Expected Behavior

An explicitly declared prefix index is a lossy candidate structure. It may narrow base-table rows, but it must not be used as the source of the complete column value. Forced/default index selection must return the same SQL result as a base-table scan.

Steps to Reproduce

CREATE DATABASE prefix_covering_repro;
USE prefix_covering_repro;

CREATE TABLE t (
  id INT PRIMARY KEY,
  s VARCHAR(400) NOT NULL,
  payload VARCHAR(32) NOT NULL,
  INDEX idx_p(s(191))
);

INSERT INTO t VALUES
  (190, CONCAT(REPEAT('a',189),'0'), 'n-1'),
  (191, CONCAT(REPEAT('b',190),'1'), 'n'),
  (192, CONCAT(REPEAT('c',191),'2'), 'n+1'),
  (193, CONCAT(REPEAT('c',191),'3'), 'same-prefix-control');

SELECT COUNT(*) FROM t FORCE INDEX(idx_p)
WHERE s=CONCAT(REPEAT('a',189),'0');
-- 1
SELECT COUNT(*) FROM t IGNORE INDEX(idx_p)
WHERE s=CONCAT(REPEAT('a',189),'0');
-- 1

SELECT COUNT(*) FROM t FORCE INDEX(idx_p)
WHERE s=CONCAT(REPEAT('b',190),'1');
-- 1
SELECT COUNT(*) FROM t IGNORE INDEX(idx_p)
WHERE s=CONCAT(REPEAT('b',190),'1');
-- 1

SELECT COUNT(*) FROM t FORCE INDEX(idx_p)
WHERE s=CONCAT(REPEAT('c',191),'2');
-- wrong: 0
SELECT COUNT(*) FROM t IGNORE INDEX(idx_p)
WHERE s=CONCAT(REPEAT('c',191),'2');
-- correct: 1

SELECT id,payload FROM t FORCE INDEX(idx_p)
WHERE s=CONCAT(REPEAT('c',191),'2');
-- correct through an INDEX join + base-table residual: 192, n+1

SELECT mo_ctl('dn','flush','prefix_covering_repro.t');
SELECT SLEEP(1);

SELECT COUNT(*) FROM t FORCE INDEX(idx_p)
WHERE s=CONCAT(REPEAT('c',191),'2');
-- still wrong: 0

The same index-only failure reproduces with multibyte Unicode values and with a 301-character value behind a 191-character prefix.

Additional Information

The DML path correctly materializes the declared substring in appendIndexPrefixProjection. The read optimizer's tryIndexOnlyScan only checks whether a referenced column name appears in IndexDef.Parts; it does not account for IndexAlgoParams declaring that the stored part is truncated. It then maps the logical column to serial_extract(__mo_index_idx_col, ...), treating the lossy prefix as the full value.

The safe invariant is: an index with any declared prefix length may still feed an INDEX join, but cannot satisfy a covering/index-only projection unless the full source value is available elsewhere.

Please retain both black-box and white-box coverage:

  • public SQL at N-1, N, and N+1, including a same-prefix neighbor, Unicode, flush, covering aggregate/projection, and a backfill control;
  • a planner assertion that tryIndexOnlyScan rejects an IndexDef whose persisted prefix-length metadata is non-empty while the ordinary non-prefix control remains eligible.

Activity

  1. self-assigned this
    on Aug 9, 2026
  2. added a commit that references this issue on Aug 9, 2026
    ac33944
  3. added theissue type on Aug 11, 2026
  4. added
    severity/s0
    on Aug 11, 2026
  5. LeftHandCold commented on Aug 11, 2026

    @LeftHandCold
    CollaboratorAuthor

    Validation result — fixed by #26829.

    Verified on the exact merge commit ac339444b558daf2c3d7fe63f755743449db7696, against the original 08770e1 baseline.

    Baseline: FORCE INDEX lost values longer than the declared prefix (including after TN flush) and could return truncated values through a covering/order path.
    Fix verification: FORCE and IGNORE returned the same full result for the original reproduction, the 191-character N-1/N/N+1 boundary, same-prefix neighbours, Unicode values, projection/order, and post-flush reads. EXPLAIN shows a prefix equality candidate lookup with the original full predicate retained on the base table; lossy prefix indexes no longer serve index-only reconstruction.

    Focused and complete pkg/catalog + pkg/sql/plan tests passed, together with the PR BVT coverage.

    Conclusion: the reported correctness defect is fixed. Closing.

  6. added 3 commits that reference this issue on Sep 17, 2026
    b5b2b81
    31e4bad
    0c59fbb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

severity/s0

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions