Skip to content

Commit b9f0d8d

Browse files
authored
[CBO] UnionAll stats propagation fix (#20746)
1 parent 7c81ab1 commit b9f0d8d

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

ydb/library/yql/dq/opt/dq_opt_stat.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,8 @@ void InferStatisticsForExtendBase(const TExprNode::TPtr& input, TTypeAnnotationC
650650
for (const auto& input: input->Children()) {
651651
if (auto inputStats = typeCtx->GetStats(input.Get())) {
652652
stats->Nrows += inputStats->Nrows;
653+
stats->ByteSize += inputStats->ByteSize;
654+
stats->Cost += inputStats->Cost;
653655
}
654656
}
655657

@@ -849,6 +851,22 @@ void InferStatisticsForDqMerge(const TExprNode::TPtr& input, TTypeAnnotationCont
849851
typeCtx->SetStats(merge.Raw(), newStats);
850852
}
851853

854+
void InferStatisticsForUnionAll(const TExprNode::TPtr& input, TTypeAnnotationContext* typeCtx) {
855+
auto inputNode = TExprBase(input);
856+
auto unionAll = inputNode.Cast<TCoUnionAll>();
857+
858+
auto stats = std::make_shared<TOptimizerStatistics>();
859+
for (const auto& input: input->Children()) {
860+
if (auto inputStats = typeCtx->GetStats(input.Get())) {
861+
stats->Nrows += inputStats->Nrows;
862+
stats->ByteSize += inputStats->ByteSize;
863+
stats->Cost += inputStats->Cost;
864+
}
865+
}
866+
867+
typeCtx->SetStats(inputNode.Raw(), std::move(stats));
868+
}
869+
852870
/**
853871
* Just update the sorted order with alias
854872
*/
@@ -865,8 +883,6 @@ void InferStatisticsForDqPhyCrossJoin(const TExprNode::TPtr& input, TTypeAnnotat
865883
typeCtx->SetStats(cross.Raw(), outputStats);
866884
}
867885

868-
869-
870886
std::shared_ptr<TOptimizerStatistics> RemoveSorting(const std::shared_ptr<TOptimizerStatistics>& stats) {
871887
if (stats->SortingOrderings.HasState()) {
872888
auto newStats = *stats;

ydb/library/yql/dq/opt/dq_opt_stat.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ void InferStatisticsForTopBase(const TExprNode::TPtr& input, TTypeAnnotationCont
2929
void InferStatisticsForSortBase(const TExprNode::TPtr& input, TTypeAnnotationContext* typeCtx);
3030
bool InferStatisticsForListParam(const TExprNode::TPtr& input, TTypeAnnotationContext* typeCtx);
3131
void InferStatisticsForEquiJoin(const TExprNode::TPtr& input, TTypeAnnotationContext* typeCtx);
32+
void InferStatisticsForUnionAll(const TExprNode::TPtr& input, TTypeAnnotationContext* typeCtx);
3233
std::shared_ptr<TOptimizerStatistics> RemoveSorting(const std::shared_ptr<TOptimizerStatistics>& stats);
3334
std::shared_ptr<TOptimizerStatistics> RemoveSorting(const std::shared_ptr<TOptimizerStatistics>& stats, const TExprNode::TPtr& input);
3435

ydb/library/yql/dq/opt/dq_opt_stat_transformer_base.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ bool TDqStatisticsTransformerBase::BeforeLambdas(const TExprNode::TPtr& input, T
139139
else if (auto sortBase = TMaybeNode<TCoSortBase>(input)) {
140140
InferStatisticsForSortBase(input, TypeCtx);
141141
}
142+
else if (TCoUnionAll::Match(input.Get())) {
143+
InferStatisticsForUnionAll(input, TypeCtx);
144+
}
142145
else {
143146
matched = false;
144147
}

0 commit comments

Comments
 (0)