• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/null_overlay.h"
18 
19 #include "src/trace_processor/db/storage_variants.h"
20 
21 namespace perfetto {
22 namespace trace_processor {
23 namespace column {
24 
Filter(FilterOp op,SqlValue sql_val,RowMap & rm) const25 void NullOverlay::Filter(FilterOp op, SqlValue sql_val, RowMap& rm) const {
26   if (op == FilterOp::kIsNull) {
27     rm.Intersect(RowMap(null_bv_->Not()));
28     return;
29   }
30   if (op == FilterOp::kIsNotNull) {
31     rm.Intersect(RowMap(null_bv_->Copy()));
32     return;
33   }
34 
35   // Row map for filtered data, not the size of whole column.
36   RowMap filtered_data_rm(0, null_bv_->CountSetBits());
37   inner_->Filter(op, sql_val, filtered_data_rm);
38 
39   // Select only rows that were not filtered out from null BitVector and
40   // intersect it with RowMap&.
41   rm.Intersect(RowMap(null_bv_->Copy()).SelectRows(filtered_data_rm));
42 }
43 
StableSort(uint32_t * rows,uint32_t rows_size) const44 void NullOverlay::StableSort(uint32_t* rows, uint32_t rows_size) const {
45   uint32_t count_set_bits = null_bv_->CountSetBits();
46 
47   std::vector<uint32_t> non_null_rows(count_set_bits);
48   std::vector<uint32_t> storage_to_rows(count_set_bits);
49 
50   // Saving the map from `out` index to `storage` index gives us free `IsSet()`
51   // function, which would be very expensive otherwise.
52   for (auto it = null_bv_->IterateSetBits(); it; it.Next()) {
53     storage_to_rows[it.ordinal()] = it.index();
54   }
55 
56   uint32_t cur_non_null_id = 0;
57   uint32_t cur_null_id = 0;
58 
59   // Sort elements into null and non null.
60   for (uint32_t i = 0; i < rows_size; ++i) {
61     uint32_t row_idx = rows[i];
62     auto it = std::lower_bound(storage_to_rows.begin(), storage_to_rows.end(),
63                                row_idx);
64 
65     // This condition holds if the row is null.
66     if (it == storage_to_rows.end() || *it != row_idx) {
67       // We can override the out because we already used this data.
68       rows[cur_null_id++] = row_idx;
69       continue;
70     }
71 
72     uint32_t non_null_idx =
73         static_cast<uint32_t>(std::distance(storage_to_rows.begin(), it));
74     non_null_rows[cur_non_null_id++] = non_null_idx;
75   }
76 
77   // Sort storage and translate them into `rows` indices.
78   inner_->StableSort(non_null_rows.data(), count_set_bits);
79   uint32_t set_rows_offset = null_bv_->size() - count_set_bits;
80   for (uint32_t i = 0; i < count_set_bits; ++i) {
81     rows[set_rows_offset + i] = storage_to_rows[non_null_rows[i]];
82   }
83 }
84 
85 }  // namespace column
86 }  // namespace trace_processor
87 }  // namespace perfetto
88