Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
FunctionName.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 
22 import com.cloudera.impala.thrift.TFunctionName;
23 import com.google.common.base.Joiner;
24 import com.google.common.base.Preconditions;
25 
30 public class FunctionName {
31  // Only set for parsed function names.
32  private final ArrayList<String> fnNamePath_;
33 
34  // Set/validated during analysis.
35  private String db_;
36  private String fn_;
37  private boolean isBuiltin_ = false;
38  private boolean isAnalyzed_ = false;
39 
44  public FunctionName(ArrayList<String> fnNamePath) {
45  fnNamePath_ = fnNamePath;
46  }
47 
48  public FunctionName(String dbName, String fn) {
49  db_ = (dbName != null) ? dbName.toLowerCase() : null;
50  fn_ = fn.toLowerCase();
51  fnNamePath_ = null;
52  }
53 
54  public FunctionName(String fn) {
55  this(null, fn);
56  }
57 
58  @Override
59  public boolean equals(Object obj) {
60  if (!(obj instanceof FunctionName)) return false;
61  FunctionName o = (FunctionName)obj;
62  if ((db_ == null || o.db_ == null) && (db_ != o.db_)) {
63  if (db_ == null && o.db_ != null) return false;
64  if (db_ != null && o.db_ == null) return false;
65  if (!db_.equalsIgnoreCase(o.db_)) return false;
66  }
67  return fn_.equalsIgnoreCase(o.fn_);
68  }
69 
70  public String getDb() { return db_; }
71  public String getFunction() { return fn_; }
72  public boolean isFullyQualified() { return db_ != null; }
73  public boolean isBuiltin() { return isBuiltin_; }
74  public ArrayList<String> getFnNamePath() { return fnNamePath_; }
75 
76  @Override
77  public String toString() {
78  // The fnNamePath_ is not always set.
79  if (!isAnalyzed_ && fnNamePath_ != null) return Joiner.on(".").join(fnNamePath_);
80  if (db_ == null || isBuiltin_) return fn_;
81  return db_ + "." + fn_;
82  }
83 
84  public void analyze(Analyzer analyzer) throws AnalysisException {
85  if (isAnalyzed_) return;
87  if (fn_.isEmpty()) throw new AnalysisException("Function name cannot be empty.");
88  for (int i = 0; i < fn_.length(); ++i) {
89  if (!isValidCharacter(fn_.charAt(i))) {
90  throw new AnalysisException(
91  "Function names must be all alphanumeric or underscore. " +
92  "Invalid name: " + fn_);
93  }
94  }
95  if (Character.isDigit(fn_.charAt(0))) {
96  throw new AnalysisException("Function cannot start with a digit: " + fn_);
97  }
98 
99  // Resolve the database for this function.
100  if (!isFullyQualified()) {
101  Db builtinDb = analyzer.getCatalog().getBuiltinsDb();
102  if (builtinDb.containsFunction(fn_)) {
103  // If it isn't fully qualified and is the same name as a builtin, use
104  // the builtin.
106  isBuiltin_ = true;
107  } else {
108  db_ = analyzer.getDefaultDb();
109  isBuiltin_ = false;
110  }
111  } else {
112  isBuiltin_ = db_.equals(Catalog.BUILTINS_DB);
113  }
114  isAnalyzed_ = true;
115  }
116 
117  private void analyzeFnNamePath() throws AnalysisException {
118  if (fnNamePath_ == null) return;
119  if (fnNamePath_.size() > 2 || fnNamePath_.isEmpty()) {
120  throw new AnalysisException(
121  String.format("Invalid function name: '%s'. Expected [dbname].funcname.",
122  Joiner.on(".").join(fnNamePath_)));
123  } else if (fnNamePath_.size() > 1) {
124  db_ = fnNamePath_.get(0);
125  fn_ = fnNamePath_.get(1).toLowerCase();
126  } else {
127  Preconditions.checkState(fnNamePath_.size() == 1);
128  fn_ = fnNamePath_.get(0).toLowerCase();
129  }
130  }
131 
132  private boolean isValidCharacter(char c) {
133  return Character.isLetterOrDigit(c) || c == '_';
134  }
135 
136  public TFunctionName toThrift() {
137  TFunctionName name = new TFunctionName(fn_);
138  name.setDb_name(db_);
139  return name;
140  }
141 
142  public static FunctionName fromThrift(TFunctionName fnName) {
143  return new FunctionName(fnName.getDb_name(), fnName.getFunction_name());
144  }
145 }
final ArrayList< String > fnNamePath_
static final String BUILTINS_DB
Definition: Catalog.java:61
FunctionName(String dbName, String fn)
static FunctionName fromThrift(TFunctionName fnName)
FunctionName(ArrayList< String > fnNamePath)
boolean containsFunction(String name)
Definition: Db.java:131
string name
Definition: cpu-info.cc:50