Skip to content

Commit

Permalink
Default to distinct union queries (#6439)
Browse files Browse the repository at this point in the history
|      Q       |   A
|------------- | -----------
| Type         | improvement
| Fixed issues | Follows #6369

#### Summary

I think that people usually want to have a distinct union, if they build
a union query. This would also be in line with SQL where a `UNION`
without any additional keywords would produce a distinct union.

cc @sbuerk
  • Loading branch information
derrabus authored Jun 18, 2024
1 parent 5caaee4 commit 9cc0c0c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
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

0 comments on commit 9cc0c0c

Please sign in to comment.