Skip to content

Commit ed392b6

Browse files
ExecuteData set transaction mode always (#7982)
Co-authored-by: Ivan Sukhov <[email protected]>
1 parent 56be871 commit ed392b6

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

ydb/core/viewer/viewer_query.h

+4
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,10 @@ class TJsonQuery : public TViewerPipeClient {
305305
request.SetType(NKikimrKqp::QUERY_TYPE_SQL_DML);
306306
request.SetKeepSession(false);
307307
SetTransactionMode(request);
308+
if (!request.txcontrol().has_begin_tx()) {
309+
request.mutable_txcontrol()->mutable_begin_tx()->mutable_serializable_read_write();
310+
request.mutable_txcontrol()->set_commit_tx(true);
311+
}
308312
} else if (Action == "explain" || Action == "explain-ast" || Action == "explain-data") {
309313
request.SetAction(NKikimrKqp::QUERY_ACTION_EXPLAIN);
310314
request.SetType(NKikimrKqp::QUERY_TYPE_SQL_DML);

ydb/core/viewer/viewer_ut.cpp

+86
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include <library/cpp/testing/unittest/registar.h>
1919
#include <library/cpp/testing/unittest/tests_data.h>
20+
#include <ydb/core/kqp/common/kqp.h>
2021
#include <ydb/core/testlib/test_client.h>
2122
#include <ydb/core/testlib/tenant_runtime.h>
2223
#include <ydb/public/lib/deprecated/kicli/kicli.h>
@@ -1593,6 +1594,91 @@ Y_UNIT_TEST_SUITE(Viewer) {
15931594
size_t AuthorizeTicketFails = 0;
15941595
};
15951596

1597+
TString PostQuery(TKeepAliveHttpClient& httpClient, TString query, TString action = "", TString transactionMode = "") {
1598+
TStringStream requestBody;
1599+
requestBody
1600+
<< "{ \"query\": \"" << query << "\","
1601+
<< " \"database\": \"/Root\","
1602+
<< " \"action\": \"" << action << "\","
1603+
<< " \"syntax\": \"yql_v1\","
1604+
<< " \"transaction_mode\": \"" << transactionMode << "\","
1605+
<< " \"stats\": \"none\" }";
1606+
TStringStream responseStream;
1607+
TKeepAliveHttpClient::THeaders headers;
1608+
headers["Content-Type"] = "application/json";
1609+
headers["Authorization"] = "test_ydb_token";
1610+
const TKeepAliveHttpClient::THttpCode statusCode = httpClient.DoPost("/viewer/query?timeout=600000&base64=false&schema=modern", requestBody.Str(), &responseStream, headers);
1611+
const TString response = responseStream.ReadAll();
1612+
UNIT_ASSERT_EQUAL_C(statusCode, HTTP_OK, statusCode << ": " << response);
1613+
return response;
1614+
}
1615+
1616+
Y_UNIT_TEST(ExecuteQueryDoesntExecuteSchemeOperationsInsideTransation) {
1617+
TPortManager tp;
1618+
ui16 port = tp.GetPort(2134);
1619+
ui16 grpcPort = tp.GetPort(2135);
1620+
ui16 monPort = tp.GetPort(8765);
1621+
auto settings = TServerSettings(port);
1622+
settings.InitKikimrRunConfig()
1623+
.SetNodeCount(1)
1624+
.SetUseRealThreads(true)
1625+
.SetDomainName("Root")
1626+
.SetMonitoringPortOffset(monPort, true);
1627+
1628+
TServer server(settings);
1629+
server.EnableGRpc(grpcPort);
1630+
TClient client(settings);
1631+
client.InitRootScheme();
1632+
1633+
TTestActorRuntime& runtime = *server.GetRuntime();
1634+
runtime.SetLogPriority(NKikimrServices::TICKET_PARSER, NLog::PRI_TRACE);
1635+
1636+
TKeepAliveHttpClient httpClient("localhost", monPort);
1637+
1638+
//Scheme operations cannot be executed inside transaction
1639+
TString response = PostQuery(httpClient, "CREATE TABLE `/Root/Test` (Key Uint64, Value String, PRIMARY KEY (Key));", "execute-query", "serializable-read-write");
1640+
{
1641+
NJson::TJsonReaderConfig jsonCfg;
1642+
NJson::TJsonValue json;
1643+
NJson::ReadJsonTree(response, &jsonCfg, &json, /* throwOnError = */ true);
1644+
UNIT_ASSERT_EQUAL_C(json["status"].GetString(), "PRECONDITION_FAILED", response);
1645+
}
1646+
}
1647+
1648+
Y_UNIT_TEST(UseTransactionWhenExecuteDataActionQuery) {
1649+
TPortManager tp;
1650+
ui16 port = tp.GetPort(2134);
1651+
ui16 grpcPort = tp.GetPort(2135);
1652+
ui16 monPort = tp.GetPort(8765);
1653+
auto settings = TServerSettings(port);
1654+
settings.InitKikimrRunConfig()
1655+
.SetNodeCount(1)
1656+
.SetUseRealThreads(true)
1657+
.SetDomainName("Root")
1658+
.SetMonitoringPortOffset(monPort, true);
1659+
1660+
TServer server(settings);
1661+
server.EnableGRpc(grpcPort);
1662+
TClient client(settings);
1663+
client.InitRootScheme();
1664+
1665+
TTestActorRuntime& runtime = *server.GetRuntime();
1666+
runtime.SetLogPriority(NKikimrServices::TICKET_PARSER, NLog::PRI_TRACE);
1667+
1668+
TKeepAliveHttpClient httpClient("localhost", monPort);
1669+
1670+
PostQuery(httpClient, "CREATE TABLE `/Root/Test` (Key Uint64, Value String, PRIMARY KEY (Key));", "execute-query");
1671+
PostQuery(httpClient, "INSERT INTO `/Root/Test` (Key, Value) VALUES (1, 'testvalue');", "execute-query");
1672+
TString response = PostQuery(httpClient, "SELECT * FROM `/Root/Test`;", "execute-data");
1673+
{
1674+
NJson::TJsonReaderConfig jsonCfg;
1675+
NJson::TJsonValue json;
1676+
NJson::ReadJsonTree(response, &jsonCfg, &json, /* throwOnError = */ true);
1677+
auto resultSets = json["result"].GetArray();
1678+
UNIT_ASSERT_EQUAL_C(1, resultSets.size(), response);
1679+
}
1680+
}
1681+
15961682
Y_UNIT_TEST(FloatPointJsonQuery) {
15971683
TPortManager tp;
15981684
ui16 port = tp.GetPort(2134);

0 commit comments

Comments
 (0)