Skip to content

Commit

Permalink
[CBO] Transitive closure adding conditions to existed edges (#10528)
Browse files Browse the repository at this point in the history
  • Loading branch information
pashandor789 authored Oct 17, 2024
1 parent 0a9d2cf commit 192e3bc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
16 changes: 16 additions & 0 deletions ydb/library/yql/dq/opt/dq_opt_hypergraph_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,22 @@ Y_UNIT_TEST_SUITE(HypergraphBuild) {
}
}

Y_UNIT_TEST(ManyCondsBetweenJoinForTransitiveClosure) {
auto join = Join(Join("A", "B", "A.PUDGE=B.PUDGE,A.DOTA=B.DOTA"), "C", "A.PUDGE=C.PUDGE,A.DOTA=C.DOTA");

auto graph = MakeJoinHypergraph<TNodeSet64>(join);
Cout << graph.String() << Endl;

auto B = graph.GetNodesByRelNames({"B"});
auto C = graph.GetNodesByRelNames({"C"});
UNIT_ASSERT(graph.FindEdgeBetween(B, C));

{
auto optimizedJoin = Enumerate(join, TOptimizerHints::Parse("Rows(B C # 0)"));
UNIT_ASSERT(HaveSameConditionCount(optimizedJoin, join));
}
}

auto MakeClique(size_t size) {
std::shared_ptr<IBaseOptimizerNode> root = Join("R0", "R1", "R0.id=R1.id");

Expand Down
17 changes: 15 additions & 2 deletions ydb/library/yql/dq/opt/dq_opt_join_hypergraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,21 @@ class TTransitiveClosureConstructor {
auto iNode = Graph_.GetNodesByRelNames({joinCondById[i].RelName});
auto jNode = Graph_.GetNodesByRelNames({joinCondById[j].RelName});

if (Graph_.FindEdgeBetween(iNode, jNode)) {
continue;
if (auto* maybeEdge = Graph_.FindEdgeBetween(iNode, jNode)) {
auto addUniqueKey = [](auto& vector, const auto& key) {
if (std::find(vector.begin(), vector.end(), key) == vector.end()) {
vector.push_back(key);
}
};

auto& revEdge = Graph_.GetEdge(maybeEdge->ReversedEdgeId);
addUniqueKey(revEdge.LeftJoinKeys, joinCondById[j]);
addUniqueKey(revEdge.RightJoinKeys, joinCondById[i]);

auto& edge = Graph_.GetEdge(revEdge.ReversedEdgeId);
addUniqueKey(edge.LeftJoinKeys, joinCondById[i]);
addUniqueKey(edge.RightJoinKeys, joinCondById[j]);
continue;
}

Graph_.AddEdge(THyperedge(iNode, jNode, InnerJoin, false, false, true, {joinCondById[i]}, {joinCondById[j]}));
Expand Down

0 comments on commit 192e3bc

Please sign in to comment.