Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
expr.h
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 
17 //
25 //
28 //
33 //
35 //
37 //
43 //
47 //
56 //
58 //
66 //
71 //
75 
76 #ifndef IMPALA_EXPRS_EXPR_H
77 #define IMPALA_EXPRS_EXPR_H
78 
79 #include <string>
80 #include <vector>
81 
82 #include "common/status.h"
83 #include "impala-ir/impala-ir-functions.h"
84 #include "runtime/descriptors.h"
85 #include "runtime/decimal-value.h"
86 #include "runtime/lib-cache.h"
87 #include "runtime/raw-value.h"
88 #include "runtime/tuple.h"
89 #include "runtime/tuple-row.h"
90 #include "runtime/string-value.h"
92 #include "udf/udf.h"
93 
94 using namespace impala_udf;
95 
96 namespace llvm {
97  class BasicBlock;
98  class Function;
99  class Type;
100  class Value;
101 };
102 
103 namespace impala {
104 
105 class Expr;
106 class IsNullExpr;
107 class LlvmCodeGen;
108 class ObjectPool;
109 class RowDescriptor;
110 class RuntimeState;
111 class TColumnValue;
112 class TExpr;
113 class TExprNode;
114 
116 class Expr {
117  public:
118  virtual ~Expr();
119 
124  virtual BooleanVal GetBooleanVal(ExprContext* context, TupleRow*);
125  virtual TinyIntVal GetTinyIntVal(ExprContext* context, TupleRow*);
126  virtual SmallIntVal GetSmallIntVal(ExprContext* context, TupleRow*);
127  virtual IntVal GetIntVal(ExprContext* context, TupleRow*);
128  virtual BigIntVal GetBigIntVal(ExprContext* context, TupleRow*);
129  virtual FloatVal GetFloatVal(ExprContext* context, TupleRow*);
130  virtual DoubleVal GetDoubleVal(ExprContext* context, TupleRow*);
131  virtual StringVal GetStringVal(ExprContext* context, TupleRow*);
132  virtual TimestampVal GetTimestampVal(ExprContext* context, TupleRow*);
133  virtual DecimalVal GetDecimalVal(ExprContext* context, TupleRow*);
134 
139  int output_scale() const { return output_scale_; }
140 
141  void AddChild(Expr* expr) { children_.push_back(expr); }
142  Expr* GetChild(int i) const { return children_[i]; }
143  int GetNumChildren() const { return children_.size(); }
144 
145  const ColumnType& type() const { return type_; }
146  bool is_slotref() const { return is_slotref_; }
147 
148  const std::vector<Expr*>& children() const { return children_; }
149 
153  virtual bool IsConstant() const;
154 
157  virtual int GetSlotIds(std::vector<SlotId>* slot_ids) const;
158 
162  static Status CreateExprTree(ObjectPool* pool, const TExpr& texpr, ExprContext** ctx);
163 
167  static Status CreateExprTrees(ObjectPool* pool, const std::vector<TExpr>& texprs,
168  std::vector<ExprContext*>* ctxs);
169 
172  static Status Prepare(const std::vector<ExprContext*>& ctxs, RuntimeState* state,
174 
176  static Status Open(const std::vector<ExprContext*>& ctxs, RuntimeState* state);
177 
181  static Status Clone(const std::vector<ExprContext*>& ctxs, RuntimeState* state,
182  std::vector<ExprContext*>* new_ctxs);
183 
185  static void Close(const std::vector<ExprContext*>& ctxs, RuntimeState* state);
186 
190  static Expr* CreateLiteral(ObjectPool* pool, const ColumnType& type, void* data);
191 
195  static Expr* CreateLiteral(ObjectPool* pool, const ColumnType& type,
196  const std::string&);
197 
204  static int ComputeResultsLayout(const std::vector<Expr*>& exprs,
205  std::vector<int>* offsets, int* var_result_begin);
206  static int ComputeResultsLayout(const std::vector<ExprContext*>& ctxs,
207  std::vector<int>* offsets, int* var_result_begin);
208 
211  //
214  virtual Status GetCodegendComputeFn(RuntimeState* state, llvm::Function** fn) = 0;
215 
220  virtual AnyVal* GetConstVal(ExprContext* context);
221 
222  virtual std::string DebugString() const;
223  static std::string DebugString(const std::vector<Expr*>& exprs);
224  static std::string DebugString(const std::vector<ExprContext*>& ctxs);
225 
230  static void InitBuiltinsDummy();
231 
232  static const char* LLVM_CLASS_NAME;
233 
234  protected:
235  friend class AggFnEvaluator;
236  friend class CastExpr;
237  friend class ComputeFunctions;
238  friend class DecimalFunctions;
239  friend class DecimalLliteral;
240  friend class DecimalOperators;
241  friend class MathFunctions;
242  friend class StringFunctions;
243  friend class TimestampFunctions;
244  friend class ConditionalFunctions;
245  friend class UtilityFunctions;
246  friend class CaseExpr;
247  friend class InPredicate;
248  friend class FunctionCall;
249  friend class ScalarFnCall;
250 
251  Expr(const ColumnType& type, bool is_slotref = false);
252  Expr(const TExprNode& node, bool is_slotref = false);
253 
257  //
260  virtual Status Prepare(RuntimeState* state, const RowDescriptor& row_desc,
261  ExprContext* context);
262 
266  //
269  virtual Status Open(RuntimeState* state, ExprContext* context,
271 
273  //
277  virtual void Close(RuntimeState* state, ExprContext* context,
279 
282 
284  TFunction fn_;
285 
287  const bool is_slotref_;
290  std::vector<Expr*> children_;
292 
297 
299  llvm::Function* ir_compute_fn_;
300 
303  boost::scoped_ptr<AnyVal> constant_val_;
304 
307  FunctionContext* RegisterFunctionContext(
308  ExprContext* ctx, RuntimeState* state, int varargs_buffer_size = 0);
309 
313  llvm::Function* CreateIrFunctionPrototype(LlvmCodeGen* codegen, const std::string& name,
314  llvm::Value* (*args)[2]);
315 
318  //
324  Status GetCodegendComputeFnWrapper(RuntimeState* state, llvm::Function** fn);
325 
329  llvm::Function* GetStaticGetValWrapper(ColumnType type, LlvmCodeGen* codegen);
330 
332  std::string DebugString(const std::string& expr_name) const {
333  std::stringstream out;
334  out << expr_name << "(" << Expr::DebugString() << ")";
335  return out.str();
336  }
337 
338  private:
339  friend class ExprContext;
340  friend class ExprTest;
341 
343  static Status CreateExpr(ObjectPool* pool, const TExprNode& texpr_node, Expr** expr);
344 
357  static Status CreateTreeFromThrift(ObjectPool* pool,
358  const std::vector<TExprNode>& nodes, Expr* parent, int* node_idx,
359  Expr** root_expr, ExprContext** ctx);
360 
363  //
367  static BooleanVal GetBooleanVal(Expr* expr, ExprContext* context, TupleRow* row);
368  static TinyIntVal GetTinyIntVal(Expr* expr, ExprContext* context, TupleRow* row);
369  static SmallIntVal GetSmallIntVal(Expr* expr, ExprContext* context, TupleRow* row);
370  static IntVal GetIntVal(Expr* expr, ExprContext* context, TupleRow* row);
371  static BigIntVal GetBigIntVal(Expr* expr, ExprContext* context, TupleRow* row);
372  static FloatVal GetFloatVal(Expr* expr, ExprContext* context, TupleRow* row);
373  static DoubleVal GetDoubleVal(Expr* expr, ExprContext* context, TupleRow* row);
374  static StringVal GetStringVal(Expr* expr, ExprContext* context, TupleRow* row);
375  static TimestampVal GetTimestampVal(Expr* expr, ExprContext* context, TupleRow* row);
376  static DecimalVal GetDecimalVal(Expr* expr, ExprContext* context, TupleRow* row);
377 };
378 
379 }
380 
381 #endif
llvm::Function * ir_compute_fn_
Cached codegened compute function. Exprs should set this in GetCodegendComputeFn().
Definition: expr.h:299
const std::vector< Expr * > & children() const
Definition: expr.h:148
MemTracker tracker
int context_index_
Definition: expr.h:296
This object has a compatible storage format with boost::ptime.
Definition: udf.h:495
boost::scoped_ptr< AnyVal > constant_val_
Definition: expr.h:303
LLVM code generator. This is the top level object to generate jitted code.
Definition: llvm-codegen.h:107
void AddChild(Expr *expr)
Definition: expr.h:141
Conditional functions that can be expressed as UDFs.
static const char * LLVM_CLASS_NAME
Definition: expr.h:232
ObjectPool pool
std::string DebugString(const T &val)
Definition: udf-debug.h:27
LibCache::LibCacheEntry * cache_entry_
Cache entry for the library implementing this function.
Definition: expr.h:281
This is the superclass of all expr evaluation nodes.
Definition: expr.h:116
This class is thread-safe.
Definition: mem-tracker.h:61
Expr * GetChild(int i) const
Definition: expr.h:142
const RowDescriptor & row_desc() const
TODO: Reconsider whether this class needs to exist.
std::string DebugString(const std::string &expr_name) const
Simple debug string that provides no expr subclass-specific information.
Definition: expr.h:332
TFunction fn_
Function description.
Definition: expr.h:284
const ColumnType & type() const
Definition: expr.h:145
int output_scale_
Definition: expr.h:291
int output_scale() const
Definition: expr.h:139
const ColumnType type_
analysis is done, types are fixed at this point
Definition: expr.h:289
std::vector< Expr * > children_
Definition: expr.h:290
const bool is_slotref_
recognize if this node is a slotref in order to speed up GetValue()
Definition: expr.h:287
Predicate for evaluating expressions of the form "val [NOT] IN (x1, x2, x3...)".
Definition: in-predicate.h:42
string name
Definition: cpu-info.cc:50
int GetNumChildren() const
Definition: expr.h:143
bool is_slotref() const
Definition: expr.h:146