Skip to content

[Bug]: NULL-first cluster keys make range filters skip all blocks across data types #26817

Closed

Description

@XuPeng-SH

Is there an existing issue for the same bug?

  • I have checked the existing issues.

Searched open and closed issues for nullable cluster keys, uninitialized zonemaps, quick-break, and zero input blocks. #26808 covers scale-dependent DECIMAL comparisons; this bug reproduces with equal-scale DECIMAL, BIGINT, and VARCHAR and is caused by unknown/uninitialized sorted metadata.

Branch Name

main

Commit ID

08770e1

Other Environment Information

  • Hardware parameters: single-node launch, CN/TN memory cache 6 GB
  • OS type: Linux
  • Others: explicit TN flush; non-partitioned ordinary tables

Actual Behavior

If a nullable CLUSTER BY key is NULL for an initial run of rows and later rows contain values, direct < and <= predicates can return zero rows. Equivalent expression predicates return the correct rows. EXPLAIN ANALYZE shows that the wrong query reads zero blocks.

The failure is type-independent:

DECIMAL(20,4) <  2000.0000       0  (expression oracle 19999)
DECIMAL(20,4) <= 2000.0000       0  (expression oracle 20000)
BIGINT <  20000                  0  (expression oracle 19999)
BIGINT <= 20000                  0  (expression oracle 20000)
VARCHAR <  '00020000'            0  (expression oracle 19999)
VARCHAR <= '00020000'            0  (expression oracle 20000)

For all three types, > returns the same 10,000 rows as its expression oracle, providing an operator-direction control.

Expected Behavior

Unknown or uninitialized zonemap metadata must fail open. An all-NULL sorted block/object cannot prove that later blocks/objects contain no values satisfying < or <=; direct predicates must return the same rows as row-level expression evaluation.

Steps to Reproduce

CREATE DATABASE IF NOT EXISTS nullable_cluster_zm_repro;
USE nullable_cluster_zm_repro;

CREATE TABLE decimal_probe(id BIGINT NOT NULL, v DECIMAL(20,4)) CLUSTER BY(v);
INSERT INTO decimal_probe
SELECT result,
       CASE WHEN result<=20000 THEN NULL
            ELSE CAST((result-20000)/10.0 AS DECIMAL(20,4)) END
FROM generate_series(1,50000) g;
SELECT mo_ctl('dn','flush','nullable_cluster_zm_repro.decimal_probe');
SELECT Sleep(2);

SELECT COUNT(*),SUM(id),SUM(v) FROM decimal_probe WHERE v<2000.0000;
-- wrong: 0, NULL, NULL
SELECT COUNT(*),SUM(id),SUM(v) FROM decimal_probe WHERE v+0<2000.0000;
-- correct: 19999, 599970000, 19999000.0000

CREATE TABLE int_probe(id BIGINT NOT NULL, v BIGINT) CLUSTER BY(v);
INSERT INTO int_probe
SELECT result,CASE WHEN result<=20000 THEN NULL ELSE result-20000 END
FROM generate_series(1,50000) g;
SELECT mo_ctl('dn','flush','nullable_cluster_zm_repro.int_probe');
SELECT Sleep(2);

SELECT COUNT(*),SUM(id) FROM int_probe WHERE v<20000;
-- wrong: 0, NULL
SELECT COUNT(*),SUM(id) FROM int_probe WHERE v+0<20000;
-- correct: 19999, 599970000

CREATE TABLE varchar_probe(id BIGINT NOT NULL, v VARCHAR(16)) CLUSTER BY(v);
INSERT INTO varchar_probe
SELECT result,
       CASE WHEN result<=20000 THEN NULL
            ELSE LPAD(CAST(result-20000 AS VARCHAR),8,'0') END
FROM generate_series(1,50000) g;
SELECT mo_ctl('dn','flush','nullable_cluster_zm_repro.varchar_probe');
SELECT Sleep(2);

SELECT COUNT(*),SUM(id) FROM varchar_probe WHERE v<'00020000';
-- wrong: 0, NULL
SELECT COUNT(*),SUM(id) FROM varchar_probe WHERE CONCAT('',v)<'00020000';
-- correct: 19999, 599970000

EXPLAIN ANALYZE SELECT COUNT(*) FROM int_probe WHERE v<20000;
-- Table Scan: inputBlocks=0, inputRows=0, outputRows=0

Additional information

White-box root cause is in the sorted readutil filter contract. AnyLTByValue/AnyLEByValue return false for an uninitialized zonemap. </<= block-filter paths turn that false into quickBreak=true, and the executor stops scanning all following blocks. The same unknown-as-false state can also affect object-level pruning.

The fix needs a tri-state contract: known match, known non-match, and unknown. Unknown/uninitialized metadata must not create a quick-break or seek exclusion proof. Regression coverage should include multiple types, nullable NULL-first persisted data, object/block filters, sorted seek/break, and compound predicates.

Activity

  1. added
    kind/bug
    on Aug 9, 2026
  2. XuPeng-SH commented on Aug 9, 2026

    @XuPeng-SH
    ContributorAuthor

    Additional operator-shape controls on the persisted nullable BIGINT table:

    v < 20000 AND id > 0          0   (oracle 19999)
    v < 20000 OR id = -1      19999   (oracle 19999)
    20000 > v                 19999   (oracle 19999)
    v BETWEEN 10000 AND 20000 10001   (oracle 10001)
    v IS NULL                 20000
    

    The failure follows the canonical sorted </<= fast-filter path and propagates through AND. OR and constant-on-left forms fall back to paths that do not incorrectly stop at the unknown NULL-first metadata; BETWEEN and IS NULL are controls on this baseline. This is useful for a before-fix test that proves the quick-break path—not row evaluation—is responsible.

  3. XuPeng-SH commented on Aug 9, 2026

    @XuPeng-SH
    ContributorAuthor

    Restart durability check: after restarting the single-node service, all three persisted probes still reproduce identically: DECIMAL 0 vs 19999, BIGINT 0 vs 19999, VARCHAR 0 vs 19999 for direct < versus expression oracle. This rules out transient metadata/cache state; pprof remained healthy after restart.

  4. XuPeng-SH commented on Aug 9, 2026

    @XuPeng-SH
    ContributorAuthor

    Fix is now in draft PR #26815 at follow-up commit 7a5c1cc0e6. Independent candidate validation on the persisted DECIMAL/BIGINT/VARCHAR NULL-first tables passed before and after restart: each strict < route now equals its non-pruning oracle at 19,999 rows / checksum 599,970,000; DECIMAL <= is 20,000 on both routes. The implementation also tests an unknown middle block that binary search would not sample and conservatively returns seek position 0.

  5. Ariznawlll commented on Aug 12, 2026

    @Ariznawlll
    Collaborator

    测试版本

    • 分支:main
    • commit:ca845354162ca2b01feb391d7b5b6e029c609420
    • 部署方式:本地单机 mo-service,1 CN / 1 TN / 1 Log service;显式 TN flush,MySQL text protocol
    • 测试日期:2026-08-12

    原问题验证

    在最新 main 上分别创建 DECIMAL、BIGINT、VARCHAR nullable CLUSTER BY 表。每种类型先写入 20000 行全 NULL 并 flush,再写入 30000 行非 NULL 并再次 flush。直接 < 查询与禁用直接 zonemap 的表达式 oracle 均返回 19999 行,连续 3 轮一致。

    Happy Path 覆盖

    1. 三种数据类型的 direct/oracle count 和 SUM(id) 完全一致。
    2. <= 返回 20000、> 返回 10000、IS NULL 返回 20000。
    3. 反向常量比较和附加 id>0 的 compound predicate 均返回 19999,连接最终可继续执行。

    Unhappy Path 覆盖

    本问题由合法 NULL-heavy 持久化数据触发,不涉及应拒绝输入。风险路径已覆盖“前置对象全 NULL、后续对象有值”的 unknown zonemap、对象/块边界和 sorted quick-break;未知元数据未造成漏读、panic、重启或数据变化。

    白盒验证

    未做故障注入;通过分阶段 TN flush 从公开 SQL 强制形成原问题需要的持久化对象边界。本轮以 direct/oracle 等价性为准。

    回归测试

    已加入 motr PR #106 的 storage/index 用例,该 flush case 已设为 exclusive,连续 3 轮稳定通过。

    测试证据

    motr PR #106;result 记录三种类型的 direct/oracle、方向、复合条件和 NULL 计数。

    测试结论

    测试通过。

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