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/selector_overlay.h"
18
19 #include <cstdint>
20 #include <utility>
21 #include <vector>
22
23 #include "data_layer.h"
24 #include "perfetto/trace_processor/basic_types.h"
25 #include "src/trace_processor/containers/bit_vector.h"
26 #include "src/trace_processor/db/column/fake_storage.h"
27 #include "src/trace_processor/db/column/numeric_storage.h"
28 #include "src/trace_processor/db/column/types.h"
29 #include "src/trace_processor/db/column/utils.h"
30 #include "test/gtest_and_gmock.h"
31
32 namespace perfetto::trace_processor::column {
33 namespace {
34
35 using testing::ElementsAre;
36 using testing::IsEmpty;
37
38 using Indices = DataLayerChain::Indices;
39 using OrderedIndices = DataLayerChain::OrderedIndices;
40
TEST(SelectorOverlay,SingleSearch)41 TEST(SelectorOverlay, SingleSearch) {
42 BitVector selector{0, 1, 1, 0, 0, 1, 1, 0};
43 auto fake = FakeStorageChain::SearchSubset(8, Range(2, 5));
44 SelectorOverlay storage(&selector);
45 auto chain = storage.MakeChain(std::move(fake));
46
47 ASSERT_EQ(chain->SingleSearch(FilterOp::kGe, SqlValue::Long(0u), 1),
48 SingleSearchResult::kMatch);
49 ASSERT_EQ(chain->SingleSearch(FilterOp::kGe, SqlValue::Long(0u), 0),
50 SingleSearchResult::kNoMatch);
51 }
52
TEST(SelectorOverlay,SearchAll)53 TEST(SelectorOverlay, SearchAll) {
54 BitVector selector{0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1};
55 auto fake = FakeStorageChain::SearchAll(10);
56 SelectorOverlay storage(&selector);
57 auto chain = storage.MakeChain(std::move(fake));
58
59 auto res = chain->Search(FilterOp::kGe, SqlValue::Long(0u), Range(1, 4));
60 ASSERT_THAT(utils::ToIndexVectorForTests(res), ElementsAre(1u, 2u, 3u));
61 }
62
TEST(SelectorOverlay,SearchNone)63 TEST(SelectorOverlay, SearchNone) {
64 BitVector selector{0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1};
65 auto fake = FakeStorageChain::SearchNone(10);
66 SelectorOverlay storage(&selector);
67 auto chain = storage.MakeChain(std::move(fake));
68
69 auto res = chain->Search(FilterOp::kGe, SqlValue::Long(0u), Range(1, 4));
70 ASSERT_THAT(utils::ToIndexVectorForTests(res), IsEmpty());
71 }
72
TEST(SelectorOverlay,SearchLimited)73 TEST(SelectorOverlay, SearchLimited) {
74 BitVector selector{0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1};
75 auto fake = FakeStorageChain::SearchSubset(10, Range(4, 5));
76 SelectorOverlay storage(&selector);
77 auto chain = storage.MakeChain(std::move(fake));
78
79 auto res = chain->Search(FilterOp::kGe, SqlValue::Long(0u), Range(1, 5));
80 ASSERT_THAT(utils::ToIndexVectorForTests(res), ElementsAre(2u));
81 }
82
TEST(SelectorOverlay,SearchBitVector)83 TEST(SelectorOverlay, SearchBitVector) {
84 BitVector selector{0, 1, 1, 0, 0, 1, 1, 0};
85 auto fake =
86 FakeStorageChain::SearchSubset(8, BitVector({0, 1, 0, 1, 0, 1, 0, 0}));
87 SelectorOverlay storage(&selector);
88 auto chain = storage.MakeChain(std::move(fake));
89
90 auto res = chain->Search(FilterOp::kGe, SqlValue::Long(0u), Range(0, 4));
91 ASSERT_THAT(utils::ToIndexVectorForTests(res), ElementsAre(0, 2));
92 }
93
TEST(SelectorOverlay,IndexSearch)94 TEST(SelectorOverlay, IndexSearch) {
95 BitVector selector{0, 1, 1, 0, 0, 1, 1, 0};
96 auto fake =
97 FakeStorageChain::SearchSubset(8, BitVector({0, 1, 0, 1, 0, 1, 0, 0}));
98 SelectorOverlay storage(&selector);
99 auto chain = storage.MakeChain(std::move(fake));
100
101 auto indices = Indices::CreateWithIndexPayloadForTesting(
102 {1u, 0u, 3u}, Indices::State::kNonmonotonic);
103 chain->IndexSearch(FilterOp::kGe, SqlValue::Long(0u), indices);
104 ASSERT_THAT(utils::ExtractPayloadForTesting(indices), ElementsAre(1u));
105 }
106
TEST(SelectorOverlay,OrderedIndexSearch)107 TEST(SelectorOverlay, OrderedIndexSearch) {
108 std::vector<uint32_t> numeric_data{1, 0, 0, 1, 1};
109 NumericStorage<uint32_t> numeric(&numeric_data, ColumnType::kUint32, false);
110
111 BitVector selector{1, 0, 1, 0, 1};
112 SelectorOverlay storage(&selector);
113 auto chain = storage.MakeChain(numeric.MakeChain());
114
115 std::vector<uint32_t> table_idx{1u, 0u, 2u};
116 Range res = chain->OrderedIndexSearch(
117 FilterOp::kGe, SqlValue::Long(1u),
118 OrderedIndices{table_idx.data(), static_cast<uint32_t>(table_idx.size()),
119 Indices::State::kNonmonotonic});
120 ASSERT_EQ(res.start, 1u);
121 ASSERT_EQ(res.end, 3u);
122 }
123
TEST(SelectorOverlay,StableSort)124 TEST(SelectorOverlay, StableSort) {
125 std::vector<uint32_t> numeric_data{3, 1, 0, 0, 2, 4, 3, 4};
126 NumericStorage<uint32_t> numeric(&numeric_data, ColumnType::kUint32, false);
127
128 BitVector selector{0, 1, 0, 1, 1, 1, 1, 1};
129 SelectorOverlay overlay(&selector);
130 auto chain = overlay.MakeChain(numeric.MakeChain());
131
132 auto make_tokens = []() {
133 return std::vector{
134 Token{0, 0}, Token{1, 1}, Token{2, 2},
135 Token{3, 3}, Token{4, 4}, Token{5, 5},
136 };
137 };
138 {
139 auto tokens = make_tokens();
140 chain->StableSort(tokens.data(), tokens.data() + tokens.size(),
141 SortDirection::kAscending);
142 ASSERT_THAT(utils::ExtractPayloadForTesting(tokens),
143 ElementsAre(1, 0, 2, 4, 3, 5));
144 }
145 {
146 auto tokens = make_tokens();
147 chain->StableSort(tokens.data(), tokens.data() + tokens.size(),
148 SortDirection::kDescending);
149 ASSERT_THAT(utils::ExtractPayloadForTesting(tokens),
150 ElementsAre(3, 5, 4, 2, 0, 1));
151 }
152 }
153
154 } // namespace
155 } // namespace perfetto::trace_processor::column
156