Ticket #732 (closed defect: fixed)

Opened 18 months ago

Last modified 16 months ago

using multiple aggregates in HAVING

Reported by: pookey Owned by: zYne
Priority: minor Milestone: 2.0.0 (OLD)
Component: Query/Hydration Version:
Severity: Keywords:
Cc: Has Test:
Status: Has Patch:

Description

Using multiple aggregates in HAVING causes a problem.. have a look here how the second SUM fails to be turned into the correct table alias in SQL.

$q = Doctrine_Query::create()
  ->from('BalancedTransfer bt')
  ->where('bt.id IN (SELECT bt1.id FROM BalancedTransfer bt1
                     INNER JOIN bt1.Transfers t
                     INNER JOIN t.TransactionIn ti
                     INNER JOIN t.TransactionOut to
                     GROUP BY bt1.id, bt1.amount
                     HAVING (SUM(ti.amount) != SUM(to.amount)))')

results in

SELECT b.id AS b__id, b.amount AS b__amount, b.created_at AS b__created_at, b.player_id AS b__player_id FROM balanced_transfer b WHERE b.id IN (SELECT b2.id AS b2__id FROM balanced_transfer b2 INNER JOIN balanced_transfer_component b3 ON b2.id = b3.balanced_transfer_id INNER JOIN player_transfer p ON p.id = b3.player_transfer_id INNER JOIN player_transaction p2 ON p.transaction_in = p2.id INNER JOIN player_transaction p3 ON p.transaction_out = p3.id GROUP BY b2.id, b2.amount HAVING SUM(p2.amount) != SUM(to.amount))

Change History

Changed 18 months ago by pookey

(In [3531]) refs #732

Changed 18 months ago by pookey

seems the problem is in Doctrine/Query/Having.php

    final public function load($having)
    {
        $tokens = $this->_tokenizer->bracketExplode($having, ' ', '(', ')');
        $part = $this->parseAggregateFunction(array_shift($tokens));
        $operator  = array_shift($tokens);
        $value     = implode(' ', $tokens);
        $value = $this->parseAggregateFunction($value);   <=-------
        $part .= ' ' . $operator . ' ' . $value;
        return $part;
    }

notice the line above, this fixes the problem, but it's a complete hack, and breaks everything else.

The problem with the HAVING here is that the $value is an aggregate function, and isn't parsed....

Changed 18 months ago by pookey

also see #685

Changed 18 months ago by anonymous

  • milestone beta3 deleted

Milestone beta3 deleted

Changed 18 months ago by jwage

  • milestone set to 1.0

Changed 17 months ago by pookey

  • status changed from new to closed
  • resolution set to fixed

(In [3723]) fixes #732 - yes, it's a nasty hack, but we have a new parser on the way

Changed 16 months ago by anonymous

  • milestone 1.0 deleted

Milestone 1.0 deleted

Note: See TracTickets for help on using tickets.