Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
Main Page
Namespaces
Classes
Files
File List
File Members
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
"
16
#include "
exprs/conditional-functions.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
30
IS_NULL_COMPUTE_FUNCTION
(
BooleanVal
);
31
IS_NULL_COMPUTE_FUNCTION
(
TinyIntVal
);
32
IS_NULL_COMPUTE_FUNCTION
(
SmallIntVal
);
33
IS_NULL_COMPUTE_FUNCTION
(
IntVal
);
34
IS_NULL_COMPUTE_FUNCTION
(
BigIntVal
);
35
IS_NULL_COMPUTE_FUNCTION
(
FloatVal
);
36
IS_NULL_COMPUTE_FUNCTION
(
DoubleVal
);
37
IS_NULL_COMPUTE_FUNCTION
(
StringVal
);
38
IS_NULL_COMPUTE_FUNCTION
(
TimestampVal
);
39
IS_NULL_COMPUTE_FUNCTION
(
DecimalVal
);
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
55
NULL_IF_COMPUTE_FUNCTION
(
BooleanVal
);
56
NULL_IF_COMPUTE_FUNCTION
(
TinyIntVal
);
57
NULL_IF_COMPUTE_FUNCTION
(
SmallIntVal
);
58
NULL_IF_COMPUTE_FUNCTION
(
IntVal
);
59
NULL_IF_COMPUTE_FUNCTION
(
BigIntVal
);
60
NULL_IF_COMPUTE_FUNCTION
(
FloatVal
);
61
NULL_IF_COMPUTE_FUNCTION
(
DoubleVal
);
62
NULL_IF_COMPUTE_FUNCTION
(
StringVal
);
63
NULL_IF_COMPUTE_FUNCTION
(
TimestampVal
);
64
NULL_IF_COMPUTE_FUNCTION
(
DecimalVal
);
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
72
DecimalVal
ConditionalFunctions::NullIfZero
(
73
FunctionContext
* context,
const
DecimalVal
& val) {
74
if
(val.
is_null
)
return
DecimalVal::null();
75
ColumnType
type =
AnyValUtil::TypeDescToColumnType
(context->
GetReturnType
());
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
92
NULL_IF_ZERO_COMPUTE_FUNCTION
(
TinyIntVal
);
93
NULL_IF_ZERO_COMPUTE_FUNCTION
(
SmallIntVal
);
94
NULL_IF_ZERO_COMPUTE_FUNCTION
(
IntVal
);
95
NULL_IF_ZERO_COMPUTE_FUNCTION
(
BigIntVal
);
96
NULL_IF_ZERO_COMPUTE_FUNCTION
(
FloatVal
);
97
NULL_IF_ZERO_COMPUTE_FUNCTION
(
DoubleVal
);
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
105
ZERO_IF_NULL_COMPUTE_FUNCTION
(
TinyIntVal
);
106
ZERO_IF_NULL_COMPUTE_FUNCTION
(
SmallIntVal
);
107
ZERO_IF_NULL_COMPUTE_FUNCTION
(
IntVal
);
108
ZERO_IF_NULL_COMPUTE_FUNCTION
(
BigIntVal
);
109
ZERO_IF_NULL_COMPUTE_FUNCTION
(
FloatVal
);
110
ZERO_IF_NULL_COMPUTE_FUNCTION
(
DoubleVal
);
111
ZERO_IF_NULL_COMPUTE_FUNCTION
(
DecimalVal
);
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
123
IF_COMPUTE_FUNCTION
(
BooleanVal
);
124
IF_COMPUTE_FUNCTION
(
TinyIntVal
);
125
IF_COMPUTE_FUNCTION
(
SmallIntVal
);
126
IF_COMPUTE_FUNCTION
(
IntVal
);
127
IF_COMPUTE_FUNCTION
(
BigIntVal
);
128
IF_COMPUTE_FUNCTION
(
FloatVal
);
129
IF_COMPUTE_FUNCTION
(
DoubleVal
);
130
IF_COMPUTE_FUNCTION
(
StringVal
);
131
IF_COMPUTE_FUNCTION
(
TimestampVal
);
132
IF_COMPUTE_FUNCTION
(
DecimalVal
);
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
144
COALESCE_COMPUTE_FUNCTION
(
BooleanVal
);
145
COALESCE_COMPUTE_FUNCTION
(
TinyIntVal
);
146
COALESCE_COMPUTE_FUNCTION
(
SmallIntVal
);
147
COALESCE_COMPUTE_FUNCTION
(
IntVal
);
148
COALESCE_COMPUTE_FUNCTION
(
BigIntVal
);
149
COALESCE_COMPUTE_FUNCTION
(
FloatVal
);
150
COALESCE_COMPUTE_FUNCTION
(
DoubleVal
);
151
COALESCE_COMPUTE_FUNCTION
(
StringVal
);
152
COALESCE_COMPUTE_FUNCTION
(
TimestampVal
);
153
COALESCE_COMPUTE_FUNCTION
(
DecimalVal
);
impala::ConditionalFunctions::NullIfZero
static TinyIntVal NullIfZero(FunctionContext *context, const TinyIntVal &val)
impala_udf::FunctionContext::GetReturnType
const TypeDesc & GetReturnType() const
Definition:
udf-ir.cc:34
IF_COMPUTE_FUNCTION
#define IF_COMPUTE_FUNCTION(type)
Definition:
conditional-functions-ir.cc:113
impala::AnyValUtil::TypeDescToColumnType
static ColumnType TypeDescToColumnType(const FunctionContext::TypeDesc &type)
Definition:
anyval-util.cc:101
impala_udf::DecimalVal::val16
__int128_t val16
Definition:
udf.h:572
NULL_IF_ZERO_COMPUTE_FUNCTION
#define NULL_IF_ZERO_COMPUTE_FUNCTION(type)
Definition:
conditional-functions-ir.cc:66
impala_udf::DoubleVal
Definition:
udf.h:475
udf.h
NULL_IF_COMPUTE_FUNCTION
#define NULL_IF_COMPUTE_FUNCTION(AnyValType)
Definition:
conditional-functions-ir.cc:41
impala_udf::DecimalVal::val4
int32_t val4
Definition:
udf.h:570
impala_udf::TimestampVal
This object has a compatible storage format with boost::ptime.
Definition:
udf.h:495
impala_udf::FunctionContext
Definition:
udf.h:47
impala_udf::AnyVal::is_null
bool is_null
Definition:
udf.h:359
impala_udf::TinyIntVal
Definition:
udf.h:382
impala_udf::FloatVal
Definition:
udf.h:458
anyval-util.h
impala_udf::SmallIntVal
Definition:
udf.h:401
COALESCE_COMPUTE_FUNCTION
#define COALESCE_COMPUTE_FUNCTION(type)
Definition:
conditional-functions-ir.cc:134
impala_udf::DecimalVal
Definition:
udf.h:556
impala::ColumnType::GetByteSize
int GetByteSize() const
Returns the byte size of this type. Returns 0 for variable length types.
Definition:
types.h:178
impala_udf::StringVal
Definition:
udf.h:521
impala_udf::DecimalVal::val8
int64_t val8
Definition:
udf.h:571
impala_udf::IntVal
Definition:
udf.h:420
IS_NULL_COMPUTE_FUNCTION
#define IS_NULL_COMPUTE_FUNCTION(type)
Definition:
conditional-functions-ir.cc:22
ZERO_IF_NULL_COMPUTE_FUNCTION
#define ZERO_IF_NULL_COMPUTE_FUNCTION(type)
Definition:
conditional-functions-ir.cc:99
impala_udf::BigIntVal
Definition:
udf.h:439
conditional-functions.h
impala::ColumnType
Definition:
types.h:59
impala_udf::BooleanVal
Definition:
udf.h:363
be
src
exprs
conditional-functions-ir.cc
Generated on Thu May 7 2015 16:10:36 for Impala by
1.8.6