-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add greatest and least scalar functions (#1320)
* Added greatest and least scalar functions * Updated dsl json * Fixed failing tests
- Loading branch information
1 parent
4ed793f
commit 9f418b6
Showing
7 changed files
with
211 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Flow\ETL\Function; | ||
|
||
use Flow\ETL\Function\Comparison\Comparable; | ||
use Flow\ETL\Row; | ||
|
||
final class Greatest extends ScalarFunctionChain | ||
{ | ||
use Comparable; | ||
|
||
public function __construct( | ||
private readonly array $values, | ||
) { | ||
} | ||
|
||
public function eval(Row $row) : mixed | ||
{ | ||
$extractedValues = []; | ||
|
||
foreach ($this->values as $value) { | ||
$extractedValues[] = (new Parameter($value))->eval($row); | ||
} | ||
|
||
$this->assertAllComparable($extractedValues, '>'); | ||
|
||
return max($extractedValues); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Flow\ETL\Function; | ||
|
||
use Flow\ETL\Function\Comparison\Comparable; | ||
use Flow\ETL\Row; | ||
|
||
final class Least extends ScalarFunctionChain | ||
{ | ||
use Comparable; | ||
|
||
public function __construct( | ||
private readonly array $values, | ||
) { | ||
} | ||
|
||
public function eval(Row $row) : mixed | ||
{ | ||
$extractedValues = []; | ||
|
||
foreach ($this->values as $value) { | ||
$extractedValues[] = (new Parameter($value))->eval($row); | ||
} | ||
|
||
$this->assertAllComparable($extractedValues, '<'); | ||
|
||
return \min($extractedValues); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/core/etl/tests/Flow/ETL/Tests/Unit/Function/GreatestTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Flow\ETL\Tests\Unit\Function; | ||
|
||
use function Flow\ETL\DSL\{greatest, int_entry, ref, row}; | ||
use Flow\ETL\Tests\FlowTestCase; | ||
|
||
final class GreatestTest extends FlowTestCase | ||
{ | ||
public function test_greatest_value() : void | ||
{ | ||
$greatest = greatest( | ||
10, | ||
20, | ||
ref('int'), | ||
40 | ||
); | ||
|
||
self::assertSame( | ||
55, | ||
$greatest->eval(row(int_entry('int', 55))) | ||
); | ||
} | ||
|
||
public function test_greatest_with_non_comparable_values() : void | ||
{ | ||
$greatest = greatest( | ||
null, | ||
20, | ||
ref('int'), | ||
new \DateTimeImmutable('now') | ||
); | ||
|
||
$this->expectExceptionMessage("Can't compare '(datetime > integer)' due to data type mismatch."); | ||
|
||
$greatest->eval(row(int_entry('int', 55))); | ||
} | ||
|
||
public function test_greatest_with_null() : void | ||
{ | ||
$greatest = greatest( | ||
null, | ||
20, | ||
ref('int'), | ||
1257 | ||
); | ||
|
||
self::assertSame( | ||
1257, | ||
$greatest->eval(row(int_entry('int', 55))) | ||
); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/core/etl/tests/Flow/ETL/Tests/Unit/Function/LeastTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Flow\ETL\Tests\Unit\Function; | ||
|
||
use function Flow\ETL\DSL\{int_entry, least, ref, row}; | ||
use Flow\ETL\Tests\FlowTestCase; | ||
|
||
final class LeastTest extends FlowTestCase | ||
{ | ||
public function test_greatest_with_non_comparable_values() : void | ||
{ | ||
$lest = least( | ||
null, | ||
20, | ||
ref('int'), | ||
new \DateTimeImmutable('now'), | ||
); | ||
|
||
$this->expectExceptionMessage("Can't compare '(datetime < integer)' due to data type mismatch."); | ||
|
||
$lest->eval(row(int_entry('int', 55))); | ||
} | ||
|
||
public function test_least_value() : void | ||
{ | ||
$lest = least( | ||
10, | ||
20, | ||
ref('int'), | ||
40, | ||
); | ||
|
||
self::assertSame( | ||
10, | ||
$lest->eval(row(int_entry('int', 55))) | ||
); | ||
} | ||
|
||
public function test_least_with_null() : void | ||
{ | ||
$ltest = least( | ||
null, | ||
20, | ||
ref('int'), | ||
1257 | ||
); | ||
|
||
self::assertNull( | ||
$ltest->eval(row(int_entry('int', 4))) | ||
); | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.