Bug #117736 varchar length prefix plus data description is not correct
Submitted: 18 Mar 9:47 Modified: 19 Mar 6:00
Reporter: Guisen Yang Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: Documentation Severity:S3 (Non-critical)
Version:8.0 OS:Any
Assigned to: CPU Architecture:Any

[18 Mar 9:47] Guisen Yang
Description:
Regarding the prefix plus data of varchar(https://dev.mysql.com/doc/refman/8.0/en/char.html) in the document: 

> In contrast to CHAR, VARCHAR values are stored as a 1-byte or 2-byte length prefix plus data. The length prefix indicates the number of bytes in the value. A column uses one length byte if values require no more than 255 bytes, two length bytes if values may require more than 255 bytes.

There is data in the following table:
create table t20250318_varchar(c1 varchar(300) primary key);
insert into t20250318_varchar values('1234567890');

parse IBD file,  VARCHAR values are stored as a 1-byte length prefix plus data. (10<128)

example:
>>> import struct
>>> f = open('/data/db1/t20250318_varchar.ibd','rb')
>>> f.seek(4*16384,0)
65536
>>> data = f.read(16384)
>>> offset = struct.unpack('>h',data[97:99])[0] + 99
>>> data[offset-5-2:offset]
b'm\n\x00\x00\x10\xff\xf2'
>>> data[offset:offset+10]
b'1234567890'
>>> struct.unpack('>B',b'\n')[0]
10

How to repeat:
-- SQL
create table t20250318_varchar(c1 varchar(300) primary key);
insert into t20250318_varchar values('1234567890');

-- python
import struct
f = open('/data/db1/t20250318_varchar.ibd','rb')
f.seek(4*16384,0)
data = f.read(16384)
offset = struct.unpack('>h',data[97:99])[0] + 99
data[offset-5-2:offset]
data[offset:offset+10] 

Suggested fix:
So, I think it should be described as:

In contrast to CHAR, VARCHAR values are stored as a 1-byte or 2-byte length prefix plus data. The length prefix indicates the number of bytes in the value. A column uses one length byte if values require no more than 255 bytes, 1-byte or 2-byte if values may require more than 255 bytes.
[19 Mar 6:00] MySQL Verification Team
Hello Guisen Yang,

Thank you for the report and feedback.

regards,
Umesh