Search in sources :

Example 1 with DynamicSliceOutput

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();
}
Also used : SliceOutput(io.airlift.slice.SliceOutput) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput)

Example 2 with DynamicSliceOutput

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) {
        }
    }
}
Also used : SliceOutput(io.airlift.slice.SliceOutput) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) Slice(io.airlift.slice.Slice) Block(com.facebook.presto.spi.block.Block) BlockSerdeUtil.writeBlock(com.facebook.presto.block.BlockSerdeUtil.writeBlock) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput)

Example 3 with DynamicSliceOutput

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();
}
Also used : DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with DynamicSliceOutput

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);
}
Also used : SliceOutput(io.airlift.slice.SliceOutput) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) Test(org.testng.annotations.Test)

Example 5 with DynamicSliceOutput

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());
}
Also used : DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) Test(org.testng.annotations.Test)

Aggregations

DynamicSliceOutput (io.airlift.slice.DynamicSliceOutput)24 SliceOutput (io.airlift.slice.SliceOutput)9 Test (org.testng.annotations.Test)8 Block (com.facebook.presto.spi.block.Block)5 Slice (io.airlift.slice.Slice)4 BlockSerdeUtil.writeBlock (com.facebook.presto.block.BlockSerdeUtil.writeBlock)3 BlockBuilderStatus (com.facebook.presto.spi.block.BlockBuilderStatus)3 Page (com.facebook.presto.spi.Page)2 PrestoException (com.facebook.presto.spi.PrestoException)2 LiteralParameters (com.facebook.presto.spi.function.LiteralParameters)2 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)2 SqlType (com.facebook.presto.spi.function.SqlType)2 UsedByGeneratedCode (com.facebook.presto.annotation.UsedByGeneratedCode)1 BufferResult (com.facebook.presto.execution.buffer.BufferResult)1 Signature (com.facebook.presto.metadata.Signature)1 BlockBuilder (com.facebook.presto.spi.block.BlockBuilder)1 BlockEncoding (com.facebook.presto.spi.block.BlockEncoding)1 InterleavedBlockBuilder (com.facebook.presto.spi.block.InterleavedBlockBuilder)1 Description (com.facebook.presto.spi.function.Description)1 Type (com.facebook.presto.spi.type.Type)1