YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/master/yql_columns_vtable.cc
Line
Count
Source
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
2.00k
    : YQLVirtualTable(table_name, namespace_name, master, CreateSchema()) {
40
2.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
27.9k
    RETURN_NOT_OK(SetColumnValue(kColumnName,
51
27.9k
                                 YcqlName::DemangleName(schema.column(col_idx).name()),
52
27.9k
                                 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
263k
  for (scoped_refptr<TableInfo> table : tables) {
69
70
    // Skip non-YQL tables.
71
263k
    if (!IsYcqlTable(*table)) {
72
20.7k
      continue;
73
20.7k
    }
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
242k
    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
489k
    for (int32_t i = 0; i < num_hash_columns; i++) {
88
246k
      QLRow& row = vtable->Extend();
89
246k
      RETURN_NOT_OK(PopulateColumnInformation(schema, keyspace_name, table_name, i, &row));
90
      // kind (always partition_key for hash columns)
91
246k
      RETURN_NOT_OK(SetColumnValue(kKind, "partition_key", &row));
92
246k
      RETURN_NOT_OK(SetColumnValue(kPosition, i, &row));
93
246k
    }
94
95
    // Now fill in the range columns
96
242k
    auto num_range_columns = narrow_cast<int32_t>(schema.num_range_key_columns());
97
495k
    for (int32_t i = num_hash_columns; i < num_hash_columns + num_range_columns; i++) {
98
252k
      QLRow& row = vtable->Extend();
99
252k
      RETURN_NOT_OK(PopulateColumnInformation(schema, keyspace_name, table_name, i, &row));
100
      // kind (always clustering for range columns)
101
252k
      RETURN_NOT_OK(SetColumnValue(kKind, "clustering", &row));
102
252k
      RETURN_NOT_OK(SetColumnValue(kPosition, i - num_hash_columns, &row));
103
252k
    }
104
105
    // Now fill in the rest of the columns.
106
242k
    auto num_columns = narrow_cast<int32_t>(schema.num_columns());
107
1.76M
    for (auto i = num_hash_columns + num_range_columns; i < num_columns; i++) {
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" : "regular";
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
2.00k
Schema YQLColumnsVTable::CreateSchema() const {
122
2.00k
  SchemaBuilder builder;
123
2.00k
  CHECK_OK(builder.AddHashKeyColumn(kKeyspaceName, DataType::STRING));
124
2.00k
  CHECK_OK(builder.AddKeyColumn(kTableName, DataType::STRING));
125
2.00k
  CHECK_OK(builder.AddKeyColumn(kColumnName, DataType::STRING));
126
2.00k
  CHECK_OK(builder.AddColumn(kClusteringOrder, DataType::STRING));
127
2.00k
  CHECK_OK(builder.AddColumn(kColumnNameBytes, DataType::BINARY));
128
2.00k
  CHECK_OK(builder.AddColumn(kKind, DataType::STRING));
129
2.00k
  CHECK_OK(builder.AddColumn(kPosition, DataType::INT32));
130
2.00k
  CHECK_OK(builder.AddColumn(kType, DataType::STRING));
131
2.00k
  return builder.Build();
132
2.00k
}
133
134
}  // namespace master
135
}  // namespace yb