Search in sources :

Example 1 with DuplicateMapKeyException

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

the class MapConstructor method createMap.

@UsedByGeneratedCode
public static Block createMap(MapType mapType, MethodHandle keyIndeterminate, State state, ConnectorSession session, Block keyBlock, Block valueBlock) {
    checkCondition(keyBlock.getPositionCount() == valueBlock.getPositionCount(), INVALID_FUNCTION_ARGUMENT, "Key and value arrays must be the same length");
    PageBuilder pageBuilder = state.getPageBuilder();
    if (pageBuilder.isFull()) {
        pageBuilder.reset();
    }
    MapBlockBuilder mapBlockBuilder = (MapBlockBuilder) pageBuilder.getBlockBuilder(0);
    mapBlockBuilder.strict();
    BlockBuilder blockBuilder = mapBlockBuilder.beginBlockEntry();
    for (int i = 0; i < keyBlock.getPositionCount(); i++) {
        if (keyBlock.isNull(i)) {
            // close block builder before throwing as we may be in a TRY() call
            // so that subsequent calls do not find it in an inconsistent state
            mapBlockBuilder.closeEntry();
            throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "map key cannot be null");
        }
        Object keyObject = readNativeValue(mapType.getKeyType(), keyBlock, i);
        try {
            if ((boolean) keyIndeterminate.invoke(keyObject)) {
                throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "map key cannot be indeterminate: " + mapType.getKeyType().getObjectValue(session, keyBlock, i));
            }
        } catch (Throwable t) {
            mapBlockBuilder.closeEntry();
            throw internalError(t);
        }
        mapType.getKeyType().appendTo(keyBlock, i, blockBuilder);
        mapType.getValueType().appendTo(valueBlock, i, blockBuilder);
    }
    try {
        mapBlockBuilder.closeEntry();
    } catch (DuplicateMapKeyException e) {
        throw e.withDetailedMessage(mapType.getKeyType(), session);
    } finally {
        pageBuilder.declarePosition();
    }
    return mapType.getObject(mapBlockBuilder, mapBlockBuilder.getPositionCount() - 1);
}
Also used : MapBlockBuilder(io.trino.spi.block.MapBlockBuilder) TrinoException(io.trino.spi.TrinoException) DuplicateMapKeyException(io.trino.spi.block.DuplicateMapKeyException) PageBuilder(io.trino.spi.PageBuilder) MapBlockBuilder(io.trino.spi.block.MapBlockBuilder) BlockBuilder(io.trino.spi.block.BlockBuilder) UsedByGeneratedCode(io.trino.annotation.UsedByGeneratedCode)

Aggregations

UsedByGeneratedCode (io.trino.annotation.UsedByGeneratedCode)1 PageBuilder (io.trino.spi.PageBuilder)1 TrinoException (io.trino.spi.TrinoException)1 BlockBuilder (io.trino.spi.block.BlockBuilder)1 DuplicateMapKeyException (io.trino.spi.block.DuplicateMapKeyException)1 MapBlockBuilder (io.trino.spi.block.MapBlockBuilder)1