-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClickHouse.php
More file actions
360 lines (296 loc) · 12.9 KB
/
ClickHouse.php
File metadata and controls
360 lines (296 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
<?php
namespace Utopia\Query\Schema;
use Utopia\Query\Builder;
use Utopia\Query\Builder\Statement;
use Utopia\Query\Exception\UnsupportedException;
use Utopia\Query\Exception\ValidationException;
use Utopia\Query\QuotesIdentifiers;
use Utopia\Query\Schema;
use Utopia\Query\Schema\ClickHouse\Engine;
use Utopia\Query\Schema\Feature\ColumnComments;
use Utopia\Query\Schema\Feature\DropPartition;
use Utopia\Query\Schema\Feature\TableComments;
class ClickHouse extends Schema implements TableComments, ColumnComments, DropPartition
{
use QuotesIdentifiers;
protected function compileColumnType(Column $column): string
{
if ($column->userTypeName !== null) {
throw new UnsupportedException('User-defined types are not supported in ClickHouse.');
}
$type = match ($column->type) {
ColumnType::String, ColumnType::Varchar, ColumnType::Relationship => 'String',
ColumnType::Text => 'String',
ColumnType::MediumText, ColumnType::LongText => 'String',
ColumnType::Integer => $column->isUnsigned ? 'UInt32' : 'Int32',
ColumnType::BigInteger, ColumnType::Id => $column->isUnsigned ? 'UInt64' : 'Int64',
ColumnType::Float, ColumnType::Double => 'Float64',
ColumnType::Boolean => 'UInt8',
ColumnType::Datetime => $column->precision ? 'DateTime64(' . $column->precision . ')' : 'DateTime',
ColumnType::Timestamp => $column->precision ? 'DateTime64(' . $column->precision . ')' : 'DateTime',
ColumnType::Json, ColumnType::Object => 'String',
ColumnType::Binary => 'String',
ColumnType::Enum => $this->compileClickHouseEnum($column->enumValues),
ColumnType::Point => 'Tuple(Float64, Float64)',
ColumnType::Linestring => 'Array(Tuple(Float64, Float64))',
ColumnType::Polygon => 'Array(Array(Tuple(Float64, Float64)))',
ColumnType::Uuid7 => 'FixedString(36)',
ColumnType::Vector => 'Array(Float64)',
ColumnType::Serial, ColumnType::BigSerial, ColumnType::SmallSerial => throw new UnsupportedException('SERIAL types are not supported in ClickHouse.'),
};
if ($column->isNullable) {
$type = 'Nullable(' . $type . ')';
}
return $type;
}
protected function compileAutoIncrement(): string
{
return '';
}
protected function compileUnsigned(): string
{
return '';
}
protected function compileColumnDefinition(Column $column): string
{
if ($column->generatedExpression !== null) {
throw new UnsupportedException('Generated columns are not supported in ClickHouse.');
}
if ($column->checkExpression !== null) {
throw new UnsupportedException('CHECK constraints are not supported in ClickHouse.');
}
$parts = [
$this->quote($column->name),
$this->compileColumnType($column),
];
if ($column->hasDefault) {
$parts[] = 'DEFAULT ' . $this->compileDefaultValue($column->default);
}
if ($column->ttl !== null) {
$parts[] = 'TTL ' . $column->ttl;
}
if ($column->comment !== null) {
$parts[] = "COMMENT '" . \str_replace(['\\', "'"], ['\\\\', "''"], $column->comment) . "'";
}
return \implode(' ', $parts);
}
public function dropIndex(string $table, string $name): Statement
{
return new Statement(
'ALTER TABLE ' . $this->quote($table)
. ' DROP INDEX ' . $this->quote($name),
[],
executor: $this->executor,
);
}
#[\Override]
public function compileAlter(Table $table): Statement
{
$alterations = [];
foreach ($table->columns as $column) {
$keyword = $column->isModify ? 'MODIFY COLUMN' : 'ADD COLUMN';
$alterations[] = $keyword . ' ' . $this->compileColumnDefinition($column);
}
foreach ($table->renameColumns as $rename) {
$alterations[] = 'RENAME COLUMN ' . $this->quote($rename->from)
. ' TO ' . $this->quote($rename->to);
}
foreach ($table->dropColumns as $col) {
$alterations[] = 'DROP COLUMN ' . $this->quote($col);
}
foreach ($table->dropIndexes as $name) {
$alterations[] = 'DROP INDEX ' . $this->quote($name);
}
foreach ($table->indexes as $index) {
if ($index->type !== IndexType::Index) {
throw new UnsupportedException(
'Only data-skipping indexes (index()) are supported in ClickHouse ALTER TABLE.'
);
}
$alterations[] = 'ADD ' . $this->compileSkipIndex($index);
}
if (! empty($table->foreignKeys)) {
throw new UnsupportedException('Foreign keys are not supported in ClickHouse.');
}
if (! empty($table->dropForeignKeys)) {
throw new UnsupportedException('Foreign keys are not supported in ClickHouse.');
}
if (! empty($table->settings)) {
throw new UnsupportedException(
'Table SETTINGS can only be set on CREATE TABLE; emit `ALTER TABLE ... MODIFY SETTING` directly to change them.'
);
}
if (empty($alterations)) {
throw new ValidationException('ALTER TABLE requires at least one alteration.');
}
$sql = 'ALTER TABLE ' . $this->quote($table->name)
. ' ' . \implode(', ', $alterations);
return new Statement($sql, [], executor: $this->executor);
}
#[\Override]
public function compileCreate(Table $table, bool $ifNotExists = false): Statement
{
$columnDefs = [];
$primaryKeys = [];
foreach ($table->columns as $column) {
$def = $this->compileColumnDefinition($column);
$columnDefs[] = $def;
if ($column->isPrimary) {
$primaryKeys[] = $this->quote($column->name);
}
}
if (! empty($table->compositePrimaryKey) && ! empty($primaryKeys)) {
throw new ValidationException('Cannot combine column-level primary() with Table::primary() composite key.');
}
if (empty($primaryKeys) && ! empty($table->compositePrimaryKey)) {
$primaryKeys = \array_map(fn (string $c): string => $this->quote($c), $table->compositePrimaryKey);
}
foreach ($table->indexes as $index) {
if ($index->type !== IndexType::Index) {
throw new UnsupportedException(
'Only data-skipping indexes (index()) are supported in ClickHouse CREATE TABLE.'
);
}
$columnDefs[] = $this->compileSkipIndex($index);
}
if (! empty($table->foreignKeys)) {
throw new UnsupportedException('Foreign keys are not supported in ClickHouse.');
}
if (! empty($table->checks)) {
throw new UnsupportedException('CHECK constraints are not supported in ClickHouse.');
}
$engine = $table->engine ?? Engine::MergeTree;
$sql = 'CREATE TABLE ' . ($ifNotExists ? 'IF NOT EXISTS ' : '') . $this->quote($table->name)
. ' (' . \implode(', ', $columnDefs) . ')'
. ' ENGINE = ' . $this->compileEngine($engine, $table->engineArgs);
if ($table->partitionType !== null) {
$sql .= ' PARTITION BY ' . $table->partitionExpression;
}
if ($engine->requiresOrderBy()) {
$orderBy = ! empty($table->orderBy)
? \array_map(fn (string $c): string => $this->quote($c), $table->orderBy)
: $primaryKeys;
$sql .= ! empty($orderBy)
? ' ORDER BY (' . \implode(', ', $orderBy) . ')'
: ' ORDER BY tuple()';
}
if ($table->ttl !== null) {
$sql .= ' TTL ' . $table->ttl;
}
if (! empty($table->settings)) {
$kv = [];
foreach ($table->settings as $k => $v) {
$kv[] = $k . ' = ' . $v;
}
$sql .= ' SETTINGS ' . \implode(', ', $kv);
}
return new Statement($sql, [], executor: $this->executor);
}
/**
* Render a full `INDEX <name> <columns> TYPE <algorithm>[(args)] GRANULARITY <n>`
* fragment, used by both CREATE TABLE and ALTER TABLE ADD INDEX.
*
* Defaults to `TYPE minmax GRANULARITY 3` when no algorithm is set on the
* index — matches the ClickHouse default behaviour for callers using the
* generic `Table::index()` without picking an algorithm.
*/
private function compileSkipIndex(Index $index): string
{
$cols = \array_map(fn (string $c): string => $this->quote($c), $index->columns);
$expr = \count($cols) === 1 ? $cols[0] : '(' . \implode(', ', $cols) . ')';
if ($index->algorithm === null) {
return 'INDEX ' . $this->quote($index->name) . ' ' . $expr
. ' TYPE minmax GRANULARITY 3';
}
$type = $index->algorithm->value;
if ($index->algorithmArgs !== []) {
$args = \array_map(
fn (string|int|float $arg): string => match (true) {
\is_string($arg) => "'" . \str_replace("'", "''", $arg) . "'",
// sprintf('%F', ...) avoids scientific notation (e.g. 1.0E-5)
// which ClickHouse rejects in index type arguments. Trim
// trailing zeros so 0.01 stays "0.010000" → "0.01".
\is_float($arg) => \rtrim(\rtrim(\sprintf('%F', $arg), '0'), '.'),
default => (string) $arg,
},
$index->algorithmArgs,
);
$type .= '(' . \implode(', ', $args) . ')';
}
return 'INDEX ' . $this->quote($index->name) . ' ' . $expr
. ' TYPE ' . $type . ' GRANULARITY ' . $index->granularity;
}
/**
* Compile an engine declaration: `<Name>` or `<Name>(<args...>)`.
*
* Identifier-type args (version column, sign column, column lists) are
* quoted. Zookeeper path and replica name for ReplicatedMergeTree are
* emitted as single-quoted string literals.
*
* @param list<string> $args
*/
private function compileEngine(Engine $engine, array $args): string
{
return match ($engine) {
Engine::MergeTree,
Engine::AggregatingMergeTree => $engine->value . '()',
Engine::ReplacingMergeTree => $engine->value . '('
. (isset($args[0]) ? $this->quote($args[0]) : '')
. ')',
Engine::SummingMergeTree => $engine->value . '('
. (empty($args)
? ''
: \implode(', ', \array_map(fn (string $c): string => $this->quote($c), $args)))
. ')',
Engine::CollapsingMergeTree => $engine->value . '(' . $this->quote($args[0]) . ')',
Engine::ReplicatedMergeTree => $engine->value
. "('" . \str_replace("'", "''", $args[0]) . "'"
. ", '" . \str_replace("'", "''", $args[1]) . "')",
Engine::Memory,
Engine::Log,
Engine::TinyLog,
Engine::StripeLog => $engine->value,
};
}
public function createView(string $name, Builder $query): Statement
{
$result = $query->build();
$sql = 'CREATE VIEW ' . $this->quote($name) . ' AS ' . $result->query;
return new Statement($sql, $result->bindings, executor: $this->executor);
}
/**
* @param string[] $values
*/
private function compileClickHouseEnum(array $values): string
{
$parts = [];
foreach (\array_values($values) as $i => $value) {
$parts[] = "'" . \str_replace(['\\', "'"], ['\\\\', "\\'"], $value) . "' = " . ($i + 1);
}
return 'Enum8(' . \implode(', ', $parts) . ')';
}
public function commentOnTable(string $table, string $comment): Statement
{
return new Statement(
'ALTER TABLE ' . $this->quote($table) . " MODIFY COMMENT '" . str_replace(['\\', "'"], ['\\\\', "''"], $comment) . "'",
[],
executor: $this->executor,
);
}
public function commentOnColumn(string $table, string $column, string $comment): Statement
{
return new Statement(
'ALTER TABLE ' . $this->quote($table) . ' COMMENT COLUMN ' . $this->quote($column) . " '" . str_replace(['\\', "'"], ['\\\\', "''"], $comment) . "'",
[],
executor: $this->executor,
);
}
public function dropPartition(string $table, string $name): Statement
{
return new Statement(
'ALTER TABLE ' . $this->quote($table) . " DROP PARTITION '" . str_replace(['\\', "'"], ['\\\\', "''"], $name) . "'",
[],
executor: $this->executor,
);
}
}