Skip to content

Commit

Permalink
ExtractMembers from pg_tables read. Resolves #7286 (#7595)
Browse files Browse the repository at this point in the history
  • Loading branch information
qrort authored Aug 9, 2024
1 parent c59279a commit 633ff57
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
23 changes: 17 additions & 6 deletions ydb/core/kqp/provider/yql_kikimr_opt_build.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -887,23 +887,23 @@ TExprNode::TPtr KiBuildQuery(TExprBase node, TExprContext& ctx, TStringBuf datab

TNodeOnNodeOwnedMap replaces;
std::unordered_set<std::string> pgDynTables = {"pg_tables", "tables", "pg_class"};
std::unordered_map<const NYql::TExprNode*, TString> tableNames;
VisitExpr(node.Ptr(), [&replaces, &tableNames, &pgDynTables](const TExprNode::TPtr& input) -> bool {
VisitExpr(node.Ptr(), [&replaces, &pgDynTables](const TExprNode::TPtr& input) -> bool {
if (input->IsCallable("PgTableContent")) {
TPgTableContent content(input);
if (pgDynTables.contains(content.Table().StringValue())) {
replaces[input.Get()] = nullptr;
tableNames[input.Get()] = content.Table().StringValue();
}
}
return true;
});
if (!replaces.empty()) {
for (auto& [key, _] : replaces) {
for (auto& [input, _] : replaces) {
TPgTableContent content(input);

TExprNode::TPtr path = ctx.NewCallable(
node.Pos(),
"String",
{ ctx.NewAtom(node.Pos(), TStringBuilder() << "/" << database << "/.sys/" << tableNames[key]) }
{ ctx.NewAtom(node.Pos(), TStringBuilder() << "/" << database << "/.sys/" << content.Table().StringValue()) }
);
auto table = ctx.NewList(node.Pos(), {ctx.NewAtom(node.Pos(), "table"), path});
auto newKey = ctx.NewCallable(node.Pos(), "Key", {table});
Expand All @@ -923,10 +923,21 @@ TExprNode::TPtr KiBuildQuery(TExprBase node, TExprContext& ctx, TStringBuf datab
.Build()
.Done().Ptr();


auto readData = Build<TCoRight>(ctx, node.Pos())
.Input(ydbSysTableRead)
.Done().Ptr();
replaces[key] = readData;

if (auto v = content.Columns().Maybe<TCoVoid>()) {
replaces[input] = readData;
} else {
auto extractMembers = Build<TCoExtractMembers>(ctx, node.Pos())
.Input(readData)
.Members(content.Columns().Ptr())
.Done().Ptr();

replaces[input] = extractMembers;
}
}
ctx.Step
.Repeat(TExprStep::ExprEval)
Expand Down
13 changes: 12 additions & 1 deletion ydb/core/kqp/ut/pg/pg_catalog_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,8 @@ Y_UNIT_TEST_SUITE(PgCatalog) {
{
auto result = db.ExecuteQuery(R"(
CREATE TABLE table1 (
id int4 primary key
id int4 primary key,
value int4
);
CREATE TABLE table2 (
id varchar primary key
Expand Down Expand Up @@ -486,6 +487,16 @@ Y_UNIT_TEST_SUITE(PgCatalog) {
["table1"];["table2"]
])", FormatResultSetYson(result.GetResultSet(0)));
}
{ //https://github.com/ydb-platform/ydb/issues/7286
auto result = db.ExecuteQuery(R"(
select tablename from pg_catalog.pg_tables where tablename='pg_proc'
)", NYdb::NQuery::TTxControl::BeginTx().CommitTx(), settings).ExtractValueSync();
UNIT_ASSERT_C(result.IsSuccess(), result.GetIssues().ToString());
UNIT_ASSERT_C(!result.GetResultSets().empty(), "no result sets");
CompareYson(R"([
["pg_proc"]
])", FormatResultSetYson(result.GetResultSet(0)));
}
}
}

Expand Down

0 comments on commit 633ff57

Please sign in to comment.