Search in sources :

Example 16 with RowType

use of io.prestosql.spi.type.RowType in project hetu-core by openlookeng.

the class MapEntriesFunction method mapFromEntries.

@TypeParameter("K")
@TypeParameter("V")
@SqlType("array(row(K,V))")
public Block mapFromEntries(@TypeParameter("row(K,V)") RowType rowType, @SqlType("map(K,V)") Block block) {
    verify(rowType.getTypeParameters().size() == 2);
    verify(block.getPositionCount() % 2 == 0);
    Type keyType = rowType.getTypeParameters().get(0);
    Type valueType = rowType.getTypeParameters().get(1);
    ArrayType arrayType = new ArrayType(rowType);
    if (pageBuilder.isFull()) {
        pageBuilder.reset();
    }
    int entryCount = block.getPositionCount() / 2;
    BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(0);
    BlockBuilder entryBuilder = blockBuilder.beginBlockEntry();
    for (int i = 0; i < entryCount; i++) {
        BlockBuilder rowBuilder = entryBuilder.beginBlockEntry();
        keyType.appendTo(block, 2 * i, rowBuilder);
        valueType.appendTo(block, 2 * i + 1, rowBuilder);
        entryBuilder.closeEntry();
    }
    blockBuilder.closeEntry();
    pageBuilder.declarePosition();
    return arrayType.getObject(blockBuilder, blockBuilder.getPositionCount() - 1);
}
Also used : ArrayType(io.prestosql.spi.type.ArrayType) SqlType(io.prestosql.spi.function.SqlType) ArrayType(io.prestosql.spi.type.ArrayType) RowType(io.prestosql.spi.type.RowType) Type(io.prestosql.spi.type.Type) BlockBuilder(io.prestosql.spi.block.BlockBuilder) TypeParameter(io.prestosql.spi.function.TypeParameter) SqlType(io.prestosql.spi.function.SqlType)

Example 17 with RowType

use of io.prestosql.spi.type.RowType in project hetu-core by openlookeng.

the class RowEqualOperator method equals.

public static Boolean equals(RowType rowType, List<MethodHandle> fieldEqualOperators, Block leftRow, Block rightRow) {
    boolean indeterminate = false;
    for (int fieldIndex = 0; fieldIndex < leftRow.getPositionCount(); fieldIndex++) {
        if (leftRow.isNull(fieldIndex) || rightRow.isNull(fieldIndex)) {
            indeterminate = true;
            continue;
        }
        Type fieldType = rowType.getTypeParameters().get(fieldIndex);
        Object leftField = readNativeValue(fieldType, leftRow, fieldIndex);
        Object rightField = readNativeValue(fieldType, rightRow, fieldIndex);
        try {
            MethodHandle equalOperator = fieldEqualOperators.get(fieldIndex);
            Boolean result = (Boolean) equalOperator.invoke(leftField, rightField);
            if (result == null) {
                indeterminate = true;
            } else if (!result) {
                return false;
            }
        } catch (Throwable t) {
            throw internalError(t);
        }
    }
    if (indeterminate) {
        return null;
    }
    return true;
}
Also used : RowType(io.prestosql.spi.type.RowType) Type(io.prestosql.spi.type.Type) MethodHandle(java.lang.invoke.MethodHandle)

Example 18 with RowType

use of io.prestosql.spi.type.RowType in project hetu-core by openlookeng.

the class MultimapFromEntriesFunction method multimapFromEntries.

@TypeParameter("K")
@TypeParameter("V")
@SqlType("map(K,array(V))")
@SqlNullable
public Block multimapFromEntries(@TypeParameter("map(K,array(V))") MapType mapType, @SqlType("array(row(K,V))") Block block) {
    Type keyType = mapType.getKeyType();
    Type valueType = ((ArrayType) mapType.getValueType()).getElementType();
    RowType rowType = RowType.anonymous(ImmutableList.of(keyType, valueType));
    if (pageBuilder.isFull()) {
        pageBuilder.reset();
    }
    int entryCount = block.getPositionCount();
    if (entryCount > entryIndicesList.length) {
        initializeEntryIndicesList(entryCount);
    }
    TypedSet keySet = new TypedSet(keyType, entryCount, NAME);
    for (int i = 0; i < entryCount; i++) {
        if (block.isNull(i)) {
            clearEntryIndices(keySet.size());
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "map entry cannot be null");
        }
        Block rowBlock = rowType.getObject(block, i);
        if (rowBlock.isNull(0)) {
            clearEntryIndices(keySet.size());
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "map key cannot be null");
        }
        if (keySet.contains(rowBlock, 0)) {
            entryIndicesList[keySet.positionOf(rowBlock, 0)].add(i);
        } else {
            keySet.add(rowBlock, 0);
            entryIndicesList[keySet.size() - 1].add(i);
        }
    }
    BlockBuilder multimapBlockBuilder = pageBuilder.getBlockBuilder(0);
    BlockBuilder singleMapWriter = multimapBlockBuilder.beginBlockEntry();
    for (int i = 0; i < keySet.size(); i++) {
        keyType.appendTo(rowType.getObject(block, entryIndicesList[i].getInt(0)), 0, singleMapWriter);
        BlockBuilder singleArrayWriter = singleMapWriter.beginBlockEntry();
        for (int entryIndex : entryIndicesList[i]) {
            valueType.appendTo(rowType.getObject(block, entryIndex), 1, singleArrayWriter);
        }
        singleMapWriter.closeEntry();
    }
    multimapBlockBuilder.closeEntry();
    pageBuilder.declarePosition();
    clearEntryIndices(keySet.size());
    return mapType.getObject(multimapBlockBuilder, multimapBlockBuilder.getPositionCount() - 1);
}
Also used : ArrayType(io.prestosql.spi.type.ArrayType) ArrayType(io.prestosql.spi.type.ArrayType) MapType(io.prestosql.spi.type.MapType) SqlType(io.prestosql.spi.function.SqlType) RowType(io.prestosql.spi.type.RowType) Type(io.prestosql.spi.type.Type) RowType(io.prestosql.spi.type.RowType) TypedSet(io.prestosql.operator.aggregation.TypedSet) Block(io.prestosql.spi.block.Block) PrestoException(io.prestosql.spi.PrestoException) BlockBuilder(io.prestosql.spi.block.BlockBuilder) SqlNullable(io.prestosql.spi.function.SqlNullable) TypeParameter(io.prestosql.spi.function.TypeParameter) SqlType(io.prestosql.spi.function.SqlType)

Example 19 with RowType

use of io.prestosql.spi.type.RowType in project hetu-core by openlookeng.

the class MapFromEntriesFunction method mapFromEntries.

@TypeParameter("K")
@TypeParameter("V")
@SqlType("map(K,V)")
@SqlNullable
public Block mapFromEntries(@TypeParameter("map(K,V)") MapType mapType, ConnectorSession session, @SqlType("array(row(K,V))") Block block) {
    Type keyType = mapType.getKeyType();
    Type valueType = mapType.getValueType();
    RowType rowType = RowType.anonymous(ImmutableList.of(keyType, valueType));
    if (pageBuilder.isFull()) {
        pageBuilder.reset();
    }
    int entryCount = block.getPositionCount();
    BlockBuilder mapBlockBuilder = pageBuilder.getBlockBuilder(0);
    BlockBuilder resultBuilder = mapBlockBuilder.beginBlockEntry();
    TypedSet uniqueKeys = new TypedSet(keyType, entryCount, "map_from_entries");
    for (int i = 0; i < entryCount; i++) {
        if (block.isNull(i)) {
            mapBlockBuilder.closeEntry();
            pageBuilder.declarePosition();
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "map entry cannot be null");
        }
        Block rowBlock = rowType.getObject(block, i);
        if (rowBlock.isNull(0)) {
            mapBlockBuilder.closeEntry();
            pageBuilder.declarePosition();
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "map key cannot be null");
        }
        if (uniqueKeys.contains(rowBlock, 0)) {
            mapBlockBuilder.closeEntry();
            pageBuilder.declarePosition();
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, String.format("Duplicate keys (%s) are not allowed", keyType.getObjectValue(session, rowBlock, 0)));
        }
        uniqueKeys.add(rowBlock, 0);
        keyType.appendTo(rowBlock, 0, resultBuilder);
        valueType.appendTo(rowBlock, 1, resultBuilder);
    }
    mapBlockBuilder.closeEntry();
    pageBuilder.declarePosition();
    return mapType.getObject(mapBlockBuilder, mapBlockBuilder.getPositionCount() - 1);
}
Also used : MapType(io.prestosql.spi.type.MapType) SqlType(io.prestosql.spi.function.SqlType) RowType(io.prestosql.spi.type.RowType) Type(io.prestosql.spi.type.Type) RowType(io.prestosql.spi.type.RowType) TypedSet(io.prestosql.operator.aggregation.TypedSet) Block(io.prestosql.spi.block.Block) PrestoException(io.prestosql.spi.PrestoException) BlockBuilder(io.prestosql.spi.block.BlockBuilder) SqlNullable(io.prestosql.spi.function.SqlNullable) TypeParameter(io.prestosql.spi.function.TypeParameter) SqlType(io.prestosql.spi.function.SqlType)

Example 20 with RowType

use of io.prestosql.spi.type.RowType in project hetu-core by openlookeng.

the class ZipFunction method zip.

@UsedByGeneratedCode
public static Block zip(List<Type> types, Block... arrays) {
    int biggestCardinality = 0;
    for (Block array : arrays) {
        biggestCardinality = Math.max(biggestCardinality, array.getPositionCount());
    }
    RowType rowType = RowType.anonymous(types);
    BlockBuilder outputBuilder = rowType.createBlockBuilder(null, biggestCardinality);
    for (int outputPosition = 0; outputPosition < biggestCardinality; outputPosition++) {
        BlockBuilder rowBuilder = outputBuilder.beginBlockEntry();
        for (int fieldIndex = 0; fieldIndex < arrays.length; fieldIndex++) {
            if (arrays[fieldIndex].getPositionCount() <= outputPosition) {
                rowBuilder.appendNull();
            } else {
                types.get(fieldIndex).appendTo(arrays[fieldIndex], outputPosition, rowBuilder);
            }
        }
        outputBuilder.closeEntry();
    }
    return outputBuilder.build();
}
Also used : Block(io.prestosql.spi.block.Block) RowType(io.prestosql.spi.type.RowType) BlockBuilder(io.prestosql.spi.block.BlockBuilder) UsedByGeneratedCode(io.prestosql.spi.annotation.UsedByGeneratedCode)

Aggregations

RowType (io.prestosql.spi.type.RowType)76 ArrayType (io.prestosql.spi.type.ArrayType)38 Type (io.prestosql.spi.type.Type)35 MapType (io.prestosql.spi.type.MapType)29 Test (org.testng.annotations.Test)25 List (java.util.List)19 ImmutableList (com.google.common.collect.ImmutableList)18 VarcharType (io.prestosql.spi.type.VarcharType)17 Block (io.prestosql.spi.block.Block)16 BlockBuilder (io.prestosql.spi.block.BlockBuilder)15 Map (java.util.Map)12 BigintType (io.prestosql.spi.type.BigintType)11 DecimalType (io.prestosql.spi.type.DecimalType)11 DoubleType (io.prestosql.spi.type.DoubleType)11 BooleanType (io.prestosql.spi.type.BooleanType)10 IntegerType (io.prestosql.spi.type.IntegerType)10 RealType (io.prestosql.spi.type.RealType)10 SmallintType (io.prestosql.spi.type.SmallintType)10 TimestampType (io.prestosql.spi.type.TimestampType)10 VarbinaryType (io.prestosql.spi.type.VarbinaryType)10