Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make CountWalker use COUNT(*) when $distinct is explicitly set to false (#11552) #11557

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
36 changes: 20 additions & 16 deletions src/Tools/Pagination/CountWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,31 @@ public function walkSelectStatement(SelectStatement $selectStatement): void
throw new RuntimeException('Cannot count query which selects two FROM components, cannot make distinction');
}

$fromRoot = reset($from);
$rootAlias = $fromRoot->rangeVariableDeclaration->aliasIdentificationVariable;
$rootClass = $this->getMetadataForDqlAlias($rootAlias);
$identifierFieldName = $rootClass->getSingleIdentifierFieldName();
$distinct = $this->_getQuery()->getHint(self::HINT_DISTINCT);

$pathType = PathExpression::TYPE_STATE_FIELD;
if (isset($rootClass->associationMappings[$identifierFieldName])) {
$pathType = PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION;
}
$countPathExpressionOrLiteral = '*';
if ($distinct) {
$fromRoot = reset($from);
$rootAlias = $fromRoot->rangeVariableDeclaration->aliasIdentificationVariable;
$rootClass = $this->getMetadataForDqlAlias($rootAlias);
$identifierFieldName = $rootClass->getSingleIdentifierFieldName();

$pathType = PathExpression::TYPE_STATE_FIELD;
if (isset($rootClass->associationMappings[$identifierFieldName])) {
$pathType = PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION;
}

$pathExpression = new PathExpression(
PathExpression::TYPE_STATE_FIELD | PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION,
$rootAlias,
$identifierFieldName,
);
$pathExpression->type = $pathType;
$countPathExpressionOrLiteral = new PathExpression(
PathExpression::TYPE_STATE_FIELD | PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION,
$rootAlias,
$identifierFieldName,
);
$countPathExpressionOrLiteral->type = $pathType;
}

$distinct = $this->_getQuery()->getHint(self::HINT_DISTINCT);
$selectStatement->selectClause->selectExpressions = [
new SelectExpression(
new AggregateExpression('count', $pathExpression, $distinct),
new AggregateExpression('count', $countPathExpressionOrLiteral, $distinct),
null,
),
];
Expand Down
14 changes: 14 additions & 0 deletions tests/Tests/ORM/Tools/Pagination/CountWalkerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ public function testCountQuery(): void
);
}

public function testCountQueryWithoutDistinctUsesCountStar(): void
{
$query = $this->entityManager->createQuery(
'SELECT p, c, a FROM Doctrine\Tests\ORM\Tools\Pagination\BlogPost p JOIN p.category c JOIN p.author a',
);
$query->setHint(Query::HINT_CUSTOM_TREE_WALKERS, [CountWalker::class]);
$query->setHint(CountWalker::HINT_DISTINCT, false);

self::assertEquals(
'SELECT count(*) AS sclr_0 FROM BlogPost b0_ INNER JOIN Category c1_ ON b0_.category_id = c1_.id INNER JOIN Author a2_ ON b0_.author_id = a2_.id',
$query->getSQL(),
);
}

public function testCountQueryMixedResultsWithName(): void
{
$query = $this->entityManager->createQuery(
Expand Down
Loading