Skip to content

Commit 110f67c

Browse files
authored
Merge c2ff102 into ce94a36
2 parents ce94a36 + c2ff102 commit 110f67c

21 files changed

+2266
-33
lines changed

ydb/core/kqp/provider/yql_kikimr_datasink.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1038,7 +1038,7 @@ class TKikimrDataSink : public TDataProviderBase
10381038
} else {
10391039
YQL_ENSURE(false, "Invalid key type for sequence");
10401040
}
1041-
1041+
10421042

10431043
return Build<TKiAlterSequence>(ctx, node->Pos())
10441044
.World(node->Child(0))

ydb/core/kqp/provider/yql_kikimr_opt_build.cpp

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,176 @@ TExprNode::TPtr KiBuildQuery(TExprBase node, TExprContext& ctx, TStringBuf datab
942942
return res;
943943
}
944944

945+
TNodeOnNodeOwnedMap showCreateTableReadReplaces;
946+
VisitExpr(node.Ptr(), [&showCreateTableReadReplaces](const TExprNode::TPtr& input) -> bool {
947+
TExprBase currentNode(input);
948+
if (auto maybeReadTable = currentNode.Maybe<TKiReadTable>()) {
949+
auto readTable = maybeReadTable.Cast();
950+
for (auto setting : readTable.Settings()) {
951+
auto name = setting.Name().Value();
952+
if (name == "showCreateTable") {
953+
showCreateTableReadReplaces[input.Get()] = nullptr;
954+
}
955+
}
956+
}
957+
return true;
958+
});
959+
960+
if (!showCreateTableReadReplaces.empty()) {
961+
for (auto& [input, _] : showCreateTableReadReplaces) {
962+
TKiReadTable content(input);
963+
964+
TExprNode::TPtr path = ctx.NewCallable(
965+
node.Pos(),
966+
"String",
967+
{ ctx.NewAtom(node.Pos(), NKikimr::CanonizePath(NKikimr::JoinPath({TString(database), ".sys/show_create"}))) }
968+
);
969+
auto table = ctx.NewList(node.Pos(), {ctx.NewAtom(node.Pos(), "table"), path});
970+
auto newKey = ctx.NewCallable(node.Pos(), "Key", {table});
971+
972+
TKikimrKey key(ctx);
973+
YQL_ENSURE(key.Extract(content.TableKey().Ref()));
974+
975+
auto showCreateValue = Build<TCoNameValueTuple>(ctx, node.Pos())
976+
.Name()
977+
.Build("showCreateTable")
978+
.Value<TCoAtom>()
979+
.Value(key.GetTablePath())
980+
.Build()
981+
.Done();
982+
983+
auto showCreateTableRead = Build<TCoRead>(ctx, node.Pos())
984+
.World<TCoWorld>().Build()
985+
.DataSource<TCoDataSource>()
986+
.Category(ctx.NewAtom(node.Pos(), KikimrProviderName))
987+
.FreeArgs()
988+
.Add(ctx.NewAtom(node.Pos(), "db"))
989+
.Build()
990+
.Build()
991+
.FreeArgs()
992+
.Add(newKey)
993+
.Add(ctx.NewCallable(node.Pos(), "Void", {}))
994+
.Add(ctx.NewList(node.Pos(), {}))
995+
.Add(showCreateValue)
996+
.Build()
997+
.Done().Ptr();
998+
999+
showCreateTableReadReplaces[input] = showCreateTableRead;
1000+
}
1001+
auto res = ctx.ReplaceNodes(std::move(node.Ptr()), showCreateTableReadReplaces);
1002+
1003+
TExprBase resNode(res);
1004+
1005+
TNodeOnNodeOwnedMap showCreateTableRightReplaces;
1006+
VisitExpr(resNode.Ptr(), [&showCreateTableRightReplaces](const TExprNode::TPtr& input) -> bool {
1007+
TExprBase currentNode(input);
1008+
if (auto rightMaybe = currentNode.Maybe<TCoRight>()) {
1009+
auto right = rightMaybe.Cast();
1010+
if (auto maybeRead = right.Input().Maybe<TCoRead>()) {
1011+
auto read = maybeRead.Cast();
1012+
for (auto arg : read.FreeArgs()) {
1013+
if (auto tuple = arg.Maybe<TCoNameValueTuple>()) {
1014+
auto name = tuple.Cast().Name().Value();
1015+
if (name == "showCreateTable") {
1016+
showCreateTableRightReplaces[input.Get()] = nullptr;
1017+
}
1018+
}
1019+
}
1020+
}
1021+
}
1022+
return true;
1023+
});
1024+
1025+
for (auto& [input, _] : showCreateTableRightReplaces) {
1026+
TCoRight right(input);
1027+
TCoRead read(right.Input().Ptr());
1028+
1029+
TString tablePath;
1030+
for (auto arg : read.FreeArgs()) {
1031+
if (auto tuple = arg.Maybe<TCoNameValueTuple>()) {
1032+
auto name = tuple.Cast().Name().Value();
1033+
if (name == "showCreateTable") {
1034+
tablePath = tuple.Cast().Value().Cast().Cast<TCoAtom>().StringValue();
1035+
}
1036+
}
1037+
}
1038+
YQL_ENSURE(!tablePath.empty(), "Unexpected empty table path for SHOW CREATE TABLE");
1039+
1040+
auto tempTablePath = tablesData->GetTempTablePath(tablePath);
1041+
if (tempTablePath) {
1042+
tablePath = tempTablePath.value();
1043+
}
1044+
1045+
auto showCreateArg = Build<TCoArgument>(ctx, resNode.Pos())
1046+
.Name("_show_create_arg")
1047+
.Done();
1048+
1049+
TCoAtom columnPathAtom(ctx.NewAtom(resNode.Pos(), "Path"));
1050+
auto columnPathArg = Build<TCoArgument>(ctx, resNode.Pos())
1051+
.Name("_column_path_arg")
1052+
.Done();
1053+
auto columnPath = Build<TCoMember>(ctx, resNode.Pos())
1054+
.Struct(showCreateArg)
1055+
.Name(columnPathAtom)
1056+
.Done().Ptr();
1057+
1058+
auto pathCondition = Build<TCoCmpEqual>(ctx, resNode.Pos())
1059+
.Left(columnPath)
1060+
.Right<TCoString>()
1061+
.Literal().Build(tablePath)
1062+
.Build()
1063+
.Done();
1064+
1065+
TCoAtom columnPathTypeAtom(ctx.NewAtom(resNode.Pos(), "PathType"));
1066+
auto columnPathType = Build<TCoMember>(ctx, resNode.Pos())
1067+
.Struct(showCreateArg)
1068+
.Name(columnPathTypeAtom)
1069+
.Done().Ptr();
1070+
1071+
auto pathTypeCondition = Build<TCoCmpEqual>(ctx, resNode.Pos())
1072+
.Left(columnPathType)
1073+
.Right<TCoString>()
1074+
.Literal().Build("Table")
1075+
.Build()
1076+
.Done();
1077+
1078+
auto lambda = Build<TCoLambda>(ctx, resNode.Pos())
1079+
.Args({showCreateArg})
1080+
.Body<TCoCoalesce>()
1081+
.Predicate<TCoAnd>()
1082+
.Add(pathCondition)
1083+
.Add(pathTypeCondition)
1084+
.Build()
1085+
.Value<TCoBool>()
1086+
.Literal().Build("false")
1087+
.Build()
1088+
.Build()
1089+
.Done().Ptr();
1090+
1091+
auto readData = Build<TCoRight>(ctx, resNode.Pos())
1092+
.Input(right.Input().Ptr())
1093+
.Done().Ptr();
1094+
1095+
auto filterData = Build<TCoFilter>(ctx, resNode.Pos())
1096+
.Input(readData)
1097+
.Lambda(lambda)
1098+
.Done().Ptr();
1099+
1100+
showCreateTableRightReplaces[input] = filterData;
1101+
}
1102+
1103+
ctx.Step
1104+
.Repeat(TExprStep::RewriteIO)
1105+
.Repeat(TExprStep::ExprEval)
1106+
.Repeat(TExprStep::DiscoveryIO)
1107+
.Repeat(TExprStep::Epochs)
1108+
.Repeat(TExprStep::Intents)
1109+
.Repeat(TExprStep::LoadTablesMetadata)
1110+
.Repeat(TExprStep::RewriteIO);
1111+
1112+
return ctx.ReplaceNodes(std::move(resNode.Ptr()), showCreateTableRightReplaces);;
1113+
}
1114+
9451115
TKiExploreTxResults txExplore;
9461116
txExplore.ConcurrentResults = concurrentResults;
9471117
if (!ExploreTx(commit.World(), ctx, kiDataSink, txExplore, tablesData, types) || txExplore.HasErrors) {

ydb/core/kqp/provider/yql_kikimr_provider.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,19 @@ const TKikimrTableDescription& TKikimrTablesData::ExistingTable(const TStringBuf
233233
return *desc;
234234
}
235235

236+
std::optional<TString> TKikimrTablesData::GetTempTablePath(const TStringBuf& table) const {
237+
if (!TempTablesState) {
238+
return std::nullopt;
239+
}
240+
241+
auto tempTableInfoIt = TempTablesState->FindInfo(table, false);
242+
243+
if (tempTableInfoIt != TempTablesState->TempTables.end()) {
244+
return NKikimr::NKqp::GetTempTablePath(TempTablesState->Database, TempTablesState->SessionId, tempTableInfoIt->first);
245+
}
246+
return std::nullopt;
247+
}
248+
236249
bool TKikimrTableDescription::Load(TExprContext& ctx, bool withSystemColumns) {
237250
ColumnTypes.clear();
238251

ydb/core/kqp/provider/yql_kikimr_provider.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ class TKikimrTablesData : public TThrRefBase {
203203
return Tables;
204204
}
205205

206+
std::optional<TString> GetTempTablePath(const TStringBuf& table) const;
207+
206208
void Reset() {
207209
Tables.clear();
208210
}

ydb/core/kqp/provider/yql_kikimr_provider_impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ void TableDescriptionToTableInfo(const TKikimrTableDescription& desc, TYdbOperat
298298

299299
Ydb::Table::VectorIndexSettings_Metric VectorIndexSettingsParseDistance(std::string_view distance);
300300
Ydb::Table::VectorIndexSettings_Metric VectorIndexSettingsParseSimilarity(std::string_view similarity);
301-
Ydb::Table::VectorIndexSettings_VectorType VectorIndexSettingsParseVectorType(std::string_view vectorType);
301+
Ydb::Table::VectorIndexSettings_VectorType VectorIndexSettingsParseVectorType(std::string_view vectorType);
302302

303303
bool IsPgNullExprNode(const NNodes::TExprBase& maybeLiteral);
304304
std::optional<TString> FillLiteralProto(NNodes::TExprBase maybeLiteral, const TTypeAnnotationNode* valueType, Ydb::TypedValue& proto);

ydb/core/kqp/ut/common/kqp_ut_common.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,7 +1243,7 @@ std::vector<NJson::TJsonValue> FindPlanNodes(const NJson::TJsonValue& plan, cons
12431243

12441244
std::vector<NJson::TJsonValue> FindPlanStages(const NJson::TJsonValue& plan) {
12451245
std::vector<NJson::TJsonValue> stages;
1246-
FindPlanStagesImpl(plan.GetMapSafe().at("Plan"), stages);
1246+
FindPlanStagesImpl(plan.GetMapSafe().at("Plan"), stages);
12471247
return stages;
12481248
}
12491249

@@ -1485,7 +1485,7 @@ NJson::TJsonValue SimplifyPlan(NJson::TJsonValue& opt, const TGetPlanParams& par
14851485
if (auto ops = opt.GetMapSafe().find("Operators"); ops != opt.GetMapSafe().end()) {
14861486
auto opName = ops->second.GetArraySafe()[0].GetMapSafe().at("Name").GetStringSafe();
14871487
if (
1488-
opName.find("Join") != TString::npos ||
1488+
opName.find("Join") != TString::npos ||
14891489
opName.find("Union") != TString::npos ||
14901490
(opName.find("Filter") != TString::npos && params.IncludeFilters) ||
14911491
(opName.find("HashShuffle") != TString::npos && params.IncludeShuffles)
@@ -1541,7 +1541,7 @@ bool JoinOrderAndAlgosMatch(const TString& optimized, const TString& reference){
15411541
NJson::TJsonValue optRoot;
15421542
NJson::ReadJsonTree(optimized, &optRoot, true);
15431543
optRoot = SimplifyPlan(optRoot.GetMapSafe().at("SimplifiedPlan"), {});
1544-
1544+
15451545
NJson::TJsonValue refRoot;
15461546
NJson::ReadJsonTree(reference, &refRoot, true);
15471547

@@ -1567,7 +1567,7 @@ NJson::TJsonValue GetDetailedJoinOrderImpl(const NJson::TJsonValue& opt, const T
15671567
res["table"] = op.GetMapSafe().at("Table").GetStringSafe();
15681568
return res;
15691569
}
1570-
1570+
15711571
auto subplans = opt.GetMapSafe().at("Plans").GetArraySafe();
15721572
for (size_t i = 0; i < subplans.size(); ++i) {
15731573
res["args"].AppendValue(GetDetailedJoinOrderImpl(subplans[i], params));

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2761,6 +2761,38 @@ Y_UNIT_TEST_SUITE(KqpQueryService) {
27612761
checkUpsert(true, 2);
27622762
}
27632763

2764+
Y_UNIT_TEST(ShowCreateTable) {
2765+
auto serverSettings = TKikimrSettings();
2766+
2767+
TKikimrRunner kikimr(serverSettings);
2768+
auto db = kikimr.GetQueryClient();
2769+
auto session = db.GetSession().GetValueSync().GetSession();
2770+
2771+
{
2772+
auto result = session.ExecuteQuery(R"(
2773+
CREATE TABLE test_show_create (
2774+
Key Uint32,
2775+
Value Uint32,
2776+
PRIMARY KEY (Key)
2777+
);
2778+
)", TTxControl::NoTx()).ExtractValueSync();
2779+
UNIT_ASSERT_VALUES_EQUAL_C(result.GetStatus(), EStatus::SUCCESS, result.GetIssues().ToString());
2780+
}
2781+
2782+
{
2783+
auto result = session.ExecuteQuery(R"(
2784+
SHOW CREATE TABLE `/Root/test_show_create`;
2785+
)", TTxControl::NoTx()).ExtractValueSync();
2786+
UNIT_ASSERT_VALUES_EQUAL_C(result.GetStatus(), EStatus::SUCCESS, result.GetIssues().ToString());
2787+
2788+
UNIT_ASSERT(!result.GetResultSets().empty());
2789+
2790+
CompareYson(R"([
2791+
[["test_show_create"];["Table"];["CREATE TABLE `test_show_create` (\n\t`Key` Uint32,\n\t`Value` Uint32,\n\tFAMILY default (COMPRESSION = \"off\"),\n\tPRIMARY KEY (`Key`)\n) WITH (\n\tAUTO_PARTITIONING_BY_SIZE = DISABLED,\n\tAUTO_PARTITIONING_BY_LOAD = DISABLED,\n\tAUTO_PARTITIONING_MIN_PARTITIONS_COUNT = 1\n);"]];
2792+
])", FormatResultSetYson(result.GetResultSet(0)));
2793+
}
2794+
}
2795+
27642796
Y_UNIT_TEST(DdlCache) {
27652797
NKikimrConfig::TAppConfig appConfig;
27662798
auto setting = NKikimrKqp::TKqpSetting();

ydb/core/protos/kqp_physical.proto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ message TKqpPhyColumnId {
6262

6363
message TKqpPhyColumnTypeParam {
6464
oneof TypeParam {
65-
string PgTypeName = 1;
65+
string PgTypeName = 1;
6666
Ydb.DecimalType Decimal = 2;
6767
}
6868
}

ydb/core/sys_view/common/schema.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,8 @@ class TSystemViewResolver : public ISystemViewResolver {
298298
RegisterSystemView<Schema::AuthPermissions>(PermissionsName);
299299
RegisterSystemView<Schema::AuthPermissions>(EffectivePermissionsName);
300300
}
301+
302+
RegisterSystemView<Schema::ShowCreate>(ShowCreateName);
301303
}
302304

303305
private:

ydb/core/sys_view/common/schema.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ constexpr TStringBuf PgClassName = "pg_class";
5151

5252
constexpr TStringBuf ResourcePoolClassifiersName = "resource_pool_classifiers";
5353

54+
constexpr TStringBuf ShowCreateName = "show_create";
55+
5456
namespace NAuth {
5557
constexpr TStringBuf UsersName = "auth_users";
5658
constexpr TStringBuf GroupsName = "auth_groups";
@@ -717,6 +719,19 @@ struct Schema : NIceDb::Schema {
717719
Rank,
718720
Config>;
719721
};
722+
723+
struct ShowCreate: Table<21> {
724+
struct Path: Column<1, NScheme::NTypeIds::Utf8> {};
725+
struct Statement: Column<2, NScheme::NTypeIds::Utf8> {};
726+
struct PathType: Column<3, NScheme::NTypeIds::Utf8> {};
727+
728+
using TKey = TableKey<Path, PathType>;
729+
using TColumns = TableColumns<
730+
Path,
731+
Statement,
732+
PathType
733+
>;
734+
};
720735
};
721736

722737
bool MaybeSystemViewPath(const TVector<TString>& path);

ydb/core/sys_view/pg_tables/pg_tables.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ void TPgTablesScanBase::Handle(NSchemeShard::TEvSchemeShard::TEvDescribeSchemeRe
102102
switch (status) {
103103
case NKikimrScheme::StatusSuccess: {
104104
const auto& pathDescription = record.GetPathDescription();
105-
Y_ENSURE(pathDescription.GetSelf().GetPathType() == NKikimrSchemeOp::EPathTypeDir
105+
Y_ENSURE(pathDescription.GetSelf().GetPathType() == NKikimrSchemeOp::EPathTypeDir
106106
|| pathDescription.GetSelf().GetPathType() == NKikimrSchemeOp::EPathTypeSubDomain);
107107
break;
108108
}

ydb/core/sys_view/scan.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <ydb/core/sys_view/query_stats/query_stats.h>
1717
#include <ydb/core/sys_view/resource_pool_classifiers/resource_pool_classifiers.h>
1818
#include <ydb/core/sys_view/sessions/sessions.h>
19+
#include <ydb/core/sys_view/show_create/show_create.h>
1920
#include <ydb/core/sys_view/storage/groups.h>
2021
#include <ydb/core/sys_view/storage/pdisks.h>
2122
#include <ydb/core/sys_view/storage/storage_pools.h>
@@ -254,7 +255,7 @@ THolder<NActors::IActor> CreateSystemViewScan(
254255
if (tableId.SysViewInfo == InformationSchemaTablesName) {
255256
return CreateInformationSchemaTablesScan(ownerId, scanId, tableId, tablePath, tableRange, columns);
256257
}
257-
258+
258259
if (tableId.SysViewInfo == PgClassName) {
259260
return CreatePgClassScan(ownerId, scanId, tableId, tablePath, tableRange, columns);
260261
}
@@ -283,6 +284,10 @@ THolder<NActors::IActor> CreateSystemViewScan(
283284
}
284285
}
285286

287+
if (tableId.SysViewInfo == ShowCreateName) {
288+
return CreateShowCreate(ownerId, scanId, tableId, tableRange, columns, database, std::move(userToken));
289+
}
290+
286291
return {};
287292
}
288293

0 commit comments

Comments
 (0)