use of com.facebook.presto.operator.aggregation.OptimizedTypedSet in project presto by prestodb.
the class MapConcatFunction method mapConcat.
@UsedByGeneratedCode
public static Block mapConcat(MapType mapType, Block[] maps) {
int entries = 0;
int lastMapIndex = maps.length - 1;
int firstMapIndex = lastMapIndex;
for (int i = 0; i < maps.length; i++) {
entries += maps[i].getPositionCount();
if (maps[i].getPositionCount() > 0) {
lastMapIndex = i;
firstMapIndex = min(firstMapIndex, i);
}
}
if (lastMapIndex == firstMapIndex) {
return maps[lastMapIndex];
}
Type keyType = mapType.getKeyType();
Type valueType = mapType.getValueType();
// We need to divide the entries by 2 because the maps array is SingleMapBlocks and it had the positionCount twice as large as a normal Block
OptimizedTypedSet typedSet = new OptimizedTypedSet(keyType, maps.length, entries / 2);
for (int i = lastMapIndex; i >= firstMapIndex; i--) {
SingleMapBlock singleMapBlock = (SingleMapBlock) maps[i];
Block keyBlock = singleMapBlock.getKeyBlock();
typedSet.union(keyBlock);
}
List<SelectedPositions> selectedPositionsList = typedSet.getPositionsForBlocks();
BlockBuilder mapBlockBuilder = mapType.createBlockBuilder(null, selectedPositionsList.size());
BlockBuilder blockBuilder = mapBlockBuilder.beginBlockEntry();
for (int i = lastMapIndex; i >= firstMapIndex; i--) {
SingleMapBlock singleMapBlock = (SingleMapBlock) maps[i];
// selectedPositions was ordered by addUnion sequence therefore the order should be reversed.
SelectedPositions selectedPositions = selectedPositionsList.get(lastMapIndex - i);
assert selectedPositions.isList();
int[] positions = selectedPositions.getPositions();
for (int j = 0; j < selectedPositions.size(); j++) {
int position = positions[j];
keyType.appendTo(singleMapBlock, 2 * position, blockBuilder);
valueType.appendTo(singleMapBlock, 2 * position + 1, blockBuilder);
}
}
mapBlockBuilder.closeEntry();
return mapType.getObject(mapBlockBuilder, mapBlockBuilder.getPositionCount() - 1);
}
use of com.facebook.presto.operator.aggregation.OptimizedTypedSet in project presto by prestodb.
the class ArrayExceptFunction method except.
@TypeParameter("E")
@SqlType("array(E)")
public static Block except(@TypeParameter("E") Type type, @SqlType("array(E)") Block leftArray, @SqlType("array(E)") Block rightArray) {
int leftPositionCount = leftArray.getPositionCount();
int rightPositionCount = rightArray.getPositionCount();
if (leftPositionCount == 0) {
return leftArray;
}
OptimizedTypedSet typedSet = new OptimizedTypedSet(type, max(leftPositionCount, rightPositionCount));
typedSet.union(rightArray);
typedSet.except(leftArray);
return typedSet.getBlock();
}
use of com.facebook.presto.operator.aggregation.OptimizedTypedSet in project presto by prestodb.
the class ArrayUnionFunction method union.
@TypeParameter("E")
@SqlType("array(E)")
public static Block union(@TypeParameter("E") Type type, @SqlType("array(E)") Block leftArray, @SqlType("array(E)") Block rightArray) {
int leftArrayCount = leftArray.getPositionCount();
int rightArrayCount = rightArray.getPositionCount();
OptimizedTypedSet typedSet = new OptimizedTypedSet(type, leftArrayCount + rightArrayCount);
typedSet.union(leftArray);
typedSet.union(rightArray);
return typedSet.getBlock();
}
use of com.facebook.presto.operator.aggregation.OptimizedTypedSet in project presto by prestodb.
the class ArrayIntersectFunction method intersect.
@ScalarFunction("array_intersect")
@Description("Intersects elements of the two given arrays")
@TypeParameter("E")
@SqlType("array(E)")
public static Block intersect(@TypeParameter("E") Type type, @SqlType("array(E)") Block leftArray, @SqlType("array(E)") Block rightArray) {
if (leftArray.getPositionCount() < rightArray.getPositionCount()) {
Block tempArray = leftArray;
leftArray = rightArray;
rightArray = tempArray;
}
int rightPositionCount = rightArray.getPositionCount();
if (rightPositionCount == 0) {
return rightArray;
}
OptimizedTypedSet typedSet = new OptimizedTypedSet(type, rightPositionCount);
typedSet.union(rightArray);
typedSet.intersect(leftArray);
return typedSet.getBlock();
}
Aggregations