OmniSciDB  a987f07e93
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RelAlgTranslator.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 #ifndef QUERYENGINE_RELALGTRANSLATOR_H
18 #define QUERYENGINE_RELALGTRANSLATOR_H
19 
20 #include "Execute.h"
21 #include "RelAlgDag.h"
22 
23 #include "ThirdParty/robin_hood/robin_hood.h"
24 
25 #include <ctime>
26 #include <memory>
27 #include <unordered_map>
28 #include <vector>
29 
30 namespace Analyzer {
31 
32 class Expr;
33 
34 } // namespace Analyzer
35 
36 namespace Catalog_Namespace {
37 
38 class Catalog;
39 class SessionInfo;
40 
41 } // namespace Catalog_Namespace
42 
43 namespace query_state {
44 
45 class QueryState;
46 
47 } // namespace query_state
48 
50  public:
52  std::shared_ptr<const query_state::QueryState> q_s,
53  const Executor* executor,
54  const std::unordered_map<const RelAlgNode*, int>& input_to_nest_level,
55  const std::vector<JoinType>& join_types,
56  const time_t now,
57  const bool just_explain)
58  : cat_(cat)
59  , query_state_(q_s)
60  , executor_(executor)
61  , input_to_nest_level_(input_to_nest_level)
62  , join_types_(join_types)
63  , now_(now)
65  , just_explain_(just_explain) {}
66 
67  // Clear cache_ after calling translateScalarRex(rex).
68  std::shared_ptr<Analyzer::Expr> translate(const RexScalar* rex) const;
69 
70  static std::shared_ptr<Analyzer::Expr> translateAggregateRex(
71  const RexAgg* rex,
72  const std::vector<std::shared_ptr<Analyzer::Expr>>& scalar_sources);
73 
74  static std::shared_ptr<Analyzer::Expr> translateLiteral(const RexLiteral*);
75 
77 
78  template <typename T>
79  std::shared_ptr<Analyzer::Expr> translateRexScalar(RexScalar const*) const {
80  throw std::runtime_error("Specialization of translateRexScalar() required.");
81  }
82 
83  private:
84  std::shared_ptr<Analyzer::Expr> translateScalarRex(const RexScalar* rex) const;
85 
86  std::shared_ptr<Analyzer::Expr> translateScalarSubquery(const RexSubQuery*) const;
87 
88  std::shared_ptr<Analyzer::Expr> translateInput(const RexInput*) const;
89 
90  std::shared_ptr<Analyzer::Expr> translateUoper(const RexOperator*) const;
91 
92  std::shared_ptr<Analyzer::Expr> translateInOper(const RexOperator*) const;
93 
94  std::shared_ptr<Analyzer::Expr> getInIntegerSetExpr(std::shared_ptr<Analyzer::Expr> arg,
95  const ResultSet& val_set) const;
96 
97  std::shared_ptr<Analyzer::Expr> translateOper(const RexOperator*) const;
98 
99  std::shared_ptr<Analyzer::Expr> translateOverlapsOper(const RexOperator*) const;
100 
101  std::shared_ptr<Analyzer::Expr> translateCase(const RexCase*) const;
102 
103  std::shared_ptr<Analyzer::Expr> translateWidthBucket(const RexFunctionOperator*) const;
104 
105  std::shared_ptr<Analyzer::Expr> translateLike(const RexFunctionOperator*) const;
106 
107  std::shared_ptr<Analyzer::Expr> translateRegexp(const RexFunctionOperator*) const;
108 
109  std::shared_ptr<Analyzer::Expr> translateLikely(const RexFunctionOperator*) const;
110 
111  std::shared_ptr<Analyzer::Expr> translateUnlikely(const RexFunctionOperator*) const;
112 
113  std::shared_ptr<Analyzer::Expr> translateExtract(const RexFunctionOperator*) const;
114 
115  std::shared_ptr<Analyzer::Expr> translateDateadd(const RexFunctionOperator*) const;
116 
117  std::shared_ptr<Analyzer::Expr> translateDatePlusMinus(const RexOperator*) const;
118 
119  std::shared_ptr<Analyzer::Expr> translateDatediff(const RexFunctionOperator*) const;
120 
121  std::shared_ptr<Analyzer::Expr> translateDatepart(const RexFunctionOperator*) const;
122 
123  std::shared_ptr<Analyzer::Expr> translateLength(const RexFunctionOperator*) const;
124 
125  std::shared_ptr<Analyzer::Expr> translateKeyForString(const RexFunctionOperator*) const;
126 
127  std::shared_ptr<Analyzer::Expr> translateSampleRatio(const RexFunctionOperator*) const;
128 
129  std::shared_ptr<Analyzer::Expr> translateCurrentUser(const RexFunctionOperator*) const;
130 
131  std::shared_ptr<Analyzer::Expr> translateStringOper(const RexFunctionOperator*) const;
132 
133  std::shared_ptr<Analyzer::Expr> translateCardinality(const RexFunctionOperator*) const;
134 
135  std::shared_ptr<Analyzer::Expr> translateItem(const RexFunctionOperator*) const;
136 
137  std::shared_ptr<Analyzer::Expr> translateCurrentDate() const;
138 
139  std::shared_ptr<Analyzer::Expr> translateCurrentTime() const;
140 
141  std::shared_ptr<Analyzer::Expr> translateCurrentTimestamp() const;
142 
143  std::shared_ptr<Analyzer::Expr> translateDatetime(const RexFunctionOperator*) const;
144 
145  std::shared_ptr<Analyzer::Expr> translateHPTLiteral(const RexFunctionOperator*) const;
146 
147  std::shared_ptr<Analyzer::Expr> translateAbs(const RexFunctionOperator*) const;
148 
149  std::shared_ptr<Analyzer::Expr> translateSign(const RexFunctionOperator*) const;
150 
151  std::shared_ptr<Analyzer::Expr> translateOffsetInFragment() const;
152 
153  std::shared_ptr<Analyzer::Expr> translateArrayFunction(
154  const RexFunctionOperator*) const;
155 
156  std::shared_ptr<Analyzer::Expr> translateFunction(const RexFunctionOperator*) const;
157 
158  std::shared_ptr<Analyzer::Expr> translateWindowFunction(
159  const RexWindowFunctionOperator*) const;
160 
161  std::shared_ptr<Analyzer::Expr> translateIntervalExprForWindowFraming(
162  std::shared_ptr<Analyzer::Expr> order_key,
163  bool for_preceding_bound,
164  const Analyzer::BinOper* frame_bound_expr) const;
165 
167 
168  std::shared_ptr<Analyzer::Expr> translateUnaryGeoFunction(
169  const RexFunctionOperator*) const;
170 
171  std::shared_ptr<Analyzer::Expr> translateBinaryGeoFunction(
172  const RexFunctionOperator*) const;
173 
174  std::shared_ptr<Analyzer::Expr> translateTernaryGeoFunction(
175  const RexFunctionOperator*) const;
176 
177  std::shared_ptr<Analyzer::Expr> translateFunctionWithGeoArg(
178  const RexFunctionOperator*) const;
179 
180  std::shared_ptr<Analyzer::Expr> translateGeoComparison(const RexOperator*) const;
181 
182  std::shared_ptr<Analyzer::Expr> translateGeoProjection(const RexFunctionOperator*,
183  SQLTypeInfo&,
184  const bool with_bounds) const;
185 
186  std::shared_ptr<Analyzer::Expr> translateUnaryGeoPredicate(
187  const RexFunctionOperator*,
188  SQLTypeInfo&,
189  const bool with_bounds) const;
190 
191  std::shared_ptr<Analyzer::Expr> translateUnaryGeoConstructor(
192  const RexFunctionOperator*,
193  SQLTypeInfo&,
194  const bool with_bounds) const;
195 
196  std::shared_ptr<Analyzer::Expr> translateBinaryGeoPredicate(
197  const RexFunctionOperator*,
198  SQLTypeInfo&,
199  const bool with_bounds) const;
200 
201  std::shared_ptr<Analyzer::Expr> translateBinaryGeoConstructor(
202  const RexFunctionOperator*,
203  SQLTypeInfo&,
204  const bool with_bounds) const;
205 
206  std::shared_ptr<Analyzer::Expr> translateGeoOverlapsOper(const RexOperator*) const;
207 
208  std::vector<std::shared_ptr<Analyzer::Expr>> translateGeoFunctionArg(
209  const RexScalar* rex_scalar,
210  SQLTypeInfo& arg_ti,
211  const bool with_bounds,
212  const bool with_render_group,
213  const bool expand_geo_col,
214  const bool is_projection = false,
215  const bool use_geo_expressions = false,
216  const bool try_to_compress = false,
217  const bool allow_gdal_transforms = false) const;
218 
219  std::vector<std::shared_ptr<Analyzer::Expr>> translateGeoColumn(
220  const RexInput*,
221  SQLTypeInfo&,
222  const bool with_bounds,
223  const bool with_render_group,
224  const bool expand_geo_col) const;
225 
226  std::vector<std::shared_ptr<Analyzer::Expr>> translateGeoLiteral(const RexLiteral*,
227  SQLTypeInfo&,
228  bool) const;
229 
230  std::pair<std::shared_ptr<Analyzer::Expr>, SQLQualifier> getQuantifiedRhs(
231  const RexScalar*) const;
232 
234  std::shared_ptr<const query_state::QueryState> query_state_;
235  const Executor* executor_;
236  const std::unordered_map<const RelAlgNode*, int> input_to_nest_level_;
237  const std::vector<JoinType> join_types_;
238  time_t now_;
239  mutable bool generated_geos_ops_;
240  const bool just_explain_;
241 
242  // Cache results from translateScalarRex() to avoid exponential recursion.
243  mutable robin_hood::unordered_map<RexScalar const*, std::shared_ptr<Analyzer::Expr>>
245 };
246 
248  const std::list<std::shared_ptr<Analyzer::Expr>> simple_quals;
249  const std::list<std::shared_ptr<Analyzer::Expr>> quals;
250 };
251 
253  const std::shared_ptr<Analyzer::Expr> qual_expr);
254 
255 std::vector<std::shared_ptr<Analyzer::Expr>> qual_to_disjunctive_form(
256  const std::shared_ptr<Analyzer::Expr>& qual_expr);
257 
258 inline auto func_resolve = [](auto funcname, auto&&... strlist) {
259  return ((funcname == strlist) || ...);
260 };
261 
262 using namespace std::literals;
263 
264 #endif // QUERYENGINE_RELALGTRANSLATOR_H
const std::vector< JoinType > join_types_
auto func_resolve
std::shared_ptr< Analyzer::Expr > translateOffsetInFragment() const
std::string cat(Ts &&...args)
std::shared_ptr< Analyzer::Expr > translateCurrentTimestamp() const
std::shared_ptr< Analyzer::Expr > translateBinaryGeoPredicate(const RexFunctionOperator *, SQLTypeInfo &, const bool with_bounds) const
class for a per-database catalog. also includes metadata for the current database and the current use...
Definition: Catalog.h:132
std::shared_ptr< Analyzer::Expr > translateRegexp(const RexFunctionOperator *) const
const Executor * executor_
std::shared_ptr< Analyzer::Expr > translateUnlikely(const RexFunctionOperator *) const
std::vector< std::shared_ptr< Analyzer::Expr > > translateGeoFunctionArg(const RexScalar *rex_scalar, SQLTypeInfo &arg_ti, const bool with_bounds, const bool with_render_group, const bool expand_geo_col, const bool is_projection=false, const bool use_geo_expressions=false, const bool try_to_compress=false, const bool allow_gdal_transforms=false) const
SQLQualifier
Definition: sqldefs.h:71
std::shared_ptr< Analyzer::Expr > translateFunction(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateScalarRex(const RexScalar *rex) const
std::shared_ptr< Analyzer::Expr > translateUoper(const RexOperator *) const
const std::list< std::shared_ptr< Analyzer::Expr > > quals
std::shared_ptr< Analyzer::Expr > translateDateadd(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateIntervalExprForWindowFraming(std::shared_ptr< Analyzer::Expr > order_key, bool for_preceding_bound, const Analyzer::BinOper *frame_bound_expr) const
std::shared_ptr< Analyzer::Expr > translateAbs(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > getInIntegerSetExpr(std::shared_ptr< Analyzer::Expr > arg, const ResultSet &val_set) const
std::shared_ptr< Analyzer::Expr > translateItem(const RexFunctionOperator *) const
QualsConjunctiveForm qual_to_conjunctive_form(const std::shared_ptr< Analyzer::Expr > qual_expr)
std::shared_ptr< Analyzer::Expr > translateGeoProjection(const RexFunctionOperator *, SQLTypeInfo &, const bool with_bounds) const
std::shared_ptr< Analyzer::Expr > translateOper(const RexOperator *) const
std::shared_ptr< Analyzer::Expr > translateDatediff(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateInput(const RexInput *) const
std::shared_ptr< Analyzer::Expr > translateSign(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateUnaryGeoFunction(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateGeoOverlapsOper(const RexOperator *) const
std::vector< std::shared_ptr< Analyzer::Expr > > translateGeoLiteral(const RexLiteral *, SQLTypeInfo &, bool) const
robin_hood::unordered_map< RexScalar const *, std::shared_ptr< Analyzer::Expr > > cache_
static std::shared_ptr< Analyzer::Expr > translateLiteral(const RexLiteral *)
std::shared_ptr< Analyzer::Expr > translateDatetime(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateStringOper(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateRexScalar(RexScalar const *) const
std::pair< std::shared_ptr< Analyzer::Expr >, SQLQualifier > getQuantifiedRhs(const RexScalar *) const
std::shared_ptr< Analyzer::Expr > translateCurrentTime() const
RelAlgTranslator(const Catalog_Namespace::Catalog &cat, std::shared_ptr< const query_state::QueryState > q_s, const Executor *executor, const std::unordered_map< const RelAlgNode *, int > &input_to_nest_level, const std::vector< JoinType > &join_types, const time_t now, const bool just_explain)
std::shared_ptr< Analyzer::Expr > translate(const RexScalar *rex) const
const std::unordered_map< const RelAlgNode *, int > input_to_nest_level_
Analyzer::ExpressionPtrVector translateFunctionArgs(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateUnaryGeoPredicate(const RexFunctionOperator *, SQLTypeInfo &, const bool with_bounds) const
std::shared_ptr< Analyzer::Expr > translateOverlapsOper(const RexOperator *) const
std::shared_ptr< Analyzer::Expr > translateUnaryGeoConstructor(const RexFunctionOperator *, SQLTypeInfo &, const bool with_bounds) const
std::shared_ptr< Analyzer::Expr > translateArrayFunction(const RexFunctionOperator *) const
bool is_projection(const RelAlgExecutionUnit &ra_exe_unit)
std::shared_ptr< Analyzer::Expr > translateCurrentUser(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateSampleRatio(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateLike(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateLikely(const RexFunctionOperator *) const
bool g_enable_watchdog false
Definition: Execute.cpp:79
std::shared_ptr< Analyzer::Expr > translateTernaryGeoFunction(const RexFunctionOperator *) const
std::vector< std::shared_ptr< Analyzer::Expr > > qual_to_disjunctive_form(const std::shared_ptr< Analyzer::Expr > &qual_expr)
std::shared_ptr< Analyzer::Expr > translateBinaryGeoFunction(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateWidthBucket(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateInOper(const RexOperator *) const
std::vector< ExpressionPtr > ExpressionPtrVector
Definition: Analyzer.h:190
std::shared_ptr< Analyzer::Expr > translateCase(const RexCase *) const
std::shared_ptr< Analyzer::Expr > translateFunctionWithGeoArg(const RexFunctionOperator *) const
std::shared_ptr< const query_state::QueryState > query_state_
std::shared_ptr< Analyzer::Expr > translateCurrentDate() const
std::shared_ptr< Analyzer::Expr > translateCardinality(const RexFunctionOperator *) const
const bool just_explain_
std::shared_ptr< Analyzer::Expr > translateGeoComparison(const RexOperator *) const
std::shared_ptr< Analyzer::Expr > translateDatePlusMinus(const RexOperator *) const
const std::list< std::shared_ptr< Analyzer::Expr > > simple_quals
std::shared_ptr< Analyzer::Expr > translateHPTLiteral(const RexFunctionOperator *) const
std::vector< std::shared_ptr< Analyzer::Expr > > translateGeoColumn(const RexInput *, SQLTypeInfo &, const bool with_bounds, const bool with_render_group, const bool expand_geo_col) const
std::shared_ptr< Analyzer::Expr > translateDatepart(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateBinaryGeoConstructor(const RexFunctionOperator *, SQLTypeInfo &, const bool with_bounds) const
const Catalog_Namespace::Catalog & cat_
std::shared_ptr< Analyzer::Expr > translateKeyForString(const RexFunctionOperator *) const
static std::shared_ptr< Analyzer::Expr > translateAggregateRex(const RexAgg *rex, const std::vector< std::shared_ptr< Analyzer::Expr >> &scalar_sources)
std::shared_ptr< Analyzer::Expr > translateWindowFunction(const RexWindowFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateScalarSubquery(const RexSubQuery *) const
std::shared_ptr< Analyzer::Expr > translateLength(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateExtract(const RexFunctionOperator *) const