Skip to content

Commit cf51f73

Browse files
committed
add new option samplingExpression on create table
1 parent c6bf283 commit cf51f73

File tree

3 files changed

+71
-15
lines changed

3 files changed

+71
-15
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ foreach ($sqlArray as $sql) {
9797
```
9898

9999
```php
100-
// ***more options***
100+
// ***more options (optional)***
101101

102102
//specify table engine
103103
$newTable->addOption('engine', 'MergeTree');
@@ -112,7 +112,8 @@ $newTable->addOption('eventDateProviderColumn', 'updated_at');
112112
// *if specified -- event date column will be added with default value toDate(updated_at);
113113
// 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`
114114
// if the type of provider column is neither `string`, nor `date`, nor `datetime`, provider column values must contain a valid UNIX Timestamp
115-
115+
$newTable->addOption('samplingExpression', 'intHash32(id)');
116+
// samplingExpression -- a tuple that defines the table's primary key, and the index granularity
116117

117118
//specify index granularity
118119
$newTable->addOption('indexGranularity', 4096);

src/ClickHousePlatform.php

+18-5
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,8 @@ protected function _getCreateTableSQL($tableName, array $columns, array $options
659659
],
660660
true
661661
)) {
662-
$indexGranularity = ! empty($options['indexGranularity']) ? $options['indexGranularity'] : 8192;
662+
$indexGranularity = ! empty($options['indexGranularity']) ? $options['indexGranularity'] : 8192;
663+
$samplingExpression = '';
663664

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

749-
$engineOptions = '(' . $eventDateColumnName . ', (' . implode(
750-
', ',
751-
array_unique(array_values($options['primary']))
752-
) . '), ' . $indexGranularity;
750+
$primaryIndex = array_values($options['primary']);
751+
if (! empty($options['samplingExpression'])) {
752+
$samplingExpression = ', ' . $options['samplingExpression'];
753+
$primaryIndex[] = $options['samplingExpression'];
754+
}
755+
756+
$engineOptions = sprintf(
757+
'(%s%s, (%s), %d',
758+
$eventDateColumnName,
759+
$samplingExpression,
760+
implode(
761+
', ',
762+
array_unique($primaryIndex)
763+
),
764+
$indexGranularity
765+
);
753766

754767
/**
755768
* any specific MergeTree* table parameters

tests/CreateSchemaTest.php

+50-8
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ public function testCreateNewTableSQL()
5656
$newTable->setPrimaryKey(['id']);
5757

5858
$migrationSQLs = $fromSchema->getMigrateToSql($toSchema, $this->connection->getDatabasePlatform());
59-
$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));
59+
$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)",
60+
implode(';', $migrationSQLs));
6061
foreach ($migrationSQLs as $sql) {
6162
$this->connection->exec($sql);
6263
}
@@ -102,7 +103,8 @@ public function testIndexGranularityOption()
102103

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

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

167170
$migrationSQLs = $fromSchema->getMigrateToSql($toSchema, $this->connection->getDatabasePlatform());
168171
$generatedSQL = implode(';', $migrationSQLs);
169-
$this->assertEquals("CREATE TABLE test_table (event_date Date DEFAULT today(), id UInt32, payload String) ENGINE = ReplacingMergeTree(event_date, (id), 8192)", $generatedSQL);
172+
$this->assertEquals("CREATE TABLE test_table (event_date Date DEFAULT today(), id UInt32, payload String) ENGINE = ReplacingMergeTree(event_date, (id), 8192)",
173+
$generatedSQL);
170174
foreach ($migrationSQLs as $sql) {
171175
$this->connection->exec($sql);
172176
}
@@ -189,7 +193,8 @@ public function testEventDateColumnBadOption()
189193
$this->expectException(\Exception::class);
190194
$migrationSQLs = $fromSchema->getMigrateToSql($toSchema, $this->connection->getDatabasePlatform());
191195
$generatedSQL = implode(';', $migrationSQLs);
192-
$this->assertEquals("CREATE TABLE test_table (event_date Date DEFAULT today(), id UInt32, payload String) ENGINE = ReplacingMergeTree(event_date, (id), 8192)", $generatedSQL);
196+
$this->assertEquals("CREATE TABLE test_table (event_date Date DEFAULT today(), id UInt32, payload String) ENGINE = ReplacingMergeTree(event_date, (id), 8192)",
197+
$generatedSQL);
193198
foreach ($migrationSQLs as $sql) {
194199
$this->connection->exec($sql);
195200
}
@@ -211,7 +216,8 @@ public function testEventDateProviderColumnOption()
211216

212217
$migrationSQLs = $fromSchema->getMigrateToSql($toSchema, $this->connection->getDatabasePlatform());
213218
$generatedSQL = implode(';', $migrationSQLs);
214-
$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);
219+
$this->assertEquals("CREATE TABLE test_table (EventDate Date DEFAULT toDate(updated_at), id UInt32, payload String, updated_at DateTime) ENGINE = ReplacingMergeTree(EventDate, (id), 8192)",
220+
$generatedSQL);
215221
foreach ($migrationSQLs as $sql) {
216222
$this->connection->exec($sql);
217223
}
@@ -234,7 +240,8 @@ public function testEventDateProviderColumnBadOption()
234240
$this->expectException(\Exception::class);
235241
$migrationSQLs = $fromSchema->getMigrateToSql($toSchema, $this->connection->getDatabasePlatform());
236242
$generatedSQL = implode(';', $migrationSQLs);
237-
$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);
243+
$this->assertEquals("CREATE TABLE test_table (EventDate Date DEFAULT toDate(updated_at), id UInt32, payload String, updated_at DateTime) ENGINE = ReplacingMergeTree(EventDate, (id), 8192)",
244+
$generatedSQL);
238245
foreach ($migrationSQLs as $sql) {
239246
$this->connection->exec($sql);
240247
}
@@ -262,7 +269,7 @@ public function testListTableIndexes()
262269

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

265-
if($index = current($indexes)) {
272+
if ($index = current($indexes)) {
266273
$this->assertInstanceOf(Index::class, $index);
267274

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

282+
public function testTableWithSamplingExpression()
283+
{
284+
$fromSchema = $this->connection->getSchemaManager()->createSchema();
285+
$toSchema = clone $fromSchema;
286+
287+
$newTable = $toSchema->createTable('test_sampling_table');
288+
289+
$newTable->addColumn('id', 'integer', ['unsigned' => true]);
290+
$newTable->addColumn('payload', 'string');
291+
$newTable->addColumn('event_date', Type::DATE);
292+
$newTable->addOption('eventDateColumn', 'event_date');
293+
$newTable->addOption('samplingExpression', 'intHash32(id)');
294+
$newTable->setPrimaryKey(['id', 'event_date']);
295+
$migrationSQLs = $fromSchema->getMigrateToSql($toSchema, $this->connection->getDatabasePlatform());
296+
$generatedSQL = implode(';', $migrationSQLs);
297+
$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)",
298+
$generatedSQL);
299+
foreach ($migrationSQLs as $sql) {
300+
$this->connection->exec($sql);
301+
}
302+
303+
$indexes = $this->connection->getSchemaManager()->listTableIndexes('test_sampling_table');
304+
305+
$this->assertEquals(1, \count($indexes));
306+
307+
if ($index = current($indexes)) {
308+
$this->assertInstanceOf(Index::class, $index);
309+
310+
$this->assertEquals(['id', 'event_date'], $index->getColumns());
311+
$this->assertTrue($index->isPrimary());
312+
}
313+
314+
$this->connection->exec('DROP TABLE test_sampling_table');
315+
}
316+
275317
public function testNullableColumns()
276318
{
277319
$fromSchema = $this->connection->getSchemaManager()->createSchema();

0 commit comments

Comments
 (0)