Skip to content

Commit 70ed290

Browse files
authored
refactoring in unit tests of SHOW CREATE TABLE (#16929)
1 parent a22835a commit 70ed290

File tree

2 files changed

+90
-93
lines changed

2 files changed

+90
-93
lines changed

ydb/core/sys_view/ut_common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ struct TTestEnvSettings {
2323
bool EnableSVP = false;
2424
bool EnableForceFollowers = false;
2525
bool ShowCreateTable = false;
26-
NKikimrProto::TAuthConfig AuthConfig;
26+
NKikimrProto::TAuthConfig AuthConfig = {};
2727
};
2828

2929
class TTestEnv {

ydb/core/sys_view/ut_kqp.cpp

Lines changed: 89 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ void BreakLock(TSession& session, const TString& tableName) {
149149
if (yson == "[]") {
150150
continue;
151151
}
152-
152+
153153
NKqp::CompareYson(R"([
154154
[[55u];["Fifty five"]];
155155
])", yson);
@@ -167,7 +167,7 @@ void BreakLock(TSession& session, const TString& tableName) {
167167
{ // tx1: try to commit
168168
auto result = tx1->Commit().ExtractValueSync();
169169
UNIT_ASSERT_VALUES_EQUAL_C(result.GetStatus(), EStatus::SUCCESS, result.GetIssues().ToString());
170-
}
170+
}
171171
}
172172

173173
void SetupAuthEnvironment(TTestEnv& env) {
@@ -256,16 +256,38 @@ void WaitForStats(TTableClient& client, const TString& tableName, const TString&
256256
break;
257257
Sleep(TDuration::Seconds(5));
258258
}
259-
UNIT_ASSERT_GE(rowCount, 0);
259+
UNIT_ASSERT_GE(rowCount, 0);
260+
}
261+
262+
NQuery::TExecuteQueryResult ExecuteQuery(NQuery::TSession& session, const std::string& query) {
263+
auto result = session.ExecuteQuery(query, NQuery::TTxControl::NoTx()).ExtractValueSync();
264+
UNIT_ASSERT_VALUES_EQUAL_C(result.GetStatus(), EStatus::SUCCESS, result.GetIssues().ToString());
265+
return result;
266+
}
267+
268+
NKikimrSchemeOp::TPathDescription DescribePath(TTestActorRuntime& runtime, TString&& path) {
269+
if (!IsStartWithSlash(path)) {
270+
path = CanonizePath(JoinPath({"/Root", path}));
271+
}
272+
auto sender = runtime.AllocateEdgeActor();
273+
TAutoPtr<IEventHandle> handle;
274+
275+
auto request = MakeHolder<TEvTxUserProxy::TEvNavigate>();
276+
request->Record.MutableDescribePath()->SetPath(path);
277+
request->Record.MutableDescribePath()->MutableOptions()->SetShowPrivateTable(true);
278+
request->Record.MutableDescribePath()->MutableOptions()->SetReturnBoundaries(true);
279+
runtime.Send(new IEventHandle(MakeTxProxyID(), sender, request.Release()));
280+
return runtime.GrabEdgeEventRethrow<NSchemeShard::TEvSchemeShard::TEvDescribeSchemeResult>(handle)->GetRecord().GetPathDescription();
260281
}
261282

262-
class TShowCreateTableChecker {
283+
class TShowCreateChecker {
263284
public:
264285

265-
explicit TShowCreateTableChecker(TTestEnv& env)
286+
explicit TShowCreateChecker(TTestEnv& env)
266287
: Env(env)
288+
, Runtime(*Env.GetServer().GetRuntime())
267289
, QueryClient(NQuery::TQueryClient(Env.GetDriver()))
268-
, TableClient(TTableClient(Env.GetDriver()))
290+
, Session(QueryClient.GetSession().GetValueSync().GetSession())
269291
{
270292
CreateTier("tier1");
271293
CreateTier("tier2");
@@ -279,7 +301,7 @@ class TShowCreateTableChecker {
279301
sessionId = session.GetId();
280302
}
281303

282-
CreateTable(session, query);
304+
ExecuteQuery(session, query);
283305
auto showCreateTableQuery = ShowCreateTable(session, tableName);
284306

285307
if (formatQuery) {
@@ -290,132 +312,106 @@ class TShowCreateTableChecker {
290312

291313
DropTable(session, tableName);
292314

293-
CreateTable(session, showCreateTableQuery);
315+
ExecuteQuery(session, showCreateTableQuery);
294316
auto describeResultNew = DescribeTable(tableName, sessionId);
295317

296318
DropTable(session, tableName);
297319

298-
CompareDescriptions(std::move(describeResultOrig), std::move(describeResultNew), showCreateTableQuery);
320+
CompareDescriptions(describeResultOrig, describeResultNew, showCreateTableQuery);
299321
}
300322

301323
private:
302324

303-
void CreateTable(NYdb::NQuery::TSession& session, const std::string& query) {
304-
auto result = session.ExecuteQuery(query, NQuery::TTxControl::NoTx()).ExtractValueSync();
305-
UNIT_ASSERT_VALUES_EQUAL_C(result.GetStatus(), EStatus::SUCCESS, result.GetIssues().ToString());
306-
}
307-
308-
void CreateTier(const TString& tierName) {
309-
auto session = TableClient.CreateSession().GetValueSync().GetSession();
310-
auto result = session.ExecuteSchemeQuery(R"(
325+
void CreateTier(const std::string& tierName) {
326+
ExecuteQuery(Session, std::format(R"(
311327
UPSERT OBJECT `accessKey` (TYPE SECRET) WITH (value = `secretAccessKey`);
312328
UPSERT OBJECT `secretKey` (TYPE SECRET) WITH (value = `fakeSecret`);
313-
CREATE EXTERNAL DATA SOURCE `)" + tierName + R"(` WITH (
314-
SOURCE_TYPE="ObjectStorage",
315-
LOCATION="http://fake.fake/olap-)" + tierName + R"(",
316-
AUTH_METHOD="AWS",
317-
AWS_ACCESS_KEY_ID_SECRET_NAME="accessKey",
318-
AWS_SECRET_ACCESS_KEY_SECRET_NAME="secretKey",
319-
AWS_REGION="ru-central1"
320-
);
321-
)").GetValueSync();
322-
UNIT_ASSERT_VALUES_EQUAL_C(result.GetStatus(), EStatus::SUCCESS, result.GetIssues().ToString());
329+
CREATE EXTERNAL DATA SOURCE `{}` WITH (
330+
SOURCE_TYPE = "ObjectStorage",
331+
LOCATION = "http://fake.fake/olap-{}",
332+
AUTH_METHOD = "AWS",
333+
AWS_ACCESS_KEY_ID_SECRET_NAME = "accessKey",
334+
AWS_SECRET_ACCESS_KEY_SECRET_NAME = "secretKey",
335+
AWS_REGION = "ru-central1"
336+
);
337+
)", tierName, tierName));
323338
}
324339

325-
Ydb::Table::CreateTableRequest DescribeTable(const std::string& tableName,
326-
std::optional<TString> sessionId = std::nullopt) {
327-
328-
auto describeTable = [this](const TString& path) {
329-
auto& runtime = *(this->Env.GetServer().GetRuntime());
330-
auto sender = runtime.AllocateEdgeActor();
331-
TAutoPtr<IEventHandle> handle;
340+
Ydb::Table::CreateTableRequest DescribeTable(const std::string& tableName, std::optional<TString> sessionId = std::nullopt) {
332341

333-
auto request = MakeHolder<TEvTxUserProxy::TEvNavigate>();
334-
request->Record.MutableDescribePath()->SetPath(path);
335-
request->Record.MutableDescribePath()->MutableOptions()->SetShowPrivateTable(true);
336-
request->Record.MutableDescribePath()->MutableOptions()->SetReturnBoundaries(true);
337-
runtime.Send(new IEventHandle(MakeTxProxyID(), sender, request.Release()));
338-
auto reply = runtime.GrabEdgeEventRethrow<NSchemeShard::TEvSchemeShard::TEvDescribeSchemeResult>(handle);
339-
340-
if (reply->GetRecord().GetPathDescription().HasColumnTableDescription()) {
341-
const auto& tableDescription = reply->GetRecord().GetPathDescription().GetColumnTableDescription();
342+
auto describeTable = [this](TString&& path) {
343+
auto pathDescription = DescribePath(Runtime, std::move(path));
342344

345+
if (pathDescription.HasColumnTableDescription()) {
346+
const auto& tableDescription = pathDescription.GetColumnTableDescription();
343347
return *GetCreateTableRequest(tableDescription);
344348
}
345349

346-
if (!reply->GetRecord().GetPathDescription().HasTable()) {
347-
UNIT_ASSERT_C(false, "Invalid path type");
350+
if (!pathDescription.HasTable()) {
351+
UNIT_FAIL("Invalid path type: " << pathDescription.GetSelf().GetPathType());
348352
}
349353

350-
const auto& tableDescription = reply->GetRecord().GetPathDescription().GetTable();
351-
354+
const auto& tableDescription = pathDescription.GetTable();
352355
return *GetCreateTableRequest(tableDescription);
353356
};
354357

355-
TString tablePath = TString(tableName);
358+
auto tablePath = TString(tableName);
356359
if (!IsStartWithSlash(tablePath)) {
357360
tablePath = CanonizePath(JoinPath({"/Root", tablePath}));
358361
}
359362
if (sessionId.has_value()) {
360363
auto pos = sessionId.value().find("&id=");
361364
tablePath = NKqp::GetTempTablePath("Root", sessionId.value().substr(pos + 4), tablePath);
362365
}
363-
auto tableDesc = describeTable(tablePath);
366+
auto tableDesc = describeTable(std::move(tablePath));
364367

365368
return tableDesc;
366369
}
367370

368-
std::string ShowCreateTable(NYdb::NQuery::TSession& session, const std::string& tableName) {
369-
auto result = session.ExecuteQuery(TStringBuilder() << R"(
370-
SHOW CREATE TABLE `)" << tableName << R"(`;
371-
)", NQuery::TTxControl::NoTx()).ExtractValueSync();
372-
UNIT_ASSERT_VALUES_EQUAL_C(result.GetStatus(), EStatus::SUCCESS, result.GetIssues().ToString());
371+
std::string ShowCreate(NQuery::TSession& session, std::string_view type, const std::string& path) {
372+
const auto result = ExecuteQuery(session, std::format("SHOW CREATE {} `{}`;", type, path));
373373

374374
UNIT_ASSERT_VALUES_EQUAL(result.GetResultSets().size(), 1);
375375
auto resultSet = result.GetResultSet(0);
376376
auto columnsMeta = resultSet.GetColumnsMeta();
377-
UNIT_ASSERT(columnsMeta.size() == 3);
377+
UNIT_ASSERT_VALUES_EQUAL(columnsMeta.size(), 3);
378378

379-
NYdb::TResultSetParser parser(resultSet);
379+
TResultSetParser parser(resultSet);
380380
UNIT_ASSERT(parser.TryNextRow());
381381

382-
TString tablePath = TString(tableName);
383-
384382
TString statement = "";
385383

386-
for (size_t i = 0; i < columnsMeta.size(); i++) {
387-
const auto& column = columnsMeta[i];
384+
for (const auto& column : columnsMeta) {
385+
TValueParser parserValue(parser.GetValue(column.Name));
386+
parserValue.OpenOptional();
387+
const auto& value = parserValue.GetUtf8();
388+
388389
if (column.Name == "Path") {
389-
TValueParser parserValue(parser.GetValue(i));
390-
parserValue.OpenOptional();
391-
UNIT_ASSERT_VALUES_EQUAL(parserValue.GetUtf8(), std::string(tablePath));
392-
continue;
390+
UNIT_ASSERT_VALUES_EQUAL(value, path);
393391
} else if (column.Name == "PathType") {
394-
TValueParser parserValue(parser.GetValue(i));
395-
parserValue.OpenOptional();
396-
UNIT_ASSERT_VALUES_EQUAL(parserValue.GetUtf8(), "Table");
397-
continue;
392+
auto actualType = to_upper(TString(value));
393+
UNIT_ASSERT_VALUES_EQUAL(actualType, type);
398394
} else if (column.Name == "Statement") {
399-
TValueParser parserValue(parser.GetValue(i));
400-
parserValue.OpenOptional();
401-
statement = parserValue.GetUtf8();
395+
statement = value;
402396
} else {
403-
UNIT_ASSERT_C(false, "Invalid column name");
397+
UNIT_FAIL("Invalid column name: " << column.Name);
404398
}
405399
}
406400
UNIT_ASSERT(statement);
407401

408402
return statement;
409403
}
410404

411-
void DropTable(NYdb::NQuery::TSession& session, const std::string& tableName) {
412-
auto result = session.ExecuteQuery(TStringBuilder() << R"(
413-
DROP TABLE `)" << tableName << R"(`;
414-
)", NQuery::TTxControl::NoTx()).ExtractValueSync();
415-
UNIT_ASSERT_VALUES_EQUAL_C(result.GetStatus(), EStatus::SUCCESS, result.GetIssues().ToString());
405+
std::string ShowCreateTable(NQuery::TSession& session, const std::string& tableName) {
406+
return ShowCreate(session, "TABLE", tableName);
407+
}
408+
409+
void DropTable(NQuery::TSession& session, const std::string& tableName) {
410+
ExecuteQuery(session, std::format("DROP TABLE `{}`;", tableName));
416411
}
417412

418-
void CompareDescriptions(Ydb::Table::CreateTableRequest describeResultOrig, Ydb::Table::CreateTableRequest describeResultNew, const std::string& showCreateTableQuery) {
413+
template <typename TProtobufDescription>
414+
void CompareDescriptions(const TProtobufDescription& describeResultOrig, const TProtobufDescription& describeResultNew, const std::string& showCreateTableQuery) {
419415
TString first;
420416
::google::protobuf::TextFormat::PrintToString(describeResultOrig, &first);
421417
TString second;
@@ -470,8 +466,9 @@ class TShowCreateTableChecker {
470466

471467
private:
472468
TTestEnv& Env;
469+
TTestActorRuntime& Runtime;
473470
NQuery::TQueryClient QueryClient;
474-
TTableClient TableClient;
471+
NQuery::TSession Session;
475472
};
476473

477474
class TYsonFieldChecker {
@@ -705,7 +702,7 @@ Y_UNIT_TEST_SUITE(SystemView) {
705702
env.GetServer().GetRuntime()->SetLogPriority(NKikimrServices::KQP_YQL, NActors::NLog::PRI_TRACE);
706703
env.GetServer().GetRuntime()->SetLogPriority(NKikimrServices::SYSTEM_VIEWS, NActors::NLog::PRI_DEBUG);
707704

708-
TShowCreateTableChecker checker(env);
705+
TShowCreateChecker checker(env);
709706

710707
checker.CheckShowCreateTable(
711708
R"(CREATE TABLE test_show_create (
@@ -899,7 +896,7 @@ R"(CREATE TABLE `test_show_create` (
899896
env.GetServer().GetRuntime()->SetLogPriority(NKikimrServices::KQP_YQL, NActors::NLog::PRI_TRACE);
900897
env.GetServer().GetRuntime()->SetLogPriority(NKikimrServices::SYSTEM_VIEWS, NActors::NLog::PRI_DEBUG);
901898

902-
TShowCreateTableChecker checker(env);
899+
TShowCreateChecker checker(env);
903900

904901
checker.CheckShowCreateTable(R"(
905902
CREATE TABLE test_show_create (
@@ -992,7 +989,7 @@ WITH (PARTITION_AT_KEYS = ((FALSE), (FALSE, 1, 2), (TRUE, 1, 1, 1, 1, 'str'), (T
992989
env.GetServer().GetRuntime()->SetLogPriority(NKikimrServices::KQP_YQL, NActors::NLog::PRI_TRACE);
993990
env.GetServer().GetRuntime()->SetLogPriority(NKikimrServices::SYSTEM_VIEWS, NActors::NLog::PRI_DEBUG);
994991

995-
TShowCreateTableChecker checker(env);
992+
TShowCreateChecker checker(env);
996993

997994
checker.CheckShowCreateTable(R"(
998995
CREATE TABLE test_show_create (
@@ -1029,7 +1026,7 @@ WITH (
10291026
env.GetServer().GetRuntime()->SetLogPriority(NKikimrServices::KQP_YQL, NActors::NLog::PRI_TRACE);
10301027
env.GetServer().GetRuntime()->SetLogPriority(NKikimrServices::SYSTEM_VIEWS, NActors::NLog::PRI_DEBUG);
10311028

1032-
TShowCreateTableChecker checker(env);
1029+
TShowCreateChecker checker(env);
10331030

10341031
checker.CheckShowCreateTable(R"(
10351032
CREATE TABLE test_show_create (
@@ -1093,7 +1090,7 @@ WITH (
10931090
env.GetServer().GetRuntime()->SetLogPriority(NKikimrServices::KQP_YQL, NActors::NLog::PRI_TRACE);
10941091
env.GetServer().GetRuntime()->SetLogPriority(NKikimrServices::SYSTEM_VIEWS, NActors::NLog::PRI_DEBUG);
10951092

1096-
TShowCreateTableChecker checker(env);
1093+
TShowCreateChecker checker(env);
10971094

10981095
checker.CheckShowCreateTable(R"(
10991096
CREATE TABLE test_show_create (
@@ -1117,7 +1114,7 @@ WITH (
11171114
env.GetServer().GetRuntime()->SetLogPriority(NKikimrServices::KQP_YQL, NActors::NLog::PRI_TRACE);
11181115
env.GetServer().GetRuntime()->SetLogPriority(NKikimrServices::SYSTEM_VIEWS, NActors::NLog::PRI_DEBUG);
11191116

1120-
TShowCreateTableChecker checker(env);
1117+
TShowCreateChecker checker(env);
11211118

11221119
checker.CheckShowCreateTable(R"(
11231120
CREATE TABLE test_show_create (
@@ -1158,7 +1155,7 @@ WITH (READ_REPLICAS_SETTINGS = 'ANY_AZ:3');
11581155
env.GetServer().GetRuntime()->SetLogPriority(NKikimrServices::KQP_YQL, NActors::NLog::PRI_TRACE);
11591156
env.GetServer().GetRuntime()->SetLogPriority(NKikimrServices::SYSTEM_VIEWS, NActors::NLog::PRI_DEBUG);
11601157

1161-
TShowCreateTableChecker checker(env);
1158+
TShowCreateChecker checker(env);
11621159

11631160
checker.CheckShowCreateTable(R"(
11641161
CREATE TABLE test_show_create (
@@ -1199,7 +1196,7 @@ WITH (KEY_BLOOM_FILTER = DISABLED);
11991196
env.GetServer().GetRuntime()->SetLogPriority(NKikimrServices::KQP_YQL, NActors::NLog::PRI_TRACE);
12001197
env.GetServer().GetRuntime()->SetLogPriority(NKikimrServices::SYSTEM_VIEWS, NActors::NLog::PRI_DEBUG);
12011198

1202-
TShowCreateTableChecker checker(env);
1199+
TShowCreateChecker checker(env);
12031200

12041201
checker.CheckShowCreateTable(R"(
12051202
CREATE TABLE test_show_create (
@@ -1315,7 +1312,7 @@ WITH (
13151312
env.GetServer().GetRuntime()->SetLogPriority(NKikimrServices::KQP_YQL, NActors::NLog::PRI_TRACE);
13161313
env.GetServer().GetRuntime()->SetLogPriority(NKikimrServices::SYSTEM_VIEWS, NActors::NLog::PRI_DEBUG);
13171314

1318-
TShowCreateTableChecker checker(env);
1315+
TShowCreateChecker checker(env);
13191316

13201317
checker.CheckShowCreateTable(R"(
13211318
CREATE TEMPORARY TABLE test_show_create (
@@ -1341,7 +1338,7 @@ R"(CREATE TEMPORARY TABLE `test_show_create` (
13411338
env.GetServer().GetRuntime()->SetLogPriority(NKikimrServices::KQP_YQL, NActors::NLog::PRI_TRACE);
13421339
env.GetServer().GetRuntime()->SetLogPriority(NKikimrServices::SYSTEM_VIEWS, NActors::NLog::PRI_DEBUG);
13431340

1344-
TShowCreateTableChecker checker(env);
1341+
TShowCreateChecker checker(env);
13451342

13461343
checker.CheckShowCreateTable(
13471344
R"(CREATE TABLE `/Root/test_show_create` (
@@ -1868,7 +1865,7 @@ WITH (
18681865

18691866
TTableClient client(env.GetDriver());
18701867
auto session = client.CreateSession().GetValueSync().GetSession();
1871-
1868+
18721869
BreakLock(session, "/Root/Table0");
18731870

18741871
WaitForStats(client, "/Root/.sys/partition_stats", "LocksBroken != 0");
@@ -1888,7 +1885,7 @@ WITH (
18881885
check.Uint64(1); // LocksAcquired
18891886
check.Uint64(0); // LocksWholeShard
18901887
check.Uint64(1); // LocksBroken
1891-
}
1888+
}
18921889

18931890
Y_UNIT_TEST(PartitionStatsFields) {
18941891
NDataShard::gDbStatsReportInterval = TDuration::Seconds(0);
@@ -2717,7 +2714,7 @@ WITH (
27172714

27182715
TTableClient client(env.GetDriver());
27192716
auto session = client.CreateSession().GetValueSync().GetSession();
2720-
2717+
27212718
const TString tableName = "/Root/Tenant1/Table1";
27222719
const TString viewName = "/Root/Tenant1/.sys/top_partitions_by_tli_one_minute";
27232720

0 commit comments

Comments
 (0)