Search in sources :

Example 6 with DecimalTypeInfo

use of org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo in project hive by apache.

the class TestGenericUDFOPPositive method testDecimal.

@Test
public void testDecimal() throws HiveException {
    GenericUDFOPPositive udf = new GenericUDFOPPositive();
    HiveDecimalWritable input = new HiveDecimalWritable(HiveDecimal.create("32300.004747"));
    DecimalTypeInfo inputTypeInfo = TypeInfoFactory.getDecimalTypeInfo(11, 6);
    ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(inputTypeInfo) };
    DeferredObject[] args = { new DeferredJavaObject(input) };
    PrimitiveObjectInspector oi = (PrimitiveObjectInspector) udf.initialize(inputOIs);
    Assert.assertEquals(inputTypeInfo, oi.getTypeInfo());
    HiveDecimalWritable res = (HiveDecimalWritable) udf.evaluate(args);
    Assert.assertEquals(HiveDecimal.create("32300.004747"), res.getHiveDecimal());
}
Also used : DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) DeferredJavaObject(org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredJavaObject) HiveDecimalWritable(org.apache.hadoop.hive.serde2.io.HiveDecimalWritable) DeferredObject(org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) Test(org.junit.Test)

Example 7 with DecimalTypeInfo

use of org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo in project hive by apache.

the class TestGenericUDFRound method testDecimalRoundingMetaData1.

@Test
public void testDecimalRoundingMetaData1() throws UDFArgumentException {
    GenericUDFRound udf = new GenericUDFRound();
    ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.getDecimalTypeInfo(7, 3)), PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector(TypeInfoFactory.intTypeInfo, new IntWritable(-2)) };
    PrimitiveObjectInspector outputOI = (PrimitiveObjectInspector) udf.initialize(inputOIs);
    DecimalTypeInfo outputTypeInfo = (DecimalTypeInfo) outputOI.getTypeInfo();
    Assert.assertEquals(TypeInfoFactory.getDecimalTypeInfo(5, 0), outputTypeInfo);
}
Also used : DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) IntWritable(org.apache.hadoop.io.IntWritable) Test(org.junit.Test) BaseScalarUdfTest(org.apache.hadoop.hive.ql.testutil.BaseScalarUdfTest)

Example 8 with DecimalTypeInfo

use of org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo in project hive by apache.

the class TestGenericUDFOPNegative method testDecimal.

@Test
public void testDecimal() throws HiveException {
    GenericUDFOPNegative udf = new GenericUDFOPNegative();
    HiveDecimalWritable input = new HiveDecimalWritable(HiveDecimal.create("32300.004747"));
    DecimalTypeInfo inputTypeInfo = TypeInfoFactory.getDecimalTypeInfo(11, 6);
    ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(inputTypeInfo) };
    DeferredObject[] args = { new DeferredJavaObject(input) };
    PrimitiveObjectInspector oi = (PrimitiveObjectInspector) udf.initialize(inputOIs);
    Assert.assertEquals(inputTypeInfo, oi.getTypeInfo());
    HiveDecimalWritable res = (HiveDecimalWritable) udf.evaluate(args);
    Assert.assertEquals(HiveDecimal.create("-32300.004747"), res.getHiveDecimal());
}
Also used : DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) DeferredJavaObject(org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredJavaObject) HiveDecimalWritable(org.apache.hadoop.hive.serde2.io.HiveDecimalWritable) DeferredObject(org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) Test(org.junit.Test)

Example 9 with DecimalTypeInfo

use of org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo in project hive by apache.

the class LazyBinaryDeserializeRead method readNextField.

/*
   * Reads the the next field.
   *
   * Afterwards, reading is positioned to the next field.
   *
   * @return  Return true when the field was not null and data is put in the appropriate
   *          current* member.
   *          Otherwise, false when the field is null.
   *
   */
@Override
public boolean readNextField() throws IOException {
    if (fieldIndex >= fieldCount) {
        return false;
    }
    fieldStart = offset;
    if (fieldIndex == 0) {
        // the NULL byte.
        if (offset >= end) {
            throw new EOFException();
        }
        nullByte = bytes[offset++];
    }
    // NOTE: The bit is set to 1 if a field is NOT NULL.    boolean isNull;
    if ((nullByte & (1 << (fieldIndex % 8))) == 0) {
        // Logically move past this field.
        fieldIndex++;
        // Every 8 fields we read a new NULL byte.
        if (fieldIndex < fieldCount) {
            if ((fieldIndex % 8) == 0) {
                // Get next null byte.
                if (offset >= end) {
                    throw new EOFException();
                }
                nullByte = bytes[offset++];
            }
        }
        return false;
    } else {
        // Make sure there is at least one byte that can be read for a value.
        if (offset >= end) {
            throw new EOFException();
        }
        /*
       * We have a field and are positioned to it.  Read it.
       */
        switch(primitiveCategories[fieldIndex]) {
            case BOOLEAN:
                // No check needed for single byte read.
                currentBoolean = (bytes[offset++] != 0);
                break;
            case BYTE:
                // No check needed for single byte read.
                currentByte = bytes[offset++];
                break;
            case SHORT:
                // Last item -- ok to be at end.
                if (offset + 2 > end) {
                    throw new EOFException();
                }
                currentShort = LazyBinaryUtils.byteArrayToShort(bytes, offset);
                offset += 2;
                break;
            case INT:
                // Parse the first byte of a vint/vlong to determine the number of bytes.
                if (offset + WritableUtils.decodeVIntSize(bytes[offset]) > end) {
                    throw new EOFException();
                }
                LazyBinaryUtils.readVInt(bytes, offset, tempVInt);
                offset += tempVInt.length;
                currentInt = tempVInt.value;
                break;
            case LONG:
                // Parse the first byte of a vint/vlong to determine the number of bytes.
                if (offset + WritableUtils.decodeVIntSize(bytes[offset]) > end) {
                    throw new EOFException();
                }
                LazyBinaryUtils.readVLong(bytes, offset, tempVLong);
                offset += tempVLong.length;
                currentLong = tempVLong.value;
                break;
            case FLOAT:
                // Last item -- ok to be at end.
                if (offset + 4 > end) {
                    throw new EOFException();
                }
                currentFloat = Float.intBitsToFloat(LazyBinaryUtils.byteArrayToInt(bytes, offset));
                offset += 4;
                break;
            case DOUBLE:
                // Last item -- ok to be at end.
                if (offset + 8 > end) {
                    throw new EOFException();
                }
                currentDouble = Double.longBitsToDouble(LazyBinaryUtils.byteArrayToLong(bytes, offset));
                offset += 8;
                break;
            case BINARY:
            case STRING:
            case CHAR:
            case VARCHAR:
                {
                    // Parse the first byte of a vint/vlong to determine the number of bytes.
                    if (offset + WritableUtils.decodeVIntSize(bytes[offset]) > end) {
                        throw new EOFException();
                    }
                    LazyBinaryUtils.readVInt(bytes, offset, tempVInt);
                    offset += tempVInt.length;
                    int saveStart = offset;
                    int length = tempVInt.value;
                    offset += length;
                    // Last item -- ok to be at end.
                    if (offset > end) {
                        throw new EOFException();
                    }
                    currentBytes = bytes;
                    currentBytesStart = saveStart;
                    currentBytesLength = length;
                }
                break;
            case DATE:
                // Parse the first byte of a vint/vlong to determine the number of bytes.
                if (offset + WritableUtils.decodeVIntSize(bytes[offset]) > end) {
                    throw new EOFException();
                }
                LazyBinaryUtils.readVInt(bytes, offset, tempVInt);
                offset += tempVInt.length;
                currentDateWritable.set(tempVInt.value);
                break;
            case TIMESTAMP:
                {
                    int length = TimestampWritable.getTotalLength(bytes, offset);
                    int saveStart = offset;
                    offset += length;
                    // Last item -- ok to be at end.
                    if (offset > end) {
                        throw new EOFException();
                    }
                    currentTimestampWritable.set(bytes, saveStart);
                }
                break;
            case INTERVAL_YEAR_MONTH:
                // Parse the first byte of a vint/vlong to determine the number of bytes.
                if (offset + WritableUtils.decodeVIntSize(bytes[offset]) > end) {
                    throw new EOFException();
                }
                LazyBinaryUtils.readVInt(bytes, offset, tempVInt);
                offset += tempVInt.length;
                currentHiveIntervalYearMonthWritable.set(tempVInt.value);
                break;
            case INTERVAL_DAY_TIME:
                // Parse the first byte of a vint/vlong to determine the number of bytes.
                if (offset + WritableUtils.decodeVIntSize(bytes[offset]) >= end) {
                    throw new EOFException();
                }
                LazyBinaryUtils.readVLong(bytes, offset, tempVLong);
                offset += tempVLong.length;
                // Parse the first byte of a vint/vlong to determine the number of bytes.
                if (offset + WritableUtils.decodeVIntSize(bytes[offset]) > end) {
                    throw new EOFException();
                }
                LazyBinaryUtils.readVInt(bytes, offset, tempVInt);
                offset += tempVInt.length;
                currentHiveIntervalDayTimeWritable.set(tempVLong.value, tempVInt.value);
                break;
            case DECIMAL:
                {
                    // Parse the first byte of a vint/vlong to determine the number of bytes.
                    if (offset + WritableUtils.decodeVIntSize(bytes[offset]) >= end) {
                        throw new EOFException();
                    }
                    LazyBinaryUtils.readVInt(bytes, offset, tempVInt);
                    offset += tempVInt.length;
                    int readScale = tempVInt.value;
                    // Parse the first byte of a vint/vlong to determine the number of bytes.
                    if (offset + WritableUtils.decodeVIntSize(bytes[offset]) > end) {
                        throw new EOFException();
                    }
                    LazyBinaryUtils.readVInt(bytes, offset, tempVInt);
                    offset += tempVInt.length;
                    int saveStart = offset;
                    offset += tempVInt.value;
                    // Last item -- ok to be at end.
                    if (offset > end) {
                        throw new EOFException();
                    }
                    int length = offset - saveStart;
                    //   scale = 2, length = 6, value = -6065716379.11
                    //   \002\006\255\114\197\131\083\105
                    //           \255\114\197\131\083\105
                    currentHiveDecimalWritable.setFromBigIntegerBytesAndScale(bytes, saveStart, length, readScale);
                    boolean decimalIsNull = !currentHiveDecimalWritable.isSet();
                    if (!decimalIsNull) {
                        DecimalTypeInfo decimalTypeInfo = (DecimalTypeInfo) typeInfos[fieldIndex];
                        int precision = decimalTypeInfo.getPrecision();
                        int scale = decimalTypeInfo.getScale();
                        decimalIsNull = !currentHiveDecimalWritable.mutateEnforcePrecisionScale(precision, scale);
                    }
                    if (decimalIsNull) {
                        // Logically move past this field.
                        fieldIndex++;
                        // Every 8 fields we read a new NULL byte.
                        if (fieldIndex < fieldCount) {
                            if ((fieldIndex % 8) == 0) {
                                // Get next null byte.
                                if (offset >= end) {
                                    throw new EOFException();
                                }
                                nullByte = bytes[offset++];
                            }
                        }
                        return false;
                    }
                }
                break;
            default:
                throw new Error("Unexpected primitive category " + primitiveCategories[fieldIndex].name());
        }
    }
    // Logically move past this field.
    fieldIndex++;
    // Every 8 fields we read a new NULL byte.
    if (fieldIndex < fieldCount) {
        if ((fieldIndex % 8) == 0) {
            // Get next null byte.
            if (offset >= end) {
                throw new EOFException();
            }
            nullByte = bytes[offset++];
        }
    }
    return true;
}
Also used : DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) EOFException(java.io.EOFException)

Example 10 with DecimalTypeInfo

use of org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo in project hive by apache.

the class WritableConstantHiveDecimalObjectInspector method getWritableConstantValue.

@Override
public HiveDecimalWritable getWritableConstantValue() {
    // We need to enforce precision/scale here.
    DecimalTypeInfo decTypeInfo = (DecimalTypeInfo) typeInfo;
    HiveDecimalWritable result = new HiveDecimalWritable(value);
    result.mutateEnforcePrecisionScale(decTypeInfo.precision(), decTypeInfo.scale());
    if (!result.isSet()) {
        return null;
    }
    return result;
}
Also used : DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) HiveDecimalWritable(org.apache.hadoop.hive.serde2.io.HiveDecimalWritable)

Aggregations

DecimalTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo)40 HiveDecimalWritable (org.apache.hadoop.hive.serde2.io.HiveDecimalWritable)14 PrimitiveTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo)14 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)13 CharTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.CharTypeInfo)12 VarcharTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.VarcharTypeInfo)12 HiveVarchar (org.apache.hadoop.hive.common.type.HiveVarchar)11 HiveChar (org.apache.hadoop.hive.common.type.HiveChar)10 IntWritable (org.apache.hadoop.io.IntWritable)10 Test (org.junit.Test)10 HiveDecimal (org.apache.hadoop.hive.common.type.HiveDecimal)9 PrimitiveObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector)9 PrimitiveCategory (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory)9 Timestamp (java.sql.Timestamp)8 Date (java.sql.Date)7 DoubleWritable (org.apache.hadoop.hive.serde2.io.DoubleWritable)7 ShortWritable (org.apache.hadoop.hive.serde2.io.ShortWritable)7 BooleanWritable (org.apache.hadoop.io.BooleanWritable)7 BytesWritable (org.apache.hadoop.io.BytesWritable)7 FloatWritable (org.apache.hadoop.io.FloatWritable)7