From 37816179d0a07232a8e5837f43dd75b4ed49ed71 Mon Sep 17 00:00:00 2001 From: Arthur Peng Date: Wed, 21 Jan 2015 16:44:33 +0000 Subject: [PATCH] IMPALA-1597: add typeOf builtin typeOf returns the type of the given expression. e.g. typeOf(bigint_col) -> "BIGINT" --- be/src/exprs/expr-test.cc | 13 +++++++++++++ be/src/exprs/utility-functions.cc | 16 ++++++++++++++++ be/src/exprs/utility-functions.h | 3 +++ common/function-registry/impala_functions.py | 10 ++++++++++ 4 files changed, 42 insertions(+) diff --git a/be/src/exprs/expr-test.cc b/be/src/exprs/expr-test.cc index 4d24259..d2689f8 100644 --- a/be/src/exprs/expr-test.cc +++ b/be/src/exprs/expr-test.cc @@ -2008,6 +2008,19 @@ TEST_F(ExprTest, UtilityFunctions) { TestValue("sleep(100)", TYPE_BOOLEAN, true); TestIsNull("sleep(NULL)", TYPE_BOOLEAN); + // Test typeOf + TestStringValue("typeOf(!true)", "BOOL"); + TestStringValue("typeOf(1)", "TINYINT"); + TestStringValue("typeOf(128)", "SMALLINT"); + TestStringValue("typeOf(32768)", "INT"); + TestStringValue("typeOf(2147483648)", "BIGINT"); + TestStringValue("typeOf(32768)", "INT"); + TestStringValue("typeOf(cast(10 as FLOAT))", "FLOAT"); + TestStringValue("typeOf(cast(10 as DOUBLE))", "DOUBLE"); + TestStringValue("typeOf(current_database())", "STRING"); + TestStringValue("typeOf(now())", "TIMESTAMP"); + TestStringValue("typeOf(cast(10 as DECIMAL))", "DECIMAL"); + // Test fnv_hash string s("hello world"); uint64_t expected = HashUtil::FnvHash64(s.data(), s.size(), HashUtil::FNV_SEED); diff --git a/be/src/exprs/utility-functions.cc b/be/src/exprs/utility-functions.cc index 449ef54..87fae73 100644 --- a/be/src/exprs/utility-functions.cc +++ b/be/src/exprs/utility-functions.cc @@ -101,5 +101,21 @@ StringVal UtilityFunctions::CurrentDatabase(FunctionContext* ctx) { return (database.len > 0) ? database : StringVal::null(); } +template +StringVal UtilityFunctions::TypeOf(FunctionContext* ctx, const T& input_val) { + if (input_val.is_null) return StringVal::null(); + ColumnType type = AnyValUtil::TypeDescToColumnType(*(ctx->GetArgType(0))); + return AnyValUtil::FromString(ctx, TypeToString(type.type)); +} +template StringVal UtilityFunctions::TypeOf(FunctionContext* ctx, const BooleanVal& input_val); +template StringVal UtilityFunctions::TypeOf(FunctionContext* ctx, const TinyIntVal& input_val); +template StringVal UtilityFunctions::TypeOf(FunctionContext* ctx, const SmallIntVal& input_val); +template StringVal UtilityFunctions::TypeOf(FunctionContext* ctx, const IntVal& input_val); +template StringVal UtilityFunctions::TypeOf(FunctionContext* ctx, const BigIntVal& input_val); +template StringVal UtilityFunctions::TypeOf(FunctionContext* ctx, const FloatVal& input_val); +template StringVal UtilityFunctions::TypeOf(FunctionContext* ctx, const DoubleVal& input_val); +template StringVal UtilityFunctions::TypeOf(FunctionContext* ctx, const StringVal& input_val); +template StringVal UtilityFunctions::TypeOf(FunctionContext* ctx, const TimestampVal& input_val); +template StringVal UtilityFunctions::TypeOf(FunctionContext* ctx, const DecimalVal& input_val); } diff --git a/be/src/exprs/utility-functions.h b/be/src/exprs/utility-functions.h index 1737c92..848064e 100644 --- a/be/src/exprs/utility-functions.h +++ b/be/src/exprs/utility-functions.h @@ -52,6 +52,9 @@ class UtilityFunctions { // Implementation of the current_database() function. Returns the current default // database from the parent session of this query. static StringVal CurrentDatabase(FunctionContext* ctx); + + // Implementation of the typeOf() function. Returns the type of the input expression. + template static StringVal TypeOf(FunctionContext* ctx, const T& input_val); }; } diff --git a/common/function-registry/impala_functions.py b/common/function-registry/impala_functions.py index abf1f7c..3254b24 100755 --- a/common/function-registry/impala_functions.py +++ b/common/function-registry/impala_functions.py @@ -480,6 +480,16 @@ functions = [ [['sleep'], 'BOOLEAN', ['INT'], 'impala::UtilityFunctions::Sleep'], [['pid'], 'INT', [], 'impala::UtilityFunctions::Pid'], [['version'], 'STRING', [], 'impala::UtilityFunctions::Version'], + [['typeOf'], 'STRING', ['BOOLEAN'], '_ZN6impala16UtilityFunctions6TypeOfIN10impala_udf10BooleanValEEENS2_9StringValEPNS2_15FunctionContextERKT_'], + [['typeOf'], 'STRING', ['TINYINT'], '_ZN6impala16UtilityFunctions6TypeOfIN10impala_udf10TinyIntValEEENS2_9StringValEPNS2_15FunctionContextERKT_'], + [['typeOf'], 'STRING', ['SMALLINT'], '_ZN6impala16UtilityFunctions6TypeOfIN10impala_udf11SmallIntValEEENS2_9StringValEPNS2_15FunctionContextERKT_'], + [['typeOf'], 'STRING', ['INT'], '_ZN6impala16UtilityFunctions6TypeOfIN10impala_udf6IntValEEENS2_9StringValEPNS2_15FunctionContextERKT_'], + [['typeOf'], 'STRING', ['BIGINT'], '_ZN6impala16UtilityFunctions6TypeOfIN10impala_udf9BigIntValEEENS2_9StringValEPNS2_15FunctionContextERKT_'], + [['typeOf'], 'STRING', ['FLOAT'], '_ZN6impala16UtilityFunctions6TypeOfIN10impala_udf8FloatValEEENS2_9StringValEPNS2_15FunctionContextERKT_'], + [['typeOf'], 'STRING', ['DOUBLE'], '_ZN6impala16UtilityFunctions6TypeOfIN10impala_udf9DoubleValEEENS2_9StringValEPNS2_15FunctionContextERKT_'], + [['typeOf'], 'STRING', ['STRING'], '_ZN6impala16UtilityFunctions6TypeOfIN10impala_udf9StringValEEES3_PNS2_15FunctionContextERKT_'], + [['typeOf'], 'STRING', ['TIMESTAMP'], '_ZN6impala16UtilityFunctions6TypeOfIN10impala_udf12TimestampValEEENS2_9StringValEPNS2_15FunctionContextERKT_'], + [['typeOf'], 'STRING', ['DECIMAL'], '_ZN6impala16UtilityFunctions6TypeOfIN10impala_udf10DecimalValEEENS2_9StringValEPNS2_15FunctionContextERKT_'], [['fnv_hash'], 'BIGINT', ['TINYINT'], '_ZN6impala16UtilityFunctions7FnvHashIN10impala_udf10TinyIntValEEENS2_9BigIntValEPNS2_15FunctionContextERKT_'], [['fnv_hash'], 'BIGINT', ['SMALLINT'], -- 1.7.9.5