OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ResultSetRecycler.h
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
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 #pragma once
18 
20 #include "QueryEngine/QueryHint.h"
22 
24  ResultSetMetaInfo(const std::unordered_set<size_t> input_table_infos) {
25  input_table_keys.insert(input_table_infos.begin(), input_table_infos.end());
26  };
27 
28  void keepTargetExprs(std::vector<std::shared_ptr<Analyzer::Expr>>& in_target_exprs) {
29  for (const auto& expr : in_target_exprs) {
30  target_exprs.push_back(expr->get_shared_ptr());
31  }
32  }
33 
34  std::vector<std::shared_ptr<Analyzer::Expr>>& getTargetExprs() { return target_exprs; }
35 
36  std::unordered_set<size_t> input_table_keys;
37  std::vector<std::shared_ptr<Analyzer::Expr>> target_exprs;
38 };
39 
40 class ResultSetRecycler : public DataRecycler<ResultSetPtr, ResultSetMetaInfo> {
41  public:
46  0) {}
47 
49  QueryPlanHash key,
50  CacheItemType item_type,
51  DeviceIdentifier device_identifier,
52  std::optional<ResultSetMetaInfo> meta_info = std::nullopt) override;
53 
54  std::optional<std::vector<TargetMetaInfo>> getOutputMetaInfo(QueryPlanHash key);
55 
57  ResultSetPtr item_ptr,
58  CacheItemType item_type,
59  DeviceIdentifier device_identifier,
60  size_t item_size,
61  size_t compute_time,
62  std::optional<ResultSetMetaInfo> meta_info = std::nullopt) override;
63 
64  // nothing to do with resultset recycler
65  void initCache() override {}
66 
67  void clearCache() override;
68 
69  void markCachedItemAsDirty(size_t table_key,
70  std::unordered_set<QueryPlanHash>& key_set,
71  CacheItemType item_type,
72  DeviceIdentifier device_identifier) override;
73 
74  std::string toString() const override;
75 
76  std::tuple<QueryPlanHash, ResultSetPtr, std::optional<ResultSetMetaInfo>>
77  getCachedResultSetWithoutCacheKey(std::set<size_t>& visited,
78  DeviceIdentifier device_identifier);
79 
80  void addQueryPlanDagForTableKeys(size_t hashed_query_plan_dag,
81  const std::unordered_set<size_t>& table_keys,
82  std::lock_guard<std::mutex>& lock);
83 
84  std::optional<std::unordered_set<size_t>> getMappedQueryPlanDagsWithTableKey(
85  size_t table_key) const;
86 
87  void removeTableKeyInfoFromQueryPlanDagMap(size_t table_key);
88 
89  bool hasItemInCache(QueryPlanHash key);
90 
91  std::vector<std::shared_ptr<Analyzer::Expr>>& getTargetExprs(QueryPlanHash key) const;
92 
95  }
96 
97  private:
98  bool hasItemInCache(
99  QueryPlanHash key,
100  CacheItemType item_type,
101  DeviceIdentifier device_identifier,
102  std::lock_guard<std::mutex>& lock,
103  std::optional<ResultSetMetaInfo> meta_info = std::nullopt) const override;
104 
105  void removeItemFromCache(
106  QueryPlanHash key,
107  CacheItemType item_type,
108  DeviceIdentifier device_identifier,
109  std::lock_guard<std::mutex>& lock,
110  std::optional<ResultSetMetaInfo> meta_info = std::nullopt) override;
111 
113  CacheItemType item_type,
114  DeviceIdentifier device_identifier,
115  size_t required_size,
116  std::lock_guard<std::mutex>& lock,
117  std::optional<ResultSetMetaInfo> meta_info = std::nullopt) override;
118 
119  // keep all table keys referenced to compute cached resultset
120  std::unordered_map<size_t, std::unordered_set<size_t>> table_key_to_query_plan_dag_map_;
121 };
void cleanupCacheForInsertion(CacheItemType item_type, DeviceIdentifier device_identifier, size_t required_size, std::lock_guard< std::mutex > &lock, std::optional< ResultSetMetaInfo > meta_info=std::nullopt) override
size_t DeviceIdentifier
Definition: DataRecycler.h:129
std::vector< std::shared_ptr< Analyzer::Expr > > & getTargetExprs(QueryPlanHash key) const
CacheMetricTracker & getMetricTracker(CacheItemType item_type)
Definition: DataRecycler.h:654
std::unordered_map< size_t, std::unordered_set< size_t > > table_key_to_query_plan_dag_map_
std::vector< std::shared_ptr< Analyzer::Expr > > target_exprs
std::optional< std::vector< TargetMetaInfo > > getOutputMetaInfo(QueryPlanHash key)
std::vector< std::shared_ptr< Analyzer::Expr > > & getTargetExprs()
void addQueryPlanDagForTableKeys(size_t hashed_query_plan_dag, const std::unordered_set< size_t > &table_keys, std::lock_guard< std::mutex > &lock)
void clearCache() override
std::shared_ptr< ResultSet > ResultSetPtr
void initCache() override
ResultSetMetaInfo(const std::unordered_set< size_t > input_table_infos)
std::optional< std::unordered_set< size_t > > getMappedQueryPlanDagsWithTableKey(size_t table_key) const
CacheItemType
Definition: DataRecycler.h:38
std::tuple< QueryPlanHash, ResultSetPtr, std::optional< ResultSetMetaInfo > > getCachedResultSetWithoutCacheKey(std::set< size_t > &visited, DeviceIdentifier device_identifier)
std::string toString() const override
void putItemToCache(QueryPlanHash key, ResultSetPtr item_ptr, CacheItemType item_type, DeviceIdentifier device_identifier, size_t item_size, size_t compute_time, std::optional< ResultSetMetaInfo > meta_info=std::nullopt) override
void removeItemFromCache(QueryPlanHash key, CacheItemType item_type, DeviceIdentifier device_identifier, std::lock_guard< std::mutex > &lock, std::optional< ResultSetMetaInfo > meta_info=std::nullopt) override
size_t QueryPlanHash
CacheMetricTracker & getResultSetRecyclerMetricTracker()
bool hasItemInCache(QueryPlanHash key)
void markCachedItemAsDirty(size_t table_key, std::unordered_set< QueryPlanHash > &key_set, CacheItemType item_type, DeviceIdentifier device_identifier) override
virtual ResultSetPtr getItemFromCache(QueryPlanHash key, CacheItemType item_type, DeviceIdentifier device_identifier, std::optional< ResultSetMetaInfo > meta_info=std::nullopt)=0
std::unordered_set< size_t > input_table_keys
void keepTargetExprs(std::vector< std::shared_ptr< Analyzer::Expr >> &in_target_exprs)
Execution unit for relational algebra. It&#39;s a low-level description of any relational algebra operati...
void removeTableKeyInfoFromQueryPlanDagMap(size_t table_key)