Skip to content

[Bug]: Prepared NTILE parameter fails at execution with internal type error #26867

Closed

Description

@Ariznawlll

Is there an existing issue for the same bug?

  • I searched open and closed issues and pull requests for prepared NTILE, parameter markers, and the observed internal error. No exact duplicate was found.

Branch Name

main

Commit ID

1d00cca77012a2fac20c94bbbd170579e715d217

Other Environment Information

  • MatrixOne: 8.0.30-MatrixOne-v1.3.0
  • Deployment: local mo-service, 2 CN / 1 TN / 1 Log service / Proxy
  • Both SQL PREPARE and MySQL binary-protocol prepared statements were tested

Actual Behavior

NTILE(?) prepares successfully, but executing it with a valid positive integer fails with an internal error:

ERROR 20101 (HY000): internal error: ntile bucket count must be integer type

Values 1, 2, and 5 all produced the same error. The failure reproduced 3/3 times with binary-protocol prepared statements and 3/3 times with SQL PREPARE/EXECUTE. The full matrix was rerun after main advanced from ec06a68b8 to the commit above and still reproduced 3/3.

The literal and explicitly typed parameter controls both work:

NTILE(2):                  1:1 | 2:1 | 3:2 | 4:1
NTILE(CAST(? AS SIGNED)):  1:1 | 2:1 | 3:2 | 4:1

Expected Behavior

Binding a valid positive integer through a positional parameter marker should produce the same buckets as the equivalent literal. MySQL documents a positional parameter marker as a valid form for the N argument of NTILE.

Steps to Reproduce

DROP DATABASE IF EXISTS ps_ntile;
CREATE DATABASE ps_ntile;
USE ps_ntile;

CREATE TABLE t(id INT PRIMARY KEY, g INT);
INSERT INTO t VALUES (1,1),(2,1),(3,1),(4,2);

-- Correct literal control.
SELECT id,NTILE(2) OVER(PARTITION BY g ORDER BY id)
FROM t ORDER BY id;

-- PREPARE succeeds, but EXECUTE returns internal error 20101.
PREPARE ntile_param FROM
  'SELECT id,NTILE(?) OVER(PARTITION BY g ORDER BY id) FROM t ORDER BY id';
SET @buckets=2;
EXECUTE ntile_param USING @buckets;

-- Correct typed-parameter control.
PREPARE ntile_cast FROM
  'SELECT id,NTILE(CAST(? AS SIGNED)) OVER(PARTITION BY g ORDER BY id) FROM t ORDER BY id';
EXECUTE ntile_cast USING @buckets;

The same error occurs with protocol prepared statements such as Go db.Prepare followed by stmt.Query(2).

Additional information

This appears to be another missing numeric-context propagation for a prepared window argument:

  • The NTILE function definition in pkg/sql/plan/function/list_window.go accepts one argument without constraining it to an integer type.
  • During execution, pkg/sql/colexec/window/window.go evaluates that argument and passes its vector to the NTILE aggregate executor.
  • pkg/sql/colexec/aggexec/window.go::ntileWindowExec.Fill accepts only integer vectors and emits the internal error for every other type.
  • CAST(? AS SIGNED) succeeds, isolating the failure to prepared-parameter type inference rather than the NTILE algorithm.

Related but not duplicate: #23017 / #23760 implemented NTILE; #25993 concerned parameter markers in window frame bounds; #26019 concerned prepared SUM/AVG inference.

MySQL reference: https://dev.mysql.com/doc/refman/8.0/en/window-function-descriptions.html

Activity

  1. added this to the MO-202608 milestone on Aug 9, 2026
  2. added
    kind/bug
    on Aug 9, 2026
  3. added theissue type on Aug 9, 2026
  4. added
    ai-easy
    on Aug 12, 2026
  5. Ariznawlll commented on Aug 12, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:e8ecba8662ca4c4d9c0179978425fc08505967dc
    • 部署方式:本地 1 CN / 1 TN / 1 Log,MySQL 协议端口 6001
    • 测试日期:2026-08-12

    原问题验证

    使用 SQL PREPARE 和 go-sql-driver/mysql 的真实 binary prepare(interpolateParams=false,复用同一连接和同一 statement)执行 NTILE(?)。合法参数 1 / 2 / 5 连续 3 轮均成功,分区内 bucket 结果与 literal NTILE 对照完全一致,未再出现 ntile bucket count must be integer type。

    Happy Path 覆盖

    1. 覆盖 3 个分区,行数分别为 5/4/7,并覆盖不能整除 bucket 数的分配边界;NTILE(1/2/5) 结果均正确。
    2. 字符串参数 "2" 按支持的数字转换规则成功执行;随后同一 statement 再绑定整数,结果仍正确。
    3. literal NTILE、SQL PREPARE 和 binary prepare 三条路径结果一致;每次变更参数均使用当前执行值。

    Unhappy Path 覆盖

    1. 0、-1 被拒绝为 bucket 数必须为正;NULL 被拒绝为不能为 NULL。
    2. 2.5 和 bad 被拒绝为无效整数值;同时核对稳定的错误 cause,避免把无关 internal error 当作通过。
    3. 每次错误后立即复用同一 statement 执行合法整数,均可恢复;源表 count/sum 保持不变,无部分写入、状态污染、panic、hang 或残留测试库。

    白盒验证

    本轮验证的是公开 SQL 与 binary protocol 行为,不需要故障注入;修复侧的类型推断/执行器测试由关联修复覆盖。

    回归测试

    已在 motr PR #107 增加 14_issue_regression.issue_26867_prepared_ntile,使用真实 binary prepare,并包含合法参数、转换边界、全部无效输入和错误后同 handle 恢复。定向 --repeat 3 为 3/3,通过;与 #26797、#26892 并行复跑总计 9/9,通过。

    测试证据

    • 回归代码与稳定输出:motr PR #107
    • 本地定向回归:3/3;合法参数结果与 literal oracle 一致;所有非法参数均命中预期错误类别与 cause,且错误后恢复断言通过。

    测试结论

    测试通过。

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

Metadata

Metadata

Assignees

Labels

ai-easykind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions