Skip to content

refactoring in unit tests of SHOW CREATE TABLE #16929

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ydb/core/sys_view/ut_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct TTestEnvSettings {
bool EnableSVP = false;
bool EnableForceFollowers = false;
bool ShowCreateTable = false;
NKikimrProto::TAuthConfig AuthConfig;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my VS code was issuing compilation warnings because of this

NKikimrProto::TAuthConfig AuthConfig = {};
};

class TTestEnv {
Expand Down
181 changes: 89 additions & 92 deletions ydb/core/sys_view/ut_kqp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ void BreakLock(TSession& session, const TString& tableName) {
if (yson == "[]") {
continue;
}

NKqp::CompareYson(R"([
[[55u];["Fifty five"]];
])", yson);
Expand All @@ -167,7 +167,7 @@ void BreakLock(TSession& session, const TString& tableName) {
{ // tx1: try to commit
auto result = tx1->Commit().ExtractValueSync();
UNIT_ASSERT_VALUES_EQUAL_C(result.GetStatus(), EStatus::SUCCESS, result.GetIssues().ToString());
}
}
}

void SetupAuthEnvironment(TTestEnv& env) {
Expand Down Expand Up @@ -256,16 +256,38 @@ void WaitForStats(TTableClient& client, const TString& tableName, const TString&
break;
Sleep(TDuration::Seconds(5));
}
UNIT_ASSERT_GE(rowCount, 0);
UNIT_ASSERT_GE(rowCount, 0);
}

NQuery::TExecuteQueryResult ExecuteQuery(NQuery::TSession& session, const std::string& query) {
auto result = session.ExecuteQuery(query, NQuery::TTxControl::NoTx()).ExtractValueSync();
UNIT_ASSERT_VALUES_EQUAL_C(result.GetStatus(), EStatus::SUCCESS, result.GetIssues().ToString());
return result;
}

NKikimrSchemeOp::TPathDescription DescribePath(TTestActorRuntime& runtime, TString&& path) {
if (!IsStartWithSlash(path)) {
path = CanonizePath(JoinPath({"/Root", path}));
}
auto sender = runtime.AllocateEdgeActor();
TAutoPtr<IEventHandle> handle;

auto request = MakeHolder<TEvTxUserProxy::TEvNavigate>();
request->Record.MutableDescribePath()->SetPath(path);
request->Record.MutableDescribePath()->MutableOptions()->SetShowPrivateTable(true);
request->Record.MutableDescribePath()->MutableOptions()->SetReturnBoundaries(true);
runtime.Send(new IEventHandle(MakeTxProxyID(), sender, request.Release()));
return runtime.GrabEdgeEventRethrow<NSchemeShard::TEvSchemeShard::TEvDescribeSchemeResult>(handle)->GetRecord().GetPathDescription();
}

class TShowCreateTableChecker {
class TShowCreateChecker {
public:

explicit TShowCreateTableChecker(TTestEnv& env)
explicit TShowCreateChecker(TTestEnv& env)
: Env(env)
, Runtime(*Env.GetServer().GetRuntime())
, QueryClient(NQuery::TQueryClient(Env.GetDriver()))
, TableClient(TTableClient(Env.GetDriver()))
, Session(QueryClient.GetSession().GetValueSync().GetSession())
{
CreateTier("tier1");
CreateTier("tier2");
Expand All @@ -279,7 +301,7 @@ class TShowCreateTableChecker {
sessionId = session.GetId();
}

CreateTable(session, query);
ExecuteQuery(session, query);
auto showCreateTableQuery = ShowCreateTable(session, tableName);

if (formatQuery) {
Expand All @@ -290,132 +312,106 @@ class TShowCreateTableChecker {

DropTable(session, tableName);

CreateTable(session, showCreateTableQuery);
ExecuteQuery(session, showCreateTableQuery);
auto describeResultNew = DescribeTable(tableName, sessionId);

DropTable(session, tableName);

CompareDescriptions(std::move(describeResultOrig), std::move(describeResultNew), showCreateTableQuery);
CompareDescriptions(describeResultOrig, describeResultNew, showCreateTableQuery);
}

private:

void CreateTable(NYdb::NQuery::TSession& session, const std::string& query) {
auto result = session.ExecuteQuery(query, NQuery::TTxControl::NoTx()).ExtractValueSync();
UNIT_ASSERT_VALUES_EQUAL_C(result.GetStatus(), EStatus::SUCCESS, result.GetIssues().ToString());
}

void CreateTier(const TString& tierName) {
auto session = TableClient.CreateSession().GetValueSync().GetSession();
auto result = session.ExecuteSchemeQuery(R"(
void CreateTier(const std::string& tierName) {
ExecuteQuery(Session, std::format(R"(
UPSERT OBJECT `accessKey` (TYPE SECRET) WITH (value = `secretAccessKey`);
UPSERT OBJECT `secretKey` (TYPE SECRET) WITH (value = `fakeSecret`);
CREATE EXTERNAL DATA SOURCE `)" + tierName + R"(` WITH (
SOURCE_TYPE="ObjectStorage",
LOCATION="http://fake.fake/olap-)" + tierName + R"(",
AUTH_METHOD="AWS",
AWS_ACCESS_KEY_ID_SECRET_NAME="accessKey",
AWS_SECRET_ACCESS_KEY_SECRET_NAME="secretKey",
AWS_REGION="ru-central1"
);
)").GetValueSync();
UNIT_ASSERT_VALUES_EQUAL_C(result.GetStatus(), EStatus::SUCCESS, result.GetIssues().ToString());
CREATE EXTERNAL DATA SOURCE `{}` WITH (
SOURCE_TYPE = "ObjectStorage",
LOCATION = "http://fake.fake/olap-{}",
AUTH_METHOD = "AWS",
AWS_ACCESS_KEY_ID_SECRET_NAME = "accessKey",
AWS_SECRET_ACCESS_KEY_SECRET_NAME = "secretKey",
AWS_REGION = "ru-central1"
);
)", tierName, tierName));
}

Ydb::Table::CreateTableRequest DescribeTable(const std::string& tableName,
std::optional<TString> sessionId = std::nullopt) {

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

auto request = MakeHolder<TEvTxUserProxy::TEvNavigate>();
request->Record.MutableDescribePath()->SetPath(path);
request->Record.MutableDescribePath()->MutableOptions()->SetShowPrivateTable(true);
request->Record.MutableDescribePath()->MutableOptions()->SetReturnBoundaries(true);
runtime.Send(new IEventHandle(MakeTxProxyID(), sender, request.Release()));
auto reply = runtime.GrabEdgeEventRethrow<NSchemeShard::TEvSchemeShard::TEvDescribeSchemeResult>(handle);

if (reply->GetRecord().GetPathDescription().HasColumnTableDescription()) {
const auto& tableDescription = reply->GetRecord().GetPathDescription().GetColumnTableDescription();
auto describeTable = [this](TString&& path) {
auto pathDescription = DescribePath(Runtime, std::move(path));

if (pathDescription.HasColumnTableDescription()) {
const auto& tableDescription = pathDescription.GetColumnTableDescription();
return *GetCreateTableRequest(tableDescription);
}

if (!reply->GetRecord().GetPathDescription().HasTable()) {
UNIT_ASSERT_C(false, "Invalid path type");
if (!pathDescription.HasTable()) {
UNIT_FAIL("Invalid path type: " << pathDescription.GetSelf().GetPathType());
}

const auto& tableDescription = reply->GetRecord().GetPathDescription().GetTable();

const auto& tableDescription = pathDescription.GetTable();
return *GetCreateTableRequest(tableDescription);
};

TString tablePath = TString(tableName);
auto tablePath = TString(tableName);
if (!IsStartWithSlash(tablePath)) {
tablePath = CanonizePath(JoinPath({"/Root", tablePath}));
}
if (sessionId.has_value()) {
auto pos = sessionId.value().find("&id=");
tablePath = NKqp::GetTempTablePath("Root", sessionId.value().substr(pos + 4), tablePath);
}
auto tableDesc = describeTable(tablePath);
auto tableDesc = describeTable(std::move(tablePath));

return tableDesc;
}

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

UNIT_ASSERT_VALUES_EQUAL(result.GetResultSets().size(), 1);
auto resultSet = result.GetResultSet(0);
auto columnsMeta = resultSet.GetColumnsMeta();
UNIT_ASSERT(columnsMeta.size() == 3);
UNIT_ASSERT_VALUES_EQUAL(columnsMeta.size(), 3);

NYdb::TResultSetParser parser(resultSet);
TResultSetParser parser(resultSet);
UNIT_ASSERT(parser.TryNextRow());

TString tablePath = TString(tableName);

TString statement = "";

for (size_t i = 0; i < columnsMeta.size(); i++) {
const auto& column = columnsMeta[i];
for (const auto& column : columnsMeta) {
TValueParser parserValue(parser.GetValue(column.Name));
parserValue.OpenOptional();
const auto& value = parserValue.GetUtf8();

if (column.Name == "Path") {
TValueParser parserValue(parser.GetValue(i));
parserValue.OpenOptional();
UNIT_ASSERT_VALUES_EQUAL(parserValue.GetUtf8(), std::string(tablePath));
continue;
UNIT_ASSERT_VALUES_EQUAL(value, path);
} else if (column.Name == "PathType") {
TValueParser parserValue(parser.GetValue(i));
parserValue.OpenOptional();
UNIT_ASSERT_VALUES_EQUAL(parserValue.GetUtf8(), "Table");
continue;
auto actualType = to_upper(TString(value));
UNIT_ASSERT_VALUES_EQUAL(actualType, type);
} else if (column.Name == "Statement") {
TValueParser parserValue(parser.GetValue(i));
parserValue.OpenOptional();
statement = parserValue.GetUtf8();
statement = value;
} else {
UNIT_ASSERT_C(false, "Invalid column name");
UNIT_FAIL("Invalid column name: " << column.Name);
}
}
UNIT_ASSERT(statement);

return statement;
}

void DropTable(NYdb::NQuery::TSession& session, const std::string& tableName) {
auto result = session.ExecuteQuery(TStringBuilder() << R"(
DROP TABLE `)" << tableName << R"(`;
)", NQuery::TTxControl::NoTx()).ExtractValueSync();
UNIT_ASSERT_VALUES_EQUAL_C(result.GetStatus(), EStatus::SUCCESS, result.GetIssues().ToString());
std::string ShowCreateTable(NQuery::TSession& session, const std::string& tableName) {
return ShowCreate(session, "TABLE", tableName);
}

void DropTable(NQuery::TSession& session, const std::string& tableName) {
ExecuteQuery(session, std::format("DROP TABLE `{}`;", tableName));
}

void CompareDescriptions(Ydb::Table::CreateTableRequest describeResultOrig, Ydb::Table::CreateTableRequest describeResultNew, const std::string& showCreateTableQuery) {
template <typename TProtobufDescription>
void CompareDescriptions(const TProtobufDescription& describeResultOrig, const TProtobufDescription& describeResultNew, const std::string& showCreateTableQuery) {
TString first;
::google::protobuf::TextFormat::PrintToString(describeResultOrig, &first);
TString second;
Expand Down Expand Up @@ -470,8 +466,9 @@ class TShowCreateTableChecker {

private:
TTestEnv& Env;
TTestActorRuntime& Runtime;
NQuery::TQueryClient QueryClient;
TTableClient TableClient;
NQuery::TSession Session;
};

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

TShowCreateTableChecker checker(env);
TShowCreateChecker checker(env);

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

TShowCreateTableChecker checker(env);
TShowCreateChecker checker(env);

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

TShowCreateTableChecker checker(env);
TShowCreateChecker checker(env);

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

TShowCreateTableChecker checker(env);
TShowCreateChecker checker(env);

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

TShowCreateTableChecker checker(env);
TShowCreateChecker checker(env);

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

TShowCreateTableChecker checker(env);
TShowCreateChecker checker(env);

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

TShowCreateTableChecker checker(env);
TShowCreateChecker checker(env);

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

TShowCreateTableChecker checker(env);
TShowCreateChecker checker(env);

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

TShowCreateTableChecker checker(env);
TShowCreateChecker checker(env);

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

TShowCreateTableChecker checker(env);
TShowCreateChecker checker(env);

checker.CheckShowCreateTable(
R"(CREATE TABLE `/Root/test_show_create` (
Expand Down Expand Up @@ -1868,7 +1865,7 @@ WITH (

TTableClient client(env.GetDriver());
auto session = client.CreateSession().GetValueSync().GetSession();

BreakLock(session, "/Root/Table0");

WaitForStats(client, "/Root/.sys/partition_stats", "LocksBroken != 0");
Expand All @@ -1888,7 +1885,7 @@ WITH (
check.Uint64(1); // LocksAcquired
check.Uint64(0); // LocksWholeShard
check.Uint64(1); // LocksBroken
}
}

Y_UNIT_TEST(PartitionStatsFields) {
NDataShard::gDbStatsReportInterval = TDuration::Seconds(0);
Expand Down Expand Up @@ -2717,7 +2714,7 @@ WITH (

TTableClient client(env.GetDriver());
auto session = client.CreateSession().GetValueSync().GetSession();

const TString tableName = "/Root/Tenant1/Table1";
const TString viewName = "/Root/Tenant1/.sys/top_partitions_by_tli_one_minute";

Expand Down
Loading