Skip to content

Commit ebad631

Browse files
Fixed statistics calculation for index read (#6629)
1 parent e0d21c5 commit ebad631

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

ydb/core/kqp/opt/kqp_statistics_transformer.cpp

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ void InferStatisticsForReadTable(const TExprNode::TPtr& input, TTypeAnnotationCo
4444
Y_ENSURE(false, "Invalid node type for InferStatisticsForReadTable");
4545
}
4646

47+
auto keyColumns = inputStats->KeyColumns;
48+
if (auto indexRead = inputNode.Maybe<TKqlReadTableIndex>()) {
49+
const auto& tableData = kqpCtx.Tables->ExistingTable(kqpCtx.Cluster, indexRead.Cast().Table().Path().Value());
50+
const auto& [indexMeta, _] = tableData.Metadata->GetIndexMetadata(indexRead.Cast().Index().StringValue());
51+
52+
keyColumns = TIntrusivePtr<TOptimizerStatistics::TKeyColumns>(
53+
new TOptimizerStatistics::TKeyColumns(indexMeta->KeyColumnNames));
54+
}
55+
4756
/**
4857
* We need index statistics to calculate this in the future
4958
* Right now we use very small estimates to make sure CBO picks Lookup Joins
@@ -65,7 +74,7 @@ void InferStatisticsForReadTable(const TExprNode::TPtr& input, TTypeAnnotationCo
6574
nAttrs,
6675
byteSize,
6776
0.0,
68-
inputStats->KeyColumns,
77+
keyColumns,
6978
inputStats->ColumnStatistics);
7079

7180
YQL_CLOG(TRACE, CoreDq) << "Infer statistics for read table, nrows: " << stats->Nrows << ", nattrs: " << stats->Ncols << ", byteSize: " << stats->ByteSize;
@@ -169,7 +178,9 @@ void InferStatisticsForLookupTable(const TExprNode::TPtr& input, TTypeAnnotation
169178
* We look into range expression to check if its a point lookup or a full scan
170179
* We currently don't try to figure out whether this is a small range vs full scan
171180
*/
172-
void InferStatisticsForRowsSourceSettings(const TExprNode::TPtr& input, TTypeAnnotationContext* typeCtx) {
181+
void InferStatisticsForRowsSourceSettings(const TExprNode::TPtr& input, TTypeAnnotationContext* typeCtx,
182+
const TKqpOptimizeContext& kqpCtx) {
183+
173184
auto inputNode = TExprBase(input);
174185
auto sourceSettings = inputNode.Cast<TKqpReadRangesSourceSettings>();
175186

@@ -192,6 +203,15 @@ void InferStatisticsForRowsSourceSettings(const TExprNode::TPtr& input, TTypeAnn
192203
}
193204
}
194205

206+
auto keyColumns = inputStats->KeyColumns;
207+
if (auto indexRead = inputNode.Maybe<TKqlReadTableIndexRanges>()) {
208+
const auto& tableData = kqpCtx.Tables->ExistingTable(kqpCtx.Cluster, indexRead.Cast().Table().Path().Value());
209+
const auto& [indexMeta, _] = tableData.Metadata->GetIndexMetadata(indexRead.Cast().Index().StringValue());
210+
211+
keyColumns = TIntrusivePtr<TOptimizerStatistics::TKeyColumns>(
212+
new TOptimizerStatistics::TKeyColumns(indexMeta->KeyColumnNames));
213+
}
214+
195215
int nAttrs = sourceSettings.Columns().Size();
196216

197217
double sizePerRow = inputStats->ByteSize / (inputRows==0?1:inputRows);
@@ -204,7 +224,7 @@ void InferStatisticsForRowsSourceSettings(const TExprNode::TPtr& input, TTypeAnn
204224
nAttrs,
205225
byteSize,
206226
cost,
207-
inputStats->KeyColumns,
227+
keyColumns,
208228
inputStats->ColumnStatistics));
209229
}
210230

@@ -355,7 +375,7 @@ bool TKqpStatisticsTransformer::BeforeLambdasSpecific(const TExprNode::TPtr& inp
355375
InferStatisticsForKqpTable(input, TypeCtx, KqpCtx);
356376
}
357377
else if (TKqpReadRangesSourceSettings::Match(input.Get())) {
358-
InferStatisticsForRowsSourceSettings(input, TypeCtx);
378+
InferStatisticsForRowsSourceSettings(input, TypeCtx, KqpCtx);
359379
}
360380
else if (TKqpCnStreamLookup::Match(input.Get())) {
361381
InferStatisticsForSteamLookup(input, TypeCtx);

0 commit comments

Comments
 (0)