66namespace NKikimr {
77namespace 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
0 commit comments