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

Don't omit precomputes in presense of a returning statement #12965

Merged
merged 3 commits into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 22 additions & 0 deletions ydb/core/kqp/opt/kqp_opt_phy_finalize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,33 @@ TStatus KqpBuildPureExprStagesResult(const TExprNode::TPtr& input, TExprNode::TP
break;
}
}

std::function<bool(TExprBase)> hasReturning = [&] (TExprBase node) {
if (auto effect = node.Maybe<TKqlUpsertRows>()) {
return !effect.ReturningColumns().Cast().Empty();
}
if (auto effect = node.Maybe<TKqlDeleteRows>()) {
return !effect.ReturningColumns().Cast().Empty();
}
if (auto effect = node.Maybe<TExprList>()) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

А в каком случае эта ветка работает? У нас кажется не может быть список в этом месте?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Может, это записи в таблицы со вторичными индексами. В returning правилах тоже бранч есть про это

Copy link
Collaborator

Choose a reason for hiding this comment

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

Для меня похоже на dead code, давай потом посмотрим по coverage, мы вообще в этом бранче бываем?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Теста кстати в kqp_returning_ut не нашел с индексами. надо б добавить

for (auto item : effect.Cast()) {
if (hasReturning(item)) {
return true;
}
}
}
return false;
};

for (const auto& effect : query.Effects()) {
if (!hasPrecomputes(effect)) {
omitResultPrecomputes = false;
break;
}
if (hasReturning(effect)) {
omitResultPrecomputes = false;
ssmike marked this conversation as resolved.
Show resolved Hide resolved
break;
}
}

TNodeOnNodeOwnedMap replaces;
Expand Down
14 changes: 4 additions & 10 deletions ydb/core/kqp/opt/physical/effects/kqp_opt_phy_returning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ TExprBase KqpBuildReturning(TExprBase node, TExprContext& ctx, const TKqpOptimiz
auto inputExpr = Build<TCoExtractMembers>(ctx, pos)
.Input(input.Cast())
.Members(returning.Columns())
.Done().Ptr();
.Done();

return TExprBase(ctx.ChangeChild(*returning.Raw(), TKqlReturningList::idx_Update, std::move(inputExpr)));
return TExprBase(ctx.ChangeChild(*returning.Raw(), TKqlReturningList::idx_Update, inputExpr.Ptr()));
};

if (auto maybeList = returning.Update().Maybe<TExprList>()) {
Expand Down Expand Up @@ -200,9 +200,6 @@ TExprBase KqpBuildReturning(TExprBase node, TExprContext& ctx, const TKqpOptimiz

TExprBase KqpRewriteReturningUpsert(TExprBase node, TExprContext& ctx, const TKqpOptimizeContext&) {
auto upsert = node.Cast<TKqlUpsertRows>();
if (upsert.ReturningColumns().Empty()) {
return node;
}

if (upsert.Input().Maybe<TDqPrecompute>() || upsert.Input().Maybe<TDqPhyPrecompute>()) {
return node;
Expand All @@ -216,15 +213,12 @@ TExprBase KqpRewriteReturningUpsert(TExprBase node, TExprContext& ctx, const TKq
.Table(upsert.Table())
.Columns(upsert.Columns())
.Settings(upsert.Settings())
.ReturningColumns<TCoAtomList>().Build()
.ReturningColumns(upsert.ReturningColumns())
.Done();
}

TExprBase KqpRewriteReturningDelete(TExprBase node, TExprContext& ctx, const TKqpOptimizeContext&) {
auto del = node.Cast<TKqlDeleteRows>();
if (del.ReturningColumns().Empty()) {
return node;
}

if (del.Input().Maybe<TDqPrecompute>() || del.Input().Maybe<TDqPhyPrecompute>()) {
return node;
Expand All @@ -236,7 +230,7 @@ TExprBase KqpRewriteReturningDelete(TExprBase node, TExprContext& ctx, const TKq
.Input(del.Input())
.Build()
.Table(del.Table())
.ReturningColumns<TCoAtomList>().Build()
.ReturningColumns(del.ReturningColumns())
.Done();
}

Expand Down
25 changes: 25 additions & 0 deletions ydb/core/kqp/ut/opt/kqp_returning_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,31 @@ Y_UNIT_TEST(ReturningColumnsOrder) {

}

Y_UNIT_TEST(Random) {
auto kikimr = DefaultKikimrRunner();

auto client = kikimr.GetQueryClient();
auto settings = NYdb::NQuery::TExecuteQuerySettings()
.Syntax(NYdb::NQuery::ESyntax::YqlV1)
.ConcurrentResultSets(false);

{
auto result = client.ExecuteQuery("CREATE TABLE example (key Uint64, value String, PRIMARY KEY (key));",
NYdb::NQuery::TTxControl::NoTx(), settings).ExtractValueSync();
UNIT_ASSERT(result.IsSuccess());
}

{
auto result = client.ExecuteQuery(
R"(
UPSERT INTO example (key, value) VALUES (1, CAST(RandomUuid(1) AS String)) RETURNING *;
SELECT * FROM example;
)",
NYdb::NQuery::TTxControl::BeginTx().CommitTx(), settings).ExtractValueSync();
CompareYson(FormatResultSetYson(result.GetResultSet(0)), FormatResultSetYson(result.GetResultSet(1)));
}
}

Y_UNIT_TEST(ReturningTypes) {
auto kikimr = DefaultKikimrRunner();

Expand Down
Loading