diff options
-rw-r--r-- | mysql-test/r/derived.result | 8 | ||||
-rw-r--r-- | mysql-test/r/subselect.result | 14 | ||||
-rw-r--r-- | mysql-test/r/union.result | 10 | ||||
-rw-r--r-- | sql/sql_class.h | 2 | ||||
-rw-r--r-- | sql/sql_lex.h | 2 | ||||
-rw-r--r-- | sql/sql_select.cc | 47 |
6 files changed, 52 insertions, 31 deletions
diff --git a/mysql-test/r/derived.result b/mysql-test/r/derived.result index cdbca9aa2aa..dd4c32403b5 100644 --- a/mysql-test/r/derived.result +++ b/mysql-test/r/derived.result @@ -94,13 +94,13 @@ id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY <derived2> ALL NULL NULL NULL NULL 3 2 DERIVED t1 ALL NULL NULL NULL NULL 4 3 UNION t1 ALL NULL NULL NULL NULL 4 --2 UNION RESULT <union2> ALL NULL NULL NULL NULL NULL +NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL explain select * from (select * from t1 union all select * from t1) a; id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY <derived2> ALL NULL NULL NULL NULL 8 2 DERIVED t1 ALL NULL NULL NULL NULL 4 3 UNION t1 ALL NULL NULL NULL NULL 4 --2 UNION RESULT <union2> ALL NULL NULL NULL NULL NULL +NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL CREATE TABLE t2 (a int not null); insert into t2 values(1); select * from (select * from t1 where t1.a=(select a from t2 where t2.a=t1.a)) a; @@ -248,10 +248,10 @@ id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY <derived4> ALL NULL NULL NULL NULL 2 4 DERIVED t1 ALL NULL NULL NULL NULL 2 5 UNION t1 ALL NULL NULL NULL NULL 2 --4 UNION RESULT <union4> ALL NULL NULL NULL NULL NULL +NULL UNION RESULT <union4,5> ALL NULL NULL NULL NULL NULL 2 DERIVED t1 ALL NULL NULL NULL NULL 2 3 UNION t1 ALL NULL NULL NULL NULL 2 --2 UNION RESULT <union2> ALL NULL NULL NULL NULL NULL +NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL drop table t1; CREATE TABLE `t1` ( `N` int(11) unsigned NOT NULL default '0', diff --git a/mysql-test/r/subselect.result b/mysql-test/r/subselect.result index ea0fd7217a8..9ad3a5a9cf2 100644 --- a/mysql-test/r/subselect.result +++ b/mysql-test/r/subselect.result @@ -16,7 +16,7 @@ explain extended SELECT (SELECT 1) UNION SELECT (SELECT 2); id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used 3 UNION NULL NULL NULL NULL NULL NULL NULL No tables used --1 UNION RESULT <union1> ALL NULL NULL NULL NULL NULL +NULL UNION RESULT <union1,3> ALL NULL NULL NULL NULL NULL Warnings: Note 1249 Select 2 was reduced during optimisation Note 1249 Select 4 was reduced during optimisation @@ -29,7 +29,7 @@ id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used 3 SUBQUERY NULL NULL NULL NULL NULL NULL NULL No tables used 4 UNION NULL NULL NULL NULL NULL NULL NULL No tables used --3 UNION RESULT <union3> ALL NULL NULL NULL NULL NULL +NULL UNION RESULT <union3,4> ALL NULL NULL NULL NULL NULL Warnings: Note 1249 Select 2 was reduced during optimisation Note 1003 select (select 0 AS `0` union select 0 AS `0`) AS `(SELECT (SELECT 0 UNION SELECT 0))` @@ -184,7 +184,7 @@ id select_type table type possible_keys key key_len ref rows Extra 2 SUBQUERY t3 ALL NULL NULL NULL NULL 3 Using filesort 3 UNION t4 ALL NULL NULL NULL NULL 3 Using where; Using filesort 4 SUBQUERY t2 ALL NULL NULL NULL NULL 2 --1 UNION RESULT <union1> ALL NULL NULL NULL NULL NULL +NULL UNION RESULT <union1,3> ALL NULL NULL NULL NULL NULL Warnings: Note 1003 (select test.t2.a AS `a`,test.t2.b AS `b` from test.t2 where (test.t2.b = (select test.t3.a AS `a` from test.t3 order by test.t3.a desc limit 1))) union (select test.t4.a AS `a`,test.t4.b AS `b` from test.t4 where (test.t4.b = (select (max(test.t2.a) * 4) AS `max(t2.a)*4` from test.t2)) order by test.t4.a) select (select a from t3 where a<t2.a*4 order by 1 desc limit 1), a from t2; @@ -309,7 +309,7 @@ id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY t2 ALL NULL NULL NULL NULL 2 2 DEPENDENT SUBQUERY t1 system NULL NULL NULL NULL 1 3 DEPENDENT UNION t5 ALL NULL NULL NULL NULL 2 Using where --2 UNION RESULT <union2> ALL NULL NULL NULL NULL NULL +NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL Warnings: Note 1276 Field or reference 't2.a' of SELECT #2 was resolved in SELECT #1 Note 1276 Field or reference 't2.a' of SELECT #3 was resolved in SELECT #1 @@ -417,7 +417,7 @@ id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY t1 index NULL topic 3 NULL 2 Using index 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL No tables used 3 UNION NULL NULL NULL NULL NULL NULL NULL No tables used --2 UNION RESULT <union2> ALL NULL NULL NULL NULL NULL +NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL Warnings: Note 1003 select 1 AS `1` from test.t1 drop table t1; @@ -732,7 +732,7 @@ id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY t2 index NULL id 5 NULL 2 Using where; Using index 2 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL No tables used 3 DEPENDENT UNION NULL NULL NULL NULL NULL NULL NULL No tables used --2 UNION RESULT <union2> ALL NULL NULL NULL NULL NULL +NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL Warnings: Note 1003 select test.t2.id AS `id` from test.t2 where <in_optimizer>(test.t2.id,<exists>(select 1 AS `Not_used` having (<cache>(test.t2.id) = <null_helper>(1)) union select 1 AS `Not_used` having (<cache>(test.t2.id) = <null_helper>(3)))) SELECT * FROM t2 WHERE id IN (SELECT 5 UNION SELECT 3); @@ -1547,7 +1547,7 @@ id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY t1 system NULL NULL NULL NULL 1 2 SUBQUERY t1 system NULL NULL NULL NULL 1 3 UNION t1 system NULL NULL NULL NULL 1 --2 UNION RESULT <union2> ALL NULL NULL NULL NULL NULL +NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL Warnings: Note 1003 select test.t1.s1 AS `s1` from test.t1 drop table t1; diff --git a/mysql-test/r/union.result b/mysql-test/r/union.result index 4655f92c7d7..0735ea4dc40 100644 --- a/mysql-test/r/union.result +++ b/mysql-test/r/union.result @@ -86,7 +86,7 @@ explain extended (select a,b from t1 limit 2) union all (select a,b from t2 ord id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY t1 ALL NULL NULL NULL NULL 4 2 UNION t2 ALL NULL NULL NULL NULL 4 Using filesort --1 UNION RESULT <union1> ALL NULL NULL NULL NULL NULL Using filesort +NULL UNION RESULT <union1,2> ALL NULL NULL NULL NULL NULL Using filesort Warnings: Note 1003 (select test.t1.a AS `a`,test.t1.b AS `b` from test.t1 limit 2) union all (select test.t2.a AS `a`,test.t2.b AS `b` from test.t2 order by test.t2.a limit 1) order by b desc (select sql_calc_found_rows a,b from t1 limit 2) union all (select a,b from t2 order by a) limit 2; @@ -107,7 +107,7 @@ explain select a,b from t1 union all select a,b from t2; id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY t1 ALL NULL NULL NULL NULL 4 2 UNION t2 ALL NULL NULL NULL NULL 4 --1 UNION RESULT <union1> ALL NULL NULL NULL NULL NULL +NULL UNION RESULT <union1,2> ALL NULL NULL NULL NULL NULL explain select xx from t1 union select 1; ERROR 42S22: Unknown column 'xx' in 'field list' explain select a,b from t1 union select 1; @@ -470,7 +470,7 @@ explain extended (select * from t1 where a=1) union (select * from t2 where a=1) id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY t1 const PRIMARY PRIMARY 4 const 1 2 UNION t2 const PRIMARY PRIMARY 4 const 1 --1 UNION RESULT <union1> ALL NULL NULL NULL NULL NULL +NULL UNION RESULT <union1,2> ALL NULL NULL NULL NULL NULL Warnings: Note 1003 (select test.t1.a AS `a`,test.t1.b AS `b` from test.t1 where (test.t1.a = 1)) union (select test.t2.a AS `a`,test.t2.b AS `b` from test.t2 where (test.t2.a = 1)) (select * from t1 where a=5) union (select * from t2 where a=1); @@ -493,12 +493,12 @@ id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables 2 UNION t1 index PRIMARY PRIMARY 4 NULL 4 Using index 2 UNION t2 index PRIMARY PRIMARY 4 NULL 3 Using where; Using index --1 UNION RESULT <union1> ALL NULL NULL NULL NULL NULL +NULL UNION RESULT <union1,2> ALL NULL NULL NULL NULL NULL explain (select * from t1 where a=1) union (select * from t1 where b=1); id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY t1 const PRIMARY PRIMARY 4 const 1 2 UNION t1 ref b b 5 const 1 Using where --1 UNION RESULT <union1> ALL NULL NULL NULL NULL NULL +NULL UNION RESULT <union1,2> ALL NULL NULL NULL NULL NULL drop table t1,t2; create table t1 ( id int not null auto_increment, primary key (id) ,user_name text ); create table t2 ( id int not null auto_increment, primary key (id) ,group_name text ); diff --git a/sql/sql_class.h b/sql/sql_class.h index 9ed49c22119..493ac8e1256 100644 --- a/sql/sql_class.h +++ b/sql/sql_class.h @@ -773,7 +773,7 @@ public: uint current_tablenr,tmp_table; uint server_status,open_options,system_thread; uint32 db_length; - int select_number; //number of select (used for EXPLAIN) + uint select_number; //number of select (used for EXPLAIN) /* variables.transaction_isolation is reset to this after each commit */ enum_tx_isolation session_tx_isolation; enum_check_fields count_cuted_fields; diff --git a/sql/sql_lex.h b/sql/sql_lex.h index e6a2dcdb754..2df2c998ff0 100644 --- a/sql/sql_lex.h +++ b/sql/sql_lex.h @@ -425,7 +425,7 @@ public: ulong table_join_options; uint in_sum_expr; - int select_number; /* number of select (used for EXPLAIN) */ + uint select_number; /* number of select (used for EXPLAIN) */ uint with_wild; /* item list contain '*' */ bool braces; /* SELECT ... UNION (SELECT ... ) <- this braces */ /* TRUE when having fix field called in processing of this SELECT */ diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 891efb50d11..194f27e6fb8 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -9133,7 +9133,7 @@ static void select_describe(JOIN *join, bool need_tmp_table, bool need_order, if (message) { - item_list.push_back(new Item_int((longlong) + item_list.push_back(new Item_int((int32) join->select_lex->select_number)); item_list.push_back(new Item_string(join->select_lex->type, strlen(join->select_lex->type), cs)); @@ -9146,23 +9146,44 @@ static void select_describe(JOIN *join, bool need_tmp_table, bool need_order, else if (join->select_lex == join->unit->fake_select_lex) { /* - Here is guessing about fake_select_lex to avoid union SELECT - execution to get accurate information + here we assume that the query will return at least two rows, so we + show "filesort" in EXPLAIN. Of course, sometimes we'll be wrong + and no filesort will be actually done, but executing all selects in + the UNION to provide precise EXPLAIN information will hardly be + appreciated :) */ - char table_name_buffer[64]; + char table_name_buffer[NAME_LEN]; item_list.empty(); /* id */ - item_list.push_back(new Item_int((int32) - join->select_lex->select_number)); + item_list.push_back(new Item_null); /* select_type */ item_list.push_back(new Item_string(join->select_lex->type, strlen(join->select_lex->type), cs)); /* table */ - int len= my_snprintf(table_name_buffer, sizeof(table_name_buffer)-1, - "<union%d>", - -join->select_lex->select_number); - item_list.push_back(new Item_string(table_name_buffer, len, cs)); + { + SELECT_LEX *sl= join->unit->first_select(); + uint len= 6, lastop= 0; + memcpy(table_name_buffer, "<union", 6); + for (; sl && len + lastop + 5 < NAME_LEN; sl= sl->next_select()) + { + len+= lastop; + lastop= my_snprintf(table_name_buffer + len, NAME_LEN - len, + "%u,", sl->select_number); + } + if (sl || len + lastop >= NAME_LEN) + { + memcpy(table_name_buffer + len, "...>", 5); + len+= 4; + } + else + { + len+= lastop; + table_name_buffer[len - 1]= '>'; // change ',' to '>' + } + item_list.push_back(new Item_string(table_name_buffer, len, cs)); + } + /* type */ item_list.push_back(new Item_string(join_type_str[JT_ALL], strlen(join_type_str[JT_ALL]), cs)); @@ -9195,7 +9216,7 @@ static void select_describe(JOIN *join, bool need_tmp_table, bool need_order, TABLE *table=tab->table; char buff[512],*buff_ptr=buff; char buff1[512], buff2[512]; - char table_name_buffer[64]; + char table_name_buffer[NAME_LEN]; String tmp1(buff1,sizeof(buff1),cs); String tmp2(buff2,sizeof(buff2),cs); tmp1.length(0); @@ -9203,7 +9224,7 @@ static void select_describe(JOIN *join, bool need_tmp_table, bool need_order, item_list.empty(); /* id */ - item_list.push_back(new Item_int((int32) + item_list.push_back(new Item_uint((uint32) join->select_lex->select_number)); /* select_type */ item_list.push_back(new Item_string(join->select_lex->type, @@ -9379,7 +9400,7 @@ int mysql_explain_union(THD *thd, SELECT_LEX_UNIT *unit, select_result *result) } if (first->next_select()) { - unit->fake_select_lex->select_number= -first->select_number; + unit->fake_select_lex->select_number= UINT_MAX; // jost for initialization unit->fake_select_lex->type= "UNION RESULT"; unit->fake_select_lex->options|= SELECT_DESCRIBE; if (!(res= unit->prepare(thd, result, SELECT_NO_UNLOCK | SELECT_DESCRIBE))) |