From 0fa2b7c51ac9cb62fc873ad3086a706a82401117 Mon Sep 17 00:00:00 2001 From: Joseph Bielawski Date: Fri, 27 Oct 2023 15:25:27 +0200 Subject: [PATCH] Rework `GroupBy::result()` method to not recreate entries & rows in loop (#655) * Rework `GroupBy::result()` method to not recreate entries & rows in loop * Use `FlowContext` in `GroupBy` * Fixed broken `GroupByTest` --- src/core/etl/src/Flow/ETL/GroupBy.php | 24 ++++++++----------- .../src/Flow/ETL/Pipeline/GroupByPipeline.php | 2 +- .../tests/Flow/ETL/Tests/Unit/GroupByTest.php | 4 +++- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/core/etl/src/Flow/ETL/GroupBy.php b/src/core/etl/src/Flow/ETL/GroupBy.php index 826874aa2..c5835969f 100644 --- a/src/core/etl/src/Flow/ETL/GroupBy.php +++ b/src/core/etl/src/Flow/ETL/GroupBy.php @@ -8,8 +8,6 @@ use Flow\ETL\Exception\RuntimeException; use Flow\ETL\GroupBy\Aggregation; use Flow\ETL\GroupBy\Aggregator; -use Flow\ETL\Row\Entries; -use Flow\ETL\Row\Factory\NativeEntryFactory; use Flow\ETL\Row\Reference; use Flow\ETL\Row\References; @@ -82,30 +80,28 @@ public function group(Rows $rows) : void } } - public function result() : Rows + public function result(FlowContext $context) : Rows { - $rows = new Rows(); + $rows = []; foreach ($this->groups as $group) { - $entries = new Entries(); + $entries = []; - if (\array_key_exists('values', $group)) { - /** @var mixed $value */ - foreach ($group['values'] as $entry => $value) { - $entries = $entries->add((new NativeEntryFactory)->create($entry, $value)); - } + /** @var mixed $value */ + foreach ($group['values'] ?? [] as $entry => $value) { + $entries[] = $context->entryFactory()->create($entry, $value); } foreach ($group['aggregators'] as $aggregator) { - $entries = $entries->add($aggregator->result()); + $entries[] = $aggregator->result(); } - if ($entries->count()) { - $rows = $rows->add(new Row($entries)); + if (\count($entries)) { + $rows[] = Row::create(...$entries); } } - return $rows; + return new Rows(...$rows); } /** diff --git a/src/core/etl/src/Flow/ETL/Pipeline/GroupByPipeline.php b/src/core/etl/src/Flow/ETL/Pipeline/GroupByPipeline.php index 5a730a3d7..f2d75d95d 100644 --- a/src/core/etl/src/Flow/ETL/Pipeline/GroupByPipeline.php +++ b/src/core/etl/src/Flow/ETL/Pipeline/GroupByPipeline.php @@ -59,7 +59,7 @@ public function process(FlowContext $context) : \Generator $this->groupBy->group($nextRows); } - $this->nextPipeline->source(new Extractor\ProcessExtractor($this->groupBy->result())); + $this->nextPipeline->source(new Extractor\ProcessExtractor($this->groupBy->result($context))); foreach ($this->nextPipeline->process($context) as $nextRows) { yield $nextRows; diff --git a/src/core/etl/tests/Flow/ETL/Tests/Unit/GroupByTest.php b/src/core/etl/tests/Flow/ETL/Tests/Unit/GroupByTest.php index 3c557a463..08d77b7ad 100644 --- a/src/core/etl/tests/Flow/ETL/Tests/Unit/GroupByTest.php +++ b/src/core/etl/tests/Flow/ETL/Tests/Unit/GroupByTest.php @@ -4,9 +4,11 @@ namespace Flow\ETL\Tests\Unit; +use Flow\ETL\Config; use Flow\ETL\DSL\Entry; use Flow\ETL\Exception\InvalidArgumentException; use Flow\ETL\Exception\RuntimeException; +use Flow\ETL\FlowContext; use Flow\ETL\GroupBy; use Flow\ETL\Row; use Flow\ETL\Rows; @@ -44,7 +46,7 @@ public function test_group_by_missing_entry() : void Row::create(Entry::null('type')), Row::create(Entry::string('type', 'c')) ), - $groupBy->result() + $groupBy->result(new FlowContext(Config::default())) ); }