Skip to content

[Compatibility]: SUBSTRING, LEFT, and RIGHT do not narrow bounded VARCHAR result metadata #28901

Closed

Description

@Ariznawlll

Problem

SUBSTRING, SUBSTR, MID, LEFT, and RIGHT do not narrow the declared width of a bounded character result when the requested length is a constant. Values are sliced correctly, but View and CTAS schemas retain the full source width.

Reproduction

drop database if exists substring_width_repro;
create database substring_width_repro;
use substring_width_repro;

create table src(id int primary key, wide varchar(5000));
insert into src values (1,repeat('Aé中',1000)),(2,'searchable'),(3,null);

create view v as
select id,
       substring(wide,1,100) sub100,
       substr(wide,2,3) sub3,
       mid(wide,2,3) mid3,
       left(wide,3) left3,
       right(wide,3) right3
from src;
create table ctas as select * from v;

select table_name,column_name,column_type,character_maximum_length
from information_schema.columns
where table_schema=database() and table_name in ('v','ctas')
order by table_name,ordinal_position;

MatrixOne result

All ten derived View/CTAS columns are declared as VARCHAR(5000), regardless of whether the explicit result limit is 100 or 3 characters.

The returned values are correctly limited, including the multibyte input: SUBSTRING(wide,1,100) contains exactly 100 characters, SUBSTR(wide,2,3) / MID(wide,2,3) return é中A, and LEFT / RIGHT each return at most 3 characters.

MySQL 8.0.45 result

  • SUBSTRING(wide,1,100) is VARCHAR(100);
  • SUBSTR(wide,2,3), MID(wide,2,3), LEFT(wide,3), and RIGHT(wide,3) are VARCHAR(3);
  • View and CTAS expose the same bounded metadata.

Controls and scope

  • SUBSTRING(VARBINARY(128),1,2) is VARBINARY(2) in both systems, so the issue is confined to the character-string overload in this reproduction.
  • NULL, short input, long multibyte input, View, CTAS, and secondary indexes over the derived columns are covered.
  • Three independent executions produced identical results on each database.

Incorrect declared width affects schema introspection, generated client models, storage sizing, and downstream DDL based on CTAS/View metadata even though the scalar bytes are correct.

Code location

The affected character overloads in pkg/sql/plan/function/list_builtIn.go derive the return type only from argument 0 via derivedStringReturnType(parameters, 0, ...); the constant length argument does not participate in the bound. Binary-string planning already produces the expected narrowed width and is a useful control.

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. changed the title [-][Compatibility]: SUBSTRING, SUBSTR, and MID do not narrow bounded VARCHAR result metadata[/-] [+][Compatibility]: SUBSTRING, LEFT, and RIGHT do not narrow bounded VARCHAR result metadata[/+] on Sep 15, 2026
  4. self-assigned this
    on Sep 15, 2026
  5. added a commit that references this issue on Sep 17, 2026
    f6f8c0c
  6. Ariznawlll commented on Sep 17, 2026

    @Ariznawlll
    CollaboratorAuthor

    当前 main 复测未通过,转回 @XuPeng-SH 继续处理。

    验证环境:本地单机 MatrixOne,由远端 main 0370bb4d6b118da29e164c54aa34ee010ed897bc 全新构建;PR #28941 的 merge commit f6f8c0c885f999cbde3b0d394194b0a4a74176ae 已在测试提交历史中。独立执行 3/3,结果一致。

    复现:源列为 VARCHAR(5000);在 View 与 CTAS 中查询 SUBSTRING(wide,1,100)、SUBSTR/MID(wide,2,3)、LEFT(wide,3)、RIGHT(wide,3)。十个派生列全部仍公开为长度 5000,未使用常量长度参数收窄。多字节值控制正确:SUBSTRING 返回 100 个字符,三个字符的结果也正确,说明失败集中在可见 schema 元数据。

    期望:SUBSTRING(...,100) 为 VARCHAR(100),其余常量长度为 3 的表达式为 VARCHAR(3),并在 View/CTAS 一致。当前验收未通过,issue 保持 Open。

  7. added 2 commits that reference this issue on Sep 17, 2026
    385154b
    fe9c331
  8. added 2 commits that reference this issue on Sep 19, 2026
    aeee7db
    d8ddce9
  9. Ariznawlll commented on Sep 20, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:d8ddce92b1c5c172111b50aefe6b6b200b2589cb
    • 日期:2026-09-20

    原问题验证

    验证 SUBSTRING、LEFT、RIGHT 的 CTAS 元数据。源列 VARCHAR(100) 经三个函数后,结果元数据分别为 VARCHAR(100)、VARCHAR(3)、VARCHAR(3),不再错误保留 VARCHAR(5000)。连续执行 3 次通过。

    覆盖范围

    1. SUBSTRING、LEFT、RIGHT。
    2. CTAS 后的列定义检查与结果值检查。
    3. ASCII 与中文多字节字符输入。

    边界与异常路径

    1. 截取长度位于列宽边界时元数据仍正确。
    2. 多字节字符串截取不破坏字符边界。

    自动化回归

    • motr#193 新增 issue_28901_slice_metadata.test。
    • MOTR 目标集连续运行 3 轮,共 33/33 成功。

    测试结论

    测试通过。

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