Skip to content

Commit dba1ef6

Browse files
committed
Fixes
1 parent 6ac905f commit dba1ef6

File tree

6 files changed

+309
-52
lines changed

6 files changed

+309
-52
lines changed

ydb/core/kqp/compile_service/kqp_compile_service.cpp

+26-9
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@ class TKqpCompileService : public TActorBootstrapped<TKqpCompileService> {
741741

742742
bool keepInCache = compileRequest.KeepInCache && compileResult->AllowCache;
743743

744-
bool hasTempTables = WithCache(compileResult, compileRequest.TempTablesState) == nullptr;
744+
bool hasTempTables = WithCache(compileResult, compileRequest.TempTablesState, true) == nullptr;
745745

746746
try {
747747
if (compileResult->Status == Ydb::StatusIds::SUCCESS) {
@@ -815,18 +815,35 @@ class TKqpCompileService : public TActorBootstrapped<TKqpCompileService> {
815815
}
816816

817817
TKqpCompileResult::TConstPtr WithCache(
818-
TKqpCompileResult::TConstPtr cacheResult, TKqpTempTablesState::TConstPtr tempTablesState) {
819-
if (!cacheResult) {
818+
TKqpCompileResult::TConstPtr compileResult,
819+
TKqpTempTablesState::TConstPtr tempTablesState, bool forInsert = false) {
820+
if (!compileResult) {
820821
return nullptr;
821822
}
822-
if (!cacheResult->PreparedQuery) {
823-
return cacheResult;
823+
if (!compileResult->PreparedQuery) {
824+
return compileResult;
824825
}
825-
auto hasTempTables = cacheResult->PreparedQuery->HasTempTables(tempTablesState);
826-
if (hasTempTables) {
827-
return nullptr;
826+
if (forInsert) {
827+
auto hasTempTables = compileResult->PreparedQuery->HasTempTables(tempTablesState);
828+
if (hasTempTables) {
829+
return nullptr;
830+
}
831+
return compileResult;
832+
}
833+
if (!tempTablesState) {
834+
return compileResult;
835+
}
836+
auto tables = compileResult->PreparedQuery->GetQueryTables();
837+
auto tempTables = THashSet<TString>();
838+
for (const auto& [path, info] : tempTablesState->TempTables) {
839+
tempTables.insert(path.second);
840+
}
841+
for (const auto& path: tables) {
842+
if (tempTables.contains(path)) {
843+
return nullptr;
844+
}
828845
}
829-
return cacheResult;
846+
return compileResult;
830847
}
831848

832849
void UpdateQueryCache(TKqpCompileResult::TConstPtr compileResult, bool keepInCache) {

ydb/core/kqp/executer_actor/kqp_scheme_executer.cpp

+1-5
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,7 @@ class TKqpSchemeExecuter : public TActorBootstrapped<TKqpSchemeExecuter> {
113113
}
114114

115115
case NKqpProto::TKqpSchemeOperation::kDropTable: {
116-
auto modifyScheme = schemeOp.GetDropTable();
117-
if (Temporary) {
118-
auto* dropTable = modifyScheme.MutableDrop();
119-
dropTable->SetName(dropTable->GetName() + SessionId);
120-
}
116+
const auto& modifyScheme = schemeOp.GetDropTable();
121117
ev->Record.MutableTransaction()->MutableModifyScheme()->CopyFrom(modifyScheme);
122118
break;
123119
}

ydb/core/kqp/query_data/kqp_prepared_query.cpp

+5-6
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ bool TKqpPhyTxHolder::IsLiteralTx() const {
107107
return LiteralTx;
108108
}
109109

110-
std::optional<std::pair<std::pair<TString, TString>, bool>>
110+
std::optional<std::pair<bool, std::pair<TString, TString>>>
111111
TKqpPhyTxHolder::GetSchemeOpTempTablePath() const {
112112
if (GetType() != NKqpProto::TKqpPhyTx::TYPE_SCHEME) {
113113
return std::nullopt;
@@ -131,8 +131,7 @@ TKqpPhyTxHolder::GetSchemeOpTempTablePath() const {
131131
}
132132
if (tableDesc->HasTemporary()) {
133133
if (tableDesc->GetTemporary()) {
134-
return {{{modifyScheme.GetWorkingDir(), tableDesc->GetName()},
135-
true}};
134+
return {{true, {modifyScheme.GetWorkingDir(), tableDesc->GetName()}}};
136135
}
137136
}
138137
break;
@@ -141,8 +140,7 @@ TKqpPhyTxHolder::GetSchemeOpTempTablePath() const {
141140
auto modifyScheme = schemeOperation.GetDropTable();
142141
auto* dropTable = modifyScheme.MutableDrop();
143142

144-
return {{{modifyScheme.GetWorkingDir(), dropTable->GetName()},
145-
false}};
143+
return {{false, {modifyScheme.GetWorkingDir(), dropTable->GetName()}}};
146144
}
147145
default:
148146
return std::nullopt;
@@ -274,6 +272,7 @@ bool TPreparedQueryHolder::HasTempTables(TKqpTempTablesState::TConstPtr tempTabl
274272
if (!tempTablesState) {
275273
return false;
276274
}
275+
YQL_ENSURE(tempTablesState->SessionId);
277276
auto tempTables = THashSet<TString>();
278277
for (const auto& [path, info] : tempTablesState->TempTables) {
279278
tempTables.insert(path.second + *tempTablesState->SessionId);
@@ -289,7 +288,7 @@ bool TPreparedQueryHolder::HasTempTables(TKqpTempTablesState::TConstPtr tempTabl
289288
if (!optPath) {
290289
continue;
291290
} else {
292-
const auto& [path, isCreate] = *optPath;
291+
const auto& [isCreate, path] = *optPath;
293292
if (isCreate) {
294293
return true;
295294
} else {

ydb/core/kqp/query_data/kqp_prepared_query.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ class TKqpPhyTxHolder {
117117

118118
bool IsLiteralTx() const;
119119

120-
std::optional<std::pair<std::pair<TString, TString>, bool>>
120+
std::optional<std::pair<bool, std::pair<TString, TString>>>
121121
GetSchemeOpTempTablePath() const;
122122
};
123123

ydb/core/kqp/session_actor/kqp_session_actor.cpp

+32-31
Original file line numberDiff line numberDiff line change
@@ -1162,39 +1162,34 @@ class TKqpSessionActor : public TActorBootstrapped<TKqpSessionActor> {
11621162
}
11631163
}
11641164

1165-
std::optional<TKqpTempTablesState::TTempTableInfo> GetTemporaryTableInfo(TKqpPhyTxHolder::TConstPtr tx) {
1165+
std::optional<std::pair<bool, TKqpTempTablesState::TTempTableInfo>>
1166+
GetTemporaryTableInfo(TKqpPhyTxHolder::TConstPtr tx) {
11661167
if (!tx) {
11671168
return std::nullopt;
11681169
}
1169-
const auto& schemeOperation = tx->GetSchemeOperation();
1170-
switch (schemeOperation.GetOperationCase()) {
1171-
case NKqpProto::TKqpSchemeOperation::kCreateTable: {
1172-
const auto& modifyScheme = schemeOperation.GetCreateTable();
1173-
const NKikimrSchemeOp::TTableDescription* tableDesc = nullptr;
1174-
switch (modifyScheme.GetOperationType()) {
1175-
case NKikimrSchemeOp::ESchemeOpCreateTable: {
1176-
tableDesc = &modifyScheme.GetCreateTable();
1177-
break;
1178-
}
1179-
case NKikimrSchemeOp::ESchemeOpCreateIndexedTable: {
1180-
tableDesc = &modifyScheme.GetCreateIndexedTable().GetTableDescription();
1181-
break;
1182-
}
1183-
default:
1184-
YQL_ENSURE(false, "Unexpected operation type");
1185-
}
1186-
auto userToken = QueryState ? QueryState->UserToken : TIntrusiveConstPtr<NACLib::TUserToken>();
1187-
if (tableDesc->HasTemporary()) {
1188-
if (tableDesc->GetTemporary()) {
1189-
return {{tableDesc->GetName(), modifyScheme.GetWorkingDir(), Settings.Cluster, userToken, Settings.Database}};
1190-
}
1191-
}
1192-
break;
1193-
}
1194-
default:
1195-
return std::nullopt;
1170+
auto optPath = tx->GetSchemeOpTempTablePath();
1171+
if (!optPath) {
1172+
return std::nullopt;
11961173
}
1197-
return std::nullopt;
1174+
const auto& [isCreate, path] = *optPath;
1175+
if (isCreate) {
1176+
auto userToken = QueryState ? QueryState->UserToken : TIntrusiveConstPtr<NACLib::TUserToken>();
1177+
return {{true, {path.second, path.first, Settings.Cluster, userToken, Settings.Database}}};
1178+
}
1179+
1180+
TString name = path.second;
1181+
auto pos = name.find(*TempTablesState.SessionId);
1182+
1183+
if (pos == TString::npos) {
1184+
return std::nullopt;
1185+
}
1186+
name.erase(pos, name.size());
1187+
1188+
auto it = TempTablesState.TempTables.find(std::make_pair(Settings.Cluster, JoinPath({path.first, name})));
1189+
if (it == TempTablesState.TempTables.end()) {
1190+
return std::nullopt;
1191+
}
1192+
return {{false, it->second}};
11981193
}
11991194

12001195
void UpdateTempTablesState() {
@@ -1205,8 +1200,14 @@ class TKqpSessionActor : public TActorBootstrapped<TKqpSessionActor> {
12051200
if (!tx) {
12061201
return;
12071202
}
1208-
if (auto tempTableInfo = GetTemporaryTableInfo(tx)) {
1209-
TempTablesState.TempTables[std::make_pair(tempTableInfo->Database, JoinPath({tempTableInfo->WorkingDir, tempTableInfo->Name}))] = std::move(*tempTableInfo);
1203+
auto optInfo = GetTemporaryTableInfo(tx);
1204+
if (optInfo) {
1205+
auto [isCreate, tempTableInfo] = *optInfo;
1206+
if (isCreate) {
1207+
TempTablesState.TempTables[std::make_pair(tempTableInfo.Database, JoinPath({tempTableInfo.WorkingDir, tempTableInfo.Name}))] = tempTableInfo;
1208+
} else {
1209+
TempTablesState.TempTables.erase(std::make_pair(tempTableInfo.Database, JoinPath({tempTableInfo.WorkingDir, tempTableInfo.Name})));
1210+
}
12101211
QueryState->UpdateTempTablesState(TempTablesState);
12111212
}
12121213
}

0 commit comments

Comments
 (0)