From fc6b6435b4c3f5203e6ffc3de289f0413a7ef33f Mon Sep 17 00:00:00 2001 From: Sergey Petrunya Date: Tue, 6 Sep 2011 17:06:04 +0400 Subject: [PATCH] BUG#823930: Wrong result with semijoin materialization and blob fields - Make subquery_types_allow_materialization() detect a case where create_tmp_table() would create a blob column which would make it impossible to use materialization Non-semi-join materialization worked because it detected that this case and felt back to use IN->EXISTS. Semi-join Materialization cannot easily fallback, so we have to detect this case early. --- mysql-test/r/subselect_sj_mat.result | 14 ++++++++------ sql/item.cc | 4 ++++ sql/opt_subselect.cc | 10 ++++++++++ 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/mysql-test/r/subselect_sj_mat.result b/mysql-test/r/subselect_sj_mat.result index cbfe5f22467..2ce54609fa4 100644 --- a/mysql-test/r/subselect_sj_mat.result +++ b/mysql-test/r/subselect_sj_mat.result @@ -856,15 +856,16 @@ explain extended select left(a1,7), left(a2,7) from t1_1024 where a1 in (select substring(b1,1,1024) from t2_1024 where b1 > '0'); id select_type table type possible_keys key key_len ref rows filtered Extra -1 PRIMARY t1_1024 ALL NULL NULL NULL NULL 3 100.00 -1 PRIMARY eq_ref NULL distinct_key 15 func,func 1 100.00 -2 SUBQUERY t2_1024 ALL NULL NULL NULL NULL 3 100.00 Using where +1 PRIMARY t1_1024 ALL NULL NULL NULL NULL 3 100.00 Start temporary +1 PRIMARY t2_1024 ALL NULL NULL NULL NULL 3 100.00 Using where; End temporary; Using join buffer (flat, BNL join) Warnings: Note 1003 select left(`test`.`t1_1024`.`a1`,7) AS `left(a1,7)`,left(`test`.`t1_1024`.`a2`,7) AS `left(a2,7)` from `test`.`t1_1024` semi join (`test`.`t2_1024`) where ((`test`.`t2_1024`.`b1` > '0') and (`test`.`t1_1024`.`a1` = substr(`test`.`t2_1024`.`b1`,1,1024))) select left(a1,7), left(a2,7) from t1_1024 where a1 in (select substring(b1,1,1024) from t2_1024 where b1 > '0'); left(a1,7) left(a2,7) +1 - 01x 2 - 01x +1 - 02x 2 - 02x explain extended select left(a1,7), left(a2,7) from t1_1024 where a1 in (select group_concat(b1) from t2_1024 group by b2); @@ -950,15 +951,16 @@ explain extended select left(a1,7), left(a2,7) from t1_1025 where a1 in (select substring(b1,1,1025) from t2_1025 where b1 > '0'); id select_type table type possible_keys key key_len ref rows filtered Extra -1 PRIMARY t1_1025 ALL NULL NULL NULL NULL 3 100.00 -1 PRIMARY eq_ref NULL distinct_key 15 func,func 1 100.00 -2 SUBQUERY t2_1025 ALL NULL NULL NULL NULL 3 100.00 Using where +1 PRIMARY t1_1025 ALL NULL NULL NULL NULL 3 100.00 Start temporary +1 PRIMARY t2_1025 ALL NULL NULL NULL NULL 3 100.00 Using where; End temporary; Using join buffer (flat, BNL join) Warnings: Note 1003 select left(`test`.`t1_1025`.`a1`,7) AS `left(a1,7)`,left(`test`.`t1_1025`.`a2`,7) AS `left(a2,7)` from `test`.`t1_1025` semi join (`test`.`t2_1025`) where ((`test`.`t2_1025`.`b1` > '0') and (`test`.`t1_1025`.`a1` = substr(`test`.`t2_1025`.`b1`,1,1025))) select left(a1,7), left(a2,7) from t1_1025 where a1 in (select substring(b1,1,1025) from t2_1025 where b1 > '0'); left(a1,7) left(a2,7) +1 - 01x 2 - 01x +1 - 02x 2 - 02x explain extended select left(a1,7), left(a2,7) from t1_1025 where a1 in (select group_concat(b1) from t2_1025 group by b2); diff --git a/sql/item.cc b/sql/item.cc index ca4d41fcbdb..4fcffcfbc2a 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -5191,6 +5191,10 @@ Field *Item::make_string_field(TABLE *table) { Field *field; DBUG_ASSERT(collation.collation); + /* + Note: the following check is repeated in + subquery_types_allow_materialization(): + */ if (max_length/collation.collation->mbmaxlen > CONVERT_IF_BIGGER_TO_BLOB) field= new Field_blob(max_length, maybe_null, name, collation.collation); diff --git a/sql/opt_subselect.cc b/sql/opt_subselect.cc index 9ea55390fc5..3cb13b0b88c 100644 --- a/sql/opt_subselect.cc +++ b/sql/opt_subselect.cc @@ -559,6 +559,16 @@ bool subquery_types_allow_materialization(Item_in_subselect *in_subs) if (inner->field_type() == MYSQL_TYPE_BLOB || inner->field_type() == MYSQL_TYPE_GEOMETRY) DBUG_RETURN(FALSE); + /* + Materialization also is unable to work when create_tmp_table() will + create a blob column because item->max_length is too big. + The following check is copied from Item::make_string_field(): + */ + if (inner->max_length / inner->collation.collation->mbmaxlen > + CONVERT_IF_BIGGER_TO_BLOB) + { + DBUG_RETURN(FALSE); + } break; case TIME_RESULT: if (mysql_type_to_time_type(outer->field_type()) !=