Skip to content

[Bug]: Higher-scale DECIMAL lower bounds return wrong rows through composite secondary indexes #26801

Closed

Description

@XuPeng-SH

MatrixOne version

bdcada5438 (2026-08-09), local single-node deployment, 6 GiB memory cache.

What happened?

A range predicate whose DECIMAL lower-bound literal has a larger scale (only trailing zeroes) returns wrong rows when a composite secondary index is selected. Equality works, the upper bound works, and the same query is correct with an equal-scale literal or without the secondary index.

The physical index table is complete: it has 10,000 rows for 10,000 base rows, 10,000 distinct encoded index keys, and 10,000 distinct primary-key mappings.

Reproduction

CREATE DATABASE decimal_index_repro;
USE decimal_index_repro;

CREATE TABLE decimal_scale_probe (
  tenant_id INT NOT NULL,
  id BIGINT NOT NULL,
  price DECIMAL(10,2) NOT NULL,
  PRIMARY KEY (tenant_id,id),
  KEY idx_price_owner (price,tenant_id,id)
);

CREATE TABLE decimal_scale_scan (
  tenant_id INT NOT NULL,
  id BIGINT NOT NULL,
  price DECIMAL(10,2) NOT NULL,
  PRIMARY KEY (tenant_id,id)
);

INSERT INTO decimal_scale_probe
SELECT 1 + result % 8, result, CAST(result / 100.0 AS DECIMAL(10,2))
FROM generate_series(1,10000) g;
INSERT INTO decimal_scale_scan SELECT * FROM decimal_scale_probe;

SELECT COUNT(*), SUM(price)
FROM decimal_scale_probe FORCE INDEX(idx_price_owner)
WHERE price BETWEEN 10.250000 AND 15.750000;
-- wrong: 0, 0.00

SELECT COUNT(*), SUM(price)
FROM decimal_scale_probe IGNORE INDEX(idx_price_owner)
WHERE price BETWEEN 10.250000 AND 15.750000;
-- correct: 551, 7163.00

SELECT COUNT(*), SUM(price)
FROM decimal_scale_scan
WHERE price BETWEEN 10.250000 AND 15.750000;
-- correct: 551, 7163.00

SELECT COUNT(*) FROM decimal_scale_probe FORCE INDEX(idx_price_owner)
WHERE price > 10.250000 AND price < 15.750000;
-- wrong: 8975 (scan returns 549)

SELECT COUNT(*) FROM decimal_scale_probe FORCE INDEX(idx_price_owner)
WHERE price = 10.250000;
-- correct: 1

SELECT COUNT(*) FROM decimal_scale_probe FORCE INDEX(idx_price_owner)
WHERE price <= 15.750000;
-- correct: 1575

SELECT COUNT(*) FROM decimal_scale_probe FORCE INDEX(idx_price_owner)
WHERE price BETWEEN 10.25 AND 15.75;
-- correct: 551

The optimizer selects the index even without FORCE INDEX, and the default query is also wrong.

Expected behavior

DECIMAL literals with extra trailing-zero scale should compare identically to literals represented at the column scale. Indexed and non-indexed execution must return the same 551 rows (549 for the open interval).

Initial root-cause analysis

The ordinary comparison binder already recognizes harmless trailing-zero scale differences, but the composite-index range rewrite in replaceRangePairCondition serializes copied range constants directly. It does not first normalize each bound to the indexed part's DECIMAL type. The encoded lower key therefore differs from the DECIMAL(10,2) encoding stored in __mo_index_idx_col.

This is distinct from #25348: that issue involved empty/incomplete hidden index tables. Here the hidden table and primary mappings are complete, and changing only the literal scale changes the result.

Additional validation requested

Please cover closed/open range pairs, single-sided lower and upper bounds, equality controls, default/FORCE/IGNORE plans, composite primary keys, and direct physical-index count/distinct-mapping checks after indexed DML.

Activity

  1. added
    kind/bug
    on Aug 8, 2026
  2. self-assigned this
    on Aug 8, 2026
  3. added
    severity/s0
    on Aug 8, 2026
  4. XuPeng-SH commented on Aug 8, 2026

    @XuPeng-SH
    ContributorAuthor

    Fix is available in PR #26806. Higher-scale trailing-zero bounds are losslessly normalized before composite-key serialization; rounding-sensitive bounds bypass the index rewrite. Full planner tests and the isolated range BVT pass.

  5. XuPeng-SH commented on Aug 9, 2026

    @XuPeng-SH
    ContributorAuthor

    Round 30 found an index-order interaction between this DECIMAL range-key defect and #26807's VARBINARY prefix defect on a 50,005-row table with composite PK (tenant_id,id) and both index orders:

    KEY idx_amount_b_pk(amount,b,id,tenant_id),
    KEY idx_b_amount_pk(b,amount,status,id,tenant_id)

    After an indexed UPDATE that changes both DECIMAL values and binary lengths/NUL prefixes, all three hidden tables remain exactly one-to-one (50005/50005/50005) and survive restart.

    amount BETWEEN 1000.000000 AND 2000.000000
      default / FORCE(amount,b) / scan = 10001 / 10001 / 10001
    
    same range AND b IN (empty, 0x00)
      FORCE(amount,b,PK) = 0       -- wrong; scan = 121
      FORCE(b,amount,PK) = 242     -- wrong in the opposite direction; scan = 121
      default = 121                -- optimizer chose the correct table scan
    

    With (amount,b,PK), the higher-scale DECIMAL range-key rewrite prunes the candidate set to zero before the binary predicate can help. Reversing the index to (b,amount,PK) avoids that leading DECIMAL failure but exposes #26807's prefix matching and doubles the result.

    Please retain this as a cross-regression: fixing either leading key in isolation should not leave the alternate index ordering semantically wrong. The nearest control (the amount-only range) is important because it is currently correct on this DECIMAL(20,4) data shape.

  6. added a commit that references this issue on Aug 9, 2026
    4259618
  7. Ariznawlll commented on Aug 12, 2026

    @Ariznawlll
    Collaborator

    测试版本

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

    原问题验证

    在最新 main 上保留原始 10000 行 DECIMAL(10,2) composite secondary index 数据。高 scale 闭区间在 default/FORCE/IGNORE/shadow scan 四种路径均返回 551 行、SUM(price)=7163.00;高 scale 开区间均返回 549 行,连续 3 轮一致。

    Happy Path 覆盖

    1. 覆盖 default/FORCE/IGNORE 以及无 secondary index shadow table。
    2. 覆盖闭区间、开区间、单侧 lower/upper、等值、高 scale trailing zero。
    3. DML 前后隐藏索引均为 10000 行、10000 distinct key、10000 distinct PK mapping;indexed/shadow DML 后无差异。

    Unhappy Path 覆盖

    本问题由合法 DECIMAL bound 触发,不涉及应拒绝输入。舍入敏感边界 10.255000/15.755000 同时覆盖 open 和 BETWEEN 路径,FORCE 与 scan 均返回 550;确认不能通过错误索引重写产生漏行/多行,连接和源数据保持正常。

    白盒验证

    未做故障注入;公开 FORCE INDEX 已强制进入 composite range-key 路径,并用 IGNORE INDEX 和独立 shadow table 双重对照。

    回归测试

    已加入 motr PR #106 的 storage/index 用例,连续 3 轮稳定通过。

    测试证据

    motr PR #106;result 记录所有 range 变体、DML 前后查询和物理隐藏表完整性。

    测试结论

    测试通过。

  8. added 3 commits that reference this issue on Sep 17, 2026
    6127527
    1e7f7dd
    404221a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s0

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions