use of io.prestosql.spi.type.MapType in project hetu-core by openlookeng.
the class MapFromEntriesFunction method mapFromEntries.
@TypeParameter("K")
@TypeParameter("V")
@SqlType("map(K,V)")
@SqlNullable
public Block mapFromEntries(@TypeParameter("map(K,V)") MapType mapType, ConnectorSession session, @SqlType("array(row(K,V))") Block block) {
Type keyType = mapType.getKeyType();
Type valueType = mapType.getValueType();
RowType rowType = RowType.anonymous(ImmutableList.of(keyType, valueType));
if (pageBuilder.isFull()) {
pageBuilder.reset();
}
int entryCount = block.getPositionCount();
BlockBuilder mapBlockBuilder = pageBuilder.getBlockBuilder(0);
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();
pageBuilder.declarePosition();
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "map entry cannot be null");
}
Block rowBlock = rowType.getObject(block, i);
if (rowBlock.isNull(0)) {
mapBlockBuilder.closeEntry();
pageBuilder.declarePosition();
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "map key cannot be null");
}
if (uniqueKeys.contains(rowBlock, 0)) {
mapBlockBuilder.closeEntry();
pageBuilder.declarePosition();
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, String.format("Duplicate keys (%s) are not allowed", keyType.getObjectValue(session, rowBlock, 0)));
}
uniqueKeys.add(rowBlock, 0);
keyType.appendTo(rowBlock, 0, resultBuilder);
valueType.appendTo(rowBlock, 1, resultBuilder);
}
mapBlockBuilder.closeEntry();
pageBuilder.declarePosition();
return mapType.getObject(mapBlockBuilder, mapBlockBuilder.getPositionCount() - 1);
}
use of io.prestosql.spi.type.MapType in project hetu-core by openlookeng.
the class TestMapAggAggregation method testDoubleMapMap.
@Test
public void testDoubleMapMap() {
MapType innerMapType = mapType(VARCHAR, VARCHAR);
MapType mapType = mapType(DOUBLE, innerMapType);
InternalAggregationFunction aggFunc = metadata.getFunctionAndTypeManager().getAggregateFunctionImplementation(new Signature(QualifiedObjectName.valueOfDefaultFunction(NAME), AGGREGATE, mapType.getTypeSignature(), parseTypeSignature(StandardTypes.DOUBLE), innerMapType.getTypeSignature()));
BlockBuilder builder = innerMapType.createBlockBuilder(null, 3);
innerMapType.writeObject(builder, mapBlockOf(VARCHAR, VARCHAR, ImmutableMap.of("a", "b")));
innerMapType.writeObject(builder, mapBlockOf(VARCHAR, VARCHAR, ImmutableMap.of("c", "d")));
innerMapType.writeObject(builder, mapBlockOf(VARCHAR, VARCHAR, ImmutableMap.of("e", "f")));
assertAggregation(aggFunc, ImmutableMap.of(1.0, ImmutableMap.of("a", "b"), 2.0, ImmutableMap.of("c", "d"), 3.0, ImmutableMap.of("e", "f")), createDoublesBlock(1.0, 2.0, 3.0), builder.build());
}
use of io.prestosql.spi.type.MapType in project hetu-core by openlookeng.
the class TestMapAggAggregation method testDoubleRowMap.
@Test
public void testDoubleRowMap() {
RowType innerRowType = RowType.from(ImmutableList.of(RowType.field("f1", INTEGER), RowType.field("f2", DOUBLE)));
MapType mapType = mapType(DOUBLE, innerRowType);
InternalAggregationFunction aggFunc = metadata.getFunctionAndTypeManager().getAggregateFunctionImplementation(new Signature(QualifiedObjectName.valueOfDefaultFunction(NAME), AGGREGATE, mapType.getTypeSignature(), parseTypeSignature(StandardTypes.DOUBLE), innerRowType.getTypeSignature()));
BlockBuilder builder = innerRowType.createBlockBuilder(null, 3);
innerRowType.writeObject(builder, toRow(ImmutableList.of(INTEGER, DOUBLE), 1L, 1.0));
innerRowType.writeObject(builder, toRow(ImmutableList.of(INTEGER, DOUBLE), 2L, 2.0));
innerRowType.writeObject(builder, toRow(ImmutableList.of(INTEGER, DOUBLE), 3L, 3.0));
assertAggregation(aggFunc, ImmutableMap.of(1.0, ImmutableList.of(1, 1.0), 2.0, ImmutableList.of(2, 2.0), 3.0, ImmutableList.of(3, 3.0)), createDoublesBlock(1.0, 2.0, 3.0), builder.build());
}
use of io.prestosql.spi.type.MapType in project hetu-core by openlookeng.
the class TestMapAggAggregation method testDoubleArrayMap.
@Test
public void testDoubleArrayMap() {
ArrayType arrayType = new ArrayType(VARCHAR);
MapType mapType = mapType(DOUBLE, arrayType);
InternalAggregationFunction aggFunc = metadata.getFunctionAndTypeManager().getAggregateFunctionImplementation(new Signature(QualifiedObjectName.valueOfDefaultFunction(NAME), AGGREGATE, mapType.getTypeSignature(), parseTypeSignature(StandardTypes.DOUBLE), arrayType.getTypeSignature()));
assertAggregation(aggFunc, ImmutableMap.of(1.0, ImmutableList.of("a", "b"), 2.0, ImmutableList.of("c", "d"), 3.0, ImmutableList.of("e", "f")), createDoublesBlock(1.0, 2.0, 3.0), createStringArraysBlock(ImmutableList.of(ImmutableList.of("a", "b"), ImmutableList.of("c", "d"), ImmutableList.of("e", "f"))));
}
use of io.prestosql.spi.type.MapType in project hetu-core by openlookeng.
the class TestMapUnionAggregation method testSimpleWithNulls.
@Test
public void testSimpleWithNulls() {
MapType mapType = mapType(DOUBLE, VARCHAR);
InternalAggregationFunction aggFunc = metadata.getFunctionAndTypeManager().getAggregateFunctionImplementation(new Signature(QualifiedObjectName.valueOfDefaultFunction(NAME), AGGREGATE, mapType.getTypeSignature(), mapType.getTypeSignature()));
Map<Object, Object> expected = mapOf(23.0, "aaa", 33.0, null, 43.0, "ccc", 53.0, "ddd");
assertAggregation(aggFunc, expected, arrayBlockOf(mapType, mapBlockOf(DOUBLE, VARCHAR, mapOf(23.0, "aaa", 33.0, null, 53.0, "ddd")), null, mapBlockOf(DOUBLE, VARCHAR, mapOf(43.0, "ccc", 53.0, "ddd"))));
}
Aggregations