PageRenderTime 20ms CodeModel.GetById 2ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/release-0.0.0-rc0/hive/external/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDAFStd.java

#
Java | 92 lines | 57 code | 8 blank | 27 comment | 10 complexity | a6abacf20dc9ed5ef2d3b22e706ed01d MD5 | raw file
 1/**
 2 * Licensed to the Apache Software Foundation (ASF) under one
 3 * or more contributor license agreements.  See the NOTICE file
 4 * distributed with this work for additional information
 5 * regarding copyright ownership.  The ASF licenses this file
 6 * to you under the Apache License, Version 2.0 (the
 7 * "License"); you may not use this file except in compliance
 8 * with the License.  You may obtain a copy of the License at
 9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18package org.apache.hadoop.hive.ql.udf.generic;
19
20import org.apache.hadoop.hive.ql.exec.Description;
21import org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException;
22import org.apache.hadoop.hive.ql.metadata.HiveException;
23import org.apache.hadoop.hive.ql.parse.SemanticException;
24import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
25import org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo;
26import org.apache.hadoop.hive.serde2.typeinfo.TypeInfo;
27
28/**
29 * Compute the standard deviation by extending GenericUDAFVariance and
30 * overriding the terminate() method of the evaluator.
31 * 
32 */
33@Description(name = "std,stddev,stddev_pop",
34    value = "_FUNC_(x) - Returns the standard deviation of a set of numbers")
35public class GenericUDAFStd extends GenericUDAFVariance {
36
37  @Override
38  public GenericUDAFEvaluator getEvaluator(TypeInfo[] parameters)
39      throws SemanticException {
40    if (parameters.length != 1) {
41      throw new UDFArgumentTypeException(parameters.length - 1,
42          "Exactly one argument is expected.");
43    }
44
45    if (parameters[0].getCategory() != ObjectInspector.Category.PRIMITIVE) {
46      throw new UDFArgumentTypeException(0,
47          "Only primitive type arguments are accepted but "
48          + parameters[0].getTypeName() + " is passed.");
49    }
50    switch (((PrimitiveTypeInfo) parameters[0]).getPrimitiveCategory()) {
51    case BYTE:
52    case SHORT:
53    case INT:
54    case LONG:
55    case FLOAT:
56    case DOUBLE:
57    case STRING:
58      return new GenericUDAFStdEvaluator();
59    case BOOLEAN:
60    default:
61      throw new UDFArgumentTypeException(0,
62          "Only numeric or string type arguments are accepted but "
63          + parameters[0].getTypeName() + " is passed.");
64    }
65  }
66
67  /**
68   * Compute the standard deviation by extending GenericUDAFVarianceEvaluator
69   * and overriding the terminate() method of the evaluator.
70   * 
71   */
72  public static class GenericUDAFStdEvaluator extends
73      GenericUDAFVarianceEvaluator {
74
75    @Override
76    public Object terminate(AggregationBuffer agg) throws HiveException {
77      StdAgg myagg = (StdAgg) agg;
78
79      if (myagg.count == 0) { // SQL standard - return null for zero elements
80        return null;
81      } else {
82        if (myagg.count > 1) {
83          getResult().set(Math.sqrt(myagg.variance / (myagg.count)));
84        } else { // for one element the variance is always 0
85          getResult().set(0);
86        }
87        return getResult();
88      }
89    }
90  }
91
92}