@@ -17,8 +17,7 @@ using namespace NYql::NNodes;
17
17
18
18
namespace {
19
19
20
- TCoAtomList BuildKeyColumnsList (const TKikimrTableDescription& /* table */ , TPositionHandle pos, TExprContext& ctx,
21
- const auto & columnsToSelect) {
20
+ TCoAtomList BuildKeyColumnsList (TPositionHandle pos, TExprContext& ctx, const auto & columnsToSelect) {
22
21
TVector<TExprBase> columnsList;
23
22
columnsList.reserve (columnsToSelect.size ());
24
23
for (auto column : columnsToSelect) {
@@ -35,7 +34,7 @@ TCoAtomList BuildKeyColumnsList(const TKikimrTableDescription& /* table */, TPos
35
34
}
36
35
37
36
TCoAtomList BuildKeyColumnsList (const TKikimrTableDescription& table, TPositionHandle pos, TExprContext& ctx) {
38
- return BuildKeyColumnsList (table, pos, ctx, table.Metadata ->KeyColumnNames );
37
+ return BuildKeyColumnsList (pos, ctx, table.Metadata ->KeyColumnNames );
39
38
}
40
39
41
40
TCoAtomList MergeColumns (const NNodes::TCoAtomList& col1, const TVector<TString>& col2, TExprContext& ctx) {
@@ -306,21 +305,20 @@ struct TReadMatch {
306
305
}
307
306
};
308
307
309
- template <typename TRead>
310
- bool CheckIndexCovering (const TRead& read, const TIntrusivePtr<TKikimrTableMetadata>& indexMeta) {
311
- for (const auto & col : read.Columns ()) {
308
+ bool CheckIndexCovering (const TCoAtomList& readColumns, const TIntrusivePtr<TKikimrTableMetadata>& indexMeta) {
309
+ for (const auto & col : readColumns) {
312
310
if (!indexMeta->Columns .contains (col.StringValue ())) {
313
- return true ;
311
+ return false ;
314
312
}
315
313
}
316
- return false ;
314
+ return true ;
317
315
}
318
316
319
317
TExprBase DoRewriteIndexRead (const TReadMatch& read, TExprContext& ctx,
320
318
const TKikimrTableDescription& tableDesc, TIntrusivePtr<TKikimrTableMetadata> indexMeta, bool useStreamLookup,
321
319
const TVector<TString>& extraColumns, const std::function<TExprBase(const TExprBase&)>& middleFilter = {})
322
320
{
323
- const bool needDataRead = CheckIndexCovering (read, indexMeta);
321
+ const bool isCovered = CheckIndexCovering (read. Columns () , indexMeta);
324
322
325
323
if (read.FullScan ()) {
326
324
const auto indexName = read.Index ().StringValue ();
@@ -329,7 +327,7 @@ TExprBase DoRewriteIndexRead(const TReadMatch& read, TExprContext& ctx,
329
327
ctx.AddWarning (issue);
330
328
}
331
329
332
- if (!needDataRead ) {
330
+ if (isCovered ) {
333
331
// We can read all data from index table.
334
332
auto ret = read.BuildRead (ctx, BuildTableMeta (*indexMeta, read.Pos (), ctx), read.Columns ());
335
333
@@ -522,22 +520,37 @@ void VectorReadLevel(
522
520
523
521
void VectorReadMain (
524
522
TExprContext& ctx, TPositionHandle pos,
525
- const TKqpTable& postingTable, const TCoAtomList& postingColumns,
526
- const TKqpTable& mainTable, const TCoAtomList& mainColumns,
523
+ const TKqpTable& postingTable,
524
+ const TIntrusivePtr<TKikimrTableMetadata> & postingTableMeta,
525
+ const TKqpTable& mainTable,
526
+ const TIntrusivePtr<TKikimrTableMetadata> & mainTableMeta,
527
+ const TCoAtomList& mainColumns,
527
528
TExprNodePtr& read)
528
529
{
529
- // TODO(mbkkt) handle covered index columns
530
- read = Build<TKqlLookupTable>(ctx, pos)
531
- .Table (postingTable)
532
- .LookupKeys (read)
533
- .Columns (postingColumns)
534
- .Done ().Ptr ();
530
+ const bool isCovered = CheckIndexCovering (mainColumns, postingTableMeta);
535
531
536
- read = Build<TKqlLookupTable>(ctx, pos)
537
- .Table (mainTable)
538
- .LookupKeys (read)
539
- .Columns (mainColumns)
540
- .Done ().Ptr ();
532
+ if (!isCovered) {
533
+ const auto postingColumns = BuildKeyColumnsList (pos, ctx, mainTableMeta->KeyColumnNames );
534
+
535
+ read = Build<TKqlLookupTable>(ctx, pos)
536
+ .Table (postingTable)
537
+ .LookupKeys (read)
538
+ .Columns (postingColumns)
539
+ .Done ().Ptr ();
540
+
541
+ read = Build<TKqlLookupTable>(ctx, pos)
542
+ .Table (mainTable)
543
+ .LookupKeys (read)
544
+ .Columns (mainColumns)
545
+ .Done ().Ptr ();
546
+ } else {
547
+ read = Build<TKqlStreamLookupTable>(ctx, pos)
548
+ .Table (postingTable)
549
+ .LookupKeys (read)
550
+ .Columns (mainColumns)
551
+ .Settings (settings.BuildNode (ctx, pos))
552
+ .Done ().Ptr ();
553
+ }
541
554
}
542
555
543
556
void VectorTopMain (TExprContext& ctx, const TCoTopBase& top, TExprNodePtr& read) {
@@ -569,9 +582,8 @@ TExprBase DoRewriteTopSortOverKMeansTree(
569
582
const auto postingTable = BuildTableMeta (*postingTableDesc->Metadata , pos, ctx);
570
583
const auto mainTable = BuildTableMeta (*tableDesc.Metadata , pos, ctx);
571
584
572
- const auto levelColumns = BuildKeyColumnsList (*levelTableDesc, pos, ctx,
585
+ const auto levelColumns = BuildKeyColumnsList (pos, ctx,
573
586
std::initializer_list<std::string_view>{NTableIndex::NTableVectorKmeansTreeIndex::IdColumn, NTableIndex::NTableVectorKmeansTreeIndex::CentroidColumn});
574
- const auto postingColumns = BuildKeyColumnsList (*postingTableDesc, pos, ctx, tableDesc.Metadata ->KeyColumnNames );
575
587
const auto & mainColumns = match.Columns ();
576
588
577
589
TNodeOnNodeOwnedMap replaces;
@@ -602,7 +614,7 @@ TExprBase DoRewriteTopSortOverKMeansTree(
602
614
603
615
VectorReadLevel (indexDesc, ctx, pos, kqpCtx, levelLambda, top, levelTable, levelColumns, read);
604
616
605
- VectorReadMain (ctx, pos, postingTable, postingColumns , mainTable, mainColumns, read);
617
+ VectorReadMain (ctx, pos, postingTable, postingTableDesc-> Metadata , mainTable, tableDesc. Metadata , mainColumns, read);
606
618
607
619
if (flatMap) {
608
620
read = Build<TCoFlatMap>(ctx, flatMap.Cast ().Pos ())
@@ -639,13 +651,12 @@ TExprBase DoRewriteTopSortOverPrefixedKMeansTree(
639
651
const auto prefixTable = BuildTableMeta (*prefixTableDesc->Metadata , pos, ctx);
640
652
const auto mainTable = BuildTableMeta (*tableDesc.Metadata , pos, ctx);
641
653
642
- const auto levelColumns = BuildKeyColumnsList (*levelTableDesc, pos, ctx,
654
+ const auto levelColumns = BuildKeyColumnsList (pos, ctx,
643
655
std::initializer_list<std::string_view>{NTableIndex::NTableVectorKmeansTreeIndex::IdColumn, NTableIndex::NTableVectorKmeansTreeIndex::CentroidColumn});
644
- const auto postingColumns = BuildKeyColumnsList (*postingTableDesc, pos, ctx, tableDesc.Metadata ->KeyColumnNames );
645
656
const auto prefixColumns = [&] {
646
657
auto columns = indexDesc.KeyColumns ;
647
658
columns.back ().assign (NTableIndex::NTableVectorKmeansTreeIndex::IdColumn);
648
- return BuildKeyColumnsList (*prefixTableDesc, pos, ctx, columns);
659
+ return BuildKeyColumnsList (pos, ctx, columns);
649
660
}();
650
661
const auto & mainColumns = match.Columns ();
651
662
@@ -686,7 +697,7 @@ TExprBase DoRewriteTopSortOverPrefixedKMeansTree(
686
697
687
698
VectorReadLevel (indexDesc, ctx, pos, kqpCtx, levelLambda, top, levelTable, levelColumns, read);
688
699
689
- VectorReadMain (ctx, pos, postingTable, postingColumns , mainTable, mainColumns, read);
700
+ VectorReadMain (ctx, pos, postingTable, postingTableDesc-> Metadata , mainTable, tableDesc. Metadata , mainColumns, read);
690
701
691
702
if (mainLambda) {
692
703
read = Build<TCoMap>(ctx, flatMap.Pos ())
@@ -733,9 +744,9 @@ TExprBase KqpRewriteLookupIndex(const TExprBase& node, TExprContext& ctx, const
733
744
YQL_ENSURE (indexDesc->Type != TIndexDescription::EType::GlobalSyncVectorKMeansTree,
734
745
" lookup doesn't support vector index: " << indexName);
735
746
736
- const bool needDataRead = CheckIndexCovering (lookupIndex, implTable);
747
+ const bool isCovered = CheckIndexCovering (lookupIndex. Columns () , implTable);
737
748
738
- if (!needDataRead ) {
749
+ if (isCovered ) {
739
750
if (kqpCtx.Config ->EnableKqpDataQueryStreamLookup ) {
740
751
TKqpStreamLookupSettings settings;
741
752
settings.Strategy = EStreamLookupStrategyType::LookupRows;
@@ -805,8 +816,8 @@ TExprBase KqpRewriteStreamLookupIndex(const TExprBase& node, TExprContext& ctx,
805
816
YQL_ENSURE (indexDesc->Type != TIndexDescription::EType::GlobalSyncVectorKMeansTree,
806
817
" stream lookup doesn't support vector index: " << indexName);
807
818
808
- const bool needDataRead = CheckIndexCovering (streamLookupIndex, implTable);
809
- if (!needDataRead ) {
819
+ const bool isCovered = CheckIndexCovering (streamLookupIndex. Columns () , implTable);
820
+ if (isCovered ) {
810
821
return Build<TKqlStreamLookupTable>(ctx, node.Pos ())
811
822
.Table (BuildTableMeta (*implTable, node.Pos (), ctx))
812
823
.LookupKeys (streamLookupIndex.LookupKeys ())
0 commit comments