• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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_GENERATOR_STRUCTURED_QUERY_GENERATOR_H_
18 #define SRC_TRACE_PROCESSOR_PERFETTO_SQL_GENERATOR_STRUCTURED_QUERY_GENERATOR_H_
19 
20 #include <cstddef>
21 #include <cstdint>
22 #include <memory>
23 #include <string>
24 #include <string_view>
25 #include <vector>
26 
27 #include "perfetto/base/status.h"
28 #include "perfetto/ext/base/flat_hash_map.h"
29 #include "perfetto/ext/base/status_or.h"
30 #include "protos/perfetto/perfetto_sql/structured_query.pbzero.h"
31 
32 namespace perfetto::trace_processor::perfetto_sql::generator {
33 
34 using StructuredQuery = protos::pbzero::PerfettoSqlStructuredQuery;
35 
36 // Allows conversion of a `PerfettoSqlStructuredQuery` proto to a PerfettoSQL
37 // query with support for shared queries.
38 class StructuredQueryGenerator {
39  public:
40   struct Query {
41     std::string id;
42     std::string table_name;
43     std::string sql;
44   };
45   struct QueryProto {
46     std::unique_ptr<uint8_t[]> data;
47     size_t size;
48   };
49 
50   // Generates an SQL query from the given StructuredQuery proto.
51   //
52   // This query implicitly assumes that all SQL modules indicated by
53   // `referenced_modules` have been included and all shared queries indicated
54   // by `referenced_shared_queries` are available, either by tables or views or
55   // as a common table expression (CTE).
56   base::StatusOr<std::string> Generate(const uint8_t* data, size_t size);
57 
58   // Generates an SQL query for a query with the given id. The query should have
59   // been added with `AddQuery`
60   //
61   // See `Generate` above for expectations of this function
62   base::StatusOr<std::string> GenerateById(const std::string& id);
63 
64   // Adds a query to the internal state to reference in all future calls to
65   // `Generate*`.
66   base::Status AddQuery(const uint8_t* data, size_t size);
67 
68   // Computes all the PerfettoSQL modules referenced by any past calls to
69   // `Generate` and `AddSharedQuery`.
70   std::vector<std::string> ComputeReferencedModules() const;
71 
72   // Computes all the PerfettoSQL preambles referenced by any past calls to
73   // `Generate` and `AddSharedQuery`.
74   // Preamble strings should be executed before executing the result of call
75   // to `Generate` to ensure it can be run safely.
ComputePreambles()76   const std::vector<std::string>& ComputePreambles() const {
77     return preambles_;
78   }
79 
80   // Returns a summary of all the shared queries which have been referenced
81   // by any past calls to `Generate`.
referenced_queries()82   std::vector<Query> referenced_queries() const { return referenced_queries_; }
83 
84  private:
85   base::FlatHashMap<std::string, QueryProto> query_protos_;
86   std::vector<Query> referenced_queries_;
87 
88   // We don't have FlatHashSet so just (ab)use FlatHashMap by storing a noop
89   // value.
90   base::FlatHashMap<std::string, std::nullptr_t> referenced_modules_;
91   std::vector<std::string> preambles_;
92 };
93 
94 }  // namespace perfetto::trace_processor::perfetto_sql::generator
95 
96 #endif  // SRC_TRACE_PROCESSOR_PERFETTO_SQL_GENERATOR_STRUCTURED_QUERY_GENERATOR_H_
97