-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed incorrect generation of union queries (#46)
* Fixed incorrect generation of union queries * Changed to add SELECT * FROM before each union part. * Changed to wrap the query before adding a final order by/limit/offset for union queries. * This is needed because otherwise these clauses are applied only to the last union subquery. * Changed to don't run tests in the non-integration mode * Added more tests * Fixed case when offset is set without a limit clause
- Loading branch information
1 parent
9e31536
commit 943a303
Showing
2 changed files
with
245 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
<?php | ||
|
||
namespace SingleStore\Laravel\Tests\Hybrid; | ||
|
||
use Illuminate\Support\Facades\DB; | ||
use SingleStore\Laravel\Schema\Blueprint; | ||
use SingleStore\Laravel\Tests\BaseTest; | ||
|
||
class UnionTest extends BaseTest | ||
{ | ||
use HybridTestHelpers; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
if ($this->runHybridIntegrations()) { | ||
$this->createTable(function (Blueprint $table) { | ||
$table->id(); | ||
}); | ||
|
||
DB::table('test')->insert([ | ||
['id' => 1], | ||
['id' => 2], | ||
['id' => 3], | ||
['id' => 4], | ||
['id' => 100], | ||
]); | ||
} | ||
} | ||
|
||
/** @test */ | ||
function union() { | ||
if (! $this->runHybridIntegrations()) { | ||
return; | ||
} | ||
|
||
$first = DB::table('test')->where('id', '<', 3); | ||
$second = DB::table('test')->where('id', '>', 5); | ||
$res = $first->union($second)->get(); | ||
|
||
$indexes = array_map(function ($value): int { | ||
return $value->id; | ||
}, $res->toArray()); | ||
sort($indexes); | ||
|
||
$this->assertEquals([1, 2, 100], $indexes); | ||
} | ||
|
||
/** @test */ | ||
function unionAll() { | ||
if (! $this->runHybridIntegrations()) { | ||
return; | ||
} | ||
|
||
$first = DB::table('test')->where('id', '<', 4); | ||
$second = DB::table('test')->where('id', '>', 2); | ||
$res = $first->unionAll($second)->get(); | ||
|
||
$indexes = array_map(function ($value): int { | ||
return $value->id; | ||
}, $res->toArray()); | ||
sort($indexes); | ||
|
||
$this->assertEquals([1, 2, 3, 3, 4, 100], $indexes); | ||
} | ||
|
||
/** @test */ | ||
function unionWithOrderByLimitAndOffset() { | ||
if (! $this->runHybridIntegrations()) { | ||
return; | ||
} | ||
|
||
$first = DB::table('test')->where('id', '<', 3); | ||
$second = DB::table('test')->where('id', '>', 5); | ||
$res = $first->union($second)->orderBy('id')->limit(1)->offset(1)->get(); | ||
|
||
$indexes = array_map(function ($value): int { | ||
return $value->id; | ||
}, $res->toArray()); | ||
|
||
$this->assertEquals([2], $indexes); | ||
} | ||
|
||
/** @test */ | ||
function unionWithOrderBy() { | ||
if (! $this->runHybridIntegrations()) { | ||
return; | ||
} | ||
|
||
$first = DB::table('test')->where('id', '<', 3); | ||
$second = DB::table('test')->where('id', '>', 5); | ||
$res = $first->union($second)->orderBy('id')->get(); | ||
|
||
$indexes = array_map(function ($value): int { | ||
return $value->id; | ||
}, $res->toArray()); | ||
|
||
$this->assertEquals([1, 2, 100], $indexes); | ||
} | ||
|
||
/** @test */ | ||
function unionWithLimit() { | ||
if (! $this->runHybridIntegrations()) { | ||
return; | ||
} | ||
|
||
$first = DB::table('test')->where('id', '<', 3); | ||
$second = DB::table('test')->where('id', '>', 5); | ||
$res = $first->union($second)->limit(2)->get(); | ||
|
||
$this->assertCount(2, $res); | ||
} | ||
|
||
/** @test */ | ||
function unionWithOffset() { | ||
if (! $this->runHybridIntegrations()) { | ||
return; | ||
} | ||
|
||
$first = DB::table('test')->where('id', '<', 3); | ||
$second = DB::table('test')->where('id', '>', 5); | ||
$res = $first->union($second)->offset(1)->get(); | ||
|
||
$this->assertCount(2, $res); | ||
} | ||
|
||
/** @test */ | ||
function unionWithInnerOffset() { | ||
if (! $this->runHybridIntegrations()) { | ||
return; | ||
} | ||
|
||
$first = DB::table('test')->where('id', '<', 3)->offset(1)->orderBy('id'); | ||
$second = DB::table('test')->where('id', '>', 5); | ||
$res = $first->union($second)->get(); | ||
|
||
$indexes = array_map(function ($value): int { | ||
return $value->id; | ||
}, $res->toArray()); | ||
sort($indexes); | ||
|
||
$this->assertEquals([2, 100], $indexes); | ||
} | ||
} |