@@ -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.
5660using 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
7674void 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 }
0 commit comments