Skip to content

Commit

Permalink
Fixed groupLimit functionality (#87)
Browse files Browse the repository at this point in the history
This functionality was added in Laravel 11.
MySQL driver checks the version of the database and if it is less than 8 then generates a query with unsupported syntax for UDV.
It seems that functionality for MySQL >= 8 works well with SingleStore.
Added test that checks this.

---------

Co-authored-by: AdalbertMemSQL <[email protected]>
  • Loading branch information
AdalbertMemSQL and AdalbertMemSQL authored Jul 3, 2024
1 parent 8e6b5b4 commit d15a998
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 12 deletions.
4 changes: 1 addition & 3 deletions src/Connect/Connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

use Illuminate\Database\Connectors\MySqlConnector;

class Connector extends MySqlConnector
{
}
class Connector extends MySqlConnector {}
4 changes: 1 addition & 3 deletions src/Exceptions/SingleStoreDriverException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@

namespace SingleStore\Laravel\Exceptions;

class SingleStoreDriverException extends \Exception
{
}
class SingleStoreDriverException extends \Exception {}
4 changes: 1 addition & 3 deletions src/Exceptions/UnsupportedFunctionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@

namespace SingleStore\Laravel\Exceptions;

class UnsupportedFunctionException extends SingleStoreDriverException
{
}
class UnsupportedFunctionException extends SingleStoreDriverException {}
4 changes: 1 addition & 3 deletions src/Fluency/SpatialIndexCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@
/**
* @method $this resolution(int $resolution) The spatial index resolution
*/
class SpatialIndexCommand extends Fluent
{
}
class SpatialIndexCommand extends Fluent {}
5 changes: 5 additions & 0 deletions src/Query/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,9 @@ protected function compileDeleteWithJoins(Builder $query, $table, $where): strin

return "delete {$deleteTable} from {$table} {$joins} {$where}";
}

public function useLegacyGroupLimit(Builder $query)
{
return false;
}
}
54 changes: 54 additions & 0 deletions tests/Hybrid/GroupLimitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace SingleStore\Laravel\Tests\Hybrid;

use Illuminate\Foundation\Application;
use Illuminate\Support\Facades\DB;
use SingleStore\Laravel\Schema\Blueprint;
use SingleStore\Laravel\Tests\BaseTest;

class GroupLimitTest extends BaseTest
{
use HybridTestHelpers;

/** @test */
public function groupLimit()
{
if (version_compare(Application::VERSION, '11.0.0', '<')) {
// fulltext not added until later on in laravel 8 releases
$this->markTestSkipped('requires higher laravel version');

return;
}

$query = DB::table('test')->orderBy('id')->groupLimit(2, 'group');
$this->assertEquals(
'select * from (select *, row_number() over (partition by `group` order by `id` asc) as `laravel_row` from `test`) as `laravel_table` where `laravel_row` <= 2 order by `laravel_row`',
$query->toSql()
);

if (! $this->runHybridIntegrations()) {
return;
}

$this->createTable(function (Blueprint $table) {
$table->id();
$table->integer('group');
});

DB::table('test')->insert([
['id' => 1, 'group' => 1],
['id' => 2, 'group' => 1],
['id' => 3, 'group' => 1],
['id' => 4, 'group' => 2],
['id' => 5, 'group' => 3],
['id' => 6, 'group' => 3],
['id' => 7, 'group' => 3],
['id' => 8, 'group' => 3],
]);

$ids = $query->get(['id'])->pluck('id')->toArray();
sort($ids);
$this->assertEquals($ids, [1, 2, 4, 5, 6]);
}
}

0 comments on commit d15a998

Please sign in to comment.