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