Skip to content

Commit

Permalink
add new option samplingExpression on create table
Browse files Browse the repository at this point in the history
  • Loading branch information
argayash committed Jul 26, 2018
1 parent c6bf283 commit cf51f73
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 15 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ foreach ($sqlArray as $sql) {
```

```php
// ***more options***
// ***more options (optional)***

//specify table engine
$newTable->addOption('engine', 'MergeTree');
Expand All @@ -112,7 +112,8 @@ $newTable->addOption('eventDateProviderColumn', 'updated_at');
// *if specified -- event date column will be added with default value toDate(updated_at);
// if the type of the provider column is `string`, the valid format of provider column values must be either `YYYY-MM-DD` or `YYYY-MM-DD hh:mm:ss`
// if the type of provider column is neither `string`, nor `date`, nor `datetime`, provider column values must contain a valid UNIX Timestamp

$newTable->addOption('samplingExpression', 'intHash32(id)');
// samplingExpression -- a tuple that defines the table's primary key, and the index granularity

//specify index granularity
$newTable->addOption('indexGranularity', 4096);
Expand Down
23 changes: 18 additions & 5 deletions src/ClickHousePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,8 @@ protected function _getCreateTableSQL($tableName, array $columns, array $options
],
true
)) {
$indexGranularity = ! empty($options['indexGranularity']) ? $options['indexGranularity'] : 8192;
$indexGranularity = ! empty($options['indexGranularity']) ? $options['indexGranularity'] : 8192;
$samplingExpression = '';

/**
* eventDateColumn section
Expand Down Expand Up @@ -746,10 +747,22 @@ protected function _getCreateTableSQL($tableName, array $columns, array $options
throw new \Exception('You need specify PrimaryKey for MergeTree* tables');
}

$engineOptions = '(' . $eventDateColumnName . ', (' . implode(
', ',
array_unique(array_values($options['primary']))
) . '), ' . $indexGranularity;
$primaryIndex = array_values($options['primary']);
if (! empty($options['samplingExpression'])) {
$samplingExpression = ', ' . $options['samplingExpression'];
$primaryIndex[] = $options['samplingExpression'];
}

$engineOptions = sprintf(
'(%s%s, (%s), %d',
$eventDateColumnName,
$samplingExpression,
implode(
', ',
array_unique($primaryIndex)
),
$indexGranularity
);

/**
* any specific MergeTree* table parameters
Expand Down
58 changes: 50 additions & 8 deletions tests/CreateSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public function testCreateNewTableSQL()
$newTable->setPrimaryKey(['id']);

$migrationSQLs = $fromSchema->getMigrateToSql($toSchema, $this->connection->getDatabasePlatform());
$this->assertEquals("CREATE TABLE test_table (EventDate Date DEFAULT today(), id UInt32, payload String, oneVal Float64, twoVal String, flag UInt8, mask Int16, hash FixedString(32)) ENGINE = ReplacingMergeTree(EventDate, (id), 8192)", implode(';', $migrationSQLs));
$this->assertEquals("CREATE TABLE test_table (EventDate Date DEFAULT today(), id UInt32, payload String, oneVal Float64, twoVal String, flag UInt8, mask Int16, hash FixedString(32)) ENGINE = ReplacingMergeTree(EventDate, (id), 8192)",
implode(';', $migrationSQLs));
foreach ($migrationSQLs as $sql) {
$this->connection->exec($sql);
}
Expand Down Expand Up @@ -102,7 +103,8 @@ public function testIndexGranularityOption()

$migrationSQLs = $fromSchema->getMigrateToSql($toSchema, $this->connection->getDatabasePlatform());
$generatedSQL = implode(';', $migrationSQLs);
$this->assertEquals("CREATE TABLE test_table (EventDate Date DEFAULT today(), id UInt32, payload String) ENGINE = ReplacingMergeTree(EventDate, (id), 4096)", $generatedSQL);
$this->assertEquals("CREATE TABLE test_table (EventDate Date DEFAULT today(), id UInt32, payload String) ENGINE = ReplacingMergeTree(EventDate, (id), 4096)",
$generatedSQL);
foreach ($migrationSQLs as $sql) {
$this->connection->exec($sql);
}
Expand All @@ -123,7 +125,8 @@ public function testEngineMergeOption()

$migrationSQLs = $fromSchema->getMigrateToSql($toSchema, $this->connection->getDatabasePlatform());
$generatedSQL = implode(';', $migrationSQLs);
$this->assertEquals("CREATE TABLE test_table (EventDate Date DEFAULT today(), id UInt32, payload String) ENGINE = MergeTree(EventDate, (id), 8192)", $generatedSQL);
$this->assertEquals("CREATE TABLE test_table (EventDate Date DEFAULT today(), id UInt32, payload String) ENGINE = MergeTree(EventDate, (id), 8192)",
$generatedSQL);
foreach ($migrationSQLs as $sql) {
$this->connection->exec($sql);
}
Expand Down Expand Up @@ -166,7 +169,8 @@ public function testEventDateColumnOption()

$migrationSQLs = $fromSchema->getMigrateToSql($toSchema, $this->connection->getDatabasePlatform());
$generatedSQL = implode(';', $migrationSQLs);
$this->assertEquals("CREATE TABLE test_table (event_date Date DEFAULT today(), id UInt32, payload String) ENGINE = ReplacingMergeTree(event_date, (id), 8192)", $generatedSQL);
$this->assertEquals("CREATE TABLE test_table (event_date Date DEFAULT today(), id UInt32, payload String) ENGINE = ReplacingMergeTree(event_date, (id), 8192)",
$generatedSQL);
foreach ($migrationSQLs as $sql) {
$this->connection->exec($sql);
}
Expand All @@ -189,7 +193,8 @@ public function testEventDateColumnBadOption()
$this->expectException(\Exception::class);
$migrationSQLs = $fromSchema->getMigrateToSql($toSchema, $this->connection->getDatabasePlatform());
$generatedSQL = implode(';', $migrationSQLs);
$this->assertEquals("CREATE TABLE test_table (event_date Date DEFAULT today(), id UInt32, payload String) ENGINE = ReplacingMergeTree(event_date, (id), 8192)", $generatedSQL);
$this->assertEquals("CREATE TABLE test_table (event_date Date DEFAULT today(), id UInt32, payload String) ENGINE = ReplacingMergeTree(event_date, (id), 8192)",
$generatedSQL);
foreach ($migrationSQLs as $sql) {
$this->connection->exec($sql);
}
Expand All @@ -211,7 +216,8 @@ public function testEventDateProviderColumnOption()

$migrationSQLs = $fromSchema->getMigrateToSql($toSchema, $this->connection->getDatabasePlatform());
$generatedSQL = implode(';', $migrationSQLs);
$this->assertEquals("CREATE TABLE test_table (EventDate Date DEFAULT toDate(updated_at), id UInt32, payload String, updated_at DateTime) ENGINE = ReplacingMergeTree(EventDate, (id), 8192)", $generatedSQL);
$this->assertEquals("CREATE TABLE test_table (EventDate Date DEFAULT toDate(updated_at), id UInt32, payload String, updated_at DateTime) ENGINE = ReplacingMergeTree(EventDate, (id), 8192)",
$generatedSQL);
foreach ($migrationSQLs as $sql) {
$this->connection->exec($sql);
}
Expand All @@ -234,7 +240,8 @@ public function testEventDateProviderColumnBadOption()
$this->expectException(\Exception::class);
$migrationSQLs = $fromSchema->getMigrateToSql($toSchema, $this->connection->getDatabasePlatform());
$generatedSQL = implode(';', $migrationSQLs);
$this->assertEquals("CREATE TABLE test_table (EventDate Date DEFAULT toDate(updated_at), id UInt32, payload String, updated_at DateTime) ENGINE = ReplacingMergeTree(EventDate, (id), 8192)", $generatedSQL);
$this->assertEquals("CREATE TABLE test_table (EventDate Date DEFAULT toDate(updated_at), id UInt32, payload String, updated_at DateTime) ENGINE = ReplacingMergeTree(EventDate, (id), 8192)",
$generatedSQL);
foreach ($migrationSQLs as $sql) {
$this->connection->exec($sql);
}
Expand Down Expand Up @@ -262,7 +269,7 @@ public function testListTableIndexes()

$this->assertEquals(1, \count($indexes));

if($index = current($indexes)) {
if ($index = current($indexes)) {
$this->assertInstanceOf(Index::class, $index);

$this->assertEquals(['id', 'event_date'], $index->getColumns());
Expand All @@ -272,6 +279,41 @@ public function testListTableIndexes()
$this->connection->exec('DROP TABLE test_indexes_table');
}

public function testTableWithSamplingExpression()
{
$fromSchema = $this->connection->getSchemaManager()->createSchema();
$toSchema = clone $fromSchema;

$newTable = $toSchema->createTable('test_sampling_table');

$newTable->addColumn('id', 'integer', ['unsigned' => true]);
$newTable->addColumn('payload', 'string');
$newTable->addColumn('event_date', Type::DATE);
$newTable->addOption('eventDateColumn', 'event_date');
$newTable->addOption('samplingExpression', 'intHash32(id)');
$newTable->setPrimaryKey(['id', 'event_date']);
$migrationSQLs = $fromSchema->getMigrateToSql($toSchema, $this->connection->getDatabasePlatform());
$generatedSQL = implode(';', $migrationSQLs);
$this->assertEquals("CREATE TABLE test_sampling_table (event_date Date DEFAULT today(), id UInt32, payload String) ENGINE = ReplacingMergeTree(event_date, intHash32(id), (id, event_date, intHash32(id)), 8192)",
$generatedSQL);
foreach ($migrationSQLs as $sql) {
$this->connection->exec($sql);
}

$indexes = $this->connection->getSchemaManager()->listTableIndexes('test_sampling_table');

$this->assertEquals(1, \count($indexes));

if ($index = current($indexes)) {
$this->assertInstanceOf(Index::class, $index);

$this->assertEquals(['id', 'event_date'], $index->getColumns());
$this->assertTrue($index->isPrimary());
}

$this->connection->exec('DROP TABLE test_sampling_table');
}

public function testNullableColumns()
{
$fromSchema = $this->connection->getSchemaManager()->createSchema();
Expand Down

0 comments on commit cf51f73

Please sign in to comment.