private static void writeValue(Type type, BlockBuilder blockBuilder, Object value) blockBuilder.appendNull(); else if (BIGINT.equals(type)) { type.writeLong(blockBuilder, ((Number) value).longValue()); else if (INTEGER.equals(type)) { type.writeLong(blockBuilder, ((Number) value).intValue()); type.writeLong(blockBuilder, (long) floatToRawIntBits(((Number) value).floatValue())); else if (DOUBLE.equals(type)) { type.writeDouble(blockBuilder, ((Number) value).doubleValue()); else if (BOOLEAN.equals(type)) { type.writeBoolean(blockBuilder, (Boolean) value); type.writeSlice(blockBuilder, Slices.utf8Slice((String) value)); type.writeSlice(blockBuilder, Slices.utf8Slice((String) value)); else if (VARBINARY.equals(type)) { type.writeSlice(blockBuilder, Slices.wrappedBuffer((byte[]) value)); blockBuilder.closeEntry();
public static Block createSlicesBlock(Iterable<Slice> values) { BlockBuilder builder = VARBINARY.createBlockBuilder(null, 100); for (Slice value : values) { if (value == null) { builder.appendNull(); } else { VARBINARY.writeSlice(builder, value); } } return builder.build(); }
@Override public void deserialize(Block block, int index, LongDecimalWithOverflowState state) { if (!block.isNull(index)) { SliceInput slice = VARBINARY.getSlice(block, index).getInput(); state.setOverflow(slice.readLong()); state.setLongDecimal(Slices.copyOf(slice.readSlice(slice.available()))); } } }
private static Block toSingeValuedBlock(Slice expectedValue) { BlockBuilder blockBuilder = VARBINARY.createBlockBuilder(null, 1, expectedValue.length()); VARBINARY.writeSlice(blockBuilder, expectedValue); return blockBuilder.build(); }
@Test public void testHashCode() { Slice data = Slices.wrappedBuffer(ALL_BYTES); Block block = VARBINARY.createBlockBuilder(null, 1, ALL_BYTES.length) .writeBytes(data, 0, data.length()) .closeEntry() .build(); assertEquals(VarbinaryOperators.hashCode(data), VARBINARY.hash(block, 0)); }
if (BOOLEAN.equals(type)) { checkArgument(value instanceof Boolean, "Expected value %d to be an instance of Boolean, but is a %s", i, value.getClass().getSimpleName()); else if (INTEGER.equals(type)) { checkArgument(value instanceof Integer, "Expected value %d to be an instance of Integer, but is a %s", i, value.getClass().getSimpleName()); else if (BIGINT.equals(type) || DATE.equals(type) || TIMESTAMP.equals(type) || TIMESTAMP_WITH_TIME_ZONE.equals(type)) { checkArgument(value instanceof Integer || value instanceof Long, "Expected value %d to be an instance of Integer or Long, but is a %s", i, value.getClass().getSimpleName()); else if (DOUBLE.equals(type)) { checkArgument(value instanceof Double, "Expected value %d to be an instance of Double, but is a %s", i, value.getClass().getSimpleName()); "Expected value %d to be an instance of String or byte[], but is a %s", i, value.getClass().getSimpleName()); else if (VARBINARY.equals(type)) { checkArgument(value instanceof Slice, "Expected value %d to be an instance of Slice, but is a %s", i, value.getClass().getSimpleName());
Block expectedBoolean = VARBINARY.createBlockBuilder(null, 1).writeByte(1).closeEntry().build(); Block actualBoolean = toBinaryBlock(BOOLEAN, true, getInspector(Boolean.class)); assertBlockEquals(actualBoolean, expectedBoolean); Block expectedByte = VARBINARY.createBlockBuilder(null, 1).writeByte(5).closeEntry().build(); Block actualByte = toBinaryBlock(TINYINT, (byte) 5, getInspector(Byte.class)); assertBlockEquals(actualByte, expectedByte); Block expectedShort = VARBINARY.createBlockBuilder(null, 1).writeShort(2).closeEntry().build(); Block actualShort = toBinaryBlock(SMALLINT, (short) 2, getInspector(Short.class)); assertBlockEquals(actualShort, expectedShort); Block expectedInt = VARBINARY.createBlockBuilder(null, 1).writeInt(1).closeEntry().build(); Block actualInt = toBinaryBlock(INTEGER, 1, getInspector(Integer.class)); assertBlockEquals(actualInt, expectedInt); Block expectedLong = VARBINARY.createBlockBuilder(null, 1).writeLong(10).closeEntry().build(); Block actualLong = toBinaryBlock(BIGINT, 10L, getInspector(Long.class)); assertBlockEquals(actualLong, expectedLong); Block expectedFloat = VARBINARY.createBlockBuilder(null, 1).writeInt(floatToRawIntBits(20.0f)).closeEntry().build(); Block actualFloat = toBinaryBlock(REAL, 20.0f, getInspector(Float.class)); assertBlockEquals(actualFloat, expectedFloat); Block expectedDouble = VARBINARY.createBlockBuilder(null, 1).writeLong(doubleToLongBits(30.12)).closeEntry().build(); Block actualDouble = toBinaryBlock(DOUBLE, 30.12d, getInspector(Double.class)); assertBlockEquals(actualDouble, expectedDouble); Block expectedString = VARBINARY.createBlockBuilder(null, 1).writeBytes(utf8Slice("abdd"), 0, 4).closeEntry().build();
builder.appendNull(); return; BooleanType.BOOLEAN.writeBoolean(builder, ((BooleanObjectInspector) inspector).get(object)); return; case BYTE: return; case INT: IntegerType.INTEGER.writeLong(builder, ((IntObjectInspector) inspector).get(object)); return; case LONG: BigintType.BIGINT.writeLong(builder, ((LongObjectInspector) inspector).get(object)); return; case FLOAT: return; case BINARY: VARBINARY.writeSlice(builder, Slices.wrappedBuffer(((BinaryObjectInspector) inspector).getPrimitiveJavaObject(object))); return; case DECIMAL:
Block expectedBoolean = VARBINARY.createBlockBuilder(new BlockBuilderStatus(), 1).writeByte(1).closeEntry().build(); Block actualBoolean = toBinaryBlock(BOOLEAN, true, getInspector(Boolean.class)); assertBlockEquals(actualBoolean, expectedBoolean); Block expectedByte = VARBINARY.createBlockBuilder(new BlockBuilderStatus(), 1).writeLong(5).closeEntry().build(); Block actualByte = toBinaryBlock(BIGINT, (byte) 5, getInspector(Byte.class)); assertBlockEquals(actualByte, expectedByte); Block expectedShort = VARBINARY.createBlockBuilder(new BlockBuilderStatus(), 1).writeLong(2).closeEntry().build(); Block actualShort = toBinaryBlock(BIGINT, (short) 2, getInspector(Short.class)); assertBlockEquals(actualShort, expectedShort); Block expectedInt = VARBINARY.createBlockBuilder(new BlockBuilderStatus(), 1).writeLong(1).closeEntry().build(); Block actualInt = toBinaryBlock(BIGINT, 1, getInspector(Integer.class)); assertBlockEquals(actualInt, expectedInt); Block expectedLong = VARBINARY.createBlockBuilder(new BlockBuilderStatus(), 1).writeLong(10).closeEntry().build(); Block actualLong = toBinaryBlock(BIGINT, 10L, getInspector(Long.class)); assertBlockEquals(actualLong, expectedLong); Block expectedFloat = VARBINARY.createBlockBuilder(new BlockBuilderStatus(), 1).writeDouble(20.0).closeEntry().build(); Block actualFloat = toBinaryBlock(DOUBLE, 20.0f, getInspector(Float.class)); assertBlockEquals(actualFloat, expectedFloat); Block expectedDouble = VARBINARY.createBlockBuilder(new BlockBuilderStatus(), 1).writeDouble(30.12).closeEntry().build(); Block actualDouble = toBinaryBlock(DOUBLE, 30.12d, getInspector(Double.class)); assertBlockEquals(actualDouble, expectedDouble); Block expectedString = VARBINARY.createBlockBuilder(new BlockBuilderStatus(), 1).writeBytes(utf8Slice("abdd"), 0, 4).closeEntry().build();
protected void assertSlicePosition(Block block, int position, Slice expectedSliceValue) { int length = block.getSliceLength(position); assertEquals(length, expectedSliceValue.length()); Block expectedBlock = toSingeValuedBlock(expectedSliceValue); for (int offset = 0; offset < length - 3; offset++) { assertEquals(block.getSlice(position, offset, 3), expectedSliceValue.slice(offset, 3)); assertTrue(block.bytesEqual(position, offset, expectedSliceValue, offset, 3)); // if your tests fail here, please change your test to not use this value assertFalse(block.bytesEqual(position, offset, Slices.utf8Slice("XXX"), 0, 3)); assertEquals(block.bytesCompare(position, offset, 3, expectedSliceValue, offset, 3), 0); assertTrue(block.bytesCompare(position, offset, 3, expectedSliceValue, offset, 2) > 0); Slice greaterSlice = createGreaterValue(expectedSliceValue, offset, 3); assertTrue(block.bytesCompare(position, offset, 3, greaterSlice, 0, greaterSlice.length()) < 0); assertTrue(block.equals(position, offset, expectedBlock, 0, offset, 3)); assertEquals(block.compareTo(position, offset, 3, expectedBlock, 0, offset, 3), 0); BlockBuilder blockBuilder = VARBINARY.createBlockBuilder(null, 1); block.writeBytesTo(position, offset, 3, blockBuilder); blockBuilder.closeEntry(); Block segment = blockBuilder.build(); assertTrue(block.equals(position, offset, segment, 0, 0, 3)); } }
assertTrue(block.bytesEqual(position, offset, expectedSliceValue, offset, 3)); assertFalse(block.bytesEqual(position, offset, Slices.utf8Slice("XXX"), 0, 3)); assertEquals(block.compareTo(position, offset, 3, expectedBlock, 0, offset, 3), 0); BlockBuilder blockBuilder = VARBINARY.createBlockBuilder(new BlockBuilderStatus(), 1); block.writeBytesTo(position, offset, 3, blockBuilder); blockBuilder.closeEntry(); Block segment = blockBuilder.build(); assertEquals(actual.getPositionCount(), expected.length); for (int i = 0; i < expected.length; i++) { assertEquals(BIGINT.getLong(actual, i), expected[i]);
private static void serializePrimitive(Type type, BlockBuilder builder, Object object) { requireNonNull(builder, "parent builder is null"); if (object == null) { builder.appendNull(); return; } if (BOOLEAN.equals(type)) { BOOLEAN.writeBoolean(builder, (Boolean) object); } else if (BIGINT.equals(type) || INTEGER.equals(type) || SMALLINT.equals(type) || TINYINT.equals(type) || REAL.equals(type) || DATE.equals(type) || TIMESTAMP.equals(type)) { type.writeLong(builder, getLongExpressedValue(object)); } else if (DOUBLE.equals(type)) { DOUBLE.writeDouble(builder, ((Number) object).doubleValue()); } else if (isVarcharType(type) || VARBINARY.equals(type) || isCharType(type)) { type.writeSlice(builder, getSliceExpressedValue(object, type)); } else { throw new UnsupportedOperationException("Unsupported primitive type: " + type); } }
private Page buildPage() { BlockBuilder col1 = BIGINT.createBlockBuilder(null, 1); BlockBuilder col2 = DOUBLE.createBlockBuilder(null, 1); BlockBuilder col3 = VARBINARY.createBlockBuilder(null, 1); col1.writeLong(42).closeEntry(); col2.writeLong(doubleToLongBits(43.0)).closeEntry(); col3.writeLong(doubleToLongBits(43.0)).writeLong(1).closeEntry(); return new Page(col1.build(), col2.build(), col3.build()); } }
@Override public void serialize(LongDecimalWithOverflowState state, BlockBuilder out) { if (state.getLongDecimal() == null) { out.appendNull(); } else { Slice slice = Slices.allocate(Long.BYTES + UnscaledDecimal128Arithmetic.UNSCALED_DECIMAL_128_SLICE_LENGTH); SliceOutput output = slice.getOutput(); output.writeLong(state.getOverflow()); output.writeBytes(state.getLongDecimal()); VARBINARY.writeSlice(out, slice); } }
@Override public void addBinary(Binary value) { addMissingValues(); if (type == TIMESTAMP) { builder.writeLong(ParquetTimestampUtils.getTimestampMillis(value)).closeEntry(); } else { VARBINARY.writeSlice(builder, wrappedBuffer(value.getBytes())); } }
private Page createFragmentsPage() { Collection<Slice> fragments = getFutureValue(finishFuture); committed = true; updateWrittenBytes(); // output page will only be constructed once, // so a new PageBuilder is constructed (instead of using PageBuilder.reset) PageBuilder page = new PageBuilder(fragments.size() + 1, ImmutableList.of(types.get(ROW_COUNT_CHANNEL), types.get(FRAGMENT_CHANNEL))); BlockBuilder rowsBuilder = page.getBlockBuilder(0); BlockBuilder fragmentBuilder = page.getBlockBuilder(1); // write row count page.declarePosition(); BIGINT.writeLong(rowsBuilder, rowCount); fragmentBuilder.appendNull(); // write fragments for (Slice fragment : fragments) { page.declarePosition(); rowsBuilder.appendNull(); VARBINARY.writeSlice(fragmentBuilder, fragment); } return page.build(); }
public static void output(NullableLongState state, BlockBuilder out) { if (state.isNull()) { out.appendNull(); } else { VARBINARY.writeSlice(out, wrappedLongArray(state.getLong())); } } }
@Override public void serialize(DoubleHistogramAggregation.State state, BlockBuilder out) { if (state.get() == null) { out.appendNull(); } else { VARBINARY.writeSlice(out, state.get().serialize()); } }
private static Block getPrimitiveBlock(com.facebook.presto.spi.type.Type type, Object object, ObjectInspector inspector) { BlockBuilder builder = VARBINARY.createBlockBuilder(null, 1); serializeObject(type, builder, object, inspector); return builder.build(); } }