Skip to content

Commit

Permalink
Return integers from Round func when precision is set to 0 (#1368)
Browse files Browse the repository at this point in the history
* Return integers from Round func when precision is set to 0

* Fixed failing tests
  • Loading branch information
norberttech authored Jan 14, 2025
1 parent 4e36234 commit a2f2a0f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/core/etl/src/Flow/ETL/Function/Round.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function __construct(
) {
}

public function eval(Row $row) : ?float
public function eval(Row $row) : int|float|null
{
$value = (new Parameter($this->value))->asNumber($row);
$precision = (new Parameter($this->precision))->asInt($row);
Expand All @@ -29,6 +29,6 @@ public function eval(Row $row) : ?float
$mode = 1;
}

return \round($value, $precision, $mode);
return $precision === 0 ? (int) \round($value, $precision, $mode) : \round($value, $precision, $mode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -464,9 +464,9 @@ public function test_window_avg_function() : void

self::assertSame(
[
['department' => 'Sales', 'avg_salary' => 3917.0],
['department' => 'Marketing', 'avg_salary' => 2940.0],
['department' => 'Finance', 'avg_salary' => 3550.0],
['department' => 'Sales', 'avg_salary' => 3917],
['department' => 'Marketing', 'avg_salary' => 2940],
['department' => 'Finance', 'avg_salary' => 3550],
],
df()
->from(from_all(from_memory($memoryPage1), from_memory($memoryPage2)))
Expand Down
36 changes: 36 additions & 0 deletions src/core/etl/tests/Flow/ETL/Tests/Unit/Function/RoundTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Flow\ETL\Tests\Unit\Function;

use function Flow\ETL\DSL\{float_entry, lit, row};
use function Flow\ETL\DSL\{ref};
use Flow\ETL\{Tests\FlowTestCase};

final class RoundTest extends FlowTestCase
{
public function test_round_float() : void
{
self::assertEquals(
10.12,
ref('float')->round(lit(2))->eval(row(float_entry('float', 10.123)))
);

self::assertIsFloat(
ref('float')->round(lit(2))->eval(row(float_entry('float', 10.123)))
);
}

public function test_round_with_precision_0() : void
{
self::assertEquals(
10,
ref('float')->round(lit(0))->eval(row(float_entry('float', 10.123)))
);

self::assertIsInt(
ref('float')->round(lit(0))->eval(row(float_entry('float', 10.123)))
);
}
}

0 comments on commit a2f2a0f

Please sign in to comment.