OmniSciDB  a5dc49c757
 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:
51  RelAlgTranslator(std::shared_ptr<const query_state::QueryState> q_s,
52  const Executor* executor,
53  const std::unordered_map<const RelAlgNode*, int>& input_to_nest_level,
54  const std::vector<JoinType>& join_types,
55  const time_t now,
56  const bool just_explain)
57  : query_state_(q_s)
58  , executor_(executor)
59  , input_to_nest_level_(input_to_nest_level)
60  , join_types_(join_types)
61  , now_(now)
63  , just_explain_(just_explain) {}
64 
65  // Clear cache_ after calling translateScalarRex(rex).
66  std::shared_ptr<Analyzer::Expr> translate(const RexScalar* rex) const;
67 
68  static std::shared_ptr<Analyzer::Expr> translateAggregateRex(
69  const RexAgg* rex,
70  const std::vector<std::shared_ptr<Analyzer::Expr>>& scalar_sources);
71 
72  static std::shared_ptr<Analyzer::Expr> translateLiteral(const RexLiteral*);
73 
75 
76  template <typename T>
77  std::shared_ptr<Analyzer::Expr> translateRexScalar(RexScalar const*) const {
78  throw std::runtime_error("Specialization of translateRexScalar() required.");
79  }
80 
81  private:
82  std::shared_ptr<Analyzer::Expr> translateScalarRex(const RexScalar* rex) const;
83 
84  std::shared_ptr<Analyzer::Expr> translateScalarSubquery(const RexSubQuery*) const;
85 
86  std::shared_ptr<Analyzer::Expr> translateInput(const RexInput*) const;
87 
88  std::shared_ptr<Analyzer::Expr> translateUoper(const RexOperator*) const;
89 
90  std::shared_ptr<Analyzer::Expr> translateInOper(const RexOperator*) const;
91 
92  std::shared_ptr<Analyzer::Expr> getInIntegerSetExpr(std::shared_ptr<Analyzer::Expr> arg,
93  const ResultSet& val_set) const;
94 
95  std::shared_ptr<Analyzer::Expr> translateOper(const RexOperator*) const;
96 
97  std::shared_ptr<Analyzer::Expr> translateBoundingBoxIntersectOper(
98  const RexOperator*) const;
99 
100  std::shared_ptr<Analyzer::Expr> translateCase(const RexCase*) const;
101 
102  std::shared_ptr<Analyzer::Expr> translateWidthBucket(const RexFunctionOperator*) const;
103 
104  std::shared_ptr<Analyzer::Expr> translateMLPredict(const RexFunctionOperator*) const;
105 
106  std::shared_ptr<Analyzer::Expr> translatePCAProject(const RexFunctionOperator*) const;
107 
108  std::shared_ptr<Analyzer::Expr> translateLike(const RexFunctionOperator*) const;
109 
110  std::shared_ptr<Analyzer::Expr> translateRegexp(const RexFunctionOperator*) const;
111 
112  std::shared_ptr<Analyzer::Expr> translateLikely(const RexFunctionOperator*) const;
113 
114  std::shared_ptr<Analyzer::Expr> translateUnlikely(const RexFunctionOperator*) const;
115 
116  std::shared_ptr<Analyzer::Expr> translateExtract(const RexFunctionOperator*) const;
117 
118  std::shared_ptr<Analyzer::Expr> translateDateadd(const RexFunctionOperator*) const;
119 
120  std::shared_ptr<Analyzer::Expr> translateDatePlusMinus(const RexOperator*) const;
121 
122  std::shared_ptr<Analyzer::Expr> translateDatediff(const RexFunctionOperator*) const;
123 
124  std::shared_ptr<Analyzer::Expr> translateDatepart(const RexFunctionOperator*) const;
125 
126  std::shared_ptr<Analyzer::Expr> translateLength(const RexFunctionOperator*) const;
127 
128  std::shared_ptr<Analyzer::Expr> translateKeyForString(const RexFunctionOperator*) const;
129 
130  std::shared_ptr<Analyzer::Expr> translateSampleRatio(const RexFunctionOperator*) const;
131 
132  std::shared_ptr<Analyzer::Expr> translateCurrentUser(const RexFunctionOperator*) const;
133 
134  std::shared_ptr<Analyzer::Expr> translateStringOper(const RexFunctionOperator*) const;
135 
136  std::shared_ptr<Analyzer::Expr> translateCardinality(const RexFunctionOperator*) const;
137 
138  std::shared_ptr<Analyzer::Expr> translateItem(const RexFunctionOperator*) const;
139 
140  std::shared_ptr<Analyzer::Expr> translateCurrentDate() const;
141 
142  std::shared_ptr<Analyzer::Expr> translateCurrentTime() const;
143 
144  std::shared_ptr<Analyzer::Expr> translateCurrentTimestamp() const;
145 
146  std::shared_ptr<Analyzer::Expr> translateDatetime(const RexFunctionOperator*) const;
147 
148  std::shared_ptr<Analyzer::Expr> translateHPTLiteral(const RexFunctionOperator*) const;
149 
150  std::shared_ptr<Analyzer::Expr> translateAbs(const RexFunctionOperator*) const;
151 
152  std::shared_ptr<Analyzer::Expr> translateSign(const RexFunctionOperator*) const;
153 
154  std::shared_ptr<Analyzer::Expr> translateOffsetInFragment() const;
155 
156  std::shared_ptr<Analyzer::Expr> translateArrayFunction(
157  const RexFunctionOperator*) const;
158 
159  std::shared_ptr<Analyzer::Expr> translateFunction(const RexFunctionOperator*) const;
160 
161  std::shared_ptr<Analyzer::Expr> translateWindowFunction(
162  const RexWindowFunctionOperator*) const;
163 
164  std::tuple<bool, bool, std::shared_ptr<Analyzer::Expr>> translateFrameBoundExpr(
165  const RexScalar* bound_expr) const;
166 
167  std::shared_ptr<Analyzer::Expr> translateIntervalExprForWindowFraming(
168  std::shared_ptr<Analyzer::Expr> order_key,
169  bool for_preceding_bound,
170  const Analyzer::Expr* expr) const;
171 
173 
174  std::shared_ptr<Analyzer::Expr> translateUnaryGeoFunction(
175  const RexFunctionOperator*) const;
176 
177  std::shared_ptr<Analyzer::Expr> translateBinaryGeoFunction(
178  const RexFunctionOperator*) const;
179 
180  std::shared_ptr<Analyzer::Expr> translateTernaryGeoFunction(
181  const RexFunctionOperator*) const;
182 
183  std::shared_ptr<Analyzer::Expr> translateFunctionWithGeoArg(
184  const RexFunctionOperator*) const;
185 
186  std::shared_ptr<Analyzer::Expr> translateGeoComparison(const RexOperator*) const;
187 
188  std::shared_ptr<Analyzer::Expr> translateGeoProjection(const RexFunctionOperator*,
189  SQLTypeInfo&,
190  const bool with_bounds) const;
191 
192  std::shared_ptr<Analyzer::Expr> translateUnaryGeoPredicate(
193  const RexFunctionOperator*,
194  SQLTypeInfo&,
195  const bool with_bounds) const;
196 
197  std::shared_ptr<Analyzer::Expr> translateUnaryGeoConstructor(
198  const RexFunctionOperator*,
199  SQLTypeInfo&,
200  const bool with_bounds) const;
201 
202  std::shared_ptr<Analyzer::Expr> translateBinaryGeoPredicate(
203  const RexFunctionOperator*,
204  SQLTypeInfo&,
205  const bool with_bounds) const;
206 
207  std::shared_ptr<Analyzer::Expr> translateBinaryGeoConstructor(
208  const RexFunctionOperator*,
209  SQLTypeInfo&,
210  const bool with_bounds) const;
211 
212  std::shared_ptr<Analyzer::Expr> translateGeoBoundingBoxIntersectOper(
213  const RexOperator*) const;
214 
215  std::vector<std::shared_ptr<Analyzer::Expr>> translateGeoFunctionArg(
216  const RexScalar* rex_scalar,
217  SQLTypeInfo& arg_ti,
218  const bool with_bounds,
219  const bool expand_geo_col,
220  const bool is_projection = false,
221  const bool use_geo_expressions = false,
222  const bool try_to_compress = false,
223  const bool allow_gdal_transforms = false) const;
224 
225  std::vector<std::shared_ptr<Analyzer::Expr>> translateGeoColumn(
226  const RexInput*,
227  SQLTypeInfo&,
228  const bool with_bounds,
229  const bool expand_geo_col) const;
230 
231  std::vector<std::shared_ptr<Analyzer::Expr>> translateGeoLiteral(const RexLiteral*,
232  SQLTypeInfo&,
233  bool) const;
234 
235  std::pair<std::shared_ptr<Analyzer::Expr>, SQLQualifier> getQuantifiedRhs(
236  const RexScalar*) const;
237 
238  std::shared_ptr<const query_state::QueryState> query_state_;
239  const Executor* executor_;
240  const std::unordered_map<const RelAlgNode*, int> input_to_nest_level_;
241  const std::vector<JoinType> join_types_;
242  time_t now_;
243  mutable bool generated_geos_ops_;
244  const bool just_explain_;
245 
246  // Cache results from translateScalarRex() to avoid exponential recursion.
247  mutable robin_hood::unordered_map<RexScalar const*, std::shared_ptr<Analyzer::Expr>>
249 };
250 
252  const std::list<std::shared_ptr<Analyzer::Expr>> simple_quals;
253  const std::list<std::shared_ptr<Analyzer::Expr>> quals;
254 };
255 
257  const std::shared_ptr<Analyzer::Expr> qual_expr);
258 
259 std::vector<std::shared_ptr<Analyzer::Expr>> qual_to_disjunctive_form(
260  const std::shared_ptr<Analyzer::Expr>& qual_expr);
261 
262 inline auto func_resolve = [](auto funcname, auto&&... strlist) {
263  return ((funcname == strlist) || ...);
264 };
265 
266 using namespace std::literals;
267 
268 #endif // QUERYENGINE_RELALGTRANSLATOR_H
const std::vector< JoinType > join_types_
auto func_resolve
std::shared_ptr< Analyzer::Expr > translateOffsetInFragment() const
std::vector< std::shared_ptr< Analyzer::Expr > > translateGeoColumn(const RexInput *, SQLTypeInfo &, const bool with_bounds, const bool expand_geo_col) const
std::shared_ptr< Analyzer::Expr > translateCurrentTimestamp() const
std::shared_ptr< Analyzer::Expr > translateBinaryGeoPredicate(const RexFunctionOperator *, SQLTypeInfo &, const bool with_bounds) const
std::shared_ptr< Analyzer::Expr > translateRegexp(const RexFunctionOperator *) const
const Executor * executor_
std::shared_ptr< Analyzer::Expr > translateUnlikely(const RexFunctionOperator *) const
RelAlgTranslator(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)
SQLQualifier
Definition: sqldefs.h:74
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 > 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::vector< std::shared_ptr< Analyzer::Expr > > translateGeoLiteral(const RexLiteral *, SQLTypeInfo &, bool) const
std::shared_ptr< Analyzer::Expr > translateBoundingBoxIntersectOper(const RexOperator *) 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
std::tuple< bool, bool, std::shared_ptr< Analyzer::Expr > > translateFrameBoundExpr(const RexScalar *bound_expr) const
std::shared_ptr< Analyzer::Expr > translate(const RexScalar *rex) const
std::shared_ptr< Analyzer::Expr > translatePCAProject(const RexFunctionOperator *) 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 > translateIntervalExprForWindowFraming(std::shared_ptr< Analyzer::Expr > order_key, bool for_preceding_bound, const Analyzer::Expr *expr) 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 > translateMLPredict(const RexFunctionOperator *) const
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
std::vector< std::shared_ptr< Analyzer::Expr > > translateGeoFunctionArg(const RexScalar *rex_scalar, SQLTypeInfo &arg_ti, const bool with_bounds, 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
bool g_enable_watchdog false
Definition: Execute.cpp:80
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:186
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::shared_ptr< Analyzer::Expr > translateDatepart(const RexFunctionOperator *) const
std::shared_ptr< Analyzer::Expr > translateBinaryGeoConstructor(const RexFunctionOperator *, SQLTypeInfo &, const bool with_bounds) const
std::shared_ptr< Analyzer::Expr > translateGeoBoundingBoxIntersectOper(const RexOperator *) const
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