Skip to content

[Bug]: prepared ELT truncates fractional DECIMAL indexes unlike literal and column evaluation #28908

Closed

Description

@Ariznawlll

Problem

PreparedStatement execution of ELT truncates fractional DECIMAL/string indexes toward zero, while the same literal or DECIMAL column is rounded to the nearest integer. The chosen array element therefore depends on the execution path.

Reproduction

drop database if exists elt_prepared_repro;
create database elt_prepared_repro;
use elt_prepared_repro;

select elt(1.4,'a','b','c'), elt(1.5,'a','b','c'), elt(1.6,'a','b','c'),
       elt(2.4,'a','b','c'), elt(2.5,'a','b','c'), elt(2.6,'a','b','c');

create table src(id int primary key,idx decimal(4,2));
insert into src values (1,1.40),(2,1.50),(3,1.60),(4,2.40),(5,2.50),(6,2.60);
select id,idx,elt(idx,'a','b','c') from src order by id;

prepare p from 'select elt(?,''a'',''b'',''c'')';
set @i=1.5; execute p using @i;
set @i=1.6; execute p using @i;
set @i=2.5; execute p using @i;
set @i=2.6; execute p using @i;
set @i=cast(1.5 as decimal(4,2)); execute p using @i;
set @i='1.5'; execute p using @i;

MatrixOne result

  • Direct literals and DECIMAL columns choose b, b, c, and c for indexes 1.5, 1.6, 2.5, and 2.6.
  • The prepared executions choose a, a, b, and b for the same values.
  • Explicit DECIMAL parameter 1.5 and string parameter '1.5' also choose a.

Thus values from [1.0,2.0) select item 1 and values from [2.0,3.0) select item 2 only on the prepared path.

MySQL 8.0.45 result

DECIMAL literals, DECIMAL columns, ordinary DECIMAL user variables, explicit DECIMAL parameters, and numeric strings use the same nearest-integer conversion: 1.5/1.6 choose b; 2.5/2.6 choose c.

An explicit DOUBLE parameter is a type-sensitive control and follows MySQL's DOUBLE-to-integer behavior; it is not used as the expected result for the DECIMAL cases above.

Stability and scope

  • 1.4, 1.5, 1.6, 2.4, 2.5, 2.6, NULL, explicit DECIMAL, explicit DOUBLE, numeric string, and integer string are covered.
  • A second prepared statement executes the types in reverse order; the MatrixOne result remains the same, so this is not first-execution cache poisoning.
  • Three independent executions produced identical output on each database.

Code location

eltCheck in pkg/sql/plan/function/func_binary.go resolves noninteger indexes by inserting an INT64 cast. The prepared-parameter conversion does not preserve the DECIMAL/string integer-conversion semantics used by literals and columns. This is the focused code-path hypothesis; the externally visible bug is the result difference above.

Environment

  • MatrixOne: official main commit 0c3a04f390adaf6281fd592a49778ea5b1155e67
  • MySQL control: 8.0.45
  • Local single-node MatrixOne deployment

Activity

  1. added this to the MO-202609 milestone on Sep 15, 2026
  2. added theissue type on Sep 15, 2026
  3. self-assigned this
    on Sep 15, 2026
  4. Ariznawlll commented on Sep 16, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:8a4c84f4516d5098de9eef50b8afecc7524f37a2
    • 部署方式:1 Log Service / 1 DN / 2 CN,MySQL 协议
    • 测试日期:2026-09-16

    原问题验证

    验证字面量、DECIMAL 列和 Prepared ELT 的小数索引语义。1.5/1.6 均选择第 2 个元素,2.5/2.6 均选择第 3 个元素;Prepared 与非 Prepared 结果一致。

    Happy Path 覆盖

    1. 直接调用 ELT(1.5/2.5, ...) 结果为 b/c。
    2. Prepared statement 重复执行并变更参数,结果仍为 b/c。
    3. 3/3 次独立执行一致。

    Unhappy Path 覆盖

    不适用;本 issue 验证的是合法 DECIMAL/Prepared 参数的兼容性,没有引入非法索引作为替代预期。

    白盒验证

    不适用;以公开 SQL 结果一致性为准。

    回归测试

    已加入 motr PR #181 的 issue_28908_28904_compat.test/.result。

    测试证据

    最新 main 集群版本为 commit-8a4c84f;3/3 次 Prepared 与字面量结果一致。

    测试结论

    测试通过。

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

Metadata

Metadata

Assignees

Labels

area/compatibilitykind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions