OmniSciDB  16c4e035a1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
HashTablePropertyRecycler.h
Go to the documentation of this file.
1 /*
2  * Copyright 2021 OmniSci, 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 
19 #include "DataRecycler.h"
20 #include "QueryEngine/QueryHint.h"
21 
24 
26  HashTableProperty(std::optional<HashTableLayoutType> layout_in,
27  std::optional<HashTableHashingType> hashing_in)
28  : layout(layout_in), hashing(hashing_in) {}
29 
30  std::optional<HashTableLayoutType> layout;
31  std::optional<HashTableHashingType> hashing;
32 };
33 
35  : public DataRecycler<std::optional<HashTableProperty>, EMPTY_META_INFO> {
36  public:
37  // hashing scheme recycler caches logical information instead of actual data
38  // so we do not limit its capacity
39  // thus we do not maintain a metric cache for hashing scheme
42  std::numeric_limits<size_t>::max(),
43  std::numeric_limits<size_t>::max(),
44  0) {}
45 
46  std::optional<HashTableProperty> getItemFromCache(
47  QueryPlanHash key,
48  CacheItemType item_type,
49  DeviceIdentifier device_identifier,
50  std::optional<EMPTY_META_INFO> meta_info = std::nullopt) override;
51 
53  std::optional<HashTableProperty> item,
54  CacheItemType item_type,
55  DeviceIdentifier device_identifier,
56  size_t item_size,
57  size_t compute_time,
58  std::optional<EMPTY_META_INFO> meta_info = std::nullopt) override;
59 
61  std::optional<HashTableProperty> item,
62  CacheItemType item_type,
63  DeviceIdentifier device_identifier);
64 
65  // nothing to do with hashing scheme recycler
66  void initCache() override {}
67 
68  void clearCache() override;
69 
70  void markCachedItemAsDirty(size_t table_key,
71  std::unordered_set<QueryPlanHash>& key_set,
72  CacheItemType item_type,
73  DeviceIdentifier device_identifier) override;
74 
75  std::string toString() const override;
76 
78  // we do not consider ManyToMany type
79  return layout_type == HashTableLayoutType::ONE ? HashType::OneToOne
81  }
82 
86  }
87 
88  std::optional<std::unordered_set<size_t>> getMappedQueryPlanDagsWithTableKey(
89  size_t table_key) const;
90 
91  void removeTableKeyInfoFromQueryPlanDagMap(size_t table_key);
92 
93  void addQueryPlanDagForTableKeys(size_t hashed_query_plan_dag,
94  const std::unordered_set<size_t>& table_keys);
95 
96  static std::string getLayoutString(std::optional<HashTableLayoutType> layout) {
97  auto layout_str =
98  layout ? *layout == HashTableLayoutType::ONE ? "OneToOne" : "OneToMany" : "None";
99  return layout_str;
100  }
101 
102  static std::string getHashingString(std::optional<HashTableHashingType> hashing) {
103  auto hashing_str =
104  hashing ? *hashing == HashTableHashingType::BASELINE ? "Baseline" : "Perfect"
105  : "None";
106  return hashing_str;
107  }
108 
110  auto ret = "{layout: " + getLayoutString(prop.layout) +
111  ", hashing: " + getHashingString(prop.hashing) + "}";
112  return ret;
113  }
114 
115  private:
116  bool hasItemInCache(
117  QueryPlanHash key,
118  CacheItemType item_type,
119  DeviceIdentifier device_identifier,
120  std::lock_guard<std::mutex>& lock,
121  std::optional<EMPTY_META_INFO> meta_info = std::nullopt) const override;
122 
123  // hashing scheme recycler clears the cached layouts at once
124  void removeItemFromCache(
125  QueryPlanHash key,
126  CacheItemType item_type,
127  DeviceIdentifier device_identifier,
128  std::lock_guard<std::mutex>& lock,
129  std::optional<EMPTY_META_INFO> meta_info = std::nullopt) override;
130 
131  // hashing scheme recycler has unlimited capacity so we do not need this
133  CacheItemType item_type,
134  DeviceIdentifier device_identifier,
135  size_t required_size,
136  std::lock_guard<std::mutex>& lock,
137  std::optional<EMPTY_META_INFO> meta_info = std::nullopt) override {}
138 
139  std::unordered_map<size_t, std::unordered_set<size_t>> table_key_to_query_plan_dag_map_;
140 };
size_t DeviceIdentifier
Definition: DataRecycler.h:112
std::string toString() const override
std::unordered_map< size_t, std::unordered_set< size_t > > table_key_to_query_plan_dag_map_
HashTableProperty(std::optional< HashTableLayoutType > layout_in, std::optional< HashTableHashingType > hashing_in)
static std::string getHashtablePropertyString(HashTableProperty prop)
bool hasItemInCache(QueryPlanHash key, CacheItemType item_type, DeviceIdentifier device_identifier, std::lock_guard< std::mutex > &lock, std::optional< EMPTY_META_INFO > meta_info=std::nullopt) const override
static HashTableLayoutType translateHashType(HashType type)
void addQueryPlanDagForTableKeys(size_t hashed_query_plan_dag, const std::unordered_set< size_t > &table_keys)
void cleanupCacheForInsertion(CacheItemType item_type, DeviceIdentifier device_identifier, size_t required_size, std::lock_guard< std::mutex > &lock, std::optional< EMPTY_META_INFO > meta_info=std::nullopt) override
std::optional< HashTableLayoutType > layout
constexpr DeviceIdentifier PROPERTY_CACHE_DEVICE_IDENTIFIER
CacheItemType
Definition: DataRecycler.h:37
static std::string getHashingString(std::optional< HashTableHashingType > hashing)
HashTableLayoutType
Definition: QueryHint.h:44
void removeTableKeyInfoFromQueryPlanDagMap(size_t table_key)
void putItemToCache(QueryPlanHash key, std::optional< HashTableProperty > item, CacheItemType item_type, DeviceIdentifier device_identifier, size_t item_size, size_t compute_time, std::optional< EMPTY_META_INFO > meta_info=std::nullopt) override
static HashType translateLayoutType(HashTableLayoutType layout_type)
void updateItemInCacheIfNecessary(QueryPlanHash key, std::optional< HashTableProperty > item, CacheItemType item_type, DeviceIdentifier device_identifier)
size_t QueryPlanHash
static std::string getLayoutString(std::optional< HashTableLayoutType > layout)
std::optional< std::unordered_set< size_t > > getMappedQueryPlanDagsWithTableKey(size_t table_key) const
void removeItemFromCache(QueryPlanHash key, CacheItemType item_type, DeviceIdentifier device_identifier, std::lock_guard< std::mutex > &lock, std::optional< EMPTY_META_INFO > meta_info=std::nullopt) override
virtual std::optional< HashTableProperty > getItemFromCache(QueryPlanHash key, CacheItemType item_type, DeviceIdentifier device_identifier, std::optional< EMPTY_META_INFO > meta_info=std::nullopt)=0
void markCachedItemAsDirty(size_t table_key, std::unordered_set< QueryPlanHash > &key_set, CacheItemType item_type, DeviceIdentifier device_identifier) override
static constexpr DeviceIdentifier CPU_DEVICE_IDENTIFIER
Definition: DataRecycler.h:133
HashType
Definition: HashTable.h:19
std::optional< HashTableHashingType > hashing