Search in sources :

Example 36 with RowType

use of com.facebook.presto.common.type.RowType in project presto by prestodb.

the class TestBlockEncodingBuffers method buildBlockStatusWithType.

private BlockStatus buildBlockStatusWithType(Type type, int positionCount, boolean isView, float primitiveNullRate, float nestedNullRate, List<Encoding> wrappings) {
    BlockStatus blockStatus = null;
    if (isView) {
        positionCount *= 2;
    }
    if (type == BIGINT) {
        blockStatus = buildBigintBlockStatus(positionCount, primitiveNullRate);
    } else if (type instanceof DecimalType) {
        if (!((DecimalType) type).isShort()) {
            blockStatus = buildLongDecimalBlockStatus(positionCount, primitiveNullRate);
        } else {
            blockStatus = buildShortDecimalBlockStatus(positionCount, primitiveNullRate);
        }
    } else if (type == INTEGER) {
        blockStatus = buildIntegerBlockStatus(positionCount, primitiveNullRate);
    } else if (type == SMALLINT) {
        blockStatus = buildSmallintBlockStatus(positionCount, primitiveNullRate);
    } else if (type == BOOLEAN) {
        blockStatus = buildBooleanBlockStatus(positionCount, primitiveNullRate);
    } else if (type == VARCHAR) {
        blockStatus = buildVarcharBlockStatus(positionCount, primitiveNullRate, 10);
    } else {
        // Nested types
        // Build isNull and offsets of size positionCount
        boolean[] isNull = null;
        if (nestedNullRate > 0) {
            isNull = new boolean[positionCount];
        }
        int[] offsets = new int[positionCount + 1];
        for (int position = 0; position < positionCount; position++) {
            if (nestedNullRate > 0 && ThreadLocalRandom.current().nextDouble(1) < nestedNullRate) {
                isNull[position] = true;
                offsets[position + 1] = offsets[position];
            } else {
                offsets[position + 1] = offsets[position] + (type instanceof RowType ? 1 : ThreadLocalRandom.current().nextInt(10) + 1);
            }
        }
        // Build the nested block of size offsets[positionCount].
        if (type instanceof ArrayType) {
            blockStatus = buildArrayBlockStatus((ArrayType) type, positionCount, isView, Optional.ofNullable(isNull), offsets, primitiveNullRate, nestedNullRate, wrappings);
        } else if (type instanceof MapType) {
            blockStatus = buildMapBlockStatus((MapType) type, positionCount, isView, Optional.ofNullable(isNull), offsets, primitiveNullRate, nestedNullRate, wrappings);
        } else if (type instanceof RowType) {
            blockStatus = buildRowBlockStatus((RowType) type, positionCount, isView, Optional.ofNullable(isNull), offsets, primitiveNullRate, nestedNullRate, wrappings);
        } else {
            throw new UnsupportedOperationException(format("type %s is not supported.", type));
        }
    }
    if (isView) {
        positionCount /= 2;
        int offset = positionCount / 2;
        Block blockView = blockStatus.block.getRegion(offset, positionCount);
        int[] expectedRowSizesView = Arrays.stream(blockStatus.expectedRowSizes, offset, offset + positionCount).toArray();
        blockStatus = new BlockStatus(blockView, expectedRowSizesView);
    }
    blockStatus = buildDictRleBlockStatus(blockStatus, positionCount, wrappings);
    return blockStatus;
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) DecimalType(com.facebook.presto.common.type.DecimalType) DecimalType.createDecimalType(com.facebook.presto.common.type.DecimalType.createDecimalType) RowType(com.facebook.presto.common.type.RowType) MapBlock.fromKeyValueBlock(com.facebook.presto.common.block.MapBlock.fromKeyValueBlock) BlockAssertions.createRandomLongsBlock(com.facebook.presto.block.BlockAssertions.createRandomLongsBlock) OptimizedPartitionedOutputOperator.decodeBlock(com.facebook.presto.operator.repartition.OptimizedPartitionedOutputOperator.decodeBlock) BlockAssertions.createRandomDictionaryBlock(com.facebook.presto.block.BlockAssertions.createRandomDictionaryBlock) BlockAssertions.createRandomSmallintsBlock(com.facebook.presto.block.BlockAssertions.createRandomSmallintsBlock) ArrayBlock.fromElementBlock(com.facebook.presto.common.block.ArrayBlock.fromElementBlock) BlockAssertions.createAllNullsBlock(com.facebook.presto.block.BlockAssertions.createAllNullsBlock) BlockAssertions.createRandomBooleansBlock(com.facebook.presto.block.BlockAssertions.createRandomBooleansBlock) BlockAssertions.createRandomStringBlock(com.facebook.presto.block.BlockAssertions.createRandomStringBlock) BlockAssertions.createRLEBlock(com.facebook.presto.block.BlockAssertions.createRLEBlock) BlockAssertions.createRandomShortDecimalsBlock(com.facebook.presto.block.BlockAssertions.createRandomShortDecimalsBlock) BlockAssertions.createStringsBlock(com.facebook.presto.block.BlockAssertions.createStringsBlock) BlockAssertions.wrapBlock(com.facebook.presto.block.BlockAssertions.wrapBlock) BlockSerdeUtil.readBlock(com.facebook.presto.common.block.BlockSerdeUtil.readBlock) DictionaryBlock(com.facebook.presto.common.block.DictionaryBlock) BlockAssertions.createRandomIntsBlock(com.facebook.presto.block.BlockAssertions.createRandomIntsBlock) Block(com.facebook.presto.common.block.Block) BlockAssertions.createRandomLongDecimalsBlock(com.facebook.presto.block.BlockAssertions.createRandomLongDecimalsBlock) MapType(com.facebook.presto.common.type.MapType) BlockAssertions.createMapType(com.facebook.presto.block.BlockAssertions.createMapType)

Example 37 with RowType

use of com.facebook.presto.common.type.RowType in project presto by prestodb.

the class ParquetTestUtils method getHiveType.

private static String getHiveType(Type type) {
    if (type.equals(BOOLEAN) || type.equals(BIGINT) || type.equals(SmallintType.SMALLINT) || type.equals(TinyintType.TINYINT) || type.equals(DOUBLE)) {
        return type.getTypeSignature().toString();
    }
    if (type.equals(INTEGER)) {
        return "int";
    }
    if (type.equals(REAL)) {
        return "float";
    }
    if (type.equals(TIMESTAMP)) {
        return "timestamp";
    }
    if (type instanceof VarcharType) {
        VarcharType varcharType = (VarcharType) type;
        int varcharLength = varcharType.getLength();
        return "varchar(" + varcharLength + ")";
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        return format("decimal(%d,%d)", decimalType.getPrecision(), decimalType.getScale());
    }
    if (type instanceof ArrayType) {
        return "array<" + getHiveType(((ArrayType) type).getElementType()) + ">";
    }
    if (type instanceof RowType) {
        return "struct<" + Joiner.on(",").join(((RowType) type).getFields().stream().map(t -> t.getName().get() + ":" + getHiveType(t.getType())).collect(toList())) + ">";
    }
    throw new IllegalArgumentException("unsupported type: " + type);
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) VarcharType(com.facebook.presto.common.type.VarcharType) DecimalType(com.facebook.presto.common.type.DecimalType) RowType(com.facebook.presto.common.type.RowType)

Example 38 with RowType

use of com.facebook.presto.common.type.RowType in project presto by prestodb.

the class ParquetTestUtils method getObjectInspector.

private static ObjectInspector getObjectInspector(Type type) {
    if (type.equals(BOOLEAN)) {
        return writableBooleanObjectInspector;
    }
    if (type.equals(BIGINT)) {
        return writableLongObjectInspector;
    }
    if (type.equals(INTEGER)) {
        return writableIntObjectInspector;
    }
    if (type.equals(SmallintType.SMALLINT)) {
        return writableShortObjectInspector;
    }
    if (type.equals(TinyintType.TINYINT)) {
        return writableByteObjectInspector;
    }
    if (type.equals(DOUBLE)) {
        return writableDoubleObjectInspector;
    }
    if (type.equals(REAL)) {
        return writableFloatObjectInspector;
    }
    if (type.equals(TIMESTAMP)) {
        return writableTimestampObjectInspector;
    }
    if (type instanceof VarcharType) {
        VarcharType varcharType = (VarcharType) type;
        int varcharLength = varcharType.getLength();
        return getPrimitiveWritableObjectInspector(getVarcharTypeInfo(varcharLength));
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        return getPrimitiveWritableObjectInspector(new DecimalTypeInfo(decimalType.getPrecision(), decimalType.getScale()));
    }
    if (type instanceof ArrayType || type instanceof RowType) {
        return getJavaObjectInspector(type);
    }
    throw new IllegalArgumentException("unsupported type: " + type);
}
Also used : DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) ArrayType(com.facebook.presto.common.type.ArrayType) VarcharType(com.facebook.presto.common.type.VarcharType) DecimalType(com.facebook.presto.common.type.DecimalType) RowType(com.facebook.presto.common.type.RowType)

Example 39 with RowType

use of com.facebook.presto.common.type.RowType in project presto by prestodb.

the class TestTypeConversions method testConvertOneLevelRecordField.

@Test
public void testConvertOneLevelRecordField() {
    Field field = Field.of("rec", RECORD, Field.of("sub_s", STRING), Field.of("sub_i", INTEGER));
    ColumnMetadata metadata = Conversions.toColumnMetadata(field);
    RowType targetType = RowType.from(ImmutableList.of(RowType.field("sub_s", VarcharType.VARCHAR), RowType.field("sub_i", BigintType.BIGINT)));
    assertThat(metadata.getType()).isEqualTo(targetType);
}
Also used : Field(com.google.cloud.bigquery.Field) ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) RowType(com.facebook.presto.common.type.RowType) Test(org.testng.annotations.Test)

Example 40 with RowType

use of com.facebook.presto.common.type.RowType in project presto by prestodb.

the class TestTypeConversions method testConvertTwoLevelsRecordColumn.

@Test
public void testConvertTwoLevelsRecordColumn() {
    BigQueryColumnHandle column = new BigQueryColumnHandle("rec", BigQueryType.RECORD, NULLABLE, ImmutableList.of(new BigQueryColumnHandle("sub_rec", BigQueryType.RECORD, NULLABLE, ImmutableList.of(new BigQueryColumnHandle("sub_sub_s", BigQueryType.STRING, NULLABLE, ImmutableList.of(), null), new BigQueryColumnHandle("sub_sub_i", BigQueryType.INTEGER, NULLABLE, ImmutableList.of(), null)), null), new BigQueryColumnHandle("sub_s", BigQueryType.STRING, NULLABLE, ImmutableList.of(), null), new BigQueryColumnHandle("sub_i", BigQueryType.INTEGER, NULLABLE, ImmutableList.of(), null)), null);
    ColumnMetadata metadata = column.getColumnMetadata();
    RowType targetType = RowType.from(ImmutableList.of(RowType.field("sub_rec", RowType.from(ImmutableList.of(RowType.field("sub_sub_s", VarcharType.VARCHAR), RowType.field("sub_sub_i", BigintType.BIGINT)))), RowType.field("sub_s", VarcharType.VARCHAR), RowType.field("sub_i", BigintType.BIGINT)));
    assertThat(metadata.getType()).isEqualTo(targetType);
}
Also used : ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) RowType(com.facebook.presto.common.type.RowType) Test(org.testng.annotations.Test)

Aggregations

RowType (com.facebook.presto.common.type.RowType)61 ArrayType (com.facebook.presto.common.type.ArrayType)37 Type (com.facebook.presto.common.type.Type)32 MapType (com.facebook.presto.common.type.MapType)28 ImmutableList (com.google.common.collect.ImmutableList)19 ArrayList (java.util.ArrayList)18 DecimalType (com.facebook.presto.common.type.DecimalType)16 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)15 Test (org.testng.annotations.Test)15 List (java.util.List)14 VarcharType (com.facebook.presto.common.type.VarcharType)12 Block (com.facebook.presto.common.block.Block)11 CharType (com.facebook.presto.common.type.CharType)9 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)9 PrestoException (com.facebook.presto.spi.PrestoException)8 Map (java.util.Map)8 ImmutableMap (com.google.common.collect.ImmutableMap)7 VarcharType.createUnboundedVarcharType (com.facebook.presto.common.type.VarcharType.createUnboundedVarcharType)6 ColumnMetadata (com.facebook.presto.spi.ColumnMetadata)6 TimestampType (com.facebook.presto.common.type.TimestampType)5