Skip to content
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

add more unit tests with indexes #15593

Merged
merged 1 commit into from
Mar 11, 2025
Merged
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
196 changes: 169 additions & 27 deletions ydb/core/kqp/ut/opt/kqp_returning_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,62 +314,204 @@ Y_UNIT_TEST(ReturningSerial) {
}
}

TString ExecuteReturningQuery(TKikimrRunner& kikimr, bool queryService, TString query) {
if (queryService) {
auto qdb = kikimr.GetQueryClient();
auto qSession = qdb.GetSession().GetValueSync().GetSession();
auto result = qSession.ExecuteQuery(
query, NYdb::NQuery::TTxControl::BeginTx().CommitTx()).ExtractValueSync();
UNIT_ASSERT_VALUES_EQUAL_C(result.GetStatus(), EStatus::SUCCESS, result.GetIssues().ToString());
return FormatResultSetYson(result.GetResultSet(0));
}

Y_UNIT_TEST(ReturningWorksQS) {
auto kikimr = DefaultKikimrRunner();
auto db = kikimr.GetTableClient();
auto session = db.CreateSession().GetValueSync().GetSession();
CreateSampleTablesWithIndex(session, true);

auto qdb = kikimr.GetQueryClient();
auto qSession = qdb.GetSession().GetValueSync().GetSession();
auto result = qSession.ExecuteQuery(R"(
UPSERT INTO `/Root/SecondaryKeys` (Key, Fk, Value) VALUES (1, 1, "Payload1") RETURNING *;
)", NYdb::NQuery::TTxControl::BeginTx().CommitTx()).ExtractValueSync();
auto result = session.ExecuteDataQuery(query, TTxControl::BeginTx().CommitTx()).ExtractValueSync();
UNIT_ASSERT_VALUES_EQUAL_C(result.GetStatus(), EStatus::SUCCESS, result.GetIssues().ToString());
CompareYson(R"([
[[1];[1];["Payload1"]];
])", FormatResultSetYson(result.GetResultSet(0)));
return FormatResultSetYson(result.GetResultSet(0));
}

Y_UNIT_TEST(ReturningWorksIndexedUpsertV2) {
Y_UNIT_TEST_TWIN(ReturningWorks, QueryService) {
auto kikimr = DefaultKikimrRunner();
auto db = kikimr.GetTableClient();
auto session = db.CreateSession().GetValueSync().GetSession();
CreateSampleTablesWithIndex(session, true);
CompareYson(
R"([[[101];[101];["Payload1"]];])",
ExecuteReturningQuery(kikimr, QueryService, R"(
UPSERT INTO `/Root/SecondaryKeys` (Key, Fk, Value) VALUES (101, 101, "Payload1") RETURNING *;
)")
);
CompareYson(
R"(
[[#;#;["Payload8"]];
[[1];[1];["Payload1"]];
[[2];[2];["Payload2"]];
[[5];[5];["Payload5"]];
[#;[7];["Payload7"]];
[[101];[101];["Payload1"]]
])",
ExecuteReturningQuery(kikimr, QueryService, "SELECT * FROM `/Root/SecondaryKeys` ORDER BY Key, Fk;")
);
}

auto qdb = kikimr.GetQueryClient();
auto qSession = qdb.GetSession().GetValueSync().GetSession();
auto result = qSession.ExecuteQuery(R"(
Y_UNIT_TEST_TWIN(ReturningWorksIndexedUpsert, QueryService) {
auto kikimr = DefaultKikimrRunner();
auto db = kikimr.GetTableClient();
auto session = db.CreateSession().GetValueSync().GetSession();
CreateSampleTablesWithIndex(session, true);
CompareYson(R"([
[[110];[110];["Payload5"]];
])", ExecuteReturningQuery(kikimr, QueryService, R"(
$v1 = (SELECT Key + 100 as Key, Fk + 100 as Fk, Value FROM `/Root/SecondaryKeys` WHERE Key IS NOT NULL AND Fk IS NOT NULL);
$v2 = (SELECT Key + 105 as Key, Fk + 105 as Fk, Value FROM `/Root/SecondaryKeys` WHERE Key IS NOT NULL AND Fk IS NOT NULL);
UPSERT INTO `/Root/SecondaryKeys`
SELECT * FROM (SELECT * FROM $v1 UNION ALL SELECT * FROM $v2) WHERE Key > 107 RETURNING *;
)", NYdb::NQuery::TTxControl::BeginTx().CommitTx()).ExtractValueSync();
UNIT_ASSERT_VALUES_EQUAL_C(result.GetStatus(), EStatus::SUCCESS, result.GetIssues().ToString());
)"));
CompareYson(
R"(
[[#;#;["Payload8"]];
[[1];[1];["Payload1"]];
[[2];[2];["Payload2"]];
[[5];[5];["Payload5"]];
[#;[7];["Payload7"]];
[[110];[110];["Payload5"]]
])",
ExecuteReturningQuery(kikimr, QueryService, "SELECT * FROM `/Root/SecondaryKeys` ORDER BY Key, Fk;")
);
}

Y_UNIT_TEST_TWIN(ReturningWorksIndexedDelete, QueryService) {
auto kikimr = DefaultKikimrRunner();
auto db = kikimr.GetTableClient();
auto session = db.CreateSession().GetValueSync().GetSession();
CreateSampleTablesWithIndex(session, true);
CompareYson(R"([
[[110];[110];["Payload5"]];
])", FormatResultSetYson(result.GetResultSet(0)));
[[5];[5];["Payload5"]];
])", ExecuteReturningQuery(kikimr, QueryService, R"(
$v1 = (SELECT Key, Fk, Value FROM `/Root/SecondaryKeys` WHERE Key IS NOT NULL AND Fk IS NOT NULL AND Key >= 1);
$v2 = (SELECT Key, Fk, Value FROM `/Root/SecondaryKeys` WHERE Key IS NOT NULL AND Fk IS NOT NULL AND Key <= 5);
DELETE FROM `/Root/SecondaryKeys` ON
SELECT * FROM (SELECT * FROM $v1 UNION ALL SELECT * FROM $v2) WHERE Key >= 5 RETURNING *;
)"));
CompareYson(
R"(
[[#;#;["Payload8"]];
[[1];[1];["Payload1"]];
[[2];[2];["Payload2"]];
[#;[7];["Payload7"]];
])",
ExecuteReturningQuery(kikimr, QueryService, "SELECT * FROM `/Root/SecondaryKeys` ORDER BY Key, Fk;")
);
}

Y_UNIT_TEST(ReturningWorksIndexedInsertV3) {
Y_UNIT_TEST_TWIN(ReturningWorksIndexedDeleteV2, QueryService) {
auto kikimr = DefaultKikimrRunner();
auto db = kikimr.GetTableClient();
auto session = db.CreateSession().GetValueSync().GetSession();
CreateSampleTablesWithIndex(session, true);
CompareYson(R"([
[[1];[1];["Payload1"]];
])", ExecuteReturningQuery(kikimr, QueryService, R"(
DELETE FROM `/Root/SecondaryKeys` WHERE Key = 1 RETURNING *;
)"));
CompareYson(
R"(
[[#;#;["Payload8"]];
[[2];[2];["Payload2"]];
[[5];[5];["Payload5"]];
[#;[7];["Payload7"]];
])",
ExecuteReturningQuery(kikimr, QueryService, "SELECT * FROM `/Root/SecondaryKeys` ORDER BY Key, Fk;")
);
}

auto qdb = kikimr.GetQueryClient();
auto qSession = qdb.GetSession().GetValueSync().GetSession();
auto result = qSession.ExecuteQuery(R"(

Y_UNIT_TEST_TWIN(ReturningWorksIndexedInsert, QueryService) {
auto kikimr = DefaultKikimrRunner();
auto db = kikimr.GetTableClient();
auto session = db.CreateSession().GetValueSync().GetSession();
CreateSampleTablesWithIndex(session, true);

CompareYson(R"([
[[101];[101];["Payload1"]];
])", ExecuteReturningQuery(kikimr, QueryService, R"(
$v1 = (SELECT Key + 100 as Key, Fk + 100 as Fk, Value FROM `/Root/SecondaryKeys` WHERE Key IS NOT NULL AND Fk IS NOT NULL);
$v2 = (SELECT Key + 205 as Key, Fk + 205 as Fk, Value FROM `/Root/SecondaryKeys` WHERE Key IS NOT NULL AND Fk IS NOT NULL);
INSERT INTO `/Root/SecondaryKeys`
SELECT * FROM (SELECT * FROM $v1 UNION ALL SELECT * FROM $v2 ) WHERE Key < 102 RETURNING *;
)", NYdb::NQuery::TTxControl::BeginTx().CommitTx()).ExtractValueSync();
UNIT_ASSERT_VALUES_EQUAL_C(result.GetStatus(), EStatus::SUCCESS, result.GetIssues().ToString());
)"));

CompareYson(
R"(
[[#;#;["Payload8"]];
[[1];[1];["Payload1"]];
[[2];[2];["Payload2"]];
[[5];[5];["Payload5"]];
[#;[7];["Payload7"]];
[[101];[101];["Payload1"]]
])",
ExecuteReturningQuery(kikimr, QueryService, "SELECT * FROM `/Root/SecondaryKeys` ORDER BY Key, Fk;")
);
}

Y_UNIT_TEST_TWIN(ReturningWorksIndexedReplace, QueryService) {
auto kikimr = DefaultKikimrRunner();
auto db = kikimr.GetTableClient();
auto session = db.CreateSession().GetValueSync().GetSession();
CreateSampleTablesWithIndex(session, true);

CompareYson(R"([
[[101];[101];["Payload1"]];
])", FormatResultSetYson(result.GetResultSet(0)));
])", ExecuteReturningQuery(kikimr, QueryService, R"(
$v1 = (SELECT Key + 100 as Key, Fk + 100 as Fk, Value FROM `/Root/SecondaryKeys` WHERE Key IS NOT NULL AND Fk IS NOT NULL);
$v2 = (SELECT Key + 205 as Key, Fk + 205 as Fk, Value FROM `/Root/SecondaryKeys` WHERE Key IS NOT NULL AND Fk IS NOT NULL);
REPLACE INTO `/Root/SecondaryKeys`
SELECT * FROM (SELECT * FROM $v1 UNION ALL SELECT * FROM $v2 ) WHERE Key < 102 RETURNING *;
)"));

CompareYson(
R"(
[[#;#;["Payload8"]];
[[1];[1];["Payload1"]];
[[2];[2];["Payload2"]];
[[5];[5];["Payload5"]];
[#;[7];["Payload7"]];
[[101];[101];["Payload1"]]
])",
ExecuteReturningQuery(kikimr, QueryService, "SELECT * FROM `/Root/SecondaryKeys` ORDER BY Key, Fk;")
);
}

Y_UNIT_TEST_TWIN(ReturningWorksIndexedOperationsWithDefault, QueryService) {
auto kikimr = DefaultKikimrRunner();
auto db = kikimr.GetTableClient();
auto session = db.CreateSession().GetValueSync().GetSession();
{
auto res = session.ExecuteSchemeQuery(R"(
--!syntax_v1
CREATE TABLE `/Root/SecondaryKeys` (
Key Serial,
Fk Int32,
Value String,
PRIMARY KEY (Key),
INDEX Index GLOBAL ON (Fk)
);
)").GetValueSync();
}

CompareYson(R"([
[1;[1];["Payload"]];
])", ExecuteReturningQuery(kikimr, QueryService, R"(
REPLACE INTO `/Root/SecondaryKeys` (Fk, Value) VALUES (1, "Payload") RETURNING Key, Fk, Value;
)"));

CompareYson(
R"([
[1;[1];["Payload"]];
])",
ExecuteReturningQuery(kikimr, QueryService, "SELECT Key, Fk, Value FROM `/Root/SecondaryKeys` ORDER BY Key, Fk;")
);
}

Y_UNIT_TEST(ReturningColumnsOrder) {
Expand Down
Loading