Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
utility-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 <gutil/strings/substitute.h>
18 
19 #include "exprs/anyval-util.h"
20 #include "runtime/runtime-state.h"
21 #include "udf/udf-internal.h"
22 #include "util/debug-util.h"
23 #include "util/time.h"
24 
25 #include "common/names.h"
26 
27 using namespace strings;
28 
29 namespace impala {
30 
31 BigIntVal UtilityFunctions::FnvHashString(FunctionContext* ctx,
32  const StringVal& input_val) {
33  if (input_val.is_null) return BigIntVal::null();
34  return BigIntVal(HashUtil::FnvHash64(input_val.ptr, input_val.len, HashUtil::FNV_SEED));
35 }
36 
37 BigIntVal UtilityFunctions::FnvHashTimestamp(FunctionContext* ctx,
38  const TimestampVal& input_val) {
39  if (input_val.is_null) return BigIntVal::null();
40  TimestampValue tv = TimestampValue::FromTimestampVal(input_val);
41  return BigIntVal(HashUtil::FnvHash64(&tv, 12, HashUtil::FNV_SEED));
42 }
43 
44 template<typename T>
46  if (input_val.is_null) return BigIntVal::null();
47  return BigIntVal(
48  HashUtil::FnvHash64(&input_val.val, sizeof(input_val.val), HashUtil::FNV_SEED));
49 }
50 
51 // Note that this only hashes the unscaled value and not the scale or precision, so this
52 // function is only valid when used over a single decimal type.
53 BigIntVal UtilityFunctions::FnvHashDecimal(FunctionContext* ctx,
54  const DecimalVal& input_val) {
55  if (input_val.is_null) return BigIntVal::null();
56  ColumnType input_type = AnyValUtil::TypeDescToColumnType(*ctx->GetArgType(0));
57  int byte_size = input_type.GetByteSize();
58  return BigIntVal(HashUtil::FnvHash64(&input_val.val16, byte_size, HashUtil::FNV_SEED));
59 }
60 
62  FunctionContext* ctx, const BooleanVal& input_val);
64  FunctionContext* ctx, const TinyIntVal& input_val);
66  FunctionContext* ctx, const SmallIntVal& input_val);
68  FunctionContext* ctx, const IntVal& input_val);
70  FunctionContext* ctx, const BigIntVal& input_val);
72  FunctionContext* ctx, const FloatVal& input_val);
74  FunctionContext* ctx, const DoubleVal& input_val);
75 
76 StringVal UtilityFunctions::User(FunctionContext* ctx) {
77  StringVal user(ctx->user());
78  // An empty string indicates the user wasn't set in the session or in the query request.
79  return (user.len > 0) ? user : StringVal::null();
80 }
81 
82 StringVal UtilityFunctions::Version(FunctionContext* ctx) {
83  return AnyValUtil::FromString(ctx, GetVersionString());
84 }
85 
86 IntVal UtilityFunctions::Pid(FunctionContext* ctx) {
87  int pid = ctx->impl()->state()->query_ctx().pid;
88  // Will be -1 if the PID could not be determined
89  if (pid == -1) return IntVal::null();
90  // Otherwise the PID should be greater than 0
91  DCHECK(pid > 0);
92  return IntVal(pid);
93 }
94 
95 BooleanVal UtilityFunctions::Sleep(FunctionContext* ctx, const IntVal& milliseconds ) {
96  if (milliseconds.is_null) return BooleanVal::null();
97  SleepForMs(milliseconds.val);
98  return BooleanVal(true);
99 }
100 
101 StringVal UtilityFunctions::CurrentDatabase(FunctionContext* ctx) {
102  StringVal database =
103  AnyValUtil::FromString(ctx, ctx->impl()->state()->query_ctx().session.database);
104  // An empty string indicates the current database wasn't set.
105  return (database.len > 0) ? database : StringVal::null();
106 }
107 
108 template<typename T>
109 StringVal UtilityFunctions::TypeOf(FunctionContext* ctx, const T& /*input_val*/) {
110  FunctionContext::TypeDesc type_desc = *(ctx->GetArgType(0));
111  ColumnType column_type = AnyValUtil::TypeDescToColumnType(type_desc);
112  const string& type_string = TypeToString(column_type.type);
113 
114  switch(column_type.type) {
115  // Show the precision and scale of DECIMAL type.
116  case TYPE_DECIMAL:
117  return AnyValUtil::FromString(ctx, Substitute("$0($1,$2)", type_string,
118  type_desc.precision, type_desc.scale));
119  // Show length of CHAR and VARCHAR.
120  case TYPE_CHAR:
121  case TYPE_VARCHAR:
122  return AnyValUtil::FromString(ctx, Substitute("$0($1)", type_string,
123  type_desc.len));
124  default:
125  return AnyValUtil::FromString(ctx, type_string);
126  }
127 }
128 
129 template StringVal UtilityFunctions::TypeOf(FunctionContext* ctx, const BooleanVal& input_val);
130 template StringVal UtilityFunctions::TypeOf(FunctionContext* ctx, const TinyIntVal& input_val);
131 template StringVal UtilityFunctions::TypeOf(FunctionContext* ctx, const SmallIntVal& input_val);
132 template StringVal UtilityFunctions::TypeOf(FunctionContext* ctx, const IntVal& input_val);
133 template StringVal UtilityFunctions::TypeOf(FunctionContext* ctx, const BigIntVal& input_val);
134 template StringVal UtilityFunctions::TypeOf(FunctionContext* ctx, const FloatVal& input_val);
135 template StringVal UtilityFunctions::TypeOf(FunctionContext* ctx, const DoubleVal& input_val);
136 template StringVal UtilityFunctions::TypeOf(FunctionContext* ctx, const StringVal& input_val);
137 template StringVal UtilityFunctions::TypeOf(FunctionContext* ctx, const TimestampVal& input_val);
138 template StringVal UtilityFunctions::TypeOf(FunctionContext* ctx, const DecimalVal& input_val);
139 }
int precision
Only valid if type == TYPE_DECIMAL.
Definition: udf.h:75
impala::FunctionContextImpl * impl()
TODO: Add mechanism for UDAs to update stats similar to runtime profile counters. ...
Definition: udf.h:202
__int128_t val16
Definition: udf.h:572
string GetVersionString(bool compact)
Returns "<program short name> version <GetBuildVersion(compact)>".
Definition: debug-util.cc:239
int32_t val
Definition: udf.h:421
This object has a compatible storage format with boost::ptime.
Definition: udf.h:495
uint8_t * ptr
Definition: udf.h:523
void SleepForMs(const int64_t duration_ms)
Sleeps the current thread for at least duration_ms milliseconds.
Definition: time.cc:21
string TypeToString(PrimitiveType t)
Definition: types.cc:73
bool is_null
Definition: udf.h:359
PrimitiveType type
Definition: types.h:60
const TypeDesc * GetArgType(int arg_idx) const
Definition: udf.cc:425
static uint64_t FnvHash(const void *data, int32_t bytes, uint64_t hash)
int GetByteSize() const
Returns the byte size of this type. Returns 0 for variable length types.
Definition: types.h:178
RuntimeState * state()
Definition: udf-internal.h:95
const TQueryCtx & query_ctx() const
const char * user() const
Definition: udf.cc:237
int len
Only valid if type == TYPE_FIXED_BUFFER || type == TYPE_VARCHAR.
Definition: udf.h:79