FIX Exists statements can't remove GROUP BY if there's a HAVING clause as it might rely on an aggregate

This commit is contained in:
Guy Marriott 2019-04-17 09:49:46 +12:00 committed by Sam Minnee
parent 91591373d6
commit ef0af7ae9c

View File

@ -466,12 +466,17 @@ class DataQuery
$statement = $this->getFinalisedQuery();
$statement->setSelect('*');
// Clear limit, distinct, grouping, and order as it's not relevant for an exists query
// Clear limit, distinct, and order as it's not relevant for an exists query
$statement->setDistinct(false);
$statement->setOrderBy(null);
$statement->setGroupBy(null);
$statement->setLimit(null);
// We can remove grouping if there's no "having" that might be relying on an aggregate
$having = $statement->getHaving();
if (empty($having)) {
$statement->setGroupBy(null);
}
// Wrap the whole thing in an "EXISTS"
$sql = 'SELECT EXISTS(' . $statement->sql($params) . ')';
$result = DB::prepared_query($sql, $params);