Bug #2633 count(distinct) do not work with prepared statement
Submitted: 3 Feb 2004 14:58 Modified: 20 Feb 2004 5:48
Reporter: Oleksandr Byelkin Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server Severity:S1 (Critical)
Version:4.1 OS:Any (all)
Assigned to: Oleksandr Byelkin CPU Architecture:Any

[3 Feb 2004 14:58] Oleksandr Byelkin
Description:
count(distinct) do not work with prepared statement, because of double delete 
of TMP_TABLE_PARAM of Item_sum_cout_distinct. (it looks like items was deleted 
twice, after query executing and in THD delete (prepared statements delete)) 
 
 

How to repeat:
Apply following patch (which fix other bug in prepared statements and add 
test-suite of this bug to test/client-test: 
===== sql/sql_prepare.cc 1.70 vs edited ===== 
*** /tmp/sql_prepare.cc-1.70-22599	Mon Jan 19 19:06:23 2004 
--- edited/sql/sql_prepare.cc	Tue Feb  3 18:22:48 2004 
*************** 
*** 1009,1025 **** 
        order->item= (Item **)(order+1); 
      for (order=(ORDER *)sl->order_list.first ; order ; order=order->next) 
        order->item= (Item **)(order+1); 
    } 
   
-   /* 
-     TODO: When the new table structure is ready, then have a status bit  
-     to indicate the table is altered, and re-do the setup_*  
-     and open the tables back. 
-   */ 
-   for (TABLE_LIST *tables= (TABLE_LIST*) 
stmt->lex->select_lex.table_list.first; 
-        tables; 
-        tables= tables->next) 
-     tables->table= 0; // safety - nasty init 
   
  #ifndef EMBEDDED_LIBRARY 
    if (stmt->param_count && setup_params_data(stmt)) 
--- 1009,1029 ---- 
        order->item= (Item **)(order+1); 
      for (order=(ORDER *)sl->order_list.first ; order ; order=order->next) 
        order->item= (Item **)(order+1); 
+  
+     /* 
+       TODO: When the new table structure is ready, then have a status bit  
+       to indicate the table is altered, and re-do the setup_*  
+       and open the tables back. 
+     */ 
+     for (TABLE_LIST *tables= (TABLE_LIST*) sl->table_list.first; 
+ 	 tables; 
+ 	 tables= tables->next) 
+     { 
+       tables->table= 0; // safety - nasty init 
+       tables->table_list= 0; 
+     } 
    } 
   
   
  #ifndef EMBEDDED_LIBRARY 
    if (stmt->param_count && setup_params_data(stmt)) 
===== tests/client_test.c 1.51 vs edited ===== 
*** /tmp/client_test.c-1.51-22599	Sat Dec 20 01:16:00 2003 
--- edited/tests/client_test.c	Wed Feb  4 00:45:40 2004 
*************** 
*** 8095,8100 **** 
--- 8095,8132 ---- 
    rc= mysql_query(mysql,"DROP TABLE prepare_command"); 
  } 
   
+ static void test_bug2462() 
+ { 
+   MYSQL_STMT *stmt; 
+   int rc, i; 
+   /* const char *query= "SELECT SQL_BUFFER_RESULT (SELECT COUNT(DISTINCT b) 
FROM t1 GROUP BY t1.a LIMIT 1) FROM t1,t2 WHERE t1.a=t2.b"; */ 
+   const char *query= "SELECT COUNT(DISTINCT b) FROM t1 GROUP BY t1.a LIMIT 
1"; 
+   myheader("test_bug2462"); 
+    
+   rc = mysql_query(mysql, "DROP TABLE IF EXISTS t1,t2"); 
+   myquery(rc); 
+    
+   rc= mysql_query(mysql,"CREATE TABLE t1 (a int , b int);"); 
+   myquery(rc); 
+  
+   rc= mysql_query(mysql, 
+ 		  "insert into t1 values (1,1), (2, 2), (3,3), (4,4), 
(5,5);"); 
+   myquery(rc); 
+  
+   rc= mysql_query(mysql,"create table t2 select * from t1;"); 
+   myquery(rc); 
+  
+   stmt= mysql_prepare(mysql, query, strlen(query)); 
+ /*   for (i= 0; i < 10; i++) */ 
+ /*   { */ 
+     rc= mysql_execute(stmt); 
+     mystmt(stmt,rc); 
+ /*   } */ 
+   mysql_stmt_close(stmt); 
+  
+   rc= mysql_query(mysql, "DROP TABLE t1,t2"); 
+   myquery(rc); 
+ } 
   
  /* 
    Read and parse arguments and MySQL options from my.cnf 
*************** 
*** 8234,8239 **** 
--- 8266,8274 ---- 
      test_count= 1; 
      
      start_time= time((time_t *)0); 
+  
+     test_bug2462(); 
+  
      client_query();         /* simple client query test */ 
  #if NOT_YET_WORKING 
      /* Used for internal new development debugging */
[8 Feb 2004 10:59] Oleksandr Byelkin
ChangeSet 
  1.1694 04/02/08 20:57:14 bell@sanja.is.com.ua +2 -0 
  fixed cleupup() for distinct aggregate functions (BUG#2663)