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