Skip to content

[Bug]: large nullable secondary-index UPDATE misaligns rows and makes DELETE miss rows #27398

Closed

Description

@Lundomn

Problem

On a fresh table with a nullable single-column secondary index, an ordinary large UPDATE of the indexed column can silently misalign values with primary-key rows. The base-table path and secondary-index path then disagree, and a subsequent DELETE misses rows even though the statement succeeds.

This is not an inherited stale-index case: the database, table, data, and index are created from scratch on the tested main commit.

Versions and environment

  • MatrixOne branch: main
  • MatrixOne commit: bcfa944abcacf9ab6fc4e2cd0925c64d5f5fe113 (latest main tested on 2026-08-20)
  • Build: local source build with Go 1.26.4
  • Deployment: fresh single-process launch with new CN/TN/log data directories
  • OS: macOS; MySQL protocol port 6001
  • Client path: ordinary text-protocol SQL, not COM_STMT_PREPARE

Minimal reproduction

Create the table:

DROP DATABASE IF EXISTS nullable_index_update_repro;
CREATE DATABASE nullable_index_update_repro;
USE nullable_index_update_repro;

CREATE TABLE t (
  id INT PRIMARY KEY,
  n  INT NULL
);

Load 10,000 deterministic rows using 250 separate 40-row INSERT ... VALUES statements. The statement boundaries are significant for this reproduction. With any MySQL-compatible Python cursor connected to MatrixOne:

for start in range(0, 10000, 40):
    values = []
    for i in range(start, min(start + 40, 10000)):
        n = "NULL" if i % 7 == 0 else str(i % 19 - 9)
        values.append(f"({i},{n})")
    cursor.execute("INSERT INTO t VALUES " + ",".join(values))

Create the index, update its nullable key, and compare the indexed predicate with an equivalent full expression scan:

CREATE INDEX idx_n ON t(n);

UPDATE t
SET n = COALESCE(n, 0)
WHERE id % 4 = 0;
-- reports 2500 affected rows

SELECT COUNT(*) FROM t WHERE n = 0;

SELECT SUM(IF(n + 0 = 0, 1, 0))
FROM t;

SELECT SUM(IF(NOT(
  n <=> CASE
    WHEN id % 7 = 0 AND id % 4 = 0 THEN 0
    WHEN id % 7 = 0 THEN NULL
    ELSE id % 19 - 9
  END
), 1, 0)) AS value_mismatches
FROM t;

DELETE FROM t WHERE id % 17 = 0;

SELECT COUNT(*) FROM t;
SELECT COUNT(*) FROM t WHERE id % 17 = 0;

Actual result

After the UPDATE:

UPDATE affected rows                          2500
SELECT COUNT(*) ... WHERE n = 0                809
expression/full-scan equivalent               1445
rows whose n differs from deterministic model 1729

The mismatch sample includes rows whose non-NULL values unexpectedly become NULL or 0, and rows expected to remain NULL that read as 0.

After the DELETE:

expected deleted rows   589
actual affected rows    489
expected final rows    9411
actual final rows      9511
remaining id % 17 = 0   100

The same 10,000-row case reproduced repeatedly with identical counts.

A separate Kafka-to-MatrixOne deterministic pipeline reproduced the downstream failure twice at 100,000 rows: expected final count 94,117, actual 95,061 (944 missed deletes). Kafka round-trip, initial row count, NULL aggregates, grouped aggregates, transaction atomicity, and pre-update index equivalence all passed before the indexed-column update.

Expected result

  • UPDATE must preserve the association between every primary-key row and its value.
  • The indexed predicate and an equivalent base/full-scan expression must both return 809 zero-valued rows.
  • value_mismatches must be 0.
  • DELETE WHERE id % 17 = 0 must delete 589 rows and leave 9,411 rows with no remaining matches.
  • If index maintenance cannot be completed consistently, the transaction must fail instead of committing silent wrong data.

Controls and boundary evidence

  • The same update without a secondary index produces 0 value mismatches and the correct delete count.
  • Creating an index but updating only a non-indexed column also passes.
  • Dropping idx_n before the update and recreating it afterward passes all checks.
  • Loading all rows through one INSERT ... SELECT ... generate_series(...) did not reproduce; repeated 40-row VALUES batches did, indicating a data/block-layout-sensitive execution path.
  • In the original wider deterministic table, 9,996 rows passed and 9,997 rows failed for this load/update shape.

Existing-issue check

I searched open and closed issues for nullable secondary-index updates, row/index inconsistency, wrong-row updates, and deletes missing rows.

Workaround

Before modifying a nullable indexed column, drop the affected secondary index, perform and validate the update, then recreate the index. This workaround passed the reproduction. If the issue has already occurred, restore or recompute the affected base-column values from an authoritative source while the index is absent, then rebuild the index; rebuilding directly from already-corrupted base values is not sufficient.

Activity

  1. self-assigned this
    on Aug 20, 2026
  2. added a commit that references this issue on Aug 21, 2026
    d656668
  3. reopened this on Sep 7, 2026
  4. Lundomn commented on Sep 7, 2026

    @Lundomn
    ContributorAuthor

    QA 转测(2026-09-07)

    • 修复 PR:PR #27446(已 merged)
    • 根因与修复范围:fixed-vector mask shrink 重映射 NULL/group bitmap 时未传播 vector offset,导致 nullable indexed UPDATE 行错位并使 DELETE 漏删;PR 补齐 offset 传播。
    • 开发侧验证:null/vector 单测、10,000 行 BVT(265/265)、全仓 unit/race/static/build/vet 及 218,939-event Kafka 一致性回归通过,受影响函数覆盖率 97.1%-100%。
    • QA 请重点覆盖:大表 nullable secondary-index UPDATE/DELETE、不同 offset、跨 batch/block、NULL bitmap、固定/非固定 vector 及事务回滚。

    请在本 Issue 记录实际测试版本/环境、通过或失败结论、未覆盖项及补测要求。按流程,QA 明确通过后再关闭 Issue;如发现失败或回归请保持 open 并反馈复现信息。

  5. Ariznawlll commented on Sep 8, 2026

    @Ariznawlll
    Collaborator

    测试版本

    • 分支:main
    • commit:4b43f8c99bb22e6a35c389c93bf64c2dc043feaf
    • 部署方式:本地单 CN / TN / LogService,MySQL text protocol。
    • 测试日期:2026-09-08

    原问题验证

    严格使用原始 10,000 行、250 个独立 40 行 INSERT。创建 nullable 二级索引后执行 UPDATE COALESCE(n,0);3 轮中索引扫描和全表扫描均为 809,逐行确定性模型差异为 0。

    Happy Path 覆盖

    1. 强制 idx_n 的 n=0 查询与普通全表路径一致。
    2. 更新后按主键、NULL 分布和计算模型逐行校验,无错位。

    Unhappy Path 覆盖

    1. 随后 DELETE id%17=0;每轮剩余 9411 行,索引路径 n=0 为 760,未出现漏删或陈旧索引项。

    白盒验证

    不适用:本次使用公开 SQL 重现原始大批量路径。

    回归测试

    已加入 motr#151:14_issue_regression.issue_27398_nullable_index_large,保留 10,000 行与 40 行分批边界,MOTR 3 轮通过。

    测试证据

    三轮输出一致:indexed=809、full=809、mismatches=0、rows=9411、indexed_zero=760。

    测试结论

    测试通过。

  6. added 3 commits that reference this issue on Sep 17, 2026
    719bff0
    f470f08
    6ca1c51
  7. iamlinjunhong commented on Nov 5, 2026

    @iamlinjunhong
    Contributor

    Revalidated this report's exact 10,000-row, 250 separate 40-row INSERT statements fixture on 2026-11-05. The bug remains reproducible on the tested 4.2-dev revision; the main control passes.

    • main: 1d51125661c60ff5216c1216a897a3d312875336
    • 4.2-dev: 58edabfe8f9f8a6c126e0634e7246c031a6f0ade
    • Local source builds, macOS arm64, Go 1.27.1, ordinary text-protocol SQL, synthetic fresh tables. One bounded run per branch; no source changes or manual hidden-index writes.

    Used the original sequence: nullable n, INSERT batches, CREATE INDEX ... (n), UPDATE ... SET n=COALESCE(n,0) WHERE id%4=0, then DELETE ... WHERE id%17=0. Before UPDATE, both branches had 10,000 correct base rows and matching index keys.

    Check Expected / main actual 4.2-dev actual
    Base rows after UPDATE 10,000 10,000
    Base values differing from deterministic per-id model after UPDATE 0 1,729
    Base-scan n=0 rows after UPDATE 809 1,445
    Hidden index keys differing from current base values after UPDATE 0 1,729
    Base rows after DELETE 9,411 9,511
    Remaining rows satisfying id%17=0 0 100

    Direct key-level comparison found no missing/orphan origin-PK mappings or duplicate origin-PK references in this fixture; the discrepancy is wrong/stale key values and corrupted base values. Main remains fully consistent after DELETE; 4.2 has 1,975 model/key discrepancies after DELETE. Counts alone do not detect this: the base and index row counts remain equal.

    Code comparison confirms that the effective #27446 fix is absent from this 4.2-dev revision, not merely absent under its original SHA:

    • 4.2 shrinkFixedByMask copies payload using selection + offset, then calls the offset-free nulls.FilterByMaskInPlace on the NULL/grouping bitmaps in both branches.
    • main uses FilterByMaskInPlaceWithOffset(..., offset) and contains fix(vector): align null masks for offset shrink #27446 (d6566684722323f6c66420dcf7e64582bf275c2b). The tested 4.2 nulls implementation has no equivalent offset adjustment.

    Please evaluate backporting #27446 to 4.2-dev with its vector/nulls and layout-sensitive SQL regressions. This is additional branch evidence for the existing issue, not a new root cause or a reproduction of the missing-only historical cohort in #32245. It is also separate from the transaction-local DELETE + COPY ALTER defect in #32247.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions