Skip to content

Commit b486489

Browse files
committed
Implement auto transaction
1 parent 8020689 commit b486489

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
lines changed

ydb/core/kqp/session_actor/kqp_query_state.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,4 +306,21 @@ bool TKqpQueryState::HasErrors(const NSchemeCache::TSchemeCacheNavigate& respons
306306
return true;
307307
}
308308

309+
bool TKqpQueryState::HasImpliedAutostartTransactions() const {
310+
if (!HasTxControl()
311+
&& (RequestEv->GetAction() == NKikimrKqp::QUERY_ACTION_EXECUTE
312+
|| RequestEv->GetAction() == NKikimrKqp::QUERY_ACTION_EXECUTE_PREPARED)
313+
&& (RequestEv->GetType() == NKikimrKqp::QUERY_TYPE_SQL_GENERIC_QUERY
314+
|| RequestEv->GetType() == NKikimrKqp::QUERY_TYPE_SQL_GENERIC_SCRIPT
315+
|| RequestEv->GetType() == NKikimrKqp::QUERY_TYPE_SQL_GENERIC_CONCURRENT_QUERY))
316+
{
317+
for (const auto& transactionPtr : PreparedQuery->GetTransactions()) {
318+
if (transactionPtr->GetType() == NKqpProto::TKqpPhyTx::TYPE_GENERIC) { // data transaction
319+
return true;
320+
}
321+
}
322+
}
323+
return false;
324+
}
325+
309326
}

ydb/core/kqp/session_actor/kqp_query_state.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,8 @@ class TKqpQueryState : public TNonCopyable {
362362
return RequestEv->HasTxControl();
363363
}
364364

365+
bool HasImpliedAutostartTransactions() const;
366+
365367
const ::Ydb::Table::TransactionControl& GetTxControl() const {
366368
return RequestEv->GetTxControl();
367369
}

ydb/core/kqp/session_actor/kqp_session_actor.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -636,9 +636,21 @@ class TKqpSessionActor : public TActorBootstrapped<TKqpSessionActor> {
636636
Counters->ReportBeginTransaction(Settings.DbCounters, Transactions.EvictedTx, Transactions.Size(), Transactions.ToBeAbortedSize());
637637
}
638638

639+
static const Ydb::Table::TransactionControl& GetAutoTransactionControl() {
640+
auto create = []() -> Ydb::Table::TransactionControl {
641+
Ydb::Table::TransactionControl control;
642+
control.mutable_begin_tx()->mutable_serializable_read_write();
643+
control.set_commit_tx(true);
644+
return control;
645+
};
646+
static const Ydb::Table::TransactionControl control = create();
647+
return control;
648+
}
649+
639650
bool PrepareQueryTransaction() {
640-
if (QueryState->HasTxControl()) {
641-
const auto& txControl = QueryState->GetTxControl();
651+
bool autoTransaction = false;
652+
if (QueryState->HasTxControl() || (autoTransaction = QueryState->HasImpliedAutostartTransactions())) {
653+
const auto& txControl = autoTransaction ? GetAutoTransactionControl() : QueryState->GetTxControl();
642654

643655
QueryState->Commit = txControl.commit_tx();
644656
switch (txControl.tx_selector_case()) {
@@ -941,7 +953,7 @@ class TKqpSessionActor : public TActorBootstrapped<TKqpSessionActor> {
941953
case NKqpProto::TKqpPhyTx::TYPE_SCHEME:
942954
YQL_ENSURE(tx->StagesSize() == 0);
943955

944-
if (QueryState->TxCtx->EffectiveIsolationLevel != NKikimrKqp::ISOLATION_LEVEL_UNDEFINED) {
956+
if (QueryState->HasTxControl() && QueryState->TxCtx->EffectiveIsolationLevel != NKikimrKqp::ISOLATION_LEVEL_UNDEFINED) {
945957
ReplyQueryError(Ydb::StatusIds::PRECONDITION_FAILED,
946958
"Scheme operations cannot be executed inside transaction");
947959
return true;

ydb/core/kqp/ut/service/kqp_qs_queries_ut.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1534,7 +1534,7 @@ Y_UNIT_TEST_SUITE(KqpQueryService) {
15341534
UPSERT INTO KeyValue (Key, Value) VALUES (3, "Three");
15351535
SELECT * FROM KeyValue;
15361536
)", TTxControl::NoTx()).ExtractValueSync();
1537-
UNIT_ASSERT_VALUES_EQUAL_C(result.GetStatus(), EStatus::PRECONDITION_FAILED, result.GetIssues().ToString());
1537+
UNIT_ASSERT_VALUES_EQUAL_C(result.GetStatus(), EStatus::SUCCESS, result.GetIssues().ToString());
15381538
}
15391539

15401540
Y_UNIT_TEST(Tcl) {

0 commit comments

Comments
 (0)