Search in sources :

Example 86 with PrestoException

use of com.facebook.presto.spi.PrestoException in project presto by prestodb.

the class MapTransformKeyFunction method transform.

public static Block transform(Type keyType, Type transformedKeyType, Type valueType, ConnectorSession session, Block block, MethodHandle function) {
    int positionCount = block.getPositionCount();
    BlockBuilder resultBuilder = new InterleavedBlockBuilder(ImmutableList.of(transformedKeyType, valueType), new BlockBuilderStatus(), positionCount);
    TypedSet typedSet = new TypedSet(transformedKeyType, positionCount / 2);
    for (int position = 0; position < positionCount; position += 2) {
        Object key = readNativeValue(keyType, block, position);
        Object value = readNativeValue(valueType, block, position + 1);
        Object transformedKey;
        try {
            transformedKey = function.invoke(key, value);
        } catch (Throwable throwable) {
            throw Throwables.propagate(throwable);
        }
        if (transformedKey == null) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "map key cannot be null");
        }
        writeNativeValue(transformedKeyType, resultBuilder, transformedKey);
        valueType.appendTo(block, position + 1, resultBuilder);
        if (typedSet.contains(resultBuilder, position)) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Duplicate keys (%s) are not allowed", transformedKeyType.getObjectValue(session, resultBuilder, position)));
        }
        typedSet.add(resultBuilder, position);
    }
    return resultBuilder.build();
}
Also used : TypedSet(com.facebook.presto.operator.aggregation.TypedSet) PrestoException(com.facebook.presto.spi.PrestoException) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus)

Example 87 with PrestoException

use of com.facebook.presto.spi.PrestoException in project presto by prestodb.

the class ParametricScalar method specialize.

@Override
public ScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry) {
    Signature boundSignature = applyBoundVariables(getSignature(), boundVariables, arity);
    if (implementations.getExactImplementations().containsKey(boundSignature)) {
        ScalarImplementation implementation = implementations.getExactImplementations().get(boundSignature);
        Optional<MethodHandleAndConstructor> methodHandleAndConstructor = implementation.specialize(boundSignature, boundVariables, typeManager, functionRegistry);
        checkCondition(methodHandleAndConstructor.isPresent(), FUNCTION_IMPLEMENTATION_ERROR, String.format("Exact implementation of %s do not match expected java types.", boundSignature.getName()));
        return new ScalarFunctionImplementation(implementation.isNullable(), implementation.getNullableArguments(), implementation.getNullFlags(), methodHandleAndConstructor.get().getMethodHandle(), methodHandleAndConstructor.get().getConstructor(), isDeterministic());
    }
    ScalarFunctionImplementation selectedImplementation = null;
    for (ScalarImplementation implementation : implementations.getSpecializedImplementations()) {
        Optional<MethodHandleAndConstructor> methodHandle = implementation.specialize(boundSignature, boundVariables, typeManager, functionRegistry);
        if (methodHandle.isPresent()) {
            checkCondition(selectedImplementation == null, AMBIGUOUS_FUNCTION_IMPLEMENTATION, "Ambiguous implementation for %s with bindings %s", getSignature(), boundVariables.getTypeVariables());
            selectedImplementation = new ScalarFunctionImplementation(implementation.isNullable(), implementation.getNullableArguments(), implementation.getNullFlags(), methodHandle.get().getMethodHandle(), methodHandle.get().getConstructor(), isDeterministic());
        }
    }
    if (selectedImplementation != null) {
        return selectedImplementation;
    }
    for (ScalarImplementation implementation : implementations.getGenericImplementations()) {
        Optional<MethodHandleAndConstructor> methodHandle = implementation.specialize(boundSignature, boundVariables, typeManager, functionRegistry);
        if (methodHandle.isPresent()) {
            checkCondition(selectedImplementation == null, AMBIGUOUS_FUNCTION_IMPLEMENTATION, "Ambiguous implementation for %s with bindings %s", getSignature(), boundVariables.getTypeVariables());
            selectedImplementation = new ScalarFunctionImplementation(implementation.isNullable(), implementation.getNullableArguments(), implementation.getNullFlags(), methodHandle.get().getMethodHandle(), methodHandle.get().getConstructor(), isDeterministic());
        }
    }
    if (selectedImplementation != null) {
        return selectedImplementation;
    }
    throw new PrestoException(FUNCTION_IMPLEMENTATION_MISSING, format("Unsupported type parameters (%s) for %s", boundVariables, getSignature()));
}
Also used : MethodHandleAndConstructor(com.facebook.presto.operator.scalar.annotations.ScalarImplementation.MethodHandleAndConstructor) Signature(com.facebook.presto.metadata.Signature) PrestoException(com.facebook.presto.spi.PrestoException) ScalarImplementation(com.facebook.presto.operator.scalar.annotations.ScalarImplementation)

Example 88 with PrestoException

use of com.facebook.presto.spi.PrestoException in project presto by prestodb.

the class RowComparisonOperator method compare.

protected static int compare(RowType rowType, List<MethodHandle> comparisonFunctions, Block leftRow, Block rightRow) {
    for (int i = 0; i < leftRow.getPositionCount(); i++) {
        checkElementNotNull(leftRow.isNull(i), "null value at position " + i);
        checkElementNotNull(rightRow.isNull(i), "null value at position " + i);
        Type type = rowType.getTypeParameters().get(i);
        Object leftElement = readNativeValue(type, leftRow, i);
        Object rightElement = readNativeValue(type, rightRow, i);
        try {
            if ((boolean) comparisonFunctions.get(i).invoke(leftElement, rightElement)) {
                return 1;
            }
            if ((boolean) comparisonFunctions.get(i).invoke(rightElement, leftElement)) {
                return -1;
            }
        } catch (Throwable t) {
            Throwables.propagateIfInstanceOf(t, Error.class);
            Throwables.propagateIfInstanceOf(t, PrestoException.class);
            throw new PrestoException(GENERIC_INTERNAL_ERROR, t);
        }
    }
    return 0;
}
Also used : RowType(com.facebook.presto.type.RowType) OperatorType(com.facebook.presto.spi.function.OperatorType) Type(com.facebook.presto.spi.type.Type) PrestoException(com.facebook.presto.spi.PrestoException)

Example 89 with PrestoException

use of com.facebook.presto.spi.PrestoException in project presto by prestodb.

the class MapConstructor method createMap.

@UsedByGeneratedCode
public static Block createMap(MapType mapType, Block keyBlock, Block valueBlock) {
    BlockBuilder blockBuilder = new InterleavedBlockBuilder(mapType.getTypeParameters(), new BlockBuilderStatus(), keyBlock.getPositionCount() * 2);
    checkCondition(keyBlock.getPositionCount() == valueBlock.getPositionCount(), INVALID_FUNCTION_ARGUMENT, "Key and value arrays must be the same length");
    for (int i = 0; i < keyBlock.getPositionCount(); i++) {
        if (keyBlock.isNull(i)) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "map key cannot be null");
        }
        mapType.getKeyType().appendTo(keyBlock, i, blockBuilder);
        mapType.getValueType().appendTo(valueBlock, i, blockBuilder);
    }
    return blockBuilder.build();
}
Also used : PrestoException(com.facebook.presto.spi.PrestoException) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus) UsedByGeneratedCode(com.facebook.presto.annotation.UsedByGeneratedCode)

Example 90 with PrestoException

use of com.facebook.presto.spi.PrestoException in project presto by prestodb.

the class RaptorColumnIdentity method deserialize.

public static RaptorColumnIdentity deserialize(byte[] bytes) {
    checkArgument(bytes.length >= Byte.BYTES, "bytes for RaptorColumnIdentity is corrupt");
    ByteArrayDataInput input = newDataInput(bytes);
    byte version = input.readByte();
    if ((version == CURRENT_VERSION) && (bytes.length == SERIALIZED_SIZE)) {
        long columnId = input.readLong();
        return new RaptorColumnIdentity(columnId);
    }
    throw new PrestoException(CORRUPT_SERIALIZED_IDENTITY, "RaptorColumnIdentity is corrupt: " + base16().lowerCase().encode(bytes));
}
Also used : PrestoException(com.facebook.presto.spi.PrestoException) ByteArrayDataInput(com.google.common.io.ByteArrayDataInput)

Aggregations

PrestoException (com.facebook.presto.spi.PrestoException)273 IOException (java.io.IOException)58 Type (com.facebook.presto.spi.type.Type)48 ImmutableList (com.google.common.collect.ImmutableList)40 SchemaTableName (com.facebook.presto.spi.SchemaTableName)32 ArrayList (java.util.ArrayList)32 List (java.util.List)28 Path (org.apache.hadoop.fs.Path)28 Map (java.util.Map)24 Slice (io.airlift.slice.Slice)23 TableNotFoundException (com.facebook.presto.spi.TableNotFoundException)22 SqlType (com.facebook.presto.spi.function.SqlType)22 ImmutableMap (com.google.common.collect.ImmutableMap)22 Optional (java.util.Optional)20 ColumnMetadata (com.facebook.presto.spi.ColumnMetadata)19 Block (com.facebook.presto.spi.block.Block)19 Test (org.testng.annotations.Test)19 ConnectorSession (com.facebook.presto.spi.ConnectorSession)17 Table (com.facebook.presto.hive.metastore.Table)15 Session (com.facebook.presto.Session)14