Skip to content

Commit 01ab3ac

Browse files
committed
Fix: ROUND can take 1 argument
1 parent 2e8b092 commit 01ab3ac

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

src/Processor/Expression/FunctionEvaluator.php

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
namespace Vimeo\MysqlEngine\Processor\Expression;
33

44
use Vimeo\MysqlEngine\FakePdoInterface;
5+
use Vimeo\MysqlEngine\Processor\ProcessorException;
56
use Vimeo\MysqlEngine\Processor\QueryResult;
67
use Vimeo\MysqlEngine\Processor\Scope;
7-
use Vimeo\MysqlEngine\Processor\ProcessorException;
88
use Vimeo\MysqlEngine\Query\Expression\ColumnExpression;
9+
use Vimeo\MysqlEngine\Query\Expression\ConstantExpression;
910
use Vimeo\MysqlEngine\Query\Expression\Expression;
1011
use Vimeo\MysqlEngine\Query\Expression\FunctionExpression;
1112
use Vimeo\MysqlEngine\Query\Expression\IntervalOperatorExpression;
@@ -226,6 +227,19 @@ public static function getColumnSchema(
226227
return Evaluator::getColumnSchema($expr->args[0], $scope, $columns);
227228

228229
case 'ROUND':
230+
$precision = 0;
231+
232+
if (isset($expr->args[1])) {
233+
/** @var ConstantExpression $arg */
234+
$arg = $expr->args[1];
235+
236+
$precision = (int)$arg->value;
237+
}
238+
239+
if ($precision === 0) {
240+
return new Column\IntColumn(false, 10);
241+
}
242+
229243
return Evaluator::getColumnSchema($expr->args[0], $scope, $columns);
230244

231245
case 'DATEDIFF':
@@ -1186,24 +1200,31 @@ private static function sqlDay(
11861200

11871201
/**
11881202
* @param array<string, mixed> $row
1203+
*
1204+
* @return float|int
11891205
*/
11901206
private static function sqlRound(
11911207
FakePdoInterface $conn,
11921208
Scope $scope,
11931209
FunctionExpression $expr,
11941210
array $row,
11951211
QueryResult $result
1196-
) : float {
1212+
) {
11971213
$args = $expr->args;
11981214

1199-
if (\count($args) !== 2) {
1200-
throw new ProcessorException("MySQL ROUND() function must be called with one arguments");
1215+
if (\count($args) !== 1 && \count($args) !== 2) {
1216+
throw new ProcessorException("MySQL ROUND() function must be called with one or two arguments");
1217+
}
1218+
1219+
$number = (float)Evaluator::evaluate($conn, $scope, $args[0], $row, $result);
1220+
1221+
if (!isset($args[1])) {
1222+
return \round($number);
12011223
}
12021224

1203-
$first = Evaluator::evaluate($conn, $scope, $args[0], $row, $result);
1204-
$second = Evaluator::evaluate($conn, $scope, $args[1], $row, $result);
1225+
$precision = (int)Evaluator::evaluate($conn, $scope, $args[1], $row, $result);
12051226

1206-
return \round($first, $second);
1227+
return \round($number, $precision);
12071228
}
12081229

12091230
private static function getPhpIntervalFromExpression(

tests/EndToEndTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,26 @@ public function testDecimalArithhmetic()
522522
);
523523
}
524524

525+
public function testRound()
526+
{
527+
$pdo = self::getPdo('mysql:foo');
528+
$pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
529+
530+
$query = $pdo->prepare('SELECT ROUND(3.141592) AS a, ROUND(3.141592, 2) AS b');
531+
532+
$query->execute();
533+
534+
$this->assertSame(
535+
[
536+
[
537+
'a' => 3,
538+
'b' => 3.14,
539+
],
540+
],
541+
$query->fetchAll(\PDO::FETCH_ASSOC)
542+
);
543+
}
544+
525545
public function testIsInFullSubquery()
526546
{
527547
$pdo = self::getConnectionToFullDB(false);

0 commit comments

Comments
 (0)