Skip to content

Default to distinct union queries #6439

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

Merged
merged 1 commit into from
Jun 18, 2024
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
8 changes: 4 additions & 4 deletions docs/en/reference/query-builder.rst
Original file line number Diff line number Diff line change
Expand Up @@ -322,17 +322,17 @@ To combine multiple ``SELECT`` queries into one result-set you can pass SQL Part
or QueryBuilder instances to one of the following methods:

* ``union(string|QueryBuilder $part)``
* ``addUnion(string|QueryBuilder $part, UnionType $type)``
* ``addUnion(string|QueryBuilder $part, UnionType $type = UnionType::DISTINCT)``

.. code-block:: php

<?php

$queryBuilder
->union('SELECT 1 AS field')
->addUnion('SELECT 2 AS field', UnionType::DISTINCT)
->addUnion('SELECT 3 AS field', UnionType::DISTINCT)
->addUnion('SELECT 3 as field', UnionType::DISTINCT);
->addUnion('SELECT 2 AS field')
->addUnion('SELECT 3 AS field')
->addUnion('SELECT 3 as field');

$queryBuilder
->union('SELECT 1 AS field')
Expand Down
2 changes: 1 addition & 1 deletion src/Query/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ public function union(string|QueryBuilder $part): self
*
* @return $this
*/
public function addUnion(string|QueryBuilder $part, UnionType $type): self
public function addUnion(string|QueryBuilder $part, UnionType $type = UnionType::DISTINCT): self
{
$this->type = QueryType::UNION;

Expand Down
15 changes: 14 additions & 1 deletion tests/Functional/Query/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function testUnionAllReturnsExpectedResult(): void
self::assertSame($expectedRows, $qb->executeQuery()->fetchAllAssociative());
}

public function testUnionReturnsExpectedResult(): void
public function testUnionDistinctReturnsExpectedResult(): void
{
$expectedRows = $this->prepareExpectedRows([['field_one' => 1], ['field_one' => 2]]);
$platform = $this->connection->getDatabasePlatform();
Expand All @@ -137,6 +137,19 @@ public function testUnionReturnsExpectedResult(): void
self::assertSame($expectedRows, $qb->executeQuery()->fetchAllAssociative());
}

public function testUnionIsDistinctByDefault(): void
{
$expectedRows = $this->prepareExpectedRows([['field_one' => 1], ['field_one' => 2]]);
$platform = $this->connection->getDatabasePlatform();
$qb = $this->connection->createQueryBuilder();
$qb->union($platform->getDummySelectSQL('2 as field_one'))
->addUnion($platform->getDummySelectSQL('1 as field_one'))
->addUnion($platform->getDummySelectSQL('1 as field_one'))
->orderBy('field_one', 'ASC');

self::assertSame($expectedRows, $qb->executeQuery()->fetchAllAssociative());
}

public function testUnionWithDescOrderByReturnsExpectedResult(): void
{
$expectedRows = $this->prepareExpectedRows([['field_one' => 2], ['field_one' => 1]]);
Expand Down
9 changes: 9 additions & 0 deletions tests/Query/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1472,6 +1472,15 @@ public function testUnionAndAddUnionReturnsUnionQuery(): void
self::assertSame('SELECT 1 AS field_one UNION SELECT 2 as field_one', $qb->getSQL());
}

public function testUnionIsDistinctByDefault(): void
{
$qb = new QueryBuilder($this->conn);
$qb->union('SELECT 1 AS field_one')
->addUnion('SELECT 2 as field_one');

self::assertSame('SELECT 1 AS field_one UNION SELECT 2 as field_one', $qb->getSQL());
}

public function testUnionAndOrderByReturnsUnionQueryWithOrderBy(): void
{
$qb = new QueryBuilder($this->conn);
Expand Down
Loading