xref: /aosp_15_r20/external/perfetto/src/trace_processor/sqlite/bindings/sqlite_window_function.h (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
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 #ifndef SRC_TRACE_PROCESSOR_SQLITE_BINDINGS_SQLITE_WINDOW_FUNCTION_H_
18 #define SRC_TRACE_PROCESSOR_SQLITE_BINDINGS_SQLITE_WINDOW_FUNCTION_H_
19 
20 struct sqlite3_context;
21 struct sqlite3_value;
22 
23 namespace perfetto::trace_processor {
24 
25 // Prototype for a window function which can be registered with SQLite.
26 //
27 // See https://www.sqlite.org/windowfunctions.html#udfwinfunc for details on how
28 // to implement the methods of this class.
29 class SqliteWindowFunction {
30  public:
31   // The type of the context object which will be passed to the function.
32   // Can be redefined in any sub-classes to override the context.
33   using Context = void;
34 
35   // The xStep function which will be executed by SQLite to add a row of values
36   // to the current window.
37   //
38   // Implementations MUST define this function themselves; this function is
39   // declared but *not* defined so linker errors will be thrown if not defined.
40   static void Step(sqlite3_context*, int argc, sqlite3_value** argv);
41 
42   // The xStep function which will be executed by SQLite to remove a row of
43   // values from the current window.
44   //
45   // Implementations MUST define this function themselves; this function is
46   // declared but *not* defined so linker errors will be thrown if not defined.
47   static void Inverse(sqlite3_context* ctx, int argc, sqlite3_value** argv);
48 
49   // The xValue function which will be executed by SQLite to obtain the current
50   // value of the aggregate.
51   //
52   // Implementations MUST define this function themselves; this function is
53   // declared but *not* defined so linker errors will be thrown if not defined.
54   static void Value(sqlite3_context* ctx);
55 
56   // The xInverse function which will be executed by SQLite to obtain the
57   // current value of the aggregate *and* free all resources allocated by
58   // previous calls to Step, Inverse and Value.
59   //
60   // Implementations MUST define this function themselves; this function is
61   // declared but *not* defined so linker errors will be thrown if not defined.
62   static void Final(sqlite3_context* ctx);
63 };
64 
65 }  // namespace perfetto::trace_processor
66 
67 #endif  // SRC_TRACE_PROCESSOR_SQLITE_BINDINGS_SQLITE_WINDOW_FUNCTION_H_
68