From 9d294b869e6f092a6baa28b26f3ba3835dc9890d Mon Sep 17 00:00:00 2001 From: "bell@sanja.is.com.ua" <> Date: Sun, 19 Oct 2003 14:25:33 +0300 Subject: [PATCH] correct table name assigned to temporary table field: - correct table name shown in EXPLAIN Iindex reference) - pointer on freed memmory (reallocation of table name in reusing table entry) can't be used in EXPLAIN (BUG#1584) --- mysql-test/r/derived.result | 15 +++++++++++++++ mysql-test/r/subselect.result | 7 ------- mysql-test/t/derived.test | 10 ++++++++++ mysql-test/t/subselect.test | 9 --------- sql/item.cc | 12 +++++++----- sql/sql_select.cc | 3 +++ 6 files changed, 35 insertions(+), 21 deletions(-) diff --git a/mysql-test/r/derived.result b/mysql-test/r/derived.result index d24ac5e898a..12d8bbaf8ae 100644 --- a/mysql-test/r/derived.result +++ b/mysql-test/r/derived.result @@ -215,3 +215,18 @@ ERROR 42000: You have an error in your SQL syntax. Check the manual that corres insert into (select * from t1) values (5); ERROR 42000: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '(select * from t1) values (5)' at line 1 drop table t1; +create table t1 (E1 INTEGER UNSIGNED NOT NULL, E2 INTEGER UNSIGNED NOT NULL, E3 INTEGER UNSIGNED NOT NULL, PRIMARY KEY(E1) +); +insert into t1 VALUES(1,1,1), (2,2,1); +select count(*) from t1 INNER JOIN (SELECT A.E1, A.E2, A.E3 FROM t1 AS A WHERE A.E3 = (SELECT MAX(B.E3) FROM t1 AS B WHERE A.E2 = B.E2)) AS THEMAX ON t1.E1 = THEMAX.E2 AND t1.E1 = t1.E2; +count(*) +2 +explain select count(*) from t1 INNER JOIN (SELECT A.E1, A.E2, A.E3 FROM t1 AS A WHERE A.E3 = (SELECT MAX(B.E3) FROM t1 AS B WHERE A.E2 = B.E2)) AS THEMAX ON t1.E1 = THEMAX.E2 AND t1.E1 = t1.E2; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY ALL NULL NULL NULL NULL 2 +1 PRIMARY t1 eq_ref PRIMARY PRIMARY 4 THEMAX.E2 1 Using where +2 DERIVED t1 ALL NULL NULL NULL NULL 2 Using where +3 DEPENDENT SUBQUERY B ALL NULL NULL NULL NULL 2 Using where +Warnings: +Note 1275 Field or reference 'A.E2' of SELECT #3 was resolved in SELECT #2 +drop table t1; diff --git a/mysql-test/r/subselect.result b/mysql-test/r/subselect.result index a2460b8f11a..14a132531ca 100644 --- a/mysql-test/r/subselect.result +++ b/mysql-test/r/subselect.result @@ -1439,10 +1439,3 @@ id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY t3 ALL NULL NULL NULL NULL 3 Using where 2 SUBQUERY t2 ALL NULL NULL NULL NULL 4 Using temporary; Using filesort drop table t2, t3; -create table t1 (E1 INTEGER UNSIGNED NOT NULL, E2 INTEGER UNSIGNED NOT NULL, E3 INTEGER UNSIGNED NOT NULL, PRIMARY KEY(E1) -); -insert into t1 VALUES(1,1,1), (2,2,1); -select count(*) from t1 INNER JOIN (SELECT A.E1, A.E2, A.E3 FROM t1 AS A WHERE A.E3 = (SELECT MAX(B.E3) FROM t1 AS B WHERE A.E2 = B.E2)) AS THEMAX ON t1.E1 = THEMAX.E2 AND t1.E1 = t1.E2; -count(*) -2 -drop table t1; diff --git a/mysql-test/t/derived.test b/mysql-test/t/derived.test index a8583aa5ea3..80b131ac6fe 100644 --- a/mysql-test/t/derived.test +++ b/mysql-test/t/derived.test @@ -122,3 +122,13 @@ delete from (select * from t1); -- error 1064 insert into (select * from t1) values (5); drop table t1; + +# +# deived tables with subquery inside all by one table +# +create table t1 (E1 INTEGER UNSIGNED NOT NULL, E2 INTEGER UNSIGNED NOT NULL, E3 INTEGER UNSIGNED NOT NULL, PRIMARY KEY(E1) +); +insert into t1 VALUES(1,1,1), (2,2,1); +select count(*) from t1 INNER JOIN (SELECT A.E1, A.E2, A.E3 FROM t1 AS A WHERE A.E3 = (SELECT MAX(B.E3) FROM t1 AS B WHERE A.E2 = B.E2)) AS THEMAX ON t1.E1 = THEMAX.E2 AND t1.E1 = t1.E2; +explain select count(*) from t1 INNER JOIN (SELECT A.E1, A.E2, A.E3 FROM t1 AS A WHERE A.E3 = (SELECT MAX(B.E3) FROM t1 AS B WHERE A.E2 = B.E2)) AS THEMAX ON t1.E1 = THEMAX.E2 AND t1.E1 = t1.E2; +drop table t1; diff --git a/mysql-test/t/subselect.test b/mysql-test/t/subselect.test index afc2d1a9328..8c0b3fb3919 100644 --- a/mysql-test/t/subselect.test +++ b/mysql-test/t/subselect.test @@ -965,12 +965,3 @@ insert into t2 values (2,2), (2,1), (3,3), (3,1); select * from t3 where a > all (select max(b) from t2 group by a); explain select * from t3 where a > all (select max(b) from t2 group by a); drop table t2, t3; - -# -# deived tables with subquery inside all by one table -# -create table t1 (E1 INTEGER UNSIGNED NOT NULL, E2 INTEGER UNSIGNED NOT NULL, E3 INTEGER UNSIGNED NOT NULL, PRIMARY KEY(E1) -); -insert into t1 VALUES(1,1,1), (2,2,1); -select count(*) from t1 INNER JOIN (SELECT A.E1, A.E2, A.E3 FROM t1 AS A WHERE A.E3 = (SELECT MAX(B.E3) FROM t1 AS B WHERE A.E2 = B.E2)) AS THEMAX ON t1.E1 = THEMAX.E2 AND t1.E1 = t1.E2; -drop table t1; diff --git a/sql/item.cc b/sql/item.cc index 4de4951cb51..6fbc3a304d6 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -272,11 +272,13 @@ Item_field::Item_field(Field *f) :Item_ident(NullS,f->table_name,f->field_name) } // Constructor need to process subselect with temporary tables (see Item) -Item_field::Item_field(THD *thd, Item_field &item): - Item_ident(thd, item), - field(item.field), - result_field(item.result_field) -{ collation.set(DERIVATION_IMPLICIT); } +Item_field::Item_field(THD *thd, Item_field &item) + :Item_ident(thd, item), + field(item.field), + result_field(item.result_field) +{ + collation.set(DERIVATION_IMPLICIT); +} void Item_field::set_field(Field *field_par) { diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 89aae95c951..9bdb989b6a1 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -4866,6 +4866,9 @@ create_tmp_table(THD *thd,TMP_TABLE_PARAM *param,List &fields, recinfo->type=FIELD_NORMAL; if (!--hidden_field_count) null_count=(null_count+7) & ~7; // move to next byte + + // fix table name in field entry + field->table_name= table->table_name; } param->copy_field_end=copy;