Skip to content

[Bug]: Prepared EXPLAIN ANALYZE skips SELECT parameter narrowing and profiles a full scan #29553

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, CPU build, Go 1.26.4. Isolated two-CN launch; SQL protocol through port 26001. No GPU. 24,000 rows, three flushed blocks.

Actual Behavior

Preparing EXPLAIN ANALYZE SELECT ... WHERE id=? changes the prepared comparison domain relative to preparing its underlying SELECT. An INT key and a safely representable numeric SQL user variable get a column-side BIGINT cast and lose block pruning only in the prepared EXPLAIN path.

Normal prepared SELECT via EXPLAIN FORCE EXECUTE:

Filter Cond: (t.id = cast(cast('16001' AS BIGINT) AS INT))
Block Filter Cond: (t.id = cast(cast('16001' AS BIGINT) AS INT))

Prepared EXPLAIN ANALYZE with the same integer value:

inputBlocks=3 inputRows=24000 outputRows=1
Filter Cond: (cast(t.id AS BIGINT) = cast('16001' AS BIGINT))
-- HINT: Cast expression may prevent index usage

Nearest control: changing only SET @p=16001 to SET @p='16001' on that same prepared EXPLAIN handle produces native t.id=16001, a Block Filter Cond, and inputBlocks=1 inputRows=1.

Expected Behavior

EXPLAIN should describe/profile the same prepared SELECT parameter semantics and optimizations. Wrapping a SELECT in EXPLAIN should not silently disable a proven-safe parameter conversion or make profiling scan the whole table.

Steps to Reproduce

CREATE DATABASE qa_explain;
USE qa_explain;
CREATE TABLE t(id INT PRIMARY KEY);
INSERT INTO t SELECT result FROM generate_series(1,24000) g;
SELECT mo_ctl('dn','flush','qa_explain.t');
PREPARE plain FROM 'SELECT sum(id) FROM t WHERE id=?';
SET @p=16001;
EXPLAIN FORCE EXECUTE plain USING @p;
EXECUTE plain USING @p; -- 16001
PREPARE prof FROM 'EXPLAIN ANALYZE SELECT sum(id) FROM t WHERE id=?';
EXECUTE prof USING @p; -- full scan, column widened
SET @p='16001';
EXECUTE prof USING @p; -- native filter, one block / one scan input row
SET @p=1;
EXECUTE prof USING @p; -- full scan again

Observed on a table with additional non-key columns; profiling only reads the key. Reproduction requires no large dataset or latency threshold.

Additional information

Root cause: pkg/sql/plan/prepared_binding.go:750 initializes selectStatement from stmt.GetQueryType()==tree.QueryTypeDQL. ExplainAnalyze.GetQueryType() in pkg/sql/parsers/tree/explain.go:165 returns QueryTypeOth. The safe wider-integer parameter branch at prepared_binding.go:509 is guarded by state.selectStatement, so the EXPLAIN wrapper suppresses a transformation admitted for the underlying SELECT. Text bindings enter a different unguarded proof branch, explaining the control.

Reuse existing statement/comparison ownership and account for the explained SELECT; do not globally mark all EXPLAIN wrappers as SELECT, especially explained DML. Check other SELECT-only prepared conversions and both SQL/binary protocol diagnostics.

Suggested priority: P2. This is a current-main plan/profiling fidelity defect, not evidence that ordinary integer-bound SELECT still full-scans. #29511 tracks ordinary wider-integer comparisons; the underlying SELECT here already has the native filter. #29509 tracks a separate snapshot-binding failure in EXPLAIN ANALYZE FORCE EXECUTE; this supported PREPARE-of-EXPLAIN path succeeds but executes a different plan. Local evidence: round2/prepared-plan.log and round2/explain-controls.json under /mnt/nvme/main-qa-20261001.

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 2, 2026
    7a169dd
  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 次。INT prepared EXPLAIN ANALYZE 显示 inputBlocks=1、inputRows=1 和 native INT block filter。

    Happy Path 覆盖

    覆盖等值命中、不命中及多次整数重绑。

    Unhappy Path 覆盖

    覆盖 NULL、文本/浮点来源和越界;不能安全转换时不错误下推。

    白盒验证

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

    回归测试

    测试证据

    • 精确测试版本:d50a2d475a406829f67127fef7b2ad4abc1c1d3d
    • 当前结果:INT prepared EXPLAIN ANALYZE 显示 inputBlocks=1、inputRows=1 和 native INT block filter。
    • 回归代码与 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