Search in sources :

Example 96 with BlockBuilder

use of io.trino.spi.block.BlockBuilder in project trino by trinodb.

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, @OperatorDependency(operator = EQUAL, argumentTypes = { "K", "K" }, convention = @Convention(arguments = { BLOCK_POSITION, BLOCK_POSITION }, result = NULLABLE_RETURN)) BlockPositionEqual keyEqual, @OperatorDependency(operator = HASH_CODE, argumentTypes = "K", convention = @Convention(arguments = BLOCK_POSITION, result = FAIL_ON_NULL)) BlockPositionHashCode keyHashCode, @SqlType("array(row(K,V))") Block mapEntries) {
    Type keyType = mapType.getKeyType();
    Type valueType = ((ArrayType) mapType.getValueType()).getElementType();
    RowType mapEntryType = RowType.anonymous(ImmutableList.of(keyType, valueType));
    if (pageBuilder.isFull()) {
        pageBuilder.reset();
    }
    int entryCount = mapEntries.getPositionCount();
    if (entryCount > entryIndicesList.length) {
        initializeEntryIndicesList(entryCount);
    }
    TypedSet keySet = createEqualityTypedSet(keyType, keyEqual, keyHashCode, entryCount, NAME);
    for (int i = 0; i < entryCount; i++) {
        if (mapEntries.isNull(i)) {
            clearEntryIndices(keySet.size());
            throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "map entry cannot be null");
        }
        Block mapEntryBlock = mapEntryType.getObject(mapEntries, i);
        if (mapEntryBlock.isNull(0)) {
            clearEntryIndices(keySet.size());
            throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "map key cannot be null");
        }
        if (keySet.add(mapEntryBlock, 0)) {
            entryIndicesList[keySet.size() - 1].add(i);
        } else {
            entryIndicesList[keySet.positionOf(mapEntryBlock, 0)].add(i);
        }
    }
    BlockBuilder multimapBlockBuilder = pageBuilder.getBlockBuilder(0);
    BlockBuilder mapWriter = multimapBlockBuilder.beginBlockEntry();
    for (int i = 0; i < keySet.size(); i++) {
        keyType.appendTo(mapEntryType.getObject(mapEntries, entryIndicesList[i].getInt(0)), 0, mapWriter);
        BlockBuilder valuesArray = mapWriter.beginBlockEntry();
        for (int entryIndex : entryIndicesList[i]) {
            valueType.appendTo(mapEntryType.getObject(mapEntries, entryIndex), 1, valuesArray);
        }
        mapWriter.closeEntry();
    }
    multimapBlockBuilder.closeEntry();
    pageBuilder.declarePosition();
    clearEntryIndices(keySet.size());
    return mapType.getObject(multimapBlockBuilder, multimapBlockBuilder.getPositionCount() - 1);
}
Also used : ArrayType(io.trino.spi.type.ArrayType) Type(io.trino.spi.type.Type) SqlType(io.trino.spi.function.SqlType) RowType(io.trino.spi.type.RowType) MapType(io.trino.spi.type.MapType) ArrayType(io.trino.spi.type.ArrayType) RowType(io.trino.spi.type.RowType) TypedSet(io.trino.operator.aggregation.TypedSet) TypedSet.createEqualityTypedSet(io.trino.operator.aggregation.TypedSet.createEqualityTypedSet) TrinoException(io.trino.spi.TrinoException) Block(io.trino.spi.block.Block) BlockBuilder(io.trino.spi.block.BlockBuilder) SqlNullable(io.trino.spi.function.SqlNullable) TypeParameter(io.trino.spi.function.TypeParameter) SqlType(io.trino.spi.function.SqlType)

Example 97 with BlockBuilder

use of io.trino.spi.block.BlockBuilder in project trino by trinodb.

the class QuantileDigestFunctions method valuesAtQuantilesBigint.

@ScalarFunction("values_at_quantiles")
@Description("For each input q between [0, 1], find the value whose rank in the sorted sequence of the n values represented by the qdigest is qn.")
@SqlType("array(bigint)")
public static Block valuesAtQuantilesBigint(@SqlType("qdigest(bigint)") Slice input, @SqlType("array(double)") Block percentilesArrayBlock) {
    QuantileDigest digest = new QuantileDigest(input);
    BlockBuilder output = BIGINT.createBlockBuilder(null, percentilesArrayBlock.getPositionCount());
    for (int i = 0; i < percentilesArrayBlock.getPositionCount(); i++) {
        BIGINT.writeLong(output, digest.getQuantile(DOUBLE.getDouble(percentilesArrayBlock, i)));
    }
    return output.build();
}
Also used : QuantileDigest(io.airlift.stats.QuantileDigest) BlockBuilder(io.trino.spi.block.BlockBuilder) ScalarFunction(io.trino.spi.function.ScalarFunction) Description(io.trino.spi.function.Description) SqlType(io.trino.spi.function.SqlType)

Example 98 with BlockBuilder

use of io.trino.spi.block.BlockBuilder in project trino by trinodb.

the class QuantileDigestFunctions method valuesAtQuantilesDouble.

@ScalarFunction("values_at_quantiles")
@Description("For each input q between [0, 1], find the value whose rank in the sorted sequence of the n values represented by the qdigest is qn.")
@SqlType("array(double)")
public static Block valuesAtQuantilesDouble(@SqlType("qdigest(double)") Slice input, @SqlType("array(double)") Block percentilesArrayBlock) {
    QuantileDigest digest = new QuantileDigest(input);
    BlockBuilder output = DOUBLE.createBlockBuilder(null, percentilesArrayBlock.getPositionCount());
    for (int i = 0; i < percentilesArrayBlock.getPositionCount(); i++) {
        DOUBLE.writeDouble(output, sortableLongToDouble(digest.getQuantile(DOUBLE.getDouble(percentilesArrayBlock, i))));
    }
    return output.build();
}
Also used : QuantileDigest(io.airlift.stats.QuantileDigest) BlockBuilder(io.trino.spi.block.BlockBuilder) ScalarFunction(io.trino.spi.function.ScalarFunction) Description(io.trino.spi.function.Description) SqlType(io.trino.spi.function.SqlType)

Example 99 with BlockBuilder

use of io.trino.spi.block.BlockBuilder in project trino by trinodb.

the class QuantileDigestFunctions method valuesAtQuantilesReal.

@ScalarFunction("values_at_quantiles")
@Description("For each input q between [0, 1], find the value whose rank in the sorted sequence of the n values represented by the qdigest is qn.")
@SqlType("array(real)")
public static Block valuesAtQuantilesReal(@SqlType("qdigest(real)") Slice input, @SqlType("array(double)") Block percentilesArrayBlock) {
    QuantileDigest digest = new QuantileDigest(input);
    BlockBuilder output = REAL.createBlockBuilder(null, percentilesArrayBlock.getPositionCount());
    for (int i = 0; i < percentilesArrayBlock.getPositionCount(); i++) {
        REAL.writeLong(output, floatToRawIntBits(sortableIntToFloat((int) digest.getQuantile(DOUBLE.getDouble(percentilesArrayBlock, i)))));
    }
    return output.build();
}
Also used : QuantileDigest(io.airlift.stats.QuantileDigest) BlockBuilder(io.trino.spi.block.BlockBuilder) ScalarFunction(io.trino.spi.function.ScalarFunction) Description(io.trino.spi.function.Description) SqlType(io.trino.spi.function.SqlType)

Example 100 with BlockBuilder

use of io.trino.spi.block.BlockBuilder in project trino by trinodb.

the class JsonToRowCast method toRow.

@UsedByGeneratedCode
public static Block toRow(RowType rowType, BlockBuilderAppender rowAppender, ConnectorSession connectorSession, Slice json) {
    try (JsonParser jsonParser = createJsonParser(JSON_FACTORY, json)) {
        jsonParser.nextToken();
        if (jsonParser.getCurrentToken() == JsonToken.VALUE_NULL) {
            return null;
        }
        BlockBuilder rowBlockBuilder = rowType.createBlockBuilder(null, 1);
        rowAppender.append(jsonParser, rowBlockBuilder);
        if (jsonParser.nextToken() != null) {
            throw new JsonCastException(format("Unexpected trailing token: %s", jsonParser.getText()));
        }
        return rowType.getObject(rowBlockBuilder, 0);
    } catch (TrinoException | JsonCastException e) {
        throw new TrinoException(INVALID_CAST_ARGUMENT, format("Cannot cast to %s. %s\n%s", rowType, e.getMessage(), truncateIfNecessaryForErrorMessage(json)), e);
    } catch (Exception e) {
        throw new TrinoException(INVALID_CAST_ARGUMENT, format("Cannot cast to %s.\n%s", rowType, truncateIfNecessaryForErrorMessage(json)), e);
    }
}
Also used : JsonCastException(io.trino.util.JsonCastException) TrinoException(io.trino.spi.TrinoException) JsonCastException(io.trino.util.JsonCastException) TrinoException(io.trino.spi.TrinoException) JsonUtil.createJsonParser(io.trino.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) BlockBuilder(io.trino.spi.block.BlockBuilder) UsedByGeneratedCode(io.trino.annotation.UsedByGeneratedCode)

Aggregations

BlockBuilder (io.trino.spi.block.BlockBuilder)459 Block (io.trino.spi.block.Block)98 Test (org.testng.annotations.Test)89 Slice (io.airlift.slice.Slice)87 Type (io.trino.spi.type.Type)61 SqlType (io.trino.spi.function.SqlType)52 Page (io.trino.spi.Page)41 ArrayType (io.trino.spi.type.ArrayType)41 RowType (io.trino.spi.type.RowType)35 MapType (io.trino.spi.type.MapType)34 Map (java.util.Map)25 PageBuilder (io.trino.spi.PageBuilder)22 VarcharType (io.trino.spi.type.VarcharType)22 ScalarFunction (io.trino.spi.function.ScalarFunction)21 TypeParameter (io.trino.spi.function.TypeParameter)20 UsedByGeneratedCode (io.trino.annotation.UsedByGeneratedCode)19 TrinoException (io.trino.spi.TrinoException)18 RowBlockBuilder (io.trino.spi.block.RowBlockBuilder)18 VariableWidthBlockBuilder (io.trino.spi.block.VariableWidthBlockBuilder)17 Description (io.trino.spi.function.Description)16