Skip to content

[Bug]: Index-only aggregates over composite secondary indexes return zero or NULL #25348

Closed

Description

@Ariznawlll

Is there an existing issue for the same bug?

I have checked the existing issues.

Existing issue #25345 covers COUNT(*) returning zero through composite secondary indexes for DATE / DECIMAL / INT predicates. This issue covers aggregate functions over indexed columns themselves, such as COUNT(col), SUM, AVG, MIN, and MAX, where the plan aggregates values extracted from __mo_index_idx_col.

Branch Name

main

Commit ID

0e8602b78

Other Environment Information

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

Actual Behavior

When MatrixOne uses a composite secondary index as an index-only aggregate source, aggregates over the indexed leading column return zero counts and NULL values even though matching rows exist.

Examples observed on latest main:

-- INT indexed column, score BETWEEN 7 AND 10
MySQL / MO non-indexed control: count=4, sum=32, avg=8, min=7, max=10
MO indexed path:              count=0, sum=NULL, avg=NULL, min=NULL, max=NULL

-- DECIMAL indexed column, amount BETWEEN 1.00 AND 3.00
MySQL / MO non-indexed control: count=4, sum=8.05, avg=2.0125, min=1.25, max=3.00
MO indexed path:              count=0, sum=NULL, avg=NULL, min=NULL, max=NULL

-- DATE indexed column, date range
MySQL / MO non-indexed control: count=4, min=2024-01-02, max=2024-01-15
MO indexed path:              count=0, min=NULL, max=NULL

EXPLAIN shows aggregates being computed from serial_extract(__mo_index_idx_col, ...) on an Index Table Scan:

Aggregate Functions: starcount(1), count(serial_extract(__mo_index_idx_col, 0, INT))),
                     sum(serial_extract(__mo_index_idx_col, 0, INT))),
                     avg(serial_extract(__mo_index_idx_col, 0, INT))),
                     min(serial_extract(__mo_index_idx_col, 0, INT))),
                     max(serial_extract(__mo_index_idx_col, 0, INT)))
  -> Index Table Scan on t_idx.idx_score_status
       Filter Cond: prefix_between(__mo_index_idx_col)

Expected Behavior

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

Steps to Reproduce

DROP DATABASE IF EXISTS mysql_compat_index_only_agg_min;
CREATE DATABASE mysql_compat_index_only_agg_min;
USE mysql_compat_index_only_agg_min;

CREATE TABLE t_idx (
  id varchar(32) not null,
  tenant varchar(32) not null,
  sess varchar(32) not null,
  status varchar(16) not null,
  score int null,
  amount decimal(10,2) null,
  d date null,
  primary key(tenant, sess, id),
  index idx_score_status(score, status, tenant, sess, id),
  index idx_amount_status(amount, status, tenant, sess, id),
  index idx_d_status(d, status, tenant, sess, id)
);

CREATE TABLE t_no_idx (
  id varchar(32) not null,
  tenant varchar(32) not null,
  sess varchar(32) not null,
  status varchar(16) not null,
  score int null,
  amount decimal(10,2) null,
  d date null,
  primary key(tenant, sess, id)
);

INSERT INTO t_idx VALUES
 ('a1','u1','s1','active',7,1.25,'2024-01-02'),
 ('a2','u1','s2','active',8,1.30,'2024-01-03'),
 ('a3','u1','s3','active',7,2.50,'2024-01-04'),
 ('a4','u2','s1','archived',10,3.00,'2024-02-01'),
 ('a5','u2','s2','pending',NULL,NULL,NULL),
 ('a6','u2','s3','active',-3,-3.40,'2024-01-15');
INSERT INTO t_no_idx SELECT * FROM t_idx;

SELECT 'score_idx' q, COUNT(*), COUNT(score), SUM(score), AVG(score), MIN(score), MAX(score)
FROM t_idx
WHERE score BETWEEN 7 AND 10;

SELECT 'score_no_idx' q, COUNT(*), COUNT(score), SUM(score), AVG(score), MIN(score), MAX(score)
FROM t_no_idx
WHERE score BETWEEN 7 AND 10;

SELECT 'amount_idx' q, COUNT(*), COUNT(amount), SUM(amount), AVG(amount), MIN(amount), MAX(amount)
FROM t_idx
WHERE amount BETWEEN 1.00 AND 3.00;

SELECT 'amount_no_idx' q, COUNT(*), COUNT(amount), SUM(amount), AVG(amount), MIN(amount), MAX(amount)
FROM t_no_idx
WHERE amount BETWEEN 1.00 AND 3.00;

SELECT 'date_idx' q, COUNT(*), COUNT(d), MIN(d), MAX(d)
FROM t_idx
WHERE d >= '2024-01-01' AND d < '2024-02-01';

SELECT 'date_no_idx' q, COUNT(*), COUNT(d), MIN(d), MAX(d)
FROM t_no_idx
WHERE d >= '2024-01-01' AND d < '2024-02-01';

EXPLAIN SELECT COUNT(*), COUNT(score), SUM(score), AVG(score), MIN(score), MAX(score)
FROM t_idx
WHERE score BETWEEN 7 AND 10;

Additional information

This was found while backfilling composite-index compatibility coverage for index-only aggregate paths. Row-returning scans and base-table aggregate paths can still return correct rows; the wrong result appears when the optimizer satisfies aggregates from the secondary index table scan.

Activity

  1. added
    kind/bug
    severity/s1
    on Jul 2, 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. Ariznawlll commented on Jul 2, 2026

    @Ariznawlll
    CollaboratorAuthor

    Supplemental DML/index-only aggregate coverage on latest main 51656deb6:

    After UPDATE / DELETE / INSERT on a table with composite secondary index (status, due, amount, tenant, id), row-returning queries can return the correct rows while aggregate queries over the same indexed predicate return zero/NULL.

    Observed after DML:

    where status='active' and due='2024-01-02'
    
    MO indexed row query:
      (8,'t4','active','2024-01-02',18.00)
      (2,'t1','active','2024-01-02',20.00)
      (3,'t1','active','2024-01-02',22.00)
    
    MO no-index control:
      same 3 rows
    
    MO indexed aggregate:
      count(*) = 0, sum(amount) = NULL
    
    MO no-index control aggregate / MySQL:
      count(*) = 3, sum(amount) = 60.00
    

    This makes the failure look plan/path-specific: the indexed row scan can see the rows, but the index-only aggregate path loses them.

  7. Ariznawlll commented on Jul 2, 2026

    @Ariznawlll
    CollaboratorAuthor

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

    A deterministic 30-step DML chain (UPDATE indexed columns, DELETE, REPLACE, ODKU without unique-key-column update, primary-key update, NULL/value transitions) kept the final data in the indexed table and no-secondary-index control table identical. However, aggregate reads through composite secondary indexes still returned zero/NULL after the DML chain.

    Examples:

    checkpoint_10, predicate: status='new'
    MySQL / MO no-index control: count=6, sum(amount)=463.00
    MO indexed aggregate:        count=0, sum(amount)=NULL
    
    checkpoint_30, predicate: due >= '2024-02-10'
    MySQL / MO no-index control: count=5, sum(amount)=718.00
    MO indexed aggregate:        count=0, sum(amount)=NULL
    

    The final ordered row dump from t_idx and t_scan matched, so this pass did not find dirty-index data corruption; it reinforces that the failure is in index-only aggregate read paths after normal DML maintenance.

  8. Ariznawlll commented on Jul 3, 2026

    @Ariznawlll
    CollaboratorAuthor

    Supplemental Index Model K large deterministic random-DML-chain coverage on latest main 51656deb6:

    I ran a larger generated chain with:

    • 1000 initial rows
    • 200 deterministic mixed DML operations (INSERT, UPDATE indexed columns, DELETE, REPLACE)
    • checkpoint every 25 operations
    • indexed table and no-secondary-index control table with identical data

    Data maintenance checks stayed aligned at every checkpoint:

    idx_all count / amount_sum / score_sum == scan_all count / amount_sum / score_sum
    

    But predicate aggregates over composite secondary indexes returned 0/NULL from the initial checkpoint onward:

    checkpoint_000:
      status='new'
        MySQL / MO no-index control: count=200, sum(amount)=10151.75
        MO indexed aggregate:        count=0,   sum(amount)=NULL
    
      policy='keep' and score between 100 and 300
        MySQL / MO no-index control: count=129, sum(score)=25867
        MO indexed aggregate:        count=0,   sum(score)=NULL
    
      due >= '2024-02-01' and due < '2024-03-01'
        MySQL / MO no-index control: count=300, sum(amount)=15809.95
        MO indexed aggregate:        count=0,   sum(amount)=NULL
    

    The same zero/NULL pattern persisted through checkpoints 25/50/75/100/125/150/175/200 while full-table checksums remained aligned. This again points to index-only aggregate read path bugs rather than DML maintenance corruption.

  9. ck89119 commented on Jul 3, 2026

    @ck89119
    Contributor

    Root cause analysis

    Same root cause as #25341 / #25344 / #25345 / #25351, not specific to the aggregate path itself.

    Every secondary index in the reproduction explicitly contains all primary key columns (e.g. idx_score_status(score, status, tenant, sess, id) covers PK (tenant, sess, 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 index-only aggregate (COUNT(col), SUM, AVG, MIN, MAX over serial_extract(__mo_index_idx_col, ...)) therefore returned zero / NULL.

    This issue was reported at commit 0e8602b78, which predates the fix.

    Fixed by #25342

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

    score_idx   count=4, count(score)=4, sum=32, avg=8, min=7, max=10        (was 0/NULL)
    amount_idx  count=4, count(amount)=4, sum=8.05, avg=2.0125, min=1.25, max=3.00
    date_idx    count=4, count(d)=4, min=2024-01-02, max=2024-01-15
    

    All results now match MySQL and the MatrixOne non-indexed control table. EXPLAIN confirms the query still goes through the index-only aggregate path (Aggregate(serial_extract(__mo_index_idx_col, ...)) -> Index Table Scan with prefix_between), i.e. the aggregate path itself is correct once the hidden index tables are maintained. The INSERT plan's Multi Update now maintains all three hidden index tables, and their row counts match the base table.

  10. 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:

    25348_score_idx       count=4, count(score)=4, sum=32, avg=8, min=7, max=10
    25348_score_no_idx    count=4, count(score)=4, sum=32, avg=8, min=7, max=10
    25348_amount_idx      count=4, count(amount)=4, sum=8.05, avg=2.01250000, min=1.25, max=3.00
    25348_amount_no_idx   count=4, count(amount)=4, sum=8.05, avg=2.01250000, min=1.25, max=3.00
    25348_date_idx        count=4, count(d)=4, min=2024-01-02, max=2024-01-15
    25348_date_no_idx     count=4, count(d)=4, min=2024-01-02, max=2024-01-15
    

    The indexed aggregate results now match the non-indexed control table and MySQL. MySQL and MO only format AVG scale slightly differently; the numeric values are the same. This issue can be closed as fixed by #25342.

  11. Ariznawlll commented on Jul 3, 2026

    @Ariznawlll
    CollaboratorAuthor

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

  12. Ariznawlll commented on Jul 3, 2026

    @Ariznawlll
    CollaboratorAuthor

    Regression case added in matrixorigin/motr#39.

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