Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
CreateUdfStmt.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.analysis;
16 
17 import java.util.ArrayList;
18 import java.util.HashMap;
19 
20 import jline.internal.Preconditions;
21 
27 import com.cloudera.impala.thrift.TFunctionBinaryType;
28 import com.cloudera.impala.thrift.TSymbolType;
29 
33 public class CreateUdfStmt extends CreateFunctionStmtBase {
45  TypeDef retTypeDef, HdfsUri location, boolean ifNotExists,
46  HashMap<CreateFunctionStmtBase.OptArg, String> optArgs) {
47  super(fnName, args, retTypeDef, location, ifNotExists, optArgs);
48  }
49 
50  @Override
51  public void analyze(Analyzer analyzer) throws AnalysisException {
52  super.analyze(analyzer);
53  Preconditions.checkNotNull(fn_);
54  Preconditions.checkNotNull(fn_ instanceof ScalarFunction);
56 
57  if (udf.getBinaryType() == TFunctionBinaryType.HIVE) {
58  if (!udf.getReturnType().isScalarType()) {
59  throw new AnalysisException("Non-scalar return types not supported: "
60  + udf.getReturnType().toSql());
61  }
62  if (udf.getReturnType().isTimestamp()) {
63  throw new AnalysisException(
64  "Hive UDFs that use TIMESTAMP are not yet supported.");
65  }
66  if (udf.getReturnType().isDecimal()) {
67  throw new AnalysisException(
68  "Hive UDFs that use DECIMAL are not yet supported.");
69  }
70  for (int i = 0; i < udf.getNumArgs(); ++i) {
71  if (!udf.getArgs()[i].isScalarType()) {
72  throw new AnalysisException("Non-scalar argument types not supported: "
73  + udf.getArgs()[i].toSql());
74  }
75  if (udf.getArgs()[i].isTimestamp()) {
76  throw new AnalysisException(
77  "Hive UDFs that use TIMESTAMP are not yet supported.");
78  }
79  if (udf.getArgs()[i].isDecimal()) {
80  throw new AnalysisException(
81  "Hive UDFs that use DECIMAL are not yet supported.");
82  }
83  }
84  }
85 
86  if (udf.getReturnType().getPrimitiveType() == PrimitiveType.CHAR) {
87  throw new AnalysisException("UDFs that use CHAR are not yet supported.");
88  }
89  if (udf.getReturnType().getPrimitiveType() == PrimitiveType.VARCHAR) {
90  throw new AnalysisException("UDFs that use VARCHAR are not yet supported.");
91  }
92  for (int i = 0; i < udf.getNumArgs(); ++i) {
93  if (udf.getArgs()[i].getPrimitiveType() == PrimitiveType.CHAR) {
94  throw new AnalysisException("UDFs that use CHAR are not yet supported.");
95  }
96  if (udf.getArgs()[i].getPrimitiveType() == PrimitiveType.VARCHAR) {
97  throw new AnalysisException("UDFs that use VARCHAR are not yet supported.");
98  }
99  }
100 
101  // Check the user provided symbol exists
102  udf.setSymbolName(udf.lookupSymbol(
103  checkAndGetOptArg(OptArg.SYMBOL), TSymbolType.UDF_EVALUATE, null,
104  udf.hasVarArgs(), udf.getArgs()));
105 
106  // Set optional Prepare/Close functions
107  String prepareFn = optArgs_.get(OptArg.PREPARE_FN);
108  if (prepareFn != null) {
109  udf.setPrepareFnSymbol(udf.lookupSymbol(prepareFn, TSymbolType.UDF_PREPARE));
110  }
111  String closeFn = optArgs_.get(OptArg.CLOSE_FN);
112  if (closeFn != null) {
113  udf.setCloseFnSymbol(udf.lookupSymbol(closeFn, TSymbolType.UDF_CLOSE));
114  }
115 
116  // Udfs should not set any of these
122 
123  StringBuilder sb = new StringBuilder("CREATE ");
124  sb.append("FUNCTION ");
125  if (ifNotExists_) sb.append("IF NOT EXISTS ");
126  sb.append(udf.signatureString())
127  .append(" RETURNS ").append(udf.getReturnType())
128  .append(" LOCATION ").append(udf.getLocation())
129  .append(" SYMBOL=").append(udf.getSymbolName());
130  if (getComment() != null) sb.append(" COMMENT = '" + getComment() + "'");
131  sqlString_ = sb.toString();
132  }
133 
134  @Override
135  protected Function createFunction(FunctionName fnName, ArrayList<Type> argTypes, Type retType,
136  boolean hasVarArgs) {
137  return new ScalarFunction(fnName, argTypes, retType, hasVarArgs);
138  }
139 }
Function createFunction(FunctionName fnName, ArrayList< Type > argTypes, Type retType, boolean hasVarArgs)
CreateUdfStmt(FunctionName fnName, FunctionArgs args, TypeDef retTypeDef, HdfsUri location, boolean ifNotExists, HashMap< CreateFunctionStmtBase.OptArg, String > optArgs)
PrimitiveType
Definition: types.h:27