Bug #9303 blob field with specified length < 256 does not create tinyblob
Submitted: 20 Mar 2005 8:22 Modified: 26 Apr 2005 0:02
Reporter: Brion Vibber Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server Severity:S3 (Non-critical)
Version:4.1.10a OS:Linux (Linux)
Assigned to: Magnus Blåudd CPU Architecture:Any

[20 Mar 2005 8:22] Brion Vibber
Description:
When a blob field is given a specified length in CREATE TABLE, MySQL 4.1 and higher select the smallest blob type which will hold that size: eg blob, mediumblob, or longblob.

For instance:
  create table foo(bar blob(100000000));
makes the 'bar' field a longblob.

For a length under 256 bytes, the smallest type would be tinyblob, but when issuing:
  create table foo(bar blob(100));
the larger 'blob' type (with 65536 byte cutoff) is selected instead.

This appears to be caused by a typo rather than a deliberate choice of minimum: TINYBLOB is in fact selected, but always overridden by BLOB due to a missing 'else' clause in sql/sql_parse.cc. See the suggested patch.

How to repeat:
mysql> create table foo(bar blob(100));
Query OK, 0 rows affected (0.01 sec)

mysql> show create table foo\G
*************************** 1. row ***************************
       Table: foo
Create Table: CREATE TABLE `foo` (
  `bar` blob
) ENGINE=MyISAM DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

Suggested fix:
Adding an 'else' prevents the larger size from overriding the smaller one:

--- mysql-4.1.10a/sql/sql_parse.cc.orig	Sat Mar 19 23:24:45 2005
+++ mysql-4.1.10a/sql/sql_parse.cc	Sat Mar 19 23:24:55 2005
@@ -4338,7 +4338,7 @@
       /* The user has given a length to the blob column */
       if (new_field->length < 256)
 	type= FIELD_TYPE_TINY_BLOB;
-      if (new_field->length < 65536)
+      else if (new_field->length < 65536)
 	type= FIELD_TYPE_BLOB;
       else if (new_field->length < 256L*256L*256L)
 	type= FIELD_TYPE_MEDIUM_BLOB;
[24 Mar 2005 10:37] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/internals/23342
[24 Mar 2005 12:50] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/internals/23348
[12 Apr 2005 10:25] Magnus Blåudd
Pushed to 4.1.10a. 

Does not exist in 5.0
[26 Apr 2005 0:02] Paul DuBois
Noted in 4.1.11 changelog.