Skip to content

Commit

Permalink
Cleanup duplicate code in ArraySortComparatorFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
electrum committed Oct 30, 2018
1 parent ff0df17 commit 57b4ade
Showing 1 changed file with 40 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.google.common.primitives.Ints;
import io.airlift.slice.Slice;

import java.util.Comparator;
import java.util.List;

import static com.facebook.presto.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT;
Expand All @@ -38,7 +39,6 @@ public final class ArraySortComparatorFunction
{
private final PageBuilder pageBuilder;
private static final int INITIAL_LENGTH = 128;
private static final String COMPARATOR_RETURN_ERROR = "Lambda comparator must return either -1, 0, or 1";
private List<Integer> positions = Ints.asList(new int[INITIAL_LENGTH]);

@TypeParameter("T")
Expand All @@ -58,16 +58,11 @@ public Block sortLong(
int arrayLength = block.getPositionCount();
initPositionsList(arrayLength);

positions.subList(0, arrayLength).sort((p1, p2) -> {
Long lambdaResult = function.apply(
block.isNull(p1) ? null : type.getLong(block, p1),
block.isNull(p2) ? null : type.getLong(block, p2));
checkCondition(
lambdaResult != null && (lambdaResult == -1 || lambdaResult == 0 || lambdaResult == 1),
INVALID_FUNCTION_ARGUMENT,
COMPARATOR_RETURN_ERROR);
return lambdaResult.intValue();
});
Comparator<Integer> comparator = (x, y) -> comparatorResult(function.apply(
block.isNull(x) ? null : type.getLong(block, x),
block.isNull(y) ? null : type.getLong(block, y)));

sortPositions(arrayLength, comparator);

return computeResultBlock(type, block, arrayLength);
}
Expand All @@ -83,16 +78,11 @@ public Block sortDouble(
int arrayLength = block.getPositionCount();
initPositionsList(arrayLength);

positions.subList(0, arrayLength).sort((p1, p2) -> {
Long lambdaResult = function.apply(
block.isNull(p1) ? null : type.getDouble(block, p1),
block.isNull(p2) ? null : type.getDouble(block, p2));
checkCondition(
lambdaResult != null && (lambdaResult == -1 || lambdaResult == 0 || lambdaResult == 1),
INVALID_FUNCTION_ARGUMENT,
COMPARATOR_RETURN_ERROR);
return lambdaResult.intValue();
});
Comparator<Integer> comparator = (x, y) -> comparatorResult(function.apply(
block.isNull(x) ? null : type.getDouble(block, x),
block.isNull(y) ? null : type.getDouble(block, y)));

sortPositions(arrayLength, comparator);

return computeResultBlock(type, block, arrayLength);
}
Expand All @@ -108,16 +98,11 @@ public Block sortBoolean(
int arrayLength = block.getPositionCount();
initPositionsList(arrayLength);

positions.subList(0, arrayLength).sort((p1, p2) -> {
Long lambdaResult = function.apply(
block.isNull(p1) ? null : type.getBoolean(block, p1),
block.isNull(p2) ? null : type.getBoolean(block, p2));
checkCondition(
lambdaResult != null && (lambdaResult == -1 || lambdaResult == 0 || lambdaResult == 1),
INVALID_FUNCTION_ARGUMENT,
COMPARATOR_RETURN_ERROR);
return lambdaResult.intValue();
});
Comparator<Integer> comparator = (x, y) -> comparatorResult(function.apply(
block.isNull(x) ? null : type.getBoolean(block, x),
block.isNull(y) ? null : type.getBoolean(block, y)));

sortPositions(arrayLength, comparator);

return computeResultBlock(type, block, arrayLength);
}
Expand All @@ -133,16 +118,11 @@ public Block sortSlice(
int arrayLength = block.getPositionCount();
initPositionsList(arrayLength);

positions.subList(0, arrayLength).sort((p1, p2) -> {
Long lambdaResult = function.apply(
block.isNull(p1) ? null : type.getSlice(block, p1),
block.isNull(p2) ? null : type.getSlice(block, p2));
checkCondition(
lambdaResult != null && (lambdaResult == -1 || lambdaResult == 0 || lambdaResult == 1),
INVALID_FUNCTION_ARGUMENT,
COMPARATOR_RETURN_ERROR);
return lambdaResult.intValue();
});
Comparator<Integer> comparator = (x, y) -> comparatorResult(function.apply(
block.isNull(x) ? null : type.getSlice(block, x),
block.isNull(y) ? null : type.getSlice(block, y)));

sortPositions(arrayLength, comparator);

return computeResultBlock(type, block, arrayLength);
}
Expand All @@ -158,16 +138,11 @@ public Block sortObject(
int arrayLength = block.getPositionCount();
initPositionsList(arrayLength);

positions.subList(0, arrayLength).sort((p1, p2) -> {
Long lambdaResult = function.apply(
block.isNull(p1) ? null : (Block) type.getObject(block, p1),
block.isNull(p2) ? null : (Block) type.getObject(block, p2));
checkCondition(
lambdaResult != null && (lambdaResult == -1 || lambdaResult == 0 || lambdaResult == 1),
INVALID_FUNCTION_ARGUMENT,
COMPARATOR_RETURN_ERROR);
return lambdaResult.intValue();
});
Comparator<Integer> comparator = (x, y) -> comparatorResult(function.apply(
block.isNull(x) ? null : (Block) type.getObject(block, x),
block.isNull(y) ? null : (Block) type.getObject(block, y)));

sortPositions(arrayLength, comparator);

return computeResultBlock(type, block, arrayLength);
}
Expand All @@ -182,6 +157,11 @@ private void initPositionsList(int arrayLength)
}
}

private void sortPositions(int arrayLength, Comparator<Integer> comparator)
{
positions.subList(0, arrayLength).sort(comparator);
}

private Block computeResultBlock(Type type, Block block, int arrayLength)
{
if (pageBuilder.isFull()) {
Expand All @@ -198,6 +178,15 @@ private Block computeResultBlock(Type type, Block block, int arrayLength)
return blockBuilder.getRegion(blockBuilder.getPositionCount() - arrayLength, arrayLength);
}

private static int comparatorResult(Long result)
{
checkCondition(
(result != null) && ((result == -1) || (result == 0) || (result == 1)),
INVALID_FUNCTION_ARGUMENT,
"Lambda comparator must return either -1, 0, or 1");
return result.intValue();
}

@FunctionalInterface
public interface ComparatorLongLambda
extends LambdaFunctionInterface
Expand Down

0 comments on commit 57b4ade

Please sign in to comment.