Skip to content

Commit a2f2a0f

Browse files
authored
Return integers from Round func when precision is set to 0 (#1368)
* Return integers from Round func when precision is set to 0 * Fixed failing tests
1 parent 4e36234 commit a2f2a0f

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

src/core/etl/src/Flow/ETL/Function/Round.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function __construct(
1515
) {
1616
}
1717

18-
public function eval(Row $row) : ?float
18+
public function eval(Row $row) : int|float|null
1919
{
2020
$value = (new Parameter($this->value))->asNumber($row);
2121
$precision = (new Parameter($this->precision))->asInt($row);
@@ -29,6 +29,6 @@ public function eval(Row $row) : ?float
2929
$mode = 1;
3030
}
3131

32-
return \round($value, $precision, $mode);
32+
return $precision === 0 ? (int) \round($value, $precision, $mode) : \round($value, $precision, $mode);
3333
}
3434
}

src/core/etl/tests/Flow/ETL/Tests/Integration/DataFrame/GroupByTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -464,9 +464,9 @@ public function test_window_avg_function() : void
464464

465465
self::assertSame(
466466
[
467-
['department' => 'Sales', 'avg_salary' => 3917.0],
468-
['department' => 'Marketing', 'avg_salary' => 2940.0],
469-
['department' => 'Finance', 'avg_salary' => 3550.0],
467+
['department' => 'Sales', 'avg_salary' => 3917],
468+
['department' => 'Marketing', 'avg_salary' => 2940],
469+
['department' => 'Finance', 'avg_salary' => 3550],
470470
],
471471
df()
472472
->from(from_all(from_memory($memoryPage1), from_memory($memoryPage2)))
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Flow\ETL\Tests\Unit\Function;
6+
7+
use function Flow\ETL\DSL\{float_entry, lit, row};
8+
use function Flow\ETL\DSL\{ref};
9+
use Flow\ETL\{Tests\FlowTestCase};
10+
11+
final class RoundTest extends FlowTestCase
12+
{
13+
public function test_round_float() : void
14+
{
15+
self::assertEquals(
16+
10.12,
17+
ref('float')->round(lit(2))->eval(row(float_entry('float', 10.123)))
18+
);
19+
20+
self::assertIsFloat(
21+
ref('float')->round(lit(2))->eval(row(float_entry('float', 10.123)))
22+
);
23+
}
24+
25+
public function test_round_with_precision_0() : void
26+
{
27+
self::assertEquals(
28+
10,
29+
ref('float')->round(lit(0))->eval(row(float_entry('float', 10.123)))
30+
);
31+
32+
self::assertIsInt(
33+
ref('float')->round(lit(0))->eval(row(float_entry('float', 10.123)))
34+
);
35+
}
36+
}

0 commit comments

Comments
 (0)