Skip to content

Commit

Permalink
Merge pull request #25 from srdante/with-variables
Browse files Browse the repository at this point in the history
Implement with statement for sortKey
  • Loading branch information
Carl Sverre authored Sep 20, 2022
2 parents 4afaa50 + 3ad2e27 commit ab7fada
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,22 @@ Schema::create('table', function (Blueprint $table) {
});
```

### Sort Keys - Columnstore variables

Sometimes you may want to customize your [columstore variables](https://docs.singlestore.com/managed-service/en/create-a-database/physical-database-schema-design/procedures-for-physical-database-schema-design/configuring-the-columnstore-to-work-effectively.html) individually per table. You can do it by appending `with` fluently to the `sortKey` definition.

```php
Schema::create('table', function (Blueprint $table) {
$table->string('name');

$table->sortKey('name')->with(['columnstore_segment_rows' => 100000]);
});

Schema::create('table', function (Blueprint $table) {
$table->string('name')->sortKey()->with(['columnstore_segment_rows' => 100000]);
});
```

### Series Timestamps
To denote a column as a series timestamp, use the `seriesTimestamp` column modifier.

Expand Down
10 changes: 8 additions & 2 deletions src/Schema/Blueprint/InlinesIndexes.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,15 @@ protected function addFluentSingleStoreIndexes()
foreach ($this->singleStoreIndexes as $index) {
if (isset($column->{$index})) {
if ($column->{$index} === true) {
$this->{$index}($column->name);
$command = $this->{$index}($column->name);
} else {
$this->{$index}($column->name, $column->{$index});
$command = $this->{$index}($column->name, $column->{$index});
}

// Forward with attributes if sortKey
if ($index === 'sortKey' && isset($column->with)) {
$command->with($column->with);
$column->with = null;
}

$column->{$index} = false;
Expand Down
8 changes: 8 additions & 0 deletions src/Schema/Grammar/CompilesKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ public function compileShardKey(Blueprint $blueprint, Fluent $command)

public function compileSortKey(Blueprint $blueprint, Fluent $command)
{
if (is_array($command->with)) {
$compiled = collect($command->with)->map(function ($value, $variable) {
return "{$variable}={$value}";
})->join(',');

return "sort key({$this->columnize($command->columns)} {$command->direction}) with ({$compiled})";
}

return "sort key({$this->columnize($command->columns)} {$command->direction})";
}

Expand Down
43 changes: 43 additions & 0 deletions tests/Hybrid/CreateTable/SortKeysTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,47 @@ public function shard_and_sort_keys()
'create table `test` (`name` varchar(255) not null, shard key(`name`), sort key(`name` asc))'
);
}

/** @test */
public function it_adds_a_sort_key_with_with_statement()
{
$blueprint = $this->createTable(function (Blueprint $table) {
$table->string('name');
$table->sortKey('name')->with(['columnstore_segment_rows' => 100000]);
});

$this->assertCreateStatement(
$blueprint,
'create table `test` (`name` varchar(255) not null, sort key(`name` asc) with (columnstore_segment_rows=100000))'
);
}

/** @test */
public function it_adds_a_sort_key_fluent_with_with_statement()
{
$blueprint = $this->createTable(function (Blueprint $table) {
$table->string('name')->sortKey()->with(['columnstore_segment_rows' => 100000]);
});

$this->assertCreateStatement(
$blueprint,
'create table `test` (`name` varchar(255) not null, sort key(`name` asc) with (columnstore_segment_rows=100000))'
);
}

/** @test */
public function it_adds_a_sort_key_fluent_with_dual_with_statement()
{
$blueprint = $this->createTable(function (Blueprint $table) {
$table->string('name')->sortKey()->with([
'columnstore_segment_rows' => 100000,
'columnstore_flush_bytes' => 4194304,
]);
});

$this->assertCreateStatement(
$blueprint,
'create table `test` (`name` varchar(255) not null, sort key(`name` asc) with (columnstore_segment_rows=100000,columnstore_flush_bytes=4194304))'
);
}
}

0 comments on commit ab7fada

Please sign in to comment.