@@ -30,11 +30,15 @@ PlanObjectCP singleTable(PlanObjectCP object) {
3030 return nullptr ;
3131}
3232
33- } // namespace
34-
35- void DerivedTable::addJoinEquality (ExprCP left, ExprCP right) {
33+ // Adds an equijoin edge between 'left' and 'right'.
34+ void addJoinEquality (ExprCP left, ExprCP right, JoinEdgeVector& joins) {
3635 auto leftTable = singleTable (left);
3736 auto rightTable = singleTable (right);
37+
38+ VELOX_CHECK_NOT_NULL (leftTable);
39+ VELOX_CHECK_NOT_NULL (rightTable);
40+ VELOX_CHECK (leftTable != rightTable);
41+
3842 for (auto & join : joins) {
3943 if (join->leftTable () == leftTable && join->rightTable () == rightTable) {
4044 join->addEquality (left, right);
@@ -52,26 +56,20 @@ void DerivedTable::addJoinEquality(ExprCP left, ExprCP right) {
5256 joins.push_back (join);
5357}
5458
55- namespace {
56-
59+ // Set of pairs of column IDs. Each pair represents a join equality condition.
60+ // Pairs are canonicalized so that first ID is < second ID.
5761using EdgeSet = folly::F14FastSet<std::pair<int32_t , int32_t >>;
5862
59- void addEdge (EdgeSet& edges, int32_t id1, int32_t id2) {
60- if (id1 > id2) {
61- edges.insert (std::pair<int32_t , int32_t >(id2, id1));
62- } else {
63- edges.insert (std::pair<int32_t , int32_t >(id1, id2));
63+ bool addEdge (EdgeSet& edges, PlanObjectCP left, PlanObjectCP right) {
64+ if (left->id () == right->id ()) {
65+ return false ;
6466 }
65- }
6667
67- bool hasEdge (const EdgeSet& edges, int32_t id1, int32_t id2) {
68- if (id1 == id2) {
69- return true ;
68+ if (left->id () < right->id ()) {
69+ return edges.emplace (left->id (), right->id ()).second ;
70+ } else {
71+ return edges.emplace (right->id (), left->id ()).second ;
7072 }
71- auto it = edges.find (
72- id1 > id2 ? std::pair<int32_t , int32_t >(id2, id1)
73- : std::pair<int32_t , int32_t >(id1, id2));
74- return it != edges.end ();
7573}
7674
7775void fillJoins (
@@ -80,9 +78,8 @@ void fillJoins(
8078 EdgeSet& edges,
8179 DerivedTableP dt) {
8280 for (auto & other : equivalence.columns ) {
83- if (!hasEdge (edges, column->id (), other->id ())) {
84- addEdge (edges, column->id (), other->id ());
85- dt->addJoinEquality (column->as <Column>(), other->as <Column>());
81+ if (addEdge (edges, column, other)) {
82+ addJoinEquality (column->as <Column>(), other->as <Column>(), dt->joins );
8683 }
8784 }
8885}
@@ -96,7 +93,7 @@ void DerivedTable::addImpliedJoins() {
9693 const auto * leftKey = join->leftKeys ()[i];
9794 const auto * rightKey = join->rightKeys ()[i];
9895 if (leftKey->isColumn () && rightKey->isColumn ()) {
99- addEdge (edges, leftKey-> id () , rightKey-> id () );
96+ addEdge (edges, leftKey, rightKey);
10097 }
10198 }
10299 }
0 commit comments