From bef5ed516928648031ce026251ac69b8869ab541 Mon Sep 17 00:00:00 2001
From: Jaap Jansma <jaap.jansma@civicoop.org>
Date: Wed, 9 Oct 2019 14:17:58 +0200
Subject: [PATCH] fixed issue with sql aggregation

---
 .../DataFlow/AbstractDataFlow.php             | 19 +++++++++++++++----
 Civi/DataProcessor/DataFlow/SqlDataFlow.php   | 12 ++++++++++++
 2 files changed, 27 insertions(+), 4 deletions(-)

diff --git a/Civi/DataProcessor/DataFlow/AbstractDataFlow.php b/Civi/DataProcessor/DataFlow/AbstractDataFlow.php
index a860bbff..4fa85a1f 100644
--- a/Civi/DataProcessor/DataFlow/AbstractDataFlow.php
+++ b/Civi/DataProcessor/DataFlow/AbstractDataFlow.php
@@ -153,10 +153,7 @@ abstract class AbstractDataFlow {
       } catch (EndOfFlowException $e) {
         // Do nothing
       }
-      if (count($this->aggregateFields)) {
-        $aggregator = new Aggregator($_allRecords, $this->aggregateFields, $this->dataSpecification);
-        $_allRecords = $aggregator->aggregateRecords($fieldNameprefix);
-      }
+      $_allRecords = $this->aggregate($_allRecords, $fieldNameprefix);
       foreach($_allRecords as $record) {
         $this->_allRecords[] = $this->formatRecordOutput($record);
       }
@@ -279,5 +276,19 @@ abstract class AbstractDataFlow {
     return $compareValue;
   }
 
+  /**
+   * @param $records
+   * @param string $fieldNameprefix
+   *
+   * @return array();
+   */
+  protected function aggregate($records, $fieldNameprefix="") {
+    if (count($this->aggregateFields)) {
+      $aggregator = new Aggregator($records, $this->aggregateFields, $this->dataSpecification);
+      $records = $aggregator->aggregateRecords($fieldNameprefix);
+    }
+    return $records;
+  }
+
 
 }
diff --git a/Civi/DataProcessor/DataFlow/SqlDataFlow.php b/Civi/DataProcessor/DataFlow/SqlDataFlow.php
index 16a503c7..4f518ba7 100644
--- a/Civi/DataProcessor/DataFlow/SqlDataFlow.php
+++ b/Civi/DataProcessor/DataFlow/SqlDataFlow.php
@@ -8,6 +8,7 @@ namespace Civi\DataProcessor\DataFlow;
 
 use Civi\DataProcessor\DataFlow\Sort\SortSpecification;
 use Civi\DataProcessor\DataFlow\SqlDataFlow\WhereClauseInterface;
+use Civi\DataProcessor\DataFlow\Utils\Aggregator;
 use \Civi\DataProcessor\DataSpecification\DataSpecification;
 
 abstract class SqlDataFlow extends AbstractDataFlow {
@@ -290,4 +291,15 @@ abstract class SqlDataFlow extends AbstractDataFlow {
     return $this->dao;
   }
 
+  /**
+   * @param $records
+   * @param string $fieldNameprefix
+   *
+   * @return array();
+   */
+  protected function aggregate($records, $fieldNameprefix="") {
+    // Aggregation is done in the database.
+    return $records;
+  }
+
 }
-- 
GitLab