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