Skip to content

Commit

Permalink
Make RCFile reader independent of internal decimal representation
Browse files Browse the repository at this point in the history
  • Loading branch information
losipiuk authored and martint committed Mar 6, 2017
1 parent 9614143 commit 3749158
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 43 deletions.
Expand Up @@ -20,6 +20,7 @@
import com.facebook.presto.spi.block.BlockBuilder; import com.facebook.presto.spi.block.BlockBuilder;
import com.facebook.presto.spi.block.BlockBuilderStatus; import com.facebook.presto.spi.block.BlockBuilderStatus;
import com.facebook.presto.spi.type.DecimalType; import com.facebook.presto.spi.type.DecimalType;
import com.facebook.presto.spi.type.Decimals;
import com.facebook.presto.spi.type.Type; import com.facebook.presto.spi.type.Type;
import io.airlift.slice.Slice; import io.airlift.slice.Slice;
import io.airlift.slice.SliceOutput; import io.airlift.slice.SliceOutput;
Expand All @@ -30,7 +31,6 @@
import static com.facebook.presto.rcfile.RcFileDecoderUtils.decodeVIntSize; import static com.facebook.presto.rcfile.RcFileDecoderUtils.decodeVIntSize;
import static com.facebook.presto.rcfile.RcFileDecoderUtils.readVInt; import static com.facebook.presto.rcfile.RcFileDecoderUtils.readVInt;
import static com.facebook.presto.rcfile.RcFileDecoderUtils.writeVInt; import static com.facebook.presto.rcfile.RcFileDecoderUtils.writeVInt;
import static com.facebook.presto.spi.type.Decimals.encodeUnscaledValue;
import static com.facebook.presto.spi.type.Decimals.isShortDecimal; import static com.facebook.presto.spi.type.Decimals.isShortDecimal;
import static com.facebook.presto.spi.type.Decimals.rescale; import static com.facebook.presto.spi.type.Decimals.rescale;
import static com.google.common.base.Preconditions.checkState; import static com.google.common.base.Preconditions.checkState;
Expand Down Expand Up @@ -159,7 +159,7 @@ private long parseLong(Slice slice, int offset)


private Slice parseSlice(Slice slice, int offset) private Slice parseSlice(Slice slice, int offset)
{ {
// first vint is scale, which is ignored // first vint is scale
int scale = toIntExact(readVInt(slice, offset)); int scale = toIntExact(readVInt(slice, offset));
offset += decodeVIntSize(slice, offset); offset += decodeVIntSize(slice, offset);


Expand All @@ -181,10 +181,13 @@ private Slice parseSlice(Slice slice, int offset)


resultSlice.setBytes(BYTES_IN_LONG_DECIMAL - length, slice, offset, length); resultSlice.setBytes(BYTES_IN_LONG_DECIMAL - length, slice, offset, length);


// todo get rid of BigInteger
BigInteger decimal = new BigInteger(resultBytes);
if (scale != type.getScale()) { if (scale != type.getScale()) {
return encodeUnscaledValue(rescale(new BigInteger(resultBytes), scale, type.getScale())); decimal = Decimals.rescale(decimal, scale, type.getScale());
} }
return resultSlice;
return Decimals.encodeUnscaledValue(decimal);
} }


private void writeLong(SliceOutput output, long value) private void writeLong(SliceOutput output, long value)
Expand Down Expand Up @@ -219,43 +222,15 @@ private void writeSlice(SliceOutput output, Block block, int position)
writeVInt(output, type.getScale()); writeVInt(output, type.getScale());


// second vint is length // second vint is length
int length = getWriteByteCount(block, position); // todo get rid of BigInteger
writeVInt(output, length); BigInteger decimal = Decimals.decodeUnscaledValue(block.getSlice(position, 0, BYTES_IN_LONG_DECIMAL));
byte[] decimalBytes = decimal.toByteArray();
writeVInt(output, decimalBytes.length);


// write value (big endian) // write value (big endian)
// NOTE: long decimals are stored in a slice in big endian encoding // NOTE: long decimals are stored in a slice in big endian encoding
for (int i = BYTES_IN_LONG_DECIMAL - length; i < BYTES_IN_LONG_DECIMAL; i++) { for (byte decimalByte : decimalBytes) {
output.write(block.getByte(position, i)); output.write(decimalByte);
}
}

private static int getWriteByteCount(Block block, int position)
{
int length = BYTES_IN_LONG_DECIMAL;
if (block.getByte(position, 0) < 0) {
for (int i = 0; i < BYTES_IN_LONG_DECIMAL - 1; i++) {
int aByte = block.getByte(position, i);
if (aByte != 0xFFFF_FFFF) {
if (aByte >= 0) {
length++;
}
break;
}
length--;
}
}
else {
for (int i = 0; i < BYTES_IN_LONG_DECIMAL - 1; i++) {
int aByte = block.getByte(position, i);
if (aByte != 0) {
if (aByte < 0) {
length++;
}
break;
}
length--;
}
} }
return length;
} }
} }
Expand Up @@ -132,6 +132,13 @@ public static Slice unscaledDecimal(String unscaledValue)


public static Slice unscaledDecimal(BigInteger unscaledValue) public static Slice unscaledDecimal(BigInteger unscaledValue)
{ {
Slice decimal = Slices.allocate(UNSCALED_DECIMAL_128_SLICE_LENGTH);
return pack(unscaledValue, decimal);
}

public static Slice pack(BigInteger unscaledValue, Slice result)
{
pack(0, 0, false, result);
byte[] bytes = unscaledValue.abs().toByteArray(); byte[] bytes = unscaledValue.abs().toByteArray();


if (bytes.length > UNSCALED_DECIMAL_128_SLICE_LENGTH if (bytes.length > UNSCALED_DECIMAL_128_SLICE_LENGTH
Expand All @@ -142,15 +149,14 @@ public static Slice unscaledDecimal(BigInteger unscaledValue)
// convert to little-endian order // convert to little-endian order
reverse(bytes); reverse(bytes);


Slice decimal = Slices.allocate(UNSCALED_DECIMAL_128_SLICE_LENGTH); result.setBytes(0, bytes);
decimal.setBytes(0, bytes);
if (unscaledValue.signum() < 0) { if (unscaledValue.signum() < 0) {
setNegative(decimal, true); setNegative(result, true);
} }


throwIfOverflows(decimal); throwIfOverflows(result);


return decimal; return result;
} }


public static Slice unscaledDecimal(long unscaledValue) public static Slice unscaledDecimal(long unscaledValue)
Expand Down

0 comments on commit 3749158

Please sign in to comment.