Skip to content

[Bug]: FORCE INDEX with ORDER BY on nullable UNIQUE keys silently omits NULL rows #29557

Closed

Description

@XuPeng-SH

Is there an existing issue for the same bug?

  • I have checked the existing issues.

Branch Name

main

Commit ID

79fa30e

Other Environment Information

Debian Linux x86_64; isolated CPU build, two CNs, SQL on CN1. Independent MySQL 8.0.44 reference. Three-row reduction; also reproduced before/after real flush, during own writes, after rollback and after delete/reinsert.

Actual Behavior

FORCE INDEX(uq) on a nullable UNIQUE key, combined with ORDER BY, silently omits rows whose unique key is NULL. This affects both covering scans and backfill joins. CTAS can persist the incomplete result without an error.

For rows (1,NULL,11),(2,NULL,22),(3,1,33):

  • Unhinted SELECT returns all three.
  • SELECT id,k FROM t FORCE INDEX(uq) ORDER BY id returns only (3,1).
  • SELECT id,k,v FROM t FORCE INDEX(uq) WHERE k IS NULL ORDER BY id returns no rows, expected two.
  • <=>NULL likewise returns no rows.
  • CREATE TABLE copied AS SELECT id,k FROM t FORCE INDEX(uq) ORDER BY id succeeds but copied contains only (3,1).

MySQL returns all three / both NULL rows for the same forced queries. Removing ORDER BY from the MatrixOne controls returns all three; this is specifically an unsafe forced access selected in the ordering path, not every FORCE INDEX query.

Expected Behavior

An index hint may alter access cost and order, but must preserve the full logical rowset. An incomplete physical unique index must not replace/filter the base scan unless NULL-containing keys are excluded by a proven query predicate. Use a safe base-scan fallback where the hinted index cannot cover the requested rows.

Steps to Reproduce

CREATE DATABASE qa_nullable_hint;
USE qa_nullable_hint;
CREATE TABLE t(id INT PRIMARY KEY,k INT,v INT,UNIQUE KEY uq(k));
INSERT INTO t VALUES(1,NULL,11),(2,NULL,22),(3,1,33);
SELECT id,k FROM t ORDER BY id; -- three rows
SELECT id,k FROM t FORCE INDEX(uq) ORDER BY id; -- only (3,1)
SELECT id,k,v FROM t WHERE k IS NULL ORDER BY id; -- two rows
SELECT id,k,v FROM t FORCE INDEX(uq) WHERE k IS NULL ORDER BY id; -- zero
SELECT id,k,v FROM t FORCE INDEX(uq) WHERE k<=>NULL ORDER BY id; -- zero
EXPLAIN SELECT id,k FROM t FORCE INDEX(uq) ORDER BY id;
EXPLAIN SELECT id,k,v FROM t FORCE INDEX(uq) WHERE k IS NULL ORDER BY id;
CREATE TABLE copied AS SELECT id,k FROM t FORCE INDEX(uq) ORDER BY id;
SELECT * FROM copied ORDER BY id; -- only (3,1)

Control WHERE k=1 returns the correct non-NULL row. The failing index-vs-heap matrix preserves the same failure across memory, flush, workspace, rollback and committed delete/reinsert (10 failed semantic cells, one underlying defect).

Additional information

Root cause is incompatible access completeness, not missing base data. PreInsertUnique.Call in pkg/sql/colexec/preinsertunique/preinsertunique.go:198 uses util.CompactSingleIndexCol; pkg/sql/util/index_util.go:611 removes NULL keys and aligns primary-key values to the compacted rows. This is intentional UNIQUE storage behavior.

The forced access owner applyForcedHintAccessToScan/tryHintedIndexAccess in pkg/sql/plan/apply_indices.go:1594–1627 admits an index without proving the nullable unique parts cannot be NULL for qualifying rows. tryHintedCoveringIndexScan checks column coverage but not row coverage. buildHintedIndexBackfillJoin similarly introduces an INDEX join on the sparse physical index. The covering EXPLAIN shows only Index Table Scan on t.uq; the noncovering EXPLAIN shows base k IS NULL plus INDEX join/runtime filter built from t.uq, which excludes both NULL-key base rows. Both consumers need the same completeness admission contract; fixing only a residual filter or only the covering branch cannot restore omitted rows.

Suggested priority: P1 (silent wrong results and persisted CTAS data omission). Do not change UNIQUE semantics or introduce a duplicate index-storage path just to make FORCE access complete. Validate composite unique keys with partially NULL parts, OR predicates admitting NULL, no predicate, ORDER/GROUP scopes, covering/backfill, prepared consumers, and nearest non-NULL/NOT NULL controls. No historical regression attribution is claimed.

Local evidence: /mnt/nvme/main-qa-20261001/round3/index-results.json, confirmed-null-index.json, null-index-additional.json, null-index-ctas.json. Duplicate search found #26821 (strict-less-than on regular secondary index returning extra NULL rows), which is a different physical index and opposite error.

Additional confirmed boundary matrix: nullable composite UNIQUE(k,v) also omits either-NULL rows. k IS NOT NULL alone is insufficient: it loses the qualifying (2,1,NULL) row. k=1 loses that same row; k=1 OR k IS NULL loses two rows. The composite predicate k IS NOT NULL AND v IS NOT NULL and a single-column declared NOT NULL unique table match their base-scan controls. Evidence: round3/null-index-boundaries.json. This localizes the required proof to every unique-key part, not just the part named by one predicate.

Hint-scope closure: SELECT k,count(*) FROM t FORCE INDEX(uq) GROUP BY k ORDER BY k and FORCE INDEX FOR GROUP BY(uq) also lose the NULL group (NULL,2). The tested USE INDEX, IGNORE INDEX and ORDER-scoped FORCE controls preserve it. Evidence: round3/hint-scope-results.json (20 shape/scope cells). The affected result consumer therefore includes grouped aggregates, not only plain SELECT/CTAS.

Activity

  1. self-assigned this
    on Oct 1, 2026
  2. added
    kind/bug
    on Oct 1, 2026
  3. added a commit that references this issue on Oct 1, 2026
    c2cad8a
  4. Ariznawlll commented on Oct 2, 2026

    @Ariznawlll
    Collaborator

    测试版本

    • 分支:main
    • Commit:d50a2d475a406829f67127fef7b2ad4abc1c1d3d
    • 部署方式:本地全新单 CN mo-service,独立数据目录;MySQL 文本协议与 server-side PREPARE
    • 测试日期:2026-10-02

    原问题验证

    按 issue 原始语义在上述 commit 重新执行并重复 3 次。nullable unique index 在 FORCE INDEX 下返回全部 NULL 行,CTAS 也完整保留。

    Happy Path 覆盖

    覆盖 index scan、table scan 对照和 CTAS。

    Unhappy Path 覆盖

    覆盖重复 NULL、非 NULL 唯一值、空表;无 NULL 去重或漏行。

    白盒验证

    关联修复 #29558 已进入当前 main。同时将本 issue 的可公开观察契约固化为独立回归,避免只依赖实现细节或共享测试间接判断。

    回归测试

    测试证据

    • 精确测试版本:d50a2d475a406829f67127fef7b2ad4abc1c1d3d
    • 当前结果:nullable unique index 在 FORCE INDEX 下返回全部 NULL 行,CTAS 也完整保留。
    • 回归代码与 golden result 已提交到 MOTR #209;本地全量 14_issue_regression 中观察到的旧 GOROOT/历史 golden/并发锁冲突属于既有无关失败,以上新增用例单独重跑均稳定通过。

    测试结论

    测试通过。

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

Metadata

Metadata

Assignees

Labels

kind/bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions