Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve select handling for sharded queries #49832

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ private function newQuery(): IQueryBuilder {

// we need to save selects until we know all the table aliases
public function select(...$selects) {
if (count($selects) === 1 && is_array($selects[0])) {
$selects = $selects[0];
}
$this->selects = [];
$this->addSelect(...$selects);
return $this;
Expand All @@ -99,16 +102,33 @@ public function selectAlias($select, $alias) {
*
* 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 $column
* @param string|IQueryFunction $column
* @return void
*/
private function ensureSelect(string|IQueryFunction $column, ?string $alias = null): void {
$checkColumn = $alias ?: $column;
if (str_contains($checkColumn, '.')) {
[, $checkColumn] = explode('.', $checkColumn);
[$table, $checkColumn] = explode('.', $checkColumn);
$partition = $this->getPartition($table);
} else {
$partition = null;
}
foreach ($this->selects as $select) {
if ($select['select'] === $checkColumn || $select['select'] === '*' || str_ends_with($select['select'], '.' . $checkColumn)) {
$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;
}
}
Expand Down Expand Up @@ -284,6 +304,7 @@ private function flattenPredicates(array $predicates): array {

/**
* Split an array of predicates (WHERE query parts) by the partition they reference
*
* @param array $predicates
* @return array<string, array>
*/
Expand Down
Loading