Bug #34360 MySqlDataReader returns zero instead of actual value when doing count(*)
Submitted: 6 Feb 2008 19:06 Modified: 7 Feb 2008 0:43
Reporter: t s Email Updates:
Status: Not a Bug Impact on me:
None 
Category:Connector / NET Severity:S2 (Serious)
Version:5.1.4.0 OS:Any
Assigned to: CPU Architecture:Any
Tags: aggregate, connector.net, count, MySqlDataReader

[6 Feb 2008 19:06] t s
Description:
Running the following procedure produces the correct output when using QB, but when I have the MySqlDataReader call the sproc, it returns zero for all of the counts instead of the true value:

CREATE DEFINER=`root`@`%` PROCEDURE `sp_stacktraceoccurencecount`(days_add INT)
BEGIN
  SELECT c.stacktrace_id, COUNT(*) AS stcount, st.trace, st.bugnumber
     FROM Crashinstance c
     JOIN Stacktrace st ON c.stacktrace_id = st.id
     WHERE datefound > DATE_ADD(CURDATE(), INTERVAL days_add DAY)
     GROUP BY stacktrace_id, st.trace, st.bugnumber
     ORDER BY stcount DESC;
END

I tried just popping in the query instead of the sproc and that returns the same error so I'm assuming there is a bug in MySqlDataReader.

How to repeat:
1.  Create a table rows that are conducive to aggregation
2.  Try to do something like
 SELECT foo, bar, COUNT(*) as foobarcount
    FROM tablefoo
    GROUP BY foo, bar

3.  Make sure you get good results in QB
4.  Try to call it from a MySqlDataReader in code:

MySqlDataReader reader = null;
MySqlCommand mycommand = conn.CreateCommand();
mycommand.CommandText = "call sp_foobar";
reader = mycommand.ExecuteReader();
while(reader.Read())
{
   if (foobarcount == 0)
      Console.Writeline("WTF?!? This should NOT be zero");
}

Suggested fix:
Workaround: have the sproc create a temporary table and select your aggregation into that table, then select from the temp table to return the goods.
[6 Feb 2008 20:08] t s
The workaround does NOT work.  It seems to be something with the fact that the number that is generated is an Int64 or something like that.

From what I can see, the private members of the mysqldatareader class has the correct value for the aggregate number, but when I try to access that on the public side, it converts it to zero.
[6 Feb 2008 23:14] t s
Please close the bug, I found an error in a base class way down in my code that was causing this bug.  Sorry to "bug" you.