Search in sources :

Example 56 with DecimalTypeInfo

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

the class BinarySortableDeserializeRead method readPrimitive.

private boolean readPrimitive(Field field) throws IOException {
    final int fieldIndex = root.index;
    field.start = inputByteBuffer.tell();
    /*
     * We have a field and are positioned to it.  Read it.
     */
    switch(field.primitiveCategory) {
        case BOOLEAN:
            currentBoolean = (inputByteBuffer.read(columnSortOrderIsDesc[fieldIndex]) == 2);
            return true;
        case BYTE:
            currentByte = (byte) (inputByteBuffer.read(columnSortOrderIsDesc[fieldIndex]) ^ 0x80);
            return true;
        case SHORT:
            {
                final boolean invert = columnSortOrderIsDesc[fieldIndex];
                int v = inputByteBuffer.read(invert) ^ 0x80;
                v = (v << 8) + (inputByteBuffer.read(invert) & 0xff);
                currentShort = (short) v;
            }
            return true;
        case INT:
            {
                final boolean invert = columnSortOrderIsDesc[fieldIndex];
                int v = inputByteBuffer.read(invert) ^ 0x80;
                for (int i = 0; i < 3; i++) {
                    v = (v << 8) + (inputByteBuffer.read(invert) & 0xff);
                }
                currentInt = v;
            }
            return true;
        case LONG:
            {
                final boolean invert = columnSortOrderIsDesc[fieldIndex];
                long v = inputByteBuffer.read(invert) ^ 0x80;
                for (int i = 0; i < 7; i++) {
                    v = (v << 8) + (inputByteBuffer.read(invert) & 0xff);
                }
                currentLong = v;
            }
            return true;
        case DATE:
            {
                final boolean invert = columnSortOrderIsDesc[fieldIndex];
                int v = inputByteBuffer.read(invert) ^ 0x80;
                for (int i = 0; i < 3; i++) {
                    v = (v << 8) + (inputByteBuffer.read(invert) & 0xff);
                }
                currentDateWritable.set(v);
            }
            return true;
        case TIMESTAMP:
            {
                if (tempTimestampBytes == null) {
                    tempTimestampBytes = new byte[TimestampWritable.BINARY_SORTABLE_LENGTH];
                }
                final boolean invert = columnSortOrderIsDesc[fieldIndex];
                for (int i = 0; i < tempTimestampBytes.length; i++) {
                    tempTimestampBytes[i] = inputByteBuffer.read(invert);
                }
                currentTimestampWritable.setBinarySortable(tempTimestampBytes, 0);
            }
            return true;
        case FLOAT:
            {
                final boolean invert = columnSortOrderIsDesc[fieldIndex];
                int v = 0;
                for (int i = 0; i < 4; i++) {
                    v = (v << 8) + (inputByteBuffer.read(invert) & 0xff);
                }
                if ((v & (1 << 31)) == 0) {
                    // negative number, flip all bits
                    v = ~v;
                } else {
                    // positive number, flip the first bit
                    v = v ^ (1 << 31);
                }
                currentFloat = Float.intBitsToFloat(v);
            }
            return true;
        case DOUBLE:
            {
                final boolean invert = columnSortOrderIsDesc[fieldIndex];
                long v = 0;
                for (int i = 0; i < 8; i++) {
                    v = (v << 8) + (inputByteBuffer.read(invert) & 0xff);
                }
                if ((v & (1L << 63)) == 0) {
                    // negative number, flip all bits
                    v = ~v;
                } else {
                    // positive number, flip the first bit
                    v = v ^ (1L << 63);
                }
                currentDouble = Double.longBitsToDouble(v);
            }
            return true;
        case BINARY:
        case STRING:
        case CHAR:
        case VARCHAR:
            {
                /*
         * This code is a modified version of BinarySortableSerDe.deserializeText that lets us
         * detect if we can return a reference to the bytes directly.
         */
                // Get the actual length first
                bytesStart = inputByteBuffer.tell();
                final boolean invert = columnSortOrderIsDesc[fieldIndex];
                int length = 0;
                do {
                    byte b = inputByteBuffer.read(invert);
                    if (b == 0) {
                        // end of string
                        break;
                    }
                    if (b == 1) {
                        // the last char is an escape char. read the actual char
                        inputByteBuffer.read(invert);
                    }
                    length++;
                } while (true);
                if (length == 0 || (!invert && length == inputByteBuffer.tell() - bytesStart - 1)) {
                    // No inversion or escaping happened, so we are can reference directly.
                    currentExternalBufferNeeded = false;
                    currentBytes = inputByteBuffer.getData();
                    currentBytesStart = bytesStart;
                    currentBytesLength = length;
                } else {
                    // We are now positioned at the end of this field's bytes.
                    if (useExternalBuffer) {
                        // If we decided not to reposition and re-read the buffer to copy it with
                        // copyToExternalBuffer, we we will still be correctly positioned for the next field.
                        currentExternalBufferNeeded = true;
                        currentExternalBufferNeededLen = length;
                    } else {
                        // The copyToBuffer will reposition and re-read the input buffer.
                        currentExternalBufferNeeded = false;
                        if (internalBufferLen < length) {
                            internalBufferLen = length;
                            internalBuffer = new byte[internalBufferLen];
                        }
                        copyToBuffer(internalBuffer, 0, length);
                        currentBytes = internalBuffer;
                        currentBytesStart = 0;
                        currentBytesLength = length;
                    }
                }
            }
            return true;
        case INTERVAL_YEAR_MONTH:
            {
                final boolean invert = columnSortOrderIsDesc[fieldIndex];
                int v = inputByteBuffer.read(invert) ^ 0x80;
                for (int i = 0; i < 3; i++) {
                    v = (v << 8) + (inputByteBuffer.read(invert) & 0xff);
                }
                currentHiveIntervalYearMonthWritable.set(v);
            }
            return true;
        case INTERVAL_DAY_TIME:
            {
                final boolean invert = columnSortOrderIsDesc[fieldIndex];
                long totalSecs = inputByteBuffer.read(invert) ^ 0x80;
                for (int i = 0; i < 7; i++) {
                    totalSecs = (totalSecs << 8) + (inputByteBuffer.read(invert) & 0xff);
                }
                int nanos = inputByteBuffer.read(invert) ^ 0x80;
                for (int i = 0; i < 3; i++) {
                    nanos = (nanos << 8) + (inputByteBuffer.read(invert) & 0xff);
                }
                currentHiveIntervalDayTimeWritable.set(totalSecs, nanos);
            }
            return true;
        case DECIMAL:
            {
                // Since enforcing precision and scale can cause a HiveDecimal to become NULL,
                // we must read it, enforce it here, and either return NULL or buffer the result.
                final boolean invert = columnSortOrderIsDesc[fieldIndex];
                int b = inputByteBuffer.read(invert) - 1;
                if (!(b == 1 || b == -1 || b == 0)) {
                    throw new IOException("Unexpected byte value " + (int) b + " in binary sortable format data (invert " + invert + ")");
                }
                final boolean positive = b != -1;
                int factor = inputByteBuffer.read(invert) ^ 0x80;
                for (int i = 0; i < 3; i++) {
                    factor = (factor << 8) + (inputByteBuffer.read(invert) & 0xff);
                }
                if (!positive) {
                    factor = -factor;
                }
                final int decimalStart = inputByteBuffer.tell();
                int length = 0;
                do {
                    b = inputByteBuffer.read(positive ? invert : !invert);
                    if (b == 1) {
                        throw new IOException("Expected -1 and found byte value " + (int) b + " in binary sortable format data (invert " + invert + ")");
                    }
                    if (b == 0) {
                        // end of digits
                        break;
                    }
                    length++;
                } while (true);
                // CONSIDER: Allocate a larger initial size.
                if (tempDecimalBuffer == null || tempDecimalBuffer.length < length) {
                    tempDecimalBuffer = new byte[length];
                }
                inputByteBuffer.seek(decimalStart);
                for (int i = 0; i < length; ++i) {
                    tempDecimalBuffer[i] = inputByteBuffer.read(positive ? invert : !invert);
                }
                // read the null byte again
                inputByteBuffer.read(positive ? invert : !invert);
                // Set the value of the writable from the decimal digits that were written with no dot.
                final int scale = length - factor;
                currentHiveDecimalWritable.setFromDigitsOnlyBytesWithScale(!positive, tempDecimalBuffer, 0, length, scale);
                boolean decimalIsNull = !currentHiveDecimalWritable.isSet();
                if (!decimalIsNull) {
                    // We have a decimal.  After we enforce precision and scale, will it become a NULL?
                    final DecimalTypeInfo decimalTypeInfo = (DecimalTypeInfo) field.typeInfo;
                    final int enforcePrecision = decimalTypeInfo.getPrecision();
                    final int enforceScale = decimalTypeInfo.getScale();
                    decimalIsNull = !currentHiveDecimalWritable.mutateEnforcePrecisionScale(enforcePrecision, enforceScale);
                }
                if (decimalIsNull) {
                    return false;
                }
            }
            return true;
        default:
            throw new RuntimeException("Unexpected primitive type category " + field.primitiveCategory);
    }
}
Also used : DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) IOException(java.io.IOException)

Example 57 with DecimalTypeInfo

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

the class TypeInfoFactory method createPrimitiveTypeInfo.

/**
 * Create PrimitiveTypeInfo instance for the given full name of the type. The returned
 * type is one of the parameterized type info such as VarcharTypeInfo.
 *
 * @param fullName Fully qualified name of the type
 * @return PrimitiveTypeInfo instance
 */
private static PrimitiveTypeInfo createPrimitiveTypeInfo(String fullName) {
    String baseName = TypeInfoUtils.getBaseName(fullName);
    PrimitiveTypeEntry typeEntry = PrimitiveObjectInspectorUtils.getTypeEntryFromTypeName(baseName);
    if (null == typeEntry) {
        throw new RuntimeException("Unknown type " + fullName);
    }
    TypeInfoUtils.PrimitiveParts parts = TypeInfoUtils.parsePrimitiveParts(fullName);
    if (parts.typeParams == null || parts.typeParams.length < 1) {
        return null;
    }
    switch(typeEntry.primitiveCategory) {
        case CHAR:
            if (parts.typeParams.length != 1) {
                return null;
            }
            return new CharTypeInfo(Integer.valueOf(parts.typeParams[0]));
        case VARCHAR:
            if (parts.typeParams.length != 1) {
                return null;
            }
            return new VarcharTypeInfo(Integer.valueOf(parts.typeParams[0]));
        case DECIMAL:
            if (parts.typeParams.length != 2) {
                return null;
            }
            return new DecimalTypeInfo(Integer.valueOf(parts.typeParams[0]), Integer.valueOf(parts.typeParams[1]));
        case TIMESTAMPLOCALTZ:
            if (parts.typeParams.length != 1) {
                return null;
            }
            return new TimestampLocalTZTypeInfo(parts.typeParams[0]);
        default:
            return null;
    }
}
Also used : PrimitiveTypeEntry(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils.PrimitiveTypeEntry)

Example 58 with DecimalTypeInfo

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

the class VerifyFast method serializeWrite.

public static void serializeWrite(SerializeWrite serializeWrite, TypeInfo typeInfo, Object object) throws IOException {
    if (object == null) {
        serializeWrite.writeNull();
        return;
    }
    switch(typeInfo.getCategory()) {
        case PRIMITIVE:
            {
                PrimitiveTypeInfo primitiveTypeInfo = (PrimitiveTypeInfo) typeInfo;
                switch(primitiveTypeInfo.getPrimitiveCategory()) {
                    case BOOLEAN:
                        {
                            boolean value = ((BooleanWritable) object).get();
                            serializeWrite.writeBoolean(value);
                        }
                        break;
                    case BYTE:
                        {
                            byte value = ((ByteWritable) object).get();
                            serializeWrite.writeByte(value);
                        }
                        break;
                    case SHORT:
                        {
                            short value = ((ShortWritable) object).get();
                            serializeWrite.writeShort(value);
                        }
                        break;
                    case INT:
                        {
                            int value = ((IntWritable) object).get();
                            serializeWrite.writeInt(value);
                        }
                        break;
                    case LONG:
                        {
                            long value = ((LongWritable) object).get();
                            serializeWrite.writeLong(value);
                        }
                        break;
                    case FLOAT:
                        {
                            float value = ((FloatWritable) object).get();
                            serializeWrite.writeFloat(value);
                        }
                        break;
                    case DOUBLE:
                        {
                            double value = ((DoubleWritable) object).get();
                            serializeWrite.writeDouble(value);
                        }
                        break;
                    case STRING:
                        {
                            Text value = (Text) object;
                            byte[] stringBytes = value.getBytes();
                            int stringLength = stringBytes.length;
                            serializeWrite.writeString(stringBytes, 0, stringLength);
                        }
                        break;
                    case CHAR:
                        {
                            HiveChar value = ((HiveCharWritable) object).getHiveChar();
                            serializeWrite.writeHiveChar(value);
                        }
                        break;
                    case VARCHAR:
                        {
                            HiveVarchar value = ((HiveVarcharWritable) object).getHiveVarchar();
                            serializeWrite.writeHiveVarchar(value);
                        }
                        break;
                    case DECIMAL:
                        {
                            HiveDecimal value = ((HiveDecimalWritable) object).getHiveDecimal();
                            DecimalTypeInfo decTypeInfo = (DecimalTypeInfo) primitiveTypeInfo;
                            serializeWrite.writeHiveDecimal(value, decTypeInfo.scale());
                        }
                        break;
                    case DATE:
                        {
                            Date value = ((DateWritable) object).get();
                            serializeWrite.writeDate(value);
                        }
                        break;
                    case TIMESTAMP:
                        {
                            Timestamp value = ((TimestampWritable) object).getTimestamp();
                            serializeWrite.writeTimestamp(value);
                        }
                        break;
                    case INTERVAL_YEAR_MONTH:
                        {
                            HiveIntervalYearMonth value = ((HiveIntervalYearMonthWritable) object).getHiveIntervalYearMonth();
                            serializeWrite.writeHiveIntervalYearMonth(value);
                        }
                        break;
                    case INTERVAL_DAY_TIME:
                        {
                            HiveIntervalDayTime value = ((HiveIntervalDayTimeWritable) object).getHiveIntervalDayTime();
                            serializeWrite.writeHiveIntervalDayTime(value);
                        }
                        break;
                    case BINARY:
                        {
                            BytesWritable byteWritable = (BytesWritable) object;
                            byte[] binaryBytes = byteWritable.getBytes();
                            int length = byteWritable.getLength();
                            serializeWrite.writeBinary(binaryBytes, 0, length);
                        }
                        break;
                    default:
                        throw new Error("Unknown primitive category " + primitiveTypeInfo.getPrimitiveCategory().name());
                }
            }
            break;
        case LIST:
            {
                ListTypeInfo listTypeInfo = (ListTypeInfo) typeInfo;
                TypeInfo elementTypeInfo = listTypeInfo.getListElementTypeInfo();
                ArrayList<Object> elements = (ArrayList<Object>) object;
                serializeWrite.beginList(elements);
                boolean isFirst = true;
                for (Object elementObject : elements) {
                    if (isFirst) {
                        isFirst = false;
                    } else {
                        serializeWrite.separateList();
                    }
                    if (elementObject == null) {
                        serializeWrite.writeNull();
                    } else {
                        serializeWrite(serializeWrite, elementTypeInfo, elementObject);
                    }
                }
                serializeWrite.finishList();
            }
            break;
        case MAP:
            {
                MapTypeInfo mapTypeInfo = (MapTypeInfo) typeInfo;
                TypeInfo keyTypeInfo = mapTypeInfo.getMapKeyTypeInfo();
                TypeInfo valueTypeInfo = mapTypeInfo.getMapValueTypeInfo();
                HashMap<Object, Object> hashMap = (HashMap<Object, Object>) object;
                serializeWrite.beginMap(hashMap);
                boolean isFirst = true;
                for (Entry<Object, Object> entry : hashMap.entrySet()) {
                    if (isFirst) {
                        isFirst = false;
                    } else {
                        serializeWrite.separateKeyValuePair();
                    }
                    if (entry.getKey() == null) {
                        serializeWrite.writeNull();
                    } else {
                        serializeWrite(serializeWrite, keyTypeInfo, entry.getKey());
                    }
                    serializeWrite.separateKey();
                    if (entry.getValue() == null) {
                        serializeWrite.writeNull();
                    } else {
                        serializeWrite(serializeWrite, valueTypeInfo, entry.getValue());
                    }
                }
                serializeWrite.finishMap();
            }
            break;
        case STRUCT:
            {
                StructTypeInfo structTypeInfo = (StructTypeInfo) typeInfo;
                ArrayList<TypeInfo> fieldTypeInfos = structTypeInfo.getAllStructFieldTypeInfos();
                ArrayList<Object> fieldValues = (ArrayList<Object>) object;
                final int size = fieldValues.size();
                serializeWrite.beginStruct(fieldValues);
                boolean isFirst = true;
                for (int i = 0; i < size; i++) {
                    if (isFirst) {
                        isFirst = false;
                    } else {
                        serializeWrite.separateStruct();
                    }
                    serializeWrite(serializeWrite, fieldTypeInfos.get(i), fieldValues.get(i));
                }
                serializeWrite.finishStruct();
            }
            break;
        case UNION:
            {
                UnionTypeInfo unionTypeInfo = (UnionTypeInfo) typeInfo;
                List<TypeInfo> fieldTypeInfos = unionTypeInfo.getAllUnionObjectTypeInfos();
                final int size = fieldTypeInfos.size();
                StandardUnion standardUnion = (StandardUnion) object;
                byte tag = standardUnion.getTag();
                serializeWrite.beginUnion(tag);
                serializeWrite(serializeWrite, fieldTypeInfos.get(tag), standardUnion.getObject());
                serializeWrite.finishUnion();
            }
            break;
        default:
            throw new Error("Unknown category " + typeInfo.getCategory().name());
    }
}
Also used : HashMap(java.util.HashMap) HiveChar(org.apache.hadoop.hive.common.type.HiveChar) ArrayList(java.util.ArrayList) TimestampWritable(org.apache.hadoop.hive.serde2.io.TimestampWritable) DoubleWritable(org.apache.hadoop.hive.serde2.io.DoubleWritable) StructTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.StructTypeInfo) ShortWritable(org.apache.hadoop.hive.serde2.io.ShortWritable) Timestamp(java.sql.Timestamp) PrimitiveTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo) Entry(java.util.Map.Entry) HiveDecimal(org.apache.hadoop.hive.common.type.HiveDecimal) ArrayList(java.util.ArrayList) List(java.util.List) LongWritable(org.apache.hadoop.io.LongWritable) ByteWritable(org.apache.hadoop.hive.serde2.io.ByteWritable) IntWritable(org.apache.hadoop.io.IntWritable) HiveIntervalDayTime(org.apache.hadoop.hive.common.type.HiveIntervalDayTime) HiveDecimalWritable(org.apache.hadoop.hive.serde2.io.HiveDecimalWritable) DateWritable(org.apache.hadoop.hive.serde2.io.DateWritable) HiveCharWritable(org.apache.hadoop.hive.serde2.io.HiveCharWritable) HiveVarcharWritable(org.apache.hadoop.hive.serde2.io.HiveVarcharWritable) Text(org.apache.hadoop.io.Text) HiveIntervalDayTimeWritable(org.apache.hadoop.hive.serde2.io.HiveIntervalDayTimeWritable) BytesWritable(org.apache.hadoop.io.BytesWritable) HiveVarchar(org.apache.hadoop.hive.common.type.HiveVarchar) HiveIntervalYearMonthWritable(org.apache.hadoop.hive.serde2.io.HiveIntervalYearMonthWritable) MapTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.MapTypeInfo) ListTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.ListTypeInfo) StructTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.StructTypeInfo) PrimitiveTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo) DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) TypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfo) UnionTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.UnionTypeInfo) VarcharTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.VarcharTypeInfo) CharTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.CharTypeInfo) Date(java.sql.Date) DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) FloatWritable(org.apache.hadoop.io.FloatWritable) HiveIntervalYearMonth(org.apache.hadoop.hive.common.type.HiveIntervalYearMonth) BooleanWritable(org.apache.hadoop.io.BooleanWritable) ListTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.ListTypeInfo) MapTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.MapTypeInfo) StandardUnion(org.apache.hadoop.hive.serde2.objectinspector.StandardUnionObjectInspector.StandardUnion) UnionTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.UnionTypeInfo)

Example 59 with DecimalTypeInfo

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

the class LazyBinaryDeserializeRead method readPrimitive.

private boolean readPrimitive(Field field) throws IOException {
    final PrimitiveCategory primitiveCategory = field.primitiveCategory;
    final TypeInfo typeInfo = field.typeInfo;
    switch(primitiveCategory) {
        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) {
                    final DecimalTypeInfo decimalTypeInfo = (DecimalTypeInfo) typeInfo;
                    final int precision = decimalTypeInfo.getPrecision();
                    final int scale = decimalTypeInfo.getScale();
                    decimalIsNull = !currentHiveDecimalWritable.mutateEnforcePrecisionScale(precision, scale);
                }
                if (decimalIsNull) {
                    return false;
                }
            }
            break;
        default:
            throw new Error("Unexpected primitive category " + primitiveCategory.name());
    }
    return true;
}
Also used : DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) EOFException(java.io.EOFException) PrimitiveCategory(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory) DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) TypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfo) MapTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.MapTypeInfo) UnionTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.UnionTypeInfo) ListTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.ListTypeInfo) StructTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.StructTypeInfo) PrimitiveTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo)

Example 60 with DecimalTypeInfo

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

the class VerifyLazy method lazyCompare.

public static boolean lazyCompare(TypeInfo typeInfo, Object lazyObject, Object expectedObject) {
    if (expectedObject == null) {
        if (lazyObject != null) {
            throw new RuntimeException("Expected object is null but object is not null " + lazyObject.toString() + " typeInfo " + typeInfo.toString());
        }
        return true;
    } else if (lazyObject == null) {
        throw new RuntimeException("Expected object is not null \"" + expectedObject.toString() + "\" typeInfo " + typeInfo.toString() + " but object is null");
    }
    if (lazyObject instanceof Writable) {
        if (!lazyObject.equals(expectedObject)) {
            throw new RuntimeException("Expected object " + expectedObject.toString() + " and actual object " + lazyObject.toString() + " is not equal typeInfo " + typeInfo.toString());
        }
        return true;
    }
    if (lazyObject instanceof LazyPrimitive) {
        Object primitiveObject = ((LazyPrimitive) lazyObject).getObject();
        PrimitiveTypeInfo primitiveTypeInfo = (PrimitiveTypeInfo) typeInfo;
        switch(primitiveTypeInfo.getPrimitiveCategory()) {
            case BOOLEAN:
                {
                    if (!(primitiveObject instanceof LazyBoolean)) {
                        throw new RuntimeException("Expected LazyBoolean");
                    }
                    boolean value = ((LazyBoolean) primitiveObject).getWritableObject().get();
                    boolean expected = ((BooleanWritable) expectedObject).get();
                    if (value != expected) {
                        throw new RuntimeException("Boolean field mismatch (expected " + expected + " found " + value + ")");
                    }
                }
                break;
            case BYTE:
                {
                    if (!(primitiveObject instanceof LazyByte)) {
                        throw new RuntimeException("Expected LazyByte");
                    }
                    byte value = ((LazyByte) primitiveObject).getWritableObject().get();
                    byte expected = ((ByteWritable) expectedObject).get();
                    if (value != expected) {
                        throw new RuntimeException("Byte field mismatch (expected " + (int) expected + " found " + (int) value + ")");
                    }
                }
                break;
            case SHORT:
                {
                    if (!(primitiveObject instanceof LazyShort)) {
                        throw new RuntimeException("Expected LazyShort");
                    }
                    short value = ((LazyShort) primitiveObject).getWritableObject().get();
                    short expected = ((ShortWritable) expectedObject).get();
                    if (value != expected) {
                        throw new RuntimeException("Short field mismatch (expected " + expected + " found " + value + ")");
                    }
                }
                break;
            case INT:
                {
                    if (!(primitiveObject instanceof LazyInteger)) {
                        throw new RuntimeException("Expected LazyInteger");
                    }
                    int value = ((LazyInteger) primitiveObject).getWritableObject().get();
                    int expected = ((IntWritable) expectedObject).get();
                    if (value != expected) {
                        throw new RuntimeException("Int field mismatch (expected " + expected + " found " + value + ")");
                    }
                }
                break;
            case LONG:
                {
                    if (!(primitiveObject instanceof LazyLong)) {
                        throw new RuntimeException("Expected LazyLong");
                    }
                    long value = ((LazyLong) primitiveObject).getWritableObject().get();
                    long expected = ((LongWritable) expectedObject).get();
                    if (value != expected) {
                        throw new RuntimeException("Long field mismatch (expected " + expected + " found " + value + ")");
                    }
                }
                break;
            case FLOAT:
                {
                    if (!(primitiveObject instanceof LazyFloat)) {
                        throw new RuntimeException("Expected LazyFloat");
                    }
                    float value = ((LazyFloat) primitiveObject).getWritableObject().get();
                    float expected = ((FloatWritable) expectedObject).get();
                    if (value != expected) {
                        throw new RuntimeException("Float field mismatch (expected " + expected + " found " + value + ")");
                    }
                }
                break;
            case DOUBLE:
                {
                    if (!(primitiveObject instanceof LazyDouble)) {
                        throw new RuntimeException("Expected LazyDouble");
                    }
                    double value = ((LazyDouble) primitiveObject).getWritableObject().get();
                    double expected = ((DoubleWritable) expectedObject).get();
                    if (value != expected) {
                        throw new RuntimeException("Double field mismatch (expected " + expected + " found " + value + ")");
                    }
                }
                break;
            case STRING:
                {
                    if (!(primitiveObject instanceof LazyString)) {
                        throw new RuntimeException("Text expected writable not Text");
                    }
                    Text value = ((LazyString) primitiveObject).getWritableObject();
                    Text expected = ((Text) expectedObject);
                    if (!value.equals(expected)) {
                        throw new RuntimeException("String field mismatch (expected '" + expected + "' found '" + value + "')");
                    }
                }
                break;
            case CHAR:
                {
                    if (!(primitiveObject instanceof LazyHiveChar)) {
                        throw new RuntimeException("Expected LazyHiveChar");
                    }
                    HiveChar value = ((LazyHiveChar) primitiveObject).getWritableObject().getHiveChar();
                    HiveChar expected = ((HiveCharWritable) expectedObject).getHiveChar();
                    if (!value.equals(expected)) {
                        throw new RuntimeException("HiveChar field mismatch (expected '" + expected + "' found '" + value + "')");
                    }
                }
                break;
            case VARCHAR:
                {
                    if (!(primitiveObject instanceof LazyHiveVarchar)) {
                        throw new RuntimeException("Expected LazyHiveVarchar");
                    }
                    HiveVarchar value = ((LazyHiveVarchar) primitiveObject).getWritableObject().getHiveVarchar();
                    HiveVarchar expected = ((HiveVarcharWritable) expectedObject).getHiveVarchar();
                    if (!value.equals(expected)) {
                        throw new RuntimeException("HiveVarchar field mismatch (expected '" + expected + "' found '" + value + "')");
                    }
                }
                break;
            case DECIMAL:
                {
                    if (!(primitiveObject instanceof LazyHiveDecimal)) {
                        throw new RuntimeException("Expected LazyDecimal");
                    }
                    HiveDecimal value = ((LazyHiveDecimal) primitiveObject).getWritableObject().getHiveDecimal();
                    HiveDecimal expected = ((HiveDecimalWritable) expectedObject).getHiveDecimal();
                    if (!value.equals(expected)) {
                        DecimalTypeInfo decimalTypeInfo = (DecimalTypeInfo) primitiveTypeInfo;
                        int precision = decimalTypeInfo.getPrecision();
                        int scale = decimalTypeInfo.getScale();
                        throw new RuntimeException("Decimal field mismatch (expected " + expected.toString() + " found " + value.toString() + ") precision " + precision + ", scale " + scale);
                    }
                }
                break;
            case DATE:
                {
                    if (!(primitiveObject instanceof LazyDate)) {
                        throw new RuntimeException("Expected LazyDate");
                    }
                    Date value = ((LazyDate) primitiveObject).getWritableObject().get();
                    Date expected = ((DateWritable) expectedObject).get();
                    if (!value.equals(expected)) {
                        throw new RuntimeException("Date field mismatch (expected " + expected + " found " + value + ")");
                    }
                }
                break;
            case TIMESTAMP:
                {
                    if (!(primitiveObject instanceof LazyTimestamp)) {
                        throw new RuntimeException("TimestampWritable expected writable not TimestampWritable");
                    }
                    Timestamp value = ((LazyTimestamp) primitiveObject).getWritableObject().getTimestamp();
                    Timestamp expected = ((TimestampWritable) expectedObject).getTimestamp();
                    if (!value.equals(expected)) {
                        throw new RuntimeException("Timestamp field mismatch (expected " + expected + " found " + value + ")");
                    }
                }
                break;
            case INTERVAL_YEAR_MONTH:
                {
                    if (!(primitiveObject instanceof LazyHiveIntervalYearMonth)) {
                        throw new RuntimeException("Expected LazyHiveIntervalYearMonth");
                    }
                    HiveIntervalYearMonth value = ((LazyHiveIntervalYearMonth) primitiveObject).getWritableObject().getHiveIntervalYearMonth();
                    HiveIntervalYearMonth expected = ((HiveIntervalYearMonthWritable) expectedObject).getHiveIntervalYearMonth();
                    if (!value.equals(expected)) {
                        throw new RuntimeException("HiveIntervalYearMonth field mismatch (expected " + expected + " found " + value + ")");
                    }
                }
                break;
            case INTERVAL_DAY_TIME:
                {
                    if (!(primitiveObject instanceof LazyHiveIntervalDayTime)) {
                        throw new RuntimeException("Expected writable LazyHiveIntervalDayTime");
                    }
                    HiveIntervalDayTime value = ((LazyHiveIntervalDayTime) primitiveObject).getWritableObject().getHiveIntervalDayTime();
                    HiveIntervalDayTime expected = ((HiveIntervalDayTimeWritable) expectedObject).getHiveIntervalDayTime();
                    if (!value.equals(expected)) {
                        throw new RuntimeException("HiveIntervalDayTime field mismatch (expected " + expected + " found " + value + ")");
                    }
                }
                break;
            case BINARY:
                {
                    if (!(primitiveObject instanceof LazyBinary)) {
                        throw new RuntimeException("Expected LazyBinary");
                    }
                    BytesWritable bytesWritable = ((LazyBinary) primitiveObject).getWritableObject();
                    byte[] value = Arrays.copyOfRange(bytesWritable.getBytes(), 0, bytesWritable.getLength());
                    BytesWritable bytesWritableExpected = (BytesWritable) expectedObject;
                    byte[] expected = Arrays.copyOfRange(bytesWritableExpected.getBytes(), 0, bytesWritableExpected.getLength());
                    if (value.length != expected.length) {
                        throw new RuntimeException("Byte Array field mismatch (expected " + Arrays.toString(expected) + " found " + Arrays.toString(value) + ")");
                    }
                    for (int b = 0; b < value.length; b++) {
                        if (value[b] != expected[b]) {
                            throw new RuntimeException("Byte Array field mismatch (expected " + Arrays.toString(expected) + " found " + Arrays.toString(value) + ")");
                        }
                    }
                }
                break;
            default:
                throw new Error("Unknown primitive category " + primitiveTypeInfo.getPrimitiveCategory());
        }
    } else if (lazyObject instanceof LazyArray) {
        LazyArray lazyArray = (LazyArray) lazyObject;
        List<Object> list = lazyArray.getList();
        List<Object> expectedList = (List<Object>) expectedObject;
        ListTypeInfo listTypeInfo = (ListTypeInfo) typeInfo;
        if (list.size() != expectedList.size()) {
            throw new RuntimeException("SerDe deserialized list length does not match (list " + list.toString() + " list.size() " + list.size() + " expectedList " + expectedList.toString() + " expectedList.size() " + expectedList.size() + ")" + " elementTypeInfo " + listTypeInfo.getListElementTypeInfo().toString());
        }
        return lazyCompareList((ListTypeInfo) typeInfo, list, expectedList);
    } else if (typeInfo instanceof ListTypeInfo) {
        List<Object> list;
        if (lazyObject instanceof LazyBinaryArray) {
            list = ((LazyBinaryArray) lazyObject).getList();
        } else {
            list = (List<Object>) lazyObject;
        }
        List<Object> expectedList = (List<Object>) expectedObject;
        if (list.size() != expectedList.size()) {
            throw new RuntimeException("SerDe deserialized list length does not match (list " + list.toString() + " list.size() " + list.size() + " expectedList " + expectedList.toString() + " expectedList.size() " + expectedList.size() + ")");
        }
        return lazyCompareList((ListTypeInfo) typeInfo, list, expectedList);
    } else if (lazyObject instanceof LazyMap) {
        LazyMap lazyMap = (LazyMap) lazyObject;
        Map<Object, Object> map = lazyMap.getMap();
        Map<Object, Object> expectedMap = (Map<Object, Object>) expectedObject;
        return lazyCompareMap((MapTypeInfo) typeInfo, map, expectedMap);
    } else if (typeInfo instanceof MapTypeInfo) {
        Map<Object, Object> map;
        Map<Object, Object> expectedMap = (Map<Object, Object>) expectedObject;
        if (lazyObject instanceof LazyBinaryMap) {
            map = ((LazyBinaryMap) lazyObject).getMap();
        } else {
            map = (Map<Object, Object>) lazyObject;
        }
        return lazyCompareMap((MapTypeInfo) typeInfo, map, expectedMap);
    } else if (lazyObject instanceof LazyStruct) {
        LazyStruct lazyStruct = (LazyStruct) lazyObject;
        List<Object> fields = lazyStruct.getFieldsAsList();
        List<Object> expectedFields = (List<Object>) expectedObject;
        StructTypeInfo structTypeInfo = (StructTypeInfo) typeInfo;
        return lazyCompareStruct(structTypeInfo, fields, expectedFields);
    } else if (typeInfo instanceof StructTypeInfo) {
        ArrayList<Object> fields;
        if (lazyObject instanceof LazyBinaryStruct) {
            fields = ((LazyBinaryStruct) lazyObject).getFieldsAsList();
        } else {
            fields = (ArrayList<Object>) lazyObject;
        }
        List<Object> expectedFields = (List<Object>) expectedObject;
        StructTypeInfo structTypeInfo = (StructTypeInfo) typeInfo;
        return lazyCompareStruct(structTypeInfo, fields, expectedFields);
    } else if (lazyObject instanceof LazyUnion) {
        LazyUnion union = (LazyUnion) lazyObject;
        StandardUnionObjectInspector.StandardUnion expectedUnion = (StandardUnionObjectInspector.StandardUnion) expectedObject;
        UnionTypeInfo unionTypeInfo = (UnionTypeInfo) typeInfo;
        return lazyCompareUnion(unionTypeInfo, union, expectedUnion);
    } else if (typeInfo instanceof UnionTypeInfo) {
        StandardUnionObjectInspector.StandardUnion expectedUnion = (StandardUnionObjectInspector.StandardUnion) expectedObject;
        UnionTypeInfo unionTypeInfo = (UnionTypeInfo) typeInfo;
        if (lazyObject instanceof LazyBinaryUnion) {
            return lazyCompareUnion(unionTypeInfo, (LazyBinaryUnion) lazyObject, expectedUnion);
        } else {
            return lazyCompareUnion(unionTypeInfo, (UnionObject) lazyObject, expectedUnion);
        }
    } else {
        System.err.println("Not implemented " + typeInfo.getClass().getName());
    }
    return true;
}
Also used : StandardUnionObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StandardUnionObjectInspector) ByteWritable(org.apache.hadoop.hive.serde2.io.ByteWritable) DateWritable(org.apache.hadoop.hive.serde2.io.DateWritable) Writable(org.apache.hadoop.io.Writable) LongWritable(org.apache.hadoop.io.LongWritable) HiveCharWritable(org.apache.hadoop.hive.serde2.io.HiveCharWritable) HiveIntervalYearMonthWritable(org.apache.hadoop.hive.serde2.io.HiveIntervalYearMonthWritable) HiveIntervalDayTimeWritable(org.apache.hadoop.hive.serde2.io.HiveIntervalDayTimeWritable) BytesWritable(org.apache.hadoop.io.BytesWritable) TimestampWritable(org.apache.hadoop.hive.serde2.io.TimestampWritable) DoubleWritable(org.apache.hadoop.hive.serde2.io.DoubleWritable) ShortWritable(org.apache.hadoop.hive.serde2.io.ShortWritable) IntWritable(org.apache.hadoop.io.IntWritable) HiveVarcharWritable(org.apache.hadoop.hive.serde2.io.HiveVarcharWritable) BooleanWritable(org.apache.hadoop.io.BooleanWritable) HiveDecimalWritable(org.apache.hadoop.hive.serde2.io.HiveDecimalWritable) FloatWritable(org.apache.hadoop.io.FloatWritable) LazyBinaryArray(org.apache.hadoop.hive.serde2.lazybinary.LazyBinaryArray) PrimitiveTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo) LazyBinaryStruct(org.apache.hadoop.hive.serde2.lazybinary.LazyBinaryStruct) ArrayList(java.util.ArrayList) List(java.util.List) DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) UnionObject(org.apache.hadoop.hive.serde2.objectinspector.UnionObject) LazyBinaryMap(org.apache.hadoop.hive.serde2.lazybinary.LazyBinaryMap) LazyBinaryMap(org.apache.hadoop.hive.serde2.lazybinary.LazyBinaryMap) Map(java.util.Map) UnionTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.UnionTypeInfo) HiveChar(org.apache.hadoop.hive.common.type.HiveChar) StructTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.StructTypeInfo) Timestamp(java.sql.Timestamp) HiveDecimal(org.apache.hadoop.hive.common.type.HiveDecimal) LazyBinaryUnion(org.apache.hadoop.hive.serde2.lazybinary.LazyBinaryUnion) HiveIntervalDayTime(org.apache.hadoop.hive.common.type.HiveIntervalDayTime) Text(org.apache.hadoop.io.Text) BytesWritable(org.apache.hadoop.io.BytesWritable) HiveVarchar(org.apache.hadoop.hive.common.type.HiveVarchar) Date(java.sql.Date) HiveIntervalYearMonth(org.apache.hadoop.hive.common.type.HiveIntervalYearMonth) ListTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.ListTypeInfo) MapTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.MapTypeInfo)

Aggregations

DecimalTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo)53 PrimitiveTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo)23 HiveDecimalWritable (org.apache.hadoop.hive.serde2.io.HiveDecimalWritable)21 HiveDecimal (org.apache.hadoop.hive.common.type.HiveDecimal)16 HiveVarchar (org.apache.hadoop.hive.common.type.HiveVarchar)16 HiveChar (org.apache.hadoop.hive.common.type.HiveChar)15 CharTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.CharTypeInfo)15 VarcharTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.VarcharTypeInfo)15 IntWritable (org.apache.hadoop.io.IntWritable)15 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)14 Date (java.sql.Date)13 Timestamp (java.sql.Timestamp)13 TypeInfo (org.apache.hadoop.hive.serde2.typeinfo.TypeInfo)13 DoubleWritable (org.apache.hadoop.hive.serde2.io.DoubleWritable)12 ShortWritable (org.apache.hadoop.hive.serde2.io.ShortWritable)12 BooleanWritable (org.apache.hadoop.io.BooleanWritable)12 BytesWritable (org.apache.hadoop.io.BytesWritable)12 FloatWritable (org.apache.hadoop.io.FloatWritable)12 LongWritable (org.apache.hadoop.io.LongWritable)12 ArrayList (java.util.ArrayList)11