From a25a3fb13437eeb6b1a6501da58c44bc41931129 Mon Sep 17 00:00:00 2001 From: Alexey Kopytov Date: Sat, 21 Nov 2009 20:31:00 +0300 Subject: [PATCH] Backport of the fix for bug #33969: Updating a text field via a left join When creating a temporary TEXT/BLOB field from an Item in Item::make_string_field(), the field's type was unconditionally set to the one corresponding to the maximum length (i.e. LONGTEXT/ LONGBLOB). This resulted in problems when exactly the same TEXT/BLOB is type required in cases like CREATE ... SELECT or creating internal temporary tables for joins. Fixed by calling a different constructor for Field_blob so that an appropriate type is used depending on the Item's max_length value. --- mysql-test/r/type_blob.result | 18 ++++++++++++++++++ mysql-test/r/type_ranges.result | 4 ++-- mysql-test/t/type_blob.test | 20 ++++++++++++++++++++ sql/item.cc | 2 +- 4 files changed, 41 insertions(+), 3 deletions(-) diff --git a/mysql-test/r/type_blob.result b/mysql-test/r/type_blob.result index d11ab236c34..4d1c451f01a 100644 --- a/mysql-test/r/type_blob.result +++ b/mysql-test/r/type_blob.result @@ -974,3 +974,21 @@ ERROR 42000: Display width out of range for column 'cast as char' (max = 4294967 explain select convert(1, binary(999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999)); ERROR 42000: Display width out of range for column 'cast as char' (max = 4294967295) End of 5.0 tests +CREATE TABLE t1(id INT NOT NULL); +CREATE TABLE t2(id INT NOT NULL, c TEXT NOT NULL); +INSERT INTO t1 VALUES (1); +INSERT INTO t2 VALUES (1, ''); +UPDATE t2 SET c = REPEAT('1', 70000); +Warnings: +Warning 1265 Data truncated for column 'c' at row 1 +SELECT LENGTH(c) FROM t2; +LENGTH(c) +65535 +UPDATE t1 LEFT JOIN t2 USING(id) SET t2.c = REPEAT('1', 70000) WHERE t1.id = 1; +Warnings: +Warning 1265 Data truncated for column 'c' at row 1 +SELECT LENGTH(c) FROM t2; +LENGTH(c) +65535 +DROP TABLE t1, t2; +End of 5.1 tests diff --git a/mysql-test/r/type_ranges.result b/mysql-test/r/type_ranges.result index 6e08067d8a4..5e915fbfa2d 100644 --- a/mysql-test/r/type_ranges.result +++ b/mysql-test/r/type_ranges.result @@ -276,8 +276,8 @@ t1 int(1) NULL NO 0 # t2 varchar(1) latin1_swedish_ci NO # t3 varchar(256) latin1_swedish_ci NO # t4 varbinary(256) NULL NO # -t5 longtext latin1_swedish_ci NO NULL # -t6 longblob NULL NO NULL # +t5 text latin1_swedish_ci NO NULL # +t6 blob NULL NO NULL # t7 char(0) latin1_swedish_ci NO # t8 binary(0) NULL NO # select t1,t2,length(t3),length(t4),length(t5),length(t6),t7,t8 from t2; diff --git a/mysql-test/t/type_blob.test b/mysql-test/t/type_blob.test index 460da1c1614..85705d664f4 100644 --- a/mysql-test/t/type_blob.test +++ b/mysql-test/t/type_blob.test @@ -612,3 +612,23 @@ explain select convert(1, binary(4294967296)); explain select convert(1, binary(999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999)); --echo End of 5.0 tests + +# +# Bug #33969: Updating a text field via a left join +# + +CREATE TABLE t1(id INT NOT NULL); +CREATE TABLE t2(id INT NOT NULL, c TEXT NOT NULL); + +INSERT INTO t1 VALUES (1); +INSERT INTO t2 VALUES (1, ''); + +UPDATE t2 SET c = REPEAT('1', 70000); +SELECT LENGTH(c) FROM t2; + +UPDATE t1 LEFT JOIN t2 USING(id) SET t2.c = REPEAT('1', 70000) WHERE t1.id = 1; +SELECT LENGTH(c) FROM t2; + +DROP TABLE t1, t2; + +--echo End of 5.1 tests diff --git a/sql/item.cc b/sql/item.cc index b59a82f8045..f8fd82fda67 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -5025,7 +5025,7 @@ Field *Item::make_string_field(TABLE *table) DBUG_ASSERT(collation.collation); if (max_length/collation.collation->mbmaxlen > CONVERT_IF_BIGGER_TO_BLOB) field= new Field_blob(max_length, maybe_null, name, - collation.collation); + collation.collation, TRUE); /* Item_type_holder holds the exact type, do not change it */ else if (max_length > 0 && (type() != Item::TYPE_HOLDER || field_type() != MYSQL_TYPE_STRING))