15 package com.cloudera.impala.catalog;
17 import java.util.ArrayList;
18 import java.util.List;
23 import com.cloudera.impala.thrift.TFunction;
24 import com.cloudera.impala.thrift.TFunctionBinaryType;
25 import com.cloudera.impala.thrift.TScalarFunction;
26 import com.cloudera.impala.thrift.TSymbolType;
27 import com.google.common.base.Preconditions;
28 import com.google.common.collect.Lists;
42 super(fnName, argTypes, retType, hasVarArgs);
46 Type retType,
HdfsUri location, String symbolName, String initFnSymbol,
47 String closeFnSymbol) {
48 super(fnName, argTypes, retType,
false);
61 String prepareFnSymbol, String closeFnSymbol,
boolean isOperator) {
62 Preconditions.checkNotNull(symbol);
65 fn.setBinaryType(TFunctionBinaryType.BUILTIN);
66 fn.setUserVisible(!isOperator);
68 fn.symbolName_ = fn.lookupSymbol(symbol, TSymbolType.UDF_EVALUATE, null,
69 fn.hasVarArgs(), fn.getArgs());
72 throw new RuntimeException(
"Builtin symbol '" + symbol +
"'" + argTypes
75 if (prepareFnSymbol != null) {
77 fn.prepareFnSymbol_ = fn.lookupSymbol(prepareFnSymbol, TSymbolType.UDF_PREPARE);
80 throw new RuntimeException(
81 "Builtin symbol '" + prepareFnSymbol +
"' not found!", e);
84 if (closeFnSymbol != null) {
86 fn.closeFnSymbol_ = fn.lookupSymbol(closeFnSymbol, TSymbolType.UDF_CLOSE);
89 throw new RuntimeException(
90 "Builtin symbol '" + closeFnSymbol +
"' not found!", e);
103 ArrayList<Type> argTypes,
Type retType) {
106 String beFn = Character.toUpperCase(name.charAt(0)) + name.substring(1);
107 boolean usesDecimal =
false;
108 for (
int i = 0; i < argTypes.size(); ++i) {
109 switch (argTypes.get(i).getPrimitiveType()) {
111 beFn +=
"_BooleanVal";
114 beFn +=
"_TinyIntVal";
117 beFn +=
"_SmallIntVal";
123 beFn +=
"_BigIntVal";
129 beFn +=
"_DoubleVal";
133 beFn +=
"_StringVal";
139 beFn +=
"_TimestampVal";
142 beFn +=
"_DecimalVal";
146 Preconditions.checkState(
false,
147 "Argument type not supported: " + argTypes.get(i).toSql());
150 String beClass = usesDecimal ?
"DecimalOperators" :
"Operators";
151 String symbol =
"impala::" + beClass +
"::" + beFn;
156 ArrayList<Type> argTypes,
Type retType) {
157 return createBuiltin(name, symbol, argTypes,
false, retType,
false);
165 fn.setBinaryType(TFunctionBinaryType.BUILTIN);
168 fn.symbolName_ = fn.lookupSymbol(symbol, TSymbolType.UDF_EVALUATE, null,
169 fn.hasVarArgs(), fn.
getArgs());
172 Preconditions.checkState(
false,
"Builtin symbol '" + symbol +
"'" + argTypes
173 +
" not found!" + e.getStackTrace());
174 throw new RuntimeException(
"Builtin symbol not found!", e);
185 ArrayList<Type> fnArgs =
186 (argTypes == null) ?
new ArrayList<Type>() : Lists.newArrayList(argTypes);
189 fn.setBinaryType(TFunctionBinaryType.BUILTIN);
203 TFunction fn = super.toThrift();
204 fn.setScalar_fn(
new TScalarFunction());
void setCloseFnSymbol(String s)
static ScalarFunction createBuiltinSearchDesc(String name, Type[] argTypes, boolean hasVarArgs)
void setLocation(HdfsUri loc)
static ScalarFunction createBuiltinOperator(String name, String symbol, ArrayList< Type > argTypes, Type retType)
static final String BUILTINS_DB
static ScalarFunction createBuiltin(String name, String symbol, ArrayList< Type > argTypes, boolean hasVarArgs, Type retType, boolean userVisible)
ScalarFunction(FunctionName fnName, ArrayList< Type > argTypes, Type retType, boolean hasVarArgs)
void setSymbolName(String s)
static ScalarFunction createBuiltin(String name, ArrayList< Type > argTypes, boolean hasVarArgs, Type retType, String symbol, String prepareFnSymbol, String closeFnSymbol, boolean isOperator)
String getCloseFnSymbol()
String getPrepareFnSymbol()
ScalarFunction(FunctionName fnName, List< Type > argTypes, Type retType, HdfsUri location, String symbolName, String initFnSymbol, String closeFnSymbol)
static ScalarFunction createBuiltinOperator(String name, ArrayList< Type > argTypes, Type retType)
void setPrepareFnSymbol(String s)
static final ScalarType INVALID