|
1 | 1 | #include "schema.h"
|
2 | 2 |
|
3 | 3 | #include <ydb/core/base/appdata.h>
|
| 4 | +#include <ydb/library/yql/parser/pg_catalog/catalog.h> |
4 | 5 |
|
5 | 6 | namespace NKikimr {
|
6 | 7 | namespace NSysView {
|
7 | 8 |
|
8 |
| -const TVector<Schema::PgColumn> Schema::PgTables::Columns = { |
9 |
| - Schema::PgColumn(1, "pgname", "schemaname"), |
10 |
| - Schema::PgColumn(2, "pgname", "tablename"), |
11 |
| - Schema::PgColumn(3, "pgname", "tableowner"), |
12 |
| - Schema::PgColumn(4, "pgname", "tablespace"), |
13 |
| - Schema::PgColumn(5, "pgbool", "hasindexes"), |
14 |
| - Schema::PgColumn(6, "pgbool", "hasrules"), |
15 |
| - Schema::PgColumn(7, "pgbool", "hastriggers"), |
16 |
| - Schema::PgColumn(8, "pgbool", "rowsecurity") |
17 |
| -}; |
| 9 | +namespace { |
| 10 | +TVector<Schema::PgColumn> GetPgStaticTableColumns(const TString& schema, const TString& tableName) { |
| 11 | + TVector<Schema::PgColumn> res; |
| 12 | + auto columns = NYql::NPg::GetStaticColumns().FindPtr(NYql::NPg::TTableInfoKey{schema, tableName}); |
| 13 | + res.reserve(columns->size()); |
| 14 | + for (size_t i = 0; i < columns->size(); i++) { |
| 15 | + const auto& column = columns->at(i); |
| 16 | + res.emplace_back(i, column.UdtType, column.Name); |
| 17 | + } |
| 18 | + return res; |
| 19 | +} |
| 20 | +} |
| 21 | + |
| 22 | +Schema::PgColumn::PgColumn(NIceDb::TColumnId columnId, TStringBuf columnTypeName, TStringBuf columnName) |
| 23 | + : _ColumnId(columnId) |
| 24 | + , _ColumnTypeInfo(NScheme::NTypeIds::Pg, NPg::TypeDescFromPgTypeId(NYql::NPg::LookupType(TString(columnTypeName)).TypeId)) |
| 25 | + , _ColumnName(columnName) |
| 26 | +{} |
| 27 | + |
| 28 | +const TVector<Schema::PgColumn>& Schema::PgTablesSchemaProvider::GetColumns(TStringBuf tableName) const { |
| 29 | + TString key(tableName); |
| 30 | + Y_ENSURE(columnsStorage.contains(key)); |
| 31 | + return columnsStorage.at(key); |
| 32 | +} |
| 33 | + |
| 34 | +Schema::PgTablesSchemaProvider::PgTablesSchemaProvider() { |
| 35 | + columnsStorage[TString(PgTablesName)] = GetPgStaticTableColumns("pg_catalog", "pg_tables"); |
| 36 | + columnsStorage[TString(InformationSchemaTablesName)] = GetPgStaticTableColumns("information_schema", "tables"); |
| 37 | + columnsStorage[TString(PgClassName)] = GetPgStaticTableColumns("pg_catalog", "pg_class"); |
| 38 | +} |
18 | 39 |
|
19 | 40 | bool MaybeSystemViewPath(const TVector<TString>& path) {
|
20 | 41 | auto length = path.size();
|
@@ -179,17 +200,31 @@ class TSystemViewResolver : public ISystemViewResolver {
|
179 | 200 | }
|
180 | 201 | };
|
181 | 202 |
|
182 |
| - void RegisterPgTablesSystemView() { |
183 |
| - auto& dsv = DomainSystemViews[PgTablesName]; |
184 |
| - auto& sdsv = SubDomainSystemViews[PgTablesName]; |
185 |
| - for (const auto& column : Schema::PgTables::Columns) { |
186 |
| - dsv.Columns[column._ColumnId] = TSysTables::TTableColumnInfo( |
187 |
| - column._ColumnName, column._ColumnId, column._ColumnTypeInfo, "", -1 |
188 |
| - ); |
189 |
| - sdsv.Columns[column._ColumnId] = TSysTables::TTableColumnInfo( |
190 |
| - column._ColumnName, column._ColumnId, column._ColumnTypeInfo, "", -1 |
191 |
| - ); |
192 |
| - } |
| 203 | + void RegisterPgTablesSystemViews() { |
| 204 | + auto registerView = [&](TStringBuf tableName, const TVector<Schema::PgColumn>& columns) { |
| 205 | + auto& dsv = DomainSystemViews[tableName]; |
| 206 | + auto& sdsv = SubDomainSystemViews[tableName]; |
| 207 | + for (const auto& column : columns) { |
| 208 | + dsv.Columns[column._ColumnId + 1] = TSysTables::TTableColumnInfo( |
| 209 | + column._ColumnName, column._ColumnId + 1, column._ColumnTypeInfo, "", -1 |
| 210 | + ); |
| 211 | + sdsv.Columns[column._ColumnId + 1] = TSysTables::TTableColumnInfo( |
| 212 | + column._ColumnName, column._ColumnId + 1, column._ColumnTypeInfo, "", -1 |
| 213 | + ); |
| 214 | + } |
| 215 | + }; |
| 216 | + registerView( |
| 217 | + PgTablesName, |
| 218 | + Singleton<Schema::PgTablesSchemaProvider>()->GetColumns(PgTablesName) |
| 219 | + ); |
| 220 | + registerView( |
| 221 | + InformationSchemaTablesName, |
| 222 | + Singleton<Schema::PgTablesSchemaProvider>()->GetColumns(InformationSchemaTablesName) |
| 223 | + ); |
| 224 | + registerView( |
| 225 | + PgClassName, |
| 226 | + Singleton<Schema::PgTablesSchemaProvider>()->GetColumns(PgClassName) |
| 227 | + ); |
193 | 228 | }
|
194 | 229 |
|
195 | 230 | template <typename Table>
|
@@ -250,7 +285,7 @@ class TSystemViewResolver : public ISystemViewResolver {
|
250 | 285 | RegisterSystemView<Schema::TopPartitions>(TopPartitions1MinuteName);
|
251 | 286 | RegisterSystemView<Schema::TopPartitions>(TopPartitions1HourName);
|
252 | 287 |
|
253 |
| - RegisterPgTablesSystemView(); |
| 288 | + RegisterPgTablesSystemViews(); |
254 | 289 | }
|
255 | 290 |
|
256 | 291 | private:
|
|
0 commit comments