@@ -177,6 +177,22 @@ TMaybeNode<TExprBase> TryBuildTrivialReadTable(TCoFlatMap& flatmap, TKqlReadTabl
177177 .Done ();
178178}
179179
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+
180196} // namespace
181197
182198TExprBase KqpPushExtractedPredicateToReadTable (TExprBase node, TExprContext& ctx, const TKqpOptimizeContext& kqpCtx,
@@ -269,7 +285,7 @@ TExprBase KqpPushExtractedPredicateToReadTable(TExprBase node, TExprContext& ctx
269285 YQL_ENSURE (prepareSuccess);
270286
271287 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 >;
273289 auto calcNeedsJoin = [&] (const TKikimrTableMetadataPtr& keyTable) -> bool {
274290 bool needsJoin = false ;
275291 for (auto && column : read.Columns ()) {
@@ -280,8 +296,16 @@ TExprBase KqpPushExtractedPredicateToReadTable(TExprBase node, TExprContext& ctx
280296 return needsJoin;
281297 };
282298
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+ {
284307 return std::make_tuple (
308+ keySelector.IsValid () && IsSortKeyPrimary (keySelector.Cast (), tableDesc),
285309 buildResult.PointPrefixLen >= descriptionKeyColumns,
286310 buildResult.PointPrefixLen >= descriptionKeyColumns ? 0 : buildResult.PointPrefixLen ,
287311 buildResult.UsedPrefixLen >= descriptionKeyColumns,
@@ -293,7 +317,7 @@ TExprBase KqpPushExtractedPredicateToReadTable(TExprBase node, TExprContext& ctx
293317 auto primaryBuildResult = extractor->BuildComputeNode (mainTableDesc.Metadata ->KeyColumnNames , ctx, typesCtx);
294318
295319 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 );
297321 for (auto & index : mainTableDesc.Metadata ->Indexes ) {
298322 if (index.Type != TIndexDescription::EType::GlobalAsync) {
299323 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
307331 continue ;
308332 }
309333
310- auto key = calcKey (buildResult, index.KeyColumns .size (), needsJoin);
334+ auto key = calcKey (buildResult, index.KeyColumns .size (), needsJoin, tableDesc );
311335 if (key > maxKey) {
312336 maxKey = key;
313337 chosenIndex = index.Name ;
0 commit comments