<?xml version="1.0" encoding="utf-8"?>
<launchpad-bug id="702345">
  <date_last_updated>2011-01-17 11:49:54.958975+00:00</date_last_updated>
  <api_links>
    <bug_api_link>https://api.launchpad.net/1.0/bugs/702345</bug_api_link>
    <bug_owner_link>https://api.launchpad.net/1.0/~timour</bug_owner_link>
    <milestone_link>https://api.launchpad.net/1.0/maria/+milestone/5.3</milestone_link>
    <linked_branches_collection_link>https://api.launchpad.net/1.0/bugs/702345/linked_branches</linked_branches_collection_link>
    <activity_link>https://api.launchpad.net/1.0/bugs/702345/activity</activity_link>
  </api_links>
  <bug_web_link>https://bugs.launchpad.net/bugs/702345</bug_web_link>
  <owner>Timour Katchaounov</owner>
  <assignee>Timour Katchaounov</assignee>
  <milestone_title>Maria 5.3</milestone_title>
  <duplicate_link></duplicate_link>
  <duplicate_bug_id></duplicate_bug_id>
  <title>Empty query with SUM and HAVING, no GROUP BY returns NULL instead of empty result.</title>
  <status>Fix Released</status>
  <importance>High</importance>
  <created>2011-01-13 13:24:29.952448+00:00</created>
  <description>
<![CDATA[The following test case (for LP BUG#609121) extracted from subselect_mat.test:

create table t1 (c1 int);
create table t2 (c2 int);
insert into t1 values (1);
insert into t2 values (2);

SELECT SUM(c1) c1_sum FROM t1 WHERE c1 IN (SELECT c2 FROM t2) HAVING c1_sum;

incorrectly produces a NULL instead of an empty result.]]>  </description>
  <activities>
    <activity datechanged="2011-01-13T13:24:29.952448+00:00">
      <oldvalue>
<![CDATA[]]>      </oldvalue>
      <newvalue>
<![CDATA[]]>      </newvalue>
      <whatchanged>bug</whatchanged>
      <person>Timour Katchaounov</person>
      <message>added bug</message>
    </activity>
    <activity datechanged="2011-01-13T13:24:36.059186+00:00">
      <oldvalue>
<![CDATA[]]>      </oldvalue>
      <newvalue>
<![CDATA[5.3]]>      </newvalue>
      <whatchanged>maria: milestone</whatchanged>
      <person>Timour Katchaounov</person>
      <message></message>
    </activity>
    <activity datechanged="2011-01-13T13:24:40.290070+00:00">
      <oldvalue>
<![CDATA[]]>      </oldvalue>
      <newvalue>
<![CDATA[Timour Katchaounov (timour)]]>      </newvalue>
      <whatchanged>maria: assignee</whatchanged>
      <person>Timour Katchaounov</person>
      <message></message>
    </activity>
    <activity datechanged="2011-01-13T13:24:43.121298+00:00">
      <oldvalue>
<![CDATA[Undecided]]>      </oldvalue>
      <newvalue>
<![CDATA[High]]>      </newvalue>
      <whatchanged>maria: importance</whatchanged>
      <person>Timour Katchaounov</person>
      <message></message>
    </activity>
    <activity datechanged="2011-01-13T13:24:46.836923+00:00">
      <oldvalue>
<![CDATA[New]]>      </oldvalue>
      <newvalue>
<![CDATA[Confirmed]]>      </newvalue>
      <whatchanged>maria: status</whatchanged>
      <person>Timour Katchaounov</person>
      <message></message>
    </activity>
    <activity datechanged="2011-01-13T13:25:14.338954+00:00">
      <oldvalue>
<![CDATA[]]>      </oldvalue>
      <newvalue>
<![CDATA[lp:~maria-captains/maria/5.3-mwl89]]>      </newvalue>
      <whatchanged>branch linked</whatchanged>
      <person>Timour Katchaounov</person>
      <message></message>
    </activity>
    <activity datechanged="2011-01-17T09:39:32.614470+00:00">
      <oldvalue>
<![CDATA[Confirmed]]>      </oldvalue>
      <newvalue>
<![CDATA[In Progress]]>      </newvalue>
      <whatchanged>maria: status</whatchanged>
      <person>Timour Katchaounov</person>
      <message></message>
    </activity>
    <activity datechanged="2011-01-17T11:49:53.430326+00:00">
      <oldvalue>
<![CDATA[In Progress]]>      </oldvalue>
      <newvalue>
<![CDATA[Fix Released]]>      </newvalue>
      <whatchanged>maria: status</whatchanged>
      <person>Timour Katchaounov</person>
      <message></message>
    </activity>
  </activities>
  <comments>
    <comment commentlink="https://api.launchpad.net/1.0/maria/+bug/702345/comments/0" datecreated="2011-01-13T13:24:29.952448+00:00">
      <person>Timour Katchaounov</person>
      <subject>
<![CDATA[Empty query with SUM and HAVING, no GROUP BY returns NULL instead of empty result.]]>      </subject>
      <content>
<![CDATA[The following test case (for LP BUG#609121) extracted from subselect_mat.test:

create table t1 (c1 int);
create table t2 (c2 int);
insert into t1 values (1);
insert into t2 values (2);

SELECT SUM(c1) c1_sum FROM t1 WHERE c1 IN (SELECT c2 FROM t2) HAVING c1_sum;

incorrectly produces a NULL instead of an empty result.]]>      </content>
    </comment>
    <comment commentlink="https://api.launchpad.net/1.0/maria/+bug/702345/comments/1" datecreated="2011-01-14T14:19:04.136345+00:00">
      <person>Timour Katchaounov</person>
      <subject>
<![CDATA[Re: Empty query with SUM and HAVING, no GROUP BY returns NULL instead of empty result.]]>      </subject>
      <content>
<![CDATA[The bug is present both with MATERIALIZATION and with IN-TO-EXISTS,
but not with SEMIJOIN:
set @@optimizer_switch='materialization=on,in_to_exists=on,semijoin=off';
set @@optimizer_switch='materialization=off,in_to_exists=on,semijoin=off';
set @@optimizer_switch='materialization=on,in_to_exists=off,semijoin=off';]]>      </content>
    </comment>
    <comment commentlink="https://api.launchpad.net/1.0/maria/+bug/702345/comments/2" datecreated="2011-01-17T09:45:05.406502+00:00">
      <person>Timour Katchaounov</person>
      <subject>
<![CDATA[Re: Empty query with SUM and HAVING, no GROUP BY returns NULL instead of empty result.]]>      </subject>
      <content>
<![CDATA[Analysis:
Close to its end JOIN::optimize() assigns having to tmp_having, and
sets the having clause to NULL:

  tmp_having= having;
  if (select_options & SELECT_DESCRIBE)
  {
    error= 0;
    DBUG_RETURN(0);
  }
  having= 0;

At the same time, this query detects an empty result set, and calls
return_zero_rows(), which must check the HAVING clause by:

    if (having && having->val_int() == 0)
      send_row=0;

However having has been already set to NULL, so return_zero_rows
doesn't check the having clause, hence the wrong result.

Solution:
There are two ways to solve this problem:
a) check join->tmp_having in addition to join->having, or
b) do not set having= 0 in JOIN::optimize.
]]>      </content>
    </comment>
  </comments>
  <branches>
    <branch>lp:~maria-captains/maria/5.3-mwl89</branch>
  </branches>
</launchpad-bug>
