Search in sources :

Example 6 with UsedByGeneratedCode

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

the class ArrayConcatUtils method appendElement.

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

Example 7 with UsedByGeneratedCode

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

the class RowToJsonCast method toJsonArray.

@UsedByGeneratedCode
public static Slice toJsonArray(List<JsonGeneratorWriter> fieldWriters, Block block) {
    try {
        SliceOutput output = new DynamicSliceOutput(40);
        try (JsonGenerator jsonGenerator = createJsonGenerator(JSON_FACTORY, output)) {
            jsonGenerator.writeStartArray();
            for (int i = 0; i < block.getPositionCount(); i++) {
                fieldWriters.get(i).writeJsonValue(jsonGenerator, block, i);
            }
            jsonGenerator.writeEndArray();
        }
        return output.slice();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : SliceOutput(io.airlift.slice.SliceOutput) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) JsonUtil.createJsonGenerator(io.trino.util.JsonUtil.createJsonGenerator) IOException(java.io.IOException) TypeVariableConstraint(io.trino.metadata.TypeVariableConstraint) UsedByGeneratedCode(io.trino.annotation.UsedByGeneratedCode)

Example 8 with UsedByGeneratedCode

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

the class RowToJsonCast method toJsonObject.

@UsedByGeneratedCode
public static Slice toJsonObject(List<String> fieldNames, List<JsonGeneratorWriter> fieldWriters, Block block) {
    try {
        SliceOutput output = new DynamicSliceOutput(40);
        try (JsonGenerator jsonGenerator = createJsonGenerator(JSON_FACTORY, output)) {
            jsonGenerator.writeStartObject();
            for (int i = 0; i < block.getPositionCount(); i++) {
                jsonGenerator.writeFieldName(fieldNames.get(i));
                fieldWriters.get(i).writeJsonValue(jsonGenerator, block, i);
            }
            jsonGenerator.writeEndObject();
        }
        return output.slice();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : SliceOutput(io.airlift.slice.SliceOutput) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) JsonUtil.createJsonGenerator(io.trino.util.JsonUtil.createJsonGenerator) IOException(java.io.IOException) TypeVariableConstraint(io.trino.metadata.TypeVariableConstraint) UsedByGeneratedCode(io.trino.annotation.UsedByGeneratedCode)

Example 9 with UsedByGeneratedCode

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

the class DecimalCasts method varcharToLongDecimal.

@UsedByGeneratedCode
public static Int128 varcharToLongDecimal(Slice value, long precision, long scale, Int128 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 Int128.valueOf(result.unscaledValue());
}
Also used : TrinoException(io.trino.spi.TrinoException) BigDecimal(java.math.BigDecimal) UsedByGeneratedCode(io.trino.annotation.UsedByGeneratedCode)

Example 10 with UsedByGeneratedCode

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

the class MapElementAtFunction method elementAt.

@UsedByGeneratedCode
public static Object elementAt(Type valueType, Block map, boolean key) {
    SingleMapBlock mapBlock = (SingleMapBlock) map;
    int valuePosition = mapBlock.seekKeyExact(key);
    if (valuePosition == -1) {
        return null;
    }
    return readNativeValue(valueType, mapBlock, valuePosition);
}
Also used : SingleMapBlock(io.trino.spi.block.SingleMapBlock) 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