@@ -177,6 +177,22 @@ TMaybeNode<TExprBase> TryBuildTrivialReadTable(TCoFlatMap& flatmap, TKqlReadTabl
177
177
.Done ();
178
178
}
179
179
180
+ TMaybeNode<TCoLambda> ExtractTopSortKeySelector (TExprBase node, const NYql::TParentsMap& parentsMap) {
181
+ auto it = parentsMap.find (node.Raw ());
182
+ if (it != parentsMap.end ()) {
183
+ if (it->second .size () != 1 ) {
184
+ return {};
185
+ }
186
+ for (auto * node : it->second ) {
187
+ if (TCoTopSort::Match (node)) {
188
+ TCoTopSort topSort (node);
189
+ return topSort.KeySelectorLambda ();
190
+ }
191
+ }
192
+ }
193
+ return {};
194
+ }
195
+
180
196
} // namespace
181
197
182
198
TExprBase KqpPushExtractedPredicateToReadTable (TExprBase node, TExprContext& ctx, const TKqpOptimizeContext& kqpCtx,
@@ -269,7 +285,7 @@ TExprBase KqpPushExtractedPredicateToReadTable(TExprBase node, TExprContext& ctx
269
285
YQL_ENSURE (prepareSuccess);
270
286
271
287
if (!indexName.IsValid () && !readSettings.ForcePrimary && kqpCtx.Config ->IndexAutoChooserMode != NKikimrConfig::TTableServiceConfig_EIndexAutoChooseMode_DISABLED) {
272
- using TIndexComparisonKey = std::tuple<bool , size_t , bool , size_t , bool >;
288
+ using TIndexComparisonKey = std::tuple<bool , bool , size_t , bool , size_t , bool >;
273
289
auto calcNeedsJoin = [&] (const TKikimrTableMetadataPtr& keyTable) -> bool {
274
290
bool needsJoin = false ;
275
291
for (auto && column : read.Columns ()) {
@@ -280,8 +296,16 @@ TExprBase KqpPushExtractedPredicateToReadTable(TExprBase node, TExprContext& ctx
280
296
return needsJoin;
281
297
};
282
298
283
- auto calcKey = [&](NYql::IPredicateRangeExtractor::TBuildResult buildResult, size_t descriptionKeyColumns, bool needsJoin) -> TIndexComparisonKey {
299
+ auto keySelector = ExtractTopSortKeySelector (flatmap, parentsMap);
300
+
301
+ auto calcKey = [&](
302
+ NYql::IPredicateRangeExtractor::TBuildResult buildResult,
303
+ size_t descriptionKeyColumns,
304
+ bool needsJoin,
305
+ const NYql::TKikimrTableDescription & tableDesc) -> TIndexComparisonKey
306
+ {
284
307
return std::make_tuple (
308
+ keySelector.IsValid () && IsSortKeyPrimary (keySelector.Cast (), tableDesc),
285
309
buildResult.PointPrefixLen >= descriptionKeyColumns,
286
310
buildResult.PointPrefixLen >= descriptionKeyColumns ? 0 : buildResult.PointPrefixLen ,
287
311
buildResult.UsedPrefixLen >= descriptionKeyColumns,
@@ -293,7 +317,7 @@ TExprBase KqpPushExtractedPredicateToReadTable(TExprBase node, TExprContext& ctx
293
317
auto primaryBuildResult = extractor->BuildComputeNode (mainTableDesc.Metadata ->KeyColumnNames , ctx, typesCtx);
294
318
295
319
if (primaryBuildResult.PointPrefixLen < mainTableDesc.Metadata ->KeyColumnNames .size ()) {
296
- auto maxKey = calcKey (primaryBuildResult, mainTableDesc.Metadata ->KeyColumnNames .size (), false );
320
+ auto maxKey = calcKey (primaryBuildResult, mainTableDesc.Metadata ->KeyColumnNames .size (), false , mainTableDesc );
297
321
for (auto & index : mainTableDesc.Metadata ->Indexes ) {
298
322
if (index.Type != TIndexDescription::EType::GlobalAsync) {
299
323
auto & tableDesc = kqpCtx.Tables ->ExistingTable (kqpCtx.Cluster , mainTableDesc.Metadata ->GetIndexMetadata (TString (index.Name )).first ->Name );
@@ -307,7 +331,7 @@ TExprBase KqpPushExtractedPredicateToReadTable(TExprBase node, TExprContext& ctx
307
331
continue ;
308
332
}
309
333
310
- auto key = calcKey (buildResult, index.KeyColumns .size (), needsJoin);
334
+ auto key = calcKey (buildResult, index.KeyColumns .size (), needsJoin, tableDesc );
311
335
if (key > maxKey) {
312
336
maxKey = key;
313
337
chosenIndex = index.Name ;
0 commit comments