Skip to content

Commit

Permalink
Revert "[11.x] Support DB aggregate by group (new methods) (#53679)" (#…
Browse files Browse the repository at this point in the history
…54196)

This reverts commit 4b9aeae.
  • Loading branch information
crynobone authored Jan 15, 2025
1 parent d53756e commit 9085224
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 153 deletions.
70 changes: 0 additions & 70 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3561,17 +3561,6 @@ public function count($columns = '*')
return (int) $this->aggregate(__FUNCTION__, Arr::wrap($columns));
}

/**
* Retrieve the "count" of the distinct results of a given column for each group.
*
* @param \Illuminate\Contracts\Database\Query\Expression|string $columns
* @return \Illuminate\Support\Collection
*/
public function countByGroup($columns = '*')
{
return $this->aggregateByGroup('count', Arr::wrap($columns));
}

/**
* Retrieve the minimum value of a given column.
*
Expand All @@ -3583,17 +3572,6 @@ public function min($column)
return $this->aggregate(__FUNCTION__, [$column]);
}

/**
* Retrieve the minimum value of a given column by group.
*
* @param \Illuminate\Contracts\Database\Query\Expression|string $column
* @return \Illuminate\Support\Collection
*/
public function minByGroup($column)
{
return $this->aggregateByGroup('min', [$column]);
}

/**
* Retrieve the maximum value of a given column.
*
Expand All @@ -3605,17 +3583,6 @@ public function max($column)
return $this->aggregate(__FUNCTION__, [$column]);
}

/**
* Retrieve the maximum value of a given column by group.
*
* @param \Illuminate\Contracts\Database\Query\Expression|string $column
* @return \Illuminate\Support\Collection
*/
public function maxByGroup($column)
{
return $this->aggregateByGroup('max', [$column]);
}

/**
* Retrieve the sum of the values of a given column.
*
Expand All @@ -3629,17 +3596,6 @@ public function sum($column)
return $result ?: 0;
}

/**
* Retrieve the sum of the values of a given column by group.
*
* @param \Illuminate\Contracts\Database\Query\Expression|string $column
* @return \Illuminate\Support\Collection
*/
public function sumByGroup($column)
{
return $this->aggregateByGroup('sum', [$column]);
}

/**
* Retrieve the average of the values of a given column.
*
Expand All @@ -3651,17 +3607,6 @@ public function avg($column)
return $this->aggregate(__FUNCTION__, [$column]);
}

/**
* Retrieve the average of the values of a given column by group.
*
* @param \Illuminate\Contracts\Database\Query\Expression|string $column
* @return \Illuminate\Support\Collection
*/
public function avgByGroup($column)
{
return $this->aggregateByGroup('avg', [$column]);
}

/**
* Alias for the "avg" method.
*
Expand Down Expand Up @@ -3692,21 +3637,6 @@ public function aggregate($function, $columns = ['*'])
}
}

/**
* Execute an aggregate function for each group.
*
* @param string $function
* @param array $columns
* @return \Illuminate\Support\Collection
*/
public function aggregateByGroup(string $function, array $columns = ['*'])
{
return $this->cloneWithout($this->unions || $this->havings ? [] : ['columns'])
->cloneWithoutBindings($this->unions || $this->havings ? [] : ['select'])
->setAggregate($function, $columns)
->get($columns);
}

/**
* Execute a numeric aggregate function on the database.
*
Expand Down
14 changes: 2 additions & 12 deletions src/Illuminate/Database/Query/Grammars/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,7 @@ protected function compileAggregate(Builder $query, $aggregate)
$column = 'distinct '.$column;
}

$sql = 'select ';

$sql .= $aggregate['function'].'('.$column.') as aggregate';

if ($query->groups) {
$sql .= ', '.$this->columnize($query->groups);
}

return $sql;
return 'select '.$aggregate['function'].'('.$column.') as aggregate';
}

/**
Expand Down Expand Up @@ -1139,12 +1131,10 @@ protected function wrapUnion($sql)
protected function compileUnionAggregate(Builder $query)
{
$sql = $this->compileAggregate($query, $query->aggregate);
$groups = $query->groups ? ' '.$this->compileGroups($query, $query->groups) : '';

$query->aggregate = null;
$query->groups = null;

return $sql.' from ('.$this->compileSelect($query).') as '.$this->wrapTable('temp_table').$groups;
return $sql.' from ('.$this->compileSelect($query).') as '.$this->wrapTable('temp_table');
}

/**
Expand Down
71 changes: 0 additions & 71 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
use Illuminate\Pagination\Cursor;
use Illuminate\Pagination\CursorPaginator;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
use Illuminate\Tests\Database\Fixtures\Enums\Bar;
use InvalidArgumentException;
use Mockery as m;
Expand Down Expand Up @@ -1805,38 +1804,6 @@ public function testGroupBys()
$this->assertEquals(['whereRawBinding', 'groupByRawBinding', 'havingRawBinding'], $builder->getBindings());
}

public function testAggregateByGroup()
{
$builder = $this->getBuilder();

$queryResults = [['aggregate' => 2, 'role' => 'admin', 'city' => 'NY'], ['aggregate' => 5, 'role' => 'user', 'city' => 'LA']];
$builder->getConnection()
->shouldReceive('select')->once()
->with('select count(*) as aggregate, "role", "city" from "users" group by "role", "city"', [], true)
->andReturn($queryResults);
$builder->getProcessor()->shouldReceive('processSelect')->once()->andReturnUsing(fn ($builder, $results) => $results);
$builder->from('users')->groupBy('role', 'city');
$builder->aggregate = ['function' => 'count', 'columns' => ['*']];
$results = $builder->get();
$this->assertEquals($queryResults, $results->toArray());
}

public function testUnionAndAggregateByGroup()
{
$builder = $this->getBuilder();

$queryResults = [['aggregate' => 2, 'role' => 'admin'], ['aggregate' => 5, 'role' => 'user']];
$builder->getConnection()
->shouldReceive('select')->once()
->with('select count(*) as aggregate, "role" from ((select * from "users") union (select * from "members")) as "temp_table" group by "role"', [], true)
->andReturn($queryResults);
$builder->getProcessor()->shouldReceive('processSelect')->once()->andReturnUsing(fn ($builder, $results) => $results);
$results = $builder->from('users')
->union($this->getBuilder()->select('*')->from('members'))
->groupBy('role')->aggregateByGroup('count');
$this->assertEquals($queryResults, $results->toArray());
}

public function testOrderBys()
{
$builder = $this->getBuilder();
Expand Down Expand Up @@ -3497,44 +3464,6 @@ public function testAggregateFunctions()
$this->assertEquals(1, $results);
}

public function testAggregateFunctionsWithGroupBy()
{
$builder = $this->getBuilder();
$builder->getConnection()->shouldReceive('select')->once()->with('select count(*) as aggregate, "role" from "users" group by "role"', [], true)->andReturn([['role' => 'admin', 'aggregate' => 1]]);
$builder->getProcessor()->shouldReceive('processSelect')->once()->andReturnUsing(fn ($builder, $results) => $results);
$results = $builder->from('users')->groupBy('role')->countByGroup();
$this->assertInstanceOf(Collection::class, $results);
$this->assertEquals([['role' => 'admin', 'aggregate' => 1]], $results->toArray());

$builder = $this->getBuilder();
$builder->getConnection()->shouldReceive('select')->once()->with('select max("id") as aggregate, "role" from "users" group by "role"', [], true)->andReturn([['role' => 'admin', 'aggregate' => 1]]);
$builder->getProcessor()->shouldReceive('processSelect')->once()->andReturnUsing(fn ($builder, $results) => $results);
$results = $builder->from('users')->groupBy('role')->maxByGroup('id');
$this->assertInstanceOf(Collection::class, $results);
$this->assertEquals([['role' => 'admin', 'aggregate' => 1]], $results->toArray());

$builder = $this->getBuilder();
$builder->getConnection()->shouldReceive('select')->once()->with('select min("id") as aggregate, "role" from "users" group by "role"', [], true)->andReturn([['role' => 'admin', 'aggregate' => 1]]);
$builder->getProcessor()->shouldReceive('processSelect')->once()->andReturnUsing(fn ($builder, $results) => $results);
$results = $builder->from('users')->groupBy('role')->minByGroup('id');
$this->assertInstanceOf(Collection::class, $results);
$this->assertEquals([['role' => 'admin', 'aggregate' => 1]], $results->toArray());

$builder = $this->getBuilder();
$builder->getConnection()->shouldReceive('select')->once()->with('select sum("id") as aggregate, "role" from "users" group by "role"', [], true)->andReturn([['role' => 'admin', 'aggregate' => 1]]);
$builder->getProcessor()->shouldReceive('processSelect')->once()->andReturnUsing(fn ($builder, $results) => $results);
$results = $builder->from('users')->groupBy('role')->sumByGroup('id');
$this->assertInstanceOf(Collection::class, $results);
$this->assertEquals([['role' => 'admin', 'aggregate' => 1]], $results->toArray());

$builder = $this->getBuilder();
$builder->getConnection()->shouldReceive('select')->once()->with('select avg("id") as aggregate, "role" from "users" group by "role"', [], true)->andReturn([['role' => 'admin', 'aggregate' => 1]]);
$builder->getProcessor()->shouldReceive('processSelect')->once()->andReturnUsing(fn ($builder, $results) => $results);
$results = $builder->from('users')->groupBy('role')->avgByGroup('id');
$this->assertInstanceOf(Collection::class, $results);
$this->assertEquals([['role' => 'admin', 'aggregate' => 1]], $results->toArray());
}

public function testSqlServerExists()
{
$builder = $this->getSqlServerBuilder();
Expand Down

0 comments on commit 9085224

Please sign in to comment.