-
Notifications
You must be signed in to change notification settings - Fork 606
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
Reapply "Support returning list in delete statements (#1684)" (#1705) #1706
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,33 @@ TCoAtomList MakeColumnsList(Container rows, TExprContext& ctx, TPositionHandle p | |
return Build<TCoAtomList>(ctx, pos).Add(columnsVector).Done(); | ||
} | ||
|
||
template<typename Container> | ||
TExprBase SelectFields(TExprBase node, Container fields, TExprContext& ctx, TPositionHandle pos) { | ||
TVector<TExprBase> items; | ||
for (auto&& field : fields) { | ||
TString name; | ||
|
||
if constexpr (std::is_same_v<NYql::NNodes::TCoAtom&&, decltype(field)>) { | ||
name = field.Value(); | ||
} else { | ||
name = field; | ||
} | ||
|
||
auto tuple = Build<TCoNameValueTuple>(ctx, pos) | ||
.Name().Build(field) | ||
.template Value<TCoMember>() | ||
.Struct(node) | ||
.Name().Build(name) | ||
.Build() | ||
.Done(); | ||
|
||
items.emplace_back(tuple); | ||
} | ||
return Build<TCoAsStruct>(ctx, pos) | ||
.Add(items) | ||
.Done(); | ||
} | ||
|
||
TExprBase KqpBuildReturning(TExprBase node, TExprContext& ctx, const TKqpOptimizeContext& kqpCtx) { | ||
auto maybeReturning = node.Maybe<TKqlReturningList>(); | ||
if (!maybeReturning) { | ||
|
@@ -24,46 +51,60 @@ TExprBase KqpBuildReturning(TExprBase node, TExprContext& ctx, const TKqpOptimiz | |
auto returning = maybeReturning.Cast(); | ||
const auto& tableDesc = kqpCtx.Tables->ExistingTable(kqpCtx.Cluster, returning.Table().Path()); | ||
|
||
auto buildFromUpsert = [&](TMaybeNode<TKqlUpsertRows> upsert) -> TExprBase { | ||
auto rows = upsert.Cast().Input(); | ||
auto pos = upsert.Input().Cast().Pos(); | ||
auto buildReturningRows = [&](TExprBase rows, TCoAtomList columns, TCoAtomList returningColumns) -> TExprBase { | ||
auto pos = rows.Pos(); | ||
|
||
TSet<TString> inputColumns; | ||
TSet<TString> columnsToReadSet; | ||
|
||
for (auto&& column : upsert.Columns().Cast()) { | ||
for (auto&& column : columns) { | ||
inputColumns.insert(TString(column.Value())); | ||
} | ||
for (auto&& column : upsert.ReturningColumns().Cast()) { | ||
for (auto&& column : returningColumns) { | ||
if (!inputColumns.contains(column) && !tableDesc.GetKeyColumnIndex(TString(column))) { | ||
columnsToReadSet.insert(TString(column)); | ||
} | ||
} | ||
|
||
TMaybeNode<TExprBase> input = upsert.Input(); | ||
TMaybeNode<TExprBase> input = rows; | ||
|
||
if (!columnsToReadSet.empty()) { | ||
TString upsertInputName = "upsertInput"; | ||
TString tableInputName = "table"; | ||
auto payloadSelectorArg = TCoArgument(ctx.NewArgument(pos, "payload_selector_row")); | ||
TVector<TExprBase> payloadTuples; | ||
for (const auto& column : columns) { | ||
payloadTuples.emplace_back( | ||
Build<TCoNameValueTuple>(ctx, pos) | ||
.Name(column) | ||
.Value<TCoMember>() | ||
.Struct(payloadSelectorArg) | ||
.Name(column) | ||
.Build() | ||
.Done()); | ||
} | ||
|
||
auto payloadSelector = Build<TCoLambda>(ctx, pos) | ||
.Args({payloadSelectorArg}) | ||
.Body<TCoAsStruct>() | ||
.Add(payloadTuples) | ||
.Build() | ||
.Done(); | ||
|
||
auto payloadSelector = MakeRowsPayloadSelector(upsert.Columns().Cast(), tableDesc, pos, ctx); | ||
auto condenseResult = CondenseInputToDictByPk(input.Cast(), tableDesc, payloadSelector, ctx); | ||
if (!condenseResult) { | ||
return node; | ||
} | ||
|
||
auto inputDictAndKeys = PrecomputeDictAndKeys(*condenseResult, pos, ctx); | ||
|
||
TSet<TString> columnsToLookup = columnsToReadSet; | ||
for (auto&& column : tableDesc.Metadata->KeyColumnNames) { | ||
columnsToReadSet.insert(column); | ||
} | ||
|
||
TSet<TString> columnsToLookup = columnsToReadSet; | ||
for (auto&& column : tableDesc.Metadata->KeyColumnNames) { | ||
columnsToReadSet.erase(column); | ||
} | ||
TCoAtomList additionalColumnsToRead = MakeColumnsList(columnsToReadSet, ctx, pos); | ||
|
||
TCoArgument existingRow = Build<TCoArgument>(ctx, node.Pos()) | ||
.Name("existing_row") | ||
.Done(); | ||
auto prepareUpdateStage = Build<TDqStage>(ctx, pos) | ||
.Inputs() | ||
.Add(inputDictAndKeys.KeysPrecompute) | ||
|
@@ -80,27 +121,21 @@ TExprBase KqpBuildReturning(TExprBase node, TExprContext& ctx, const TKqpOptimiz | |
.Columns(MakeColumnsList(columnsToLookup, ctx, pos)) | ||
.Build() | ||
.Lambda() | ||
.Args({"existingRow"}) | ||
.Args({existingRow}) | ||
.Body<TCoJust>() | ||
.Input<TCoFlattenMembers>() | ||
.Add() | ||
.Name().Build("") | ||
.Value<TCoUnwrap>() // Key should always exist in the dict | ||
.Optional<TCoLookup>() | ||
.Collection("dict") | ||
.Lookup<TCoExtractMembers>() | ||
.Input("existingRow") | ||
.Members(MakeColumnsList(tableDesc.Metadata->KeyColumnNames, ctx, pos)) | ||
.Build() | ||
.Lookup(SelectFields(existingRow, tableDesc.Metadata->KeyColumnNames, ctx, pos)) | ||
.Build() | ||
.Build() | ||
.Build() | ||
.Add() | ||
.Name().Build("") | ||
.Value<TCoExtractMembers>() | ||
.Input("existingRow") | ||
.Members(additionalColumnsToRead) | ||
.Build() | ||
.Value(SelectFields(existingRow, additionalColumnsToRead, ctx, pos)) | ||
.Build() | ||
.Build() | ||
.Build() | ||
|
@@ -128,20 +163,27 @@ TExprBase KqpBuildReturning(TExprBase node, TExprContext& ctx, const TKqpOptimiz | |
for (auto item : maybeList.Cast()) { | ||
if (auto upsert = item.Maybe<TKqlUpsertRows>()) { | ||
if (upsert.Cast().Table().Raw() == returning.Table().Raw()) { | ||
return buildFromUpsert(upsert); | ||
return buildReturningRows(upsert.Input().Cast(), upsert.Columns().Cast(), returning.Columns()); | ||
} | ||
} | ||
if (auto del = item.Maybe<TKqlDeleteRows>()) { | ||
if (del.Cast().Table().Raw() == returning.Table().Raw()) { | ||
return buildReturningRows(del.Input().Cast(), MakeColumnsList(tableDesc.Metadata->KeyColumnNames, ctx, node.Pos()), returning.Columns()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (auto upsert = returning.Update().Maybe<TKqlUpsertRows>()) { | ||
return buildFromUpsert(upsert); | ||
return buildReturningRows(upsert.Input().Cast(), upsert.Columns().Cast(), returning.Columns()); | ||
} | ||
if (auto del = returning.Update().Maybe<TKqlDeleteRows>()) { | ||
return buildReturningRows(del.Input().Cast(), MakeColumnsList(tableDesc.Metadata->KeyColumnNames, ctx, node.Pos()), returning.Columns()); | ||
} | ||
|
||
return node; | ||
} | ||
|
||
|
||
TExprBase KqpRewriteReturningUpsert(TExprBase node, TExprContext& ctx, const TKqpOptimizeContext&) { | ||
auto upsert = node.Cast<TKqlUpsertRows>(); | ||
if (upsert.ReturningColumns().Empty()) { | ||
|
@@ -164,4 +206,24 @@ TExprBase KqpRewriteReturningUpsert(TExprBase node, TExprContext& ctx, const TKq | |
.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; | ||
} | ||
|
||
return | ||
Build<TKqlDeleteRows>(ctx, del.Pos()) | ||
.Input<TDqPrecompute>() | ||
.Input(del.Input()) | ||
.Build() | ||
.Table(del.Table()) | ||
.ReturningColumns<TCoAtomList>().Build() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. К сожалению, не очень понял: мы не передаем сюда del.ReturningColumns(), а конструируем пустой лист? |
||
.Done(); | ||
} | ||
|
||
} // namespace NKikimr::NKqp::NOpt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
а можно не добоавлять чайлда, наверное?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ну совершенно точно это нужная штука. Надо будет поправить правила про ExtractMembers логические к примеру