1 /*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "src/trace_processor/perfetto_sql/intrinsics/table_functions/table_info.h"
18
19 #include <cstdint>
20 #include <memory>
21 #include <string>
22 #include <utility>
23 #include <vector>
24
25 #include "perfetto/base/logging.h"
26 #include "perfetto/base/status.h"
27 #include "perfetto/ext/base/status_or.h"
28 #include "perfetto/trace_processor/basic_types.h"
29 #include "src/trace_processor/containers/string_pool.h"
30 #include "src/trace_processor/db/column.h"
31 #include "src/trace_processor/db/column/types.h"
32 #include "src/trace_processor/db/runtime_table.h"
33 #include "src/trace_processor/db/table.h"
34 #include "src/trace_processor/perfetto_sql/engine/perfetto_sql_engine.h"
35 #include "src/trace_processor/perfetto_sql/intrinsics/table_functions/tables_py.h"
36
37 namespace perfetto::trace_processor {
38 namespace tables {
39
40 PerfettoTableInfoTable::~PerfettoTableInfoTable() = default;
41
42 } // namespace tables
43
44 namespace {
45
46 using TableInfoTable = tables::PerfettoTableInfoTable;
47
GetColInfoRows(const std::vector<ColumnLegacy> & cols,StringPool * pool)48 std::vector<TableInfoTable::Row> GetColInfoRows(
49 const std::vector<ColumnLegacy>& cols,
50 StringPool* pool) {
51 std::vector<TableInfoTable::Row> rows;
52 for (const ColumnLegacy& col : cols) {
53 if (col.IsHidden()) {
54 continue;
55 }
56 TableInfoTable::Row row;
57 row.name = pool->InternString(col.name());
58 switch (col.col_type()) {
59 case ColumnType::kString:
60 row.col_type = pool->InternString("string");
61 break;
62 case ColumnType::kInt64:
63 row.col_type = pool->InternString("int64");
64 break;
65 case ColumnType::kInt32:
66 row.col_type = pool->InternString("int32");
67 break;
68 case ColumnType::kUint32:
69 row.col_type = pool->InternString("uint32");
70 break;
71 case ColumnType::kDouble:
72 row.col_type = pool->InternString("double");
73 break;
74 case ColumnType::kId:
75 row.col_type = pool->InternString("id");
76 break;
77 case ColumnType::kDummy:
78 row.col_type = pool->InternString("dummy");
79 break;
80 }
81 if (col.IsSetId()) {
82 row.col_type = pool->InternString("set id");
83 }
84 row.nullable = col.IsNullable();
85 row.sorted = col.IsSorted();
86 rows.push_back(row);
87 }
88 return rows;
89 }
90
91 } // namespace
92
TableInfo(StringPool * string_pool,const PerfettoSqlEngine * engine)93 TableInfo::TableInfo(StringPool* string_pool, const PerfettoSqlEngine* engine)
94 : string_pool_(string_pool), engine_(engine) {}
95
ComputeTable(const std::vector<SqlValue> & arguments)96 base::StatusOr<std::unique_ptr<Table>> TableInfo::ComputeTable(
97 const std::vector<SqlValue>& arguments) {
98 PERFETTO_CHECK(arguments.size() == 1);
99 if (arguments[0].type != SqlValue::kString) {
100 return base::ErrStatus("perfetto_table_info takes table name as a string.");
101 }
102
103 std::string table_name = arguments[0].AsString();
104 auto table = std::make_unique<TableInfoTable>(string_pool_);
105 auto table_name_id = string_pool_->InternString(table_name.c_str());
106
107 // Find table
108 const Table* t = engine_->GetTableOrNull(table_name);
109 if (t) {
110 for (auto& row : GetColInfoRows(t->columns(), string_pool_)) {
111 row.table_name = table_name_id;
112 table->Insert(row);
113 }
114 return std::unique_ptr<Table>(std::move(table));
115 }
116 return base::ErrStatus("Perfetto table '%s' not found.", table_name.c_str());
117 }
118
CreateSchema()119 Table::Schema TableInfo::CreateSchema() {
120 return TableInfoTable::ComputeStaticSchema();
121 }
122
TableName()123 std::string TableInfo::TableName() {
124 return TableInfoTable::Name();
125 }
126
EstimateRowCount()127 uint32_t TableInfo::EstimateRowCount() {
128 return 1;
129 }
130
131 } // namespace perfetto::trace_processor
132