Skip to content

Commit 4d6d062

Browse files
authored
refactoring before SHOW CREATE VIEW (#16629)
1 parent ced6251 commit 4d6d062

File tree

8 files changed

+247
-190
lines changed

8 files changed

+247
-190
lines changed

ydb/core/kqp/provider/yql_kikimr_opt_build.cpp

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,13 @@ TVector<TKiDataQueryBlock> MakeKiDataQueryBlocks(TExprBase node, const TKiExplor
876876
return queryBlocks;
877877
}
878878

879+
TString GetShowCreateType(const TExprNode& settings) {
880+
if (HasSetting(settings, "showCreateTable")) {
881+
return "showCreateTable";
882+
}
883+
return "";
884+
}
885+
879886
} // namespace
880887

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

956-
TNodeOnNodeOwnedMap showCreateTableReadReplaces;
957-
VisitExpr(node.Ptr(), [&showCreateTableReadReplaces](const TExprNode::TPtr& input) -> bool {
963+
TNodeOnNodeOwnedMap showCreateReadReplacements;
964+
VisitExpr(node.Ptr(), [&showCreateReadReplacements](const TExprNode::TPtr& input) -> bool {
958965
TExprBase currentNode(input);
959966
if (auto maybeReadTable = currentNode.Maybe<TKiReadTable>()) {
960967
auto readTable = maybeReadTable.Cast();
961968
for (auto setting : readTable.Settings()) {
962969
auto name = setting.Name().Value();
963970
if (name == "showCreateTable") {
964-
showCreateTableReadReplaces[input.Get()] = nullptr;
971+
showCreateReadReplacements[input.Get()] = nullptr;
965972
}
966973
}
967974
}
968975
return true;
969976
});
970977

971-
if (!showCreateTableReadReplaces.empty()) {
972-
for (auto& [input, _] : showCreateTableReadReplaces) {
978+
if (!showCreateReadReplacements.empty()) {
979+
for (auto& [input, _] : showCreateReadReplacements) {
973980
TKiReadTable content(input);
974981

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

993+
auto type = GetShowCreateType(content.Settings().Ref());
994+
YQL_ENSURE(!type.empty());
995+
986996
auto sysViewRewrittenValue = Build<TCoNameValueTuple>(ctx, node.Pos())
987997
.Name()
988998
.Build("sysViewRewritten")
@@ -991,12 +1001,12 @@ TExprNode::TPtr KiBuildQuery(TExprBase node, TExprContext& ctx, TStringBuf datab
9911001
.Build()
9921002
.Done();
9931003

994-
auto showCreateTableValue = Build<TCoNameValueTuple>(ctx, node.Pos())
1004+
auto showCreateTypeValue = Build<TCoNameValueTuple>(ctx, node.Pos())
9951005
.Name()
996-
.Build("showCreateTable")
1006+
.Build(type)
9971007
.Done();
9981008

999-
auto showCreateTableRead = Build<TCoRead>(ctx, node.Pos())
1009+
auto showCreateRead = Build<TCoRead>(ctx, node.Pos())
10001010
.World<TCoWorld>().Build()
10011011
.DataSource<TCoDataSource>()
10021012
.Category(ctx.NewAtom(node.Pos(), KikimrProviderName))
@@ -1009,61 +1019,65 @@ TExprNode::TPtr KiBuildQuery(TExprBase node, TExprContext& ctx, TStringBuf datab
10091019
.Add(ctx.NewCallable(node.Pos(), "Void", {}))
10101020
.Add(ctx.NewList(node.Pos(), {}))
10111021
.Add(sysViewRewrittenValue)
1012-
.Add(showCreateTableValue)
1022+
.Add(showCreateTypeValue)
10131023
.Build()
10141024
.Done().Ptr();
10151025

1016-
showCreateTableReadReplaces[input] = showCreateTableRead;
1026+
showCreateReadReplacements[input] = showCreateRead;
10171027
}
1018-
auto res = ctx.ReplaceNodes(std::move(node.Ptr()), showCreateTableReadReplaces);
1028+
auto res = ctx.ReplaceNodes(std::move(node.Ptr()), showCreateReadReplacements);
10191029

10201030
TExprBase resNode(res);
10211031

1022-
TNodeOnNodeOwnedMap showCreateTableRightReplaces;
1023-
VisitExpr(resNode.Ptr(), [&showCreateTableRightReplaces](const TExprNode::TPtr& input) -> bool {
1032+
TNodeOnNodeOwnedMap showCreateRightReplacements;
1033+
VisitExpr(resNode.Ptr(), [&showCreateRightReplacements](const TExprNode::TPtr& input) -> bool {
10241034
TExprBase currentNode(input);
10251035
if (auto rightMaybe = currentNode.Maybe<TCoRight>()) {
10261036
auto right = rightMaybe.Cast();
10271037
if (auto maybeRead = right.Input().Maybe<TCoRead>()) {
10281038
auto read = maybeRead.Cast();
10291039
bool isSysViewRewritten = false;
1030-
bool isShowCreateTable = false;
1040+
bool isShowCreate = false;
10311041
for (auto arg : read.FreeArgs()) {
10321042
if (auto tuple = arg.Maybe<TCoNameValueTuple>()) {
10331043
auto name = tuple.Cast().Name().Value();
10341044
if (name == "sysViewRewritten") {
10351045
isSysViewRewritten = true;
10361046
} else if (name == "showCreateTable") {
1037-
isShowCreateTable = true;
1047+
isShowCreate = true;
10381048
}
10391049
}
10401050
}
1041-
if (isShowCreateTable && isSysViewRewritten) {
1042-
showCreateTableRightReplaces[input.Get()] = nullptr;
1051+
if (isShowCreate && isSysViewRewritten) {
1052+
showCreateRightReplacements[input.Get()] = nullptr;
10431053
}
10441054
}
10451055
}
10461056
return true;
10471057
});
10481058

1049-
for (auto& [input, _] : showCreateTableRightReplaces) {
1059+
for (auto& [input, _] : showCreateRightReplacements) {
10501060
TCoRight right(input);
10511061
TCoRead read(right.Input().Ptr());
10521062

1053-
TString tablePath;
1063+
TString path;
1064+
TString pathType;
10541065
for (auto arg : read.FreeArgs()) {
10551066
if (auto tuple = arg.Maybe<TCoNameValueTuple>()) {
10561067
auto name = tuple.Cast().Name().Value();
10571068
if (name == "sysViewRewritten") {
1058-
tablePath = tuple.Cast().Value().Cast().Cast<TCoAtom>().StringValue();
1069+
path = tuple.Cast().Value().Cast().Cast<TCoAtom>().StringValue();
1070+
}
1071+
if (name == "showCreateTable") {
1072+
pathType = "Table";
10591073
}
10601074
}
10611075
}
1062-
YQL_ENSURE(!tablePath.empty(), "Unexpected empty table path for SHOW CREATE TABLE");
1076+
YQL_ENSURE(!path.empty(), "Unexpected empty path for SHOW CREATE " << pathType.to_upper());
10631077

1064-
auto tempTablePath = tablesData->GetTempTablePath(tablePath);
1078+
auto tempTablePath = tablesData->GetTempTablePath(path);
10651079
if (tempTablePath) {
1066-
tablePath = tempTablePath.value();
1080+
path = tempTablePath.value();
10671081
}
10681082

10691083
auto showCreateArg = Build<TCoArgument>(ctx, resNode.Pos())
@@ -1082,7 +1096,7 @@ TExprNode::TPtr KiBuildQuery(TExprBase node, TExprContext& ctx, TStringBuf datab
10821096
auto pathCondition = Build<TCoCmpEqual>(ctx, resNode.Pos())
10831097
.Left(columnPath)
10841098
.Right<TCoString>()
1085-
.Literal().Build(tablePath)
1099+
.Literal().Build(path)
10861100
.Build()
10871101
.Done();
10881102

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

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

1124-
showCreateTableRightReplaces[input] = filterData;
1138+
showCreateRightReplacements[input] = filterData;
11251139
}
11261140

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

1136-
return ctx.ReplaceNodes(std::move(resNode.Ptr()), showCreateTableRightReplaces);
1150+
return ctx.ReplaceNodes(std::move(resNode.Ptr()), showCreateRightReplacements);
11371151
}
11381152

11391153
TKiExploreTxResults txExplore;

ydb/core/kqp/ut/service/kqp_qs_queries_ut.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2908,15 +2908,15 @@ Y_UNIT_TEST_SUITE(KqpQueryService) {
29082908
CREATE VIEW test_view WITH security_invoker = TRUE AS
29092909
SELECT * FROM KeyValue;
29102910
)", TTxControl::NoTx()).ExtractValueSync();
2911-
UNIT_ASSERT_VALUES_EQUAL_C(result.GetStatus(), EStatus::SUCCESS, result.GetIssues().ToString());
2911+
UNIT_ASSERT_C(result.IsSuccess(), result.GetIssues().ToString());
29122912
}
29132913

29142914
{
29152915
auto result = session.ExecuteQuery(R"(
29162916
SHOW CREATE TABLE test_view;
29172917
)", TTxControl::NoTx()).ExtractValueSync();
2918-
UNIT_ASSERT_VALUES_EQUAL_C(result.GetStatus(), EStatus::ABORTED, result.GetIssues().ToString());
2919-
UNIT_ASSERT_STRING_CONTAINS(result.GetIssues().ToString(), "Invalid path type");
2918+
UNIT_ASSERT_VALUES_EQUAL_C(result.GetStatus(), EStatus::BAD_REQUEST, result.GetIssues().ToString());
2919+
UNIT_ASSERT_STRING_CONTAINS(result.GetIssues().ToString(), "Expected path type: Table");
29202920
}
29212921
}
29222922

0 commit comments

Comments
 (0)