15 package com.cloudera.impala.catalog;
17 import java.util.List;
24 import com.cloudera.impala.thrift.TAggregateFunction;
25 import com.cloudera.impala.thrift.TCatalogObjectType;
26 import com.cloudera.impala.thrift.TColumnType;
27 import com.cloudera.impala.thrift.TFunction;
28 import com.cloudera.impala.thrift.TFunctionBinaryType;
29 import com.cloudera.impala.thrift.TScalarFunction;
30 import com.cloudera.impala.thrift.TSymbolLookupParams;
31 import com.cloudera.impala.thrift.TSymbolLookupResult;
32 import com.cloudera.impala.thrift.TSymbolType;
33 import com.google.common.base.Joiner;
34 import com.google.common.base.Preconditions;
35 import com.google.common.collect.Lists;
93 Type retType,
boolean varArgs) {
95 this.hasVarArgs_ = varArgs;
96 if (argTypes == null) {
99 this.argTypes_ = argTypes;
101 this.retType_ = retType;
102 this.userVisible_ =
true;
106 Type retType,
boolean varArgs) {
107 this(
name, (
Type[])null, retType, varArgs);
108 if (args.size() > 0) {
117 public String
dbName() {
return name_.getDb(); }
128 Preconditions.checkState(argTypes_.length > 0);
141 StringBuilder sb =
new StringBuilder();
142 sb.append(name_.getFunction())
144 .append(Joiner.on(
", ").join(
argTypes_));
147 return sb.toString();
152 if (!(o instanceof
Function))
return false;
163 Preconditions.checkState(
false);
179 for (
int i = 0; i < this.argTypes_.length; ++i) {
189 this.getVarArgsType())) {
201 Type[] promoted = argTypes_.clone();
202 for (
int i = 0; i < promoted.length; ++i) {
213 Preconditions.checkArgument(candidates.size() > 0);
214 if (candidates.size() == 1)
return candidates.get(0);
223 return candidates.get(0);
228 if (o.
argTypes_.length !=
this.argTypes_.length)
return false;
229 if (o.
hasVarArgs_ !=
this.hasVarArgs_)
return false;
230 for (
int i = 0; i < this.argTypes_.length; ++i) {
238 int minArgs = Math.min(o.argTypes_.length, this.argTypes_.length);
240 for (
int i = 0; i < minArgs; ++i) {
244 if (o.
argTypes_.length ==
this.argTypes_.length)
return true;
247 if (!o.
getVarArgsType().matchesType(this.getVarArgsType()))
return false;
249 for (
int i = minArgs; i < this.getNumArgs(); ++i) {
250 if (this.
argTypes_[i].isNull())
continue;
254 for (
int i = minArgs; i < o.getNumArgs(); ++i) {
263 for (
int i = minArgs; i < this.getNumArgs(); ++i) {
264 if (this.
argTypes_[i].isNull())
continue;
270 if (this.
getNumArgs() > minArgs)
return false;
271 for (
int i = minArgs; i < o.getNumArgs(); ++i) {
295 TFunction fn =
new TFunction();
297 fn.setName(name_.toThrift());
299 if (
location_ != null) fn.setHdfs_location(location_.toString());
300 fn.setArg_types(Type.toThrift(
argTypes_));
309 List<Type> argTypes = Lists.newArrayList();
310 for (TColumnType t: fn.getArg_types()) {
311 argTypes.add(Type.fromThrift(t));
315 if (fn.isSetScalar_fn()) {
316 TScalarFunction scalarFn = fn.getScalar_fn();
319 scalarFn.getSymbol(), scalarFn.getPrepare_fn_symbol(),
320 scalarFn.getClose_fn_symbol());
321 }
else if (fn.isSetAggregate_fn()) {
322 TAggregateFunction aggFn = fn.getAggregate_fn();
326 new HdfsUri(fn.getHdfs_location()), aggFn.getUpdate_fn_symbol(),
327 aggFn.getInit_fn_symbol(), aggFn.getSerialize_fn_symbol(),
328 aggFn.getMerge_fn_symbol(), aggFn.getGet_value_fn_symbol(),
329 null, aggFn.getFinalize_fn_symbol());
334 argTypes,
Type.
fromThrift(fn.getRet_type()), fn.isHas_var_args());
336 function.setBinaryType(fn.getBinary_type());
337 function.setHasVarArgs(fn.isHas_var_args());
350 if (symbol.length() == 0) {
356 throw new AnalysisException(
"Could not find symbol ''");
359 TSymbolLookupParams lookup =
new TSymbolLookupParams();
362 lookup.location =
binaryType_ != TFunctionBinaryType.BUILTIN ?
363 location_.toString() :
"";
364 lookup.symbol = symbol;
365 lookup.symbol_type = symbolType;
367 lookup.arg_types = Type.toThrift(argTypes);
369 if (retArgType != null) lookup.setRet_arg_type(retArgType.toThrift());
372 TSymbolLookupResult result = FeSupport.LookupSymbol(lookup);
373 switch (result.result_code) {
375 return result.symbol;
376 case BINARY_NOT_FOUND:
377 Preconditions.checkState(
binaryType_ != TFunctionBinaryType.BUILTIN);
378 throw new AnalysisException(
381 case SYMBOL_NOT_FOUND:
382 throw new AnalysisException(result.error_msg);
385 throw new AnalysisException(
"Internal Error");
390 throw new AnalysisException(
"Could not find symbol: " + symbol, e);
396 Preconditions.checkState(
397 symbolType == TSymbolType.UDF_PREPARE || symbolType == TSymbolType.UDF_CLOSE);
408 return "SmallIntVal";
422 return "TimestampVal";
426 Preconditions.checkState(
false, t.toString());
static String getUdfType(Type t)
void setLocation(HdfsUri loc)
TFunctionBinaryType getBinaryType()
static final ScalarType STRING
String lookupSymbol(String symbol, TSymbolType symbolType, Type retArgType, boolean hasVarArgs, Type...argTypes)
PrimitiveType getPrimitiveType()
static Type fromThrift(TColumnType thrift)
boolean matchesType(Type t)
static final long INITIAL_CATALOG_VERSION
Function selectClosestSuperType(List< Function > candidates)
void setCatalogVersion(long newVersion)
static boolean isImplicitlyCastable(Type t1, Type t2)
String lookupSymbol(String symbol, TSymbolType symbolType)
boolean isIdentical(Function o)
void setBinaryType(TFunctionBinaryType type)
Function promoteCharsToStrings()
static FunctionName fromThrift(TFunctionName fnName)
boolean isIndistinguishable(Function o)
void setName(FunctionName name)
TFunctionBinaryType binaryType_
Function(FunctionName name, Type[] argTypes, Type retType, boolean varArgs)
boolean equals(Object obj)
Function(FunctionName name, List< Type > args, Type retType, boolean varArgs)
void setUserVisible(boolean b)
void setHasVarArgs(boolean v)
static Function fromThrift(TFunction fn)
TCatalogObjectType getCatalogObjectType()
boolean compare(Function other, CompareMode mode)
FunctionName getFunctionName()
static final ScalarType INVALID
boolean isSuperTypeOf(Function other)