Below is the list of changes that have just been committed into a local
5.0 repository of evgen. When evgen does a push these changes will
be propagated to the main repository and, within 24 hours after the
push, to the public repository.
For information on how to access the public repository
see http://dev.mysql.com/doc/mysql/en/installing-source-tree.html
ChangeSet@stripped, 2006-12-14 23:40:50+03:00, evgen@stripped +4 -0
Fixed bug#16861: User defined variable can have a wrong value if a tmp table was
used.
The Item::save_in_field() function is called from fill_record() to the
new row with a data while the CREATE TABLE ... SELECT statement.
Item::save_in_field() calls val_xxx() methods in order to get values.
val_xxx() methods do not take into account the result field. Due to this
Item_func_set_user_var object returns values from the original table,
not from a temporary one.
The save_in_field() member function is added to the Item_func_set_user_var
class. It detects when the result field should be used and properly updates
the value of the user variable.
mysql-test/r/user_var.result@stripped, 2006-12-14 23:33:01+03:00, evgen@stripped +8 -1
Extended the test case for bug#18681: User defined variable can have a wrong value if a tmp table was used.
mysql-test/t/user_var.test@stripped, 2006-12-14 23:33:06+03:00, evgen@stripped +4 -1
Extended the test case for bug#18681: User defined variable can have a wrong value if a tmp table was
used.
sql/item_func.cc@stripped, 2006-12-14 23:32:47+03:00, evgen@stripped +99 -0
Bug#16861: User defined variable can have a wrong value if a tmp table was used.
Added the save_in_field() member function to the Item_func_set_user_var class.
sql/item_func.h@stripped, 2006-12-14 23:32:56+03:00, evgen@stripped +1 -0
Bug#16861: User defined variable can have a wrong value if a tmp table was used.
Added the save_in_field() member function to the Item_func_set_user_var class.
# This is a BitKeeper patch. What follows are the unified diffs for the
# set of deltas contained in the patch. The rest of the patch, the part
# that BitKeeper cares about, is below these diffs.
# User: evgen
# Host: moonbone.local
# Root: /work/16861-bug-5.0-opt-mysql
--- 1.315/sql/item_func.cc 2006-12-04 15:25:41 +03:00
+++ 1.316/sql/item_func.cc 2006-12-14 23:32:47 +03:00
@@ -4040,6 +4040,105 @@
Item::make_field(tmp_field);
}
+
+/*
+ Save the value of a user variable into a field
+
+ SYNOPSIS
+ save_in_field()
+ field target field to save the value to
+ no_conversion flag indicating whether a conversions are allowed
+
+ DESCRIPTION
+ Save the function value into a field and update the user variable
+ accordingly. If a result field is define and the target field doesn't
+ coincide with it then the value from the result field will be used as
+ the new value of the user variable.
+
+ The reason to have this method rather than simply using the result
+ field in the val_xxx() methods is that the value from the result field
+ not always can be used when the result field is defined.
+ Let's consider the following examples:
+ 1) when filling a tmp table the result field is defined but the value of it
+ is undefined because it has to be produced yet. Thus we can't use it.
+ 2) on execution of an INSERT ... SELECT statement the save_in_field()
+ function will be called to fill the data in the new record. If the SELECT
+ part uses a tmp table then the result field is defined and should be
+ used in order to get a correct result.
+
+ The difference between the SET_USER_VAR function and regular functions
+ like CONCAT is that the Item_func objects for the regular functions are
+ replaced by Item_field objects after the values of these functions have
+ been stored in a tmp table. Yet an object of the Item_field class cannot
+ be used to update a user variable.
+ Due to this we have to handle the result field in a special way here and
+ in the Item_func_set_user_var::send() function.
+
+ RETURN VALUES
+ FALSE Ok
+ TRUE Error
+*/
+
+int Item_func_set_user_var::save_in_field(Field *field, bool no_conversions)
+{
+ bool use_result_field= (result_field && result_field != field);
+ int error;
+
+ /* Update the value of the user variable */
+ check(use_result_field);
+ update();
+
+ if (result_type() == STRING_RESULT ||
+ result_type() == REAL_RESULT &&
+ field->result_type() == STRING_RESULT)
+ {
+ String *result;
+ CHARSET_INFO *cs= collation.collation;
+ char buff[MAX_FIELD_WIDTH]; // Alloc buffer for small columns
+ str_value.set_quick(buff, sizeof(buff), cs);
+ result= entry->val_str(&null_value, &str_value, decimals);
+
+ if (null_value)
+ {
+ str_value.set_quick(0, 0, cs);
+ return set_field_to_null_with_conversions(field, no_conversions);
+ }
+
+ /* NOTE: If null_value == FALSE, "result" must be not NULL. */
+
+ field->set_notnull();
+ error=field->store(result->ptr(),result->length(),cs);
+ str_value.set_quick(0, 0, cs);
+ }
+ else if (result_type() == REAL_RESULT)
+ {
+ double nr= entry->val_real(&null_value);
+ if (null_value)
+ return set_field_to_null(field);
+ field->set_notnull();
+ error=field->store(nr);
+ }
+ else if (result_type() == DECIMAL_RESULT)
+ {
+ my_decimal decimal_value;
+ my_decimal *value= entry->val_decimal(&null_value, &decimal_value);
+ if (null_value)
+ return set_field_to_null(field);
+ field->set_notnull();
+ error=field->store_decimal(value);
+ }
+ else
+ {
+ longlong nr= entry->val_int(&null_value);
+ if (null_value)
+ return set_field_to_null_with_conversions(field, no_conversions);
+ field->set_notnull();
+ error=field->store(nr, unsigned_flag);
+ }
+ return error;
+}
+
+
String *
Item_func_get_user_var::val_str(String *str)
{
--- 1.160/sql/item_func.h 2006-11-28 16:47:47 +03:00
+++ 1.161/sql/item_func.h 2006-12-14 23:32:56 +03:00
@@ -1189,6 +1189,7 @@
void print(String *str);
void print_as_stmt(String *str);
const char *func_name() const { return "set_user_var"; }
+ int save_in_field(Field *field, bool no_conversions);
};
--- 1.43/mysql-test/r/user_var.result 2006-10-04 18:49:37 +04:00
+++ 1.44/mysql-test/r/user_var.result 2006-12-14 23:33:01 +03:00
@@ -301,7 +301,14 @@
select @var;
@var
3
-drop table t1;
+create table t2 as select @var:=f2 from t1 group by f1 order by f2 desc limit 1;
+select * from t2;
+@var:=f2
+3
+select @var;
+@var
+3
+drop table t1,t2;
insert into city 'blah';
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 ''blah'' at line 1
SHOW COUNT(*) WARNINGS;
--- 1.38/mysql-test/t/user_var.test 2006-10-04 18:49:37 +04:00
+++ 1.39/mysql-test/t/user_var.test 2006-12-14 23:33:06 +03:00
@@ -210,7 +210,10 @@
insert into t1 values (1,2),(2,3),(3,1);
select @var:=f2 from t1 group by f1 order by f2 desc limit 1;
select @var;
-drop table t1;
+create table t2 as select @var:=f2 from t1 group by f1 order by f2 desc limit 1;
+select * from t2;
+select @var;
+drop table t1,t2;
#
# Bug#19024 - SHOW COUNT(*) WARNINGS not return Errors
| Thread |
|---|
| • bk commit into 5.0 tree (evgen:1.2320) BUG#16861 | eugene | 14 Dec |