Skip to content

Commit a48c1dd

Browse files
pavelvelikhovPavel Velikhov
authored andcommitted
[NEW RBO] Recompute properties only when needed (ydb-platform#28818)
Co-authored-by: Pavel Velikhov <pavelvelikhov@localhost.localdomain>
1 parent 070b55b commit a48c1dd

File tree

5 files changed

+57
-44
lines changed

5 files changed

+57
-44
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ namespace {
7474
namespace NKikimr {
7575
namespace NKqp {
7676

77+
TConstantFoldingStage::TConstantFoldingStage() {
78+
Props = ERuleProperties::RequireParents | ERuleProperties::RequireTypes;
79+
}
80+
7781
void TConstantFoldingStage::RunStage(TOpRoot &root, TRBOContext &ctx) {
7882
TVector<TExprNode::TPtr> lambdasWithConstExpr;
7983
bool foldUdfs = ctx.KqpCtx.Config->EnableFoldUdfs;

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

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
namespace NKikimr {
77
namespace NKqp {
88

9-
bool TSimplifiedRule::TestAndApply(std::shared_ptr<IOperator> &input, TRBOContext &ctx, TPlanProps &props) {
9+
bool ISimplifiedRule::TestAndApply(std::shared_ptr<IOperator> &input, TRBOContext &ctx, TPlanProps &props) {
1010

1111
auto output = SimpleTestAndApply(input, ctx, props);
1212
if (input != output) {
@@ -16,6 +16,24 @@ bool TSimplifiedRule::TestAndApply(std::shared_ptr<IOperator> &input, TRBOContex
1616
return false;
1717
}
1818
}
19+
20+
TRuleBasedStage::TRuleBasedStage(TVector<std::shared_ptr<IRule>> rules) : Rules(rules) {
21+
for (auto & r : Rules) {
22+
Props |= r->Props;
23+
}
24+
}
25+
26+
void ComputeRequiredProps(TOpRoot &root, ui32 props, TRBOContext &ctx) {
27+
if (props & ERuleProperties::RequireParents) {
28+
root.ComputeParents();
29+
}
30+
if (props & ERuleProperties::RequireTypes) {
31+
if (root.ComputeTypes(ctx) != IGraphTransformer::TStatus::Ok) {
32+
Y_ENSURE(false, "RBO type annotation failed");
33+
}
34+
}
35+
}
36+
1937
/**
2038
* Run a rule-based stage
2139
*
@@ -31,10 +49,6 @@ void TRuleBasedStage::RunStage(TOpRoot &root, TRBOContext &ctx) {
3149
bool fired = true;
3250
int nMatches = 0;
3351

34-
if (root.ComputeTypes(ctx) != IGraphTransformer::TStatus::Ok) {
35-
Y_ENSURE(false, "RBO type annotation failed");
36-
}
37-
3852
while (fired && nMatches < 1000) {
3953
fired = false;
4054

@@ -53,15 +67,7 @@ void TRuleBasedStage::RunStage(TOpRoot &root, TRBOContext &ctx) {
5367
root.Children[0] = op;
5468
}
5569

56-
if (rule->RequiresParentRecompute) {
57-
root.ComputeParents();
58-
}
59-
60-
YQL_CLOG(TRACE, CoreDq) << "After rule:\n" << root.PlanToString(ctx.ExprCtx);
61-
62-
if (root.ComputeTypes(ctx) != IGraphTransformer::TStatus::Ok) {
63-
Y_ENSURE(false, "RBO type annotation failed");
64-
}
70+
ComputeRequiredProps(root, Props, ctx);
6571

6672
nMatches++;
6773
break;
@@ -82,22 +88,18 @@ TExprNode::TPtr TRuleBasedOptimizer::Optimize(TOpRoot &root, TExprContext &ctx)
8288

8389
auto context = TRBOContext(KqpCtx,ctx,TypeCtx, RBOTypeAnnTransformer, FuncRegistry);
8490

85-
if (root.ComputeTypes(context) != IGraphTransformer::TStatus::Ok) {
86-
Y_ENSURE(false, "RBO type annotation failed");
87-
}
88-
8991
for (size_t idx = 0; idx < Stages.size(); idx++) {
9092
YQL_CLOG(TRACE, CoreDq) << "Running stage: " << idx;
9193
auto stage = Stages[idx];
94+
ComputeRequiredProps(root, stage->Props, context);
9295
stage->RunStage(root, context);
9396
YQL_CLOG(TRACE, CoreDq) << "After stage:\n" << root.PlanToString(ctx);
94-
if (root.ComputeTypes(context) != IGraphTransformer::TStatus::Ok) {
95-
Y_ENSURE(false, "RBO type annotation failed");
96-
}
9797
}
9898

9999
YQL_CLOG(TRACE, CoreDq) << "New RBO finished, generating physical plan";
100100

101+
ComputeRequiredProps(root, ERuleProperties::RequireParents | ERuleProperties::RequireTypes, context);
102+
101103
return ConvertToPhysical(root, context, TypeAnnTransformer, PeepholeTransformer);
102104
}
103105
} // namespace NKqp

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ namespace NKqp {
99

1010
using namespace NOpt;
1111

12+
enum ERuleProperties: ui32 {
13+
RequireParents = 0x01,
14+
RequireTypes = 0x02,
15+
RequireTableMeta = 0x04,
16+
RequireCosts = 0x08,
17+
};
18+
1219
/**
1320
* Interface for transformation rule:
1421
*
@@ -18,31 +25,31 @@ using namespace NOpt;
1825
*/
1926
class IRule {
2027
public:
21-
IRule(TString name, bool parentRecompute = true) : RuleName(name), RequiresParentRecompute(parentRecompute) {}
28+
IRule(TString name) : RuleName(name) {}
29+
IRule(TString name, ui32 props) : RuleName(name), Props(props) {}
2230

2331
virtual bool TestAndApply(std::shared_ptr<IOperator> &input, TRBOContext &ctx, TPlanProps &props) = 0;
2432

2533
virtual ~IRule() = default;
2634

2735
TString RuleName;
28-
bool RequiresParentRecompute = true;
36+
ui32 Props;
2937
};
3038

3139
/**
3240
* A Simplified rule does not alter the original subplan that it matched, but instead returns a new
3341
* subplan that replaces the old one
3442
*/
35-
class TSimplifiedRule : public IRule {
43+
class ISimplifiedRule : public IRule {
3644
public:
37-
TSimplifiedRule(TString name, bool parentRecompute = true) : IRule(name, parentRecompute) {}
45+
ISimplifiedRule(TString name) : IRule(name) {}
46+
ISimplifiedRule(TString name, ui32 props) : IRule(name, props) {}
3847

3948
virtual std::shared_ptr<IOperator> SimpleTestAndApply(const std::shared_ptr<IOperator> &input, TRBOContext &ctx, TPlanProps &props) = 0;
4049

4150
virtual bool TestAndApply(std::shared_ptr<IOperator> &input, TRBOContext &ctx, TPlanProps &props) override;
4251
};
4352

44-
class TRuleBasedOptimizer;
45-
4653
/**
4754
* Stage Interface
4855
*
@@ -53,27 +60,20 @@ class IRBOStage {
5360
public:
5461
virtual void RunStage(TOpRoot &root, TRBOContext &ctx) = 0;
5562
virtual ~IRBOStage() = default;
63+
ui32 Props;
5664
};
5765

5866
/**
5967
* Rule based stage is just a collection of rules
6068
*/
6169
class TRuleBasedStage : public IRBOStage {
6270
public:
63-
TRuleBasedStage(TVector<std::shared_ptr<IRule>> rules) : Rules(rules) {}
71+
TRuleBasedStage(TVector<std::shared_ptr<IRule>> rules);
6472
virtual void RunStage(TOpRoot &root, TRBOContext &ctx) override;
6573

6674
TVector<std::shared_ptr<IRule>> Rules;
6775
};
6876

69-
/**
70-
* A Global stage uses its own logic to transform the entire plan
71-
*/
72-
class ISinglePassStage : public IRBOStage {
73-
public:
74-
virtual void RunStage(TOpRoot &root, TRBOContext &ctx) override = 0;
75-
};
76-
7777
/**
7878
* A rule based optimizer is a collection of rule-based and global stages
7979
*/

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class TExtractJoinExpressionsRule : public IRule {
2727
*/
2828
class TInlineScalarSubplanRule : public IRule {
2929
public:
30-
TInlineScalarSubplanRule() : IRule("Inline scalar subplan") {}
30+
TInlineScalarSubplanRule() : IRule("Inline scalar subplan", ERuleProperties::RequireParents | ERuleProperties::RequireTypes) {}
3131

3232
virtual bool TestAndApply(std::shared_ptr<IOperator> &input, TRBOContext &ctx, TPlanProps &props) override;
3333
};
@@ -36,9 +36,9 @@ class TInlineScalarSubplanRule : public IRule {
3636
* Push down a non-projecting map operator
3737
* Currently only pushes below joins that are immediately below
3838
*/
39-
class TPushMapRule : public TSimplifiedRule {
39+
class TPushMapRule : public ISimplifiedRule {
4040
public:
41-
TPushMapRule() : TSimplifiedRule("Push map operator") {}
41+
TPushMapRule() : ISimplifiedRule("Push map operator", ERuleProperties::RequireParents) {}
4242

4343
virtual std::shared_ptr<IOperator> SimpleTestAndApply(const std::shared_ptr<IOperator> &input, TRBOContext &ctx, TPlanProps &props) override;
4444
};
@@ -47,9 +47,9 @@ class TPushMapRule : public TSimplifiedRule {
4747
* Push down filter through joins, adding join conditions to the join operator and potentially
4848
* converting left join into inner join
4949
*/
50-
class TPushFilterRule : public TSimplifiedRule {
50+
class TPushFilterRule : public ISimplifiedRule {
5151
public:
52-
TPushFilterRule() : TSimplifiedRule("Push filter") {}
52+
TPushFilterRule() : ISimplifiedRule("Push filter", ERuleProperties::RequireParents) {}
5353

5454
virtual std::shared_ptr<IOperator> SimpleTestAndApply(const std::shared_ptr<IOperator> &input, TRBOContext &ctx, TPlanProps &props) override;
5555
};
@@ -59,7 +59,7 @@ class TPushFilterRule : public TSimplifiedRule {
5959
*/
6060
class TAssignStagesRule : public IRule {
6161
public:
62-
TAssignStagesRule() : IRule("Assign stages") {}
62+
TAssignStagesRule() : IRule("Assign stages", ERuleProperties::RequireParents) {}
6363

6464
virtual bool TestAndApply(std::shared_ptr<IOperator> &input, TRBOContext &ctx, TPlanProps &props) override;
6565
};
@@ -70,16 +70,18 @@ extern TRuleBasedStage RuleStage2;
7070
/**
7171
* Separate global stage to remove extra renames and project out unneeded columns
7272
*/
73-
class TRenameStage : public ISinglePassStage {
73+
class TRenameStage : public IRBOStage {
7474
public:
75+
TRenameStage();
7576
virtual void RunStage(TOpRoot &root, TRBOContext &ctx) override;
7677
};
7778

7879
/**
7980
* Separate global constant folding stage
8081
*/
81-
class TConstantFoldingStage : public ISinglePassStage {
82+
class TConstantFoldingStage : public IRBOStage {
8283
public:
84+
TConstantFoldingStage();
8385
virtual void RunStage(TOpRoot &root, TRBOContext &ctx) override;
8486
};
8587

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ struct TIntTUnitPairHash {
123123
/**
124124
* Global stage that removed unnecessary renames and unused columns
125125
*/
126+
127+
TRenameStage::TRenameStage() {
128+
Props = ERuleProperties::RequireParents;
129+
}
130+
126131
void TRenameStage::RunStage(TOpRoot &root, TRBOContext &ctx) {
127132

128133
YQL_CLOG(TRACE, CoreDq) << "Before compute parents";

0 commit comments

Comments
 (0)