PageRenderTime 25ms CodeModel.GetById 13ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 155 lines | 115 code | 17 blank | 23 comment | 22 complexity | 895e4f3b670bb2a79d0659fb3021b8f3 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.UDFArgumentTypeException;
 24import org.apache.hadoop.hive.ql.udf.generic.GenericUDFUtils.ReturnObjectInspectorResolver;
 25import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
 26import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorConverters;
 27import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector.Category;
 28import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorConverters.Converter;
 29import org.apache.hadoop.hive.serde2.objectinspector.primitive.BooleanObjectInspector;
 30import org.apache.hadoop.hive.serde2.objectinspector.primitive.ByteObjectInspector;
 31import org.apache.hadoop.hive.serde2.objectinspector.primitive.IntObjectInspector;
 32import org.apache.hadoop.hive.serde2.objectinspector.primitive.LongObjectInspector;
 33import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
 34import org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector;
 35import org.apache.hadoop.hive.serde2.typeinfo.TypeInfo;
 36import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory;
 37import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoUtils;
 38import org.apache.hadoop.io.BooleanWritable;
 39
 40/**
 41 * GenericUDF Base Class for operations.
 42 */
 43@Description(name = "op", value = "a op b - Returns the result of operation")
 44public abstract class GenericUDFBaseCompare extends GenericUDF {
 45  public enum CompareType {
 46    // Now only string, text, int, long, byte and boolean comparisons are
 47    // treated as special cases.
 48    // For other types, we reuse ObjectInspectorUtils.compare()
 49    COMPARE_STRING, COMPARE_TEXT, COMPARE_INT, COMPARE_LONG, COMPARE_BYTE,
 50    COMPARE_BOOL, SAME_TYPE, NEED_CONVERT
 51  }
 52
 53  protected String opName;
 54  protected String opDisplayName;
 55
 56  protected ObjectInspector[] argumentOIs;
 57
 58  protected ReturnObjectInspectorResolver conversionHelper = null;
 59  protected ObjectInspector compareOI;
 60  protected CompareType compareType;
 61  protected Converter converter0, converter1;
 62  protected StringObjectInspector soi0, soi1;
 63  protected IntObjectInspector ioi0, ioi1;
 64  protected LongObjectInspector loi0, loi1;
 65  protected ByteObjectInspector byoi0, byoi1;
 66  protected BooleanObjectInspector boi0,boi1;
 67  protected final BooleanWritable result = new BooleanWritable();
 68
 69  @Override
 70  public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
 71
 72    if (arguments.length != 2) {
 73      throw new UDFArgumentException(
 74          opName + " requires two arguments.");
 75    }
 76
 77    argumentOIs = arguments;
 78
 79    for (int i = 0; i < arguments.length; i++) {
 80      Category category = arguments[i].getCategory();
 81      if (category != Category.PRIMITIVE) {
 82        throw new UDFArgumentTypeException(i, "The "
 83            + GenericUDFUtils.getOrdinal(i + 1)
 84            + " argument of " + opName + "  is expected to a "
 85            + Category.PRIMITIVE.toString().toLowerCase() + " type, but "
 86            + category.toString().toLowerCase() + " is found");
 87      }
 88    }
 89
 90    if (TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[0]).equals(
 91      TypeInfoFactory.stringTypeInfo) &&
 92      TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[1]).equals(
 93      TypeInfoFactory.stringTypeInfo)) {
 94      soi0 = (StringObjectInspector) arguments[0];
 95      soi1 = (StringObjectInspector) arguments[1];
 96      if (soi0.preferWritable() || soi1.preferWritable()) {
 97        compareType = CompareType.COMPARE_TEXT;
 98      } else {
 99        compareType = CompareType.COMPARE_STRING;
100      }
101    } else if (TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[0]).equals(
102      TypeInfoFactory.intTypeInfo) &&
103      TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[1]).equals(
104        TypeInfoFactory.intTypeInfo)) {
105      compareType = CompareType.COMPARE_INT;
106      ioi0 = (IntObjectInspector) arguments[0];
107      ioi1 = (IntObjectInspector) arguments[1];
108    } else if (TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[0]).equals(
109        TypeInfoFactory.longTypeInfo) &&
110        TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[1]).equals(
111          TypeInfoFactory.longTypeInfo)) {
112        compareType = CompareType.COMPARE_LONG;
113        loi0 = (LongObjectInspector) arguments[0];
114        loi1 = (LongObjectInspector) arguments[1];
115    } else if (TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[0]).equals(
116        TypeInfoFactory.byteTypeInfo) &&
117        TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[1]).equals(
118          TypeInfoFactory.byteTypeInfo)) {
119        compareType = CompareType.COMPARE_BYTE;
120        byoi0 = (ByteObjectInspector) arguments[0];
121        byoi1 = (ByteObjectInspector) arguments[1];
122    } else if (TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[0]).equals(
123        TypeInfoFactory.booleanTypeInfo) &&
124        TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[1]).equals(
125          TypeInfoFactory.booleanTypeInfo)) {
126      compareType = CompareType.COMPARE_BOOL;
127      boi0 = (BooleanObjectInspector) arguments[0];
128      boi1 = (BooleanObjectInspector) arguments[1];
129     } else {
130      TypeInfo oiTypeInfo0 = TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[0]);
131      TypeInfo oiTypeInfo1 = TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[1]);
132
133      if (oiTypeInfo0 != oiTypeInfo1) {
134        compareType = CompareType.NEED_CONVERT;
135        compareOI = TypeInfoUtils.getStandardWritableObjectInspectorFromTypeInfo(
136            TypeInfoFactory.doubleTypeInfo);
137        converter0 = ObjectInspectorConverters.getConverter(arguments[0], compareOI);
138        converter1 = ObjectInspectorConverters.getConverter(arguments[1], compareOI);
139      } else {
140        compareType = CompareType.SAME_TYPE;
141      }
142    }
143    return PrimitiveObjectInspectorFactory.writableBooleanObjectInspector;
144
145  }
146
147
148  @Override
149  public String getDisplayString(String[] children) {
150    assert (children.length == 2);
151    return "(" + children[0] + " " + opDisplayName + " " + children[1] + ")";
152
153  }
154
155}