Repository navigation
Description
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)isVARCHAR(100);SUBSTR(wide,2,3),MID(wide,2,3),LEFT(wide,3), andRIGHT(wide,3)areVARCHAR(3);- View and CTAS expose the same bounded metadata.
Controls and scope
SUBSTRING(VARBINARY(128),1,2)isVARBINARY(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
maincommit0c3a04f390adaf6281fd592a49778ea5b1155e67 - MySQL control: 8.0.45
- Local single-node MatrixOne deployment
Activity
[-][Compatibility]: SUBSTRING, SUBSTR, and MID do not narrow bounded VARCHAR result metadata[/-][+][Compatibility]: SUBSTRING, LEFT, and RIGHT do not narrow bounded VARCHAR result metadata[/+]fix: preserve expression result metadata and coercion contracts (#28941)
Ariznawlll commentedon Sep 17, 2026
当前 main 复测未通过,转回 @XuPeng-SH 继续处理。
验证环境:本地单机 MatrixOne,由远端 main
0370bb4d6b118da29e164c54aa34ee010ed897bc全新构建;PR #28941 的 merge commitf6f8c0c885f999cbde3b0d394194b0a4a74176ae已在测试提交历史中。独立执行 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。fix: preserve expression result metadata and coercion contracts (matr…
fix: preserve expression result metadata and coercion contracts (matr…
fix: close expression result contract regressions (#29075)
fix(plan): narrow constant character slices across metadata consumers (…
Ariznawlll commentedon Sep 20, 2026
测试版本
maind8ddce92b1c5c172111b50aefe6b6b200b2589cb原问题验证
验证
SUBSTRING、LEFT、RIGHT的 CTAS 元数据。源列VARCHAR(100)经三个函数后,结果元数据分别为VARCHAR(100)、VARCHAR(3)、VARCHAR(3),不再错误保留VARCHAR(5000)。连续执行 3 次通过。覆盖范围
SUBSTRING、LEFT、RIGHT。边界与异常路径
自动化回归
issue_28901_slice_metadata.test。33/33成功。测试结论
测试通过。