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();
}
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()));
}
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;
}
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();
}
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));
}
Aggregations