Search in sources :

Example 6 with TypeParameter

use of io.prestosql.spi.function.TypeParameter in project hetu-core by openlookeng.

the class ArrayDistinctFunction method distinct.

@TypeParameter("E")
@SqlType("array(E)")
public Block distinct(@TypeParameter("E") Type type, @OperatorDependency(operator = IS_DISTINCT_FROM, returnType = BOOLEAN, argumentTypes = { "E", "E" }) MethodHandle elementIsDistinctFrom, @SqlType("array(E)") Block array) {
    if (array.getPositionCount() < 2) {
        return array;
    }
    if (array.getPositionCount() == 2) {
        boolean firstValueNull = array.isNull(0);
        Object firstValue = firstValueNull ? defaultValue(type.getJavaType()) : readNativeValue(type, array, 0);
        boolean secondValueNull = array.isNull(1);
        Object secondValue = secondValueNull ? defaultValue(type.getJavaType()) : readNativeValue(type, array, 1);
        boolean distinct;
        try {
            distinct = (boolean) elementIsDistinctFrom.invoke(firstValue, firstValueNull, secondValue, secondValueNull);
        } catch (Throwable t) {
            throw internalError(t);
        }
        if (distinct) {
            return array;
        }
        return array.getSingleValueBlock(0);
    }
    TypedSet typedSet = new TypedSet(type, elementIsDistinctFrom, array.getPositionCount(), "array_distinct");
    int distinctCount = 0;
    if (pageBuilder.isFull()) {
        pageBuilder.reset();
    }
    BlockBuilder distinctElementBlockBuilder = pageBuilder.getBlockBuilder(0);
    for (int i = 0; i < array.getPositionCount(); i++) {
        if (!typedSet.contains(array, i)) {
            typedSet.add(array, i);
            distinctCount++;
            type.appendTo(array, i, distinctElementBlockBuilder);
        }
    }
    pageBuilder.declarePositions(distinctCount);
    return distinctElementBlockBuilder.getRegion(distinctElementBlockBuilder.getPositionCount() - distinctCount, distinctCount);
}
Also used : TypedSet(io.prestosql.operator.aggregation.TypedSet) BlockBuilder(io.prestosql.spi.block.BlockBuilder) TypeParameter(io.prestosql.spi.function.TypeParameter) SqlType(io.prestosql.spi.function.SqlType)

Example 7 with TypeParameter

use of io.prestosql.spi.function.TypeParameter 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 8 with TypeParameter

use of io.prestosql.spi.function.TypeParameter in project hetu-core by openlookeng.

the class RepeatFunction method repeat.

@TypeParameter("T")
@SqlType("array(T)")
public static Block repeat(@TypeParameter("T") Type type, @SqlNullable @SqlType("T") Slice element, @SqlType(StandardTypes.INTEGER) long count) {
    BlockBuilder blockBuilder = createBlockBuilder(type, count);
    if (element == null) {
        return repeatNullValues(blockBuilder, count);
    }
    if (count > 0) {
        type.writeSlice(blockBuilder, element);
        checkMaxSize(blockBuilder.getSizeInBytes(), count);
    }
    for (int i = 1; i < count; i++) {
        type.writeSlice(blockBuilder, element);
    }
    return blockBuilder.build();
}
Also used : BlockBuilder(io.prestosql.spi.block.BlockBuilder) TypeParameter(io.prestosql.spi.function.TypeParameter) SqlType(io.prestosql.spi.function.SqlType)

Example 9 with TypeParameter

use of io.prestosql.spi.function.TypeParameter 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 10 with TypeParameter

use of io.prestosql.spi.function.TypeParameter 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)

Aggregations

TypeParameter (io.prestosql.spi.function.TypeParameter)24 SqlType (io.prestosql.spi.function.SqlType)20 BlockBuilder (io.prestosql.spi.block.BlockBuilder)18 TypedSet (io.prestosql.operator.aggregation.TypedSet)6 TypeParameterSpecialization (io.prestosql.spi.function.TypeParameterSpecialization)5 Block (io.prestosql.spi.block.Block)4 HyperLogLog (io.airlift.stats.cardinality.HyperLogLog)3 PrestoException (io.prestosql.spi.PrestoException)3 InputFunction (io.prestosql.spi.function.InputFunction)3 ArrayType (io.prestosql.spi.type.ArrayType)3 RowType (io.prestosql.spi.type.RowType)3 Type (io.prestosql.spi.type.Type)3 Slice (io.airlift.slice.Slice)2 SqlNullable (io.prestosql.spi.function.SqlNullable)2 MapType (io.prestosql.spi.type.MapType)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 DictionaryBlock (io.prestosql.spi.block.DictionaryBlock)1 PageBuilderStatus (io.prestosql.spi.block.PageBuilderStatus)1 OperatorType (io.prestosql.spi.function.OperatorType)1