Search in sources :

Example 16 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, long 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)

Example 17 with UsedByGeneratedCode

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

the class JsonToArrayCast method toArray.

@UsedByGeneratedCode
public static Block toArray(ArrayType arrayType, BlockBuilderAppender arrayAppender, ConnectorSession connectorSession, Slice json) {
    try (JsonParser jsonParser = createJsonParser(JSON_FACTORY, json)) {
        jsonParser.nextToken();
        if (jsonParser.getCurrentToken() == JsonToken.VALUE_NULL) {
            return null;
        }
        BlockBuilder blockBuilder = arrayType.createBlockBuilder(null, 1);
        arrayAppender.append(jsonParser, blockBuilder);
        if (jsonParser.nextToken() != null) {
            throw new JsonCastException(format("Unexpected trailing token: %s", jsonParser.getText()));
        }
        return arrayType.getObject(blockBuilder, blockBuilder.getPositionCount() - 1);
    } catch (TrinoException | JsonCastException e) {
        throw new TrinoException(INVALID_CAST_ARGUMENT, format("Cannot cast to %s. %s\n%s", arrayType, e.getMessage(), truncateIfNecessaryForErrorMessage(json)), e);
    } catch (Exception e) {
        throw new TrinoException(INVALID_CAST_ARGUMENT, format("Cannot cast to %s.\n%s", arrayType, 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)

Example 18 with UsedByGeneratedCode

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

the class JsonToMapCast method toMap.

@UsedByGeneratedCode
public static Block toMap(MapType mapType, BlockBuilderAppender mapAppender, ConnectorSession connectorSession, Slice json) {
    try (JsonParser jsonParser = createJsonParser(JSON_FACTORY, json)) {
        jsonParser.nextToken();
        if (jsonParser.getCurrentToken() == JsonToken.VALUE_NULL) {
            return null;
        }
        BlockBuilder blockBuilder = mapType.createBlockBuilder(null, 1);
        mapAppender.append(jsonParser, blockBuilder);
        if (jsonParser.nextToken() != null) {
            throw new JsonCastException(format("Unexpected trailing token: %s", jsonParser.getText()));
        }
        return mapType.getObject(blockBuilder, blockBuilder.getPositionCount() - 1);
    } catch (TrinoException | JsonCastException e) {
        throw new TrinoException(INVALID_CAST_ARGUMENT, format("Cannot cast to %s. %s\n%s", mapType, e.getMessage(), truncateIfNecessaryForErrorMessage(json)), e);
    } catch (Exception e) {
        throw new TrinoException(INVALID_CAST_ARGUMENT, format("Cannot cast to %s.\n%s", mapType, 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)

Example 19 with UsedByGeneratedCode

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

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.trino.spi.block.Block) RowType(io.trino.spi.type.RowType) BlockBuilder(io.trino.spi.block.BlockBuilder) UsedByGeneratedCode(io.trino.annotation.UsedByGeneratedCode)

Example 20 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, Object 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