Search in sources :

Example 31 with UsedByGeneratedCode

use of io.trino.annotation.UsedByGeneratedCode in project trino by trinodb.

the class ArrayConcatUtils method prependElement.

@UsedByGeneratedCode
public static Block prependElement(Type elementType, double value, Block block) {
    BlockBuilder blockBuilder = elementType.createBlockBuilder(null, block.getPositionCount() + 1);
    elementType.writeDouble(blockBuilder, value);
    for (int i = 0; i < block.getPositionCount(); i++) {
        elementType.appendTo(block, i, blockBuilder);
    }
    return blockBuilder.build();
}
Also used : BlockBuilder(io.trino.spi.block.BlockBuilder) UsedByGeneratedCode(io.trino.annotation.UsedByGeneratedCode)

Example 32 with UsedByGeneratedCode

use of io.trino.annotation.UsedByGeneratedCode in project trino by trinodb.

the class ArrayConcatUtils method prependElement.

@UsedByGeneratedCode
public static Block prependElement(Type elementType, Object value, Block block) {
    BlockBuilder blockBuilder = elementType.createBlockBuilder(null, block.getPositionCount() + 1);
    elementType.writeObject(blockBuilder, value);
    for (int i = 0; i < block.getPositionCount(); i++) {
        elementType.appendTo(block, i, blockBuilder);
    }
    return blockBuilder.build();
}
Also used : BlockBuilder(io.trino.spi.block.BlockBuilder) UsedByGeneratedCode(io.trino.annotation.UsedByGeneratedCode)

Example 33 with UsedByGeneratedCode

use of io.trino.annotation.UsedByGeneratedCode in project trino by trinodb.

the class DecimalCasts method jsonToLongDecimal.

@UsedByGeneratedCode
public static Int128 jsonToLongDecimal(Slice json, long precision, long scale, Int128 tenToScale) {
    try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
        parser.nextToken();
        Int128 result = currentTokenAsLongDecimal(parser, intPrecision(precision), DecimalConversions.intScale(scale));
        // check no trailing token
        checkCondition(parser.nextToken() == null, INVALID_CAST_ARGUMENT, "Cannot cast input json to DECIMAL(%s,%s)", precision, scale);
        return result;
    } catch (IOException | NumberFormatException | JsonCastException e) {
        throw new TrinoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to DECIMAL(%s,%s)", json.toStringUtf8(), precision, scale), e);
    }
}
Also used : JsonCastException(io.trino.util.JsonCastException) TrinoException(io.trino.spi.TrinoException) IOException(java.io.IOException) Int128(io.trino.spi.type.Int128) JsonUtil.createJsonParser(io.trino.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) UsedByGeneratedCode(io.trino.annotation.UsedByGeneratedCode)

Example 34 with UsedByGeneratedCode

use of io.trino.annotation.UsedByGeneratedCode in project trino by trinodb.

the class DecimalCasts method jsonToShortDecimal.

@UsedByGeneratedCode
public static Long jsonToShortDecimal(Slice json, long precision, long scale, long tenToScale) {
    try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
        parser.nextToken();
        Long result = currentTokenAsShortDecimal(parser, intPrecision(precision), DecimalConversions.intScale(scale));
        // check no trailing token
        checkCondition(parser.nextToken() == null, INVALID_CAST_ARGUMENT, "Cannot cast input json to DECIMAL(%s,%s)", precision, scale);
        return result;
    } catch (IOException | NumberFormatException | JsonCastException e) {
        throw new TrinoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to DECIMAL(%s,%s)", json.toStringUtf8(), precision, scale), e);
    }
}
Also used : JsonCastException(io.trino.util.JsonCastException) TrinoException(io.trino.spi.TrinoException) IOException(java.io.IOException) JsonUtil.createJsonParser(io.trino.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) UsedByGeneratedCode(io.trino.annotation.UsedByGeneratedCode)

Example 35 with UsedByGeneratedCode

use of io.trino.annotation.UsedByGeneratedCode in project trino by trinodb.

the class DecimalCasts method varcharToShortDecimal.

@UsedByGeneratedCode
public static long varcharToShortDecimal(Slice value, long precision, long scale, long tenToScale) {
    BigDecimal result;
    String stringValue = value.toString(UTF_8);
    try {
        result = new BigDecimal(stringValue).setScale(DecimalConversions.intScale(scale), HALF_UP);
    } catch (NumberFormatException e) {
        throw new TrinoException(INVALID_CAST_ARGUMENT, format("Cannot cast VARCHAR '%s' to DECIMAL(%s, %s). Value is not a number.", stringValue, precision, scale));
    }
    if (overflows(result, precision)) {
        throw new TrinoException(INVALID_CAST_ARGUMENT, format("Cannot cast VARCHAR '%s' to DECIMAL(%s, %s). Value too large.", stringValue, precision, scale));
    }
    return result.unscaledValue().longValue();
}
Also used : TrinoException(io.trino.spi.TrinoException) BigDecimal(java.math.BigDecimal) UsedByGeneratedCode(io.trino.annotation.UsedByGeneratedCode)

Aggregations

UsedByGeneratedCode (io.trino.annotation.UsedByGeneratedCode)35 BlockBuilder (io.trino.spi.block.BlockBuilder)19 TrinoException (io.trino.spi.TrinoException)11 SingleMapBlock (io.trino.spi.block.SingleMapBlock)8 JsonParser (com.fasterxml.jackson.core.JsonParser)5 JsonCastException (io.trino.util.JsonCastException)5 JsonUtil.createJsonParser (io.trino.util.JsonUtil.createJsonParser)5 IOException (java.io.IOException)5 PageBuilder (io.trino.spi.PageBuilder)4 Block (io.trino.spi.block.Block)4 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)3 DynamicSliceOutput (io.airlift.slice.DynamicSliceOutput)3 SliceOutput (io.airlift.slice.SliceOutput)3 JsonUtil.createJsonGenerator (io.trino.util.JsonUtil.createJsonGenerator)3 TypeVariableConstraint (io.trino.metadata.TypeVariableConstraint)2 TypedSet (io.trino.operator.aggregation.TypedSet)2 TypedSet.createEqualityTypedSet (io.trino.operator.aggregation.TypedSet.createEqualityTypedSet)2 MapType (io.trino.spi.type.MapType)2 Type (io.trino.spi.type.Type)2 TypeSignature.mapType (io.trino.spi.type.TypeSignature.mapType)2