Search in sources :

Example 16 with NotSupportedException

use of com.facebook.presto.common.NotSupportedException in project presto by prestodb.

the class MapConstructor method createMap.

@UsedByGeneratedCode
public static Block createMap(MapType mapType, Type keyType, Type valueType, MethodHandle keyBlockEqual, MethodHandle keyBlockHashCode, MethodHandle keyIndeterminate, SqlFunctionProperties properties, Block keyBlock, Block valueBlock) {
    checkCondition(keyBlock.getPositionCount() == valueBlock.getPositionCount(), INVALID_FUNCTION_ARGUMENT, "Key and value arrays must be the same length");
    MapBlockBuilder mapBlockBuilder = (MapBlockBuilder) mapType.createBlockBuilder(null, keyBlock.getPositionCount() * 2);
    BlockBuilder blockBuilder = mapBlockBuilder.beginBlockEntry();
    for (int i = 0; i < keyBlock.getPositionCount(); i++) {
        if (keyBlock.isNull(i)) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "map key cannot be null");
        }
        if (keyType.getJavaType() == Block.class) {
            // If it's nto primitive or string, we need to look for nulls in the block.
            Object keyObject = readNativeValue(mapType.getKeyType(), keyBlock, i);
            try {
                if ((boolean) keyIndeterminate.invoke(keyObject, false)) {
                    throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "map key cannot be indeterminate: " + mapType.getKeyType().getObjectValue(properties, keyBlock, i));
                }
            } catch (Throwable t) {
                throw internalError(t);
            }
        }
        keyType.appendTo(keyBlock, i, blockBuilder);
        valueType.appendTo(valueBlock, i, blockBuilder);
    }
    try {
        mapBlockBuilder.closeEntryStrict(keyBlockEqual, keyBlockHashCode);
    } catch (DuplicateMapKeyException e) {
        throw new PrestoException(INVALID_FUNCTION_ARGUMENT, e.getDetailedMessage(mapType.getKeyType(), properties), e);
    } catch (NotSupportedException e) {
        throw new PrestoException(NOT_SUPPORTED, e.getMessage(), e);
    }
    return mapType.getObject(mapBlockBuilder, mapBlockBuilder.getPositionCount() - 1);
}
Also used : MapBlockBuilder(com.facebook.presto.common.block.MapBlockBuilder) DuplicateMapKeyException(com.facebook.presto.common.block.DuplicateMapKeyException) PrestoException(com.facebook.presto.spi.PrestoException) NotSupportedException(com.facebook.presto.common.NotSupportedException) MapBlockBuilder(com.facebook.presto.common.block.MapBlockBuilder) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) UsedByGeneratedCode(com.facebook.presto.annotation.UsedByGeneratedCode)

Example 17 with NotSupportedException

use of com.facebook.presto.common.NotSupportedException in project presto by prestodb.

the class MapElementAtFunction method elementAt.

@UsedByGeneratedCode
public static Object elementAt(MethodHandle keyNativeHashCode, MethodHandle keyBlockNativeEquals, MethodHandle keyBlockHashCode, Type valueType, Block map, double key) {
    SingleMapBlock mapBlock = (SingleMapBlock) map;
    int valuePosition;
    try {
        valuePosition = mapBlock.seekKeyExact(key, keyNativeHashCode, keyBlockNativeEquals, keyBlockHashCode);
    } catch (NotSupportedException e) {
        throw new PrestoException(NOT_SUPPORTED, e.getMessage(), e);
    }
    if (valuePosition == -1) {
        return null;
    }
    return readNativeValue(valueType, mapBlock, valuePosition);
}
Also used : PrestoException(com.facebook.presto.spi.PrestoException) SingleMapBlock(com.facebook.presto.common.block.SingleMapBlock) NotSupportedException(com.facebook.presto.common.NotSupportedException) UsedByGeneratedCode(com.facebook.presto.annotation.UsedByGeneratedCode)

Example 18 with NotSupportedException

use of com.facebook.presto.common.NotSupportedException in project presto by prestodb.

the class MapElementAtFunction method elementAt.

@UsedByGeneratedCode
public static Object elementAt(MethodHandle keyNativeHashCode, MethodHandle keyBlockNativeEquals, MethodHandle keyBlockHashCode, Type valueType, Block map, Object key) {
    SingleMapBlock mapBlock = (SingleMapBlock) map;
    int valuePosition;
    try {
        valuePosition = mapBlock.seekKeyExact((Block) key, keyNativeHashCode, keyBlockNativeEquals, keyBlockHashCode);
    } catch (NotSupportedException e) {
        throw new PrestoException(NOT_SUPPORTED, e.getMessage(), e);
    }
    if (valuePosition == -1) {
        return null;
    }
    return readNativeValue(valueType, mapBlock, valuePosition);
}
Also used : SingleMapBlock(com.facebook.presto.common.block.SingleMapBlock) Block(com.facebook.presto.common.block.Block) PrestoException(com.facebook.presto.spi.PrestoException) SingleMapBlock(com.facebook.presto.common.block.SingleMapBlock) NotSupportedException(com.facebook.presto.common.NotSupportedException) UsedByGeneratedCode(com.facebook.presto.annotation.UsedByGeneratedCode)

Example 19 with NotSupportedException

use of com.facebook.presto.common.NotSupportedException in project presto by prestodb.

the class MapElementAtFunction method elementAt.

@UsedByGeneratedCode
public static Object elementAt(MethodHandle keyNativeHashCode, MethodHandle keyBlockNativeEquals, MethodHandle keyBlockHashCode, Type valueType, Block map, Slice key) {
    SingleMapBlock mapBlock = (SingleMapBlock) map;
    int valuePosition;
    try {
        valuePosition = mapBlock.seekKeyExact(key, keyNativeHashCode, keyBlockNativeEquals, keyBlockHashCode);
    } catch (NotSupportedException e) {
        throw new PrestoException(NOT_SUPPORTED, e.getMessage(), e);
    }
    if (valuePosition == -1) {
        return null;
    }
    return readNativeValue(valueType, mapBlock, valuePosition);
}
Also used : PrestoException(com.facebook.presto.spi.PrestoException) SingleMapBlock(com.facebook.presto.common.block.SingleMapBlock) NotSupportedException(com.facebook.presto.common.NotSupportedException) UsedByGeneratedCode(com.facebook.presto.annotation.UsedByGeneratedCode)

Example 20 with NotSupportedException

use of com.facebook.presto.common.NotSupportedException in project presto by prestodb.

the class ArraySortFunction method sort.

@TypeParameter("E")
@SqlType("array(E)")
public static Block sort(@OperatorDependency(operator = LESS_THAN, argumentTypes = { "E", "E" }) MethodHandle lessThanFunction, @TypeParameter("E") Type type, @SqlType("array(E)") Block block) {
    int arrayLength = block.getPositionCount();
    if (arrayLength < 2) {
        return block;
    }
    ListOfPositions listOfPositions = new ListOfPositions(block.getPositionCount());
    if (block.mayHaveNull()) {
        listOfPositions.sort(new Comparator<Integer>() {

            @Override
            public int compare(Integer p1, Integer p2) {
                if (block.isNull(p1)) {
                    return block.isNull(p2) ? 0 : 1;
                } else if (block.isNull(p2)) {
                    return -1;
                }
                try {
                    // TODO: This could be quite slow, it should use parametric equals
                    return type.compareTo(block, p1, block, p2);
                } catch (PrestoException | NotSupportedException e) {
                    if (e instanceof NotSupportedException || ((PrestoException) e).getErrorCode() == NOT_SUPPORTED.toErrorCode()) {
                        throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Array contains elements not supported for comparison", e);
                    }
                    throw e;
                }
            }
        });
    } else {
        listOfPositions.sort(new Comparator<Integer>() {

            @Override
            public int compare(Integer p1, Integer p2) {
                try {
                    // TODO: This could be quite slow, it should use parametric equals
                    return type.compareTo(block, p1, block, p2);
                } catch (PrestoException | NotSupportedException e) {
                    if (e instanceof NotSupportedException || ((PrestoException) e).getErrorCode() == NOT_SUPPORTED.toErrorCode()) {
                        throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Array contains elements not supported for comparison", e);
                    }
                    throw e;
                }
            }
        });
    }
    List<Integer> sortedListOfPositions = listOfPositions.getSortedListOfPositions();
    if (sortedListOfPositions == listOfPositions) {
        // Original array is already sorted.
        return block;
    }
    BlockBuilder blockBuilder = type.createBlockBuilder(null, arrayLength);
    for (int i = 0; i < arrayLength; i++) {
        type.appendTo(block, sortedListOfPositions.get(i), blockBuilder);
    }
    return blockBuilder.build();
}
Also used : PrestoException(com.facebook.presto.spi.PrestoException) NotSupportedException(com.facebook.presto.common.NotSupportedException) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) TypeParameter(com.facebook.presto.spi.function.TypeParameter) SqlType(com.facebook.presto.spi.function.SqlType)

Aggregations

NotSupportedException (com.facebook.presto.common.NotSupportedException)25 PrestoException (com.facebook.presto.spi.PrestoException)24 SingleMapBlock (com.facebook.presto.common.block.SingleMapBlock)12 UsedByGeneratedCode (com.facebook.presto.annotation.UsedByGeneratedCode)11 Block (com.facebook.presto.common.block.Block)9 Type (com.facebook.presto.common.type.Type)9 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)3 SortOrder (com.facebook.presto.common.block.SortOrder)3 JsonCodec (com.facebook.airlift.json.JsonCodec)1 JsonCodec.jsonCodec (com.facebook.airlift.json.JsonCodec.jsonCodec)1 Logger (com.facebook.airlift.log.Logger)1 Page (com.facebook.presto.common.Page)1 RuntimeStats (com.facebook.presto.common.RuntimeStats)1 AdaptiveLongBigArray (com.facebook.presto.common.array.AdaptiveLongBigArray)1 DuplicateMapKeyException (com.facebook.presto.common.block.DuplicateMapKeyException)1 MapBlockBuilder (com.facebook.presto.common.block.MapBlockBuilder)1 OperatorType (com.facebook.presto.common.function.OperatorType)1 CharType (com.facebook.presto.common.type.CharType)1 DecimalType (com.facebook.presto.common.type.DecimalType)1 MapType (com.facebook.presto.common.type.MapType)1