Search in sources :

Example 6 with BlockBuilder

use of io.prestosql.spi.block.BlockBuilder in project hetu-core by openlookeng.

the class OrcTester method writeValue.

private static void writeValue(Type type, BlockBuilder blockBuilder, Object value) {
    if (value == null) {
        blockBuilder.appendNull();
    } else {
        if (BOOLEAN.equals(type)) {
            type.writeBoolean(blockBuilder, (Boolean) value);
        } else if (TINYINT.equals(type) || SMALLINT.equals(type) || INTEGER.equals(type) || BIGINT.equals(type)) {
            type.writeLong(blockBuilder, ((Number) value).longValue());
        } else if (Decimals.isShortDecimal(type)) {
            type.writeLong(blockBuilder, ((SqlDecimal) value).toBigDecimal().unscaledValue().longValue());
        } else if (Decimals.isLongDecimal(type)) {
            type.writeSlice(blockBuilder, Decimals.encodeUnscaledValue(((SqlDecimal) value).toBigDecimal().unscaledValue()));
        } else if (DOUBLE.equals(type)) {
            type.writeDouble(blockBuilder, ((Number) value).doubleValue());
        } else if (REAL.equals(type)) {
            float floatValue = ((Number) value).floatValue();
            type.writeLong(blockBuilder, Float.floatToIntBits(floatValue));
        } else if (type instanceof VarcharType) {
            Slice slice = truncateToLength(utf8Slice((String) value), type);
            type.writeSlice(blockBuilder, slice);
        } else if (type instanceof CharType) {
            Slice slice = truncateToLengthAndTrimSpaces(utf8Slice((String) value), type);
            type.writeSlice(blockBuilder, slice);
        } else if (VARBINARY.equals(type)) {
            type.writeSlice(blockBuilder, Slices.wrappedBuffer(((SqlVarbinary) value).getBytes()));
        } else if (DATE.equals(type)) {
            long days = ((SqlDate) value).getDays();
            type.writeLong(blockBuilder, days);
        } else if (TIMESTAMP.equals(type)) {
            long millis = ((SqlTimestamp) value).getMillis();
            type.writeLong(blockBuilder, millis);
        } else {
            String baseType = type.getTypeSignature().getBase();
            if (StandardTypes.ARRAY.equals(baseType)) {
                List<?> array = (List<?>) value;
                Type elementType = type.getTypeParameters().get(0);
                BlockBuilder arrayBlockBuilder = blockBuilder.beginBlockEntry();
                for (Object elementValue : array) {
                    writeValue(elementType, arrayBlockBuilder, elementValue);
                }
                blockBuilder.closeEntry();
            } else if (StandardTypes.MAP.equals(baseType)) {
                Map<?, ?> map = (Map<?, ?>) value;
                Type keyType = type.getTypeParameters().get(0);
                Type valueType = type.getTypeParameters().get(1);
                BlockBuilder mapBlockBuilder = blockBuilder.beginBlockEntry();
                for (Entry<?, ?> entry : map.entrySet()) {
                    writeValue(keyType, mapBlockBuilder, entry.getKey());
                    writeValue(valueType, mapBlockBuilder, entry.getValue());
                }
                blockBuilder.closeEntry();
            } else if (StandardTypes.ROW.equals(baseType)) {
                List<?> array = (List<?>) value;
                List<Type> fieldTypes = type.getTypeParameters();
                BlockBuilder rowBlockBuilder = blockBuilder.beginBlockEntry();
                for (int fieldId = 0; fieldId < fieldTypes.size(); fieldId++) {
                    Type fieldType = fieldTypes.get(fieldId);
                    writeValue(fieldType, rowBlockBuilder, array.get(fieldId));
                }
                blockBuilder.closeEntry();
            } else {
                throw new IllegalArgumentException("Unsupported type " + type);
            }
        }
    }
}
Also used : VarcharType(io.prestosql.spi.type.VarcharType) SqlVarbinary(io.prestosql.spi.type.SqlVarbinary) SqlDecimal(io.prestosql.spi.type.SqlDecimal) SqlTimestamp(io.prestosql.spi.type.SqlTimestamp) VarbinaryType(io.prestosql.spi.type.VarbinaryType) CharType(io.prestosql.spi.type.CharType) VarcharType(io.prestosql.spi.type.VarcharType) DecimalType(io.prestosql.spi.type.DecimalType) Type(io.prestosql.spi.type.Type) Entry(java.util.Map.Entry) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) Slice(io.airlift.slice.Slice) Arrays.asList(java.util.Arrays.asList) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Collectors.toList(java.util.stream.Collectors.toList) CharType(io.prestosql.spi.type.CharType) Map(java.util.Map) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) BlockBuilder(io.prestosql.spi.block.BlockBuilder)

Example 7 with BlockBuilder

use of io.prestosql.spi.block.BlockBuilder in project hetu-core by openlookeng.

the class TestJsonType method createTestBlock.

public static Block createTestBlock() {
    BlockBuilder blockBuilder = JSON.createBlockBuilder(null, 1);
    Slice slice = Slices.utf8Slice("{\"x\":1, \"y\":2}");
    JSON.writeSlice(blockBuilder, slice);
    return blockBuilder.build();
}
Also used : Slice(io.airlift.slice.Slice) BlockBuilder(io.prestosql.spi.block.BlockBuilder)

Example 8 with BlockBuilder

use of io.prestosql.spi.block.BlockBuilder in project hetu-core by openlookeng.

the class TestLongDecimalType method createTestBlock.

public static Block createTestBlock() {
    BlockBuilder blockBuilder = LONG_DECIMAL_TYPE.createBlockBuilder(null, 15);
    writeBigDecimal(LONG_DECIMAL_TYPE, blockBuilder, new BigDecimal("-12345678901234567890.1234567890"));
    writeBigDecimal(LONG_DECIMAL_TYPE, blockBuilder, new BigDecimal("-12345678901234567890.1234567890"));
    writeBigDecimal(LONG_DECIMAL_TYPE, blockBuilder, new BigDecimal("-12345678901234567890.1234567890"));
    writeBigDecimal(LONG_DECIMAL_TYPE, blockBuilder, new BigDecimal("22345678901234567890.1234567890"));
    writeBigDecimal(LONG_DECIMAL_TYPE, blockBuilder, new BigDecimal("22345678901234567890.1234567890"));
    writeBigDecimal(LONG_DECIMAL_TYPE, blockBuilder, new BigDecimal("22345678901234567890.1234567890"));
    writeBigDecimal(LONG_DECIMAL_TYPE, blockBuilder, new BigDecimal("22345678901234567890.1234567890"));
    writeBigDecimal(LONG_DECIMAL_TYPE, blockBuilder, new BigDecimal("22345678901234567890.1234567890"));
    writeBigDecimal(LONG_DECIMAL_TYPE, blockBuilder, new BigDecimal("32345678901234567890.1234567890"));
    writeBigDecimal(LONG_DECIMAL_TYPE, blockBuilder, new BigDecimal("32345678901234567890.1234567890"));
    writeBigDecimal(LONG_DECIMAL_TYPE, blockBuilder, new BigDecimal("42345678901234567890.1234567890"));
    return blockBuilder.build();
}
Also used : BigDecimal(java.math.BigDecimal) Decimals.writeBigDecimal(io.prestosql.spi.type.Decimals.writeBigDecimal) BlockBuilder(io.prestosql.spi.block.BlockBuilder)

Example 9 with BlockBuilder

use of io.prestosql.spi.block.BlockBuilder in project hetu-core by openlookeng.

the class TestMapOperators method assertMapHashOperator.

private void assertMapHashOperator(String inputString, Type keyType, Type valueType, List<Object> elements) {
    checkArgument(elements.size() % 2 == 0, "the size of elements should be even number");
    MapType mapType = mapType(keyType, valueType);
    BlockBuilder mapArrayBuilder = mapType.createBlockBuilder(null, 1);
    BlockBuilder singleMapWriter = mapArrayBuilder.beginBlockEntry();
    for (int i = 0; i < elements.size(); i += 2) {
        appendToBlockBuilder(keyType, elements.get(i), singleMapWriter);
        appendToBlockBuilder(valueType, elements.get(i + 1), singleMapWriter);
    }
    mapArrayBuilder.closeEntry();
    long hashResult = mapType.hash(mapArrayBuilder.build(), 0);
    assertOperator(HASH_CODE, inputString, BIGINT, hashResult);
}
Also used : MapType(io.prestosql.spi.type.MapType) BlockBuilder(io.prestosql.spi.block.BlockBuilder) StructuralTestUtil.appendToBlockBuilder(io.prestosql.util.StructuralTestUtil.appendToBlockBuilder)

Example 10 with BlockBuilder

use of io.prestosql.spi.block.BlockBuilder in project hetu-core by openlookeng.

the class TestRowOperators method assertRowHashOperator.

private void assertRowHashOperator(String inputString, List<Type> types, List<Object> elements) {
    checkArgument(types.size() == elements.size(), "types and elements must have the same size");
    RowType rowType = RowType.anonymous(types);
    BlockBuilder blockBuilder = rowType.createBlockBuilder(null, 1);
    BlockBuilder singleRowBlockWriter = blockBuilder.beginBlockEntry();
    for (int i = 0; i < types.size(); i++) {
        appendToBlockBuilder(types.get(i), elements.get(i), singleRowBlockWriter);
    }
    blockBuilder.closeEntry();
    assertOperator(HASH_CODE, inputString, BIGINT, rowType.hash(blockBuilder.build(), 0));
}
Also used : RowType(io.prestosql.spi.type.RowType) BlockBuilder(io.prestosql.spi.block.BlockBuilder) StructuralTestUtil.appendToBlockBuilder(io.prestosql.util.StructuralTestUtil.appendToBlockBuilder)

Aggregations

BlockBuilder (io.prestosql.spi.block.BlockBuilder)440 Block (io.prestosql.spi.block.Block)100 Test (org.testng.annotations.Test)84 Slice (io.airlift.slice.Slice)80 Type (io.prestosql.spi.type.Type)78 Page (io.prestosql.spi.Page)52 SqlType (io.prestosql.spi.function.SqlType)42 ArrayType (io.prestosql.spi.type.ArrayType)38 Map (java.util.Map)36 RowType (io.prestosql.spi.type.RowType)34 MapType (io.prestosql.spi.type.MapType)31 List (java.util.List)26 PageBuilder (io.prestosql.spi.PageBuilder)24 TimestampType (io.prestosql.spi.type.TimestampType)24 ImmutableList (com.google.common.collect.ImmutableList)23 PrestoException (io.prestosql.spi.PrestoException)23 BigintType (io.prestosql.spi.type.BigintType)23 BooleanType (io.prestosql.spi.type.BooleanType)21 DoubleType (io.prestosql.spi.type.DoubleType)21 UsedByGeneratedCode (io.prestosql.spi.annotation.UsedByGeneratedCode)20