Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
AggregateFunction.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 
22 import com.cloudera.impala.thrift.TAggregateFunction;
23 import com.cloudera.impala.thrift.TFunction;
24 import com.cloudera.impala.thrift.TFunctionBinaryType;
25 
30 public class AggregateFunction extends Function {
31  // Set if different from retType_, null otherwise.
33 
34  // The symbol inside the binary at location_ that contains this particular.
35  // They can be null if it is not required.
36  private String updateFnSymbol_;
37  private String initFnSymbol_;
38  private String serializeFnSymbol_;
39  private String mergeFnSymbol_;
40  private String getValueFnSymbol_;
41  private String removeFnSymbol_;
42  private String finalizeFnSymbol_;
43 
44  private static String BE_BUILTINS_CLASS = "AggregateFunctions";
45 
46  // If true, this aggregate function should ignore distinct.
47  // e.g. min(distinct col) == min(col).
48  // TODO: currently it is not possible for user functions to specify this. We should
49  // extend the create aggregate function stmt to allow additional metadata like this.
50  private boolean ignoresDistinct_;
51 
52  // True if this function can appear within an analytic expr (fn() OVER(...)).
53  // TODO: Instead of manually setting this flag for all builtin aggregate functions
54  // we should identify this property from the function itself (e.g., based on which
55  // functions of the UDA API are implemented).
56  // Currently, there is no reliable way of doing that.
57  private boolean isAnalyticFn_;
58 
59  // True if this function can be used for aggregation (without an OVER() clause).
60  private boolean isAggregateFn_;
61 
62  // True if this function returns a non-null value on an empty input. It is used
63  // primarily during the rewrite of scalar subqueries.
64  // TODO: Instead of manually setting this flag, we should identify this
65  // property from the function itself (e.g. evaluating the function on an
66  // empty input in BE).
67  private boolean returnsNonNullOnEmpty_;
68 
69  public AggregateFunction(FunctionName fnName, ArrayList<Type> argTypes, Type retType,
70  boolean hasVarArgs) {
71  super(fnName, argTypes, retType, hasVarArgs);
72  }
73 
74  public AggregateFunction(FunctionName fnName, List<Type> argTypes,
75  Type retType, Type intermediateType,
76  HdfsUri location, String updateFnSymbol, String initFnSymbol,
77  String serializeFnSymbol, String mergeFnSymbol, String getValueFnSymbol,
78  String removeFnSymbol, String finalizeFnSymbol) {
79  super(fnName, argTypes, retType, false);
80  setLocation(location);
81  intermediateType_ = (intermediateType.equals(retType)) ? null : intermediateType;
82  updateFnSymbol_ = updateFnSymbol;
83  initFnSymbol_ = initFnSymbol;
84  serializeFnSymbol_ = serializeFnSymbol;
85  mergeFnSymbol_ = mergeFnSymbol;
86  getValueFnSymbol_ = getValueFnSymbol;
87  removeFnSymbol_ = removeFnSymbol;
88  finalizeFnSymbol_ = finalizeFnSymbol;
89  ignoresDistinct_ = false;
90  isAnalyticFn_ = false;
91  isAggregateFn_ = true;
92  returnsNonNullOnEmpty_ = false;
93  }
94 
95  public static AggregateFunction createBuiltin(Db db, String name,
96  List<Type> argTypes, Type retType, Type intermediateType,
97  String initFnSymbol, String updateFnSymbol, String mergeFnSymbol,
98  String serializeFnSymbol, String finalizeFnSymbol, boolean ignoresDistinct,
99  boolean isAnalyticFn, boolean returnsNonNullOnEmpty) {
100  return createBuiltin(db, name, argTypes, retType, intermediateType, initFnSymbol,
101  updateFnSymbol, mergeFnSymbol, serializeFnSymbol, null, null, finalizeFnSymbol,
102  ignoresDistinct, isAnalyticFn, returnsNonNullOnEmpty);
103  }
104 
105  public static AggregateFunction createBuiltin(Db db, String name,
106  List<Type> argTypes, Type retType, Type intermediateType,
107  String initFnSymbol, String updateFnSymbol, String mergeFnSymbol,
108  String serializeFnSymbol, String getValueFnSymbol, String removeFnSymbol,
109  String finalizeFnSymbol, boolean ignoresDistinct, boolean isAnalyticFn,
110  boolean returnsNonNullOnEmpty) {
112  argTypes, retType, intermediateType, null, updateFnSymbol, initFnSymbol,
113  serializeFnSymbol, mergeFnSymbol, getValueFnSymbol, removeFnSymbol,
114  finalizeFnSymbol);
115  fn.setBinaryType(TFunctionBinaryType.BUILTIN);
116  fn.ignoresDistinct_ = ignoresDistinct;
117  fn.isAnalyticFn_ = isAnalyticFn;
118  fn.isAggregateFn_ = true;
119  fn.returnsNonNullOnEmpty_ = returnsNonNullOnEmpty;
120  return fn;
121  }
122 
123  public static AggregateFunction createAnalyticBuiltin(Db db, String name,
124  List<Type> argTypes, Type retType, Type intermediateType) {
125  return createAnalyticBuiltin(db, name, argTypes, retType, intermediateType, null,
126  null, null, null, null, true);
127  }
128 
129  public static AggregateFunction createAnalyticBuiltin(Db db, String name,
130  List<Type> argTypes, Type retType, Type intermediateType,
131  String initFnSymbol, String updateFnSymbol, String removeFnSymbol,
132  String getValueFnSymbol, String finalizeFnSymbol) {
133  return createAnalyticBuiltin(db, name, argTypes, retType, intermediateType,
134  initFnSymbol, updateFnSymbol, removeFnSymbol, getValueFnSymbol, finalizeFnSymbol,
135  true);
136  }
137 
138  public static AggregateFunction createAnalyticBuiltin(Db db, String name,
139  List<Type> argTypes, Type retType, Type intermediateType,
140  String initFnSymbol, String updateFnSymbol, String removeFnSymbol,
141  String getValueFnSymbol, String finalizeFnSymbol, boolean isUserVisible) {
143  argTypes, retType, intermediateType, null, updateFnSymbol, initFnSymbol,
144  null, null, getValueFnSymbol, removeFnSymbol, finalizeFnSymbol);
145  fn.setBinaryType(TFunctionBinaryType.BUILTIN);
146  fn.ignoresDistinct_ = false;
147  fn.isAnalyticFn_ = true;
148  fn.isAggregateFn_ = false;
149  fn.returnsNonNullOnEmpty_ = false;
150  fn.setUserVisible(isUserVisible);
151  return fn;
152  }
153 
154  public String getUpdateFnSymbol() { return updateFnSymbol_; }
155  public String getInitFnSymbol() { return initFnSymbol_; }
156  public String getSerializeFnSymbol() { return serializeFnSymbol_; }
157  public String getMergeFnSymbol() { return mergeFnSymbol_; }
158  public String getGetValueFnSymbol() { return getValueFnSymbol_; }
159  public String getRemoveFnSymbol() { return removeFnSymbol_; }
160  public String getFinalizeFnSymbol() { return finalizeFnSymbol_; }
161  public boolean ignoresDistinct() { return ignoresDistinct_; }
162  public boolean isAnalyticFn() { return isAnalyticFn_; }
163  public boolean isAggregateFn() { return isAggregateFn_; }
164  public boolean returnsNonNullOnEmpty() { return returnsNonNullOnEmpty_; }
165 
171  public void setUpdateFnSymbol(String fn) { updateFnSymbol_ = fn; }
172  public void setInitFnSymbol(String fn) { initFnSymbol_ = fn; }
173  public void setSerializeFnSymbol(String fn) { serializeFnSymbol_ = fn; }
174  public void setMergeFnSymbol(String fn) { mergeFnSymbol_ = fn; }
175  public void setGetValueFnSymbol(String fn) { getValueFnSymbol_ = fn; }
176  public void setRemoveFnSymbol(String fn) { removeFnSymbol_ = fn; }
177  public void setFinalizeFnSymbol(String fn) { finalizeFnSymbol_ = fn; }
179 
180  @Override
181  public TFunction toThrift() {
182  TFunction fn = super.toThrift();
183  TAggregateFunction agg_fn = new TAggregateFunction();
184  agg_fn.setUpdate_fn_symbol(updateFnSymbol_);
185  agg_fn.setInit_fn_symbol(initFnSymbol_);
186  if (serializeFnSymbol_ != null) agg_fn.setSerialize_fn_symbol(serializeFnSymbol_);
187  agg_fn.setMerge_fn_symbol(mergeFnSymbol_);
188  if (getValueFnSymbol_ != null) agg_fn.setGet_value_fn_symbol(getValueFnSymbol_);
189  if (removeFnSymbol_ != null) agg_fn.setRemove_fn_symbol(removeFnSymbol_);
190  if (finalizeFnSymbol_ != null) agg_fn.setFinalize_fn_symbol(finalizeFnSymbol_);
191  if (intermediateType_ != null) {
192  agg_fn.setIntermediate_type(intermediateType_.toThrift());
193  } else {
194  agg_fn.setIntermediate_type(getReturnType().toThrift());
195  }
196  agg_fn.setIgnores_distinct(ignoresDistinct_);
197  fn.setAggregate_fn(agg_fn);
198  return fn;
199  }
200 }
AggregateFunction(FunctionName fnName, List< Type > argTypes, Type retType, Type intermediateType, HdfsUri location, String updateFnSymbol, String initFnSymbol, String serializeFnSymbol, String mergeFnSymbol, String getValueFnSymbol, String removeFnSymbol, String finalizeFnSymbol)
static AggregateFunction createAnalyticBuiltin(Db db, String name, List< Type > argTypes, Type retType, Type intermediateType)
static AggregateFunction createBuiltin(Db db, String name, List< Type > argTypes, Type retType, Type intermediateType, String initFnSymbol, String updateFnSymbol, String mergeFnSymbol, String serializeFnSymbol, String finalizeFnSymbol, boolean ignoresDistinct, boolean isAnalyticFn, boolean returnsNonNullOnEmpty)
static AggregateFunction createAnalyticBuiltin(Db db, String name, List< Type > argTypes, Type retType, Type intermediateType, String initFnSymbol, String updateFnSymbol, String removeFnSymbol, String getValueFnSymbol, String finalizeFnSymbol, boolean isUserVisible)
AggregateFunction(FunctionName fnName, ArrayList< Type > argTypes, Type retType, boolean hasVarArgs)
static AggregateFunction createAnalyticBuiltin(Db db, String name, List< Type > argTypes, Type retType, Type intermediateType, String initFnSymbol, String updateFnSymbol, String removeFnSymbol, String getValueFnSymbol, String finalizeFnSymbol)
static AggregateFunction createBuiltin(Db db, String name, List< Type > argTypes, Type retType, Type intermediateType, String initFnSymbol, String updateFnSymbol, String mergeFnSymbol, String serializeFnSymbol, String getValueFnSymbol, String removeFnSymbol, String finalizeFnSymbol, boolean ignoresDistinct, boolean isAnalyticFn, boolean returnsNonNullOnEmpty)
string name
Definition: cpu-info.cc:50