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

Revert "[11.x] Support DB aggregate by group (new methods)" #54196

Merged
merged 1 commit into from
Jan 15, 2025
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
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
Loading