Skip to content

Commit d15a998

Browse files
Fixed groupLimit functionality (#87)
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 <AdalbertMemSQL@users.noreply.github.com>
1 parent 8e6b5b4 commit d15a998

File tree

6 files changed

+63
-12
lines changed

6 files changed

+63
-12
lines changed

src/Connect/Connector.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,4 @@
44

55
use Illuminate\Database\Connectors\MySqlConnector;
66

7-
class Connector extends MySqlConnector
8-
{
9-
}
7+
class Connector extends MySqlConnector {}

src/Exceptions/SingleStoreDriverException.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,4 @@
22

33
namespace SingleStore\Laravel\Exceptions;
44

5-
class SingleStoreDriverException extends \Exception
6-
{
7-
}
5+
class SingleStoreDriverException extends \Exception {}

src/Exceptions/UnsupportedFunctionException.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,4 @@
22

33
namespace SingleStore\Laravel\Exceptions;
44

5-
class UnsupportedFunctionException extends SingleStoreDriverException
6-
{
7-
}
5+
class UnsupportedFunctionException extends SingleStoreDriverException {}

src/Fluency/SpatialIndexCommand.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,4 @@
77
/**
88
* @method $this resolution(int $resolution) The spatial index resolution
99
*/
10-
class SpatialIndexCommand extends Fluent
11-
{
12-
}
10+
class SpatialIndexCommand extends Fluent {}

src/Query/Grammar.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,9 @@ protected function compileDeleteWithJoins(Builder $query, $table, $where): strin
271271

272272
return "delete {$deleteTable} from {$table} {$joins} {$where}";
273273
}
274+
275+
public function useLegacyGroupLimit(Builder $query)
276+
{
277+
return false;
278+
}
274279
}

tests/Hybrid/GroupLimitTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace SingleStore\Laravel\Tests\Hybrid;
4+
5+
use Illuminate\Foundation\Application;
6+
use Illuminate\Support\Facades\DB;
7+
use SingleStore\Laravel\Schema\Blueprint;
8+
use SingleStore\Laravel\Tests\BaseTest;
9+
10+
class GroupLimitTest extends BaseTest
11+
{
12+
use HybridTestHelpers;
13+
14+
/** @test */
15+
public function groupLimit()
16+
{
17+
if (version_compare(Application::VERSION, '11.0.0', '<')) {
18+
// fulltext not added until later on in laravel 8 releases
19+
$this->markTestSkipped('requires higher laravel version');
20+
21+
return;
22+
}
23+
24+
$query = DB::table('test')->orderBy('id')->groupLimit(2, 'group');
25+
$this->assertEquals(
26+
'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`',
27+
$query->toSql()
28+
);
29+
30+
if (! $this->runHybridIntegrations()) {
31+
return;
32+
}
33+
34+
$this->createTable(function (Blueprint $table) {
35+
$table->id();
36+
$table->integer('group');
37+
});
38+
39+
DB::table('test')->insert([
40+
['id' => 1, 'group' => 1],
41+
['id' => 2, 'group' => 1],
42+
['id' => 3, 'group' => 1],
43+
['id' => 4, 'group' => 2],
44+
['id' => 5, 'group' => 3],
45+
['id' => 6, 'group' => 3],
46+
['id' => 7, 'group' => 3],
47+
['id' => 8, 'group' => 3],
48+
]);
49+
50+
$ids = $query->get(['id'])->pluck('id')->toArray();
51+
sort($ids);
52+
$this->assertEquals($ids, [1, 2, 4, 5, 6]);
53+
}
54+
}

0 commit comments

Comments
 (0)