Skip to content

Commit

Permalink
Fix UNION aggregate queries with columns (#26466)
Browse files Browse the repository at this point in the history
  • Loading branch information
staudenmeir authored and taylorotwell committed Nov 10, 2018
1 parent 66c248e commit ddb907a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2130,8 +2130,10 @@ public function getCountForPagination($columns = ['*'])
*/
protected function runPaginationCountQuery($columns = ['*'])
{
return $this->cloneWithout(['columns', 'orders', 'limit', 'offset'])
->cloneWithoutBindings(['select', 'order'])
$without = $this->unions ? ['orders', 'limit', 'offset'] : ['columns', 'orders', 'limit', 'offset'];

return $this->cloneWithout($without)
->cloneWithoutBindings($this->unions ? ['order'] : ['select', 'order'])
->setAggregate('count', $this->withoutSelectAliases($columns))
->get()->all();
}
Expand Down Expand Up @@ -2444,8 +2446,8 @@ public function average($column)
*/
public function aggregate($function, $columns = ['*'])
{
$results = $this->cloneWithout(['columns'])
->cloneWithoutBindings(['select'])
$results = $this->cloneWithout($this->unions ? [] : ['columns'])
->cloneWithoutBindings($this->unions ? [] : ['select'])
->setAggregate($function, $columns)
->get($columns);

Expand Down
20 changes: 20 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,12 @@ public function testUnionAggregate()
$builder->getProcessor()->shouldReceive('processSelect')->once();
$builder->from('posts')->union($this->getMySqlBuilder()->from('videos'))->count();

$expected = 'select count(*) as aggregate from ((select `id` from `posts`) union (select `id` from `videos`)) as `temp_table`';
$builder = $this->getMySqlBuilder();
$builder->getConnection()->shouldReceive('select')->once()->with($expected, [], true);
$builder->getProcessor()->shouldReceive('processSelect')->once();
$builder->from('posts')->select('id')->union($this->getMySqlBuilder()->from('videos')->select('id'))->count();

$expected = 'select count(*) as aggregate from (select * from "posts" union select * from "videos") as "temp_table"';
$builder = $this->getPostgresBuilder();
$builder->getConnection()->shouldReceive('select')->once()->with($expected, [], true);
Expand Down Expand Up @@ -1095,6 +1101,20 @@ public function testGetCountForPaginationWithColumnAliases()
$this->assertEquals(1, $count);
}

public function testGetCountForPaginationWithUnion()
{
$builder = $this->getBuilder();
$builder->from('posts')->select('id')->union($this->getBuilder()->from('videos')->select('id'));

$builder->getConnection()->shouldReceive('select')->once()->with('select count(*) as aggregate from (select "id" from "posts" union select "id" from "videos") as "temp_table"', [], true)->andReturn([['aggregate' => 1]]);
$builder->getProcessor()->shouldReceive('processSelect')->once()->andReturnUsing(function ($builder, $results) {
return $results;
});

$count = $builder->getCountForPagination();
$this->assertEquals(1, $count);
}

public function testWhereShortcut()
{
$builder = $this->getBuilder();
Expand Down

0 comments on commit ddb907a

Please sign in to comment.