Skip to content

[Performance]: DOUBLE comparison on DECIMAL column loses block filtering #29510

Closed

Description

@XuPeng-SH

Bug / performance gap

A selective equality on a DECIMAL(12,2) column loses block filtering when the comparison peer is DOUBLE. The planner casts the column to DOUBLE, which scans all blocks; the same numeric value as an integer or same-scale DECIMAL literal retains the column's native domain and block filter. This occurs in ordinary SQL with an explicit DOUBLE literal as well as in a binary prepared statement with a float64 parameter, so it is a general comparison coercion/optimizer issue rather than solely a PREPARE issue.

Reproduction and measured impact

On current main with 100,000 sorted rows (d DECIMAL(12,2), values 1.00 through 100000.00):

EXPLAIN ANALYZE SELECT count(*) FROM t WHERE d=54321;
-- native d=54321.00; Block Filter Cond; 1 block / 8192 scan input rows
EXPLAIN ANALYZE SELECT count(*) FROM t WHERE d=CAST(54321 AS DOUBLE);
-- cast(d AS DOUBLE)=54321; no Block Filter Cond; 13 blocks / 100000 scan input rows; 52ms scan time locally

A Go MySQL binary prepared SELECT count(*) FROM t WHERE d=?, executed 50 times on one connection with float64(54321), took ~2.82 s locally versus ~40–50 ms for integer/text parameters under the same script. The direct EXPLAIN ANALYZE result confirms the scan difference; local timings are not a benchmark-wide claim.

Correctness constraints

A generic DOUBLE-to-DECIMAL cast would change equality for values between DECIMAL scale points (for example, 0.104 versus a 0.10 column value), and precision collisions can occur at larger magnitudes. Preserve current result semantics and warnings. A safe optimization needs a proof that the peer value maps to exactly one representable DECIMAL value under the current comparison domain; otherwise keep the existing fallback. Cover SQL literals, prepared binary float, decimal scale/width, rounding boundaries, NULL and out-of-range values.

Found while challenging #29462/#29505 pushdown behavior; independent from #29506's signed BIGINT case.

Activity

  1. self-assigned this
    on Sep 29, 2026
  2. added
    severity/s0
    on Sep 29, 2026
  3. added a commit that references this issue on Sep 30, 2026
    3d22228
  4. Ariznawlll commented on Oct 2, 2026

    @Ariznawlll
    Collaborator

    测试版本

    • 分支:main
    • commit:d59cc02ef6ca68f340c9570e56f7eb71d90e42e7
    • 部署方式:本地单机 one-CN,默认配置
    • 测试日期:2026-10-02

    原问题验证

    构造 100003 行的 DECIMAL(12,2) 数据,验证 WHERE d = ? 的 binary prepared statement。

    • 可安全映射到唯一 DECIMAL 值的 54321:返回 1 行,并启用选择性 block filter;当前 fixture 的目标值位于边界,扫描 2/14 blocks
    • 无法唯一映射的 DOUBLE 0.104:返回 0 行,保留原有保守路径,扫描 14/14 blocks
    • NULL:返回 0 行

    说明修复只在能证明 DOUBLE 对应唯一 DECIMAL 值时使用原生 DECIMAL 过滤;小数刻度之间的值不会被错误下推。

    正常路径覆盖

    同一 prepared statement 连续复用,分别绑定安全的 int64、完整 numeric text、float64,均返回 1 行。每类执行 30 次,平均耗时:

    • int64:0.705 ms
    • text:0.448 ms
    • float64:0.802 ms

    边界与保守路径覆盖

    1. 0.104 不可在 DECIMAL(12,2) 中精确表示,结果为 0,完整扫描 14 blocks,30 次平均 50.69 ms。
    2. NULL 绑定返回 0。
    3. 回归用例同时覆盖大整数精度碰撞、越界值及安全/不安全参数交替复用,未发现错误裁剪或结果变化。

    白盒与 BVT 验证

    MatrixOne 现有用例 test/distributed/cases/prepare/issue_29510_decimal_float_filter.sql 连续执行 3 轮均通过。

    回归测试

    已加入 MOTR PR #209,commit 3b4d80060724d3f19c672d9f039c5df951d63c81,用例:
    suites/14_issue_regression/t/issue_29507_29510_29511_29512_filter_pruning.test

    • 新用例本地连续 10 轮:640/640 statements 通过
    • 完整 14_issue_regression 并发套件中该新增用例通过
    • 离线契约 CI 的 Python 3.10 / 3.12 / 3.13 jobs 全部通过

    本问题是值域证明和 block-filter 生成逻辑,安全与保守分支均可由确定性 SQL/计划断言覆盖,不依赖数据规模阈值,因此不另加 big-data。

    测试结论

    最新 main 中,安全 DOUBLE 参数已恢复 DECIMAL 原生过滤;不可精确表示的参数仍保留保守扫描,查询结果与原语义一致,回归已补充。

    测试通过。

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

Metadata

Metadata

Assignees

Labels

severity/s0

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions