fix: Correct SUM() evaluation for empty result sets #70
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR resolves a bug in
FunctionEvaluator.php
related to how aggregate functions (specificallySUM()
) are evaluated when the query returns an empty set. The issue stemmed from a previous change intended to handle scalar queries (e.g.,SELECT 1+1
) that inadvertently caused incorrect evaluation for table-based queries that return no rows.Changes
if ($expr instanceof FunctionExpression)
condition was too permissive. It was triggered when a query (e.g.,SELECT SUM(IF( id > 1, 0, 1 )) FROM video_game_characters WHERE id > 100;
) returned an empty result set, causing the code to incorrectly proceed with function evaluation as if it were a scalar query. This led to a bug.$isQueryWithoutFromClause
variable to precisely identify queries without a FROM clause. This check is based on whether the result object has columns. Theevaluate
method is now called only for these true scalar queries, ensuring thatSUM()
on an empty result from a table-based query correctly returnsNULL
.Impact
This fix ensures that the system adheres to standard SQL behavior for aggregate functions on empty sets and resolves the associated bug without affecting the correct evaluation of true scalar queries.