@VisibleForTesting int getBufferCapacity() { return slice.length(); } }
@Override public String toString() { StringBuilder builder = new StringBuilder("OutputStreamSliceOutputAdapter{"); builder.append("outputStream=").append(compressedOutputStream); builder.append("bufferSize=").append(slice.length()); builder.append('}'); return builder.toString(); }
private static int hashBytes(int initialValue, Slice bytes) { int result = initialValue; for (int i = 0; i < bytes.length(); i++) { result = result * 31 + bytes.getByte(i); } return result; }
private void ensureWritableBytes(int minWritableBytes) { checkArgument(minWritableBytes <= MAX_UNUSED_BUFFER_SIZE); if (bufferPosition + minWritableBytes > slice.length()) { closeChunk(); } }
@Override public void writeSlice(BlockBuilder blockBuilder, Slice value) { writeSlice(blockBuilder, value, 0, value.length()); }
private void ensureWritableBytes(int minWritableBytes) { checkArgument(minWritableBytes <= MAX_UNUSED_BUFFER_SIZE); if (bufferPosition + minWritableBytes > slice.length()) { closeChunk(); } }
private static int getSize(NullableValue nullableValue) { if (nullableValue.isNull()) { return 0; } Object value = nullableValue.getValue(); checkArgument(value instanceof Slice, "value is expected to be of Slice type"); return ((Slice) value).length(); }
private Slice dropStringMinMaxIfNecessary(Slice minOrMax) { if (minOrMax == null || minOrMax.length() > stringStatisticsLimitInBytes) { return null; } // Do not hold the entire slice where the actual stats could be small if (minOrMax.isCompact()) { return minOrMax; } return Slices.copyOf(minOrMax); } }
@Description("decode bigint value from a 32-bit 2's complement big endian varbinary") @ScalarFunction("from_big_endian_32") @SqlType(StandardTypes.INTEGER) public static long fromBigEndian32(@SqlType(StandardTypes.VARBINARY) Slice slice) { if (slice.length() != Integer.BYTES) { throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "expected 4-byte input, but got instead: " + slice.length()); } return Integer.reverseBytes(slice.getInt(0)); }
/** * Does the file start with the ORC magic bytes? */ private static boolean isValidHeaderMagic(OrcDataSource source) throws IOException { byte[] headerMagic = new byte[MAGIC.length()]; source.readFully(0, headerMagic); return MAGIC.equals(Slices.wrappedBuffer(headerMagic)); }
private static Slice decompressSnappy(Slice input, int uncompressedSize) { byte[] buffer = new byte[uncompressedSize]; decompress(new SnappyDecompressor(), input, 0, input.length(), buffer, 0); return wrappedBuffer(buffer); }
@Test public void testMinAverageValueBytes() { assertMinAverageValueBytes(0L, ImmutableList.of()); assertMinAverageValueBytes(BINARY_VALUE_BYTES_OVERHEAD, ImmutableList.of(EMPTY_SLICE)); assertMinAverageValueBytes(FIRST_VALUE.length() + BINARY_VALUE_BYTES_OVERHEAD, ImmutableList.of(FIRST_VALUE)); assertMinAverageValueBytes((FIRST_VALUE.length() + SECOND_VALUE.length()) / 2 + BINARY_VALUE_BYTES_OVERHEAD, ImmutableList.of(FIRST_VALUE, SECOND_VALUE)); }
@Test public void testSum() { StringStatisticsBuilder stringStatisticsBuilder = new StringStatisticsBuilder(Integer.MAX_VALUE); for (Slice value : ImmutableList.of(EMPTY_SLICE, LOW_BOTTOM_VALUE, LOW_TOP_VALUE)) { stringStatisticsBuilder.addValue(value); } assertStringStatistics(stringStatisticsBuilder.buildColumnStatistics(), 3, EMPTY_SLICE.length() + LOW_BOTTOM_VALUE.length() + LOW_TOP_VALUE.length()); }
@Test public void testSum() { BinaryStatisticsBuilder binaryStatisticsBuilder = new BinaryStatisticsBuilder(); for (Slice value : ImmutableList.of(EMPTY_SLICE, FIRST_VALUE, SECOND_VALUE)) { binaryStatisticsBuilder.addValue(value); } assertBinaryStatistics(binaryStatisticsBuilder.buildColumnStatistics(), 3, EMPTY_SLICE.length() + FIRST_VALUE.length() + SECOND_VALUE.length()); }
@Description("length of the given binary") @ScalarFunction @SqlType(StandardTypes.BIGINT) public static long length(@SqlType(StandardTypes.VARBINARY) Slice slice) { return slice.length(); }
@Description("decode the 32-bit big-endian binary in IEEE 754 single-precision floating-point format") @ScalarFunction("from_ieee754_32") @SqlType(StandardTypes.REAL) public static long fromIEEE754Binary32(@SqlType(StandardTypes.VARBINARY) Slice slice) { checkCondition(slice.length() == Integer.BYTES, INVALID_FUNCTION_ARGUMENT, "Input floating-point value must be exactly 4 bytes long"); return Integer.reverseBytes(slice.getInt(0)); }
@Description("compute SpookyHashV2 32-bit hash") @ScalarFunction @SqlType(StandardTypes.VARBINARY) public static Slice spookyHashV2_32(@SqlType(StandardTypes.VARBINARY) Slice slice) { Slice hash = Slices.allocate(Integer.BYTES); hash.setInt(0, Integer.reverseBytes(SpookyHashV2.hash32(slice, 0, slice.length(), 0))); return hash; }
@Description("compute SpookyHashV2 64-bit hash") @ScalarFunction @SqlType(StandardTypes.VARBINARY) public static Slice spookyHashV2_64(@SqlType(StandardTypes.VARBINARY) Slice slice) { Slice hash = Slices.allocate(Long.BYTES); hash.setLong(0, Long.reverseBytes(SpookyHashV2.hash64(slice, 0, slice.length(), 0))); return hash; }