PageRenderTime 27ms CodeModel.GetById 17ms app.highlight 7ms RepoModel.GetById 2ms app.codeStats 0ms

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

#
Java | 72 lines | 48 code | 7 blank | 17 comment | 3 complexity | 9b369bb0039ec668ba716ebc220bbbd7 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 java.util.Arrays;
22
23import org.apache.commons.logging.Log;
24import org.apache.commons.logging.LogFactory;
25import org.apache.hadoop.hive.ql.exec.Description;
26import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
27import org.apache.hadoop.hive.ql.metadata.HiveException;
28import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
29import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory;
30import org.apache.hadoop.hive.serde2.objectinspector.StandardUnionObjectInspector.StandardUnion;
31import org.apache.hadoop.hive.serde2.objectinspector.primitive.IntObjectInspector;
32
33@Description(name = "create_union", value = "_FUNC_(tag, obj1, obj2, obj3, ...)"
34   + " - Creates a union with the object for given tag",
35   extended = "Example:\n"
36   + "  > SELECT _FUNC_(1, 1, \"one\") FROM src LIMIT 1;\n" + "  one")
37public class GenericUDFUnion extends GenericUDF {
38  Log LOG = LogFactory.getLog("GenericUDFUnion");
39  ObjectInspector tagOI;
40
41  @Override
42  public ObjectInspector initialize(ObjectInspector[] arguments)
43      throws UDFArgumentException {
44    tagOI = arguments[0];
45    ObjectInspector[] unionOIs = new ObjectInspector[arguments.length-1];
46    for (int i = 1; i < arguments.length; i++) {
47      unionOIs[i-1] = arguments[i];
48    }
49    return ObjectInspectorFactory.getStandardUnionObjectInspector(
50        Arrays.asList(unionOIs));
51  }
52
53  @Override
54  public Object evaluate(DeferredObject[] arguments) throws HiveException {
55    byte tag = (byte)((IntObjectInspector)tagOI).get(arguments[0].get());
56    return new StandardUnion(tag, arguments[tag + 1].get());
57  }
58
59  @Override
60  public String getDisplayString(String[] children) {
61    StringBuilder sb = new StringBuilder();
62    sb.append("create_union(");
63    for (int i = 0; i < children.length; i++) {
64      if (i > 0) {
65        sb.append(',');
66      }
67      sb.append(children[i]);
68    }
69    sb.append(')');
70    return sb.toString();
71  }
72}