22#include " actors/kqp_ic_gateway_actors.h"
33
44#include < ydb/core/base/path.h>
5+ #include < ydb/core/base/table_index.h>
56#include < ydb/core/external_sources/external_source_factory.h>
67#include < ydb/core/kqp/federated_query/kqp_federated_query_actors.h>
78#include < ydb/core/kqp/gateway/utils/scheme_helpers.h>
@@ -175,7 +176,7 @@ TTableMetadataResult GetTableMetadataResult(const NSchemeCache::TSchemeCacheNavi
175176 THashMap<TString, NYql::TKikimrPathId> sequences;
176177
177178 for (const auto & sequenceDesc : entry.Sequences ) {
178- sequences[sequenceDesc.GetName ()] =
179+ sequences[sequenceDesc.GetName ()] =
179180 NYql::TKikimrPathId (sequenceDesc.GetPathId ().GetOwnerId (), sequenceDesc.GetPathId ().GetLocalId ());
180181 }
181182
@@ -187,7 +188,7 @@ TTableMetadataResult GetTableMetadataResult(const NSchemeCache::TSchemeCacheNavi
187188 const TString typeName = GetTypeName (NScheme::TTypeInfoMod{columnDesc.PType , columnDesc.PTypeMod });
188189 auto defaultKind = NKikimrKqp::TKqpColumnMetadataProto::DEFAULT_KIND_UNSPECIFIED;
189190 NYql::TKikimrPathId defaultFromSequencePathId = {};
190-
191+
191192 if (columnDesc.IsDefaultFromSequence ()) {
192193 defaultKind = NKikimrKqp::TKqpColumnMetadataProto::DEFAULT_KIND_SEQUENCE;
193194 auto sequenceIt = sequences.find (columnDesc.DefaultFromSequence );
@@ -196,7 +197,7 @@ TTableMetadataResult GetTableMetadataResult(const NSchemeCache::TSchemeCacheNavi
196197 } else if (columnDesc.IsDefaultFromLiteral ()) {
197198 defaultKind = NKikimrKqp::TKqpColumnMetadataProto::DEFAULT_KIND_LITERAL;
198199 }
199-
200+
200201 tableMeta->Columns .emplace (
201202 columnDesc.Name ,
202203 NYql::TKikimrColumnMetadata (
@@ -400,11 +401,15 @@ TString GetDebugString(const std::pair<NKikimr::TIndexId, TString>& id) {
400401 return TStringBuilder () << " Path: " << id.second << " TableId: " << id.first ;
401402}
402403
403- void UpdateMetadataIfSuccess (NYql::TKikimrTableMetadataPtr ptr, size_t idx, const TTableMetadataResult& value) {
404- if (value.Success ()) {
405- ptr->SecondaryGlobalIndexMetadata [idx] = value.Metadata ;
404+ void UpdateMetadataIfSuccess (NYql::TKikimrTableMetadataPtr& implTable, TTableMetadataResult& value) {
405+ YQL_ENSURE (value.Success ());
406+ if (!implTable) {
407+ implTable = std::move (value.Metadata );
408+ return ;
406409 }
407-
410+ YQL_ENSURE (!implTable->Next );
411+ YQL_ENSURE (implTable->Name < value.Metadata ->Name );
412+ implTable->Next = std::move (value.Metadata );
408413}
409414
410415void SetError (TTableMetadataResult& externalDataSourceMetadata, const TString& error) {
@@ -618,28 +623,21 @@ NThreading::TFuture<TTableMetadataResult> TKqpTableMetadataLoader::LoadIndexMeta
618623 const auto & tableName = tableMetadata->Name ;
619624 const size_t indexesCount = tableMetadata->Indexes .size ();
620625
621- TVector<NThreading::TFuture<TGenericResult >> children;
626+ TVector<NThreading::TFuture<TTableMetadataResult >> children;
622627 children.reserve (indexesCount);
623628
624- tableMetadata->SecondaryGlobalIndexMetadata .resize (indexesCount);
625629 const ui64 tableOwnerId = tableMetadata->PathId .OwnerId ();
626630
627631 for (size_t i = 0 ; i < indexesCount; i++) {
628632 const auto & index = tableMetadata->Indexes [i];
629- const auto indexTablePaths = NSchemeHelpers::CreateIndexTablePath (tableName, index.Type , index.Name );
630- for (const auto & indexTablePath : indexTablePaths ) {
633+ const auto implTablePaths = NSchemeHelpers::CreateIndexTablePath (tableName, index.Type , index.Name );
634+ for (const auto & implTablePath : implTablePaths ) {
631635 if (!index.SchemaVersion ) {
632636 LOG_DEBUG_S (*ActorSystem, NKikimrServices::KQP_GATEWAY, " Load index metadata without schema version check index: " << index.Name );
633637 children.push_back (
634- LoadTableMetadata (cluster, indexTablePath ,
638+ LoadTableMetadata (cluster, implTablePath ,
635639 TLoadTableMetadataSettings ().WithPrivateTables (true ), database, userToken)
636- .Apply ([i, tableMetadata](const TFuture<TTableMetadataResult>& result) {
637- auto value = result.GetValue ();
638- UpdateMetadataIfSuccess (tableMetadata, i, value);
639- return static_cast <TGenericResult>(value);
640- })
641640 );
642-
643641 } else {
644642 LOG_DEBUG_S (*ActorSystem, NKikimrServices::KQP_GATEWAY, " Load index metadata with schema version check"
645643 << " index: " << index.Name
@@ -650,12 +648,7 @@ NThreading::TFuture<TTableMetadataResult> TKqpTableMetadataLoader::LoadIndexMeta
650648 auto ownerId = index.PathOwnerId ? index.PathOwnerId : tableOwnerId; // for compat with 20-2
651649 children.push_back (
652650 LoadIndexMetadataByPathId (cluster,
653- NKikimr::TIndexId (ownerId, index.LocalPathId , index.SchemaVersion ), indexTablePath, database, userToken)
654- .Apply ([i, tableMetadata](const TFuture<TTableMetadataResult>& result) {
655- auto value = result.GetValue ();
656- UpdateMetadataIfSuccess (tableMetadata, i, value);
657- return static_cast <TGenericResult>(value);
658- })
651+ NKikimr::TIndexId (ownerId, index.LocalPathId , index.SchemaVersion ), implTablePath, database, userToken)
659652 );
660653
661654 }
@@ -666,14 +659,26 @@ NThreading::TFuture<TTableMetadataResult> TKqpTableMetadataLoader::LoadIndexMeta
666659 auto loadIndexMetadataChecker =
667660 [ptr, result{std::move (loadTableMetadataResult)}, children](const NThreading::TFuture<void >) mutable {
668661 bool loadOk = true ;
669- for (const auto & child : children) {
670- result.AddIssues (child.GetValue ().Issues ());
671- if (!child.GetValue ().Success ()) {
672- loadOk = false ;
662+
663+ const auto indexesCount = result.Metadata ->Indexes .size ();
664+ result.Metadata ->ImplTables .resize (indexesCount);
665+ auto it = children.begin ();
666+ for (size_t i = 0 ; i < indexesCount; i++) {
667+ for (const auto & _ : NTableIndex::GetImplTables (NYql::TIndexDescription::ConvertIndexType (
668+ result.Metadata ->Indexes [i].Type ))) {
669+ YQL_ENSURE (it != children.end ());
670+ auto value = it++->ExtractValue ();
671+ result.AddIssues (value.Issues ());
672+ if (loadOk && (loadOk = value.Success ())) {
673+ UpdateMetadataIfSuccess (result.Metadata ->ImplTables [i], value);
674+ }
673675 }
674676 }
677+ YQL_ENSURE (it == children.end ());
678+
675679 auto locked = ptr.lock ();
676680 if (!loadOk || !locked) {
681+ result.Metadata ->ImplTables .clear ();
677682 result.SetStatus (TIssuesIds::KIKIMR_INDEX_METADATA_LOAD_FAILED);
678683 } else {
679684 locked->OnLoadedTableMetadata (result);
@@ -909,13 +914,17 @@ NThreading::TFuture<TTableMetadataResult> TKqpTableMetadataLoader::LoadTableMeta
909914 case EKind::KindIndex: {
910915 Y_ENSURE (entry.ListNodeEntry , " expected children list" );
911916 for (const auto & child : entry.ListNodeEntry ->Children ) {
917+ if (!table.EndsWith (child.Name )) {
918+ continue ;
919+ }
912920 TIndexId pathId = TIndexId (child.PathId , child.SchemaVersion );
913921
914922 LoadTableMetadataCache (cluster, std::make_pair (pathId, table), settings, database, userToken)
915923 .Apply ([promise](const TFuture<TTableMetadataResult>& result) mutable
916924 {
917925 promise.SetValue (result.GetValue ());
918926 });
927+ break ;
919928 }
920929 break ;
921930 }
0 commit comments