Skip to content

YQL-17350 pg_class YQL-17354 pg_stat_database #1437

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 31, 2024
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: 45 additions & 23 deletions ydb/library/yql/parser/pg_catalog/catalog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1402,26 +1402,28 @@ struct TCatalog {
"lo_unlink"
}),
StaticTables({
{"pg_catalog", "pg_type"},
{"pg_catalog", "pg_database"},
{"pg_catalog", "pg_tablespace"},
{"pg_catalog", "pg_shdescription"},
{"pg_catalog", "pg_trigger"},
{"pg_catalog", "pg_locks"},
{"pg_catalog", "pg_stat_gssapi"},
{"pg_catalog", "pg_inherits"},
{"pg_catalog", "pg_stat_activity"},
{"pg_catalog", "pg_timezone_names"},
{"pg_catalog", "pg_timezone_abbrevs"},
{"pg_catalog", "pg_tables"},
{"pg_catalog", "pg_description"},
{"pg_catalog", "pg_am"},
{"pg_catalog", "pg_namespace"},
{"pg_catalog", "pg_auth_members"},
{"pg_catalog", "pg_roles"},
{"information_schema", "tables"},
{"information_schema", "columns"},
{"information_schema", "table_constraints"},
{{"pg_catalog", "pg_type"}, ERelKind::Relation, TypeRelationOid},
{{"pg_catalog", "pg_database"}, ERelKind::Relation, DatabaseRelationOid},
{{"pg_catalog", "pg_tablespace"}, ERelKind::Relation, TableSpaceRelationOid},
{{"pg_catalog", "pg_shdescription"}, ERelKind::Relation, SharedDescriptionRelationOid},
{{"pg_catalog", "pg_trigger"}, ERelKind::Relation, TriggerRelationOid},
{{"pg_catalog", "pg_locks"}, ERelKind::View, 10000},
{{"pg_catalog", "pg_stat_gssapi"}, ERelKind::View, 10001},
{{"pg_catalog", "pg_inherits"}, ERelKind::Relation, InheritsRelationOid},
{{"pg_catalog", "pg_stat_activity"}, ERelKind::View, 10002},
{{"pg_catalog", "pg_timezone_names"}, ERelKind::View, 10003},
{{"pg_catalog", "pg_timezone_abbrevs"}, ERelKind::View, 10004},
{{"pg_catalog", "pg_tables"}, ERelKind::View, 10005},
{{"pg_catalog", "pg_description"}, ERelKind::Relation, DescriptionRelationOid},
{{"pg_catalog", "pg_am"}, ERelKind::Relation, AccessMethodRelationOid},
{{"pg_catalog", "pg_namespace"}, ERelKind::Relation, NamespaceRelationOid},
{{"pg_catalog", "pg_auth_members"}, ERelKind::Relation, AuthMemRelationOid},
{{"pg_catalog", "pg_roles"}, ERelKind::View, 10006},
{{"pg_catalog", "pg_stat_database"}, ERelKind::View, 10007},
{{"pg_catalog", "pg_class"}, ERelKind::Relation, RelationRelationOid},
{{"information_schema", "tables"}, ERelKind::View, 10008},
{{"information_schema", "columns"}, ERelKind::View, 10009},
{{"information_schema", "table_constraints"}, ERelKind::View, 10010},
}),
AllStaticColumns({
{"pg_catalog", "pg_type", "oid", "oid"},
Expand Down Expand Up @@ -1510,6 +1512,24 @@ struct TCatalog {
{"pg_catalog", "pg_roles", "rolsuper", "bool"},
{"pg_catalog", "pg_roles", "rolvaliduntil", "timestamptz"},

{"pg_catalog", "pg_stat_database", "datid", "oid"},
{"pg_catalog", "pg_stat_database", "blks_hit", "int8"},
{"pg_catalog", "pg_stat_database", "blks_read", "int8"},
{"pg_catalog", "pg_stat_database", "tup_deleted", "int8"},
{"pg_catalog", "pg_stat_database", "tup_fetched", "int8"},
{"pg_catalog", "pg_stat_database", "tup_inserted", "int8"},
{"pg_catalog", "pg_stat_database", "tup_returned", "int8"},
{"pg_catalog", "pg_stat_database", "tup_updated", "int8"},
{"pg_catalog", "pg_stat_database", "xact_commit", "int8"},
{"pg_catalog", "pg_stat_database", "xact_rollback", "int8"},

{"pg_catalog", "pg_class", "oid", "oid"},
{"pg_catalog", "pg_class", "relispartition", "bool"},
{"pg_catalog", "pg_class", "relkind", "char"},
{"pg_catalog", "pg_class", "relname", "name"},
{"pg_catalog", "pg_class", "relnamespace", "oid"},
{"pg_catalog", "pg_class", "relowner", "oid"},

{"information_schema", "tables", "table_schema", "name"},
{"information_schema", "tables", "table_name", "name"},

Expand All @@ -1523,12 +1543,14 @@ struct TCatalog {
{"information_schema", "table_constraints", "constraint_type", "varchar"},
})
{
THashSet<ui32> usedTableOids;
for (const auto& t : StaticTables) {
StaticColumns.insert(std::make_pair(t, TVector<TColumnInfo>()));
Y_ENSURE(usedTableOids.insert(t.Oid).first);
}

for (const auto& c: AllStaticColumns) {
auto tablePtr = StaticColumns.FindPtr(TTableInfo{c.Schema, c.TableName});
auto tablePtr = StaticColumns.FindPtr(TTableInfoKey{c.Schema, c.TableName});
Y_ENSURE(tablePtr);
tablePtr->push_back(c);
}
Expand Down Expand Up @@ -1740,7 +1762,7 @@ struct TCatalog {

TVector<TTableInfo> StaticTables;
TVector<TColumnInfo> AllStaticColumns;
THashMap<TTableInfo, TVector<TColumnInfo>> StaticColumns;
THashMap<TTableInfoKey, TVector<TColumnInfo>> StaticColumns;
};

bool ValidateArgs(const TVector<ui32>& descArgTypeIds, const TVector<ui32>& argTypeIds) {
Expand Down Expand Up @@ -2904,7 +2926,7 @@ const TVector<TTableInfo>& GetStaticTables() {
return catalog.StaticTables;
}

const THashMap<TTableInfo, TVector<TColumnInfo>>& GetStaticColumns() {
const THashMap<TTableInfoKey, TVector<TColumnInfo>>& GetStaticColumns() {
const auto& catalog = TCatalog::Instance();
return catalog.StaticColumns;
}
Expand Down
32 changes: 27 additions & 5 deletions ydb/library/yql/parser/pg_catalog/catalog.h
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,16 @@ bool IsCoercible(ui32 fromTypeId, ui32 toTypeId, ECoercionCode coercionType);
inline bool IsArrayType(const TTypeDesc& typeDesc) noexcept {
return typeDesc.ArrayTypeId == typeDesc.TypeId;
}
struct TTableInfo {

enum class ERelKind : char {
Relation = 'r',
View = 'v'
};
struct TTableInfoKey {
TString Schema;
TString Name;

bool operator==(const TTableInfo& other) const {
bool operator==(const TTableInfoKey& other) const {
return Schema == other.Schema && Name == other.Name;
}

Expand All @@ -299,6 +304,23 @@ struct TTableInfo {
}
};

constexpr ui32 TypeRelationOid = 1247;
constexpr ui32 DatabaseRelationOid = 1262;
constexpr ui32 TableSpaceRelationOid = 1213;
constexpr ui32 SharedDescriptionRelationOid = 2396;
constexpr ui32 TriggerRelationOid = 2620;
constexpr ui32 InheritsRelationOid = 2611;
constexpr ui32 DescriptionRelationOid = 2609;
constexpr ui32 AccessMethodRelationOid = 2601;
constexpr ui32 NamespaceRelationOid = 2615;
constexpr ui32 AuthMemRelationOid = 1261;
constexpr ui32 RelationRelationOid = 1259;

struct TTableInfo : public TTableInfoKey {
ERelKind Kind;
ui32 Oid;
};

struct TColumnInfo {
TString Schema;
TString TableName;
Expand All @@ -307,7 +329,7 @@ struct TColumnInfo {
};

const TVector<TTableInfo>& GetStaticTables();
const THashMap<TTableInfo, TVector<TColumnInfo>>& GetStaticColumns();
const THashMap<TTableInfoKey, TVector<TColumnInfo>>& GetStaticColumns();

}

Expand All @@ -322,8 +344,8 @@ inline void Out<NYql::NPg::ECoercionCode>(IOutputStream& o, NYql::NPg::ECoercion
}

template <>
struct THash<NYql::NPg::TTableInfo> {
size_t operator ()(const NYql::NPg::TTableInfo& val) const {
struct THash<NYql::NPg::TTableInfoKey> {
size_t operator ()(const NYql::NPg::TTableInfoKey& val) const {
return val.Hash();
}
};
25 changes: 25 additions & 0 deletions ydb/library/yql/parser/pg_catalog/ut/catalog_consts_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@

extern "C" {
#include "catalog/pg_collation_d.h"
#include "catalog/pg_type_d.h"
#include "catalog/pg_database_d.h"
#include "catalog/pg_tablespace_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_trigger_d.h"
#include "catalog/pg_inherits_d.h"
#include "catalog/pg_description_d.h"
#include "catalog/pg_am_d.h"
#include "catalog/pg_namespace_d.h"
#include "catalog/pg_auth_members_d.h"
#include "catalog/pg_class_d.h"
#include "access/stratnum.h"
}

Expand Down Expand Up @@ -41,4 +52,18 @@ Y_UNIT_TEST_SUITE(TConstantsTests) {
typeDesc = LookupType("text");
UNIT_ASSERT_VALUES_EQUAL(typeDesc.TypeId, TextOid);
}

Y_UNIT_TEST(TRelationOidConsts) {
UNIT_ASSERT_VALUES_EQUAL(TypeRelationOid, TypeRelationId);
UNIT_ASSERT_VALUES_EQUAL(DatabaseRelationOid, DatabaseRelationId);
UNIT_ASSERT_VALUES_EQUAL(TableSpaceRelationOid, TableSpaceRelationId);
UNIT_ASSERT_VALUES_EQUAL(SharedDescriptionRelationOid, SharedDescriptionRelationId);
UNIT_ASSERT_VALUES_EQUAL(TriggerRelationOid, TriggerRelationId);
UNIT_ASSERT_VALUES_EQUAL(InheritsRelationOid, InheritsRelationId);
UNIT_ASSERT_VALUES_EQUAL(DescriptionRelationOid, DescriptionRelationId);
UNIT_ASSERT_VALUES_EQUAL(AccessMethodRelationOid, AccessMethodRelationId);
UNIT_ASSERT_VALUES_EQUAL(NamespaceRelationOid, NamespaceRelationId);
UNIT_ASSERT_VALUES_EQUAL(AuthMemRelationOid, AuthMemRelationId);
UNIT_ASSERT_VALUES_EQUAL(RelationRelationOid, RelationRelationId);
}
}
61 changes: 61 additions & 0 deletions ydb/library/yql/parser/pg_wrapper/comp_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,32 @@ class TPgTableContent : public TMutableComputationNode<TPgTableContent> {
};

ApplyFillers(AllPgRolesFillers, Y_ARRAY_SIZE(AllPgRolesFillers), PgRolesFillers_);
} else if (Table_ == "pg_stat_database") {
static const std::pair<const char*, TPgDatabaseStatFiller> AllPgDatabaseStatFillers[] = {
{"datid", [](ui32 index) { return ScalarDatumToPod(ObjectIdGetDatum(index ? 3 : 0)); }},
{"blks_hit", [](ui32) { return ScalarDatumToPod(Int64GetDatum(0)); }},
{"blks_read", [](ui32) { return ScalarDatumToPod(Int64GetDatum(0)); }},
{"tup_deleted", [](ui32) { return ScalarDatumToPod(Int64GetDatum(0)); }},
{"tup_fetched", [](ui32) { return ScalarDatumToPod(Int64GetDatum(0)); }},
{"tup_inserted", [](ui32) { return ScalarDatumToPod(Int64GetDatum(0)); }},
{"tup_returned", [](ui32) { return ScalarDatumToPod(Int64GetDatum(0)); }},
{"tup_updated", [](ui32) { return ScalarDatumToPod(Int64GetDatum(0)); }},
{"xact_commit", [](ui32) { return ScalarDatumToPod(Int64GetDatum(0)); }},
{"xact_rollback", [](ui32) { return ScalarDatumToPod(Int64GetDatum(0)); }},
};

ApplyFillers(AllPgDatabaseStatFillers, Y_ARRAY_SIZE(AllPgDatabaseStatFillers), PgDatabaseStatFillers_);
} else if (Table_ == "pg_class") {
static const std::pair<const char*, TPgClassFiller> AllPgClassFillers[] = {
{"oid", [](const NPg::TTableInfo& desc, ui32) { return ScalarDatumToPod(ObjectIdGetDatum(desc.Oid)); }},
{"relispartition", [](const NPg::TTableInfo&, ui32) { return ScalarDatumToPod(BoolGetDatum(false)); }},
{"relkind", [](const NPg::TTableInfo& desc, ui32) { return ScalarDatumToPod(CharGetDatum(desc.Kind)); }},
{"relname", [](const NPg::TTableInfo& desc, ui32) { return PointerDatumToPod((Datum)MakeFixedString(desc.Name, NAMEDATALEN)); }},
{"relnamespace", [](const NPg::TTableInfo&, ui32 namespaceOid) { return ScalarDatumToPod(ObjectIdGetDatum(namespaceOid)); }},
{"relowner", [](const NPg::TTableInfo&, ui32) { return ScalarDatumToPod(ObjectIdGetDatum(1)); }},
};

ApplyFillers(AllPgClassFillers, Y_ARRAY_SIZE(AllPgClassFillers), PgClassFillers_);
}
} else {
if (Table_ == "tables") {
Expand Down Expand Up @@ -424,6 +450,10 @@ class TPgTableContent : public TMutableComputationNode<TPgTableContent> {
fillers[*pos] = func;
}
}

for (const auto& f : fillers) {
Y_ENSURE(f);
}
}

NUdf::TUnboxedValuePod DoCalculate(TComputationContext& compCtx) const {
Expand Down Expand Up @@ -602,6 +632,32 @@ class TPgTableContent : public TMutableComputationNode<TPgTableContent> {
}

rows.emplace_back(row);
} else if (Table_ == "pg_stat_database") {
for (ui32 index = 0; index <= 1; ++index) {
NUdf::TUnboxedValue* items;
auto row = compCtx.HolderFactory.CreateDirectArrayHolder(PgDatabaseStatFillers_.size(), items);
for (ui32 i = 0; i < PgDatabaseStatFillers_.size(); ++i) {
items[i] = PgDatabaseStatFillers_[i](index);
}

rows.emplace_back(row);
}
} else if (Table_ == "pg_class") {
const auto& tables = NPg::GetStaticTables();
THashMap<TString, ui32> namespaces;
NPg::EnumNamespace([&](ui32 oid, const NPg::TNamespaceDesc& desc) {
namespaces[desc.Name] = oid;
});

for (const auto& t : tables) {
NUdf::TUnboxedValue* items;
auto row = compCtx.HolderFactory.CreateDirectArrayHolder(PgClassFillers_.size(), items);
for (ui32 i = 0; i < PgClassFillers_.size(); ++i) {
items[i] = PgClassFillers_[i](t, namespaces[t.Schema]);
}

rows.emplace_back(row);
}
}
} else {
if (Table_ == "tables") {
Expand Down Expand Up @@ -658,6 +714,8 @@ class TPgTableContent : public TMutableComputationNode<TPgTableContent> {
TVector<TPgAmFiller> PgAmFillers_;
using TPgRolesFiller = NUdf::TUnboxedValuePod(*)();
TVector<TPgRolesFiller> PgRolesFillers_;
using TPgDatabaseStatFiller = NUdf::TUnboxedValuePod(*)(ui32 index);
TVector<TPgDatabaseStatFiller> PgDatabaseStatFillers_;

struct TDescriptionDesc {
ui32 Objoid = 0;
Expand All @@ -675,6 +733,9 @@ class TPgTableContent : public TMutableComputationNode<TPgTableContent> {

using TColumnsFiller = NUdf::TUnboxedValuePod(*)(const NPg::TColumnInfo&);
TVector<TColumnsFiller> ColumnsFillers_;

using TPgClassFiller = NUdf::TUnboxedValuePod(*)(const NPg::TTableInfo&, ui32 namespaceOid);
TVector<TPgClassFiller> PgClassFillers_;
};

class TFunctionCallInfo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class TPgDataSourceTypeAnnotationTransformer : public TVisitorTransformerBase {

auto tableName = input->Child(TNode::idx_Table)->Content();
TVector<const TItemExprType*> items;
auto columnsPtr = NPg::GetStaticColumns().FindPtr(NPg::TTableInfo{ cluster, TString(tableName) });
auto columnsPtr = NPg::GetStaticColumns().FindPtr(NPg::TTableInfoKey{ cluster, TString(tableName) });
if (!columnsPtr) {
ctx.AddError(TIssue(ctx.GetPosition(input->Child(TPgReadTable::idx_Table)->Pos()), TStringBuilder() << "Unsupported table: " << tableName));
return TStatus::Error;
Expand Down
22 changes: 22 additions & 0 deletions ydb/library/yql/tests/sql/dq_file/part12/canondata/result.json
Original file line number Diff line number Diff line change
Expand Up @@ -2506,6 +2506,28 @@
}
],
"test.test[pg-tpch-q16-default.txt-Results]": [],
"test.test[pg_catalog-pg_class-default.txt-Analyze]": [
{
"checksum": "c1f2d837c3623c81dd596a9877913fb8",
"size": 948,
"uri": "https://{canondata_backend}/1937367/a0981807726fa8e5aad90985bda23ee6596b1473/resource.tar.gz#test.test_pg_catalog-pg_class-default.txt-Analyze_/plan.txt"
}
],
"test.test[pg_catalog-pg_class-default.txt-Debug]": [
{
"checksum": "48c1e7df973ab53841f9705a3a6f1d3e",
"size": 549,
"uri": "https://{canondata_backend}/1937367/a0981807726fa8e5aad90985bda23ee6596b1473/resource.tar.gz#test.test_pg_catalog-pg_class-default.txt-Debug_/opt.yql_patched"
}
],
"test.test[pg_catalog-pg_class-default.txt-Plan]": [
{
"checksum": "c1f2d837c3623c81dd596a9877913fb8",
"size": 948,
"uri": "https://{canondata_backend}/1937367/a0981807726fa8e5aad90985bda23ee6596b1473/resource.tar.gz#test.test_pg_catalog-pg_class-default.txt-Plan_/plan.txt"
}
],
"test.test[pg_catalog-pg_class-default.txt-Results]": [],
"test.test[pg_catalog-pg_description_pg_syntax-default.txt-Analyze]": [
{
"checksum": "c1f2d837c3623c81dd596a9877913fb8",
Expand Down
22 changes: 22 additions & 0 deletions ydb/library/yql/tests/sql/dq_file/part14/canondata/result.json
Original file line number Diff line number Diff line change
Expand Up @@ -2435,6 +2435,28 @@
}
],
"test.test[pg_catalog-pg_stat_activity-default.txt-Results]": [],
"test.test[pg_catalog-pg_stat_database-default.txt-Analyze]": [
{
"checksum": "c1f2d837c3623c81dd596a9877913fb8",
"size": 948,
"uri": "https://{canondata_backend}/1936947/960a823a7074bb0a4fc6829f35dc9035ea62bcf1/resource.tar.gz#test.test_pg_catalog-pg_stat_database-default.txt-Analyze_/plan.txt"
}
],
"test.test[pg_catalog-pg_stat_database-default.txt-Debug]": [
{
"checksum": "62d4a7970f183dc24931fa2cf712509e",
"size": 1205,
"uri": "https://{canondata_backend}/1936947/960a823a7074bb0a4fc6829f35dc9035ea62bcf1/resource.tar.gz#test.test_pg_catalog-pg_stat_database-default.txt-Debug_/opt.yql_patched"
}
],
"test.test[pg_catalog-pg_stat_database-default.txt-Plan]": [
{
"checksum": "c1f2d837c3623c81dd596a9877913fb8",
"size": 948,
"uri": "https://{canondata_backend}/1936947/960a823a7074bb0a4fc6829f35dc9035ea62bcf1/resource.tar.gz#test.test_pg_catalog-pg_stat_database-default.txt-Plan_/plan.txt"
}
],
"test.test[pg_catalog-pg_stat_database-default.txt-Results]": [],
"test.test[pg_catalog-pg_tables-default.txt-Analyze]": [
{
"checksum": "c1f2d837c3623c81dd596a9877913fb8",
Expand Down
14 changes: 14 additions & 0 deletions ydb/library/yql/tests/sql/sql2yql/canondata/result.json
Original file line number Diff line number Diff line change
Expand Up @@ -13264,6 +13264,13 @@
"uri": "https://{canondata_backend}/1942671/97ffbf971de6e6c2db7e871e5dfde6d17befe266/resource.tar.gz#test_sql2yql.test_pg_catalog-pg_auth_members_/sql.yql"
}
],
"test_sql2yql.test[pg_catalog-pg_class]": [
{
"checksum": "22ebe252b77b53e39b177ca4c9a7b530",
"size": 1075,
"uri": "https://{canondata_backend}/1936842/55a7040ab1de9722f14ba790e6f8065314d91717/resource.tar.gz#test_sql2yql.test_pg_catalog-pg_class_/sql.yql"
}
],
"test_sql2yql.test[pg_catalog-pg_database]": [
{
"checksum": "3c66358cd738a51ccd0766e18e21fc45",
Expand Down Expand Up @@ -13334,6 +13341,13 @@
"uri": "https://{canondata_backend}/1773845/118c740ff9ea824c1b2998c5bad993b2a2276f6e/resource.tar.gz#test_sql2yql.test_pg_catalog-pg_stat_activity_/sql.yql"
}
],
"test_sql2yql.test[pg_catalog-pg_stat_database]": [
{
"checksum": "76a9374e0363e44c5cffffc9d23b8f51",
"size": 1346,
"uri": "https://{canondata_backend}/1936842/55a7040ab1de9722f14ba790e6f8065314d91717/resource.tar.gz#test_sql2yql.test_pg_catalog-pg_stat_database_/sql.yql"
}
],
"test_sql2yql.test[pg_catalog-pg_stat_gssapi]": [
{
"checksum": "618acd0a4c833faffdb13606ed680142",
Expand Down
Loading