From b5b19a7877834362bf135cea408388ad2707c70c Mon Sep 17 00:00:00 2001
From: unknown <bell@sanja.is.com.ua>
Date: Mon, 25 Nov 2002 01:38:56 +0200
Subject: [PATCH] supported possiblity of item substitute (fixed bug) in
 setup_fields more efficient reference creation fixed table_name of Field in
 temporary table

mysql-test/r/subselect.result:
  test of 2 references bugs
mysql-test/t/subselect.test:
  test of 2 references bugs
sql/field.h:
  fixed layout
  fixed table name of fields of temporary table (derived table)
sql/item.cc:
  more efficient reference creation
sql/sql_base.cc:
  fixed layout
  supported possiblity of item substitute (fixed bug)
---
 mysql-test/r/subselect.result | 9 +++++++++
 mysql-test/t/subselect.test   | 5 +++++
 sql/field.h                   | 8 +++++---
 sql/item.cc                   | 2 +-
 sql/sql_base.cc               | 5 +++--
 5 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/mysql-test/r/subselect.result b/mysql-test/r/subselect.result
index 87db58f4f04..ef4da19c826 100644
--- a/mysql-test/r/subselect.result
+++ b/mysql-test/r/subselect.result
@@ -27,6 +27,9 @@ SELECT 1 FROM (SELECT 1 as a) HAVING (SELECT a)=1;
 1
 SELECT (SELECT 1), a;
 Unknown column 'a' in 'field list'
+SELECT 1 as a FROM (SELECT 1) HAVING (SELECT a)=1;
+a
+1
 drop table if exists t1,t2,t3,t4,t5,t6,t7,t8;
 create table t1 (a int);
 create table t2 (a int, b int);
@@ -308,3 +311,9 @@ id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	const	PRIMARY,numreponse	PRIMARY	7	const,const	1	
 2	SUBSELECT								Select tables optimized away
 drop table t1;
+CREATE TABLE t1 (a int(1));
+INSERT INTO t1 VALUES (1);
+SELECT 1 FROM (SELECT a FROM t1) HAVING (SELECT a)=1;
+1
+1
+drop table t1;
diff --git a/mysql-test/t/subselect.test b/mysql-test/t/subselect.test
index 3d0ba76683b..6d4fc7de30b 100644
--- a/mysql-test/t/subselect.test
+++ b/mysql-test/t/subselect.test
@@ -12,6 +12,7 @@ EXPLAIN SELECT 1 FROM (SELECT 1 as a) HAVING (SELECT a)=1;
 SELECT 1 FROM (SELECT 1 as a) HAVING (SELECT a)=1;
 -- error 1054
 SELECT (SELECT 1), a;
+SELECT 1 as a FROM (SELECT 1) HAVING (SELECT a)=1;
 drop table if exists t1,t2,t3,t4,t5,t6,t7,t8;
 create table t1 (a int);
 create table t2 (a int, b int);
@@ -197,3 +198,7 @@ EXPLAIN SELECT numreponse FROM t1 WHERE numeropost='1' AND numreponse=(SELECT 1
 EXPLAIN SELECT MAX(numreponse) FROM t1 WHERE numeropost='1';
 EXPLAIN SELECT numreponse FROM t1 WHERE numeropost='1' AND numreponse=(SELECT MAX(numreponse) FROM t1 WHERE numeropost='1');
 drop table t1;
+CREATE TABLE t1 (a int(1));
+INSERT INTO t1 VALUES (1);
+SELECT 1 FROM (SELECT a FROM t1) HAVING (SELECT a)=1;
+drop table t1;
\ No newline at end of file
diff --git a/sql/field.h b/sql/field.h
index 8c4c48968c4..9fc72cf56ec 100644
--- a/sql/field.h
+++ b/sql/field.h
@@ -126,10 +126,12 @@ class Field {
     Field *tmp= (Field*) memdup_root(root,(char*) this,size_of());
     if (tmp)
     {
-      tmp->table=new_table;
-      tmp->key_start=tmp->part_of_key=tmp->part_of_sortkey=0;
+      tmp->table= new_table;
+      tmp->key_start= tmp->part_of_key= tmp->part_of_sortkey= 0;
       tmp->unireg_check=Field::NONE;
-      tmp->flags&= (NOT_NULL_FLAG | BLOB_FLAG | UNSIGNED_FLAG | ZEROFILL_FLAG | ENUM_FLAG | SET_FLAG);
+      tmp->flags&= (NOT_NULL_FLAG | BLOB_FLAG | UNSIGNED_FLAG |
+		    ZEROFILL_FLAG | ENUM_FLAG | SET_FLAG);
+      tmp->table_name= new_table->table_name;
       tmp->reset_fields();
     }
     return tmp;
diff --git a/sql/item.cc b/sql/item.cc
index f5733d2151d..13433990107 100644
--- a/sql/item.cc
+++ b/sql/item.cc
@@ -487,7 +487,7 @@ bool Item_field::fix_fields(THD *thd, TABLE_LIST *tables, Item **ref)
       else if (refer != (Item **)not_found_item)
       {
 	Item_ref *r;
-	*ref= r= new Item_ref((char *)db_name, (char *)table_name,
+	*ref= r= new Item_ref(refer, (char *)table_name,
 			   (char *)field_name);
 	if (!r)
 	  return 1;
diff --git a/sql/sql_base.cc b/sql/sql_base.cc
index 755d64a97e8..08a17ed1197 100644
--- a/sql/sql_base.cc
+++ b/sql/sql_base.cc
@@ -2061,9 +2061,9 @@ int setup_fields(THD *thd, TABLE_LIST *tables, List<Item> &fields,
     if (item->type() == Item::FIELD_ITEM &&
 	((Item_field*) item)->field_name[0] == '*')
     {
-      uint elem=fields.elements;
+      uint elem= fields.elements;
       if (insert_fields(thd,tables,((Item_field*) item)->db_name,
-			((Item_field*) item)->table_name,&it))
+			((Item_field*) item)->table_name, &it))
 	DBUG_RETURN(-1); /* purecov: inspected */
       if (sum_func_list)
       {
@@ -2079,6 +2079,7 @@ int setup_fields(THD *thd, TABLE_LIST *tables, List<Item> &fields,
     {
       if (item->fix_fields(thd, tables, it.ref()))
 	DBUG_RETURN(-1); /* purecov: inspected */
+      item= *(it.ref()); //Item can be chenged in fix fields
       if (item->with_sum_func && item->type() != Item::SUM_FUNC_ITEM &&
 	  sum_func_list)
 	item->split_sum_func(*sum_func_list);
-- 
2.30.9