Skip to content

Commit

Permalink
Extract to helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinTF committed Feb 21, 2025
1 parent 9699068 commit 1b75e84
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 24 deletions.
60 changes: 37 additions & 23 deletions src/engine/QueryPlanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2047,6 +2047,41 @@ auto QueryPlanner::createSpatialJoin(const SubtreePlan& a, const SubtreePlan& b,
return plan;
}

// _____________________________________________________________________________________________________________________

namespace {
// Helper function that maps the indices from the unions' columns to the
// children's columns if possible. Otherwise the entry in `jcs` is dropped.
std::pair<QueryPlanner::JoinColumns, QueryPlanner::JoinColumns>
mapColumnsInUnion(size_t columnIndex, const Union& unionOperation,
const QueryPlanner::JoinColumns& jcs) {
QueryPlanner::JoinColumns leftMapping;
leftMapping.reserve(jcs.size());
QueryPlanner::JoinColumns rightMapping;
rightMapping.reserve(jcs.size());
auto mapColumns = [columnIndex, &unionOperation](
bool isLeft, std::array<ColumnIndex, 2> columns)
-> std::optional<array<ColumnIndex, 2>> {
ColumnIndex& column = columns.at(columnIndex);
auto tmp = unionOperation.getOriginalColumn(isLeft, column);
if (tmp.has_value()) {
column = tmp.value();
return columns;
}
return std::nullopt;
};
for (const auto& joinColumns : jcs) {
if (auto mappedColumn = mapColumns(true, joinColumns)) {
leftMapping.push_back(mappedColumn.value());
}
if (auto mappedColumn = mapColumns(false, joinColumns)) {
rightMapping.push_back(mappedColumn.value());
}
}
return {std::move(leftMapping), std::move(rightMapping)};
}
} // namespace

// _____________________________________________________________________________________________________________________
auto QueryPlanner::applyJoinDistributivelyToUnion(SubtreePlan a, SubtreePlan b,
const JoinColumns& jcs) const
Expand All @@ -2069,29 +2104,8 @@ auto QueryPlanner::applyJoinDistributivelyToUnion(SubtreePlan a, SubtreePlan b,
const auto& leftCandidate = flipped ? other : tmpPlan;
const auto& rightCandidate = flipped ? tmpPlan : clonedOther;

JoinColumns leftMapping;
leftMapping.reserve(jcs.size());
JoinColumns rightMapping;
rightMapping.reserve(jcs.size());
auto mapColumns = [flipped, &unionOperation](
bool isLeft, std::array<ColumnIndex, 2> columns)
-> std::optional<array<ColumnIndex, 2>> {
ColumnIndex& column = columns.at(flipped);
auto tmp = unionOperation->getOriginalColumn(isLeft, column);
if (tmp.has_value()) {
column = tmp.value();
return columns;
}
return std::nullopt;
};
for (const auto& joinColumns : jcs) {
if (auto mappedColumn = mapColumns(true, joinColumns)) {
leftMapping.push_back(mappedColumn.value());
}
if (auto mappedColumn = mapColumns(false, joinColumns)) {
rightMapping.push_back(mappedColumn.value());
}
}
auto [leftMapping, rightMapping] =
mapColumnsInUnion(flipped, *unionOperation, jcs);

tmpPlan._qet = unionOperation->leftChild();
auto joinedLeft =
Expand Down
3 changes: 2 additions & 1 deletion src/engine/QueryPlanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class QueryPlanner {
using CancellationHandle = ad_utility::SharedCancellationHandle;
template <typename T>
using vector = std::vector<T>;
using JoinColumns = std::vector<std::array<ColumnIndex, 2>>;

ParsedQuery::DatasetClauses activeDatasetClauses_;
// The variable of the innermost `GRAPH ?var` clause that the planner
Expand All @@ -33,6 +32,8 @@ class QueryPlanner {
std::optional<Variable> activeGraphVariable_;

public:
using JoinColumns = std::vector<std::array<ColumnIndex, 2>>;

explicit QueryPlanner(QueryExecutionContext* qec,
CancellationHandle cancellationHandle);

Expand Down

0 comments on commit 1b75e84

Please sign in to comment.