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