Skip to content

Commit d580805

Browse files
mbasmanovameta-codesync[bot]
authored andcommitted
refactor: Move DerivedTable::addJoinEquality to .cpp (#546)
Summary: Pull Request resolved: #546 Reviewed By: xiaoxmeng Differential Revision: D85300173 fbshipit-source-id: 188c63f95e3ac4169843e72adfc6160f7c1a614e
1 parent 494dc3d commit d580805

File tree

2 files changed

+26
-32
lines changed

2 files changed

+26
-32
lines changed

axiom/optimizer/DerivedTable.cpp

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,15 @@ PlanObjectCP singleTable(PlanObjectCP object) {
2929
return nullptr;
3030
}
3131

32-
} // namespace
33-
34-
void DerivedTable::addJoinEquality(ExprCP left, ExprCP right) {
32+
// Adds an equijoin edge between 'left' and 'right'.
33+
void addJoinEquality(ExprCP left, ExprCP right, JoinEdgeVector& joins) {
3534
auto leftTable = singleTable(left);
3635
auto rightTable = singleTable(right);
36+
37+
VELOX_CHECK_NOT_NULL(leftTable);
38+
VELOX_CHECK_NOT_NULL(rightTable);
39+
VELOX_CHECK(leftTable != rightTable);
40+
3741
for (auto& join : joins) {
3842
if (join->leftTable() == leftTable && join->rightTable() == rightTable) {
3943
join->addEquality(left, right);
@@ -51,26 +55,20 @@ void DerivedTable::addJoinEquality(ExprCP left, ExprCP right) {
5155
joins.push_back(join);
5256
}
5357

54-
namespace {
55-
58+
// Set of pairs of column IDs. Each pair represents a join equality condition.
59+
// Pairs are canonicalized so that first ID is < second ID.
5660
using EdgeSet = folly::F14FastSet<std::pair<int32_t, int32_t>>;
5761

58-
void addEdge(EdgeSet& edges, int32_t id1, int32_t id2) {
59-
if (id1 > id2) {
60-
edges.insert(std::pair<int32_t, int32_t>(id2, id1));
61-
} else {
62-
edges.insert(std::pair<int32_t, int32_t>(id1, id2));
62+
bool addEdge(EdgeSet& edges, PlanObjectCP left, PlanObjectCP right) {
63+
if (left->id() == right->id()) {
64+
return false;
6365
}
64-
}
6566

66-
bool hasEdge(const EdgeSet& edges, int32_t id1, int32_t id2) {
67-
if (id1 == id2) {
68-
return true;
67+
if (left->id() < right->id()) {
68+
return edges.emplace(left->id(), right->id()).second;
69+
} else {
70+
return edges.emplace(right->id(), left->id()).second;
6971
}
70-
auto it = edges.find(
71-
id1 > id2 ? std::pair<int32_t, int32_t>(id2, id1)
72-
: std::pair<int32_t, int32_t>(id1, id2));
73-
return it != edges.end();
7472
}
7573

7674
void fillJoins(
@@ -79,9 +77,8 @@ void fillJoins(
7977
EdgeSet& edges,
8078
DerivedTableP dt) {
8179
for (auto& other : equivalence.columns) {
82-
if (!hasEdge(edges, column->id(), other->id())) {
83-
addEdge(edges, column->id(), other->id());
84-
dt->addJoinEquality(column->as<Column>(), other->as<Column>());
80+
if (addEdge(edges, column, other)) {
81+
addJoinEquality(column->as<Column>(), other->as<Column>(), dt->joins);
8582
}
8683
}
8784
}
@@ -95,7 +92,7 @@ void DerivedTable::addImpliedJoins() {
9592
const auto* leftKey = join->leftKeys()[i];
9693
const auto* rightKey = join->rightKeys()[i];
9794
if (leftKey->isColumn() && rightKey->isColumn()) {
98-
addEdge(edges, leftKey->id(), rightKey->id());
95+
addEdge(edges, leftKey, rightKey);
9996
}
10097
}
10198
}

axiom/optimizer/DerivedTable.h

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -152,16 +152,18 @@ struct DerivedTable : public PlanObject {
152152
// Write.
153153
WritePlanCP write{nullptr};
154154

155-
/// Adds an equijoin edge between 'left' and 'right'.
156-
void addJoinEquality(ExprCP left, ExprCP right);
155+
/// Moves suitable elements of 'conjuncts' into join edges or single
156+
/// table filters. May be called repeatedly if enclosing dt's add
157+
/// more conjuncts. May call itself recursively on component dts.
158+
void distributeConjuncts();
159+
160+
/// Completes 'joins' with edges implied by column equivalences.
161+
void addImpliedJoins();
157162

158163
/// After 'joins' is filled in, links tables to their direct and
159164
/// equivalence-implied joins.
160165
void linkTablesToJoins();
161166

162-
/// Completes 'joins' with edges implied by column equivalences.
163-
void addImpliedJoins();
164-
165167
/// Extracts implied conjuncts and removes duplicates from
166168
/// 'conjuncts' and updates 'conjuncts'. Extracted conjuncts may
167169
/// allow extra pushdown or allow create join edges. May be called
@@ -223,11 +225,6 @@ struct DerivedTable : public PlanObject {
223225

224226
void addJoinedBy(JoinEdgeP join);
225227

226-
/// Moves suitable elements of 'conjuncts' into join edges or single
227-
/// table filters. May be called repeatedly if enclosing dt's add
228-
/// more conjuncts. May call itself recursively on component dts.
229-
void distributeConjuncts();
230-
231228
/// Memoizes plans for 'this' and fills in 'distribution_'. Needed
232229
/// before adding 'this' as a join side because join sides must have
233230
/// a cardinality guess.

0 commit comments

Comments
 (0)