use of io.prestosql.operator.aggregation.TypedSet in project hetu-core by openlookeng.
the class MapConcatFunction method mapConcat.
@UsedByGeneratedCode
public static Block mapConcat(MapType mapType, Object state, 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];
}
PageBuilder pageBuilder = (PageBuilder) state;
if (pageBuilder.isFull()) {
pageBuilder.reset();
}
// TODO: we should move TypedSet into user state as well
Type keyType = mapType.getKeyType();
Type valueType = mapType.getValueType();
TypedSet typedSet = new TypedSet(keyType, entries / 2, FUNCTION_NAME);
BlockBuilder mapBlockBuilder = pageBuilder.getBlockBuilder(0);
BlockBuilder blockBuilder = mapBlockBuilder.beginBlockEntry();
// the last map
Block map = maps[lastMapIndex];
for (int i = 0; i < map.getPositionCount(); i += 2) {
typedSet.add(map, i);
keyType.appendTo(map, i, blockBuilder);
valueType.appendTo(map, i + 1, blockBuilder);
}
// the map between the last and the first
for (int idx = lastMapIndex - 1; idx > firstMapIndex; idx--) {
map = maps[idx];
for (int i = 0; i < map.getPositionCount(); i += 2) {
if (!typedSet.contains(map, i)) {
typedSet.add(map, i);
keyType.appendTo(map, i, blockBuilder);
valueType.appendTo(map, i + 1, blockBuilder);
}
}
}
// the first map
map = maps[firstMapIndex];
for (int i = 0; i < map.getPositionCount(); i += 2) {
if (!typedSet.contains(map, i)) {
keyType.appendTo(map, i, blockBuilder);
valueType.appendTo(map, i + 1, blockBuilder);
}
}
mapBlockBuilder.closeEntry();
pageBuilder.declarePosition();
return mapType.getObject(mapBlockBuilder, mapBlockBuilder.getPositionCount() - 1);
}
use of io.prestosql.operator.aggregation.TypedSet in project hetu-core by openlookeng.
the class ArrayIntersectFunction method intersect.
@TypeParameter("E")
@SqlType("array(E)")
public Block intersect(@TypeParameter("E") Type type, @OperatorDependency(operator = IS_DISTINCT_FROM, returnType = BOOLEAN, argumentTypes = { "E", "E" }) MethodHandle elementIsDistinctFrom, @SqlType("array(E)") Block leftArray, @SqlType("array(E)") Block rightArray) {
Block inputLeftArray = leftArray;
Block inputRightArray = rightArray;
if (inputLeftArray.getPositionCount() < inputRightArray.getPositionCount()) {
Block tempArray = inputLeftArray;
inputLeftArray = inputRightArray;
inputRightArray = tempArray;
}
int leftPositionCount = inputLeftArray.getPositionCount();
int rightPositionCount = inputRightArray.getPositionCount();
if (rightPositionCount == 0) {
return inputRightArray;
}
if (pageBuilder.isFull()) {
pageBuilder.reset();
}
TypedSet rightTypedSet = new TypedSet(type, elementIsDistinctFrom, rightPositionCount, "array_intersect");
for (int i = 0; i < rightPositionCount; i++) {
rightTypedSet.add(inputRightArray, i);
}
BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(0);
// The intersected set can have at most rightPositionCount elements
TypedSet intersectTypedSet = new TypedSet(type, Optional.of(elementIsDistinctFrom), blockBuilder, rightPositionCount, "array_intersect");
for (int i = 0; i < leftPositionCount; i++) {
if (rightTypedSet.contains(inputLeftArray, i)) {
intersectTypedSet.add(inputLeftArray, i);
}
}
pageBuilder.declarePositions(intersectTypedSet.size());
return blockBuilder.getRegion(blockBuilder.getPositionCount() - intersectTypedSet.size(), intersectTypedSet.size());
}
use of io.prestosql.operator.aggregation.TypedSet in project hetu-core by openlookeng.
the class BenchmarkArrayDistinct method oldArrayDistinct.
@ScalarFunction
@SqlType("array(varchar)")
public static Block oldArrayDistinct(@SqlType("array(varchar)") Block array) {
if (array.getPositionCount() == 0) {
return array;
}
TypedSet typedSet = new TypedSet(VARCHAR, array.getPositionCount(), "old_array_distinct");
BlockBuilder distinctElementBlockBuilder = VARCHAR.createBlockBuilder(null, array.getPositionCount());
for (int i = 0; i < array.getPositionCount(); i++) {
if (!typedSet.contains(array, i)) {
typedSet.add(array, i);
VARCHAR.appendTo(array, i, distinctElementBlockBuilder);
}
}
return distinctElementBlockBuilder.build();
}
use of io.prestosql.operator.aggregation.TypedSet in project hetu-core by openlookeng.
the class ArrayDistinctFunction method distinct.
@TypeParameter("E")
@SqlType("array(E)")
public Block distinct(@TypeParameter("E") Type type, @OperatorDependency(operator = IS_DISTINCT_FROM, returnType = BOOLEAN, argumentTypes = { "E", "E" }) MethodHandle elementIsDistinctFrom, @SqlType("array(E)") Block array) {
if (array.getPositionCount() < 2) {
return array;
}
if (array.getPositionCount() == 2) {
boolean firstValueNull = array.isNull(0);
Object firstValue = firstValueNull ? defaultValue(type.getJavaType()) : readNativeValue(type, array, 0);
boolean secondValueNull = array.isNull(1);
Object secondValue = secondValueNull ? defaultValue(type.getJavaType()) : readNativeValue(type, array, 1);
boolean distinct;
try {
distinct = (boolean) elementIsDistinctFrom.invoke(firstValue, firstValueNull, secondValue, secondValueNull);
} catch (Throwable t) {
throw internalError(t);
}
if (distinct) {
return array;
}
return array.getSingleValueBlock(0);
}
TypedSet typedSet = new TypedSet(type, elementIsDistinctFrom, array.getPositionCount(), "array_distinct");
int distinctCount = 0;
if (pageBuilder.isFull()) {
pageBuilder.reset();
}
BlockBuilder distinctElementBlockBuilder = pageBuilder.getBlockBuilder(0);
for (int i = 0; i < array.getPositionCount(); i++) {
if (!typedSet.contains(array, i)) {
typedSet.add(array, i);
distinctCount++;
type.appendTo(array, i, distinctElementBlockBuilder);
}
}
pageBuilder.declarePositions(distinctCount);
return distinctElementBlockBuilder.getRegion(distinctElementBlockBuilder.getPositionCount() - distinctCount, distinctCount);
}
use of io.prestosql.operator.aggregation.TypedSet in project hetu-core by openlookeng.
the class MathFunctions method mapDotProduct.
private static double mapDotProduct(Block leftMap, Block rightMap) {
TypedSet rightMapKeys = new TypedSet(VARCHAR, rightMap.getPositionCount(), "cosine_similarity");
for (int i = 0; i < rightMap.getPositionCount(); i += 2) {
rightMapKeys.add(rightMap, i);
}
double result = 0.0;
for (int i = 0; i < leftMap.getPositionCount(); i += 2) {
int position = rightMapKeys.positionOf(leftMap, i);
if (position != -1) {
result += DOUBLE.getDouble(leftMap, i + 1) * DOUBLE.getDouble(rightMap, 2 * position + 1);
}
}
return result;
}
Aggregations