Skip to content

Commit

Permalink
Fix column sorting with GROUP BY
Browse files Browse the repository at this point in the history
Signed-off-by: Maximilian Krög <maxi_kroeg@web.de>
  • Loading branch information
MoonE committed Jun 1, 2024
1 parent 866a141 commit ac9f6e8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 26 deletions.
23 changes: 15 additions & 8 deletions libraries/classes/Sql.php
Original file line number Diff line number Diff line change
Expand Up @@ -738,24 +738,31 @@ private function countQueryResults(
->countRecords(true);
}
} else {
/** @var SelectStatement $statement */
$statement = $analyzedSqlResults['statement'];

$changeOrder = $analyzedSqlResults['order'] !== false;
$changeLimit = $analyzedSqlResults['limit'] !== false;
$changeExpression = $analyzedSqlResults['is_group'] === false
&& $analyzedSqlResults['distinct'] === false
&& $analyzedSqlResults['union'] === false
&& count($statement->expr) === 1;

if ($changeOrder || $changeLimit || $changeExpression) {
$statement = clone $statement;
}

// Remove ORDER BY to decrease unnecessary sorting time
if ($analyzedSqlResults['order'] !== false) {
if ($changeOrder) {
$statement->order = null;
}

// Removes LIMIT clause that might have been added
if ($analyzedSqlResults['limit'] !== null) {
if ($changeLimit) {
$statement->limit = null;
}

if (
$analyzedSqlResults['is_group'] === false
&& $analyzedSqlResults['distinct'] === false
&& $analyzedSqlResults['union'] === false
&& count($statement->expr) === 1
) {
if ($changeExpression) {
$statement->expr[0] = new Expression();
$statement->expr[0]->expr = '1';
}
Expand Down
23 changes: 5 additions & 18 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13155,7 +13155,7 @@
<LessSpecificReturnStatement occurrences="1">
<code>$unlimNumRows</code>
</LessSpecificReturnStatement>
<MixedArgument occurrences="42">
<MixedArgument occurrences="41">
<code>$analyzedSqlResults</code>
<code>$analyzedSqlResults['is_affected']</code>
<code>$analyzedSqlResults['is_analyse']</code>
Expand Down Expand Up @@ -13193,7 +13193,6 @@
<code>$oneResult['Duration']</code>
<code>$oneResult['Status']</code>
<code>$sortCol</code>
<code>$statement-&gt;expr</code>
<code>$table</code>
<code>$unlimNumRows</code>
<code>$unlimNumRows</code>
Expand Down Expand Up @@ -13224,12 +13223,11 @@
<code>$oneResult['Duration']</code>
<code>$oneResult['Status']</code>
</MixedArrayAccess>
<MixedArrayAssignment occurrences="3">
<MixedArrayAssignment occurrences="2">
<code>$_SESSION['tmpval']['pos']</code>
<code>$_SESSION['tmpval']['possible_as_geometry']</code>
<code>$statement-&gt;expr[0]</code>
</MixedArrayAssignment>
<MixedAssignment occurrences="21">
<MixedAssignment occurrences="20">
<code>$maxRows</code>
<code>$oneFieldMeta</code>
<code>$oneMeta</code>
Expand All @@ -13244,7 +13242,6 @@
<code>$sortCol</code>
<code>$statement</code>
<code>$statement</code>
<code>$statement</code>
<code>$table</code>
<code>$unlimNumRows</code>
<code>$unlimNumRows</code>
Expand All @@ -13258,10 +13255,7 @@
<code>int|numeric-string</code>
<code>string</code>
</MixedInferredReturnType>
<MixedMethodCall occurrences="1">
<code>build</code>
</MixedMethodCall>
<MixedOperand occurrences="10">
<MixedOperand occurrences="9">
<code>$GLOBALS['cfg']['TablePrimaryKeyOrder']</code>
<code>$_SESSION['tmpval']['max_rows']</code>
<code>$_SESSION['tmpval']['pos']</code>
Expand All @@ -13271,14 +13265,8 @@
<code>$oneResult['Duration']</code>
<code>$profiling['chart'][$status]</code>
<code>$profiling['states'][$status]['total_time']</code>
<code>$statement-&gt;build()</code>
</MixedOperand>
<MixedPropertyAssignment occurrences="3">
<code>$statement</code>
<code>$statement</code>
<code>$statement</code>
</MixedPropertyAssignment>
<MixedPropertyFetch occurrences="9">
<MixedPropertyFetch occurrences="8">
<code>$analyzedSqlResults['parser']-&gt;errors</code>
<code>$analyzedSqlResults['parser']-&gt;list</code>
<code>$analyzedSqlResults['parser']-&gt;list</code>
Expand All @@ -13287,7 +13275,6 @@
<code>$analyzedSqlResults['statement']-&gt;where[0]-&gt;expr</code>
<code>$oneFieldMeta-&gt;table</code>
<code>$oneMeta-&gt;name</code>
<code>$statement-&gt;expr</code>
</MixedPropertyFetch>
<MixedReturnStatement occurrences="4">
<code>$pos</code>
Expand Down

0 comments on commit ac9f6e8

Please sign in to comment.