Skip to content

Commit 71fc5f9

Browse files
pavelvelikhovPavel Velikhov
andauthored
[NEW RBO] Fixed bug in statistics computation in Map (#30299)
Co-authored-by: Pavel Velikhov <pavelvelikhov@localhost.localdomain>
1 parent eae21af commit 71fc5f9

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ void ComputeRequiredProps(TOpRoot &root, ui32 props, TRBOContext &ctx) {
5656
void TRuleBasedStage::RunStage(TOpRoot &root, TRBOContext &ctx) {
5757
bool fired = true;
5858
int nMatches = 0;
59+
bool needToLog = NYql::NLog::YqlLogger().NeedToLog(NYql::NLog::EComponent::CoreDq, NYql::NLog::ELevel::TRACE);
60+
5961

6062
while (fired && nMatches < 1000) {
6163
fired = false;
@@ -75,7 +77,7 @@ void TRuleBasedStage::RunStage(TOpRoot &root, TRBOContext &ctx) {
7577
root.SetInput(op);
7678
}
7779

78-
if (rule->LogRule) {
80+
if (needToLog && rule->LogRule) {
7981
YQL_CLOG(TRACE, CoreDq) << "Plan after applying rule:\n" << root.PlanToString(ctx.ExprCtx);
8082
}
8183

@@ -96,23 +98,34 @@ void TRuleBasedStage::RunStage(TOpRoot &root, TRBOContext &ctx) {
9698
}
9799

98100
TExprNode::TPtr TRuleBasedOptimizer::Optimize(TOpRoot &root, TExprContext &ctx) {
99-
YQL_CLOG(TRACE, CoreDq) << "Original plan:\n" << root.PlanToString(ctx);
101+
bool needToLog = NYql::NLog::YqlLogger().NeedToLog(NYql::NLog::EComponent::CoreDq, NYql::NLog::ELevel::TRACE);
102+
103+
if (needToLog) {
104+
YQL_CLOG(TRACE, CoreDq) << "Original plan:\n" << root.PlanToString(ctx);
105+
}
100106

101107
auto context = TRBOContext(KqpCtx, ctx, TypeCtx, RBOTypeAnnTransformer, FuncRegistry);
102108

103109
for (size_t idx = 0; idx < Stages.size(); idx++) {
104110
auto stage = Stages[idx];
105111
YQL_CLOG(TRACE, CoreDq) << "Running stage: " << stage->StageName;
106112
ComputeRequiredProps(root, stage->Props, context);
113+
if (needToLog) {
114+
YQL_CLOG(TRACE, CoreDq) << "Before stage:\n" << root.PlanToString(ctx, EPrintPlanOptions::PrintFullMetadata | EPrintPlanOptions::PrintBasicStatistics);
115+
}
107116
stage->RunStage(root, context);
108-
YQL_CLOG(TRACE, CoreDq) << "After stage:\n" << root.PlanToString(ctx);
117+
if (needToLog) {
118+
YQL_CLOG(TRACE, CoreDq) << "After stage:\n" << root.PlanToString(ctx, EPrintPlanOptions::PrintFullMetadata | EPrintPlanOptions::PrintBasicStatistics);
119+
}
109120
}
110121

111122
YQL_CLOG(TRACE, CoreDq) << "New RBO finished, generating physical plan";
112123

113124
auto convertProps = ERuleProperties::RequireParents | ERuleProperties::RequireTypes | ERuleProperties::RequireStatistics;
114125
ComputeRequiredProps(root, convertProps, context);
115-
YQL_CLOG(TRACE, CoreDq) << "Final plan before generation:\n" << root.PlanToString(ctx, EPrintPlanOptions::PrintFullMetadata | EPrintPlanOptions::PrintBasicStatistics);
126+
if (needToLog) {
127+
YQL_CLOG(TRACE, CoreDq) << "Final plan before generation:\n" << root.PlanToString(ctx, EPrintPlanOptions::PrintFullMetadata | EPrintPlanOptions::PrintBasicStatistics);
128+
}
116129

117130
return ConvertToPhysical(root, context, TypeAnnTransformer, PeepholeTransformer);
118131
}

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ void TOpMap::ComputeMetadata(TRBOContext & ctx, TPlanProps & planProps) {
222222
if (Project) {
223223
Props.Metadata->ColumnsCount = MapElements.size();
224224
} else {
225-
Props.Metadata->ColumnsCount += MapElements.size();
225+
Props.Metadata->ColumnsCount += inputMetadata.ColumnsCount + MapElements.size();
226226
}
227227

228228
auto renames = GetRenamesWithTransforms(planProps);
@@ -282,7 +282,14 @@ void TOpMap::ComputeStatistics(TRBOContext & ctx, TPlanProps & planProps) {
282282
int inputColumnsCount = GetInput()->Props.Metadata->ColumnsCount;
283283
if (Props.Metadata->ColumnsCount != inputColumnsCount) {
284284
double inputDataSize = Props.Statistics->DataSize;
285-
Props.Statistics->DataSize = inputDataSize * Props.Metadata->ColumnsCount / (double)inputColumnsCount;
285+
if (inputColumnsCount!=0) {
286+
Props.Statistics->DataSize = inputDataSize * Props.Metadata->ColumnsCount / (double)inputColumnsCount;
287+
}
288+
// Input may have 0 columns (e.g. EmptySource), in such case the data size depends on the number of records
289+
// and the number of columns in the output. We just assume each column contains 8 bytes
290+
else {
291+
Props.Statistics->DataSize = Props.Statistics->RecordsCount * Props.Metadata->ColumnsCount * 8;
292+
}
286293
}
287294
}
288295

ydb/core/kqp/ut/rbo/kqp_rbo_pg_ut.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,6 @@ Y_UNIT_TEST_SUITE(KqpRboPg) {
229229
NKikimrConfig::TAppConfig appConfig;
230230
appConfig.MutableTableServiceConfig()->SetEnableNewRBO(true);
231231
appConfig.MutableTableServiceConfig()->SetEnableFallbackToYqlOptimizer(false);
232-
// FIXME: Temporarily disabled cost-based optimizer, it break on this query
233-
appConfig.MutableTableServiceConfig()->SetDefaultCostBasedOptimizationLevel(0);
234232

235233
TKikimrRunner kikimr(NKqp::TKikimrSettings(appConfig).SetWithSampleTables(false));
236234
auto db = kikimr.GetTableClient();

0 commit comments

Comments
 (0)