Skip to content

Commit f0d5467

Browse files
committed
Add new unit tests for alter index
1 parent 754ab17 commit f0d5467

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed

ydb/core/kqp/ut/scheme/kqp_scheme_ut.cpp

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2382,6 +2382,116 @@ Y_UNIT_TEST_SUITE(KqpScheme) {
23822382
AlterTableAddIndex(EIndexTypeSql::GlobalAsync);
23832383
}
23842384

2385+
Y_UNIT_TEST(AlterTableAlterIndex) {
2386+
TKikimrRunner kikimr;
2387+
auto db = kikimr.GetTableClient();
2388+
auto session = db.CreateSession().GetValueSync().GetSession();
2389+
CreateSampleTablesWithIndex(session);
2390+
2391+
constexpr int minPartitionsCount = 10;
2392+
{
2393+
auto result = session.ExecuteSchemeQuery(Sprintf(R"(
2394+
ALTER TABLE `/Root/SecondaryKeys` ALTER INDEX Index SET AUTO_PARTITIONING_MIN_PARTITIONS_COUNT %d;
2395+
)", minPartitionsCount
2396+
)
2397+
).ExtractValueSync();
2398+
UNIT_ASSERT_C(result.IsSuccess(), result.GetIssues().ToString());
2399+
}
2400+
{
2401+
auto describe = session.DescribeTable("/Root/SecondaryKeys/Index/indexImplTable").GetValueSync();
2402+
UNIT_ASSERT_C(describe.IsSuccess(), describe.GetIssues().ToString());
2403+
auto indexDesc = describe.GetTableDescription();
2404+
UNIT_ASSERT_VALUES_EQUAL(indexDesc.GetPartitioningSettings().GetMinPartitionsCount(), minPartitionsCount);
2405+
}
2406+
}
2407+
2408+
Y_UNIT_TEST(AlterIndexImplTable) {
2409+
TKikimrRunner kikimr;
2410+
auto db = kikimr.GetTableClient();
2411+
auto session = db.CreateSession().GetValueSync().GetSession();
2412+
CreateSampleTablesWithIndex(session);
2413+
2414+
constexpr int minPartitionsCount = 10;
2415+
{
2416+
auto result = session.ExecuteSchemeQuery(Sprintf(R"(
2417+
ALTER TABLE `/Root/SecondaryKeys/Index/indexImplTable` SET AUTO_PARTITIONING_MIN_PARTITIONS_COUNT %d;
2418+
)", minPartitionsCount
2419+
)
2420+
).ExtractValueSync();
2421+
UNIT_ASSERT_VALUES_EQUAL_C(result.GetStatus(), EStatus::SCHEME_ERROR, result.GetIssues().ToString());
2422+
UNIT_ASSERT_STRING_CONTAINS(result.GetIssues().ToString(),
2423+
"Error: Cannot find table 'db.[/Root/SecondaryKeys/Index/indexImplTable]' because it does not exist or you do not have access permissions."
2424+
);
2425+
}
2426+
}
2427+
2428+
Y_UNIT_TEST(AlterIndexImplTableUsingPublicAPI) {
2429+
TKikimrRunner kikimr;
2430+
auto adminSession = kikimr.GetTableClient().CreateSession().GetValueSync().GetSession();
2431+
CreateSampleTablesWithIndex(adminSession);
2432+
2433+
auto grantPermissions = [&adminSession](const char* permissions, const char* path, const char* user) {
2434+
auto grantQuery = Sprintf(R"(
2435+
GRANT %s ON `%s` TO `%s`;
2436+
)",
2437+
permissions, path, user
2438+
);
2439+
auto result = adminSession.ExecuteSchemeQuery(grantQuery).ExtractValueSync();
2440+
UNIT_ASSERT_C(result.IsSuccess(), result.GetIssues().ToString());
2441+
};
2442+
2443+
// a user which does not have any implicit permissions
2444+
auto userClient = NYdb::NTable::TTableClient(kikimr.GetDriver(), NYdb::NTable::TClientSettings()
2445+
.AuthToken("user@builtin")
2446+
);
2447+
auto userSession = userClient.CreateSession().GetValueSync().GetSession();
2448+
2449+
constexpr int minPartitionsCount = 10;
2450+
auto tableSettings = NYdb::NTable::TAlterTableSettings()
2451+
.BeginAlterPartitioningSettings()
2452+
.SetMinPartitionsCount(minPartitionsCount)
2453+
.EndAlterPartitioningSettings();
2454+
2455+
// try altering indexImplTable without ALTER SCHEMA permission
2456+
{
2457+
auto result = userSession.AlterTable("/Root/SecondaryKeys/Index/indexImplTable", tableSettings).ExtractValueSync();
2458+
UNIT_ASSERT_VALUES_EQUAL_C(result.GetStatus(), EStatus::UNAUTHORIZED, result.GetIssues().ToString());
2459+
UNIT_ASSERT_STRING_CONTAINS(result.GetIssues().ToString(),
2460+
"Error: Access denied for user@builtin to path Root/SecondaryKeys/Index/indexImplTable"
2461+
);
2462+
}
2463+
// grant necessary permission
2464+
{
2465+
grantPermissions("ALTER SCHEMA", "/Root/SecondaryKeys", "user@builtin");
2466+
auto result = userSession.AlterTable("/Root/SecondaryKeys/Index/indexImplTable", tableSettings).ExtractValueSync();
2467+
UNIT_ASSERT_C(result.IsSuccess(), result.GetIssues().ToString());
2468+
}
2469+
// check result
2470+
{
2471+
grantPermissions("DESCRIBE SCHEMA", "/Root/SecondaryKeys", "user@builtin");
2472+
auto describe = userSession.DescribeTable("/Root/SecondaryKeys/Index/indexImplTable").ExtractValueSync();
2473+
UNIT_ASSERT_C(describe.IsSuccess(), describe.GetIssues().ToString());
2474+
auto indexDesc = describe.GetTableDescription();
2475+
UNIT_ASSERT_VALUES_EQUAL(indexDesc.GetPartitioningSettings().GetMinPartitionsCount(), minPartitionsCount);
2476+
}
2477+
2478+
// try altering non-partitioning setting of indexImplTable as non-superuser
2479+
auto forbiddenSettings = NYdb::NTable::TAlterTableSettings().SetCompactionPolicy("default");
2480+
{
2481+
auto result = userSession.AlterTable("/Root/SecondaryKeys/Index/indexImplTable", forbiddenSettings).ExtractValueSync();
2482+
UNIT_ASSERT_VALUES_EQUAL_C(result.GetStatus(), EStatus::SCHEME_ERROR, result.GetIssues().ToString());
2483+
UNIT_ASSERT_STRING_CONTAINS(result.GetIssues().ToString(),
2484+
"Error: Check failed: path: '/Root/SecondaryKeys/Index/indexImplTable', error: path is not a common path"
2485+
);
2486+
}
2487+
// become superuser
2488+
{
2489+
kikimr.GetTestServer().GetRuntime()->GetAppData().AdministrationAllowedSIDs.emplace_back("user@builtin");
2490+
auto result = userSession.AlterTable("/Root/SecondaryKeys/Index/indexImplTable", forbiddenSettings).ExtractValueSync();
2491+
UNIT_ASSERT_C(result.IsSuccess(), result.GetIssues().ToString());
2492+
}
2493+
}
2494+
23852495
Y_UNIT_TEST(AlterTableRenameIndex) {
23862496
TKikimrRunner kikimr;
23872497
auto db = kikimr.GetTableClient();

ydb/library/yql/sql/v1/format/sql_format_ut.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,12 @@ Y_UNIT_TEST_SUITE(CheckSqlFormatter) {
454454
"ALTER TABLE user\n\tRESET (user, user);\n"},
455455
{"alter table user add index user local on (user)",
456456
"ALTER TABLE user\n\tADD INDEX user LOCAL ON (user);\n"},
457+
{"alter table user alter index idx set setting 'foo'",
458+
"ALTER TABLE user\n\tALTER INDEX idx SET setting 'foo';\n"},
459+
{"alter table user alter index idx set (setting = 'foo', another_setting = 'bar')",
460+
"ALTER TABLE user\n\tALTER INDEX idx SET (setting = 'foo', another_setting = 'bar');\n"},
461+
{"alter table user alter index idx reset (setting, another_setting)",
462+
"ALTER TABLE user\n\tALTER INDEX idx RESET (setting, another_setting);\n"},
457463
{"alter table user drop index user",
458464
"ALTER TABLE user\n\tDROP INDEX user;\n"},
459465
{"alter table user rename to user",

ydb/library/yql/sql/v1/sql_ut.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2497,6 +2497,24 @@ Y_UNIT_TEST_SUITE(SqlParsingOnly) {
24972497
"<main>:1:40: Error: local: alternative is not implemented yet: 722:7: local_index\n");
24982498
}
24992499

2500+
Y_UNIT_TEST(AlterTableAlterIndexSetPartitioningIsCorrect) {
2501+
const auto result = SqlToYql("USE plato; ALTER TABLE table ALTER INDEX index SET AUTO_PARTITIONING_MIN_PARTITIONS_COUNT 10");
2502+
UNIT_ASSERT_C(result.IsOk(), result.Issues.ToString());
2503+
}
2504+
2505+
Y_UNIT_TEST(AlterTableAlterIndexSetMultiplePartitioningSettings) {
2506+
const auto result = SqlToYql("USE plato; ALTER TABLE table ALTER INDEX index SET "
2507+
"(AUTO_PARTITIONING_BY_LOAD = ENABLED, AUTO_PARTITIONING_MIN_PARTITIONS_COUNT = 10)"
2508+
);
2509+
UNIT_ASSERT_C(result.IsOk(), result.Issues.ToString());
2510+
}
2511+
2512+
Y_UNIT_TEST(AlterTableAlterIndexResetPartitioningIsNotSupported) {
2513+
ExpectFailWithError("USE plato; ALTER TABLE table ALTER INDEX index RESET (AUTO_PARTITIONING_MIN_PARTITIONS_COUNT)",
2514+
"<main>:1:55: Error: AUTO_PARTITIONING_MIN_PARTITIONS_COUNT reset is not supported\n"
2515+
);
2516+
}
2517+
25002518
Y_UNIT_TEST(OptionalAliases) {
25012519
UNIT_ASSERT(SqlToYql("USE plato; SELECT foo FROM (SELECT key foo FROM Input);").IsOk());
25022520
UNIT_ASSERT(SqlToYql("USE plato; SELECT a.x FROM Input1 a JOIN Input2 b ON a.key = b.key;").IsOk());

0 commit comments

Comments
 (0)