OmniSciDB  21ac014ffc
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
QueryHint.h
Go to the documentation of this file.
1 /*
2  * Copyright 2020 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 #ifndef OMNISCI_QUERYHINT_H
18 #define OMNISCI_QUERYHINT_H
19 
20 #include <optional>
21 
22 #include <boost/algorithm/string.hpp>
23 
25 
26 // we expect query hint enum val starts with zero,
27 // and let remaining enum value to be auto-incremented
28 enum QueryHint {
29  kCpuMode = 0,
35  kHintCount, // should be at the last elem before INVALID enum value to count #
36  // supported hints correctly
37  kInvalidHint // this should be the last elem of this enum
38 };
39 
40 static const std::unordered_map<std::string, QueryHint> SupportedQueryHints = {
41  {"cpu_mode", QueryHint::kCpuMode},
42  {"overlaps_bucket_threshold", QueryHint::kOverlapsBucketThreshold},
43  {"overlaps_max_size", QueryHint::kOverlapsMaxSize},
44  {"overlaps_allow_gpu_build", QueryHint::kOverlapsAllowGpuBuild},
45  {"overlaps_no_cache", QueryHint::kOverlapsNoCache},
46  {"overlaps_keys_per_bin", QueryHint::kOverlapsKeysPerBin}};
47 
49  // this class represents parsed query hint's specification
50  // our query AST analyzer translates query hint string to understandable form which we
51  // called "ExplainedQueryHint"
52  public:
54  bool query_hint,
55  bool is_marker,
56  bool has_kv_type_options)
57  : hint_(hint)
58  , query_hint_(query_hint)
59  , is_marker_(is_marker)
60  , has_kv_type_options_(has_kv_type_options) {}
61 
63  bool query_hint,
64  bool is_marker,
65  bool has_kv_type_options,
66  std::vector<std::string>& list_options)
67  : hint_(hint)
68  , query_hint_(query_hint)
69  , is_marker_(is_marker)
70  , has_kv_type_options_(has_kv_type_options)
71  , list_options_(std::move(list_options)) {}
72 
74  bool query_hint,
75  bool is_marker,
76  bool has_kv_type_options,
77  std::unordered_map<std::string, std::string>& kv_options)
78  : hint_(hint)
79  , query_hint_(query_hint)
80  , is_marker_(is_marker)
81  , has_kv_type_options_(has_kv_type_options)
82  , kv_options_(std::move(kv_options)) {}
83 
84  void setListOptions(std::vector<std::string>& list_options) {
85  list_options_ = list_options;
86  }
87 
88  void setKVOptions(std::unordered_map<std::string, std::string>& kv_options) {
89  kv_options_ = kv_options;
90  }
91 
92  void setInheritPaths(std::vector<int>& interit_paths) {
93  inherit_paths_ = interit_paths;
94  }
95 
96  const std::vector<std::string>& getListOptions() { return list_options_; }
97 
98  const std::vector<int>& getInteritPath() { return inherit_paths_; }
99 
100  const std::unordered_map<std::string, std::string>& getKVOptions() {
101  return kv_options_;
102  }
103 
104  const QueryHint getHint() const { return hint_; }
105 
106  bool isQueryHint() const { return query_hint_; }
107 
108  bool hasOptions() const { return is_marker_; }
109 
110  bool hasKvOptions() const { return has_kv_type_options_; }
111 
112  private:
114  // Set true if this hint affects globally
115  // Otherwise it just affects the node which this hint is included (aka table hint)
117  // set true if this has no extra options (neither list_options nor kv_options)
119  // Set true if it is not a marker and has key-value type options
120  // Otherwise (it is not a marker but has list type options), we set this be false
122  std::vector<int> inherit_paths_; // currently not used
123  std::vector<std::string> list_options_;
124  std::unordered_map<std::string, std::string> kv_options_;
125 };
126 
128  // for each query hint, we first translate the raw query hint info
129  // to understandable form called "ExplainedQueryHint"
130  // and we get all necessary info from it and organize it into "RegisteredQueryHint"
131  // so by using "RegisteredQueryHint", we can know and access which query hint is
132  // registered and its detailed info such as the hint's parameter values given by user
134  : cpu_mode(false)
135  , overlaps_bucket_threshold(std::numeric_limits<double>::max())
141 
143  cpu_mode = other.cpu_mode;
150  return *this;
151  }
152 
154  cpu_mode = other.cpu_mode;
161  }
162 
163  // general query execution
164  bool cpu_mode;
165 
166  // overlaps hash join
167  double overlaps_bucket_threshold; // defined in "OverlapsJoinHashTable.h"
172 
173  std::vector<bool> registered_hint;
174 
176 
177  public:
178  static QueryHint translateQueryHint(const std::string& hint_name) {
179  const auto lowered_hint_name = boost::algorithm::to_lower_copy(hint_name);
180  auto it = SupportedQueryHints.find(hint_name);
181  if (it != SupportedQueryHints.end()) {
182  return it->second;
183  }
185  }
186 
187  bool isAnyQueryHintDelivered() const {
188  for (auto flag : registered_hint) {
189  if (flag) {
190  return true;
191  }
192  }
193  return false;
194  }
195 
196  void registerHint(const QueryHint hint) {
197  const auto hint_class = static_cast<int>(hint);
198  if (hint_class >= 0 && hint_class < QueryHint::kHintCount) {
199  registered_hint[hint_class] = true;
200  }
201  }
202 
203  const bool isHintRegistered(const QueryHint hint) const {
204  const auto hint_class = static_cast<int>(hint);
205  if (hint_class >= 0 && hint_class < QueryHint::kHintCount) {
206  return registered_hint[hint_class];
207  }
208  return false;
209  }
210 };
211 
212 // a map from hint_name to its detailed info
213 using Hints = std::unordered_map<QueryHint, ExplainedQueryHint>;
214 
215 #endif // OMNISCI_QUERYHINT_H
std::unordered_map< std::string, std::string > kv_options_
Definition: QueryHint.h:124
const std::vector< int > & getInteritPath()
Definition: QueryHint.h:98
bool overlaps_allow_gpu_build
Definition: QueryHint.h:169
RegisteredQueryHint & operator=(const RegisteredQueryHint &other)
Definition: QueryHint.h:142
void setListOptions(std::vector< std::string > &list_options)
Definition: QueryHint.h:84
double overlaps_keys_per_bin
Definition: QueryHint.h:171
std::vector< bool > registered_hint
Definition: QueryHint.h:173
void setKVOptions(std::unordered_map< std::string, std::string > &kv_options)
Definition: QueryHint.h:88
bool hasKvOptions() const
Definition: QueryHint.h:110
static const std::unordered_map< std::string, QueryHint > SupportedQueryHints
Definition: QueryHint.h:40
static QueryHint translateQueryHint(const std::string &hint_name)
Definition: QueryHint.h:178
std::vector< std::string > list_options_
Definition: QueryHint.h:123
ExplainedQueryHint(QueryHint hint, bool query_hint, bool is_marker, bool has_kv_type_options, std::unordered_map< std::string, std::string > &kv_options)
Definition: QueryHint.h:73
void registerHint(const QueryHint hint)
Definition: QueryHint.h:196
bool hasOptions() const
Definition: QueryHint.h:108
const std::vector< std::string > & getListOptions()
Definition: QueryHint.h:96
double g_overlaps_target_entries_per_bin
Definition: Execute.cpp:98
size_t g_overlaps_max_table_size_bytes
Definition: Execute.cpp:97
bool g_enable_smem_group_by true
static RegisteredQueryHint defaults()
Definition: QueryHint.h:175
size_t overlaps_max_size
Definition: QueryHint.h:168
std::unordered_map< QueryHint, ExplainedQueryHint > Hints
Definition: QueryHint.h:213
QueryHint
Definition: QueryHint.h:28
ExplainedQueryHint(QueryHint hint, bool query_hint, bool is_marker, bool has_kv_type_options)
Definition: QueryHint.h:53
const bool isHintRegistered(const QueryHint hint) const
Definition: QueryHint.h:203
const std::unordered_map< std::string, std::string > & getKVOptions()
Definition: QueryHint.h:100
bool g_enable_watchdog false
Definition: Execute.cpp:75
std::vector< int > inherit_paths_
Definition: QueryHint.h:122
bool has_kv_type_options_
Definition: QueryHint.h:121
double overlaps_bucket_threshold
Definition: QueryHint.h:167
QueryHint hint_
Definition: QueryHint.h:113
const QueryHint getHint() const
Definition: QueryHint.h:104
ExplainedQueryHint(QueryHint hint, bool query_hint, bool is_marker, bool has_kv_type_options, std::vector< std::string > &list_options)
Definition: QueryHint.h:62
void setInheritPaths(std::vector< int > &interit_paths)
Definition: QueryHint.h:92
bool isAnyQueryHintDelivered() const
Definition: QueryHint.h:187
bool isQueryHint() const
Definition: QueryHint.h:106
RegisteredQueryHint(const RegisteredQueryHint &other)
Definition: QueryHint.h:153