Skip to content

Commit 5fd4695

Browse files
authored
Merge pull request #69 from tarasom/fix_evaluation
fix: Resolve evaluation issues in SUM() and CONVERT_TZ() functions
2 parents 0297dd1 + 4dccefa commit 5fd4695

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

src/Processor/Expression/FunctionEvaluator.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,13 @@ private static function sqlCount(
353353
}
354354

355355
/**
356-
* @param array<string, Column> $columns
356+
* @param FakePdoInterface $conn
357+
* @param Scope $scope
358+
* @param FunctionExpression $expr
359+
* @param QueryResult $result
357360
*
358-
* @return ?numeric
361+
* @return float|int|mixed|string|null
362+
* @throws ProcessorException
359363
*/
360364
private static function sqlSum(
361365
FakePdoInterface $conn,
@@ -368,6 +372,10 @@ private static function sqlSum(
368372
$sum = 0;
369373

370374
if (!$result->rows) {
375+
if ($expr instanceof FunctionExpression) {
376+
return self::evaluate($conn, $scope, $expr, [], $result);
377+
}
378+
371379
return null;
372380
}
373381

@@ -1575,6 +1583,10 @@ private static function sqlConvertTz(
15751583
throw new \InvalidArgumentException("CONVERT_TZ() requires exactly 3 arguments");
15761584
}
15771585

1586+
if ($args[0] instanceof ColumnExpression && empty($row)) {
1587+
return null;
1588+
}
1589+
15781590
/** @var string|null $dtValue */
15791591
$dtValue = Evaluator::evaluate($conn, $scope, $args[0], $row, $result);
15801592
/** @var string|null $fromTzValue */

tests/EndToEndTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,4 +1516,44 @@ public function leastWithExceptionProvider(): iterable
15161516
yield ['Should fail with single argument' => [1]];
15171517
yield ['Should fail without any arguments' => []];
15181518
}
1519+
1520+
public function testNestedFunctions()
1521+
{
1522+
$pdo = self::getConnectionToFullDB();
1523+
1524+
$query = $pdo->prepare("
1525+
SELECT
1526+
SUM(
1527+
TIMESTAMPDIFF(
1528+
SECOND,
1529+
CONVERT_TZ('2025-12-31 22:59:59', 'Europe/Kyiv', 'Europe/Kyiv'),
1530+
CONVERT_TZ('2025-12-31 23:59:59', 'Europe/Kyiv', 'Europe/Kyiv')
1531+
)
1532+
)
1533+
");
1534+
$query->execute();
1535+
1536+
$this->assertSame(3600, (int)$query->fetchColumn());
1537+
}
1538+
1539+
public function testNestedFunctionsFromDB()
1540+
{
1541+
$pdo = self::getConnectionToFullDB();
1542+
$count = $pdo->query("SELECT COUNT(*) FROM video_game_characters")->fetchColumn();
1543+
1544+
$query = $pdo->prepare("
1545+
SELECT SUM(
1546+
TIMESTAMPDIFF(
1547+
SECOND,
1548+
CONVERT_TZ(`created_on`, 'Europe/Kyiv', 'Europe/Kyiv'),
1549+
CONVERT_TZ(`created_on` + INTERVAL 1 SECOND, 'Europe/Kyiv', 'Europe/Kyiv')
1550+
)
1551+
)
1552+
FROM `video_game_characters`
1553+
");
1554+
1555+
$query->execute();
1556+
1557+
$this->assertSame((int)$count, (int)$query->fetchColumn());
1558+
}
15191559
}

0 commit comments

Comments
 (0)