Skip to content
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
68 changes: 41 additions & 27 deletions ydb/core/kqp/provider/yql_kikimr_opt_build.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,13 @@ TVector<TKiDataQueryBlock> MakeKiDataQueryBlocks(TExprBase node, const TKiExplor
return queryBlocks;
}

TString GetShowCreateType(const TExprNode& settings) {
if (HasSetting(settings, "showCreateTable")) {
return "showCreateTable";
}
return "";
}

} // namespace

TExprNode::TPtr KiBuildQuery(TExprBase node, TExprContext& ctx, TStringBuf database, TIntrusivePtr<TKikimrTablesData> tablesData,
Expand Down Expand Up @@ -953,23 +960,23 @@ TExprNode::TPtr KiBuildQuery(TExprBase node, TExprContext& ctx, TStringBuf datab
return res;
}

TNodeOnNodeOwnedMap showCreateTableReadReplaces;
VisitExpr(node.Ptr(), [&showCreateTableReadReplaces](const TExprNode::TPtr& input) -> bool {
TNodeOnNodeOwnedMap showCreateReadReplacements;
VisitExpr(node.Ptr(), [&showCreateReadReplacements](const TExprNode::TPtr& input) -> bool {
TExprBase currentNode(input);
if (auto maybeReadTable = currentNode.Maybe<TKiReadTable>()) {
auto readTable = maybeReadTable.Cast();
for (auto setting : readTable.Settings()) {
auto name = setting.Name().Value();
if (name == "showCreateTable") {
showCreateTableReadReplaces[input.Get()] = nullptr;
showCreateReadReplacements[input.Get()] = nullptr;
}
}
}
return true;
});

if (!showCreateTableReadReplaces.empty()) {
for (auto& [input, _] : showCreateTableReadReplaces) {
if (!showCreateReadReplacements.empty()) {
for (auto& [input, _] : showCreateReadReplacements) {
TKiReadTable content(input);

TExprNode::TPtr path = ctx.NewCallable(
Expand All @@ -983,6 +990,9 @@ TExprNode::TPtr KiBuildQuery(TExprBase node, TExprContext& ctx, TStringBuf datab
TKikimrKey key(ctx);
YQL_ENSURE(key.Extract(content.TableKey().Ref()));

auto type = GetShowCreateType(content.Settings().Ref());
YQL_ENSURE(!type.empty());

auto sysViewRewrittenValue = Build<TCoNameValueTuple>(ctx, node.Pos())
.Name()
.Build("sysViewRewritten")
Expand All @@ -991,12 +1001,12 @@ TExprNode::TPtr KiBuildQuery(TExprBase node, TExprContext& ctx, TStringBuf datab
.Build()
.Done();

auto showCreateTableValue = Build<TCoNameValueTuple>(ctx, node.Pos())
auto showCreateTypeValue = Build<TCoNameValueTuple>(ctx, node.Pos())
.Name()
.Build("showCreateTable")
.Build(type)
.Done();

auto showCreateTableRead = Build<TCoRead>(ctx, node.Pos())
auto showCreateRead = Build<TCoRead>(ctx, node.Pos())
.World<TCoWorld>().Build()
.DataSource<TCoDataSource>()
.Category(ctx.NewAtom(node.Pos(), KikimrProviderName))
Expand All @@ -1009,61 +1019,65 @@ TExprNode::TPtr KiBuildQuery(TExprBase node, TExprContext& ctx, TStringBuf datab
.Add(ctx.NewCallable(node.Pos(), "Void", {}))
.Add(ctx.NewList(node.Pos(), {}))
.Add(sysViewRewrittenValue)
.Add(showCreateTableValue)
.Add(showCreateTypeValue)
.Build()
.Done().Ptr();

showCreateTableReadReplaces[input] = showCreateTableRead;
showCreateReadReplacements[input] = showCreateRead;
}
auto res = ctx.ReplaceNodes(std::move(node.Ptr()), showCreateTableReadReplaces);
auto res = ctx.ReplaceNodes(std::move(node.Ptr()), showCreateReadReplacements);

TExprBase resNode(res);

TNodeOnNodeOwnedMap showCreateTableRightReplaces;
VisitExpr(resNode.Ptr(), [&showCreateTableRightReplaces](const TExprNode::TPtr& input) -> bool {
TNodeOnNodeOwnedMap showCreateRightReplacements;
VisitExpr(resNode.Ptr(), [&showCreateRightReplacements](const TExprNode::TPtr& input) -> bool {
TExprBase currentNode(input);
if (auto rightMaybe = currentNode.Maybe<TCoRight>()) {
auto right = rightMaybe.Cast();
if (auto maybeRead = right.Input().Maybe<TCoRead>()) {
auto read = maybeRead.Cast();
bool isSysViewRewritten = false;
bool isShowCreateTable = false;
bool isShowCreate = false;
for (auto arg : read.FreeArgs()) {
if (auto tuple = arg.Maybe<TCoNameValueTuple>()) {
auto name = tuple.Cast().Name().Value();
if (name == "sysViewRewritten") {
isSysViewRewritten = true;
} else if (name == "showCreateTable") {
isShowCreateTable = true;
isShowCreate = true;
}
}
}
if (isShowCreateTable && isSysViewRewritten) {
showCreateTableRightReplaces[input.Get()] = nullptr;
if (isShowCreate && isSysViewRewritten) {
showCreateRightReplacements[input.Get()] = nullptr;
}
}
}
return true;
});

for (auto& [input, _] : showCreateTableRightReplaces) {
for (auto& [input, _] : showCreateRightReplacements) {
TCoRight right(input);
TCoRead read(right.Input().Ptr());

TString tablePath;
TString path;
TString pathType;
for (auto arg : read.FreeArgs()) {
if (auto tuple = arg.Maybe<TCoNameValueTuple>()) {
auto name = tuple.Cast().Name().Value();
if (name == "sysViewRewritten") {
tablePath = tuple.Cast().Value().Cast().Cast<TCoAtom>().StringValue();
path = tuple.Cast().Value().Cast().Cast<TCoAtom>().StringValue();
}
if (name == "showCreateTable") {
pathType = "Table";
}
}
}
YQL_ENSURE(!tablePath.empty(), "Unexpected empty table path for SHOW CREATE TABLE");
YQL_ENSURE(!path.empty(), "Unexpected empty path for SHOW CREATE " << pathType.to_upper());

auto tempTablePath = tablesData->GetTempTablePath(tablePath);
auto tempTablePath = tablesData->GetTempTablePath(path);
if (tempTablePath) {
tablePath = tempTablePath.value();
path = tempTablePath.value();
}

auto showCreateArg = Build<TCoArgument>(ctx, resNode.Pos())
Expand All @@ -1082,7 +1096,7 @@ TExprNode::TPtr KiBuildQuery(TExprBase node, TExprContext& ctx, TStringBuf datab
auto pathCondition = Build<TCoCmpEqual>(ctx, resNode.Pos())
.Left(columnPath)
.Right<TCoString>()
.Literal().Build(tablePath)
.Literal().Build(path)
.Build()
.Done();

Expand All @@ -1095,7 +1109,7 @@ TExprNode::TPtr KiBuildQuery(TExprBase node, TExprContext& ctx, TStringBuf datab
auto pathTypeCondition = Build<TCoCmpEqual>(ctx, resNode.Pos())
.Left(columnPathType)
.Right<TCoString>()
.Literal().Build("Table")
.Literal().Build(pathType)
.Build()
.Done();

Expand All @@ -1121,7 +1135,7 @@ TExprNode::TPtr KiBuildQuery(TExprBase node, TExprContext& ctx, TStringBuf datab
.Lambda(lambda)
.Done().Ptr();

showCreateTableRightReplaces[input] = filterData;
showCreateRightReplacements[input] = filterData;
}

ctx.Step
Expand All @@ -1133,7 +1147,7 @@ TExprNode::TPtr KiBuildQuery(TExprBase node, TExprContext& ctx, TStringBuf datab
.Repeat(TExprStep::LoadTablesMetadata)
.Repeat(TExprStep::RewriteIO);

return ctx.ReplaceNodes(std::move(resNode.Ptr()), showCreateTableRightReplaces);
return ctx.ReplaceNodes(std::move(resNode.Ptr()), showCreateRightReplacements);
}

TKiExploreTxResults txExplore;
Expand Down
6 changes: 3 additions & 3 deletions ydb/core/kqp/ut/service/kqp_qs_queries_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2908,15 +2908,15 @@ Y_UNIT_TEST_SUITE(KqpQueryService) {
CREATE VIEW test_view WITH security_invoker = TRUE AS
SELECT * FROM KeyValue;
)", TTxControl::NoTx()).ExtractValueSync();
UNIT_ASSERT_VALUES_EQUAL_C(result.GetStatus(), EStatus::SUCCESS, result.GetIssues().ToString());
UNIT_ASSERT_C(result.IsSuccess(), result.GetIssues().ToString());
}

{
auto result = session.ExecuteQuery(R"(
SHOW CREATE TABLE test_view;
)", TTxControl::NoTx()).ExtractValueSync();
UNIT_ASSERT_VALUES_EQUAL_C(result.GetStatus(), EStatus::ABORTED, result.GetIssues().ToString());
UNIT_ASSERT_STRING_CONTAINS(result.GetIssues().ToString(), "Invalid path type");
UNIT_ASSERT_VALUES_EQUAL_C(result.GetStatus(), EStatus::BAD_REQUEST, result.GetIssues().ToString());
UNIT_ASSERT_STRING_CONTAINS(result.GetIssues().ToString(), "Expected path type: Table");
}
}

Expand Down
Loading
Loading