List:Internals« Previous MessageNext Message »
From:antony Date:April 22 2005 8:53am
Subject:bk commit into 4.0 tree (acurtis:1.2091) BUG#9547
View as plain text  
Below is the list of changes that have just been committed into a local
4.0 repository of acurtis. When acurtis 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.2091 05/04/22 09:04:26 acurtis@stripped +3 -0
  Bug#9547 - CASE statement gives incorrect result
    Return type of CASE should match 1st return type.
  Complete Item_func_case::print()

  sql/item_cmpfunc.cc
    1.65 05/04/22 09:04:14 acurtis@stripped +48 -4
    Bug#9547
      Return type of CASE should match 1st return type.
    Complete Item_func_case::print()

  mysql-test/t/case.test
    1.8 05/04/22 09:04:14 acurtis@stripped +11 -0
    Bug#9547
      Test for bug

  mysql-test/r/case.result
    1.10 05/04/22 09:04:14 acurtis@stripped +11 -2
    Bug#9547
      Test for bug
      + fix existing tests which exhibited bug

# 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:	acurtis
# Host:	xiphis.org
# Root:	/.amd_mnt/bk.anubis/host/work-acurtis/bug6616

--- 1.64/sql/item_cmpfunc.cc	2004-11-26 00:31:20 +00:00
+++ 1.65/sql/item_cmpfunc.cc	2005-04-22 09:04:14 +01:00
@@ -751,8 +751,33 @@
     return 0;
   }
   null_value= 0;
-  if (!(res=item->val_str(str)))
-    null_value= 1;
+  switch (cached_result_type)
+  {
+  case STRING_RESULT:
+    if (!(res=item->val_str(str)))
+      null_value= 1;
+    break;
+  case INT_RESULT:
+    str->set(item->val_int());
+    if (!item->null_value)
+      res= str;
+    else
+    {
+      null_value= 1;
+      res= 0;
+    }
+    break;
+  case REAL_RESULT:
+    str->set(item->val(), item->decimals);
+    if (!item->null_value)
+      res= str;
+    else
+    {
+      null_value= 1;
+      res= 0;
+    }
+    break;
+  }
   return res;
 }
 
@@ -876,12 +901,31 @@
   }
 }
 
-/* TODO:  Fix this so that it prints the whole CASE expression */
 
 void Item_func_case::print(String *str)
 {
-  str->append("case ");				// Not yet complete
+  if (first_expr)
+  {
+    str->append("case ");
+    first_expr->print(str);
+  }
+  else
+    str->append("case");
+  for (uint i=0 ; i < arg_count ; i+=2)
+  {
+    str->append(" when ");
+    args[i]->print(str);
+    str->append(" then ");
+    args[i+1]->print(str);
+  }
+  if (else_expr)
+  {
+    str->append(" else ");
+    else_expr->print(str);
+  }
+  str->append(" end");
 }
+
 
 /*
   Coalesce - return first not NULL argument.

--- 1.9/mysql-test/r/case.result	2002-12-26 13:55:21 +00:00
+++ 1.10/mysql-test/r/case.result	2005-04-22 09:04:14 +01:00
@@ -10,10 +10,10 @@
 3
 select CASE BINARY "b" when "a" then 1 when "B" then 2 WHEN "b" then "ok" END;
 CASE BINARY "b" when "a" then 1 when "B" then 2 WHEN "b" then "ok" END
-ok
+0
 select CASE "b" when "a" then 1 when binary "B" then 2 WHEN "b" then "ok" END;
 CASE "b" when "a" then 1 when binary "B" then 2 WHEN "b" then "ok" END
-ok
+0
 select CASE concat("a","b") when concat("ab","") then "a" when "b" then "b" end;
 CASE concat("a","b") when concat("ab","") then "a" when "b" then "b" end
 a
@@ -70,4 +70,13 @@
 orange
 yellow
 green
+drop table t1;
+create table t1 (a int not null, b varchar(4) not null);
+insert into t1 values (1,'uno'), (2,'dos'), (3,'tres');
+select sql_no_cache a, case b when 'uno' then 1 when 'dos' then 2 else 'tres' end c from t1 where a=3;
+a	c
+3	0
+select sql_buffer_result sql_no_cache a, case b when 'uno' then 1 when 'dos' then 2 else 'tres' end c from t1 where a=3;
+a	c
+3	0
 drop table t1;

--- 1.7/mysql-test/t/case.test	2002-12-26 13:55:21 +00:00
+++ 1.8/mysql-test/t/case.test	2005-04-22 09:04:14 +01:00
@@ -39,3 +39,14 @@
 insert into t1 values (1,1,'orange'),(1,2,'large'),(2,1,'yellow'),(2,2,'medium'),(3,1,'green'),(3,2,'small');
 select max(case col when 1 then val else null end) as color from t1 group by row;
 drop table t1;
+
+#
+# Bug#9547: CASE statement gives incorrect result
+# Result type should always follow type of 1st result otherwise results
+# end up different if the resultset is buffered
+#
+create table t1 (a int not null, b varchar(4) not null);
+insert into t1 values (1,'uno'), (2,'dos'), (3,'tres');
+select sql_no_cache a, case b when 'uno' then 1 when 'dos' then 2 else 'tres' end c from t1 where a=3;
+select sql_buffer_result sql_no_cache a, case b when 'uno' then 1 when 'dos' then 2 else 'tres' end c from t1 where a=3;
+drop table t1;
Thread
bk commit into 4.0 tree (acurtis:1.2091) BUG#9547antony22 Apr