PageRenderTime 44ms CodeModel.GetById 21ms 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
Possible License(s): Apache-2.0, BSD-3-Clause, JSON, CPL-1.0
  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. */
  18. package org.apache.hadoop.hive.ql.udf.generic;
  19. import org.apache.hadoop.hive.ql.exec.Description;
  20. import org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException;
  21. import org.apache.hadoop.hive.ql.metadata.HiveException;
  22. import org.apache.hadoop.hive.ql.parse.SemanticException;
  23. import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
  24. import org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo;
  25. import org.apache.hadoop.hive.serde2.typeinfo.TypeInfo;
  26. /**
  27. * Compute the standard deviation by extending GenericUDAFVariance and
  28. * overriding the terminate() method of the evaluator.
  29. *
  30. */
  31. @Description(name = "std,stddev,stddev_pop",
  32. value = "_FUNC_(x) - Returns the standard deviation of a set of numbers")
  33. public class GenericUDAFStd extends GenericUDAFVariance {
  34. @Override
  35. public GenericUDAFEvaluator getEvaluator(TypeInfo[] parameters)
  36. throws SemanticException {
  37. if (parameters.length != 1) {
  38. throw new UDFArgumentTypeException(parameters.length - 1,
  39. "Exactly one argument is expected.");
  40. }
  41. if (parameters[0].getCategory() != ObjectInspector.Category.PRIMITIVE) {
  42. throw new UDFArgumentTypeException(0,
  43. "Only primitive type arguments are accepted but "
  44. + parameters[0].getTypeName() + " is passed.");
  45. }
  46. switch (((PrimitiveTypeInfo) parameters[0]).getPrimitiveCategory()) {
  47. case BYTE:
  48. case SHORT:
  49. case INT:
  50. case LONG:
  51. case FLOAT:
  52. case DOUBLE:
  53. case STRING:
  54. return new GenericUDAFStdEvaluator();
  55. case BOOLEAN:
  56. default:
  57. throw new UDFArgumentTypeException(0,
  58. "Only numeric or string type arguments are accepted but "
  59. + parameters[0].getTypeName() + " is passed.");
  60. }
  61. }
  62. /**
  63. * Compute the standard deviation by extending GenericUDAFVarianceEvaluator
  64. * and overriding the terminate() method of the evaluator.
  65. *
  66. */
  67. public static class GenericUDAFStdEvaluator extends
  68. GenericUDAFVarianceEvaluator {
  69. @Override
  70. public Object terminate(AggregationBuffer agg) throws HiveException {
  71. StdAgg myagg = (StdAgg) agg;
  72. if (myagg.count == 0) { // SQL standard - return null for zero elements
  73. return null;
  74. } else {
  75. if (myagg.count > 1) {
  76. getResult().set(Math.sqrt(myagg.variance / (myagg.count)));
  77. } else { // for one element the variance is always 0
  78. getResult().set(0);
  79. }
  80. return getResult();
  81. }
  82. }
  83. }
  84. }