Skip to content

Commit 6b50249

Browse files
authored
Remove Rows::fromArray(), pass EntryFactory to the join methods (#1929)
* Remove `Rows::fromArray()`, pass `EntryFactory` to the join methods * Use context `EntryFactory` in the join methods * Enforce instance of `EntryFactory` in the join methods
1 parent d233d89 commit 6b50249

File tree

6 files changed

+36
-39
lines changed

6 files changed

+36
-39
lines changed

src/core/etl/src/Flow/ETL/Rows.php

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Flow\ETL;
66

7-
use function Flow\ETL\DSL\{array_to_rows, row};
7+
use function Flow\ETL\DSL\row;
88
use function Flow\Types\DSL\type_integer;
99
use Flow\ETL\Exception\{DuplicatedEntriesException, InvalidArgumentException, RuntimeException};
1010
use Flow\ETL\Hash\{Algorithm, NativePHPHash};
@@ -34,14 +34,6 @@ public function __construct(Row ...$rows)
3434
$this->partitions = new Partitions();
3535
}
3636

37-
/**
38-
* @param array<array-key, mixed> $data
39-
*/
40-
public static function fromArray(array $data, EntryFactory $entryFactory = new EntryFactory()) : self
41-
{
42-
return array_to_rows($data, $entryFactory);
43-
}
44-
4537
/**
4638
* @param array<int, Row>|array<Row> $rows
4739
* @param array<Partition>|array<string, string>|Partitions $partitions
@@ -391,7 +383,7 @@ public function joinInner(self $right, Expression $expression) : self
391383
/**
392384
* @throws InvalidArgumentException
393385
*/
394-
public function joinLeft(self $right, Expression $expression) : self
386+
public function joinLeft(self $right, Expression $expression, EntryFactory $entryFactory) : self
395387
{
396388
/**
397389
* @var array<Row> $joined
@@ -417,8 +409,6 @@ public function joinLeft(self $right, Expression $expression) : self
417409
}
418410

419411
if ($joinedRow === null) {
420-
$entryFactory = new EntryFactory();
421-
422412
$entries = [];
423413

424414
foreach ($rightSchema->definitions() as $definition) {
@@ -465,7 +455,7 @@ public function joinLeftAnti(self $right, Expression $expression) : self
465455
/**
466456
* @throws InvalidArgumentException
467457
*/
468-
public function joinRight(self $right, Expression $expression) : self
458+
public function joinRight(self $right, Expression $expression, EntryFactory $entryFactory) : self
469459
{
470460
/**
471461
* @var array<Row> $joined
@@ -491,8 +481,6 @@ public function joinRight(self $right, Expression $expression) : self
491481
}
492482

493483
if ($joinedRow === null) {
494-
$entryFactory = new EntryFactory();
495-
496484
$entries = [];
497485

498486
foreach ($leftSchema->definitions() as $definition) {

src/core/etl/src/Flow/ETL/Transformer/JoinEachRowsTransformer.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,16 @@ public static function right(DataFrameFactory $right, Expression $condition) : s
3737
}
3838

3939
/**
40-
* @param FlowContext $context
41-
*
4240
* @throws InvalidArgumentException
4341
*/
4442
public function transform(Rows $rows, FlowContext $context) : Rows
4543
{
4644
$rightRows = $this->factory->from($rows)->fetch();
4745

4846
return match ($this->type) {
49-
Join::left => $rows->joinLeft($rightRows, $this->expression),
47+
Join::left => $rows->joinLeft($rightRows, $this->expression, $context->entryFactory()),
5048
Join::left_anti => $rows->joinLeftAnti($rightRows, $this->expression),
51-
Join::right => $rows->joinRight($rightRows, $this->expression),
49+
Join::right => $rows->joinRight($rightRows, $this->expression, $context->entryFactory()),
5250
default => $rows->joinInner($rightRows, $this->expression),
5351
};
5452
}

src/core/etl/tests/Flow/ETL/Tests/Benchmark/RowsBench.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Flow\ETL\Tests\Benchmark;
66

7-
use function Flow\ETL\DSL\{ref, string_entry};
7+
use function Flow\ETL\DSL\{array_to_rows, ref, string_entry};
88
use Flow\ETL\{Row, Rows};
99
use PhpBench\Attributes\{BeforeMethods, Groups, Revs};
1010

@@ -19,7 +19,7 @@ final class RowsBench
1919

2020
public function setUp() : void
2121
{
22-
$this->rows = Rows::fromArray(
22+
$this->rows = array_to_rows(
2323
\array_merge(...\array_map(static fn () : array => [
2424
['id' => 1, 'random' => false, 'text' => null, 'from' => 666],
2525
['id' => 2, 'random' => true, 'text' => null, 'from' => 666],
@@ -29,7 +29,7 @@ public function setUp() : void
2929
], \range(0, 10_000)))
3030
);
3131

32-
$this->reducedRows = Rows::fromArray(
32+
$this->reducedRows = array_to_rows(
3333
\array_merge(...\array_map(static fn () : array => [
3434
['id' => 1, 'random' => false, 'text' => null, 'from' => 666],
3535
['id' => 2, 'random' => true, 'text' => null, 'from' => 666],

src/core/etl/tests/Flow/ETL/Tests/Benchmark/Transformer/RenameEachEntryTransformerBench.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Flow\ETL\Tests\Benchmark\Transformer;
66

7-
use function Flow\ETL\DSL\{config, flow_context, rename_style};
7+
use function Flow\ETL\DSL\{array_to_rows, config, flow_context, rename_style};
88
use Flow\ETL\{FlowContext, Rows, String\StringStyles, Transformer\RenameEachEntryTransformer};
99
use PhpBench\Attributes\{BeforeMethods, Groups};
1010

@@ -18,14 +18,14 @@ final class RenameEachEntryTransformerBench
1818

1919
public function setUp() : void
2020
{
21-
$this->rows = Rows::fromArray(
21+
$this->rows = array_to_rows(
2222
\array_merge(...\array_map(static fn () : array => [
2323
['id' => 1, 'random text' => null, 'from' => 666],
2424
['id' => 2, 'random text' => null, 'from' => 666],
2525
['id' => 3, 'random text' => null, 'from' => 666],
2626
['id' => 4, 'random text' => null, 'from' => 666],
2727
['id' => 5, 'random text' => null, 'from' => 666],
28-
], \range(0, 1_000)))
28+
], \range(0, 1_000))),
2929
);
3030
$this->context = flow_context(config());
3131
}

src/core/etl/tests/Flow/ETL/Tests/Benchmark/Transformer/RenameEntryTransformerBench.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Flow\ETL\Tests\Benchmark\Transformer;
66

7-
use function Flow\ETL\DSL\{config, flow_context};
7+
use function Flow\ETL\DSL\{array_to_rows, config, flow_context};
88
use Flow\ETL\{FlowContext, Rows};
99
use Flow\ETL\Transformer\RenameEntryTransformer;
1010
use PhpBench\Attributes\{BeforeMethods, Groups};
@@ -19,7 +19,7 @@ final class RenameEntryTransformerBench
1919

2020
public function setUp() : void
2121
{
22-
$this->rows = Rows::fromArray(
22+
$this->rows = array_to_rows(
2323
\array_merge(...\array_map(static fn () : array => [
2424
['id' => 1, 'random' => false, 'text' => null, 'from' => 666],
2525
['id' => 2, 'random' => true, 'text' => null, 'from' => 666],

src/core/etl/tests/Flow/ETL/Tests/Unit/RowsJoinTest.php

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Flow\ETL\Tests\Unit;
66

7-
use function Flow\ETL\DSL\{bool_entry, int_entry, join_on, row, rows, str_entry};
7+
use function Flow\ETL\DSL\{bool_entry, config, flow_context, int_entry, join_on, row, rows, str_entry};
88
use Flow\ETL\Exception\{DuplicatedEntriesException, InvalidArgumentException};
99
use Flow\ETL\Join\Expression;
1010
use Flow\ETL\Tests\FlowTestCase;
@@ -335,7 +335,8 @@ public function test_left_join() : void
335335
row(str_entry('code', 'US'), str_entry('name', 'United States')),
336336
row(str_entry('code', 'GB'), str_entry('name', 'Great Britain')),
337337
),
338-
Expression::on(['country' => 'code'], 'joined_')
338+
Expression::on(['country' => 'code'], 'joined_'),
339+
flow_context(config())->entryFactory(),
339340
);
340341

341342
self::assertEquals(
@@ -358,7 +359,8 @@ public function test_left_join_empty() : void
358359

359360
$joined = $left->joinLeft(
360361
rows(),
361-
Expression::on(['country' => 'code'])
362+
Expression::on(['country' => 'code']),
363+
flow_context(config())->entryFactory(),
362364
);
363365

364366
self::assertEquals(
@@ -381,7 +383,8 @@ public function test_left_join_empty_without_prefix() : void
381383

382384
$joined = $left->joinLeft(
383385
rows(),
384-
Expression::on(['country_code' => 'country_code'])
386+
Expression::on(['country_code' => 'country_code']),
387+
flow_context(config())->entryFactory(),
385388
);
386389

387390
self::assertEquals(
@@ -404,7 +407,8 @@ public function test_left_join_to_empty() : void
404407
row(str_entry('code', 'US'), str_entry('name', 'United States')),
405408
row(str_entry('code', 'GB'), str_entry('name', 'Great Britain')),
406409
),
407-
Expression::on(['country' => 'code'])
410+
Expression::on(['country' => 'code']),
411+
flow_context(config())->entryFactory(),
408412
);
409413

410414
self::assertEquals(
@@ -430,7 +434,8 @@ public function test_left_join_with_the_duplicated_columns() : void
430434
row(int_entry('id', 101), str_entry('code', 'US'), str_entry('name', 'United States')),
431435
row(int_entry('id', 102), str_entry('code', 'GB'), str_entry('name', 'Great Britain')),
432436
),
433-
Expression::on(['country' => 'code'], '')
437+
Expression::on(['country' => 'code'], ''),
438+
flow_context(config())->entryFactory(),
434439
);
435440
}
436441

@@ -448,7 +453,8 @@ public function test_left_join_without_prefix() : void
448453
row(str_entry('country_code', 'US'), str_entry('name', 'United States')),
449454
row(str_entry('country_code', 'GB'), str_entry('name', 'Great Britain')),
450455
),
451-
Expression::on(['country_code' => 'country_code'])
456+
Expression::on(['country_code' => 'country_code']),
457+
flow_context(config())->entryFactory(),
452458
);
453459

454460
self::assertEquals(
@@ -476,7 +482,8 @@ public function test_right_join() : void
476482
row(str_entry('code', 'US'), str_entry('name', 'United States')),
477483
row(str_entry('code', 'GB'), str_entry('name', 'Great Britain')),
478484
),
479-
Expression::on(['country' => 'code'], 'joined_')
485+
Expression::on(['country' => 'code'], 'joined_'),
486+
flow_context(config())->entryFactory(),
480487
);
481488

482489
self::assertEquals(
@@ -501,7 +508,8 @@ public function test_right_join_empty() : void
501508

502509
$joined = $left->joinRight(
503510
rows(),
504-
Expression::on(['country' => 'code'])
511+
Expression::on(['country' => 'code']),
512+
flow_context(config())->entryFactory(),
505513
);
506514

507515
self::assertEquals(
@@ -520,7 +528,8 @@ public function test_right_join_to_empty() : void
520528
row(str_entry('code', 'US'), str_entry('name', 'United States')),
521529
row(str_entry('code', 'GB'), str_entry('name', 'Great Britain')),
522530
),
523-
Expression::on(['country' => 'code'], 'joined_')
531+
Expression::on(['country' => 'code'], 'joined_'),
532+
flow_context(config())->entryFactory(),
524533
);
525534

526535
self::assertEquals(
@@ -551,7 +560,8 @@ public function test_right_join_with_duplicated_entry_names() : void
551560
row(int_entry('id', 102), str_entry('code', 'US'), str_entry('name', 'United States')),
552561
row(int_entry('id', 103), str_entry('code', 'GB'), str_entry('name', 'Great Britain')),
553562
),
554-
Expression::on(['country' => 'code'], '')
563+
Expression::on(['country' => 'code'], ''),
564+
flow_context(config())->entryFactory(),
555565
);
556566
}
557567

@@ -570,7 +580,8 @@ public function test_right_join_without_prefix() : void
570580
row(str_entry('country_code', 'US'), str_entry('name', 'United States')),
571581
row(str_entry('country_code', 'GB'), str_entry('name', 'Great Britain')),
572582
),
573-
Expression::on(['country_code' => 'country_code'])
583+
Expression::on(['country_code' => 'country_code']),
584+
flow_context(config())->entryFactory(),
574585
);
575586

576587
self::assertEquals(

0 commit comments

Comments
 (0)