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

Add workaround for same-named columns in GROUP BY for MySQL #664

Merged
merged 1 commit into from
Apr 10, 2024
Merged
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
33 changes: 33 additions & 0 deletions src/Collection/DbalCollection.php
Original file line number Diff line number Diff line change
@@ -5,6 +5,8 @@

use Iterator;
use Nextras\Dbal\IConnection;
use Nextras\Dbal\Platforms\Data\Fqn;
use Nextras\Dbal\Platforms\MySqlPlatform;
use Nextras\Dbal\QueryBuilder\QueryBuilder;
use Nextras\Orm\Collection\Expression\ExpressionContext;
use Nextras\Orm\Collection\Functions\Result\DbalExpressionResult;
@@ -319,6 +321,9 @@ public function getQueryBuilder(): QueryBuilder
} else {
$this->queryBuilder->andWhere($expression->expression, ...$expression->args);
}
if ($this->mapper->getDatabasePlatform()->getName() === MySqlPlatform::NAME) {
$this->applyGroupByWithSameNamedColumnsWorkaround($this->queryBuilder, $groupBy);
}
$this->filtering = [];
}

@@ -400,4 +405,32 @@ protected function getHelper(): DbalQueryBuilderHelper

return $this->helper;
}


/**
* Apply workaround for MySQL that is not able to properly resolve columns when there are more same-named
* columns in the GROUP BY clause, even though they are properly referenced to their tables. Orm workarounds
* this by adding them to the SELECT clause and renames them not to conflict anywhere.
*
* @param list<Fqn> $groupBy
*/
private function applyGroupByWithSameNamedColumnsWorkaround(QueryBuilder $queryBuilder, array $groupBy): void
{
$map = [];
foreach ($groupBy as $fqn) {
if (!isset($map[$fqn->name])) {
$map[$fqn->name] = [$fqn];
} else {
$map[$fqn->name][] = $fqn;
}
}
$i = 0;
foreach ($map as $fqns) {
if (count($fqns) > 1) {
foreach ($fqns as $fqn) {
$queryBuilder->addSelect("%column AS __nextras_fix_" . $i++, $fqn); // @phpstan-ignore-line
}
}
}
}
}
36 changes: 36 additions & 0 deletions tests/cases/integration/Collection/collection.having.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php declare(strict_types = 1);

/**
* @testCase
* @dataProvider ../../../databases.ini
*/

namespace NextrasTests\Orm\Integration\Collection;


use Nextras\Orm\Collection\ICollection;
use NextrasTests\Orm\DataTestCase;
use Tester\Assert;


require_once __DIR__ . '/../../../bootstrap.php';


class CollectionHavingTest extends DataTestCase
{
public function testHavingWithSameNamedColumnsInGroupBy(): void
{
// this is a test especially for MySQL and Orm's workaround
$books = $this->orm->books->findBy([
ICollection::OR,
'tags->id' => 1,
'author->name' => 'Writer 1',
'publisher->name' => 'Nextras publisher A',
]);
Assert::same($books->count(), 3);
}
}


$test = new CollectionHavingTest();
$test->run();
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT "books".* FROM "books" AS "books" LEFT JOIN "books_x_tags" AS "books_x_tags" ON ("books"."id" = "books_x_tags"."book_id") LEFT JOIN "tags" AS "tags_any" ON (("books_x_tags"."tag_id" = "tags_any"."id") AND "tags_any"."id" = 1) LEFT JOIN "public"."authors" AS "author" ON ("books"."author_id" = "author"."id") LEFT JOIN "publishers" AS "publisher" ON ("books"."publisher_id" = "publisher"."publisher_id") GROUP BY "books"."id", "author"."name", "publisher"."name" HAVING ((COUNT("tags_any"."id") > 0) OR ("author"."name" = 'Writer 1') OR ("publisher"."name" = 'Nextras publisher A'));