MySQL Lists are EOL. Please join:

List:Commits« Previous MessageNext Message »
From:eugene Date:February 20 2006 7:17pm
Subject:bk commit into 5.0 tree (evgen:1.2050) BUG#13575
View as plain text  
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
  1.2050 06/02/20 22:17:08 evgen@stripped +5 -0
  Fixed bug#13575: SP funcs in select with distinct/group and order by can
  produce wrong data
  
  By default Item_sp_func::val_str() returns string from it's internal buffer.
  When grouping is present Item_copy_string is used to store SP function result,
  but it doesn't buffer the result. When the next record is read, internal
  buffer is overwritten, beacuse of this Item_copy_string::val_str() will have
  wrong data. Thus producing weird query result.
  
  Item_copy_string now buffers the result if the source item is a function.

  sql/sql_select.cc
    1.395 06/02/20 22:14:35 evgen@stripped +1 -1
    Fixed bug#13575: SP funcs in select with distinct/group and order by can produce wrong data
    The setup_copy_fields() now uses Item_copy_string with buffer for function items.

  sql/item.h
    1.186 06/02/20 22:13:08 evgen@stripped +5 -0
    Fixed bug#13575: SP funcs in select with distinct/group and order by can produce wrong data
    Added constructor to the Item_copy_string class, which will allocate a buffer for the result string.

  sql/item.cc
    1.205 06/02/20 22:11:42 evgen@stripped +18 -2
    Fixed bug#13575: SP funcs in select with distinct/group and order by can
    produce wrong data
    To the Item_copy_string class added constructor which allocates a buffer for
    the result string. Item_copy_string::val_str() now uses that buffer if it was
    allocated.

  mysql-test/r/sp.result
    1.186 06/02/20 22:11:13 evgen@stripped +16 -0
    Added test case for bug#13575: SP funcs in select with distinct/group and
    order by can produce wrong data

  mysql-test/t/sp.test
    1.175 06/02/20 22:10:29 evgen@stripped +16 -0
    Added test case for bug#13575: SP funcs in select with distinct/group and order by can produce wrong data

# 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/13575-bug-5.0-mysql

--- 1.204/sql/item.cc	2006-01-24 20:15:08 +03:00
+++ 1.205/sql/item.cc	2006-02-20 22:11:42 +03:00
@@ -2806,10 +2806,26 @@
   Item_copy_string
 ****************************************************************************/
 
+Item_copy_string::Item_copy_string(THD *thd, Item *i) :item(i)
+{
+  null_value=maybe_null=item->maybe_null;
+  decimals=item->decimals;
+  max_length=item->max_length;
+  name=item->name;
+  cached_field_type= item->field_type();
+  /*
+    Allocate buffer for result. Some function items can return string from
+    internal buffer, and when it is changed, this string will change too
+    producing weird result.
+  */
+  use_buffer= true;
+  str_value.alloc(min(i->max_length,thd->variables.max_sort_length));
+}
+
 void Item_copy_string::copy()
 {
-  String *res=item->val_str(&str_value);
-  if (res && res != &str_value)
+  String *res=item->val_str(use_buffer?&tmp_value:&str_value);
+  if (res && (use_buffer || res != &str_value))
     str_value.copy(*res);
   null_value=item->null_value;
 }

--- 1.185/sql/item.h	2006-01-24 20:15:08 +03:00
+++ 1.186/sql/item.h	2006-02-20 22:13:08 +03:00
@@ -1913,6 +1913,9 @@
   enum enum_field_types cached_field_type;
 public:
   Item *item;
+  String tmp_value;
+  bool use_buffer;
+
   Item_copy_string(Item *i) :item(i)
   {
     null_value=maybe_null=item->maybe_null;
@@ -1920,7 +1923,9 @@
     max_length=item->max_length;
     name=item->name;
     cached_field_type= item->field_type();
+    use_buffer= false;
   }
+  Item_copy_string(THD *thd, Item *i);
   enum Type type() const { return COPY_STR_ITEM; }
   enum Item_result result_type () const { return STRING_RESULT; }
   enum_field_types field_type() const { return cached_field_type; }

--- 1.394/sql/sql_select.cc	2006-02-14 11:33:44 +03:00
+++ 1.395/sql/sql_select.cc	2006-02-20 22:14:35 +03:00
@@ -12968,7 +12968,7 @@
 	 on how the value is to be used: In some cases this may be an
 	 argument in a group function, like: IF(ISNULL(col),0,COUNT(*))
       */
-      if (!(pos=new Item_copy_string(pos)))
+      if (!(pos=new Item_copy_string(thd, pos)))
 	goto err;
       if (i < border)                           // HAVING, ORDER and GROUP BY
       {

--- 1.185/mysql-test/r/sp.result	2006-02-06 16:09:07 +03:00
+++ 1.186/mysql-test/r/sp.result	2006-02-20 22:11:13 +03:00
@@ -4519,4 +4519,20 @@
 Inner
 drop procedure bug15011|
 drop table t3|
+create table t3 (f1 int, f2 varchar(3), primary key(f1)) engine=innodb|
+insert into t3 values (1,'aaa'),(2,'bbb'),(3,'ccc')|
+CREATE FUNCTION bug13575 ( p1 integer ) 
+returns varchar(3) 
+BEGIN 
+DECLARE v1 VARCHAR(10) DEFAULT null;
+SELECT f2 INTO v1 FROM t3 WHERE f1 = p1; 
+RETURN v1;
+END|
+select distinct f1, bug13575(f1) from t3 order by f1|
+f1	bug13575(f1)
+1	aaa
+2	bbb
+3	ccc
+drop function bug13575;
+drop table t3|
 drop table t1,t2;

--- 1.174/mysql-test/t/sp.test	2006-02-06 16:09:07 +03:00
+++ 1.175/mysql-test/t/sp.test	2006-02-20 22:10:29 +03:00
@@ -5310,6 +5310,22 @@
 drop procedure bug15011|
 drop table t3|
 
+#
+# Bug#13575 SP funcs in select with distinct/group and order by can
+#           produce bad data
+#
+create table t3 (f1 int, f2 varchar(3), primary key(f1)) engine=innodb|
+insert into t3 values (1,'aaa'),(2,'bbb'),(3,'ccc')|
+CREATE FUNCTION bug13575 ( p1 integer ) 
+returns varchar(3) 
+BEGIN 
+DECLARE v1 VARCHAR(10) DEFAULT null;
+SELECT f2 INTO v1 FROM t3 WHERE f1 = p1; 
+RETURN v1;
+END|
+select distinct f1, bug13575(f1) from t3 order by f1|
+drop function bug13575;
+drop table t3|
 
 #
 # BUG#NNNN: New bug synopsis
Thread
bk commit into 5.0 tree (evgen:1.2050) BUG#13575eugene20 Feb