YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/master/yql_tables_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_tables_vtable.h"
15
16
#include "yb/common/ql_type.h"
17
#include "yb/common/ql_value.h"
18
#include "yb/common/schema.h"
19
20
#include "yb/master/catalog_entity_info.h"
21
#include "yb/master/catalog_manager_if.h"
22
23
#include "yb/util/status_log.h"
24
25
namespace yb {
26
namespace master {
27
28
YQLTablesVTable::YQLTablesVTable(const TableName& table_name,
29
                                 const NamespaceName& namespace_name,
30
                                 Master* const master)
31
2.00k
    : YQLVirtualTable(table_name, namespace_name, master, CreateSchema()) {
32
2.00k
}
33
34
Result<std::shared_ptr<QLRowBlock>> YQLTablesVTable::RetrieveData(
35
15.5k
    const QLReadRequestPB& request) const {
36
15.5k
  auto vtable = std::make_shared<QLRowBlock>(schema());
37
38
15.5k
  auto tables = catalog_manager().GetTables(GetTablesMode::kVisibleToClient);
39
287k
  for (const auto& table : tables) {
40
    // Skip non-YQL tables.
41
287k
    if (!IsYcqlTable(*table)) {
42
23.3k
      continue;
43
23.3k
    }
44
45
    // Skip index table.
46
264k
    if (!table->indexed_table_id().empty()) {
47
6.61k
      continue;
48
6.61k
    }
49
50
    // Get namespace for table.
51
257k
    auto ns_info = VERIFY_RESULT(catalog_manager().FindNamespaceById(table->namespace_id()));
52
53
    // Create appropriate row for the table;
54
257k
    QLRow& row = vtable->Extend();
55
257k
    RETURN_NOT_OK(SetColumnValue(kKeyspaceName, ns_info->name(), &row));
56
257k
    RETURN_NOT_OK(SetColumnValue(kTableName, table->name(), &row));
57
58
    // Create appropriate flags entry.
59
257k
    QLValuePB flags_elem;
60
257k
    flags_elem.set_string_value("compound");
61
257k
    QLValuePB flags_set;
62
257k
    *flags_set.mutable_set_value()->add_elems() = flags_elem;
63
257k
    RETURN_NOT_OK(SetColumnValue(kFlags, flags_set, &row));
64
65
    // Create appropriate table uuid entry.
66
257k
    Uuid uuid;
67
    // Note: table id is in host byte order.
68
257k
    RETURN_NOT_OK(uuid.FromHexString(table->id()));
69
257k
    RETURN_NOT_OK(SetColumnValue(kId, uuid, &row));
70
71
    // Set the values for the table properties.
72
257k
    Schema schema;
73
257k
    RETURN_NOT_OK(table->GetSchema(&schema));
74
75
    // Adjusting precision, we use milliseconds internally, CQL uses seconds.
76
    // Sanity check, larger TTL values should be caught during analysis.
77
257k
    DCHECK_LE(schema.table_properties().DefaultTimeToLive(),
78
257k
              MonoTime::kMillisecondsPerSecond * std::numeric_limits<int32>::max());
79
257k
    int32_t cql_ttl = static_cast<int32_t>(
80
257k
        schema.table_properties().DefaultTimeToLive() / MonoTime::kMillisecondsPerSecond);
81
257k
    RETURN_NOT_OK(SetColumnValue(kDefaultTimeToLive, cql_ttl, &row));
82
83
257k
    if (schema.table_properties().HasNumTablets()) {
84
132
      int32_t num_tablets = schema.table_properties().num_tablets();
85
132
      RETURN_NOT_OK(SetColumnValue(kNumTablets, num_tablets, &row));
86
132
    }
87
88
257k
    QLValue txn;
89
257k
    txn.set_map_value();
90
257k
    txn.add_map_key()->set_string_value("enabled");
91
257k
    txn.add_map_value()->set_string_value(schema.table_properties().is_transactional() ?
92
255k
                                          "true" : "false");
93
257k
    RETURN_NOT_OK(SetColumnValue(kTransactions, txn.value(), &row));
94
257k
  }
95
96
15.5k
  return vtable;
97
15.5k
}
98
99
2.00k
Schema YQLTablesVTable::CreateSchema() const {
100
2.00k
  SchemaBuilder builder;
101
2.00k
  CHECK_OK(builder.AddHashKeyColumn(kKeyspaceName, QLType::Create(DataType::STRING)));
102
2.00k
  CHECK_OK(builder.AddKeyColumn(kTableName, QLType::Create(DataType::STRING)));
103
2.00k
  CHECK_OK(builder.AddColumn(kBloomFilterChance, QLType::Create(DataType::DOUBLE)));
104
2.00k
  CHECK_OK(builder.AddColumn(kCaching, QLType::CreateTypeMap(DataType::STRING, DataType::STRING)));
105
2.00k
  CHECK_OK(builder.AddColumn(kCdc, QLType::Create(DataType::BOOL)));
106
2.00k
  CHECK_OK(builder.AddColumn(kComment, QLType::Create(DataType::STRING)));
107
2.00k
  CHECK_OK(builder.AddColumn(kCompaction,
108
2.00k
                             QLType::CreateTypeMap(DataType::STRING, DataType::STRING)));
109
2.00k
  CHECK_OK(builder.AddColumn(kCompression,
110
2.00k
                             QLType::CreateTypeMap(DataType::STRING, DataType::STRING)));
111
2.00k
  CHECK_OK(builder.AddColumn(kCrcCheck, QLType::Create(DataType::DOUBLE)));
112
2.00k
  CHECK_OK(builder.AddColumn(kLocalReadRepair, QLType::Create(DataType::DOUBLE)));
113
2.00k
  CHECK_OK(builder.AddColumn(kDefaultTimeToLive, QLType::Create(DataType::INT32)));
114
2.00k
  CHECK_OK(builder.AddColumn(kExtensions,
115
2.00k
                             QLType::CreateTypeMap(DataType::STRING, DataType::BINARY)));
116
2.00k
  CHECK_OK(builder.AddColumn(kFlags, QLType::CreateTypeSet(DataType::STRING)));
117
2.00k
  CHECK_OK(builder.AddColumn(kGcGraceSeconds, QLType::Create(DataType::INT32)));
118
2.00k
  CHECK_OK(builder.AddColumn(kId, QLType::Create(DataType::UUID)));
119
2.00k
  CHECK_OK(builder.AddColumn(kMaxIndexInterval, QLType::Create(DataType::INT32)));
120
2.00k
  CHECK_OK(builder.AddColumn(kMemTableFlushPeriod, QLType::Create(DataType::INT32)));
121
2.00k
  CHECK_OK(builder.AddColumn(kMinIndexInterval, QLType::Create(DataType::INT32)));
122
2.00k
  CHECK_OK(builder.AddColumn(kReadRepairChance, QLType::Create(DataType::DOUBLE)));
123
2.00k
  CHECK_OK(builder.AddColumn(kSpeculativeRetry, QLType::Create(DataType::STRING)));
124
2.00k
  CHECK_OK(builder.AddColumn(kTransactions,
125
2.00k
                             QLType::CreateTypeMap(DataType::STRING, DataType::STRING)));
126
2.00k
  CHECK_OK(builder.AddColumn(kNumTablets, QLType::Create(DataType::INT32)));
127
2.00k
  return builder.Build();
128
2.00k
}
129
130
}  // namespace master
131
}  // namespace yb