Search in sources :

Example 31 with ConstantObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector in project hive by apache.

the class MaskTransformer method getIntArg.

int getIntArg(ObjectInspector[] arguments, int index, int defaultValue) {
    int ret = defaultValue;
    ObjectInspector arg = (arguments != null && arguments.length > index) ? arguments[index] : null;
    if (arg != null) {
        if (arg instanceof WritableConstantIntObjectInspector) {
            IntWritable value = ((WritableConstantIntObjectInspector) arg).getWritableConstantValue();
            if (value != null) {
                ret = value.get();
            }
        } else if (arg instanceof WritableConstantLongObjectInspector) {
            LongWritable value = ((WritableConstantLongObjectInspector) arg).getWritableConstantValue();
            if (value != null) {
                ret = (int) value.get();
            }
        } else if (arg instanceof WritableConstantShortObjectInspector) {
            ShortWritable value = ((WritableConstantShortObjectInspector) arg).getWritableConstantValue();
            if (value != null) {
                ret = value.get();
            }
        } else if (arg instanceof ConstantObjectInspector) {
            Object value = ((ConstantObjectInspector) arg).getWritableConstantValue();
            if (value != null) {
                String strValue = value.toString();
                if (strValue != null && strValue.length() > 0) {
                    ret = Integer.parseInt(value.toString());
                }
            }
        }
    }
    return ret;
}
Also used : ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) LongWritable(org.apache.hadoop.io.LongWritable) ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector) ShortWritable(org.apache.hadoop.hive.serde2.io.ShortWritable) IntWritable(org.apache.hadoop.io.IntWritable)

Example 32 with ConstantObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector in project hive by apache.

the class GenericUDFNamedStruct method initialize.

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    int numFields = arguments.length;
    if (numFields % 2 == 1) {
        throw new UDFArgumentLengthException("NAMED_STRUCT expects an even number of arguments.");
    }
    ret = new Object[numFields / 2];
    ArrayList<String> fname = new ArrayList<String>(numFields / 2);
    ArrayList<ObjectInspector> retOIs = new ArrayList<ObjectInspector>(numFields / 2);
    for (int f = 0; f < numFields; f += 2) {
        if (!(arguments[f] instanceof ConstantObjectInspector)) {
            throw new UDFArgumentTypeException(f, "Even arguments" + " to NAMED_STRUCT must be a constant STRING." + arguments[f].toString());
        }
        ConstantObjectInspector constantOI = (ConstantObjectInspector) arguments[f];
        fname.add(constantOI.getWritableConstantValue().toString());
        retOIs.add(arguments[f + 1]);
    }
    StructObjectInspector soi = ObjectInspectorFactory.getStandardStructObjectInspector(fname, retOIs);
    return soi;
}
Also used : ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) UDFArgumentLengthException(org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException) ArrayList(java.util.ArrayList) UDFArgumentTypeException(org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException) ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)

Example 33 with ConstantObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector in project hive by apache.

the class GenericUDFNextDay method initialize.

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    checkArgsSize(arguments, 2, 2);
    checkArgPrimitive(arguments, 0);
    checkArgPrimitive(arguments, 1);
    checkArgGroups(arguments, 0, inputTypes, STRING_GROUP, DATE_GROUP, VOID_GROUP);
    checkArgGroups(arguments, 1, inputTypes, STRING_GROUP, VOID_GROUP);
    obtainDateConverter(arguments, 0, inputTypes, converters);
    obtainStringConverter(arguments, 1, inputTypes, converters);
    if (arguments[1] instanceof ConstantObjectInspector) {
        String dayOfWeek = getConstantStringValue(arguments, 1);
        isDayOfWeekConst = true;
        dayOfWeekIntConst = getIntDayOfWeek(dayOfWeek);
    }
    ObjectInspector outputOI = PrimitiveObjectInspectorFactory.writableStringObjectInspector;
    return outputOI;
}
Also used : ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector)

Example 34 with ConstantObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector in project hive by apache.

the class GenericUDFInternalInterval method initialize.

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    // read operation mode
    if (!(arguments[0] instanceof ConstantObjectInspector)) {
        throw new UDFArgumentTypeException(0, getFuncName() + ": may only accept constant as first argument");
    }
    Integer operationMode = getConstantIntValue(arguments, 0);
    if (operationMode == null) {
        throw new UDFArgumentTypeException(0, "must supply operationmode");
    }
    processor = getProcessorMap().get(operationMode);
    if (processor == null) {
        throw new UDFArgumentTypeException(0, getFuncName() + ": unsupported operationMode: " + operationMode);
    }
    // check value argument
    if (arguments[1].getCategory() != Category.PRIMITIVE) {
        throw new UDFArgumentTypeException(1, "The first argument to " + getFuncName() + " must be primitive");
    }
    inputOI = (PrimitiveObjectInspector) arguments[1];
    PrimitiveCategory inputCategory = inputOI.getPrimitiveCategory();
    if (!isValidInputCategory(inputCategory)) {
        throw new UDFArgumentTypeException(1, "The second argument to " + getFuncName() + " must be from the string group or numeric group (except:float/double)");
    }
    if (arguments[1] instanceof ConstantObjectInspector) {
        // return value as constant in case arg is constant
        return PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector(processor.getTypeInfo(), processor.evaluate(getConstantStringValue(arguments, 1)));
    } else {
        return PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(processor.getTypeInfo());
    }
}
Also used : UDFArgumentTypeException(org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException) ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector) PrimitiveCategory(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory)

Example 35 with ConstantObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector in project hive by apache.

the class AccumuloRangeGenerator method getBinaryValue.

/**
 * Attempts to construct the binary value from the given inspector. Falls back to UTF8 encoding
 * when the value cannot be coerced into binary.
 *
 * @return Binary value when possible, utf8 otherwise
 * @throws IOException
 */
protected Text getBinaryValue(ConstantObjectInspector objInspector) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    if (objInspector instanceof PrimitiveObjectInspector) {
        LazyUtils.writePrimitive(out, objInspector.getWritableConstantValue(), (PrimitiveObjectInspector) objInspector);
    } else {
        return getUtf8Value(objInspector);
    }
    out.close();
    return new Text(out.toByteArray());
}
Also used : PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) Text(org.apache.hadoop.io.Text) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

ConstantObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector)36 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)25 PrimitiveObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector)19 UDFArgumentTypeException (org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException)14 UDFArgumentLengthException (org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException)12 ExprNodeConstantDesc (org.apache.hadoop.hive.ql.plan.ExprNodeConstantDesc)7 ArrayList (java.util.ArrayList)6 PrimitiveCategory (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory)6 HiveException (org.apache.hadoop.hive.ql.metadata.HiveException)5 UDFArgumentException (org.apache.hadoop.hive.ql.exec.UDFArgumentException)4 StructObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)4 IntWritable (org.apache.hadoop.io.IntWritable)4 Text (org.apache.hadoop.io.Text)4 ExprNodeDesc (org.apache.hadoop.hive.ql.plan.ExprNodeDesc)3 StringObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector)3 PrimitiveTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo)3 BytesWritable (org.apache.hadoop.io.BytesWritable)3 LongWritable (org.apache.hadoop.io.LongWritable)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 List (java.util.List)2