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/db/column/utils.h"
18
19 #include <cmath>
20 #include <cstdint>
21 #include <functional>
22 #include <optional>
23 #include <utility>
24 #include <vector>
25
26 #include "perfetto/base/logging.h"
27 #include "perfetto/trace_processor/basic_types.h"
28 #include "src/trace_processor/containers/row_map.h"
29 #include "src/trace_processor/db/column/data_layer.h"
30 #include "src/trace_processor/db/column/types.h"
31
32 namespace perfetto::trace_processor::column::utils {
33
CompareIntColumnWithDouble(FilterOp op,SqlValue * sql_val)34 SearchValidationResult CompareIntColumnWithDouble(FilterOp op,
35 SqlValue* sql_val) {
36 double double_val = sql_val->AsDouble();
37 if (std::equal_to<>()(
38 double_val, static_cast<double>(static_cast<uint32_t>(double_val)))) {
39 // If double is the same as uint32_t, we should just "cast" the |sql_val|
40 // to be treated as long.
41 *sql_val = SqlValue::Long(static_cast<int64_t>(double_val));
42 return SearchValidationResult::kOk;
43 }
44 // Logic for when the value is a real double.
45 switch (op) {
46 case FilterOp::kEq:
47 return SearchValidationResult::kNoData;
48 case FilterOp::kNe:
49 return SearchValidationResult::kAllData;
50
51 case FilterOp::kLe:
52 case FilterOp::kGt:
53 *sql_val = SqlValue::Long(static_cast<int64_t>(std::floor(double_val)));
54 return SearchValidationResult::kOk;
55
56 case FilterOp::kLt:
57 case FilterOp::kGe:
58 *sql_val = SqlValue::Long(static_cast<int64_t>(std::ceil(double_val)));
59 return SearchValidationResult::kOk;
60
61 case FilterOp::kIsNotNull:
62 case FilterOp::kIsNull:
63 case FilterOp::kGlob:
64 case FilterOp::kRegex:
65 PERFETTO_FATAL("Invalid filter operation");
66 }
67 PERFETTO_FATAL("For GCC");
68 }
69
ToIndexVectorForTests(RangeOrBitVector & r_or_bv)70 std::vector<uint32_t> ToIndexVectorForTests(RangeOrBitVector& r_or_bv) {
71 RowMap rm;
72 if (r_or_bv.IsBitVector()) {
73 rm = RowMap(std::move(r_or_bv).TakeIfBitVector());
74 } else {
75 Range range = std::move(r_or_bv).TakeIfRange();
76 rm = RowMap(range.start, range.end);
77 }
78 return rm.GetAllIndices();
79 }
80
ExtractPayloadForTesting(const DataLayerChain::Indices & indices)81 std::vector<uint32_t> ExtractPayloadForTesting(
82 const DataLayerChain::Indices& indices) {
83 std::vector<uint32_t> payload;
84 payload.reserve(indices.tokens.size());
85 for (const auto& token : indices.tokens) {
86 payload.push_back(token.payload);
87 }
88 return payload;
89 }
90
ExtractPayloadForTesting(std::vector<Token> & tokens)91 std::vector<uint32_t> ExtractPayloadForTesting(std::vector<Token>& tokens) {
92 std::vector<uint32_t> payload;
93 payload.reserve(tokens.size());
94 for (const auto& token : tokens) {
95 payload.push_back(token.payload);
96 }
97 return payload;
98 }
99
CanReturnEarly(SearchValidationResult res,Range range)100 std::optional<Range> CanReturnEarly(SearchValidationResult res, Range range) {
101 switch (res) {
102 case SearchValidationResult::kOk:
103 return std::nullopt;
104 case SearchValidationResult::kAllData:
105 return range;
106 case SearchValidationResult::kNoData:
107 return Range();
108 }
109 PERFETTO_FATAL("For GCC");
110 }
111
CanReturnEarly(SearchValidationResult res,uint32_t indices_size)112 std::optional<Range> CanReturnEarly(SearchValidationResult res,
113 uint32_t indices_size) {
114 switch (res) {
115 case SearchValidationResult::kOk:
116 return std::nullopt;
117 case SearchValidationResult::kAllData:
118 return Range(0, indices_size);
119 case SearchValidationResult::kNoData:
120 return Range();
121 }
122 PERFETTO_FATAL("For GCC");
123 }
124
CanReturnEarly(SearchValidationResult res,DataLayerChain::Indices & indices)125 bool CanReturnEarly(SearchValidationResult res,
126 DataLayerChain::Indices& indices) {
127 switch (res) {
128 case SearchValidationResult::kOk:
129 return false;
130 case SearchValidationResult::kAllData:
131 return true;
132 case SearchValidationResult::kNoData:
133 indices.tokens.clear();
134 return true;
135 }
136 PERFETTO_FATAL("For GCC");
137 }
138
139 } // namespace perfetto::trace_processor::column::utils
140