/Users/deen/code/yugabyte-db/src/yb/master/yql_columns_vtable.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) YugaByte, Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
4 | | // in compliance with the License. You may obtain a copy of the License at |
5 | | // |
6 | | // http://www.apache.org/licenses/LICENSE-2.0 |
7 | | // |
8 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
9 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
10 | | // or implied. See the License for the specific language governing permissions and limitations |
11 | | // under the License. |
12 | | // |
13 | | |
14 | | #include "yb/master/yql_columns_vtable.h" |
15 | | |
16 | | #include <stdint.h> |
17 | | |
18 | | #include <glog/logging.h> |
19 | | |
20 | | #include "yb/common/ql_name.h" |
21 | | #include "yb/common/ql_type.h" |
22 | | #include "yb/common/schema.h" |
23 | | |
24 | | #include "yb/gutil/casts.h" |
25 | | |
26 | | #include "yb/master/catalog_entity_info.h" |
27 | | #include "yb/master/catalog_manager_if.h" |
28 | | #include "yb/master/master.h" |
29 | | |
30 | | #include "yb/util/status_log.h" |
31 | | #include "yb/util/uuid.h" |
32 | | |
33 | | namespace yb { |
34 | | namespace master { |
35 | | |
36 | | YQLColumnsVTable::YQLColumnsVTable(const TableName& table_name, |
37 | | const NamespaceName& namespace_name, |
38 | | Master* const master) |
39 | 3.00k | : YQLVirtualTable(table_name, namespace_name, master, CreateSchema()) { |
40 | 3.00k | } |
41 | | |
42 | | Status YQLColumnsVTable::PopulateColumnInformation(const Schema& schema, |
43 | | const string& keyspace_name, |
44 | | const string& table_name, |
45 | | const size_t col_idx, |
46 | 2.01M | QLRow* const row) const { |
47 | 2.01M | RETURN_NOT_OK(SetColumnValue(kKeyspaceName, keyspace_name, row)); |
48 | 2.01M | RETURN_NOT_OK(SetColumnValue(kTableName, table_name, row)); |
49 | 2.01M | if (schema.table_properties().use_mangled_column_name()) { |
50 | 25.4k | RETURN_NOT_OK(SetColumnValue(kColumnName, |
51 | 25.4k | YcqlName::DemangleName(schema.column(col_idx).name()), |
52 | 25.4k | row)); |
53 | 1.98M | } else { |
54 | 1.98M | RETURN_NOT_OK(SetColumnValue(kColumnName, schema.column(col_idx).name(), row)); |
55 | 1.98M | } |
56 | 2.01M | RETURN_NOT_OK(SetColumnValue(kClusteringOrder, schema.column(col_idx).sorting_type_string(), |
57 | 2.01M | row)); |
58 | 2.01M | const ColumnSchema& column = schema.column(col_idx); |
59 | 2.01M | RETURN_NOT_OK(SetColumnValue(kType, column.is_counter() ? "counter" : column.type()->ToString(), |
60 | 2.01M | row)); |
61 | 2.01M | return Status::OK(); |
62 | 2.01M | } |
63 | | |
64 | | Result<std::shared_ptr<QLRowBlock>> YQLColumnsVTable::RetrieveData( |
65 | 14.2k | const QLReadRequestPB& request) const { |
66 | 14.2k | auto vtable = std::make_shared<QLRowBlock>(schema()); |
67 | 14.2k | auto tables = catalog_manager().GetTables(GetTablesMode::kVisibleToClient); |
68 | 265k | for (scoped_refptr<TableInfo> table : tables) { |
69 | | |
70 | | // Skip non-YQL tables. |
71 | 265k | if (!IsYcqlTable(*table)) { |
72 | 23.1k | continue; |
73 | 23.1k | } |
74 | | |
75 | 242k | Schema schema; |
76 | 242k | RETURN_NOT_OK(table->GetSchema(&schema)); |
77 | | |
78 | | // Get namespace for table. |
79 | 242k | auto ns_info = VERIFY_RESULT(master_->catalog_manager()->FindNamespaceById( |
80 | 242k | table->namespace_id())); |
81 | | |
82 | 0 | const string& keyspace_name = ns_info->name(); |
83 | 242k | const string& table_name = table->name(); |
84 | | |
85 | | // Fill in the hash keys first. |
86 | 242k | auto num_hash_columns = narrow_cast<int32_t>(schema.num_hash_key_columns()); |
87 | 487k | for (int32_t i = 0; i < num_hash_columns; i++245k ) { |
88 | 245k | QLRow& row = vtable->Extend(); |
89 | 245k | RETURN_NOT_OK(PopulateColumnInformation(schema, keyspace_name, table_name, i, &row)); |
90 | | // kind (always partition_key for hash columns) |
91 | 245k | RETURN_NOT_OK(SetColumnValue(kKind, "partition_key", &row)); |
92 | 245k | RETURN_NOT_OK(SetColumnValue(kPosition, i, &row)); |
93 | 245k | } |
94 | | |
95 | | // Now fill in the range columns |
96 | 242k | auto num_range_columns = narrow_cast<int32_t>(schema.num_range_key_columns()); |
97 | 491k | for (int32_t i = num_hash_columns; i < num_hash_columns + num_range_columns; i++249k ) { |
98 | 249k | QLRow& row = vtable->Extend(); |
99 | 249k | RETURN_NOT_OK(PopulateColumnInformation(schema, keyspace_name, table_name, i, &row)); |
100 | | // kind (always clustering for range columns) |
101 | 249k | RETURN_NOT_OK(SetColumnValue(kKind, "clustering", &row)); |
102 | 249k | RETURN_NOT_OK(SetColumnValue(kPosition, i - num_hash_columns, &row)); |
103 | 249k | } |
104 | | |
105 | | // Now fill in the rest of the columns. |
106 | 242k | auto num_columns = narrow_cast<int32_t>(schema.num_columns()); |
107 | 1.75M | for (auto i = num_hash_columns + num_range_columns; i < num_columns; i++1.51M ) { |
108 | 1.51M | QLRow &row = vtable->Extend(); |
109 | 1.51M | RETURN_NOT_OK(PopulateColumnInformation(schema, keyspace_name, table_name, i, &row)); |
110 | | // kind (always regular for regular columns) |
111 | 1.51M | const ColumnSchema& column = schema.column(i); |
112 | 1.51M | string kind = column.is_static() ? "static"176 : "regular"1.51M ; |
113 | 1.51M | RETURN_NOT_OK(SetColumnValue(kKind, kind, &row)); |
114 | 1.51M | RETURN_NOT_OK(SetColumnValue(kPosition, -1, &row)); |
115 | 1.51M | } |
116 | 242k | } |
117 | | |
118 | 14.2k | return vtable; |
119 | 14.2k | } |
120 | | |
121 | 3.00k | Schema YQLColumnsVTable::CreateSchema() const { |
122 | 3.00k | SchemaBuilder builder; |
123 | 3.00k | CHECK_OK(builder.AddHashKeyColumn(kKeyspaceName, DataType::STRING)); |
124 | 3.00k | CHECK_OK(builder.AddKeyColumn(kTableName, DataType::STRING)); |
125 | 3.00k | CHECK_OK(builder.AddKeyColumn(kColumnName, DataType::STRING)); |
126 | 3.00k | CHECK_OK(builder.AddColumn(kClusteringOrder, DataType::STRING)); |
127 | 3.00k | CHECK_OK(builder.AddColumn(kColumnNameBytes, DataType::BINARY)); |
128 | 3.00k | CHECK_OK(builder.AddColumn(kKind, DataType::STRING)); |
129 | 3.00k | CHECK_OK(builder.AddColumn(kPosition, DataType::INT32)); |
130 | 3.00k | CHECK_OK(builder.AddColumn(kType, DataType::STRING)); |
131 | 3.00k | return builder.Build(); |
132 | 3.00k | } |
133 | | |
134 | | } // namespace master |
135 | | } // namespace yb |