PageRenderTime 32ms CodeModel.GetById 14ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/release-0.1-rc2/hive/external/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFStruct.java

#
Java | 73 lines | 47 code | 9 blank | 17 comment | 4 complexity | c098e627612c73765c8a6d85e69d660f 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.ArrayList;
22import java.util.Arrays;
23
24import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
25import org.apache.hadoop.hive.ql.exec.Description;
26import org.apache.hadoop.hive.ql.metadata.HiveException;
27import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
28import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory;
29import org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector;
30
31@Description(name = "struct",
32    value = "_FUNC_(col1, col2, col3, ...) - Creates a struct with the given field values")
33public class GenericUDFStruct extends GenericUDF {
34  Object[] ret;
35
36  @Override
37  public ObjectInspector initialize(ObjectInspector[] arguments)
38      throws UDFArgumentException {
39    
40    int numFields = arguments.length;
41    ret = new Object[numFields];
42    
43    ArrayList<String> fname = new ArrayList<String>(numFields);
44    for (int f = 1; f <= numFields; f++) {
45      fname.add("col" + f);
46    }
47    StructObjectInspector soi = 
48      ObjectInspectorFactory.getStandardStructObjectInspector(fname, Arrays.asList(arguments));
49    return soi;
50  }
51
52  @Override
53  public Object evaluate(DeferredObject[] arguments) throws HiveException {
54    for (int i = 0; i < arguments.length; i++) {
55      ret[i] = arguments[i].get();
56    }
57    return ret;
58  }
59
60  @Override
61  public String getDisplayString(String[] children) {
62    StringBuilder sb = new StringBuilder();
63    sb.append("struct(");
64    for (int i = 0; i < children.length; i++) {
65      if (i > 0) {
66        sb.append(',');
67      }
68      sb.append(children[i]);
69    }
70    sb.append(')');
71    return sb.toString();
72  }
73}