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