xref: /aosp_15_r20/external/perfetto/src/trace_processor/db/column/types.h (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
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 #ifndef SRC_TRACE_PROCESSOR_DB_COLUMN_TYPES_H_
17 #define SRC_TRACE_PROCESSOR_DB_COLUMN_TYPES_H_
18 
19 #include <cstdint>
20 #include <limits>
21 #include <optional>
22 #include <utility>
23 #include <variant>
24 #include <vector>
25 
26 #include "perfetto/base/logging.h"
27 #include "perfetto/trace_processor/basic_types.h"
28 #include "src/trace_processor/containers/bit_vector.h"
29 #include "src/trace_processor/containers/row_map.h"
30 
31 namespace perfetto::trace_processor {
32 
33 using Range = RowMap::Range;
34 
35 // Result of calling Storage::SingleSearch function.
36 enum class SingleSearchResult {
37   kMatch,            // The specified row matches the constraint.
38   kNoMatch,          // The specified row does not matches the constraint.
39   kNeedsFullSearch,  // SingleSearch was unable to determine if the row meets
40                      // the crtiteria, a call to *Search is required.
41 };
42 
43 // Result of calling Storage::ValidateSearchResult function.
44 enum class SearchValidationResult {
45   kOk,       // It makes sense to run search
46   kAllData,  // Don't run search, all data passes the constraint.
47   kNoData    // Don't run search, no data passes the constraint.
48 };
49 
50 // Used for result of filtering, which is sometimes (for more optimised
51 // operations) a Range and BitVector otherwise. Stores a variant of Range and
52 // BitVector.
53 class RangeOrBitVector {
54  public:
RangeOrBitVector(Range range)55   explicit RangeOrBitVector(Range range) : val(range) {}
RangeOrBitVector(BitVector bv)56   explicit RangeOrBitVector(BitVector bv) : val(std::move(bv)) {}
57 
IsRange()58   bool IsRange() const { return std::holds_alternative<Range>(val); }
IsBitVector()59   bool IsBitVector() const { return std::holds_alternative<BitVector>(val); }
60 
TakeIfBitVector()61   BitVector TakeIfBitVector() && {
62     PERFETTO_DCHECK(IsBitVector());
63     return std::move(*std::get_if<BitVector>(&val));
64   }
TakeIfRange()65   Range TakeIfRange() && {
66     PERFETTO_DCHECK(IsRange());
67     return *std::get_if<Range>(&val);
68   }
69 
70  private:
71   std::variant<Range, BitVector> val;
72 };
73 
74 // Represents the possible filter operations on a column.
75 enum class FilterOp {
76   kEq,
77   kNe,
78   kGt,
79   kLt,
80   kGe,
81   kLe,
82   kIsNull,
83   kIsNotNull,
84   kGlob,
85   kRegex,
86 };
87 
88 // Represents a constraint on a column.
89 struct Constraint {
90   uint32_t col_idx;
91   FilterOp op;
92   SqlValue value;
93 };
94 
95 // Represents an order by operation on a column.
96 struct Order {
97   uint32_t col_idx;
98   bool desc = false;
99 };
100 
101 // Structured data used to determine what Trace Processor will query using
102 // CEngine.
103 struct Query {
104   enum class OrderType {
105     // Order should only be used for sorting.
106     kSort = 0,
107     // Distinct, `orders` signify which columns are supposed to be distinct and
108     // used for sorting.
109     kDistinctAndSort = 1,
110     // Distinct and `orders` signify only columns are supposed to be distinct,
111     // don't need additional sorting.
112     kDistinct = 2
113   };
114   OrderType order_type = OrderType::kSort;
115 
116   // Query constraints.
117   std::vector<Constraint> constraints;
118 
119   // Query order bys. Check distinct to know whether they should be used for
120   // sorting.
121   std::vector<Order> orders;
122 
123   // Bitflags indicating whether column is used.
124   //
125   // If the top bit is set, that indicates that the every column >= 64 is used.
126   uint64_t cols_used = std::numeric_limits<uint64_t>::max();
127 
128   // LIMIT value.
129   std::optional<uint32_t> limit;
130 
131   // OFFSET value. Can be "!= 0" only if `limit` has value.
132   uint32_t offset = 0;
133 
134   // Returns true if query should be used for fetching minimum or maximum value
135   // of singular column.
IsMinMaxQueryQuery136   inline bool IsMinMaxQuery() const {
137     // Order needs to specify the sorting.
138     return order_type == Query::OrderType::kSort
139            // There can be only one column for sorting.
140            && orders.size() == 1
141            // Limit has value 1
142            && limit.has_value() && *limit == 1;
143   }
144 
145   // Returns true if query should be used for sorting.
RequireSortQuery146   inline bool RequireSort() const {
147     return order_type != Query::OrderType::kDistinct && !orders.empty();
148   }
149 };
150 
151 // The enum type of the column.
152 // Public only to stop GCC complaining about templates being defined in a
153 // non-namespace scope (see ColumnTypeHelper below).
154 enum class ColumnType {
155   // Standard primitive types.
156   kInt32,
157   kUint32,
158   kInt64,
159   kDouble,
160   kString,
161 
162   // Types generated on the fly.
163   kId,
164 
165   // Types which don't have any data backing them.
166   kDummy,
167 };
168 
169 // Contains an index to an element in the chain and an opaque payload class
170 // which can be set to whatever the user of the chain requires.
171 struct Token {
172   // An index pointing to an element in this chain. Indicates the element
173   // at this index should be filtered.
174   uint32_t index;
175 
176   // An opaque value which can be set to some value meaningful to the
177   // caller. While the exact meaning of |payload| should not be depended
178   // upon, implementations are free to make assumptions that |payload| will
179   // be strictly monotonic.
180   uint32_t payload;
181 
182   struct PayloadComparator {
operatorToken::PayloadComparator183     bool operator()(const Token& a, const Token& b) {
184       return a.payload < b.payload;
185     }
186   };
187 };
188 
189 // Indicates the direction of the sort on a single chain.
190 enum class SortDirection {
191   kAscending,
192   kDescending,
193 };
194 
195 }  // namespace perfetto::trace_processor
196 
197 #endif  // SRC_TRACE_PROCESSOR_DB_COLUMN_TYPES_H_
198