xref: /aosp_15_r20/external/perfetto/src/trace_processor/perfetto_sql/intrinsics/operators/window_operator.h (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2018 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 #ifndef SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_OPERATORS_WINDOW_OPERATOR_H_
18 #define SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_OPERATORS_WINDOW_OPERATOR_H_
19 
20 #include <cstdint>
21 #include <limits>
22 #include <memory>
23 #include <string>
24 
25 #include "perfetto/ext/base/flat_hash_map.h"
26 #include "src/trace_processor/sqlite/bindings/sqlite_module.h"
27 #include "src/trace_processor/sqlite/module_lifecycle_manager.h"
28 
29 namespace perfetto::trace_processor {
30 
31 class TraceStorage;
32 
33 // Operator table which can emit spans of a configurable duration.
34 struct WindowOperatorModule : sqlite::Module<WindowOperatorModule> {
35   // Defines the data to be generated by the table.
36   enum FilterType {
37     // Returns all the spans.
38     kReturnAll = 0,
39     // Only returns the first span of the table. Useful for UPDATE operations.
40     kReturnFirst = 1,
41   };
42   struct State {
43     int64_t quantum = 0;
44     int64_t window_start = 0;
45 
46     // max of int64_t because SQLite technically only supports int64s and not
47     // uint64s.
48     int64_t window_dur = std::numeric_limits<int64_t>::max();
49   };
50   struct Context {
51     sqlite::ModuleStateManager<WindowOperatorModule> manager;
52   };
53   struct Vtab : sqlite::Module<WindowOperatorModule>::Vtab {
54     sqlite::ModuleStateManager<WindowOperatorModule>::PerVtabState* state;
55   };
56   struct Cursor : sqlite::Module<WindowOperatorModule>::Cursor {
57     int64_t window_end = 0;
58     int64_t step_size = 0;
59 
60     int64_t current_ts = 0;
61     int64_t quantum_ts = 0;
62     int64_t row_id = 0;
63 
64     FilterType filter_type = FilterType::kReturnAll;
65   };
66 
67   static constexpr auto kType = kCreateOnly;
68   static constexpr bool kDoesOverloadFunctions = false;
69 
70   static int Create(sqlite3*,
71                     void*,
72                     int,
73                     const char* const*,
74                     sqlite3_vtab**,
75                     char**);
76   static int Destroy(sqlite3_vtab*);
77 
78   static int Connect(sqlite3*,
79                      void*,
80                      int,
81                      const char* const*,
82                      sqlite3_vtab**,
83                      char**);
84   static int Disconnect(sqlite3_vtab*);
85 
86   static int BestIndex(sqlite3_vtab*, sqlite3_index_info*);
87 
88   static int Open(sqlite3_vtab*, sqlite3_vtab_cursor**);
89   static int Close(sqlite3_vtab_cursor*);
90 
91   static int Filter(sqlite3_vtab_cursor*,
92                     int,
93                     const char*,
94                     int,
95                     sqlite3_value**);
96   static int Next(sqlite3_vtab_cursor*);
97   static int Eof(sqlite3_vtab_cursor*);
98   static int Column(sqlite3_vtab_cursor*, sqlite3_context*, int);
99   static int Rowid(sqlite3_vtab_cursor*, sqlite_int64*);
100 
101   static int Update(sqlite3_vtab*, int, sqlite3_value**, sqlite_int64*);
102 
103   // This needs to happen at the end as it depends on the functions
104   // defined above.
105   static constexpr sqlite3_module kModule = CreateModule();
106 };
107 
108 }  // namespace perfetto::trace_processor
109 
110 #endif  // SRC_TRACE_PROCESSOR_PERFETTO_SQL_INTRINSICS_OPERATORS_WINDOW_OPERATOR_H_
111