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