use of io.airlift.slice.DynamicSliceOutput in project presto by prestodb.
the class TestSerDeUtils method blockToSlice.
private static Slice blockToSlice(Block block) {
// This function is strictly for testing use only
SliceOutput sliceOutput = new DynamicSliceOutput(1000);
BlockSerdeUtil.writeBlock(sliceOutput, block.copyRegion(0, block.getPositionCount()));
return sliceOutput.slice();
}
use of io.airlift.slice.DynamicSliceOutput in project presto by prestodb.
the class AbstractTestType method assertPositionValue.
private void assertPositionValue(Block block, int position, Object expectedStackValue, long expectedHash, Object expectedObjectValue) {
Object objectValue = type.getObjectValue(SESSION, block, position);
assertEquals(objectValue, expectedObjectValue);
if (objectValue != null) {
assertInstanceOf(objectValue, objectValueType);
}
if (type.isComparable()) {
assertEquals(hashPosition(type, block, position), expectedHash);
} else {
try {
type.hash(block, position);
fail("Expected UnsupportedOperationException");
} catch (UnsupportedOperationException expected) {
}
}
Block expectedBlock = createBlock(type, expectedStackValue);
if (type.isComparable()) {
assertTrue(positionEqualsPosition(type, block, position, block, position));
assertTrue(positionEqualsPosition(type, block, position, expectedBlock, 0));
assertTrue(positionEqualsPosition(type, expectedBlock, 0, block, position));
}
assertEquals(block.isNull(position), expectedStackValue == null);
if (type.isOrderable()) {
assertTrue(ASC_NULLS_FIRST.compareBlockValue(type, block, position, expectedBlock, 0) == 0);
assertTrue(ASC_NULLS_LAST.compareBlockValue(type, block, position, expectedBlock, 0) == 0);
assertTrue(DESC_NULLS_FIRST.compareBlockValue(type, block, position, expectedBlock, 0) == 0);
assertTrue(DESC_NULLS_LAST.compareBlockValue(type, block, position, expectedBlock, 0) == 0);
} else {
try {
type.compareTo(block, position, expectedBlock, 0);
fail("Expected UnsupportedOperationException");
} catch (UnsupportedOperationException expected) {
}
}
verifyInvalidPositionHandling(block);
if (block.isNull(position)) {
if (type.isOrderable() && type.getJavaType() != void.class) {
Block nonNullValue = toBlock(getNonNullValue());
assertTrue(ASC_NULLS_FIRST.compareBlockValue(type, block, position, nonNullValue, 0) < 0);
assertTrue(ASC_NULLS_LAST.compareBlockValue(type, block, position, nonNullValue, 0) > 0);
assertTrue(DESC_NULLS_FIRST.compareBlockValue(type, block, position, nonNullValue, 0) < 0);
assertTrue(DESC_NULLS_LAST.compareBlockValue(type, block, position, nonNullValue, 0) > 0);
}
return;
}
if (type.isOrderable() && expectedStackValue != Boolean.TRUE) {
Block greaterValue = toBlock(getGreaterValue(expectedStackValue));
assertTrue(ASC_NULLS_FIRST.compareBlockValue(type, block, position, greaterValue, 0) < 0);
assertTrue(ASC_NULLS_LAST.compareBlockValue(type, block, position, greaterValue, 0) < 0);
assertTrue(DESC_NULLS_FIRST.compareBlockValue(type, block, position, greaterValue, 0) > 0);
assertTrue(DESC_NULLS_LAST.compareBlockValue(type, block, position, greaterValue, 0) > 0);
}
if (type.getJavaType() == boolean.class) {
assertEquals(type.getBoolean(block, position), expectedStackValue);
try {
type.getLong(block, position);
fail("Expected IllegalStateException or UnsupportedOperationException");
} catch (IllegalStateException | UnsupportedOperationException expected) {
}
try {
type.getDouble(block, position);
fail("Expected IllegalStateException or UnsupportedOperationException");
} catch (IllegalStateException | UnsupportedOperationException expected) {
}
try {
type.getObject(block, position);
fail("Expected IllegalStateException or UnsupportedOperationException");
} catch (IllegalStateException | UnsupportedOperationException expected) {
}
} else if (type.getJavaType() == long.class) {
assertEquals(type.getLong(block, position), expectedStackValue);
try {
type.getBoolean(block, position);
fail("Expected IllegalStateException or UnsupportedOperationException");
} catch (IllegalStateException | UnsupportedOperationException expected) {
}
try {
type.getDouble(block, position);
fail("Expected IllegalStateException or UnsupportedOperationException");
} catch (IllegalStateException | UnsupportedOperationException expected) {
}
try {
type.getObject(block, position);
fail("Expected IllegalStateException or UnsupportedOperationException");
} catch (IllegalStateException | UnsupportedOperationException expected) {
}
} else if (type.getJavaType() == double.class) {
assertEquals(type.getDouble(block, position), expectedStackValue);
try {
type.getBoolean(block, position);
fail("Expected IllegalStateException or UnsupportedOperationException");
} catch (IllegalStateException | UnsupportedOperationException expected) {
}
try {
type.getLong(block, position);
fail("Expected IllegalStateException or UnsupportedOperationException");
} catch (IllegalStateException | UnsupportedOperationException expected) {
}
try {
type.getObject(block, position);
fail("Expected IllegalStateException or UnsupportedOperationException");
} catch (IllegalStateException | UnsupportedOperationException expected) {
}
} else if (type.getJavaType() == Slice.class) {
assertEquals(type.getSlice(block, position), expectedStackValue);
try {
type.getBoolean(block, position);
fail("Expected IllegalStateException or UnsupportedOperationException");
} catch (IllegalStateException | UnsupportedOperationException expected) {
}
try {
type.getLong(block, position);
fail("Expected IllegalStateException or UnsupportedOperationException");
} catch (IllegalStateException | UnsupportedOperationException expected) {
}
try {
type.getDouble(block, position);
fail("Expected IllegalStateException or UnsupportedOperationException");
} catch (IllegalStateException | UnsupportedOperationException expected) {
}
try {
type.getObject(block, position);
fail("Expected IllegalStateException or UnsupportedOperationException");
} catch (IllegalStateException | UnsupportedOperationException expected) {
}
} else {
SliceOutput actualSliceOutput = new DynamicSliceOutput(100);
writeBlock(actualSliceOutput, (Block) type.getObject(block, position));
SliceOutput expectedSliceOutput = new DynamicSliceOutput(actualSliceOutput.size());
writeBlock(expectedSliceOutput, (Block) expectedStackValue);
assertEquals(actualSliceOutput.slice(), expectedSliceOutput.slice());
try {
type.getBoolean(block, position);
fail("Expected IllegalStateException or UnsupportedOperationException");
} catch (IllegalStateException | UnsupportedOperationException expected) {
}
try {
type.getLong(block, position);
fail("Expected IllegalStateException or UnsupportedOperationException");
} catch (IllegalStateException | UnsupportedOperationException expected) {
}
try {
type.getDouble(block, position);
fail("Expected IllegalStateException or UnsupportedOperationException");
} catch (IllegalStateException | UnsupportedOperationException expected) {
}
try {
type.getSlice(block, position);
fail("Expected IllegalStateException or UnsupportedOperationException");
} catch (IllegalStateException | UnsupportedOperationException expected) {
}
}
}
use of io.airlift.slice.DynamicSliceOutput in project presto by prestodb.
the class StringClassifierAdapter method getSerializedData.
@Override
public byte[] getSerializedData() {
byte[] classifierBytes = serialize(classifier).getBytes();
DynamicSliceOutput output = new DynamicSliceOutput(classifierBytes.length + 64 * labelEnumeration.size());
output.appendInt(classifierBytes.length);
output.appendBytes(classifierBytes);
output.appendInt(labelEnumeration.size());
// Write the enumeration keys
for (Map.Entry<Integer, String> entry : labelEnumeration.entrySet()) {
output.appendInt(entry.getKey());
byte[] bytes = entry.getValue().getBytes(UTF_8);
output.appendInt(bytes.length);
output.appendBytes(bytes);
}
return output.slice().getBytes();
}
use of io.airlift.slice.DynamicSliceOutput in project presto by prestodb.
the class TestRcFileReaderManual method testStartSync.
@Test
public void testStartSync() throws Exception {
SliceOutput output = new DynamicSliceOutput(10 * 1024);
List<Segment> segments = ImmutableList.of(writeSegment(output, ImmutableList.of()), writeSegment(output, ImmutableList.of(ImmutableList.of(0, 2, 3, 4), ImmutableList.of(10, 12, 13))), writeSegment(output, ImmutableList.of(ImmutableList.of(20, 22), ImmutableList.of(30, 33), ImmutableList.of(40, 44))), writeSegment(output, ImmutableList.of(ImmutableList.of(100, 101, 102))));
assertFileSegments(output.slice(), segments);
}
use of io.airlift.slice.DynamicSliceOutput in project presto by prestodb.
the class TestDictionaryBlockEncoding method testRoundTrip.
@Test
public void testRoundTrip() {
int positionCount = 40;
// build dictionary
BlockBuilder dictionaryBuilder = VARCHAR.createBlockBuilder(new BlockBuilderStatus(), 4);
VARCHAR.writeString(dictionaryBuilder, "alice");
VARCHAR.writeString(dictionaryBuilder, "bob");
VARCHAR.writeString(dictionaryBuilder, "charlie");
VARCHAR.writeString(dictionaryBuilder, "dave");
Block dictionary = dictionaryBuilder.build();
// build ids
int[] ids = new int[positionCount];
for (int i = 0; i < 40; i++) {
ids[i] = i % 4;
}
BlockEncoding blockEncoding = new DictionaryBlockEncoding(new VariableWidthBlockEncoding());
DictionaryBlock dictionaryBlock = new DictionaryBlock(positionCount, dictionary, ids);
DynamicSliceOutput sliceOutput = new DynamicSliceOutput(1024);
blockEncoding.writeBlock(sliceOutput, dictionaryBlock);
Block actualBlock = blockEncoding.readBlock(sliceOutput.slice().getInput());
assertTrue(actualBlock instanceof DictionaryBlock);
DictionaryBlock actualDictionaryBlock = (DictionaryBlock) actualBlock;
assertBlockEquals(VARCHAR, actualDictionaryBlock.getDictionary(), dictionary);
for (int position = 0; position < actualDictionaryBlock.getPositionCount(); position++) {
assertEquals(actualDictionaryBlock.getId(position), ids[position]);
}
assertEquals(actualDictionaryBlock.getDictionarySourceId(), dictionaryBlock.getDictionarySourceId());
}
Aggregations