PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Java | 84 lines | 55 code | 7 blank | 22 comment | 15 complexity | 7a100b11bd47f0e125b0d6c0ebface10 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.UDFArgumentException;
  21. import org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException;
  22. import org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException;
  23. import org.apache.hadoop.hive.ql.metadata.HiveException;
  24. import org.apache.hadoop.hive.serde.Constants;
  25. import org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector;
  26. import org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector;
  27. import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
  28. import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector.Category;
  29. import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
  30. import org.apache.hadoop.io.IntWritable;
  31. /**
  32. * GenericUDFSize.
  33. *
  34. */
  35. @Description(name = "size", value = "_FUNC_(a) - Returns the size of a")
  36. public class GenericUDFSize extends GenericUDF {
  37. private ObjectInspector returnOI;
  38. private final IntWritable result = new IntWritable(-1);
  39. @Override
  40. public ObjectInspector initialize(ObjectInspector[] arguments)
  41. throws UDFArgumentException {
  42. if (arguments.length != 1) {
  43. throw new UDFArgumentLengthException(
  44. "The function SIZE only accepts 1 argument.");
  45. }
  46. Category category = arguments[0].getCategory();
  47. String typeName = arguments[0].getTypeName();
  48. if (category != Category.MAP && category != Category.LIST
  49. && !typeName.equals(Constants.VOID_TYPE_NAME)) {
  50. throw new UDFArgumentTypeException(0, "\""
  51. + Category.MAP.toString().toLowerCase() + "\" or \""
  52. + Category.LIST.toString().toLowerCase()
  53. + "\" is expected at function SIZE, " + "but \""
  54. + arguments[0].getTypeName() + "\" is found");
  55. }
  56. returnOI = arguments[0];
  57. return PrimitiveObjectInspectorFactory.writableIntObjectInspector;
  58. }
  59. @Override
  60. public Object evaluate(DeferredObject[] arguments) throws HiveException {
  61. Object data = arguments[0].get();
  62. if (returnOI.getCategory() == Category.MAP) {
  63. result.set(((MapObjectInspector) returnOI).getMapSize(data));
  64. } else if (returnOI.getCategory() == Category.LIST) {
  65. result.set(((ListObjectInspector) returnOI).getListLength(data));
  66. } else if (returnOI.getTypeName().equals(Constants.VOID_TYPE_NAME)) {
  67. // null
  68. result.set(-1);
  69. }
  70. return result;
  71. }
  72. @Override
  73. public String getDisplayString(String[] children) {
  74. assert (children.length == 1);
  75. return "size(" + children[0] + ")";
  76. }
  77. }