Repository navigation
Description
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
Ariznawlll commentedon Jul 2, 2026
Supplemental DML/index-only aggregate coverage on latest main
51656deb6:After
UPDATE/DELETE/INSERTon 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:
This makes the failure look plan/path-specific: the indexed row scan can see the rows, but the index-only aggregate path loses them.
Ariznawlll commentedon Jul 2, 2026
Supplemental Index Model I random-DML-chain coverage on latest main
51656deb6:A deterministic 30-step DML chain (
UPDATEindexed columns,DELETE,REPLACE,ODKUwithout 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:
The final ordered row dump from
t_idxandt_scanmatched, 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.Ariznawlll commentedon Jul 3, 2026
Supplemental Index Model K large deterministic random-DML-chain coverage on latest main
51656deb6:I ran a larger generated chain with:
INSERT,UPDATEindexed columns,DELETE,REPLACE)Data maintenance checks stayed aligned at every checkpoint:
But predicate aggregates over composite secondary indexes returned
0/NULLfrom the initial checkpoint onward: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.
ck89119 commentedon Jul 3, 2026
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,getValidIndexesinpkg/sql/plan/bind_insert.gosilently 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,MAXoverserial_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, includes2bf76218f) with the full reproduction script from this issue, on a clean instance:All results now match MySQL and the MatrixOne non-indexed control table.
EXPLAINconfirms the query still goes through the index-only aggregate path (Aggregate(serial_extract(__mo_index_idx_col, ...)) -> Index Table Scanwithprefix_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.Ariznawlll commentedon Jul 3, 2026
Verified on latest
main@2a8f3b884989(built locally from latestorigin/main).Validation result: fixed.
Compared with MySQL 8.0.45 using the repro from this issue:
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.
Ariznawlll commentedon Jul 3, 2026
Closing after verification on latest main @ 2a8f3b8: index-only aggregate results now match MySQL and the no-index control table. Fixed by #25342.
Ariznawlll commentedon Jul 3, 2026
Regression case added in matrixorigin/motr#39.
Case:
14_issue_regression.issue_25348_composite_index_only_aggregatesCommit:
0bf1ab1Target branch marker:
@target-branch: mainValidation on latest main passed with the composite-index regression group: