【TiDB 使用环境】测试
【TiDB 版本】7.1.6
【操作系统】centos7
【部署方式】机器部署
【集群数据量】1
【集群节点数】3
【问题复现路径】
-- 表结构
CREATE TABLE `sbtest2` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`k` int(11) NOT NULL DEFAULT '0',
`c` char(120) NOT NULL DEFAULT '',
`pad` char(60) NOT NULL DEFAULT '',
PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */,
KEY `k_2` (`k`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin AUTO_INCREMENT=537356
-- ddl
alter table sbtest2 add column comment_test varchar(512) default '' comment "黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱";
会执行成功,但实际comment会截断
问题:
查看column表字段定义,发现COLUMN_COMMENT定义是 varchar(1024),理论是1024字符。
https://docs.pingcap.com/zh/tidb/stable/information-schema-columns/#columns
但是实际comment = 510,length=1530 >1024。
请问:是不是bug,COLUMN_COMMENT记录的是字节长度(预期应该是字符长度)。
1 个赞
sonomx
(Ti D Ber Ny Pjt054)
2025 年11 月 5 日 07:55
2
正常吧,comment一般也用不到太长
ErrorCode = 1629, SQLState = HY000, Details = Comment for field is too long (max = 2048)
是的,正常情况业务不需要这么长,就是发现这个问题了,怀疑是字符、字节 搞混了。
最后截断的汉字有多少个? 无论是表注释还是字段注释,其最大长度默认被限制为 1024 个字符 ,跟columns表定义关系不大,这个就是限制
341+1个乱码
黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄河大合唱黄�
AN_12
(Xiaoqiao)
2025 年11 月 7 日 01:11
15
1 个赞
异乡的大人
(Ti D Ber 2 Qs S2z Ws)
2025 年11 月 7 日 01:22
16
值得关注的一个问题,不过,在实际使用时,没有遇到这个情况
maxLen := MaxCommentLength
// The maximum length of table comment in MySQL 5.7 is 2048
// Other comment is 1024
switch errTooLongComment {
case dbterror.ErrTooLongTableComment:
maxLen *= 2
case dbterror.ErrTooLongFieldComment, dbterror.ErrTooLongIndexComment, dbterror.ErrTooLongTablePartitionComment:
default:
// add more types of terror.Error if need
}
if len(*comment) > maxLen {
err := errTooLongComment.GenWithStackByArgs(name, maxLen)
if sqlMode.HasStrictMode() {
// may be treated like an error.
return "", err
}
ec.AppendWarning(err)
*comment = (*comment)[:maxLen]
}
return *comment, nil
}
看代码是计算的 len(comment),是按着字节截断的。
实际按字节、字符计算都没关系,但是要与官网介绍的varchar一致(charset=utf8mb4)
mysql> show create table information_schema.columns\G
*************************** 1. row ***************************
Table: COLUMNS
Create Table: CREATE TABLE `COLUMNS` (
`TABLE_CATALOG` varchar(512) DEFAULT NULL,
`TABLE_SCHEMA` varchar(64) DEFAULT NULL,
`TABLE_NAME` varchar(64) DEFAULT NULL,
`COLUMN_NAME` varchar(64) DEFAULT NULL,
`ORDINAL_POSITION` bigint(64) DEFAULT NULL,
`COLUMN_DEFAULT` text DEFAULT NULL,
`IS_NULLABLE` varchar(3) DEFAULT NULL,
`DATA_TYPE` varchar(64) DEFAULT NULL,
`CHARACTER_MAXIMUM_LENGTH` bigint(21) DEFAULT NULL,
`CHARACTER_OCTET_LENGTH` bigint(21) DEFAULT NULL,
`NUMERIC_PRECISION` bigint(21) DEFAULT NULL,
`NUMERIC_SCALE` bigint(21) DEFAULT NULL,
`DATETIME_PRECISION` bigint(21) DEFAULT NULL,
`CHARACTER_SET_NAME` varchar(32) DEFAULT NULL,
`COLLATION_NAME` varchar(32) DEFAULT NULL,
`COLUMN_TYPE` text DEFAULT NULL,
`COLUMN_KEY` varchar(3) DEFAULT NULL,
`EXTRA` varchar(30) DEFAULT NULL,
`PRIVILEGES` varchar(80) DEFAULT NULL,
`COLUMN_COMMENT` varchar(1024) DEFAULT NULL, -- 字段类型varchar(1024)
`GENERATION_EXPRESSION` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
https://docs.pingcap.com/zh/tidb/stable/information-schema-columns/#columns
另,看mysql5.7介绍,comment长度是1024字符,是不是计算有问题
maxLen := MaxCommentLength
// The maximum length of table comment in MySQL 5.7 is 2048
// Other comment is 1024
switch errTooLongComment {
case dbterror.ErrTooLongTableComment:
maxLen *= 2
https://dev.mysql.com/doc/refman/5.7/en/create-table.html#create-table-column-comments
A comment for a column can be specified with the COMMENT option, up to 1024 characters long. The comment is displayed by the SHOW CREATE TABLE and SHOW FULL COLUMNS statements. It is also shown in the COLUMN_COMMENT column of the Information Schema COLUMNS table.