Skip to content

Commit 8e5949d

Browse files
pavelvelikhovVelikhov Pavel
andauthored
[NEW RBO] Cleaned up expression extract rule, moved methods to appropriate files (#33656)
Co-authored-by: Velikhov Pavel <pavelvelikhov@localhost.localdomain>
1 parent a624cc6 commit 8e5949d

File tree

8 files changed

+300
-306
lines changed

8 files changed

+300
-306
lines changed

ydb/core/kqp/opt/rbo/kqp_expression.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ TExprNode::TPtr FindMemberArg(TExprNode::TPtr input) {
131131
}
132132
return TExprNode::TPtr();
133133
}
134-
135134
}
136135

137136
namespace NKikimr {
@@ -142,14 +141,12 @@ TExpression::TExpression(TExprNode::TPtr node, TExprContext* ctx, TPlanProps* pr
142141

143142
if (node->IsLambda()) {
144143
Node = node;
145-
Body = node->ChildPtr(1);
146144
} else {
147145
auto arg = Build<TCoArgument>(*ctx, node->Pos()).Name("lambda_arg").Done().Ptr();
148146
Node = Build<TCoLambda>(*ctx, node->Pos())
149147
.Args({arg})
150148
.Body(ReplaceArg(node, arg, *ctx))
151149
.Done().Ptr();
152-
Body = node;
153150
}
154151
}
155152

@@ -350,12 +347,14 @@ TInfoUnit TJoinCondition::GetRightIU() const {
350347
return RightIUs[0];
351348
}
352349

353-
void TJoinCondition::ExtractExpressions(TNodeOnNodeOwnedMap& renameMap, TVector<std::pair<TInfoUnit, TExprNode::TPtr>>& exprMap) {
354-
Y_ENSURE(IncludesExpressions, "Trying to extract expressions from a join condition that doesn't contain them");
350+
bool TJoinCondition::ExtractExpressions(TNodeOnNodeOwnedMap& renameMap, TVector<std::pair<TInfoUnit, TExprNode::TPtr>>& exprMap) {
355351
Y_ENSURE(Expr.PlanProps, "Plan properties null when extracting expressions from join condition");
356352

357-
// Make sure to access the original body, otherwise the pointers won't match the original expressions
358-
auto body = Expr.Body;
353+
if (!IncludesExpressions) {
354+
return false;
355+
}
356+
357+
auto body = Expr.Node->ChildPtr(1);
359358

360359
if (body->IsCallable("FromPg")) {
361360
body = body->ChildPtr(0);
@@ -364,6 +363,7 @@ void TJoinCondition::ExtractExpressions(TNodeOnNodeOwnedMap& renameMap, TVector<
364363
TExprNode::TPtr leftArg;
365364
TExprNode::TPtr rightArg;
366365
TestAndExtractEqualityPredicate(body, leftArg, rightArg);
366+
bool expressionExtracted = false;
367367

368368
if (!leftArg->IsCallable("Member")) {
369369
auto memberArg = FindMemberArg(leftArg);
@@ -378,6 +378,7 @@ void TJoinCondition::ExtractExpressions(TNodeOnNodeOwnedMap& renameMap, TVector<
378378

379379
renameMap[leftArg.Get()] = newLeftArg;
380380
exprMap.emplace_back(TInfoUnit(newName), leftArg);
381+
expressionExtracted = true;
381382
}
382383

383384
if (!rightArg->IsCallable("Member")) {
@@ -393,7 +394,10 @@ void TJoinCondition::ExtractExpressions(TNodeOnNodeOwnedMap& renameMap, TVector<
393394

394395
renameMap[rightArg.Get()] = newRightArg;
395396
exprMap.emplace_back(TInfoUnit(newName), rightArg);
397+
expressionExtracted = true;
396398
}
399+
400+
return expressionExtracted;
397401
}
398402

399403
TExpression MakeColumnAccess(TInfoUnit column, TPositionHandle pos, TExprContext* ctx, TPlanProps* props) {

ydb/core/kqp/opt/rbo/kqp_expression.h

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ class TExpression {
2020

2121
// Constructs an expression from ExprNode, also save expression context and plan
2222
// properties. Plan properties are needed to access subplan IUs
23-
// If an expression is not a lambda, we save its body separately and generate a new
24-
// lambda. The body is needed to by in sych with the originial expression in terms of
25-
// pointers, that might be needed in replace map.
23+
// The expression can be constructed from a full lambda as well as any yql expression.
24+
// In the former case a new lambda is built automatically
25+
2626
TExpression(TExprNode::TPtr node, TExprContext* ctx, TPlanProps* props = nullptr);
2727

2828
TExpression() = default;
@@ -72,7 +72,6 @@ class TExpression {
7272
TString ToString() const;
7373

7474
TExprNode::TPtr Node;
75-
TExprNode::TPtr Body;
7675
TExprContext* Ctx;
7776
TPlanProps* PlanProps;
7877
};
@@ -92,7 +91,7 @@ class TJoinCondition {
9291
TInfoUnit GetRightIU() const;
9392

9493
// Find all non-column reference expression in this condition and insert them into a map
95-
void ExtractExpressions(TNodeOnNodeOwnedMap& map, TVector<std::pair<TInfoUnit, TExprNode::TPtr>>& exprMap);
94+
bool ExtractExpressions(TNodeOnNodeOwnedMap& map, TVector<std::pair<TInfoUnit, TExprNode::TPtr>>& exprMap);
9695

9796
const TExpression& Expr;
9897
TVector<TInfoUnit> LeftIUs;
@@ -102,14 +101,29 @@ class TJoinCondition {
102101
bool EquiJoin = false;
103102
};
104103

104+
// Create an expression that accesses a single column
105105
TExpression MakeColumnAccess(TInfoUnit column, TPositionHandle pos, TExprContext* ctx, TPlanProps* props = nullptr);
106+
107+
// Create a constant expression. Constant expressions don't need plan properties
106108
TExpression MakeConstant(TString type, TString value, TPositionHandle pos, TExprContext* ctx);
109+
110+
// Create. a null expression of a specific type, also doesn't need plan properties
107111
TExpression MakeNothing(TPositionHandle pos, const TTypeAnnotationNode* type, TExprContext* ctx);
112+
113+
// Make a conjunction from a list of conjuncts. Expression context and plan properies will be extracted
114+
// from one of the conjuncts.
108115
TExpression MakeConjunction(const TVector<TExpression>& vec, bool pgSyntax = false);
116+
117+
// Make a binary predicate with an arbitrary callable, extract context and properties from one of the arguments
109118
TExpression MakeBinaryPredicate(TString callable, const TExpression& left, const TExpression& right);
110119

120+
// Get all members from a expression node
111121
void GetAllMembers(TExprNode::TPtr node, TVector<TInfoUnit> &IUs);
122+
123+
// Get all members from an expression node, but also mark subplan context separately and optionally include
124+
// dependencies in correlated subqueries
112125
void GetAllMembers(TExprNode::TPtr node, TVector<TInfoUnit> &IUs, const TPlanProps& props, bool withSubplanContext, bool withDependencies);
126+
113127
TString PrintRBOExpression(TExprNode::TPtr expr, TExprContext & ctx);
114128

115129
}

0 commit comments

Comments
 (0)