Java Code Examples for io.prestosql.spi.block.Block#getObject()

The following examples show how to use io.prestosql.spi.block.Block#getObject() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ArrayType.java    From presto with Apache License 2.0 6 votes vote down vote up
@Override
public boolean equalTo(Block leftBlock, int leftPosition, Block rightBlock, int rightPosition)
{
    Block leftArray = leftBlock.getObject(leftPosition, Block.class);
    Block rightArray = rightBlock.getObject(rightPosition, Block.class);

    if (leftArray.getPositionCount() != rightArray.getPositionCount()) {
        return false;
    }

    for (int i = 0; i < leftArray.getPositionCount(); i++) {
        checkElementNotNull(leftArray.isNull(i), ARRAY_NULL_ELEMENT_MSG);
        checkElementNotNull(rightArray.isNull(i), ARRAY_NULL_ELEMENT_MSG);
        if (!elementType.equalTo(leftArray, i, rightArray, i)) {
            return false;
        }
    }

    return true;
}
 
Example 2
Source File: StructEncoding.java    From presto with Apache License 2.0 6 votes vote down vote up
@Override
public void encodeValue(Block block, int position, SliceOutput output)
{
    Block row = block.getObject(position, Block.class);

    // write values
    for (int batchStart = 0; batchStart < row.getPositionCount(); batchStart += 8) {
        int batchEnd = Math.min(batchStart + 8, structFields.size());

        int nullByte = 0;
        for (int fieldId = batchStart; fieldId < batchEnd; fieldId++) {
            if (!row.isNull(fieldId)) {
                nullByte |= (1 << (fieldId % 8));
            }
        }
        output.writeByte(nullByte);
        for (int fieldId = batchStart; fieldId < batchEnd; fieldId++) {
            if (!row.isNull(fieldId)) {
                BinaryColumnEncoding field = structFields.get(fieldId);
                field.encodeValueInto(row, fieldId, output);
            }
        }
    }
}
 
Example 3
Source File: ListEncoding.java    From presto with Apache License 2.0 6 votes vote down vote up
@Override
public void encodeValueInto(int depth, Block block, int position, SliceOutput output)
        throws RcFileCorruptionException
{
    byte separator = getSeparator(depth);

    Block list = block.getObject(position, Block.class);
    for (int elementIndex = 0; elementIndex < list.getPositionCount(); elementIndex++) {
        if (elementIndex > 0) {
            output.writeByte(separator);
        }
        if (list.isNull(elementIndex)) {
            output.writeBytes(nullSequence);
        }
        else {
            elementEncoding.encodeValueInto(depth + 1, list, elementIndex, output);
        }
    }
}
 
Example 4
Source File: StructEncoding.java    From presto with Apache License 2.0 6 votes vote down vote up
@Override
public void encodeValueInto(int depth, Block block, int position, SliceOutput output)
        throws RcFileCorruptionException
{
    byte separator = getSeparator(depth);

    Block row = block.getObject(position, Block.class);
    for (int fieldIndex = 0; fieldIndex < structFields.size(); fieldIndex++) {
        if (fieldIndex > 0) {
            output.writeByte(separator);
        }

        if (row.isNull(fieldIndex)) {
            output.writeBytes(nullSequence);
        }
        else {
            structFields.get(fieldIndex).encodeValueInto(depth + 1, row, fieldIndex, output);
        }
    }
}
 
Example 5
Source File: HiveWriteUtils.java    From presto with Apache License 2.0 6 votes vote down vote up
@Override
public void setField(Block block, int position)
{
    Block rowBlock = block.getObject(position, Block.class);

    // TODO reuse row object and use FieldSetters, like we do at the top level
    // Ideally, we'd use the same recursive structure starting from the top, but
    // this requires modeling row types in the same way we model table rows
    // (multiple blocks vs all fields packed in a single block)
    List<Object> value = new ArrayList<>(fieldTypes.size());
    for (int i = 0; i < fieldTypes.size(); i++) {
        Object element = getField(fieldTypes.get(i), rowBlock, i);
        value.add(element);
    }

    rowInspector.setStructFieldData(row, field, value);
}
 
Example 6
Source File: RowType.java    From presto with Apache License 2.0 6 votes vote down vote up
@Override
public boolean equalTo(Block leftBlock, int leftPosition, Block rightBlock, int rightPosition)
{
    Block leftRow = leftBlock.getObject(leftPosition, Block.class);
    Block rightRow = rightBlock.getObject(rightPosition, Block.class);

    for (int i = 0; i < leftRow.getPositionCount(); i++) {
        checkElementNotNull(leftRow.isNull(i));
        checkElementNotNull(rightRow.isNull(i));
        Type fieldType = fields.get(i).getType();
        if (!fieldType.equalTo(leftRow, i, rightRow, i)) {
            return false;
        }
    }

    return true;
}
 
Example 7
Source File: MapType.java    From presto with Apache License 2.0 6 votes vote down vote up
@Override
public Object getObjectValue(ConnectorSession session, Block block, int position)
{
    if (block.isNull(position)) {
        return null;
    }

    Block singleMapBlock = block.getObject(position, Block.class);
    if (!(singleMapBlock instanceof SingleMapBlock)) {
        throw new UnsupportedOperationException("Map is encoded with legacy block representation");
    }
    Map<Object, Object> map = new HashMap<>();
    for (int i = 0; i < singleMapBlock.getPositionCount(); i += 2) {
        map.put(keyType.getObjectValue(session, singleMapBlock, i), valueType.getObjectValue(session, singleMapBlock, i + 1));
    }

    return Collections.unmodifiableMap(map);
}
 
Example 8
Source File: HiveWriteUtils.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
public void setField(Block block, int position)
{
    Block mapBlock = block.getObject(position, Block.class);
    Map<Object, Object> map = new HashMap<>(mapBlock.getPositionCount() * 2);
    for (int i = 0; i < mapBlock.getPositionCount(); i += 2) {
        Object key = getField(keyType, mapBlock, i);
        Object value = getField(valueType, mapBlock, i + 1);
        map.put(key, value);
    }

    rowInspector.setStructFieldData(row, field, map);
}
 
Example 9
Source File: MapType.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
public boolean equalTo(Block leftBlock, int leftPosition, Block rightBlock, int rightPosition)
{
    Block leftMapBlock = leftBlock.getObject(leftPosition, Block.class);
    Block rightMapBlock = rightBlock.getObject(rightPosition, Block.class);

    if (leftMapBlock.getPositionCount() != rightMapBlock.getPositionCount()) {
        return false;
    }

    Map<KeyWrapper, Integer> wrappedLeftMap = new HashMap<>();
    for (int position = 0; position < leftMapBlock.getPositionCount(); position += 2) {
        wrappedLeftMap.put(new KeyWrapper(keyType, leftMapBlock, position), position + 1);
    }

    for (int position = 0; position < rightMapBlock.getPositionCount(); position += 2) {
        KeyWrapper key = new KeyWrapper(keyType, rightMapBlock, position);
        Integer leftValuePosition = wrappedLeftMap.get(key);
        if (leftValuePosition == null) {
            return false;
        }
        int rightValuePosition = position + 1;
        checkElementNotNull(leftMapBlock.isNull(leftValuePosition), MAP_NULL_ELEMENT_MSG);
        checkElementNotNull(rightMapBlock.isNull(rightValuePosition), MAP_NULL_ELEMENT_MSG);

        if (!valueType.equalTo(leftMapBlock, leftValuePosition, rightMapBlock, rightValuePosition)) {
            return false;
        }
    }
    return true;
}
 
Example 10
Source File: RowType.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
public long hash(Block block, int position)
{
    Block arrayBlock = block.getObject(position, Block.class);
    long result = 1;
    for (int i = 0; i < arrayBlock.getPositionCount(); i++) {
        Type elementType = fields.get(i).getType();
        result = 31 * result + TypeUtils.hashPosition(elementType, arrayBlock, i);
    }
    return result;
}
 
Example 11
Source File: ArrayType.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
public Object getObjectValue(ConnectorSession session, Block block, int position)
{
    if (block.isNull(position)) {
        return null;
    }

    if (block instanceof AbstractArrayBlock) {
        return ((AbstractArrayBlock) block).apply((valuesBlock, start, length) -> arrayBlockToObjectValues(session, valuesBlock, start, length), position);
    }
    else {
        Block arrayBlock = block.getObject(position, Block.class);
        return arrayBlockToObjectValues(session, arrayBlock, 0, arrayBlock.getPositionCount());
    }
}
 
Example 12
Source File: ArrayType.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
public int compareTo(Block leftBlock, int leftPosition, Block rightBlock, int rightPosition)
{
    if (!elementType.isOrderable()) {
        throw new UnsupportedOperationException(getTypeSignature() + " type is not orderable");
    }

    Block leftArray = leftBlock.getObject(leftPosition, Block.class);
    Block rightArray = rightBlock.getObject(rightPosition, Block.class);

    int len = Math.min(leftArray.getPositionCount(), rightArray.getPositionCount());
    int index = 0;
    while (index < len) {
        checkElementNotNull(leftArray.isNull(index), ARRAY_NULL_ELEMENT_MSG);
        checkElementNotNull(rightArray.isNull(index), ARRAY_NULL_ELEMENT_MSG);
        int comparison = elementType.compareTo(leftArray, index, rightArray, index);
        if (comparison != 0) {
            return comparison;
        }
        index++;
    }

    if (index == len) {
        return leftArray.getPositionCount() - rightArray.getPositionCount();
    }

    return 0;
}
 
Example 13
Source File: TestRowBlock.java    From presto with Apache License 2.0 5 votes vote down vote up
private void assertValue(Block rowBlock, int position, List<?> row)
{
    // null rows are handled by assertPositionValue
    requireNonNull(row, "row is null");

    assertFalse(rowBlock.isNull(position));
    SingleRowBlock singleRowBlock = (SingleRowBlock) rowBlock.getObject(position, Block.class);
    assertEquals(singleRowBlock.getPositionCount(), row.size());

    for (int i = 0; i < row.size(); i++) {
        Object fieldValue = row.get(i);
        if (fieldValue == null) {
            assertTrue(singleRowBlock.isNull(i));
        }
        else {
            if (fieldValue instanceof Long) {
                assertEquals(BIGINT.getLong(singleRowBlock, i), ((Long) fieldValue).longValue());
            }
            else if (fieldValue instanceof String) {
                assertEquals(VARCHAR.getSlice(singleRowBlock, i), utf8Slice((String) fieldValue));
            }
            else {
                throw new IllegalArgumentException();
            }
        }
    }
}
 
Example 14
Source File: HiveReaderProjectionsAdaptingRecordCursor.java    From presto with Apache License 2.0 5 votes vote down vote up
private Block applyDereferences(Block baseObject, List<Integer> dereferences, int length)
{
    checkArgument(length <= dereferences.size());
    Block current = baseObject;
    for (int i = 0; i < length; i++) {
        current = current.getObject(dereferences.get(i), Block.class);
    }
    return current;
}
 
Example 15
Source File: HiveReaderProjectionsAdaptingRecordCursor.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isNull(int field)
{
    int inputFieldIndex = channelMappings[field].getInputChannelIndex();
    List<Integer> dereferences = channelMappings[field].getDereferenceSequence();

    if (dereferences.isEmpty()) {
        return delegate.isNull(inputFieldIndex);
    }

    if (delegate.isNull(inputFieldIndex)) {
        return true;
    }

    // Get SingleRowBlock corresponding to the element at current position
    Block baseObject = (Block) delegate.getObject(inputFieldIndex);

    for (int j = 0; j < dereferences.size() - 1; j++) {
        int dereferenceIndex = dereferences.get(j);
        if (baseObject.isNull(dereferenceIndex)) {
            return true;
        }
        baseObject = baseObject.getObject(dereferenceIndex, Block.class);
    }

    int finalDereference = Iterables.getLast(dereferences);
    return baseObject.isNull(finalDereference);
}
 
Example 16
Source File: HiveWriteUtils.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
public void setField(Block block, int position)
{
    Block arrayBlock = block.getObject(position, Block.class);

    List<Object> list = new ArrayList<>(arrayBlock.getPositionCount());
    for (int i = 0; i < arrayBlock.getPositionCount(); i++) {
        Object element = getField(elementType, arrayBlock, i);
        list.add(element);
    }

    rowInspector.setStructFieldData(row, field, list);
}
 
Example 17
Source File: RowType.java    From presto with Apache License 2.0 4 votes vote down vote up
@Override
public Block getObject(Block block, int position)
{
    return block.getObject(position, Block.class);
}
 
Example 18
Source File: ArrayType.java    From presto with Apache License 2.0 4 votes vote down vote up
@Override
public Block getObject(Block block, int position)
{
    return block.getObject(position, Block.class);
}
 
Example 19
Source File: AvroDecoderTestUtil.java    From presto with Apache License 2.0 4 votes vote down vote up
public static void checkArrayValues(Block block, Type type, Object value)
{
    assertNotNull(type, "Type is null");
    assertTrue(type instanceof ArrayType, "Unexpected type");
    assertNotNull(block, "Block is null");
    assertNotNull(value, "Value is null");

    List<?> list = (List<?>) value;

    assertEquals(block.getPositionCount(), list.size());
    Type elementType = ((ArrayType) type).getElementType();
    if (elementType instanceof ArrayType) {
        for (int index = 0; index < block.getPositionCount(); index++) {
            if (block.isNull(index)) {
                assertNull(list.get(index));
                continue;
            }
            Block arrayBlock = block.getObject(index, Block.class);
            checkArrayValues(arrayBlock, elementType, list.get(index));
        }
    }
    else if (elementType instanceof MapType) {
        for (int index = 0; index < block.getPositionCount(); index++) {
            if (block.isNull(index)) {
                assertNull(list.get(index));
                continue;
            }
            Block mapBlock = block.getObject(index, Block.class);
            checkMapValues(mapBlock, elementType, list.get(index));
        }
    }
    else if (elementType instanceof RowType) {
        for (int index = 0; index < block.getPositionCount(); index++) {
            if (block.isNull(index)) {
                assertNull(list.get(index));
                continue;
            }
            Block rowBlock = block.getObject(index, Block.class);
            checkRowValues(rowBlock, elementType, list.get(index));
        }
    }
    else {
        for (int index = 0; index < block.getPositionCount(); index++) {
            checkPrimitiveValue(getObjectValue(elementType, block, index), list.get(index));
        }
    }
}
 
Example 20
Source File: TestReaderProjectionsAdapter.java    From presto with Apache License 2.0 4 votes vote down vote up
private static Block createProjectedColumnBlock(Block data, Type finalType, Type blockType, List<Integer> dereferences)
{
    if (dereferences.size() == 0) {
        return data;
    }

    BlockBuilder builder = finalType.createBlockBuilder(null, data.getPositionCount());

    for (int i = 0; i < data.getPositionCount(); i++) {
        Type sourceType = blockType;

        Block currentData = null;
        boolean isNull = data.isNull(i);

        if (!isNull) {
            // Get SingleRowBlock corresponding to element at position i
            currentData = data.getObject(i, Block.class);
        }

        // Apply all dereferences except for the last one, because the type can be different
        for (int j = 0; j < dereferences.size() - 1; j++) {
            if (isNull) {
                // If null element is discovered at any dereferencing step, break
                break;
            }

            checkArgument(sourceType instanceof RowType);
            if (currentData.isNull(dereferences.get(j))) {
                currentData = null;
            }
            else {
                sourceType = ((RowType) sourceType).getFields().get(dereferences.get(j)).getType();
                currentData = currentData.getObject(dereferences.get(j), Block.class);
            }

            isNull = isNull || (currentData == null);
        }

        if (isNull) {
            // Append null if any of the elements in the dereference chain were null
            builder.appendNull();
        }
        else {
            int lastDereference = dereferences.get(dereferences.size() - 1);

            if (currentData.isNull(lastDereference)) {
                // Append null if the last dereference is null
                builder.appendNull();
            }
            else {
                // Append actual values otherwise
                if (finalType.equals(BIGINT)) {
                    Long value = currentData.getLong(lastDereference, 0);
                    builder.writeLong(value);
                }
                else if (finalType instanceof RowType) {
                    Block block = currentData.getObject(lastDereference, Block.class);
                    builder.appendStructure(block);
                }
                else {
                    throw new UnsupportedOperationException();
                }
            }
        }
    }

    return builder.build();
}