Search in sources :

Example 31 with RowType

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

the class HiveTypes method toStructTypeInfo.

private static TypeInfo toStructTypeInfo(Type type) {
    if (type instanceof RowType) {
        RowType row = (RowType) type;
        List<RowType.Field> fields = row.getFields();
        List<String> fieldNames = new ArrayList<>(fields.size());
        List<TypeInfo> fieldTypes = new ArrayList<>(fields.size());
        for (int i = 0; i < fields.size(); i++) {
            RowType.Field field = fields.get(i);
            fieldNames.add(field.getName().orElse("col" + i));
            fieldTypes.add(toTypeInfo(field.getType()));
        }
        return getStructTypeInfo(fieldNames, fieldTypes);
    }
    throw unsupportedType(type);
}
Also used : ArrayList(java.util.ArrayList) RowType(com.facebook.presto.common.type.RowType) TypeInfoFactory.doubleTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.doubleTypeInfo) TypeInfoFactory.getCharTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getCharTypeInfo) TypeInfoFactory.getStructTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getStructTypeInfo) TypeInfoFactory.getVarcharTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getVarcharTypeInfo) TypeInfoFactory.floatTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.floatTypeInfo) TypeInfoFactory.getMapTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getMapTypeInfo) TypeInfoFactory.dateTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.dateTypeInfo) TypeInfoFactory.intTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.intTypeInfo) TypeInfoFactory.getDecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getDecimalTypeInfo) TypeInfoFactory.binaryTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.binaryTypeInfo) TypeInfoFactory.byteTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.byteTypeInfo) TypeInfoFactory.longTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.longTypeInfo) TypeInfoFactory.varcharTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.varcharTypeInfo) TypeInfoFactory.booleanTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.booleanTypeInfo) TypeInfoFactory.shortTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.shortTypeInfo) TypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfo) TypeInfoFactory.getListTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getListTypeInfo) TypeInfoFactory.timestampTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.timestampTypeInfo)

Example 32 with RowType

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

the class TestUnnesterUtil method buildExpectedPage.

static Page buildExpectedPage(Page page, List<Type> replicatedTypes, List<Type> unnestTypes, List<Type> outputTypes, int[] maxCardinalities, boolean withOrdinality, boolean legacyUnnest) {
    int totalEntries = IntStream.of(maxCardinalities).sum();
    int channelCount = page.getChannelCount();
    assertTrue(channelCount > 1);
    Block[] outputBlocks = new Block[outputTypes.size()];
    int outputChannel = 0;
    for (int i = 0; i < replicatedTypes.size(); i++) {
        outputBlocks[outputChannel++] = buildExpectedReplicatedBlock(page.getBlock(i), replicatedTypes.get(i), maxCardinalities, totalEntries);
    }
    for (int i = 0; i < unnestTypes.size(); i++) {
        Type type = unnestTypes.get(i);
        Block inputBlock = page.getBlock(replicatedTypes.size() + i);
        if (type instanceof ArrayType) {
            Type elementType = ((ArrayType) type).getElementType();
            if (elementType instanceof RowType && !legacyUnnest) {
                List<Type> rowTypes = ((RowType) elementType).getTypeParameters();
                Block[] blocks = buildExpectedUnnestedArrayOfRowBlock(inputBlock, rowTypes, maxCardinalities, totalEntries);
                for (Block block : blocks) {
                    outputBlocks[outputChannel++] = block;
                }
            } else {
                outputBlocks[outputChannel++] = buildExpectedUnnestedArrayBlock(inputBlock, ((ArrayType) unnestTypes.get(i)).getElementType(), maxCardinalities, totalEntries);
            }
        } else if (type instanceof MapType) {
            MapType mapType = (MapType) unnestTypes.get(i);
            Block[] blocks = buildExpectedUnnestedMapBlocks(inputBlock, mapType.getKeyType(), mapType.getValueType(), maxCardinalities, totalEntries);
            for (Block block : blocks) {
                outputBlocks[outputChannel++] = block;
            }
        } else {
            fail("expected an ArrayType or MapType, but found " + type);
        }
    }
    if (withOrdinality) {
        outputBlocks[outputChannel++] = buildExpectedOrdinalityBlock(maxCardinalities, totalEntries);
    }
    return new Page(outputBlocks);
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) MapType(com.facebook.presto.common.type.MapType) ArrayType(com.facebook.presto.common.type.ArrayType) Type(com.facebook.presto.common.type.Type) RowType(com.facebook.presto.common.type.RowType) Block(com.facebook.presto.common.block.Block) RowType(com.facebook.presto.common.type.RowType) Page(com.facebook.presto.common.Page) MapType(com.facebook.presto.common.type.MapType)

Example 33 with RowType

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

the class TestDirectEntryBlockBuilder method testNestedRow.

@Test
public void testNestedRow() {
    MapType mapType = new MapType(BIGINT, VARCHAR, MethodHandleUtil.methodHandle(TestDirectEntryBlockBuilder.class, "throwUnsupportedOperation"), MethodHandleUtil.methodHandle(TestDirectEntryBlockBuilder.class, "throwUnsupportedOperation"));
    ArrayType arrayType = new ArrayType(DOUBLE);
    RowType.Field nestedRowField = new RowType.Field(Optional.of("my_struct"), INTEGER);
    RowType nestedRowType = RowType.from(ImmutableList.of(nestedRowField));
    List<Type> rowType = ImmutableList.of(REAL, mapType, arrayType, nestedRowType);
    RowBlockBuilder beginEntryBlockBuilder = new RowBlockBuilder(rowType, null, 1);
    for (int i = 0; i < POSITION_COUNT; i++) {
        SingleRowBlockWriter singleRowBlockWriter = beginEntryBlockBuilder.beginBlockEntry();
        REAL.writeLong(singleRowBlockWriter, i);
        // Write Map<Bigint, Varchar> with 5 entries.
        BlockBuilder mapWriter = singleRowBlockWriter.beginBlockEntry();
        for (int j = 0; j < 5; j++) {
            BIGINT.writeLong(mapWriter, i + j);
            VARCHAR.writeSlice(mapWriter, utf8Slice("Value1" + j));
        }
        singleRowBlockWriter.closeEntry();
        // Write array .
        BlockBuilder arrayWriter = singleRowBlockWriter.beginBlockEntry();
        for (int j = 0; j < 8; j++) {
            DOUBLE.writeDouble(arrayWriter, i * 3 + j);
        }
        singleRowBlockWriter.closeEntry();
        // Write row type.
        BlockBuilder rowWriter = singleRowBlockWriter.beginBlockEntry();
        if (i % 2 == 0) {
            rowWriter.appendNull();
        } else {
            INTEGER.writeLong(rowWriter, i);
        }
        singleRowBlockWriter.closeEntry();
        beginEntryBlockBuilder.closeEntry();
        beginEntryBlockBuilder.appendNull();
    }
    RowBlockBuilder directEntryBlockBuilder = new RowBlockBuilder(rowType, null, 1);
    for (int i = 0; i < POSITION_COUNT; i++) {
        directEntryBlockBuilder.beginDirectEntry();
        REAL.writeLong(directEntryBlockBuilder.getBlockBuilder(0), i);
        // Write Map<Bigint, Varchar> with 5 entries.
        MapBlockBuilder mapBlockBuilder = (MapBlockBuilder) directEntryBlockBuilder.getBlockBuilder(1);
        mapBlockBuilder.beginDirectEntry();
        BlockBuilder keyBuilder = mapBlockBuilder.getKeyBlockBuilder();
        BlockBuilder valueBuilder = mapBlockBuilder.getValueBlockBuilder();
        for (int j = 0; j < 5; j++) {
            BIGINT.writeLong(keyBuilder, i + j);
            VARCHAR.writeSlice(valueBuilder, utf8Slice("Value1" + j));
        }
        mapBlockBuilder.closeEntry();
        // Write array .
        ArrayBlockBuilder arrayBuilder = (ArrayBlockBuilder) directEntryBlockBuilder.getBlockBuilder(2);
        arrayBuilder.beginDirectEntry();
        for (int j = 0; j < 8; j++) {
            DOUBLE.writeDouble(arrayBuilder.getElementBlockBuilder(), i * 3 + j);
        }
        arrayBuilder.closeEntry();
        // Write row type.
        RowBlockBuilder nestedRowBuilder = (RowBlockBuilder) directEntryBlockBuilder.getBlockBuilder(3);
        nestedRowBuilder.beginDirectEntry();
        BlockBuilder nestedRowValueBuilder = nestedRowBuilder.getBlockBuilder(0);
        if (i % 2 == 0) {
            nestedRowValueBuilder.appendNull();
        } else {
            INTEGER.writeLong(nestedRowValueBuilder, i);
        }
        nestedRowBuilder.closeEntry();
        directEntryBlockBuilder.closeEntry();
        directEntryBlockBuilder.appendNull();
    }
    Slice beginEntrySlice = getSlilce(beginEntryBlockBuilder);
    Slice directEntrySlice = getSlilce(directEntryBlockBuilder);
    assertEquals(beginEntrySlice.compareTo(directEntrySlice), 0);
}
Also used : RowBlockBuilder(com.facebook.presto.common.block.RowBlockBuilder) MapBlockBuilder(com.facebook.presto.common.block.MapBlockBuilder) RowType(com.facebook.presto.common.type.RowType) SingleRowBlockWriter(com.facebook.presto.common.block.SingleRowBlockWriter) MapType(com.facebook.presto.common.type.MapType) ArrayType(com.facebook.presto.common.type.ArrayType) MapType(com.facebook.presto.common.type.MapType) ArrayType(com.facebook.presto.common.type.ArrayType) Type(com.facebook.presto.common.type.Type) RowType(com.facebook.presto.common.type.RowType) ArrayBlockBuilder(com.facebook.presto.common.block.ArrayBlockBuilder) Slice(io.airlift.slice.Slice) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) MapBlockBuilder(com.facebook.presto.common.block.MapBlockBuilder) RowBlockBuilder(com.facebook.presto.common.block.RowBlockBuilder) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) ArrayBlockBuilder(com.facebook.presto.common.block.ArrayBlockBuilder) Test(org.testng.annotations.Test)

Example 34 with RowType

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

the class BlockAssertions method createRandomBlockForType.

// Note: Make sure positionCount is sufficiently large if nestedNullRate or primitiveNullRate is greater than 0
public static Block createRandomBlockForType(Type type, int positionCount, float primitiveNullRate, float nestedNullRate, boolean createView, List<Encoding> wrappings) {
    verifyNullRate(primitiveNullRate);
    verifyNullRate(nestedNullRate);
    Block block = null;
    if (createView) {
        positionCount *= 2;
    }
    if (type == BOOLEAN) {
        block = createRandomBooleansBlock(positionCount, primitiveNullRate);
    } else if (type == BIGINT) {
        block = createRandomLongsBlock(positionCount, primitiveNullRate);
    } else if (type == INTEGER || type == REAL) {
        block = createRandomIntsBlock(positionCount, primitiveNullRate);
    } else if (type == SMALLINT) {
        block = createRandomSmallintsBlock(positionCount, primitiveNullRate);
    } else if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        if (decimalType.isShort()) {
            block = createRandomLongsBlock(positionCount, primitiveNullRate);
        } else {
            block = createRandomLongDecimalsBlock(positionCount, primitiveNullRate);
        }
    } else if (type == VARCHAR) {
        block = createRandomStringBlock(positionCount, primitiveNullRate, MAX_STRING_SIZE);
    } 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(ENTRY_SIZE) + 1);
            }
        }
        // Build the nested block of size offsets[positionCount].
        if (type instanceof ArrayType) {
            Block valuesBlock = createRandomBlockForType(((ArrayType) type).getElementType(), offsets[positionCount], primitiveNullRate, nestedNullRate, createView, wrappings);
            block = fromElementBlock(positionCount, Optional.ofNullable(isNull), offsets, valuesBlock);
        } else if (type instanceof MapType) {
            MapType mapType = (MapType) type;
            Block keyBlock = createRandomBlockForType(mapType.getKeyType(), offsets[positionCount], 0.0f, 0.0f, createView, wrappings);
            Block valueBlock = createRandomBlockForType(mapType.getValueType(), offsets[positionCount], primitiveNullRate, nestedNullRate, createView, wrappings);
            block = mapType.createBlockFromKeyValue(positionCount, Optional.ofNullable(isNull), offsets, keyBlock, valueBlock);
        } else if (type instanceof RowType) {
            List<Type> fieldTypes = type.getTypeParameters();
            Block[] fieldBlocks = new Block[fieldTypes.size()];
            for (int i = 0; i < fieldBlocks.length; i++) {
                fieldBlocks[i] = createRandomBlockForType(fieldTypes.get(i), positionCount, primitiveNullRate, nestedNullRate, createView, wrappings);
            }
            block = fromFieldBlocks(positionCount, Optional.ofNullable(isNull), fieldBlocks);
        } else {
            throw new IllegalArgumentException(format("type %s is not supported.", type));
        }
    }
    if (createView) {
        positionCount /= 2;
        int offset = positionCount / 2;
        block = block.getRegion(offset, positionCount);
    }
    if (!wrappings.isEmpty()) {
        block = wrapBlock(block, positionCount, wrappings);
    }
    return block;
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) RunLengthEncodedBlock(com.facebook.presto.common.block.RunLengthEncodedBlock) ArrayBlock.fromElementBlock(com.facebook.presto.common.block.ArrayBlock.fromElementBlock) DictionaryBlock(com.facebook.presto.common.block.DictionaryBlock) Block(com.facebook.presto.common.block.Block) DecimalType(com.facebook.presto.common.type.DecimalType) RowType(com.facebook.presto.common.type.RowType) List(java.util.List) ArrayList(java.util.ArrayList) MapType(com.facebook.presto.common.type.MapType)

Example 35 with RowType

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

the class TestHistogram method testRowHistograms.

@Test
public void testRowHistograms() {
    RowType innerRowType = RowType.from(ImmutableList.of(RowType.field("f1", BIGINT), RowType.field("f2", DOUBLE)));
    InternalAggregationFunction aggregationFunction = getAggregation(innerRowType);
    BlockBuilder builder = innerRowType.createBlockBuilder(null, 3);
    innerRowType.writeObject(builder, toRow(ImmutableList.of(BIGINT, DOUBLE), 1L, 1.0));
    innerRowType.writeObject(builder, toRow(ImmutableList.of(BIGINT, DOUBLE), 2L, 2.0));
    innerRowType.writeObject(builder, toRow(ImmutableList.of(BIGINT, DOUBLE), 3L, 3.0));
    assertAggregation(aggregationFunction, ImmutableMap.of(ImmutableList.of(1L, 1.0), 1L, ImmutableList.of(2L, 2.0), 1L, ImmutableList.of(3L, 3.0), 1L), builder.build());
}
Also used : RowType(com.facebook.presto.common.type.RowType) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) 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