-
-
Notifications
You must be signed in to change notification settings - Fork 5k
Expand file tree
/
Copy pathPartitionedQueryBuilder.php
More file actions
515 lines (467 loc) · 16.8 KB
/
PartitionedQueryBuilder.php
File metadata and controls
515 lines (467 loc) · 16.8 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
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OC\DB\QueryBuilder\Partitioned;
use OC\DB\QueryBuilder\CompositeExpression;
use OC\DB\QueryBuilder\QuoteHelper;
use OC\DB\QueryBuilder\Sharded\AutoIncrementHandler;
use OC\DB\QueryBuilder\Sharded\ShardConnectionManager;
use OC\DB\QueryBuilder\Sharded\ShardedQueryBuilder;
use OCP\DB\IResult;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\DB\QueryBuilder\IQueryFunction;
use OCP\IDBConnection;
/**
* A special query builder that automatically splits queries that span across multiple database partitions[1].
*
* This is done by inspecting the query as it's being built, and when a cross-partition join is detected,
* the part of the query that touches the partition is split of into a different sub-query.
* Then, when the query is executed, the results from the sub-queries are automatically merged.
*
* This whole process is intended to be transparent to any code using the query builder, however it does impose some extra
* limitation for queries that work cross-partition. See the documentation from `InvalidPartitionedQueryException` for more details.
*
* When a join is created in the query, this builder checks if it belongs to the same partition as the table from the
* original FROM/UPDATE/DELETE/INSERT and if not, creates a new "sub query" for the partition.
* Then for every part that is added the query, the part is analyzed to determine which partition the query part is referencing
* and the query part is added to the sub query for that partition.
*
* [1]: A set of tables which can't be queried together with the rest of the tables, such as when sharding is used.
*/
class PartitionedQueryBuilder extends ShardedQueryBuilder {
/** @var array<string, PartitionQuery> $splitQueries */
private array $splitQueries = [];
/** @var array<string, PartitionSplit> */
private array $partitions = [];
/** @var array{'select': string|array, 'alias': ?string}[] */
private array $selects = [];
private ?PartitionSplit $mainPartition = null;
private bool $hasPositionalParameter = false;
private QuoteHelper $quoteHelper;
private ?int $limit = null;
private ?int $offset = null;
public function __construct(
IQueryBuilder $builder,
array $shardDefinitions,
ShardConnectionManager $shardConnectionManager,
AutoIncrementHandler $autoIncrementHandler,
) {
parent::__construct($builder, $shardDefinitions, $shardConnectionManager, $autoIncrementHandler);
$this->quoteHelper = new QuoteHelper();
}
private function newQuery(): IQueryBuilder {
// get a fresh, non-partitioning query builder
$builder = $this->builder->getConnection()->getQueryBuilder();
if ($builder instanceof PartitionedQueryBuilder) {
$builder = $builder->builder;
}
return new ShardedQueryBuilder(
$builder,
$this->shardDefinitions,
$this->shardConnectionManager,
$this->autoIncrementHandler,
);
}
// we need to save selects until we know all the table aliases
#[\Override]
public function select(...$selects) {
if (count($selects) === 1 && is_array($selects[0])) {
$selects = $selects[0];
}
$this->selects = [];
$this->addSelect(...$selects);
return $this;
}
#[\Override]
public function addSelect(...$select) {
$select = array_map(function ($select) {
return ['select' => $select, 'alias' => null];
}, $select);
$this->selects = array_merge($this->selects, $select);
return $this;
}
#[\Override]
public function selectAlias($select, $alias): self {
$this->selects[] = ['select' => $select, 'alias' => $alias];
return $this;
}
/**
* Ensure that a column is being selected by the query
*
* This is mainly used to ensure that the returned rows from both sides of a partition contains the columns of the join predicate
*
* @param string|IQueryFunction $column
* @return void
*/
private function ensureSelect(string|IQueryFunction $column, ?string $alias = null): void {
$checkColumn = $alias ?: $column;
if (str_contains($checkColumn, '.')) {
[$table, $checkColumn] = explode('.', $checkColumn);
$partition = $this->getPartition($table);
} else {
$partition = null;
}
foreach ($this->selects as $select) {
$select = $select['select'];
if (!is_string($select)) {
continue;
}
if (str_contains($select, '.')) {
[$table, $select] = explode('.', $select);
$selectPartition = $this->getPartition($table);
} else {
$selectPartition = null;
}
if (
($select === $checkColumn || $select === '*')
&& $selectPartition === $partition
) {
return;
}
}
if ($alias) {
$this->selectAlias($column, $alias);
} else {
$this->addSelect($column);
}
}
/**
* Distribute the select statements to the correct partition
*
* This is done at the end instead of when the `select` call is made, because the `select` calls are generally done
* before we know what tables are involved in the query
*
* @return void
*/
private function applySelects(): void {
foreach ($this->selects as $select) {
foreach ($this->partitions as $partition) {
if (is_string($select['select']) && (
$select['select'] === '*'
|| $partition->isColumnInPartition($select['select']))
) {
if (isset($this->splitQueries[$partition->name])) {
if ($select['alias']) {
$this->splitQueries[$partition->name]->query->selectAlias($select['select'], $select['alias']);
} else {
$this->splitQueries[$partition->name]->query->addSelect($select['select']);
}
if ($select['select'] !== '*') {
continue 2;
}
}
}
}
if ($select['alias']) {
parent::selectAlias($select['select'], $select['alias']);
} else {
parent::addSelect($select['select']);
}
}
$this->selects = [];
}
public function addPartition(PartitionSplit $partition): void {
$this->partitions[$partition->name] = $partition;
}
private function getPartition(string $table): ?PartitionSplit {
foreach ($this->partitions as $partition) {
if ($partition->containsTable($table) || $partition->containsAlias($table)) {
return $partition;
}
}
return null;
}
#[\Override]
public function from($from, $alias = null) {
if (is_string($from) && $partition = $this->getPartition($from)) {
$this->mainPartition = $partition;
if ($alias) {
$this->mainPartition->addAlias($from, $alias);
}
}
return parent::from($from, $alias);
}
#[\Override]
public function innerJoin($fromAlias, $join, $alias, $condition = null): self {
return $this->join($fromAlias, $join, $alias, $condition);
}
#[\Override]
public function leftJoin($fromAlias, $join, $alias, $condition = null): self {
return $this->join($fromAlias, $join, $alias, $condition, PartitionQuery::JOIN_MODE_LEFT);
}
#[\Override]
public function join($fromAlias, $join, $alias, $condition = null, $joinMode = PartitionQuery::JOIN_MODE_INNER): self {
if ($join instanceof IQueryFunction) {
$partition = null;
$fromPartition = null;
} else {
$partition = $this->getPartition($join);
$fromPartition = $this->getPartition($fromAlias);
}
if ($partition && $partition !== $this->mainPartition) {
/** @var string $join */
// join from the main db to a partition
$joinCondition = JoinCondition::parse($condition, $join, $alias, $fromAlias);
$partition->addAlias($join, $alias);
if (!isset($this->splitQueries[$partition->name])) {
$this->splitQueries[$partition->name] = new PartitionQuery(
$this->newQuery(),
$joinCondition->fromAlias ?? $joinCondition->fromColumn, $joinCondition->toAlias ?? $joinCondition->toColumn,
$joinMode
);
$this->splitQueries[$partition->name]->query->from($join, $alias);
$this->ensureSelect($joinCondition->fromColumn, $joinCondition->fromAlias);
$this->ensureSelect($joinCondition->toColumn, $joinCondition->toAlias);
} else {
$query = $this->splitQueries[$partition->name]->query;
if ($partition->containsAlias($fromAlias)) {
$query->innerJoin($fromAlias, $join, $alias, $condition);
} else {
throw new InvalidPartitionedQueryException("Can't join across partition boundaries more than once");
}
}
$this->splitQueries[$partition->name]->query->andWhere(...$joinCondition->toConditions);
parent::andWhere(...$joinCondition->fromConditions);
return $this;
} elseif ($fromPartition && $fromPartition !== $partition) {
/** @var string $join */
// join from partition, to the main db
$joinCondition = JoinCondition::parse($condition, $join, $alias, $fromAlias);
if (str_starts_with($fromPartition->name, 'from_')) {
$partitionName = $fromPartition->name;
} else {
$partitionName = 'from_' . $fromPartition->name;
}
if (!isset($this->splitQueries[$partitionName])) {
$newPartition = new PartitionSplit($partitionName, [$join]);
$newPartition->addAlias($join, $alias);
$this->partitions[$newPartition->name] = $newPartition;
$this->splitQueries[$partitionName] = new PartitionQuery(
$this->newQuery(),
$joinCondition->fromAlias ?? $joinCondition->fromColumn, $joinCondition->toAlias ?? $joinCondition->toColumn,
$joinMode
);
$this->ensureSelect($joinCondition->fromColumn, $joinCondition->fromAlias);
$this->ensureSelect($joinCondition->toColumn, $joinCondition->toAlias);
$this->splitQueries[$partitionName]->query->from($join, $alias);
$this->splitQueries[$partitionName]->query->andWhere(...$joinCondition->toConditions);
parent::andWhere(...$joinCondition->fromConditions);
} else {
$fromPartition->addTable($join);
$fromPartition->addAlias($join, $alias);
$query = $this->splitQueries[$partitionName]->query;
$query->innerJoin($fromAlias, $join, $alias, $condition);
}
return $this;
} else {
// join within the main db or a partition
if ($joinMode === PartitionQuery::JOIN_MODE_INNER) {
return parent::innerJoin($fromAlias, $join, $alias, $condition);
} elseif ($joinMode === PartitionQuery::JOIN_MODE_LEFT) {
return parent::leftJoin($fromAlias, $join, $alias, $condition);
} elseif ($joinMode === PartitionQuery::JOIN_MODE_RIGHT) {
return parent::rightJoin($fromAlias, $join, $alias, $condition);
} else {
throw new \InvalidArgumentException("Invalid join mode: $joinMode");
}
}
}
/**
* Flatten a list of predicates by merging the parts of any "AND" expression into the list of predicates
*
* @param array $predicates
* @return array
*/
private function flattenPredicates(array $predicates): array {
$result = [];
foreach ($predicates as $predicate) {
if ($predicate instanceof CompositeExpression && $predicate->getType() === CompositeExpression::TYPE_AND) {
$result = array_merge($result, $this->flattenPredicates($predicate->getParts()));
} else {
$result[] = $predicate;
}
}
return $result;
}
/**
* Split an array of predicates (WHERE query parts) by the partition they reference
*
* @param array $predicates
* @return array<string, array>
*/
private function splitPredicatesByParts(array $predicates): array {
$predicates = $this->flattenPredicates($predicates);
$partitionPredicates = [];
foreach ($predicates as $predicate) {
$partition = $this->getPartitionForPredicate((string)$predicate);
if ($this->mainPartition === $partition) {
$partitionPredicates[''][] = $predicate;
} elseif ($partition) {
$partitionPredicates[$partition->name][] = $predicate;
} else {
$partitionPredicates[''][] = $predicate;
}
}
return $partitionPredicates;
}
#[\Override]
public function where(...$predicates) {
return $this->andWhere(...$predicates);
}
#[\Override]
public function andWhere(...$where) {
if ($where) {
foreach ($this->splitPredicatesByParts($where) as $alias => $predicates) {
if (isset($this->splitQueries[$alias])) {
$mergedPredicate = new CompositeExpression(CompositeExpression::TYPE_AND, $predicates);
// when there is a condition on a table being left-joined it starts to behave as if it's an inner join
// since any joined column that doesn't have the left part will not match the condition
// when there the condition is `$joinToColumn IS NULL` we instead mark the query as excluding the left half
if ($this->splitQueries[$alias]->joinMode === PartitionQuery::JOIN_MODE_LEFT) {
if ($this->constraintsPartitionNotNull($mergedPredicate, $this->partitions[$alias])) {
$this->splitQueries[$alias]->joinMode = PartitionQuery::JOIN_MODE_INNER;
}
$column = $this->quoteHelper->quoteColumnName($this->splitQueries[$alias]->joinToColumn);
foreach ($predicates as $predicate) {
if ((string)$predicate === "$column IS NULL") {
$this->splitQueries[$alias]->joinMode = PartitionQuery::JOIN_MODE_LEFT_NULL;
} else {
$this->splitQueries[$alias]->query->andWhere($predicate);
}
}
} else {
$this->splitQueries[$alias]->query->andWhere(...$predicates);
}
} else {
parent::andWhere(...$predicates);
}
}
}
return $this;
}
/**
* Check if any part of a predicates constraints any part of a partition to be not null
*
* @return bool
*/
private function constraintsPartitionNotNull($predicate, PartitionSplit $partition): bool {
if ($predicate instanceof CompositeExpression) {
if ($predicate->getType() === CompositeExpression::TYPE_OR) {
$all = true;
foreach ($predicate->getParts() as $part) {
$all = $all && $this->constraintsPartitionNotNull($part, $partition);
}
return $all;
} else {
foreach ($predicate->getParts() as $part) {
if ($this->constraintsPartitionNotNull($part, $partition)) {
return true;
}
}
return false;
}
} else {
return $partition->checkPredicateForTable($predicate) && !str_ends_with($predicate, 'IS NULL');
}
}
private function getPartitionForPredicate(string $predicate): ?PartitionSplit {
foreach ($this->partitions as $partition) {
if (str_contains($predicate, '?')) {
$this->hasPositionalParameter = true;
}
if ($partition->checkPredicateForTable($predicate)) {
return $partition;
}
}
return null;
}
#[\Override]
public function update($update = null, $alias = null) {
return parent::update($update, $alias);
}
#[\Override]
public function insert($insert = null) {
return parent::insert($insert);
}
#[\Override]
public function delete($delete = null, $alias = null) {
return parent::delete($delete, $alias);
}
#[\Override]
public function setMaxResults($maxResults) {
if ($maxResults > 0) {
$this->limit = (int)$maxResults;
}
return parent::setMaxResults($maxResults);
}
#[\Override]
public function setFirstResult($firstResult) {
if ($firstResult > 0) {
$this->offset = (int)$firstResult;
}
return parent::setFirstResult($firstResult);
}
#[\Override]
public function executeQuery(?IDBConnection $connection = null): IResult {
$this->applySelects();
if ($this->splitQueries && $this->hasPositionalParameter) {
throw new InvalidPartitionedQueryException("Partitioned queries aren't allowed to to positional arguments");
}
foreach ($this->splitQueries as $split) {
$split->query->setParameters($this->getParameters(), $this->getParameterTypes());
}
if (count($this->splitQueries) > 0) {
$hasNonLeftJoins = array_reduce($this->splitQueries, function (bool $hasNonLeftJoins, PartitionQuery $query) {
return $hasNonLeftJoins || $query->joinMode !== PartitionQuery::JOIN_MODE_LEFT;
}, false);
if ($hasNonLeftJoins) {
if (is_int($this->limit)) {
throw new InvalidPartitionedQueryException('Limit is not allowed in partitioned queries');
}
if (is_int($this->offset)) {
throw new InvalidPartitionedQueryException('Offset is not allowed in partitioned queries');
}
}
}
$s = $this->getSQL();
$result = parent::executeQuery($connection);
if (count($this->splitQueries) > 0) {
return new PartitionedResult($this->splitQueries, $result);
} else {
return $result;
}
}
#[\Override]
public function executeStatement(?IDBConnection $connection = null): int {
if (count($this->splitQueries)) {
throw new InvalidPartitionedQueryException("Partitioning write queries isn't supported");
}
return parent::executeStatement($connection);
}
#[\Override]
public function getSQL() {
$this->applySelects();
return parent::getSQL();
}
public function getPartitionCount(): int {
return count($this->splitQueries) + 1;
}
#[\Override]
public function hintShardKey(string $column, mixed $value, bool $overwrite = false): self {
if (str_contains($column, '.')) {
[$alias, $column] = explode('.', $column);
$partition = $this->getPartition($alias);
if ($partition) {
$this->splitQueries[$partition->name]->query->hintShardKey($column, $value, $overwrite);
} else {
parent::hintShardKey($column, $value, $overwrite);
}
} else {
parent::hintShardKey($column, $value, $overwrite);
}
return $this;
}
}