Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/Processor/Expression/FunctionEvaluator.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,13 @@ private static function sqlCount(
}

/**
* @param array<string, Column> $columns
* @param FakePdoInterface $conn
* @param Scope $scope
* @param FunctionExpression $expr
* @param QueryResult $result
*
* @return ?numeric
* @return float|int|mixed|string|null
* @throws ProcessorException
*/
private static function sqlSum(
FakePdoInterface $conn,
Expand All @@ -368,6 +372,10 @@ private static function sqlSum(
$sum = 0;

if (!$result->rows) {
if ($expr instanceof FunctionExpression) {
return self::evaluate($conn, $scope, $expr, [], $result);
}

return null;
}

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

if ($args[0] instanceof ColumnExpression && empty($row)) {
return null;
}

/** @var string|null $dtValue */
$dtValue = Evaluator::evaluate($conn, $scope, $args[0], $row, $result);
/** @var string|null $fromTzValue */
Expand Down
40 changes: 40 additions & 0 deletions tests/EndToEndTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1516,4 +1516,44 @@ public function leastWithExceptionProvider(): iterable
yield ['Should fail with single argument' => [1]];
yield ['Should fail without any arguments' => []];
}

public function testNestedFunctions()
{
$pdo = self::getConnectionToFullDB();

$query = $pdo->prepare("
SELECT
SUM(
TIMESTAMPDIFF(
SECOND,
CONVERT_TZ('2025-12-31 22:59:59', 'Europe/Kyiv', 'Europe/Kyiv'),
CONVERT_TZ('2025-12-31 23:59:59', 'Europe/Kyiv', 'Europe/Kyiv')
)
)
");
$query->execute();

$this->assertSame(3600, (int)$query->fetchColumn());
}

public function testNestedFunctionsFromDB()
{
$pdo = self::getConnectionToFullDB();
$count = $pdo->query("SELECT COUNT(*) FROM video_game_characters")->fetchColumn();

$query = $pdo->prepare("
SELECT SUM(
TIMESTAMPDIFF(
SECOND,
CONVERT_TZ(`created_on`, 'Europe/Kyiv', 'Europe/Kyiv'),
CONVERT_TZ(`created_on` + INTERVAL 1 SECOND, 'Europe/Kyiv', 'Europe/Kyiv')
)
)
FROM `video_game_characters`
");

$query->execute();

$this->assertSame((int)$count, (int)$query->fetchColumn());
}
}
Loading