use of com.facebook.presto.common.type.MapType in project presto by prestodb.
the class TestMapUnionAggregation method testSimpleWithDuplicates.
@Test
public void testSimpleWithDuplicates() {
MapType mapType = mapType(DOUBLE, VARCHAR);
FunctionHandle functionHandle = FUNCTION_AND_TYPE_MANAGER.lookupFunction(NAME, fromTypes(mapType));
InternalAggregationFunction aggFunc = FUNCTION_AND_TYPE_MANAGER.getAggregateFunctionImplementation(functionHandle);
assertAggregation(aggFunc, ImmutableMap.of(23.0, "aaa", 33.0, "bbb", 43.0, "ccc", 53.0, "ddd", 13.0, "eee"), arrayBlockOf(mapType, mapBlockOf(DOUBLE, VARCHAR, ImmutableMap.of(23.0, "aaa", 33.0, "bbb", 53.0, "ddd")), mapBlockOf(DOUBLE, VARCHAR, ImmutableMap.of(43.0, "ccc", 53.0, "ddd", 13.0, "eee"))));
mapType = mapType(DOUBLE, BIGINT);
functionHandle = FUNCTION_AND_TYPE_MANAGER.lookupFunction(NAME, fromTypes(mapType));
aggFunc = FUNCTION_AND_TYPE_MANAGER.getAggregateFunctionImplementation(functionHandle);
assertAggregation(aggFunc, ImmutableMap.of(1.0, 99L, 2.0, 99L, 3.0, 99L, 4.0, 44L), arrayBlockOf(mapType, mapBlockOf(DOUBLE, BIGINT, ImmutableMap.of(1.0, 99L, 2.0, 99L, 3.0, 99L)), mapBlockOf(DOUBLE, BIGINT, ImmutableMap.of(1.0, 44L, 2.0, 44L, 4.0, 44L))));
mapType = mapType(BOOLEAN, BIGINT);
functionHandle = FUNCTION_AND_TYPE_MANAGER.lookupFunction(NAME, fromTypes(mapType));
aggFunc = FUNCTION_AND_TYPE_MANAGER.getAggregateFunctionImplementation(functionHandle);
assertAggregation(aggFunc, ImmutableMap.of(false, 12L, true, 13L), arrayBlockOf(mapType, mapBlockOf(BOOLEAN, BIGINT, ImmutableMap.of(false, 12L)), mapBlockOf(BOOLEAN, BIGINT, ImmutableMap.of(true, 13L, false, 33L))));
}
use of com.facebook.presto.common.type.MapType in project presto by prestodb.
the class TestSingleMapBlockWriter method testSizeInBytesForNulls.
@Test
public void testSizeInBytesForNulls() {
MapType innerMapType = new MapType(BIGINT, VARCHAR, MethodHandleUtil.methodHandle(TestSingleMapBlockWriter.class, "throwUnsupportedOperation"), MethodHandleUtil.methodHandle(TestSingleMapBlockWriter.class, "throwUnsupportedOperation"));
MapType mapType = new MapType(BIGINT, innerMapType, MethodHandleUtil.methodHandle(TestSingleMapBlockWriter.class, "throwUnsupportedOperation"), MethodHandleUtil.methodHandle(TestSingleMapBlockWriter.class, "throwUnsupportedOperation"));
MapBlockBuilder mapBlockBuilder = (MapBlockBuilder) mapType.createBlockBuilder(null, MAP_POSITIONS);
for (int i = 0; i < MAP_POSITIONS; i++) {
SingleMapBlockWriter singleMapBlockWriter = mapBlockBuilder.beginBlockEntry();
int expectedSize = 0;
for (int j = 0; j < 10; j++) {
assertEquals(singleMapBlockWriter.getSizeInBytes(), expectedSize, "For Index: " + i);
// Write Map's Key (long + isNull)
BIGINT.writeLong(singleMapBlockWriter, j);
expectedSize += 9;
assertEquals(singleMapBlockWriter.getSizeInBytes(), expectedSize, "For Index: " + i);
// Write Map<Bigint, Varchar>
BlockBuilder innerMapWriter = singleMapBlockWriter.beginBlockEntry();
// Opening entry, does not account for size.
assertEquals(singleMapBlockWriter.getSizeInBytes(), expectedSize, "For Index: " + i);
// Each entry is of 22 bytes, with 6 byte value.
BIGINT.writeLong(innerMapWriter, i * 2);
innerMapWriter.appendNull();
expectedSize += 22;
assertEquals(singleMapBlockWriter.getSizeInBytes(), expectedSize, "For Index: " + i);
// Another entry with 22 bytes.
BIGINT.writeLong(innerMapWriter, i * 2 + 1);
innerMapWriter.appendNull();
expectedSize += 22;
assertEquals(singleMapBlockWriter.getSizeInBytes(), expectedSize, "For Index: " + i);
// closing the entry increases by 5 (offset + null)
singleMapBlockWriter.closeEntry();
expectedSize += 5;
assertEquals(singleMapBlockWriter.getSizeInBytes(), expectedSize, "For Index: " + i);
}
mapBlockBuilder.closeEntry();
assertEquals(singleMapBlockWriter.getSizeInBytes(), expectedSize, "For Index: " + i);
}
}
use of com.facebook.presto.common.type.MapType in project presto by prestodb.
the class ExpressionConverter method toIcebergExpression.
private static Expression toIcebergExpression(String columnName, Type type, Domain domain) {
if (domain.isAll()) {
return alwaysTrue();
}
if (domain.getValues().isNone()) {
return domain.isNullAllowed() ? isNull(columnName) : alwaysFalse();
}
if (domain.getValues().isAll()) {
return domain.isNullAllowed() ? alwaysTrue() : not(isNull(columnName));
}
// Skip structural types. TODO: Evaluate Apache Iceberg's support for predicate on structural types
if (type instanceof ArrayType || type instanceof MapType || type instanceof RowType) {
return alwaysTrue();
}
ValueSet domainValues = domain.getValues();
Expression expression = null;
if (domain.isNullAllowed()) {
expression = isNull(columnName);
}
if (domainValues instanceof SortedRangeSet) {
List<Range> orderedRanges = ((SortedRangeSet) domainValues).getOrderedRanges();
expression = firstNonNull(expression, alwaysFalse());
for (Range range : orderedRanges) {
Marker low = range.getLow();
Marker high = range.getHigh();
Marker.Bound lowBound = low.getBound();
Marker.Bound highBound = high.getBound();
// case col <> 'val' is represented as (col < 'val' or col > 'val')
if (lowBound == EXACTLY && highBound == EXACTLY) {
// case ==
if (getIcebergLiteralValue(type, low).equals(getIcebergLiteralValue(type, high))) {
expression = or(expression, equal(columnName, getIcebergLiteralValue(type, low)));
} else {
// case between
Expression between = and(greaterThanOrEqual(columnName, getIcebergLiteralValue(type, low)), lessThanOrEqual(columnName, getIcebergLiteralValue(type, high)));
expression = or(expression, between);
}
} else {
if (lowBound == EXACTLY && low.getValueBlock().isPresent()) {
// case >=
expression = or(expression, greaterThanOrEqual(columnName, getIcebergLiteralValue(type, low)));
} else if (lowBound == ABOVE && low.getValueBlock().isPresent()) {
// case >
expression = or(expression, greaterThan(columnName, getIcebergLiteralValue(type, low)));
}
if (highBound == EXACTLY && high.getValueBlock().isPresent()) {
// case <=
if (low.getValueBlock().isPresent()) {
expression = and(expression, lessThanOrEqual(columnName, getIcebergLiteralValue(type, high)));
} else {
expression = or(expression, lessThanOrEqual(columnName, getIcebergLiteralValue(type, high)));
}
} else if (highBound == BELOW && high.getValueBlock().isPresent()) {
// case <
if (low.getValueBlock().isPresent()) {
expression = and(expression, lessThan(columnName, getIcebergLiteralValue(type, high)));
} else {
expression = or(expression, lessThan(columnName, getIcebergLiteralValue(type, high)));
}
}
}
}
return expression;
}
throw new VerifyException("Did not expect a domain value set other than SortedRangeSet but got " + domainValues.getClass().getSimpleName());
}
use of com.facebook.presto.common.type.MapType in project presto by prestodb.
the class MapConstructor method specialize.
@Override
public BuiltInScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, FunctionAndTypeManager functionAndTypeManager) {
Type keyType = boundVariables.getTypeVariable("K");
Type valueType = boundVariables.getTypeVariable("V");
Type mapType = functionAndTypeManager.getParameterizedType(MAP, ImmutableList.of(TypeSignatureParameter.of(keyType.getTypeSignature()), TypeSignatureParameter.of(valueType.getTypeSignature())));
MethodHandle keyNativeHashCode = functionAndTypeManager.getJavaScalarFunctionImplementation(functionAndTypeManager.resolveOperator(OperatorType.HASH_CODE, fromTypes(keyType))).getMethodHandle();
MethodHandle keyBlockHashCode = compose(keyNativeHashCode, nativeValueGetter(keyType));
MethodHandle keyNativeEquals = functionAndTypeManager.getJavaScalarFunctionImplementation(functionAndTypeManager.resolveOperator(OperatorType.EQUAL, fromTypes(keyType, keyType))).getMethodHandle();
MethodHandle keyBlockEquals = compose(keyNativeEquals, nativeValueGetter(keyType), nativeValueGetter(keyType));
MethodHandle keyIndeterminate = functionAndTypeManager.getJavaScalarFunctionImplementation(functionAndTypeManager.resolveOperator(INDETERMINATE, fromTypeSignatures((keyType.getTypeSignature())))).getMethodHandle();
return new BuiltInScalarFunctionImplementation(false, ImmutableList.of(valueTypeArgumentProperty(RETURN_NULL_ON_NULL), valueTypeArgumentProperty(RETURN_NULL_ON_NULL)), METHOD_HANDLE.bindTo(mapType).bindTo(keyType).bindTo(valueType).bindTo(keyBlockEquals).bindTo(keyBlockHashCode).bindTo(keyIndeterminate), Optional.empty());
}
use of com.facebook.presto.common.type.MapType in project presto by prestodb.
the class MapFromEntriesFunction method mapFromEntries.
@TypeParameter("K")
@TypeParameter("V")
@SqlType("map(K,V)")
@SqlNullable
public Block mapFromEntries(@TypeParameter("map(K,V)") MapType mapType, SqlFunctionProperties properties, @SqlType("array(row(K,V))") Block block) {
Type keyType = mapType.getKeyType();
Type valueType = mapType.getValueType();
RowType rowType = RowType.anonymous(ImmutableList.of(keyType, valueType));
int entryCount = block.getPositionCount();
BlockBuilder mapBlockBuilder = mapType.createBlockBuilder(null, block.getPositionCount());
BlockBuilder resultBuilder = mapBlockBuilder.beginBlockEntry();
TypedSet uniqueKeys = new TypedSet(keyType, entryCount, "map_from_entries");
for (int i = 0; i < entryCount; i++) {
if (block.isNull(i)) {
mapBlockBuilder.closeEntry();
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "map entry cannot be null");
}
Block rowBlock = rowType.getObject(block, i);
if (rowBlock.isNull(0)) {
mapBlockBuilder.closeEntry();
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "map key cannot be null");
}
if (uniqueKeys.contains(rowBlock, 0)) {
mapBlockBuilder.closeEntry();
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Duplicate keys (%s) are not allowed", keyType.getObjectValue(properties, rowBlock, 0)));
}
uniqueKeys.add(rowBlock, 0);
keyType.appendTo(rowBlock, 0, resultBuilder);
valueType.appendTo(rowBlock, 1, resultBuilder);
}
mapBlockBuilder.closeEntry();
return mapType.getObject(mapBlockBuilder, mapBlockBuilder.getPositionCount() - 1);
}
Aggregations