Skip to content

Commit 633ff57

Browse files
authored
ExtractMembers from pg_tables read. Resolves #7286 (#7595)
1 parent c59279a commit 633ff57

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

ydb/core/kqp/provider/yql_kikimr_opt_build.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -887,23 +887,23 @@ TExprNode::TPtr KiBuildQuery(TExprBase node, TExprContext& ctx, TStringBuf datab
887887

888888
TNodeOnNodeOwnedMap replaces;
889889
std::unordered_set<std::string> pgDynTables = {"pg_tables", "tables", "pg_class"};
890-
std::unordered_map<const NYql::TExprNode*, TString> tableNames;
891-
VisitExpr(node.Ptr(), [&replaces, &tableNames, &pgDynTables](const TExprNode::TPtr& input) -> bool {
890+
VisitExpr(node.Ptr(), [&replaces, &pgDynTables](const TExprNode::TPtr& input) -> bool {
892891
if (input->IsCallable("PgTableContent")) {
893892
TPgTableContent content(input);
894893
if (pgDynTables.contains(content.Table().StringValue())) {
895894
replaces[input.Get()] = nullptr;
896-
tableNames[input.Get()] = content.Table().StringValue();
897895
}
898896
}
899897
return true;
900898
});
901899
if (!replaces.empty()) {
902-
for (auto& [key, _] : replaces) {
900+
for (auto& [input, _] : replaces) {
901+
TPgTableContent content(input);
902+
903903
TExprNode::TPtr path = ctx.NewCallable(
904904
node.Pos(),
905905
"String",
906-
{ ctx.NewAtom(node.Pos(), TStringBuilder() << "/" << database << "/.sys/" << tableNames[key]) }
906+
{ ctx.NewAtom(node.Pos(), TStringBuilder() << "/" << database << "/.sys/" << content.Table().StringValue()) }
907907
);
908908
auto table = ctx.NewList(node.Pos(), {ctx.NewAtom(node.Pos(), "table"), path});
909909
auto newKey = ctx.NewCallable(node.Pos(), "Key", {table});
@@ -923,10 +923,21 @@ TExprNode::TPtr KiBuildQuery(TExprBase node, TExprContext& ctx, TStringBuf datab
923923
.Build()
924924
.Done().Ptr();
925925

926+
926927
auto readData = Build<TCoRight>(ctx, node.Pos())
927928
.Input(ydbSysTableRead)
928929
.Done().Ptr();
929-
replaces[key] = readData;
930+
931+
if (auto v = content.Columns().Maybe<TCoVoid>()) {
932+
replaces[input] = readData;
933+
} else {
934+
auto extractMembers = Build<TCoExtractMembers>(ctx, node.Pos())
935+
.Input(readData)
936+
.Members(content.Columns().Ptr())
937+
.Done().Ptr();
938+
939+
replaces[input] = extractMembers;
940+
}
930941
}
931942
ctx.Step
932943
.Repeat(TExprStep::ExprEval)

ydb/core/kqp/ut/pg/pg_catalog_ut.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,8 @@ Y_UNIT_TEST_SUITE(PgCatalog) {
416416
{
417417
auto result = db.ExecuteQuery(R"(
418418
CREATE TABLE table1 (
419-
id int4 primary key
419+
id int4 primary key,
420+
value int4
420421
);
421422
CREATE TABLE table2 (
422423
id varchar primary key
@@ -486,6 +487,16 @@ Y_UNIT_TEST_SUITE(PgCatalog) {
486487
["table1"];["table2"]
487488
])", FormatResultSetYson(result.GetResultSet(0)));
488489
}
490+
{ //https://github.com/ydb-platform/ydb/issues/7286
491+
auto result = db.ExecuteQuery(R"(
492+
select tablename from pg_catalog.pg_tables where tablename='pg_proc'
493+
)", NYdb::NQuery::TTxControl::BeginTx().CommitTx(), settings).ExtractValueSync();
494+
UNIT_ASSERT_C(result.IsSuccess(), result.GetIssues().ToString());
495+
UNIT_ASSERT_C(!result.GetResultSets().empty(), "no result sets");
496+
CompareYson(R"([
497+
["pg_proc"]
498+
])", FormatResultSetYson(result.GetResultSet(0)));
499+
}
489500
}
490501
}
491502

0 commit comments

Comments
 (0)