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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
|
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2024 Robin Appelman <robin@icewind.nl>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OC\DB\QueryBuilder\Sharded;
use OC\DB\QueryBuilder\CompositeExpression;
use OC\DB\QueryBuilder\ExtendedQueryBuilder;
use OC\DB\QueryBuilder\Parameter;
use OCP\DB\IResult;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
/**
* A special query builder that automatically distributes queries over multiple database shards.
*
* This relies on `PartitionedQueryBuilder` to handle splitting of parts of the query that touch the sharded tables
* from the non-sharded tables. So the query build here should only either touch only sharded table or only non-sharded tables.
*
* Most of the logic in this class is concerned with extracting either the shard key (e.g. "storage") or primary key (e.g. "fileid")
* from the query. The logic for actually running the query across the shards is mostly delegated to `ShardQueryRunner`.
*/
class ShardedQueryBuilder extends ExtendedQueryBuilder {
private array $shardKeys = [];
private array $primaryKeys = [];
private ?ShardDefinition $shardDefinition = null;
/** @var bool Run the query across all shards */
private bool $allShards = false;
private ?string $insertTable = null;
private mixed $lastInsertId = null;
private ?IDBConnection $lastInsertConnection = null;
private ?int $updateShardKey = null;
private ?int $limit = null;
private ?int $offset = null;
/** @var array{column: string, order: string}[] */
private array $sortList = [];
private string $mainTable = '';
public function __construct(
IQueryBuilder $builder,
protected array $shardDefinitions,
protected ShardConnectionManager $shardConnectionManager,
protected AutoIncrementHandler $autoIncrementHandler,
) {
parent::__construct($builder);
}
public function getShardKeys(): array {
return $this->getKeyValues($this->shardKeys);
}
public function getPrimaryKeys(): array {
return $this->getKeyValues($this->primaryKeys);
}
private function getKeyValues(array $keys): array {
$values = [];
foreach ($keys as $key) {
$values = array_merge($values, $this->getKeyValue($key));
}
return array_values(array_unique($values));
}
private function getKeyValue($value): array {
if ($value instanceof Parameter) {
$value = (string)$value;
}
if (is_string($value) && str_starts_with($value, ':')) {
$param = $this->getParameter(substr($value, 1));
if (is_array($param)) {
return $param;
} else {
return [$param];
}
} elseif ($value !== null) {
return [$value];
} else {
return [];
}
}
public function where(...$predicates) {
return $this->andWhere(...$predicates);
}
public function andWhere(...$where) {
if ($where) {
foreach ($where as $predicate) {
$this->tryLoadShardKey($predicate);
}
parent::andWhere(...$where);
}
return $this;
}
private function tryLoadShardKey($predicate): void {
if (!$this->shardDefinition) {
return;
}
if ($keys = $this->tryExtractShardKeys($predicate, $this->shardDefinition->shardKey)) {
$this->shardKeys += $keys;
}
if ($keys = $this->tryExtractShardKeys($predicate, $this->shardDefinition->primaryKey)) {
$this->primaryKeys += $keys;
}
foreach ($this->shardDefinition->companionKeys as $companionKey) {
if ($keys = $this->tryExtractShardKeys($predicate, $companionKey)) {
$this->primaryKeys += $keys;
}
}
}
/**
* @param $predicate
* @param string $column
* @return string[]
*/
private function tryExtractShardKeys($predicate, string $column): array {
if ($predicate instanceof CompositeExpression) {
$values = [];
foreach ($predicate->getParts() as $part) {
$partValues = $this->tryExtractShardKeys($part, $column);
// for OR expressions, we can only rely on the predicate if all parts contain the comparison
if ($predicate->getType() === CompositeExpression::TYPE_OR && !$partValues) {
return [];
}
$values = array_merge($values, $partValues);
}
return $values;
}
$predicate = (string)$predicate;
// expect a condition in the form of 'alias1.column1 = placeholder' or 'alias1.column1 in placeholder'
if (substr_count($predicate, ' ') > 2) {
return [];
}
if (str_contains($predicate, ' = ')) {
$parts = explode(' = ', $predicate);
if ($parts[0] === "`{$column}`" || str_ends_with($parts[0], "`.`{$column}`")) {
return [$parts[1]];
} else {
return [];
}
}
if (str_contains($predicate, ' IN ')) {
$parts = explode(' IN ', $predicate);
if ($parts[0] === "`{$column}`" || str_ends_with($parts[0], "`.`{$column}`")) {
return [trim(trim($parts[1], '('), ')')];
} else {
return [];
}
}
return [];
}
public function set($key, $value) {
if ($this->shardDefinition && $key === $this->shardDefinition->shardKey) {
$updateShardKey = $value;
}
return parent::set($key, $value);
}
public function setValue($column, $value) {
if ($this->shardDefinition) {
if ($this->shardDefinition->isKey($column)) {
$this->primaryKeys[] = $value;
}
if ($column === $this->shardDefinition->shardKey) {
$this->shardKeys[] = $value;
}
}
return parent::setValue($column, $value);
}
public function values(array $values) {
foreach ($values as $column => $value) {
$this->setValue($column, $value);
}
return $this;
}
private function actOnTable(string $table): void {
$this->mainTable = $table;
foreach ($this->shardDefinitions as $shardDefinition) {
if ($shardDefinition->hasTable($table)) {
$this->shardDefinition = $shardDefinition;
}
}
}
public function from($from, $alias = null) {
if (is_string($from) && $from) {
$this->actOnTable($from);
}
return parent::from($from, $alias);
}
public function update($update = null, $alias = null) {
if (is_string($update) && $update) {
$this->actOnTable($update);
}
return parent::update($update, $alias);
}
public function insert($insert = null) {
if (is_string($insert) && $insert) {
$this->insertTable = $insert;
$this->actOnTable($insert);
}
return parent::insert($insert);
}
public function delete($delete = null, $alias = null) {
if (is_string($delete) && $delete) {
$this->actOnTable($delete);
}
return parent::delete($delete, $alias);
}
private function checkJoin(string $table): void {
if ($this->shardDefinition) {
if ($table === $this->mainTable) {
throw new InvalidShardedQueryException("Sharded query on {$this->mainTable} isn't allowed to join on itself");
}
if (!$this->shardDefinition->hasTable($table)) {
// this generally shouldn't happen as the partitioning logic should prevent this
// but the check is here just in case
throw new InvalidShardedQueryException("Sharded query on {$this->shardDefinition->table} isn't allowed to join on $table");
}
}
}
public function innerJoin($fromAlias, $join, $alias, $condition = null) {
$this->checkJoin($join);
return parent::innerJoin($fromAlias, $join, $alias, $condition);
}
public function leftJoin($fromAlias, $join, $alias, $condition = null) {
$this->checkJoin($join);
return parent::leftJoin($fromAlias, $join, $alias, $condition);
}
public function rightJoin($fromAlias, $join, $alias, $condition = null) {
if ($this->shardDefinition) {
throw new InvalidShardedQueryException("Sharded query on {$this->shardDefinition->table} isn't allowed to right join");
}
return parent::rightJoin($fromAlias, $join, $alias, $condition);
}
public function join($fromAlias, $join, $alias, $condition = null) {
return $this->innerJoin($fromAlias, $join, $alias, $condition);
}
public function setMaxResults($maxResults) {
if ($maxResults > 0) {
$this->limit = (int)$maxResults;
}
return parent::setMaxResults($maxResults);
}
public function setFirstResult($firstResult) {
if ($firstResult > 0) {
$this->offset = (int)$firstResult;
}
if ($this->shardDefinition && count($this->shardDefinition->shards) > 1) {
// we have to emulate offset
return $this;
} else {
return parent::setFirstResult($firstResult);
}
}
public function addOrderBy($sort, $order = null) {
$this->registerOrder((string)$sort, (string)$order ?? 'ASC');
return parent::orderBy($sort, $order);
}
public function orderBy($sort, $order = null) {
$this->sortList = [];
$this->registerOrder((string)$sort, (string)$order ?? 'ASC');
return parent::orderBy($sort, $order);
}
private function registerOrder(string $column, string $order): void {
// handle `mime + 0` and similar by just sorting on the first part of the expression
[$column] = explode(' ', $column);
$column = trim($column, '`');
$this->sortList[] = [
'column' => $column,
'order' => strtoupper($order),
];
}
public function hintShardKey(string $column, mixed $value, bool $overwrite = false): self {
if ($overwrite) {
$this->primaryKeys = [];
$this->shardKeys = [];
}
if ($this->shardDefinition?->isKey($column)) {
$this->primaryKeys[] = $value;
}
if ($column === $this->shardDefinition?->shardKey) {
$this->shardKeys[] = $value;
}
return $this;
}
public function runAcrossAllShards(): self {
$this->allShards = true;
return $this;
}
/**
* @throws InvalidShardedQueryException
*/
public function validate(): void {
if ($this->shardDefinition && $this->insertTable) {
if ($this->allShards) {
throw new InvalidShardedQueryException("Can't insert across all shards");
}
if (empty($this->getShardKeys())) {
throw new InvalidShardedQueryException("Can't insert without shard key");
}
}
if ($this->shardDefinition && !$this->allShards) {
if (empty($this->getShardKeys()) && empty($this->getPrimaryKeys())) {
throw new InvalidShardedQueryException('No shard key or primary key set for query');
}
}
if ($this->shardDefinition && $this->updateShardKey) {
$newShardKey = $this->getKeyValue($this->updateShardKey);
$oldShardKeys = $this->getShardKeys();
if (count($newShardKey) !== 1) {
throw new InvalidShardedQueryException("Can't set shard key to an array");
}
$newShardKey = current($newShardKey);
if (empty($oldShardKeys)) {
throw new InvalidShardedQueryException("Can't update without shard key");
}
$oldShards = array_values(array_unique(array_map(function ($shardKey) {
return $this->shardDefinition->getShardForKey((int)$shardKey);
}, $oldShardKeys)));
$newShard = $this->shardDefinition->getShardForKey((int)$newShardKey);
if ($oldShards === [$newShard]) {
throw new InvalidShardedQueryException('Update statement would move rows to a different shard');
}
}
}
public function executeQuery(?IDBConnection $connection = null): IResult {
$this->validate();
if ($this->shardDefinition) {
$runner = new ShardQueryRunner($this->shardConnectionManager, $this->shardDefinition);
return $runner->executeQuery($this->builder, $this->allShards, $this->getShardKeys(), $this->getPrimaryKeys(), $this->sortList, $this->limit, $this->offset);
}
return parent::executeQuery($connection);
}
public function executeStatement(?IDBConnection $connection = null): int {
$this->validate();
if ($this->shardDefinition) {
$runner = new ShardQueryRunner($this->shardConnectionManager, $this->shardDefinition);
if ($this->insertTable) {
$shards = $runner->getShards($this->allShards, $this->getShardKeys());
if (!$shards) {
throw new InvalidShardedQueryException("Can't insert without shard key");
}
$count = 0;
foreach ($shards as $shard) {
$shardConnection = $this->shardConnectionManager->getConnection($this->shardDefinition, $shard);
if (!$this->primaryKeys && $this->shardDefinition->table === $this->insertTable) {
$id = $this->autoIncrementHandler->getNextPrimaryKey($this->shardDefinition, $shard);
parent::setValue($this->shardDefinition->primaryKey, $this->createParameter('__generated_primary_key'));
$this->setParameter('__generated_primary_key', $id, self::PARAM_INT);
$this->lastInsertId = $id;
}
$count += parent::executeStatement($shardConnection);
$this->lastInsertConnection = $shardConnection;
}
return $count;
} else {
return $runner->executeStatement($this->builder, $this->allShards, $this->getShardKeys(), $this->getPrimaryKeys());
}
}
return parent::executeStatement($connection);
}
public function getLastInsertId(): int {
if ($this->lastInsertId) {
return $this->lastInsertId;
}
if ($this->lastInsertConnection) {
$table = $this->builder->prefixTableName($this->insertTable);
return $this->lastInsertConnection->lastInsertId($table);
} else {
return parent::getLastInsertId();
}
}
}
|