PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Java | 84 lines | 55 code | 9 blank | 20 comment | 19 complexity | 26d0f7b1b690fd1b8f53bcd7b5d8a523 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 org.apache.hadoop.hive.ql.exec.Description;
  20. import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
  21. import org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException;
  22. import org.apache.hadoop.hive.ql.metadata.HiveException;
  23. import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
  24. import org.apache.hadoop.hive.serde2.objectinspector.primitive.BooleanObjectInspector;
  25. import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
  26. import org.apache.hadoop.io.BooleanWritable;
  27. /**
  28. * GenericUDF Class for computing and.
  29. */
  30. @Description(name = "and", value = "a _FUNC_ b - Logical and")
  31. public class GenericUDFOPAnd extends GenericUDF {
  32. private final BooleanWritable result = new BooleanWritable();
  33. BooleanObjectInspector boi0,boi1;
  34. @Override
  35. public ObjectInspector initialize(ObjectInspector[] arguments)
  36. throws UDFArgumentException {
  37. if (arguments.length != 2) {
  38. throw new UDFArgumentLengthException(
  39. "The operator 'AND' only accepts 2 argument.");
  40. }
  41. boi0 = (BooleanObjectInspector) arguments[0];
  42. boi1 = (BooleanObjectInspector) arguments[1];
  43. return PrimitiveObjectInspectorFactory.writableBooleanObjectInspector;
  44. }
  45. @Override
  46. public Object evaluate(DeferredObject[] arguments) throws HiveException {
  47. boolean bool_a0 = false, bool_a1 = false;
  48. Object a0 = arguments[0].get();
  49. if (a0 != null) {
  50. bool_a0 = boi0.get(a0);
  51. if (bool_a0 == false) {
  52. result.set(false);
  53. return result;
  54. }
  55. }
  56. Object a1 = arguments[1].get();
  57. if (a1 != null) {
  58. bool_a1 = boi1.get(a1);
  59. if (bool_a1 == false) {
  60. result.set(false);
  61. return result;
  62. }
  63. }
  64. if ((a0 != null && bool_a0 == true) && (a1 != null && bool_a1 == true)) {
  65. result.set(true);
  66. return result;
  67. }
  68. return null;
  69. }
  70. @Override
  71. public String getDisplayString(String[] children) {
  72. assert (children.length == 2);
  73. return "(" + children[0] + " and " + children[1] + ")";
  74. }
  75. }