Skip to content

Implement cbo join tree -> yt join tree converter YQL-17437 #1730

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions ydb/library/yql/providers/yt/provider/ut/yql_yt_cbo_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,50 @@ Y_UNIT_TEST(BuildOptimizerTree2TablesComplexLabel) {
UNIT_ASSERT_VALUES_EQUAL(right->Stats->Nrows, 10000);
}

Y_UNIT_TEST(BuildYtJoinTree2Tables) {
TExprContext exprCtx;
auto tree = MakeOp({"c", "c_nationkey"}, {"n", "n_nationkey"}, {"c", "n"}, exprCtx);
tree->Left = MakeLeaf({"c"}, {"c"}, 100000, 12333, exprCtx);
tree->Right = MakeLeaf({"n"}, {"n"}, 1000, 1233, exprCtx);

std::shared_ptr<IBaseOptimizerNode> resultTree;
std::shared_ptr<IProviderContext> resultCtx;
BuildOptimizerJoinTree(resultTree, resultCtx, tree);

auto joinTree = BuildYtJoinTree(resultTree, exprCtx, {});

UNIT_ASSERT(AreSimilarTrees(joinTree, tree));
}

Y_UNIT_TEST(BuildYtJoinTree2TablesComplexLabel) {
TExprContext exprCtx;
auto tree = MakeOp({"c", "c_nationkey"}, {"n", "n_nationkey"}, {"c", "n", "e"}, exprCtx);
tree->Left = MakeLeaf({"c"}, {"c"}, 1000000, 1233333, exprCtx);
tree->Right = MakeLeaf({"n"}, {"n", "e"}, 10000, 12333, exprCtx);

std::shared_ptr<IBaseOptimizerNode> resultTree;
std::shared_ptr<IProviderContext> resultCtx;
BuildOptimizerJoinTree(resultTree, resultCtx, tree);
auto joinTree = BuildYtJoinTree(resultTree, exprCtx, {});

UNIT_ASSERT(AreSimilarTrees(joinTree, tree));
}

Y_UNIT_TEST(BuildYtJoinTree2TablesTableIn2Rels)
{
TExprContext exprCtx;
auto tree = MakeOp({"c", "c_nationkey"}, {"n", "n_nationkey"}, {"c", "n", "c"}, exprCtx);
tree->Left = MakeLeaf({"c"}, {"c"}, 1000000, 1233333, exprCtx);
tree->Right = MakeLeaf({"n"}, {"n", "c"}, 10000, 12333, exprCtx);

std::shared_ptr<IBaseOptimizerNode> resultTree;
std::shared_ptr<IProviderContext> resultCtx;
BuildOptimizerJoinTree(resultTree, resultCtx, tree);
auto joinTree = BuildYtJoinTree(resultTree, exprCtx, {});

UNIT_ASSERT(AreSimilarTrees(joinTree, tree));
}

#define ADD_TEST(Name) \
Y_UNIT_TEST(Name ## _PG) { \
Name(ECostBasedOptimizerType::PG); \
Expand Down
2 changes: 2 additions & 0 deletions ydb/library/yql/providers/yt/provider/yql_yt_join_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,7 @@ struct IBaseOptimizerNode;
struct IProviderContext;

void BuildOptimizerJoinTree(std::shared_ptr<IBaseOptimizerNode>& tree, std::shared_ptr<IProviderContext>& ctx, TYtJoinNodeOp::TPtr op);
TYtJoinNode::TPtr BuildYtJoinTree(std::shared_ptr<IBaseOptimizerNode> node, TExprContext& ctx, TPositionHandle pos);
bool AreSimilarTrees(TYtJoinNode::TPtr node1, TYtJoinNode::TPtr node2);

}
106 changes: 79 additions & 27 deletions ydb/library/yql/providers/yt/provider/yql_yt_join_reorder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,6 @@ namespace NYql {

namespace {

bool AreSimilarTrees(TYtJoinNode::TPtr node1, TYtJoinNode::TPtr node2) {
if (node1 == node2) {
return true;
}
if (node1 && !node2) {
return false;
}
if (node2 && !node1) {
return false;
}
if (node1->Scope != node2->Scope) {
return false;
}
auto opLeft = dynamic_cast<TYtJoinNodeOp*>(node1.Get());
auto opRight = dynamic_cast<TYtJoinNodeOp*>(node2.Get());
if (opLeft && opRight) {
return AreSimilarTrees(opLeft->Left, opRight->Left)
&& AreSimilarTrees(opLeft->Right, opRight->Right);
} else if (!opLeft && !opRight) {
return true;
} else {
return false;
}
}

void DebugPrint(TYtJoinNode::TPtr node, TExprContext& ctx, int level) {
auto* op = dynamic_cast<TYtJoinNodeOp*>(node.Get());
auto printScope = [](const TVector<TString>& scope) -> TString {
Expand Down Expand Up @@ -409,6 +384,16 @@ class TJoinReorderer {
IOptimizer::TOutput Result;
};

class TYtRelOptimizerNode: public TRelOptimizerNode {
public:
TYtRelOptimizerNode(TString label, std::shared_ptr<TOptimizerStatistics> stats, TYtJoinNodeLeaf* leaf)
: TRelOptimizerNode(std::move(label), std::move(stats))
, OriginalLeaf(leaf)
{ }

TYtJoinNodeLeaf* OriginalLeaf;
};

class TOptimizerTreeBuilder
{
public:
Expand Down Expand Up @@ -487,8 +472,8 @@ class TOptimizerTreeBuilder
}
}

return std::make_shared<TRelOptimizerNode>(
std::move(label), std::move(stat)
return std::make_shared<TYtRelOptimizerNode>(
std::move(label), std::move(stat), leaf
);
}

Expand All @@ -498,13 +483,80 @@ class TOptimizerTreeBuilder
TYtJoinNodeOp::TPtr InputTree;
};

TYtJoinNode::TPtr BuildYtJoinTree(std::shared_ptr<IBaseOptimizerNode> node, TVector<TString>& scope, TExprContext& ctx, TPositionHandle pos) {
if (node->Kind == RelNodeType) {
auto* leaf = static_cast<TYtRelOptimizerNode*>(node.get())->OriginalLeaf;
scope.insert(scope.end(), leaf->Scope.begin(), leaf->Scope.end());
return leaf;
} else if (node->Kind == JoinNodeType) {
auto ret = MakeIntrusive<TYtJoinNodeOp>();
auto* op = static_cast<TJoinOptimizerNode*>(node.get());
ret->JoinKind = ctx.NewAtom(pos, ConvertToJoinString(op->JoinType));
TVector<TExprNodePtr> leftLabel, rightLabel;
leftLabel.reserve(op->JoinConditions.size() * 2);
rightLabel.reserve(op->JoinConditions.size() * 2);
for (auto& [left, right] : op->JoinConditions) {
leftLabel.emplace_back(ctx.NewAtom(pos, left.RelName));
leftLabel.emplace_back(ctx.NewAtom(pos, left.AttributeName));

rightLabel.emplace_back(ctx.NewAtom(pos, right.RelName));
rightLabel.emplace_back(ctx.NewAtom(pos, right.AttributeName));
}
ret->LeftLabel = Build<TCoAtomList>(ctx, pos)
.Add(leftLabel)
.Done()
.Ptr();
ret->RightLabel = Build<TCoAtomList>(ctx, pos)
.Add(rightLabel)
.Done()
.Ptr();
int index = scope.size();
ret->Left = BuildYtJoinTree(op->LeftArg, scope, ctx, pos);
ret->Right = BuildYtJoinTree(op->RightArg, scope, ctx, pos);
ret->Scope.insert(ret->Scope.end(), scope.begin() + index, scope.end());
return ret;
} else {
YQL_ENSURE(false, "Unknown node type");
}
}

} // namespace

bool AreSimilarTrees(TYtJoinNode::TPtr node1, TYtJoinNode::TPtr node2) {
if (node1 == node2) {
return true;
}
if (node1 && !node2) {
return false;
}
if (node2 && !node1) {
return false;
}
if (node1->Scope != node2->Scope) {
return false;
}
auto opLeft = dynamic_cast<TYtJoinNodeOp*>(node1.Get());
auto opRight = dynamic_cast<TYtJoinNodeOp*>(node2.Get());
if (opLeft && opRight) {
return AreSimilarTrees(opLeft->Left, opRight->Left)
&& AreSimilarTrees(opLeft->Right, opRight->Right);
} else if (!opLeft && !opRight) {
return true;
} else {
return false;
}
}

void BuildOptimizerJoinTree(std::shared_ptr<IBaseOptimizerNode>& tree, std::shared_ptr<IProviderContext>& ctx, TYtJoinNodeOp::TPtr op)
{
TOptimizerTreeBuilder(tree, ctx, op).Do();
}

TYtJoinNode::TPtr BuildYtJoinTree(std::shared_ptr<IBaseOptimizerNode> node, TExprContext& ctx, TPositionHandle pos) {
TVector<TString> scope;
return BuildYtJoinTree(node, scope, ctx, pos);
}

TYtJoinNodeOp::TPtr OrderJoins(TYtJoinNodeOp::TPtr op, const TYtState::TPtr& state, TExprContext& ctx, bool debug)
{
if (state->Types->CostBasedOptimizer == ECostBasedOptimizerType::Disable) {
Expand Down