Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
decimal-functions.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 
16 
17 #include "exprs/anyval-util.h"
18 #include "exprs/expr.h"
19 
20 #include <ctype.h>
21 #include <math.h>
22 
23 #include "common/names.h"
24 
25 namespace impala {
26 
28  return IntVal(context->GetArgType(0)->precision);
29 }
30 
32  return IntVal(context->GetArgType(0)->scale);
33 }
34 
36  if (val.is_null) return DecimalVal::null();
38  switch (type.GetByteSize()) {
39  case 4:
40  return DecimalVal(abs(val.val4));
41  case 8:
42  return DecimalVal(abs(val.val8));
43  case 16:
44  return DecimalVal(abs(val.val16));
45  default:
46  DCHECK(false);
47  return DecimalVal::null();
48  }
49 }
50 
53 }
54 
57 }
58 
61 }
62 
64  FunctionContext* context, const DecimalVal& val, int scale,
68  if (scale < 0) {
70  context, val, val_type, return_type, op, -scale);
71  } else {
73  context, val, val_type, return_type, op);
74  }
75 }
76 
78  FunctionContext* context, const DecimalVal& val, const TinyIntVal& scale) {
79  DCHECK(!scale.is_null);
80  return RoundTo(context, val, scale.val, DecimalOperators::ROUND);
81 }
83  FunctionContext* context, const DecimalVal& val, const SmallIntVal& scale) {
84  DCHECK(!scale.is_null);
85  return RoundTo(context, val, scale.val, DecimalOperators::ROUND);
86 }
88  FunctionContext* context, const DecimalVal& val, const IntVal& scale) {
89  DCHECK(!scale.is_null);
90  return RoundTo(context, val, scale.val, DecimalOperators::ROUND);
91 }
93  FunctionContext* context, const DecimalVal& val, const BigIntVal& scale) {
94  DCHECK(!scale.is_null);
95  return RoundTo(context, val, scale.val, DecimalOperators::ROUND);
96 }
97 
100 }
101 
103  FunctionContext* context, const DecimalVal& val, const TinyIntVal& scale) {
104  DCHECK(!scale.is_null);
105  return RoundTo(context, val, scale.val, DecimalOperators::TRUNCATE);
106 }
108  FunctionContext* context, const DecimalVal& val, const SmallIntVal& scale) {
109  DCHECK(!scale.is_null);
110  return RoundTo(context, val, scale.val, DecimalOperators::TRUNCATE);
111 }
113  FunctionContext* context, const DecimalVal& val, const IntVal& scale) {
114  DCHECK(!scale.is_null);
115  return RoundTo(context, val, scale.val, DecimalOperators::TRUNCATE);
116 }
118  FunctionContext* context, const DecimalVal& val, const BigIntVal& scale) {
119  DCHECK(!scale.is_null);
120  return RoundTo(context, val, scale.val, DecimalOperators::TRUNCATE);
121 }
122 
123 }
int precision
Only valid if type == TYPE_DECIMAL.
Definition: udf.h:75
const TypeDesc & GetReturnType() const
Definition: udf-ir.cc:34
static DecimalVal Truncate(FunctionContext *context, const DecimalVal &val)
int128_t abs(const int128_t &x)
static ColumnType TypeDescToColumnType(const FunctionContext::TypeDesc &type)
Definition: anyval-util.cc:101
__int128_t val16
Definition: udf.h:572
int32_t val
Definition: udf.h:421
static DecimalVal RoundDecimal(FunctionContext *context, const DecimalVal &val, const ColumnType &val_type, const ColumnType &output_type, const DecimalRoundOp &op)
static IntVal Precision(FunctionContext *context, const DecimalVal &val)
static DecimalVal RoundDecimalNegativeScale(FunctionContext *context, const DecimalVal &val, const ColumnType &val_type, const ColumnType &output_type, const DecimalRoundOp &op, int64_t rounding_scale)
bool is_null
Definition: udf.h:359
Additional digits are dropped.
static DecimalVal Ceil(FunctionContext *context, const DecimalVal &val)
static DecimalVal Floor(FunctionContext *context, const DecimalVal &val)
static DecimalVal RoundTo(FunctionContext *context, const DecimalVal &val, const SmallIntVal &scale)
const TypeDesc * GetArgType(int arg_idx) const
Definition: udf.cc:425
static IntVal Scale(FunctionContext *context, const DecimalVal &val)
int GetByteSize() const
Returns the byte size of this type. Returns 0 for variable length types.
Definition: types.h:178
static DecimalVal TruncateTo(FunctionContext *context, const DecimalVal &val, const SmallIntVal &scale)
static DecimalVal Round(FunctionContext *context, const DecimalVal &val)
static DecimalVal Abs(FunctionContext *context, const DecimalVal &val)