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