• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 #ifndef SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TYPES_ROW_DATAFRAME_H_
18 #define SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TYPES_ROW_DATAFRAME_H_
19 
20 #include <algorithm>
21 #include <cstdint>
22 #include <optional>
23 #include <string>
24 #include <vector>
25 
26 #include "src/trace_processor/perfetto_sql/intrinsics/types/array.h"
27 #include "src/trace_processor/perfetto_sql/intrinsics/types/value.h"
28 
29 namespace perfetto::trace_processor::perfetto_sql {
30 
31 // Data structure to allow easy exchange of "table-like" data between SQL and
32 // C++ code. Allows fast lookup of rows by id (if an id column exists).
33 struct RowDataframe {
34   perfetto_sql::StringArray column_names;
35   std::vector<uint32_t> id_to_cell_index;
36   // Cell = a value at a row + column index.
37   std::vector<perfetto_sql::Value> cells;
38   std::optional<uint32_t> id_column_index;
39 
RowForIdRowDataframe40   const perfetto_sql::Value* RowForId(uint32_t id) const {
41     return cells.data() + id_to_cell_index[id];
42   }
43 
FindColumnWithNameRowDataframe44   std::optional<uint32_t> FindColumnWithName(const std::string& name) {
45     auto it = std::find(column_names.begin(), column_names.end(), name);
46     return it == column_names.end() ? std::nullopt
47                                     : std::make_optional(static_cast<uint32_t>(
48                                           it - column_names.begin()));
49   }
50 
sizeRowDataframe51   uint32_t size() const {
52     return static_cast<uint32_t>(cells.size() / column_names.size());
53   }
54 };
55 
56 }  // namespace perfetto::trace_processor::perfetto_sql
57 
58 #endif  // SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_TYPES_ROW_DATAFRAME_H_
59