From a114ede24abff88609e5686f0eafbaf7fc2ed0b7 Mon Sep 17 00:00:00 2001
From: unknown <gshchepa/uchum@host.loc>
Date: Fri, 11 Jan 2008 05:06:08 +0400
Subject: [PATCH 1/8] Bug#33699: The UPDATE statement allows NULL as new value
 on a NOT NULL columns (default datatype value is assigned).

The mysql_update function has been modified to generate
an error when trying to set a NOT NULL field to NULL rather than a warning
in the set_field_to_null_with_conversions function.



mysql-test/extra/rpl_tests/rpl_extraMaster_Col.test:
  Updated test case (for bug#33699).
mysql-test/include/ps_modify.inc:
  Updated test case (for bug#33699).
mysql-test/r/auto_increment.result:
  Updated test case (for bug#33699).
mysql-test/r/null.result:
  Updated test case (for bug#33699).
mysql-test/r/ps_2myisam.result:
  Updated test case (for bug#33699).
mysql-test/r/ps_3innodb.result:
  Updated test case (for bug#33699).
mysql-test/r/ps_4heap.result:
  Updated test case (for bug#33699).
mysql-test/r/ps_5merge.result:
  Updated test case (for bug#33699).
mysql-test/r/warnings.result:
  Updated test case (for bug#33699).
mysql-test/suite/ndb/r/ps_7ndb.result:
  Updated test case (for bug#33699).
mysql-test/suite/rpl/r/rpl_extraColmaster_innodb.result:
  Updated test case (for bug#33699).
mysql-test/suite/rpl/r/rpl_extraColmaster_myisam.result:
  Updated test case (for bug#33699).
mysql-test/suite/rpl/t/rpl_err_ignoredtable.test:
  Updated test case (for bug#33699).
mysql-test/t/auto_increment.test:
  Updated test case (for bug#33699).
mysql-test/t/null.test:
  Updated test case (for bug#33699).
mysql-test/t/warnings.test:
  Updated test case (for bug#33699).
sql/sql_update.cc:
  Bug#33699: The mysql_update function has been modified to generate
  an error when trying to set a NOT NULL field to NULL rather than
  a warning in the set_field_to_null_with_conversions function.
tests/mysql_client_test.c:
  Updated test case (for bug#33699).
---
 mysql-test/extra/rpl_tests/rpl_extraMaster_Col.test  |  2 +-
 mysql-test/include/ps_modify.inc                     |  1 +
 mysql-test/r/auto_increment.result                   | 12 ++++++------
 mysql-test/r/null.result                             |  8 +++-----
 mysql-test/r/ps_2myisam.result                       |  5 ++---
 mysql-test/r/ps_3innodb.result                       |  5 ++---
 mysql-test/r/ps_4heap.result                         |  5 ++---
 mysql-test/r/ps_5merge.result                        | 10 ++++------
 mysql-test/r/warnings.result                         |  3 +--
 mysql-test/suite/ndb/r/ps_7ndb.result                |  5 ++---
 .../suite/rpl/r/rpl_extraColmaster_innodb.result     | 12 +++---------
 .../suite/rpl/r/rpl_extraColmaster_myisam.result     | 12 +++---------
 mysql-test/suite/rpl/t/rpl_err_ignoredtable.test     |  2 +-
 mysql-test/t/auto_increment.test                     |  2 ++
 mysql-test/t/null.test                               |  2 ++
 mysql-test/t/warnings.test                           |  1 +
 sql/sql_update.cc                                    |  4 +++-
 tests/mysql_client_test.c                            |  2 +-
 18 files changed, 40 insertions(+), 53 deletions(-)

diff --git a/mysql-test/extra/rpl_tests/rpl_extraMaster_Col.test b/mysql-test/extra/rpl_tests/rpl_extraMaster_Col.test
index cdd828305dc..5fc5a6cce1a 100644
--- a/mysql-test/extra/rpl_tests/rpl_extraMaster_Col.test
+++ b/mysql-test/extra/rpl_tests/rpl_extraMaster_Col.test
@@ -419,7 +419,7 @@ connection master;
    update t31 set f5=555555555555555 where f3=6;
    update t31 set f2=2 where f3=2;
    update t31 set f1=NULL where f3=1;
-   update t31 set f3=NULL, f27=NULL, f35='f35 new value' where f3=3;
+   update t31 set f3=0, f27=NULL, f35='f35 new value' where f3=3;
 
 --echo
 --echo ** Delete from Master **
diff --git a/mysql-test/include/ps_modify.inc b/mysql-test/include/ps_modify.inc
index f66f888261d..4cde18b97d1 100644
--- a/mysql-test/include/ps_modify.inc
+++ b/mysql-test/include/ps_modify.inc
@@ -108,6 +108,7 @@ execute stmt1 using @arg00, @arg01;
 select a,b from t1 where a=@arg00;
 set @arg00=NULL;
 set @arg01=2;
+--error 1048
 execute stmt1 using @arg00, @arg01;
 select a,b from t1 order by a;
 set @arg00=0;
diff --git a/mysql-test/r/auto_increment.result b/mysql-test/r/auto_increment.result
index 54c2df34a7f..bc9daf43f14 100644
--- a/mysql-test/r/auto_increment.result
+++ b/mysql-test/r/auto_increment.result
@@ -231,8 +231,7 @@ a	b
 204	7
 delete from t1 where a=0;
 update t1 set a=NULL where b=6;
-Warnings:
-Warning	1048	Column 'a' cannot be null
+ERROR 23000: Column 'a' cannot be null
 update t1 set a=300 where b=7;
 SET SQL_MODE='';
 insert into t1(a,b)values(NULL,8);
@@ -247,7 +246,7 @@ a	b
 1	1
 200	2
 201	4
-0	6
+203	6
 300	7
 301	8
 400	9
@@ -263,6 +262,7 @@ a	b
 1	1
 200	2
 201	4
+203	6
 300	7
 301	8
 400	9
@@ -273,20 +273,20 @@ a	b
 405	14
 delete from t1 where a=0;
 update t1 set a=NULL where b=13;
-Warnings:
-Warning	1048	Column 'a' cannot be null
+ERROR 23000: Column 'a' cannot be null
 update t1 set a=500 where b=14;
 select * from t1 order by b;
 a	b
 1	1
 200	2
 201	4
+203	6
 300	7
 301	8
 400	9
 401	10
 402	11
-0	13
+404	13
 500	14
 drop table t1;
 create table t1 (a bigint);
diff --git a/mysql-test/r/null.result b/mysql-test/r/null.result
index 345c9b07b98..5a2ebc37cc8 100644
--- a/mysql-test/r/null.result
+++ b/mysql-test/r/null.result
@@ -93,11 +93,9 @@ INSERT INTO t1 SET a = "", d= "2003-01-14 03:54:55";
 Warnings:
 Warning	1265	Data truncated for column 'd' at row 1
 UPDATE t1 SET d=1/NULL;
-Warnings:
-Warning	1265	Data truncated for column 'd' at row 1
+ERROR 23000: Column 'd' cannot be null
 UPDATE t1 SET d=NULL;
-Warnings:
-Warning	1048	Column 'd' cannot be null
+ERROR 23000: Column 'd' cannot be null
 INSERT INTO t1 (a) values (null);
 ERROR 23000: Column 'a' cannot be null
 INSERT INTO t1 (a) values (1/null);
@@ -132,7 +130,7 @@ Warning	1048	Column 'd' cannot be null
 Warning	1048	Column 'd' cannot be null
 select * from t1;
 a	b	c	d
-	0	0000-00-00 00:00:00	0
+	0	0000-00-00 00:00:00	2003
 	0	0000-00-00 00:00:00	0
 	0	0000-00-00 00:00:00	0
 	0	0000-00-00 00:00:00	0
diff --git a/mysql-test/r/ps_2myisam.result b/mysql-test/r/ps_2myisam.result
index fbc6781e5e7..06bfd78a351 100644
--- a/mysql-test/r/ps_2myisam.result
+++ b/mysql-test/r/ps_2myisam.result
@@ -1303,12 +1303,11 @@ a	b
 set @arg00=NULL;
 set @arg01=2;
 execute stmt1 using @arg00, @arg01;
-Warnings:
-Warning	1048	Column 'a' cannot be null
+ERROR 23000: Column 'a' cannot be null
 select a,b from t1 order by a;
 a	b
-0	two
 1	one
+2	two
 3	three
 4	four
 set @arg00=0;
diff --git a/mysql-test/r/ps_3innodb.result b/mysql-test/r/ps_3innodb.result
index fcd0b5de9a0..f56b1d37a2c 100644
--- a/mysql-test/r/ps_3innodb.result
+++ b/mysql-test/r/ps_3innodb.result
@@ -1286,12 +1286,11 @@ a	b
 set @arg00=NULL;
 set @arg01=2;
 execute stmt1 using @arg00, @arg01;
-Warnings:
-Warning	1048	Column 'a' cannot be null
+ERROR 23000: Column 'a' cannot be null
 select a,b from t1 order by a;
 a	b
-0	two
 1	one
+2	two
 3	three
 4	four
 set @arg00=0;
diff --git a/mysql-test/r/ps_4heap.result b/mysql-test/r/ps_4heap.result
index 862c0ff75c1..0c643facf72 100644
--- a/mysql-test/r/ps_4heap.result
+++ b/mysql-test/r/ps_4heap.result
@@ -1287,12 +1287,11 @@ a	b
 set @arg00=NULL;
 set @arg01=2;
 execute stmt1 using @arg00, @arg01;
-Warnings:
-Warning	1048	Column 'a' cannot be null
+ERROR 23000: Column 'a' cannot be null
 select a,b from t1 order by a;
 a	b
-0	two
 1	one
+2	two
 3	three
 4	four
 set @arg00=0;
diff --git a/mysql-test/r/ps_5merge.result b/mysql-test/r/ps_5merge.result
index 51393cc8bc3..bd3cd4ac1fc 100644
--- a/mysql-test/r/ps_5merge.result
+++ b/mysql-test/r/ps_5merge.result
@@ -1329,12 +1329,11 @@ a	b
 set @arg00=NULL;
 set @arg01=2;
 execute stmt1 using @arg00, @arg01;
-Warnings:
-Warning	1048	Column 'a' cannot be null
+ERROR 23000: Column 'a' cannot be null
 select a,b from t1 order by a;
 a	b
-0	two
 1	one
+2	two
 3	three
 4	four
 set @arg00=0;
@@ -4351,12 +4350,11 @@ a	b
 set @arg00=NULL;
 set @arg01=2;
 execute stmt1 using @arg00, @arg01;
-Warnings:
-Warning	1048	Column 'a' cannot be null
+ERROR 23000: Column 'a' cannot be null
 select a,b from t1 order by a;
 a	b
-0	two
 1	one
+2	two
 3	three
 4	four
 set @arg00=0;
diff --git a/mysql-test/r/warnings.result b/mysql-test/r/warnings.result
index 2929328a9b1..249cd583345 100644
--- a/mysql-test/r/warnings.result
+++ b/mysql-test/r/warnings.result
@@ -98,8 +98,7 @@ Warning	1265	Data truncated for column 'c' at row 1
 Warning	1265	Data truncated for column 'c' at row 2
 alter table t1 add d char(2);
 update t1 set a=NULL where a=10;
-Warnings:
-Warning	1048	Column 'a' cannot be null
+ERROR 23000: Column 'a' cannot be null
 update t1 set c='mysql ab' where c='test';
 Warnings:
 Warning	1265	Data truncated for column 'c' at row 4
diff --git a/mysql-test/suite/ndb/r/ps_7ndb.result b/mysql-test/suite/ndb/r/ps_7ndb.result
index 6e2e61bbc5e..3cbc1a0e76f 100644
--- a/mysql-test/suite/ndb/r/ps_7ndb.result
+++ b/mysql-test/suite/ndb/r/ps_7ndb.result
@@ -1286,12 +1286,11 @@ a	b
 set @arg00=NULL;
 set @arg01=2;
 execute stmt1 using @arg00, @arg01;
-Warnings:
-Warning	1048	Column 'a' cannot be null
+ERROR 23000: Column 'a' cannot be null
 select a,b from t1 order by a;
 a	b
-0	two
 1	one
+2	two
 3	three
 4	four
 set @arg00=0;
diff --git a/mysql-test/suite/rpl/r/rpl_extraColmaster_innodb.result b/mysql-test/suite/rpl/r/rpl_extraColmaster_innodb.result
index af460ded1e7..d7fadfb13e3 100644
--- a/mysql-test/suite/rpl/r/rpl_extraColmaster_innodb.result
+++ b/mysql-test/suite/rpl/r/rpl_extraColmaster_innodb.result
@@ -454,9 +454,7 @@ f1	f2	f3	f4
 update t31 set f5=555555555555555 where f3=6;
 update t31 set f2=2 where f3=2;
 update t31 set f1=NULL where f3=1;
-update t31 set f3=NULL, f27=NULL, f35='f35 new value' where f3=3;
-Warnings:
-Warning	1048	Column 'f3' cannot be null
+update t31 set f3=0, f27=NULL, f35='f35 new value' where f3=3;
 
 ** Delete from Master **
 
@@ -1594,9 +1592,7 @@ f1	f2	f3	f4
 update t31 set f5=555555555555555 where f3=6;
 update t31 set f2=2 where f3=2;
 update t31 set f1=NULL where f3=1;
-update t31 set f3=NULL, f27=NULL, f35='f35 new value' where f3=3;
-Warnings:
-Warning	1048	Column 'f3' cannot be null
+update t31 set f3=0, f27=NULL, f35='f35 new value' where f3=3;
 
 ** Delete from Master **
 
@@ -2734,9 +2730,7 @@ f1	f2	f3	f4
 update t31 set f5=555555555555555 where f3=6;
 update t31 set f2=2 where f3=2;
 update t31 set f1=NULL where f3=1;
-update t31 set f3=NULL, f27=NULL, f35='f35 new value' where f3=3;
-Warnings:
-Warning	1048	Column 'f3' cannot be null
+update t31 set f3=0, f27=NULL, f35='f35 new value' where f3=3;
 
 ** Delete from Master **
 
diff --git a/mysql-test/suite/rpl/r/rpl_extraColmaster_myisam.result b/mysql-test/suite/rpl/r/rpl_extraColmaster_myisam.result
index f0613c16825..cd2aec999db 100644
--- a/mysql-test/suite/rpl/r/rpl_extraColmaster_myisam.result
+++ b/mysql-test/suite/rpl/r/rpl_extraColmaster_myisam.result
@@ -454,9 +454,7 @@ f1	f2	f3	f4
 update t31 set f5=555555555555555 where f3=6;
 update t31 set f2=2 where f3=2;
 update t31 set f1=NULL where f3=1;
-update t31 set f3=NULL, f27=NULL, f35='f35 new value' where f3=3;
-Warnings:
-Warning	1048	Column 'f3' cannot be null
+update t31 set f3=0, f27=NULL, f35='f35 new value' where f3=3;
 
 ** Delete from Master **
 
@@ -1594,9 +1592,7 @@ f1	f2	f3	f4
 update t31 set f5=555555555555555 where f3=6;
 update t31 set f2=2 where f3=2;
 update t31 set f1=NULL where f3=1;
-update t31 set f3=NULL, f27=NULL, f35='f35 new value' where f3=3;
-Warnings:
-Warning	1048	Column 'f3' cannot be null
+update t31 set f3=0, f27=NULL, f35='f35 new value' where f3=3;
 
 ** Delete from Master **
 
@@ -2734,9 +2730,7 @@ f1	f2	f3	f4
 update t31 set f5=555555555555555 where f3=6;
 update t31 set f2=2 where f3=2;
 update t31 set f1=NULL where f3=1;
-update t31 set f3=NULL, f27=NULL, f35='f35 new value' where f3=3;
-Warnings:
-Warning	1048	Column 'f3' cannot be null
+update t31 set f3=0, f27=NULL, f35='f35 new value' where f3=3;
 
 ** Delete from Master **
 
diff --git a/mysql-test/suite/rpl/t/rpl_err_ignoredtable.test b/mysql-test/suite/rpl/t/rpl_err_ignoredtable.test
index 4e06a6a7096..a36cfb11dae 100644
--- a/mysql-test/suite/rpl/t/rpl_err_ignoredtable.test
+++ b/mysql-test/suite/rpl/t/rpl_err_ignoredtable.test
@@ -49,7 +49,7 @@ kill @id;
 drop table t2,t3;
 insert into t4 values (3),(4);
 connection master;
---error 0,1053,2013
+--error 0,1053,2013,1048
 reap;
 connection master1;
 save_master_pos;
diff --git a/mysql-test/t/auto_increment.test b/mysql-test/t/auto_increment.test
index 99e9b783d55..ff92c743960 100644
--- a/mysql-test/t/auto_increment.test
+++ b/mysql-test/t/auto_increment.test
@@ -149,6 +149,7 @@ delete from t1 where a=0;
 update t1 set a=0 where b=5;
 select * from t1 order by b;
 delete from t1 where a=0;
+--error 1048
 update t1 set a=NULL where b=6;
 update t1 set a=300 where b=7;
 SET SQL_MODE='';
@@ -164,6 +165,7 @@ delete from t1 where a=0;
 update t1 set a=0 where b=12;
 select * from t1 order by b;
 delete from t1 where a=0;
+--error 1048
 update t1 set a=NULL where b=13;
 update t1 set a=500 where b=14;
 select * from t1 order by b;
diff --git a/mysql-test/t/null.test b/mysql-test/t/null.test
index 2878b54c357..ddf6b8870fa 100644
--- a/mysql-test/t/null.test
+++ b/mysql-test/t/null.test
@@ -61,7 +61,9 @@ drop table t1;
 #
 CREATE TABLE t1 (a varchar(16) NOT NULL default '', b smallint(6) NOT NULL default 0, c datetime NOT NULL default '0000-00-00 00:00:00', d smallint(6) NOT NULL default 0);
 INSERT INTO t1 SET a = "", d= "2003-01-14 03:54:55";
+--error 1048
 UPDATE t1 SET d=1/NULL;
+--error 1048
 UPDATE t1 SET d=NULL;
 --error 1048
 INSERT INTO t1 (a) values (null);
diff --git a/mysql-test/t/warnings.test b/mysql-test/t/warnings.test
index b5bae109f5f..d0eaaf1a764 100644
--- a/mysql-test/t/warnings.test
+++ b/mysql-test/t/warnings.test
@@ -65,6 +65,7 @@ create table t1(a tinyint NOT NULL, b tinyint unsigned, c char(5));
 insert into t1 values(NULL,100,'mysql'),(10,-1,'mysql ab'),(500,256,'open source'),(20,NULL,'test');
 alter table t1 modify c char(4);
 alter table t1 add d char(2);
+--error 1048
 update t1 set a=NULL where a=10;
 update t1 set c='mysql ab' where c='test';
 update t1 set d=c;
diff --git a/sql/sql_update.cc b/sql/sql_update.cc
index ecb7acda61b..8a6c2f0f7f4 100644
--- a/sql/sql_update.cc
+++ b/sql/sql_update.cc
@@ -526,7 +526,9 @@ int mysql_update(THD *thd,
   init_read_record(&info,thd,table,select,0,1);
 
   updated= found= 0;
-  thd->count_cuted_fields= CHECK_FIELD_WARN;		/* calc cuted fields */
+  /* Generate an error when trying to set a NOT NULL field to NULL. */
+  thd->count_cuted_fields= ignore ? CHECK_FIELD_WARN
+                                  : CHECK_FIELD_ERROR_FOR_NULL;
   thd->cuted_fields=0L;
   thd->proc_info="Updating";
 
diff --git a/tests/mysql_client_test.c b/tests/mysql_client_test.c
index 9284b2182b1..3776be79399 100644
--- a/tests/mysql_client_test.c
+++ b/tests/mysql_client_test.c
@@ -15674,7 +15674,7 @@ static void test_mysql_insert_id()
   myquery(rc);
   res= mysql_insert_id(mysql);
   DIE_UNLESS(res == 0);
-  rc= mysql_query(mysql, "update t2 set f1=NULL where f1=14");
+  rc= mysql_query(mysql, "update t2 set f1=0 where f1=14");
   myquery(rc);
   res= mysql_insert_id(mysql);
   DIE_UNLESS(res == 0);

From 72ebb0aadad120b73be53d727880dd44aa6fe4ad Mon Sep 17 00:00:00 2001
From: unknown <evgen@moonbone.local>
Date: Fri, 11 Jan 2008 20:10:54 +0300
Subject: [PATCH 2/8] Bug#29477: Not all fields of the target table were
 checked to have a default value when inserting into a view.

The mysql_prepare_insert function checks all fields of the target table that
directly or indirectly (through a view) are specified in the INSERT
statement to have a default value. This check can be skipped if the INSERT
statement doesn't mention any insert fields. In case of a view this allows
fields that aren't mentioned in the view to bypass the check.

Now fields of the target table are always checked to have a default value
when insert goes into a view.


mysql-test/t/view.test:
  Added a test case for the bug#29477: Not all fields of the target table were
  checked to have a default value when inserting into a view.
mysql-test/r/view.result:
  Added a test case for the bug#29477: Not all fields of the target table were
  checked to have a default value when inserting into a view.
sql/sql_insert.cc:
  Bug#29477: Not all fields of the target table were checked to have a default
  value when inserting into a view.
  Now fields of the target table are always checked to have a default value
  when insert goes into a view.
---
 mysql-test/r/view.result | 16 ++++++++++++++++
 mysql-test/t/view.test   | 14 ++++++++++++++
 sql/sql_insert.cc        |  3 ++-
 3 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/mysql-test/r/view.result b/mysql-test/r/view.result
index 0e3d650c571..fb36304e562 100644
--- a/mysql-test/r/view.result
+++ b/mysql-test/r/view.result
@@ -3602,4 +3602,20 @@ DROP VIEW v1;
 DROP VIEW v2;
 DROP VIEW v3;
 DROP TABLE t1;
+#
+# Bug#29477: Not all fields of the target table were checked to have
+#            a default value when inserting into a view.
+#
+create table t1(f1 int, f2 int not null);
+create view v1 as select f1 from t1;
+insert into v1 values(1);
+Warnings:
+Warning	1423	Field of view 'test.v1' underlying table doesn't have a default value
+set @old_mode=@@sql_mode;
+set @@sql_mode=traditional;
+insert into v1 values(1);
+ERROR HY000: Field of view 'test.v1' underlying table doesn't have a default value
+set @@sql_mode=@old_mode;
+drop view v1;
+drop table t1;
 End of 5.0 tests.
diff --git a/mysql-test/t/view.test b/mysql-test/t/view.test
index 0faa8e7a785..340a34db5a1 100644
--- a/mysql-test/t/view.test
+++ b/mysql-test/t/view.test
@@ -3456,5 +3456,19 @@ DROP VIEW v2;
 DROP VIEW v3;
 DROP TABLE t1;
 
+--echo #
+--echo # Bug#29477: Not all fields of the target table were checked to have
+--echo #            a default value when inserting into a view.
+--echo #
+create table t1(f1 int, f2 int not null);
+create view v1 as select f1 from t1;
+insert into v1 values(1);
+set @old_mode=@@sql_mode;
+set @@sql_mode=traditional;
+--error ER_NO_DEFAULT_FOR_VIEW_FIELD
+insert into v1 values(1);
+set @@sql_mode=@old_mode;
+drop view v1;
+drop table t1;
 --echo End of 5.0 tests.
 
diff --git a/sql/sql_insert.cc b/sql/sql_insert.cc
index cf9e93b8518..14292f1cd9d 100644
--- a/sql/sql_insert.cc
+++ b/sql/sql_insert.cc
@@ -630,7 +630,8 @@ bool mysql_insert(THD *thd,TABLE_LIST *table_list,
   if (mysql_prepare_insert(thd, table_list, table, fields, values,
 			   update_fields, update_values, duplic, &unused_conds,
                            FALSE,
-                           (fields.elements || !value_count),
+                           (fields.elements || !value_count ||
+                            table_list->view != 0),
                            !ignore && (thd->variables.sql_mode &
                                        (MODE_STRICT_TRANS_TABLES |
                                         MODE_STRICT_ALL_TABLES))))

From df8e9fc234234958ee0cb6195b724657b2cd128c Mon Sep 17 00:00:00 2001
From: unknown <mhansson/martin@linux-st28.site>
Date: Fri, 11 Jan 2008 18:50:54 +0100
Subject: [PATCH 3/8] Bug#31797: error while parsing subqueries -- WHERE is
 parsed as HAVING

The name resolution for correlated subqueries and HAVING clauses
failed to distinguish which of two was being performed when there
was a reference to an outer aliased field.
Fixed by adding the condition that HAVING clause name resulotion
is being performed.


mysql-test/r/group_by.result:
  Bug#31797: Test result
mysql-test/t/group_by.test:
  Bug#31797: Test case
sql/item.cc:
  Bug#31797:
  Corrected function comment.
  The fix, raising the error is restricted to HAVING name resolution.
---
 mysql-test/r/group_by.result | 36 +++++++++++++++++++++++++++
 mysql-test/t/group_by.test   | 48 ++++++++++++++++++++++++++++++++++++
 sql/item.cc                  |  3 ++-
 3 files changed, 86 insertions(+), 1 deletion(-)

diff --git a/mysql-test/r/group_by.result b/mysql-test/r/group_by.result
index 053c2901509..97c550fcab4 100644
--- a/mysql-test/r/group_by.result
+++ b/mysql-test/r/group_by.result
@@ -1113,3 +1113,39 @@ c	b
 3	1
 3	2
 DROP TABLE t1;
+CREATE TABLE t1 ( a INT, b INT );
+SELECT b c, (SELECT a FROM t1 WHERE b = c)
+FROM t1;
+c	(SELECT a FROM t1 WHERE b = c)
+SELECT b c, (SELECT a FROM t1 WHERE b = c)
+FROM t1 
+HAVING b = 10;
+c	(SELECT a FROM t1 WHERE b = c)
+SELECT MAX(b) c, (SELECT a FROM t1 WHERE b = c)
+FROM t1 
+HAVING b = 10;
+ERROR 42S22: Reference 'c' not supported (reference to group function)
+SET @old_sql_mode = @@sql_mode;
+SET @@sql_mode='ONLY_FULL_GROUP_BY';
+SELECT b c, (SELECT a FROM t1 WHERE b = c)
+FROM t1;
+c	(SELECT a FROM t1 WHERE b = c)
+SELECT b c, (SELECT a FROM t1 WHERE b = c)
+FROM t1 
+HAVING b = 10;
+ERROR 42000: non-grouping field 'b' is used in HAVING clause
+SELECT MAX(b) c, (SELECT a FROM t1 WHERE b = c)
+FROM t1 
+HAVING b = 10;
+ERROR 42S22: Reference 'c' not supported (reference to group function)
+INSERT INTO t1 VALUES (1, 1);
+SELECT b c, (SELECT a FROM t1 WHERE b = c)
+FROM t1;
+c	(SELECT a FROM t1 WHERE b = c)
+1	1
+INSERT INTO t1 VALUES (2, 1);
+SELECT b c, (SELECT a FROM t1 WHERE b = c)
+FROM t1;
+ERROR 21000: Subquery returns more than 1 row
+DROP TABLE t1;
+SET @@sql_mode = @old_sql_mode;
diff --git a/mysql-test/t/group_by.test b/mysql-test/t/group_by.test
index b7c28cada46..c943b5eb7ed 100644
--- a/mysql-test/t/group_by.test
+++ b/mysql-test/t/group_by.test
@@ -815,3 +815,51 @@ EXPLAIN SELECT c,b   FROM t1 GROUP BY c,b;
 SELECT c,b   FROM t1 GROUP BY c,b;
 
 DROP TABLE t1;
+
+#
+# Bug #31797: error while parsing subqueries -- WHERE is parsed as HAVING
+#
+CREATE TABLE t1 ( a INT, b INT );
+
+SELECT b c, (SELECT a FROM t1 WHERE b = c)
+FROM t1;
+
+SELECT b c, (SELECT a FROM t1 WHERE b = c)
+FROM t1 
+HAVING b = 10;
+
+--error ER_ILLEGAL_REFERENCE
+SELECT MAX(b) c, (SELECT a FROM t1 WHERE b = c)
+FROM t1 
+HAVING b = 10;
+
+SET @old_sql_mode = @@sql_mode;
+SET @@sql_mode='ONLY_FULL_GROUP_BY';
+
+SELECT b c, (SELECT a FROM t1 WHERE b = c)
+FROM t1;
+
+--error ER_NON_GROUPING_FIELD_USED
+SELECT b c, (SELECT a FROM t1 WHERE b = c)
+FROM t1 
+HAVING b = 10;
+
+--error ER_ILLEGAL_REFERENCE
+SELECT MAX(b) c, (SELECT a FROM t1 WHERE b = c)
+FROM t1 
+HAVING b = 10;
+
+INSERT INTO t1 VALUES (1, 1);
+SELECT b c, (SELECT a FROM t1 WHERE b = c)
+FROM t1;
+
+INSERT INTO t1 VALUES (2, 1);
+--error ER_SUBQUERY_NO_1_ROW
+SELECT b c, (SELECT a FROM t1 WHERE b = c)
+FROM t1;
+
+DROP TABLE t1;
+SET @@sql_mode = @old_sql_mode;
+
+
+
diff --git a/sql/item.cc b/sql/item.cc
index 3177c0fb1e8..dae7694997a 100644
--- a/sql/item.cc
+++ b/sql/item.cc
@@ -3352,7 +3352,7 @@ static Item** find_field_in_group_list(Item *find_item, ORDER *group_list)
     resolve_ref_in_select_and_group()
     thd     current thread
     ref     column reference being resolved
-    select  the sub-select that ref is resolved against
+    select  the select that ref is resolved against
 
   DESCRIPTION
     Resolve a column reference (usually inside a HAVING clause) against the
@@ -3423,6 +3423,7 @@ resolve_ref_in_select_and_group(THD *thd, Item_ident *ref, SELECT_LEX *select)
   }
 
   if (thd->variables.sql_mode & MODE_ONLY_FULL_GROUP_BY &&
+      select->having_fix_field  &&
       select_ref != not_found_item && !group_by_ref)
   {
     /*

From d8363a408ffd345c45f2cd9e9427a986e56c08eb Mon Sep 17 00:00:00 2001
From: unknown <igor@olga.mysql.com>
Date: Fri, 11 Jan 2008 10:05:34 -0800
Subject: [PATCH 4/8] Fixed bug #33697. When the function
 test_if_skip_sort_order prefers index backward scan to ref access the
 corresponding access functions must be set accordingly.

mysql-test/include/mix1.inc:
  Added a test case for bug #33697.
  Corrected one previous bad merge.
mysql-test/r/innodb_mysql.result:
  Added a test case for bug #33697.
mysql-test/t/disabled.def:
  Turned innodb_mysql test on.
---
 mysql-test/include/mix1.inc      | 55 ++++++++++++++++++++++++--------
 mysql-test/r/innodb_mysql.result | 27 +++++++++++++++-
 mysql-test/t/disabled.def        |  1 -
 sql/sql_select.cc                |  3 +-
 4 files changed, 69 insertions(+), 17 deletions(-)

diff --git a/mysql-test/include/mix1.inc b/mysql-test/include/mix1.inc
index 3005e67935b..703dfa44df0 100644
--- a/mysql-test/include/mix1.inc
+++ b/mysql-test/include/mix1.inc
@@ -723,20 +723,6 @@ set @@sort_buffer_size=default;
 
 DROP TABLE t1,t2;
 
-#
-# Bug #32815: query with ORDER BY and a possible ref_or_null access
-#
-
-CREATE TABLE t1 (id int, type char(6), d int, INDEX idx(id,d)) ENGINE=InnoDB;
-INSERT INTO t1 VALUES 
-  (191, 'member', 1), (NULL, 'member', 3), (NULL, 'member', 4), (201, 'member', 2); 
-
-EXPLAIN SELECT * FROM t1 WHERE id=191 OR id IS NULL ORDER BY d;
-SELECT * FROM t1 WHERE id=191 OR id IS NULL ORDER BY d;
-
-DROP TABLE t1;
-
-
 # Test of behaviour with CREATE ... SELECT
 #
 
@@ -1091,6 +1077,19 @@ desc t1;
 show create table t1;
 drop table t1;
 
+#
+# Bug #32815: query with ORDER BY and a possible ref_or_null access
+#
+
+CREATE TABLE t1 (id int, type char(6), d int, INDEX idx(id,d)) ENGINE=InnoDB;
+INSERT INTO t1 VALUES 
+  (191, 'member', 1), (NULL, 'member', 3), (NULL, 'member', 4), (201, 'member', 2); 
+
+EXPLAIN SELECT * FROM t1 WHERE id=191 OR id IS NULL ORDER BY d;
+SELECT * FROM t1 WHERE id=191 OR id IS NULL ORDER BY d;
+
+DROP TABLE t1;
+
 --echo End of 5.0 tests
 
 # Fix for BUG#19243 "wrong LAST_INSERT_ID() after ON DUPLICATE KEY
@@ -1383,4 +1382,32 @@ create table t1 (a int auto_increment primary key) engine=innodb;
 alter table t1 order by a;
 drop table t1;
 
+#
+# Bug #33697: ORDER BY primary key DESC vs. ref access + filesort
+# (reproduced only with InnoDB tables)
+#
+
+CREATE TABLE t1
+  (vid integer NOT NULL,
+   tid integer NOT NULL,
+   idx integer NOT NULL,
+   name varchar(128) NOT NULL,
+   type varchar(128) NULL,
+   PRIMARY KEY(idx, vid, tid),
+   UNIQUE(vid, tid, name)
+) ENGINE=InnoDB;
+
+INSERT INTO t1 VALUES
+  (1,1,1,'pk',NULL),(2,1,1,'pk',NULL),(3,1,1,'pk',NULL),(4,1,1,'c1',NULL),
+  (5,1,1,'pk',NULL),(1,1,2,'c1',NULL),(2,1,2,'c1',NULL),(3,1,2,'c1',NULL),
+  (4,1,2,'c2',NULL),(5,1,2,'c1',NULL),(2,1,3,'c2',NULL),(3,1,3,'c2',NULL),
+  (4,1,3,'pk',NULL),(5,1,3,'c2',NULL),
+  (2,1,4,'c_extra',NULL),(3,1,4,'c_extra',NULL);
+
+EXPLAIN SELECT * FROM t1 WHERE tid = 1 AND vid = 3 ORDER BY idx DESC;
+
+SELECT * FROM t1 WHERE tid = 1 AND vid = 3 ORDER BY idx DESC;
+
+DROP TABLE t1;
+
 --echo End of 5.1 tests
diff --git a/mysql-test/r/innodb_mysql.result b/mysql-test/r/innodb_mysql.result
index 87cf1acc10c..e9f00a667c0 100644
--- a/mysql-test/r/innodb_mysql.result
+++ b/mysql-test/r/innodb_mysql.result
@@ -1349,7 +1349,7 @@ INSERT INTO t1 VALUES
 (191, 'member', 1), (NULL, 'member', 3), (NULL, 'member', 4), (201, 'member', 2);
 EXPLAIN SELECT * FROM t1 WHERE id=191 OR id IS NULL ORDER BY d;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	ALL	idx	NULL	NULL	NULL	3	Using where; Using filesort
+1	SIMPLE	t1	ALL	idx	NULL	NULL	NULL	4	Using where; Using filesort
 SELECT * FROM t1 WHERE id=191 OR id IS NULL ORDER BY d;
 id	type	d
 191	member	1
@@ -1609,4 +1609,29 @@ alter table t1 order by a;
 Warnings:
 Warning	1105	ORDER BY ignored as there is a user-defined clustered index in the table 't1'
 drop table t1;
+CREATE TABLE t1
+(vid integer NOT NULL,
+tid integer NOT NULL,
+idx integer NOT NULL,
+name varchar(128) NOT NULL,
+type varchar(128) NULL,
+PRIMARY KEY(idx, vid, tid),
+UNIQUE(vid, tid, name)
+) ENGINE=InnoDB;
+INSERT INTO t1 VALUES
+(1,1,1,'pk',NULL),(2,1,1,'pk',NULL),(3,1,1,'pk',NULL),(4,1,1,'c1',NULL),
+(5,1,1,'pk',NULL),(1,1,2,'c1',NULL),(2,1,2,'c1',NULL),(3,1,2,'c1',NULL),
+(4,1,2,'c2',NULL),(5,1,2,'c1',NULL),(2,1,3,'c2',NULL),(3,1,3,'c2',NULL),
+(4,1,3,'pk',NULL),(5,1,3,'c2',NULL),
+(2,1,4,'c_extra',NULL),(3,1,4,'c_extra',NULL);
+EXPLAIN SELECT * FROM t1 WHERE tid = 1 AND vid = 3 ORDER BY idx DESC;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	SIMPLE	t1	index	vid	PRIMARY	12	NULL	16	Using where
+SELECT * FROM t1 WHERE tid = 1 AND vid = 3 ORDER BY idx DESC;
+vid	tid	idx	name	type
+3	1	4	c_extra	NULL
+3	1	3	c2	NULL
+3	1	2	c1	NULL
+3	1	1	pk	NULL
+DROP TABLE t1;
 End of 5.1 tests
diff --git a/mysql-test/t/disabled.def b/mysql-test/t/disabled.def
index 2c16017241c..b7ebf332d75 100644
--- a/mysql-test/t/disabled.def
+++ b/mysql-test/t/disabled.def
@@ -18,7 +18,6 @@ federated_transactions   : Bug#29523 Transactions do not work
 events                   : Bug#32664 events.test fails randomly
 lowercase_table3         : Bug#32667 lowercase_table3.test reports to error log
 kill                 : Bug#29149: Test "kill" fails on Windows
-innodb_mysql         : Bug#32724: innodb_mysql.test fails randomly
 wait_timeout         : Bug#32801 wait_timeout.test fails randomly
 kill                 : Bug#29149 Test "kill" fails on Windows
 ctype_create         : Bug#32965 main.ctype_create fails
diff --git a/sql/sql_select.cc b/sql/sql_select.cc
index 741e8afec1d..741132d2b9c 100644
--- a/sql/sql_select.cc
+++ b/sql/sql_select.cc
@@ -13123,7 +13123,8 @@ check_reverse_order:
 	select->quick=tmp;
       }
     }
-    else if (tab->ref.key >= 0 && tab->ref.key_parts <= used_key_parts)
+    else if (tab->type != JT_NEXT && 
+             tab->ref.key >= 0 && tab->ref.key_parts <= used_key_parts)
     {
       /*
 	SELECT * FROM t1 WHERE a=1 ORDER BY a DESC,b DESC

From f9440588f5bfbb4aef8934dbbda28f3e8872476f Mon Sep 17 00:00:00 2001
From: unknown <mhansson/martin@linux-st28.site>
Date: Mon, 14 Jan 2008 16:16:36 +0100
Subject: [PATCH 5/8] Bug#33143: Incorrect ORDER BY for ROUND()/TRUNCATE()
 result

The ROUND(X, D) function would change the Item::decimals field during
execution to achieve the effect of a dynamic number of decimal digits.
This caused a series of bugs:
Bug #30617:Round() function not working under some circumstances in InnoDB
Bug #33402:ROUND with decimal and non-constant cannot round to 0 decimal places
Bug #30889:filesort and order by with float/numeric crashes server
Fixed by never changing the number of shown digits for DECIMAL when
used with a nonconstant number of decimal digits.


mysql-test/r/type_decimal.result:
  Bug#33143: Test result
mysql-test/t/type_decimal.test:
  Bug#33143: Test case
sql/item_func.cc:
  Bug#33143:
  - Moved the DECIMAL_MAX_SCALE limitation to fix_length_and_dec.
  - Removed resetting of Item::decimals field.
  - set the frac field of the output value to current scale.
strings/decimal.c:
  Bug#33143: It is necessary to set all digits in the buffer following the
  rounded one to zero, as they may now be displayed.
---
 mysql-test/r/type_decimal.result | 63 +++++++++++++++++++++++++++++++-
 mysql-test/t/type_decimal.test   | 43 ++++++++++++++++++++++
 sql/item_func.cc                 | 12 +++---
 strings/decimal.c                | 16 +++++++-
 4 files changed, 125 insertions(+), 9 deletions(-)

diff --git a/mysql-test/r/type_decimal.result b/mysql-test/r/type_decimal.result
index 72f827f11ed..5146c976659 100644
--- a/mysql-test/r/type_decimal.result
+++ b/mysql-test/r/type_decimal.result
@@ -797,7 +797,7 @@ dps tinyint(3) unsigned default NULL
 INSERT INTO t1 VALUES (1.1325,3);
 SELECT ROUND(qty,3), dps, ROUND(qty,dps) FROM t1;
 ROUND(qty,3)	dps	ROUND(qty,dps)
-1.133	3	1.133
+1.133	3	1.133000
 DROP TABLE t1;
 SELECT 1 % .123456789123456789123456789123456789123456789123456789123456789123456789123456789 AS '%';
 %
@@ -805,3 +805,64 @@ SELECT 1 % .12345678912345678912345678912345678912345678912345678912345678912345
 SELECT MOD(1, .123456789123456789123456789123456789123456789123456789123456789123456789123456789) AS 'MOD()';
 MOD()
 0.012345687012345687012345687012345687012345687012345687012345687012345687000000000
+CREATE TABLE t1( a DECIMAL(4, 3), b INT );
+INSERT INTO t1 VALUES ( 1, 5 ), ( 2, 4 ), ( 3, 3 ), ( 4, 2 ), ( 5, 1 );
+SELECT a, b, ROUND( a, b ) AS c FROM t1 ORDER BY c;
+a	b	c
+1.000	5	1.000
+2.000	4	2.000
+3.000	3	3.000
+4.000	2	4.000
+5.000	1	5.000
+SELECT a, b, ROUND( a, b ) AS c FROM t1 ORDER BY c DESC;
+a	b	c
+5.000	1	5.000
+4.000	2	4.000
+3.000	3	3.000
+2.000	4	2.000
+1.000	5	1.000
+CREATE TABLE t2 ( a INT, b INT, c DECIMAL(5, 4) );
+INSERT INTO t2 VALUES ( 0, 1, 1.2345 ), ( 1, 2, 1.2345 ),
+( 3, 3, 1.2345 ), ( 2, 4, 1.2345 );
+SELECT a, b, MAX(ROUND(c, a)) 
+FROM t2 
+GROUP BY a, b 
+ORDER BY b;
+a	b	MAX(ROUND(c, a))
+0	1	1.0000
+1	2	1.2000
+3	3	1.2350
+2	4	1.2300
+SELECT a, b, ROUND(c, a) 
+FROM t2;
+a	b	ROUND(c, a)
+0	1	1.0000
+1	2	1.2000
+3	3	1.2350
+2	4	1.2300
+CREATE TABLE t3( a INT, b DECIMAL(6, 3) );
+INSERT INTO t3 VALUES( 0, 1.5 );
+SELECT ROUND( b, a ) FROM t3;
+ROUND( b, a )
+2.000
+CREATE TABLE t4( a INT, b DECIMAL( 12, 0) );
+INSERT INTO t4 VALUES( -9, 1.5e9 );
+SELECT ROUND( b, a ) FROM t4;
+ROUND( b, a )
+2000000000
+CREATE TABLE t5( a INT, b DECIMAL( 13, 12 ) );
+INSERT INTO t5 VALUES( 0, 1.5 );
+INSERT INTO t5 VALUES( 9, 1.5e-9 );
+SELECT ROUND( b, a ) FROM t5;
+ROUND( b, a )
+2.000000000000
+0.000000002000
+CREATE TABLE t6( a INT );
+INSERT INTO t6 VALUES( 6 / 8 );
+SELECT * FROM t6;
+a
+1
+SELECT ROUND(20061108085411.000002);
+ROUND(20061108085411.000002)
+20061108085411
+DROP TABLE t1, t2, t3, t4, t5, t6;
diff --git a/mysql-test/t/type_decimal.test b/mysql-test/t/type_decimal.test
index c154b2685dd..5e6f2b5a091 100644
--- a/mysql-test/t/type_decimal.test
+++ b/mysql-test/t/type_decimal.test
@@ -416,3 +416,46 @@ DROP TABLE t1;
 
 SELECT 1 % .123456789123456789123456789123456789123456789123456789123456789123456789123456789 AS '%';
 SELECT MOD(1, .123456789123456789123456789123456789123456789123456789123456789123456789123456789) AS 'MOD()';
+
+#
+# Bug #33143: Incorrect ORDER BY for ROUND()/TRUNCATE() result
+#
+
+CREATE TABLE t1( a DECIMAL(4, 3), b INT );
+INSERT INTO t1 VALUES ( 1, 5 ), ( 2, 4 ), ( 3, 3 ), ( 4, 2 ), ( 5, 1 );
+SELECT a, b, ROUND( a, b ) AS c FROM t1 ORDER BY c;
+SELECT a, b, ROUND( a, b ) AS c FROM t1 ORDER BY c DESC;
+
+CREATE TABLE t2 ( a INT, b INT, c DECIMAL(5, 4) );
+
+INSERT INTO t2 VALUES ( 0, 1, 1.2345 ), ( 1, 2, 1.2345 ),
+                      ( 3, 3, 1.2345 ), ( 2, 4, 1.2345 );
+
+SELECT a, b, MAX(ROUND(c, a)) 
+FROM t2 
+GROUP BY a, b 
+ORDER BY b;
+
+SELECT a, b, ROUND(c, a) 
+FROM t2;
+
+CREATE TABLE t3( a INT, b DECIMAL(6, 3) );
+INSERT INTO t3 VALUES( 0, 1.5 );
+SELECT ROUND( b, a ) FROM t3;
+
+CREATE TABLE t4( a INT, b DECIMAL( 12, 0) );
+INSERT INTO t4 VALUES( -9, 1.5e9 );
+SELECT ROUND( b, a ) FROM t4;
+
+CREATE TABLE t5( a INT, b DECIMAL( 13, 12 ) );
+INSERT INTO t5 VALUES( 0, 1.5 );
+INSERT INTO t5 VALUES( 9, 1.5e-9 );
+SELECT ROUND( b, a ) FROM t5;
+
+CREATE TABLE t6( a INT );
+INSERT INTO t6 VALUES( 6 / 8 );
+SELECT * FROM t6;
+
+SELECT ROUND(20061108085411.000002);
+
+DROP TABLE t1, t2, t3, t4, t5, t6;
diff --git a/sql/item_func.cc b/sql/item_func.cc
index 96326ee2728..443360625d0 100644
--- a/sql/item_func.cc
+++ b/sql/item_func.cc
@@ -2008,7 +2008,7 @@ void Item_func_round::fix_length_and_dec()
     int length_increase= ((decimals_delta <= 0) || truncate) ? 0:1;
 
     precision-= decimals_delta - length_increase;
-    decimals= decimals_to_set;
+    decimals= min(decimals_to_set, DECIMAL_MAX_SCALE);
     max_length= my_decimal_precision_to_length(precision, decimals,
                                                unsigned_flag);
     break;
@@ -2107,18 +2107,18 @@ my_decimal *Item_func_round::decimal_op(my_decimal *decimal_value)
 {
   my_decimal val, *value= args[0]->val_decimal(&val);
   longlong dec= args[1]->val_int();
-  if (dec > 0 || (dec < 0 && args[1]->unsigned_flag))
-  {
+  if (dec >= 0 || args[1]->unsigned_flag)
     dec= min((ulonglong) dec, DECIMAL_MAX_SCALE);
-    decimals= (uint8) dec; // to get correct output
-  }
   else if (dec < INT_MIN)
     dec= INT_MIN;
     
   if (!(null_value= (args[0]->null_value || args[1]->null_value ||
                      my_decimal_round(E_DEC_FATAL_ERROR, value, (int) dec,
-                                      truncate, decimal_value) > 1)))
+                                      truncate, decimal_value) > 1))) 
+  {
+    decimal_value->frac= decimals;
     return decimal_value;
+  }
   return 0;
 }
 
diff --git a/strings/decimal.c b/strings/decimal.c
index cbea0e340c6..3176cf6afa7 100644
--- a/strings/decimal.c
+++ b/strings/decimal.c
@@ -1595,9 +1595,21 @@ decimal_round(decimal_t *from, decimal_t *to, int scale,
       x+=10;
     *buf1=powers10[pos]*(x-y);
   }
-  if (frac0 < 0)
+  /*
+    In case we're rounding e.g. 1.5e9 to 2.0e9, the decimal_digit_t's inside
+    the buffer are as follows.
+
+    Before <1, 5e8>
+    After  <2, 5e8>
+
+    Hence we need to set the 2nd field to 0.
+    The same holds if we round 1.5e-9 to 2e-9.
+   */
+  if (frac0 < frac1)
   {
-    dec1 *end=to->buf+intg0, *buf=buf1+1;
+    dec1 *buf= to->buf + ((scale == 0 && intg0 == 0) ? 1 : intg0 + frac0);
+    dec1 *end= to->buf + len;
+
     while (buf < end)
       *buf++=0;
   }

From c71a6428d822f60932e5cea0d395a06d777d2b9c Mon Sep 17 00:00:00 2001
From: unknown <sergefp@mysql.com>
Date: Fri, 18 Jan 2008 22:50:36 +0300
Subject: [PATCH 6/8] BUG#33794 "MySQL crashes executing specific query": The
 problem occurred when one had a subquery that had an equality X=Y where Y
 referred to a named select list expression from the parent select. MySQL
 crashed when trying to use the X=Y equality for ref-based access.

Fixed by allowing non-Item_field items in the described case.


mysql-test/r/subselect.result:
  BUG#33794 "MySQL crashes executing specific query"
  - Testcase
mysql-test/t/subselect.test:
  BUG#33794 "MySQL crashes executing specific query"
  - Testcase
sql/sql_select.cc:
  BUG#33794 "MySQL crashes executing specific query"
  get_store_key() assumed that if it got a reference
    t.key=Item_outer_ref(Item_direct_ref(x))
  then x was an Item_field object, which is not the case when one refers to a
  named select list expression out ot subquery.
---
 mysql-test/r/subselect.result | 48 ++++++++++++++++++++++++++++++
 mysql-test/t/subselect.test   | 55 +++++++++++++++++++++++++++++++++++
 sql/sql_select.cc             |  3 +-
 3 files changed, 105 insertions(+), 1 deletion(-)

diff --git a/mysql-test/r/subselect.result b/mysql-test/r/subselect.result
index 75df77b0790..05acbe6457a 100644
--- a/mysql-test/r/subselect.result
+++ b/mysql-test/r/subselect.result
@@ -4392,4 +4392,52 @@ select count(*) from t1 where f12 =
 count(*)
 3
 drop table t1,t2;
+CREATE TABLE t4 (
+f7 varchar(32) collate utf8_bin NOT NULL default '',
+f10 varchar(32) collate utf8_bin default NULL,
+PRIMARY KEY  (f7)
+);
+INSERT INTO t4 VALUES(1,1), (2,null);
+CREATE TABLE t2 (
+f4 varchar(32) collate utf8_bin NOT NULL default '',
+f2 varchar(50) collate utf8_bin default NULL,
+f3 varchar(10) collate utf8_bin default NULL,
+PRIMARY KEY  (f4),
+UNIQUE KEY uk1 (f2)
+);
+INSERT INTO t2 VALUES(1,1,null), (2,2,null);
+CREATE TABLE t1 (
+f8 varchar(32) collate utf8_bin NOT NULL default '',
+f1 varchar(10) collate utf8_bin default NULL,
+f9 varchar(32) collate utf8_bin default NULL,
+PRIMARY KEY  (f8)
+);
+INSERT INTO t1 VALUES (1,'P',1), (2,'P',1), (3,'R',2);
+CREATE TABLE t3 (
+f6 varchar(32) collate utf8_bin NOT NULL default '',
+f5 varchar(50) collate utf8_bin default NULL,
+PRIMARY KEY (f6)
+);
+INSERT INTO t3 VALUES (1,null), (2,null);
+SELECT
+IF(t1.f1 = 'R', a1.f2, t2.f2) AS a4,
+IF(t1.f1 = 'R', a1.f3, t2.f3) AS f3,
+SUM(
+IF(
+(SELECT VPC.f2
+FROM t2 VPC, t4 a2, t2 a3
+WHERE
+VPC.f4 = a2.f10 AND a3.f2 = a4
+LIMIT 1) IS NULL, 
+0, 
+t3.f5
+)
+) AS a6
+FROM 
+t2, t3, t1 JOIN t2 a1 ON t1.f9 = a1.f4
+GROUP BY a4;
+a4	f3	a6
+1	NULL	NULL
+2	NULL	NULL
+DROP TABLE t1, t2;
 End of 5.0 tests.
diff --git a/mysql-test/t/subselect.test b/mysql-test/t/subselect.test
index 88e4f683e9e..509f0969ca4 100644
--- a/mysql-test/t/subselect.test
+++ b/mysql-test/t/subselect.test
@@ -3252,4 +3252,59 @@ select count(*) from t1 where f12 =
 (select f22 from t2 where f22 = f12 order by f21 desc, f22, f23 limit 1);
 
 drop table t1,t2;
+
+#
+# BUG#33794 "MySQL crashes executing specific query on specific dump"
+#
+CREATE TABLE t4 (
+  f7 varchar(32) collate utf8_bin NOT NULL default '',
+  f10 varchar(32) collate utf8_bin default NULL,
+  PRIMARY KEY  (f7)
+);
+INSERT INTO t4 VALUES(1,1), (2,null);
+
+CREATE TABLE t2 (
+  f4 varchar(32) collate utf8_bin NOT NULL default '',
+  f2 varchar(50) collate utf8_bin default NULL,
+  f3 varchar(10) collate utf8_bin default NULL,
+  PRIMARY KEY  (f4),
+  UNIQUE KEY uk1 (f2)
+);
+INSERT INTO t2 VALUES(1,1,null), (2,2,null);
+
+CREATE TABLE t1 (
+  f8 varchar(32) collate utf8_bin NOT NULL default '',
+  f1 varchar(10) collate utf8_bin default NULL,
+  f9 varchar(32) collate utf8_bin default NULL,
+  PRIMARY KEY  (f8)
+);
+INSERT INTO t1 VALUES (1,'P',1), (2,'P',1), (3,'R',2);
+
+CREATE TABLE t3 (
+  f6 varchar(32) collate utf8_bin NOT NULL default '',
+  f5 varchar(50) collate utf8_bin default NULL,
+  PRIMARY KEY (f6)
+);
+INSERT INTO t3 VALUES (1,null), (2,null);
+
+SELECT
+  IF(t1.f1 = 'R', a1.f2, t2.f2) AS a4,
+  IF(t1.f1 = 'R', a1.f3, t2.f3) AS f3,
+  SUM(
+    IF(
+      (SELECT VPC.f2
+       FROM t2 VPC, t4 a2, t2 a3
+       WHERE
+         VPC.f4 = a2.f10 AND a3.f2 = a4
+       LIMIT 1) IS NULL, 
+       0, 
+       t3.f5
+    )
+  ) AS a6
+FROM 
+  t2, t3, t1 JOIN t2 a1 ON t1.f9 = a1.f4
+GROUP BY a4;
+
+DROP TABLE t1, t2;
 --echo End of 5.0 tests.
+
diff --git a/sql/sql_select.cc b/sql/sql_select.cc
index 17b6a4a44ab..87935b5548f 100644
--- a/sql/sql_select.cc
+++ b/sql/sql_select.cc
@@ -5379,7 +5379,8 @@ get_store_key(THD *thd, KEYUSE *keyuse, table_map used_tables,
            (keyuse->val->type() == Item::REF_ITEM &&
             ((Item_ref*)keyuse->val)->ref_type() == Item_ref::OUTER_REF &&
             (*(Item_ref**)((Item_ref*)keyuse->val)->ref)->ref_type() ==
-             Item_ref::DIRECT_REF) )
+             Item_ref::DIRECT_REF && 
+            keyuse->val->real_item()->type() == Item::FIELD_ITEM))
     return new store_key_field(thd,
 			       key_part->field,
 			       key_buff + maybe_null,

From 80857e0d74b77eb24d620e391b7ead82dd63a394 Mon Sep 17 00:00:00 2001
From: unknown <kaa@kaamos.(none)>
Date: Sat, 19 Jan 2008 22:13:42 +0300
Subject: [PATCH 7/8] Post-merge fixes.

mysql-test/r/group_by.result:
  Post-merge fix after merging 5.0-opt to 5.1-opt.
mysql-test/t/disabled.def:
  Disabled innodb_mysql back, bug #32724 is still not fixed.
---
 mysql-test/r/group_by.result | 36 ------------------------------------
 mysql-test/t/disabled.def    |  1 +
 2 files changed, 1 insertion(+), 36 deletions(-)

diff --git a/mysql-test/r/group_by.result b/mysql-test/r/group_by.result
index ea50b38ff25..268f290ddca 100644
--- a/mysql-test/r/group_by.result
+++ b/mysql-test/r/group_by.result
@@ -1128,42 +1128,6 @@ id	c1	c2
 4	2	3
 1	5	1
 DROP TABLE t1;
-CREATE TABLE t1 ( a INT, b INT );
-SELECT b c, (SELECT a FROM t1 WHERE b = c)
-FROM t1;
-c	(SELECT a FROM t1 WHERE b = c)
-SELECT b c, (SELECT a FROM t1 WHERE b = c)
-FROM t1 
-HAVING b = 10;
-c	(SELECT a FROM t1 WHERE b = c)
-SELECT MAX(b) c, (SELECT a FROM t1 WHERE b = c)
-FROM t1 
-HAVING b = 10;
-ERROR 42S22: Reference 'c' not supported (reference to group function)
-SET @old_sql_mode = @@sql_mode;
-SET @@sql_mode='ONLY_FULL_GROUP_BY';
-SELECT b c, (SELECT a FROM t1 WHERE b = c)
-FROM t1;
-c	(SELECT a FROM t1 WHERE b = c)
-SELECT b c, (SELECT a FROM t1 WHERE b = c)
-FROM t1 
-HAVING b = 10;
-ERROR 42000: non-grouping field 'b' is used in HAVING clause
-SELECT MAX(b) c, (SELECT a FROM t1 WHERE b = c)
-FROM t1 
-HAVING b = 10;
-ERROR 42S22: Reference 'c' not supported (reference to group function)
-INSERT INTO t1 VALUES (1, 1);
-SELECT b c, (SELECT a FROM t1 WHERE b = c)
-FROM t1;
-c	(SELECT a FROM t1 WHERE b = c)
-1	1
-INSERT INTO t1 VALUES (2, 1);
-SELECT b c, (SELECT a FROM t1 WHERE b = c)
-FROM t1;
-ERROR 21000: Subquery returns more than 1 row
-DROP TABLE t1;
-SET @@sql_mode = @old_sql_mode;
 End of 5.0 tests
 CREATE TABLE t1 (a INT, b INT,
 PRIMARY KEY (a),
diff --git a/mysql-test/t/disabled.def b/mysql-test/t/disabled.def
index 286a7b06ea6..3f9ec52ca36 100644
--- a/mysql-test/t/disabled.def
+++ b/mysql-test/t/disabled.def
@@ -18,6 +18,7 @@ federated_transactions   : Bug#29523 Transactions do not work
 events                   : Bug#32664 events.test fails randomly
 lowercase_table3         : Bug#32667 lowercase_table3.test reports to error log
 kill                 : Bug#29149: Test "kill" fails on Windows
+innodb_mysql         : Bug#32724: innodb_mysql.test fails randomly
 wait_timeout         : Bug#32801 wait_timeout.test fails randomly
 ctype_create         : Bug#32965 main.ctype_create fails
 status               : Bug#32966 main.status fails

From f80b593d21296245970460d5b95e240a1783afcf Mon Sep 17 00:00:00 2001
From: unknown <kaa@kaamos.(none)>
Date: Sun, 20 Jan 2008 13:46:55 +0300
Subject: [PATCH 8/8] Fixed a PushBuild failure of the system_mysql_db test
 caused by the lack of cleanups in the test case for bug33794.

---
 mysql-test/r/subselect.result | 2 +-
 mysql-test/t/subselect.test   | 3 ++-
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/mysql-test/r/subselect.result b/mysql-test/r/subselect.result
index 05acbe6457a..527c45671f4 100644
--- a/mysql-test/r/subselect.result
+++ b/mysql-test/r/subselect.result
@@ -4439,5 +4439,5 @@ GROUP BY a4;
 a4	f3	a6
 1	NULL	NULL
 2	NULL	NULL
-DROP TABLE t1, t2;
+DROP TABLE t1, t2, t3, t4;
 End of 5.0 tests.
diff --git a/mysql-test/t/subselect.test b/mysql-test/t/subselect.test
index 509f0969ca4..326d80f84c1 100644
--- a/mysql-test/t/subselect.test
+++ b/mysql-test/t/subselect.test
@@ -3305,6 +3305,7 @@ FROM
   t2, t3, t1 JOIN t2 a1 ON t1.f9 = a1.f4
 GROUP BY a4;
 
-DROP TABLE t1, t2;
+DROP TABLE t1, t2, t3, t4;
+
 --echo End of 5.0 tests.