Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ScalarFunction.java
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 
15 package com.cloudera.impala.catalog;
16 
17 import java.util.ArrayList;
18 import java.util.List;
19 
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;
29 
33 public class ScalarFunction extends Function {
34  // The name inside the binary at location_ that contains this particular
35  // function. e.g. org.example.MyUdf.class.
36  private String symbolName_;
37  private String prepareFnSymbol_;
38  private String closeFnSymbol_;
39 
40  public ScalarFunction(FunctionName fnName, ArrayList<Type> argTypes, Type retType,
41  boolean hasVarArgs) {
42  super(fnName, argTypes, retType, hasVarArgs);
43  }
44 
45  public ScalarFunction(FunctionName fnName, List<Type> argTypes,
46  Type retType, HdfsUri location, String symbolName, String initFnSymbol,
47  String closeFnSymbol) {
48  super(fnName, argTypes, retType, false);
49  setLocation(location);
50  setSymbolName(symbolName);
51  setPrepareFnSymbol(initFnSymbol);
52  setCloseFnSymbol(closeFnSymbol);
53  }
54 
59  public static ScalarFunction createBuiltin(String name, ArrayList<Type> argTypes,
60  boolean hasVarArgs, Type retType, String symbol,
61  String prepareFnSymbol, String closeFnSymbol, boolean isOperator) {
62  Preconditions.checkNotNull(symbol);
64  new FunctionName(Catalog.BUILTINS_DB, name), argTypes, retType, hasVarArgs);
65  fn.setBinaryType(TFunctionBinaryType.BUILTIN);
66  fn.setUserVisible(!isOperator);
67  try {
68  fn.symbolName_ = fn.lookupSymbol(symbol, TSymbolType.UDF_EVALUATE, null,
69  fn.hasVarArgs(), fn.getArgs());
70  } catch (AnalysisException e) {
71  // This should never happen
72  throw new RuntimeException("Builtin symbol '" + symbol + "'" + argTypes
73  + " not found!", e);
74  }
75  if (prepareFnSymbol != null) {
76  try {
77  fn.prepareFnSymbol_ = fn.lookupSymbol(prepareFnSymbol, TSymbolType.UDF_PREPARE);
78  } catch (AnalysisException e) {
79  // This should never happen
80  throw new RuntimeException(
81  "Builtin symbol '" + prepareFnSymbol + "' not found!", e);
82  }
83  }
84  if (closeFnSymbol != null) {
85  try {
86  fn.closeFnSymbol_ = fn.lookupSymbol(closeFnSymbol, TSymbolType.UDF_CLOSE);
87  } catch (AnalysisException e) {
88  // This should never happen
89  throw new RuntimeException(
90  "Builtin symbol '" + closeFnSymbol + "' not found!", e);
91  }
92  }
93  return fn;
94  }
95 
103  ArrayList<Type> argTypes, Type retType) {
104  // Operators have a well defined symbol based on the function name and type.
105  // Convert Add(TINYINT, TINYINT) --> Add_TinyIntVal_TinyIntVal
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()) {
110  case BOOLEAN:
111  beFn += "_BooleanVal";
112  break;
113  case TINYINT:
114  beFn += "_TinyIntVal";
115  break;
116  case SMALLINT:
117  beFn += "_SmallIntVal";
118  break;
119  case INT:
120  beFn += "_IntVal";
121  break;
122  case BIGINT:
123  beFn += "_BigIntVal";
124  break;
125  case FLOAT:
126  beFn += "_FloatVal";
127  break;
128  case DOUBLE:
129  beFn += "_DoubleVal";
130  break;
131  case STRING:
132  case VARCHAR:
133  beFn += "_StringVal";
134  break;
135  case CHAR:
136  beFn += "_Char";
137  break;
138  case TIMESTAMP:
139  beFn += "_TimestampVal";
140  break;
141  case DECIMAL:
142  beFn += "_DecimalVal";
143  usesDecimal = true;
144  break;
145  default:
146  Preconditions.checkState(false,
147  "Argument type not supported: " + argTypes.get(i).toSql());
148  }
149  }
150  String beClass = usesDecimal ? "DecimalOperators" : "Operators";
151  String symbol = "impala::" + beClass + "::" + beFn;
152  return createBuiltinOperator(name, symbol, argTypes, retType);
153  }
154 
155  public static ScalarFunction createBuiltinOperator(String name, String symbol,
156  ArrayList<Type> argTypes, Type retType) {
157  return createBuiltin(name, symbol, argTypes, false, retType, false);
158  }
159 
160  public static ScalarFunction createBuiltin(String name, String symbol,
161  ArrayList<Type> argTypes, boolean hasVarArgs, Type retType,
162  boolean userVisible) {
164  new FunctionName(Catalog.BUILTINS_DB, name), argTypes, retType, hasVarArgs);
165  fn.setBinaryType(TFunctionBinaryType.BUILTIN);
166  fn.setUserVisible(userVisible);
167  try {
168  fn.symbolName_ = fn.lookupSymbol(symbol, TSymbolType.UDF_EVALUATE, null,
169  fn.hasVarArgs(), fn.getArgs());
170  } catch (AnalysisException e) {
171  // This should never happen
172  Preconditions.checkState(false, "Builtin symbol '" + symbol + "'" + argTypes
173  + " not found!" + e.getStackTrace());
174  throw new RuntimeException("Builtin symbol not found!", e);
175  }
176  return fn;
177  }
178 
183  public static ScalarFunction createBuiltinSearchDesc(String name, Type[] argTypes,
184  boolean hasVarArgs) {
185  ArrayList<Type> fnArgs =
186  (argTypes == null) ? new ArrayList<Type>() : Lists.newArrayList(argTypes);
188  new FunctionName(Catalog.BUILTINS_DB, name), fnArgs, Type.INVALID, hasVarArgs);
189  fn.setBinaryType(TFunctionBinaryType.BUILTIN);
190  return fn;
191  }
192 
193  public void setSymbolName(String s) { symbolName_ = s; }
194  public void setPrepareFnSymbol(String s) { prepareFnSymbol_ = s; }
195  public void setCloseFnSymbol(String s) { closeFnSymbol_ = s; }
196 
197  public String getSymbolName() { return symbolName_; }
198  public String getPrepareFnSymbol() { return prepareFnSymbol_; }
199  public String getCloseFnSymbol() { return closeFnSymbol_; }
200 
201  @Override
202  public TFunction toThrift() {
203  TFunction fn = super.toThrift();
204  fn.setScalar_fn(new TScalarFunction());
205  fn.getScalar_fn().setSymbol(symbolName_);
206  if (prepareFnSymbol_ != null) fn.getScalar_fn().setPrepare_fn_symbol(prepareFnSymbol_);
207  if (closeFnSymbol_ != null) fn.getScalar_fn().setClose_fn_symbol(closeFnSymbol_);
208  return fn;
209  }
210 }
static ScalarFunction createBuiltinSearchDesc(String name, Type[] argTypes, boolean hasVarArgs)
static ScalarFunction createBuiltinOperator(String name, String symbol, ArrayList< Type > argTypes, Type retType)
static final String BUILTINS_DB
Definition: Catalog.java:61
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)
static ScalarFunction createBuiltin(String name, ArrayList< Type > argTypes, boolean hasVarArgs, Type retType, String symbol, String prepareFnSymbol, String closeFnSymbol, boolean isOperator)
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)
string name
Definition: cpu-info.cc:50
static final ScalarType INVALID
Definition: Type.java:44