Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
conditional-functions-ir.cc
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 #include "exprs/anyval-util.h"
17 #include "udf/udf.h"
18 
19 using namespace impala;
20 using namespace impala_udf;
21 
22 #define IS_NULL_COMPUTE_FUNCTION(type) \
23  type IsNullExpr::Get##type(ExprContext* context, TupleRow* row) { \
24  DCHECK_EQ(children_.size(), 2); \
25  type val = children_[0]->Get##type(context, row); \
26  if (!val.is_null) return val; /* short-circuit */ \
27  return children_[1]->Get##type(context, row); \
28  }
29 
40 
41 #define NULL_IF_COMPUTE_FUNCTION(AnyValType) \
42  AnyValType NullIfExpr::Get##AnyValType(ExprContext* ctx, TupleRow* row) { \
43  DCHECK_EQ(children_.size(), 2); \
44  AnyValType lhs_val = children_[0]->Get##AnyValType(ctx, row); \
45  /* Short-circuit in case lhs_val is NULL. Can never be equal to RHS. */ \
46  if (lhs_val.is_null) return AnyValType::null(); \
47  /* Get rhs and return NULL if lhs == rhs, lhs otherwise */ \
48  AnyValType rhs_val = children_[1]->Get##AnyValType(ctx, row); \
49  if (!rhs_val.is_null && AnyValUtil::Equals(children_[0]->type(), lhs_val, rhs_val)) {\
50  return AnyValType::null(); \
51  } \
52  return lhs_val; \
53  }
54 
65 
66 #define NULL_IF_ZERO_COMPUTE_FUNCTION(type) \
67  type ConditionalFunctions::NullIfZero(FunctionContext* context, const type& val) { \
68  if (val.is_null || val.val == 0) return type::null(); \
69  return val; \
70  }
71 
73  FunctionContext* context, const DecimalVal& val) {
74  if (val.is_null) return DecimalVal::null();
76  switch (type.GetByteSize()) {
77  case 4:
78  if (val.val4 == 0) return DecimalVal::null();
79  break;
80  case 8:
81  if (val.val8 == 0) return DecimalVal::null();
82  break;
83  case 16:
84  if (val.val16 == 0) return DecimalVal::null();
85  break;
86  default:
87  DCHECK(false);
88  }
89  return val;
90 }
91 
98 
99 #define ZERO_IF_NULL_COMPUTE_FUNCTION(type) \
100  type ConditionalFunctions::ZeroIfNull(FunctionContext* context, const type& val) { \
101  if (val.is_null) return type(0); \
102  return val; \
103  }
104 
112 
113 #define IF_COMPUTE_FUNCTION(type) \
114  type IfExpr::Get##type(ExprContext* context, TupleRow* row) { \
115  DCHECK_EQ(children_.size(), 3); \
116  BooleanVal cond = children_[0]->GetBooleanVal(context, row); \
117  if (cond.is_null || !cond.val) { \
118  return children_[2]->Get##type(context, row); \
119  } \
120  return children_[1]->Get##type(context, row); \
121  }
122 
133 
134 #define COALESCE_COMPUTE_FUNCTION(type) \
135  type CoalesceExpr::Get##type(ExprContext* context, TupleRow* row) { \
136  DCHECK_GE(children_.size(), 1); \
137  for (int i = 0; i < children_.size(); ++i) { \
138  type val = children_[i]->Get##type(context, row); \
139  if (!val.is_null) return val; \
140  } \
141  return type::null(); \
142  }
143 
static TinyIntVal NullIfZero(FunctionContext *context, const TinyIntVal &val)
const TypeDesc & GetReturnType() const
Definition: udf-ir.cc:34
#define IF_COMPUTE_FUNCTION(type)
static ColumnType TypeDescToColumnType(const FunctionContext::TypeDesc &type)
Definition: anyval-util.cc:101
__int128_t val16
Definition: udf.h:572
#define NULL_IF_ZERO_COMPUTE_FUNCTION(type)
#define NULL_IF_COMPUTE_FUNCTION(AnyValType)
This object has a compatible storage format with boost::ptime.
Definition: udf.h:495
bool is_null
Definition: udf.h:359
#define COALESCE_COMPUTE_FUNCTION(type)
int GetByteSize() const
Returns the byte size of this type. Returns 0 for variable length types.
Definition: types.h:178
#define IS_NULL_COMPUTE_FUNCTION(type)
#define ZERO_IF_NULL_COMPUTE_FUNCTION(type)