use of io.prestosql.spi.type.MapType in project hetu-core by openlookeng.
the class ParquetReader method readMap.
private ColumnChunk readMap(GroupField field) throws IOException {
List<Type> parameters = field.getType().getTypeParameters();
checkArgument(parameters.size() == 2, "Maps must have two type parameters, found %s", parameters.size());
Block[] localBlocks = new Block[parameters.size()];
ColumnChunk columnChunk = readColumnChunk(field.getChildren().get(0).get());
localBlocks[0] = columnChunk.getBlock();
localBlocks[1] = readColumnChunk(field.getChildren().get(1).get()).getBlock();
IntList offsets = new IntArrayList();
BooleanList valueIsNull = new BooleanArrayList();
calculateCollectionOffsets(field, offsets, valueIsNull, columnChunk.getDefinitionLevels(), columnChunk.getRepetitionLevels());
Block mapBlock = ((MapType) field.getType()).createBlockFromKeyValue(Optional.of(valueIsNull.toBooleanArray()), offsets.toIntArray(), localBlocks[0], localBlocks[1]);
return new ColumnChunk(mapBlock, columnChunk.getDefinitionLevels(), columnChunk.getRepetitionLevels());
}
use of io.prestosql.spi.type.MapType in project hetu-core by openlookeng.
the class TestMapOperators method assertMapHashOperator.
private void assertMapHashOperator(String inputString, Type keyType, Type valueType, List<Object> elements) {
checkArgument(elements.size() % 2 == 0, "the size of elements should be even number");
MapType mapType = mapType(keyType, valueType);
BlockBuilder mapArrayBuilder = mapType.createBlockBuilder(null, 1);
BlockBuilder singleMapWriter = mapArrayBuilder.beginBlockEntry();
for (int i = 0; i < elements.size(); i += 2) {
appendToBlockBuilder(keyType, elements.get(i), singleMapWriter);
appendToBlockBuilder(valueType, elements.get(i + 1), singleMapWriter);
}
mapArrayBuilder.closeEntry();
long hashResult = mapType.hash(mapArrayBuilder.build(), 0);
assertOperator(HASH_CODE, inputString, BIGINT, hashResult);
}
use of io.prestosql.spi.type.MapType in project hetu-core by openlookeng.
the class TestMapOperators method testMapEntries.
@Test
public void testMapEntries() {
Type unknownEntryType = entryType(UNKNOWN, UNKNOWN);
assertFunction("map_entries(null)", unknownEntryType, null);
assertFunction("map_entries(MAP(ARRAY[], null))", unknownEntryType, null);
assertFunction("map_entries(MAP(null, ARRAY[]))", unknownEntryType, null);
assertFunction("map_entries(MAP(ARRAY[1, 2, 3], null))", entryType(INTEGER, UNKNOWN), null);
assertFunction("map_entries(MAP(null, ARRAY[1, 2, 3]))", entryType(UNKNOWN, INTEGER), null);
assertFunction("map_entries(MAP(ARRAY[], ARRAY[]))", unknownEntryType, ImmutableList.of());
assertFunction("map_entries(MAP(ARRAY[1], ARRAY['x']))", entryType(INTEGER, createVarcharType(1)), ImmutableList.of(ImmutableList.of(1, "x")));
assertFunction("map_entries(MAP(ARRAY[1, 2], ARRAY['x', 'y']))", entryType(INTEGER, createVarcharType(1)), ImmutableList.of(ImmutableList.of(1, "x"), ImmutableList.of(2, "y")));
assertFunction("map_entries(MAP(ARRAY['x', 'y'], ARRAY[ARRAY[1, 2], ARRAY[3, 4]]))", entryType(createVarcharType(1), new ArrayType(INTEGER)), ImmutableList.of(ImmutableList.of("x", ImmutableList.of(1, 2)), ImmutableList.of("y", ImmutableList.of(3, 4))));
assertFunction("map_entries(MAP(ARRAY[ARRAY[1.0E0, 2.0E0], ARRAY[3.0E0, 4.0E0]], ARRAY[5.0E0, 6.0E0]))", entryType(new ArrayType(DOUBLE), DOUBLE), ImmutableList.of(ImmutableList.of(ImmutableList.of(1.0, 2.0), 5.0), ImmutableList.of(ImmutableList.of(3.0, 4.0), 6.0)));
assertFunction("map_entries(MAP(ARRAY['x', 'y'], ARRAY[MAP(ARRAY[1], ARRAY[2]), MAP(ARRAY[3], ARRAY[4])]))", entryType(createVarcharType(1), mapType(INTEGER, INTEGER)), ImmutableList.of(ImmutableList.of("x", ImmutableMap.of(1, 2)), ImmutableList.of("y", ImmutableMap.of(3, 4))));
assertFunction("map_entries(MAP(ARRAY[MAP(ARRAY[1], ARRAY[2]), MAP(ARRAY[3], ARRAY[4])], ARRAY['x', 'y']))", entryType(mapType(INTEGER, INTEGER), createVarcharType(1)), ImmutableList.of(ImmutableList.of(ImmutableMap.of(1, 2), "x"), ImmutableList.of(ImmutableMap.of(3, 4), "y")));
// null values
List<Object> expectedEntries = ImmutableList.of(asList("x", null), asList("y", null));
assertFunction("map_entries(MAP(ARRAY['x', 'y'], ARRAY[null, null]))", entryType(createVarcharType(1), UNKNOWN), expectedEntries);
assertCachedInstanceHasBoundedRetainedSize("map_entries(MAP(ARRAY[1, 2], ARRAY['x', 'y']))");
}
use of io.prestosql.spi.type.MapType in project hetu-core by openlookeng.
the class AbstractTestType method getNonNullValueForType.
/**
* @return a non-null value, represented in native container type
*/
private static Object getNonNullValueForType(Type type) {
if (type.getJavaType() == boolean.class) {
return true;
}
if (type.getJavaType() == long.class) {
return 1L;
}
if (type.getJavaType() == double.class) {
return 1.0;
}
if (type.getJavaType() == Slice.class) {
return Slices.utf8Slice("_");
}
if (type instanceof ArrayType) {
ArrayType arrayType = (ArrayType) type;
Type elementType = arrayType.getElementType();
Object elementNonNullValue = getNonNullValueForType(elementType);
return arrayBlockOf(elementType, elementNonNullValue);
}
if (type instanceof MapType) {
MapType mapType = (MapType) type;
Type keyType = mapType.getKeyType();
Type valueType = mapType.getValueType();
Object keyNonNullValue = getNonNullValueForType(keyType);
Object valueNonNullValue = getNonNullValueForType(valueType);
Map<?, ?> map = ImmutableMap.of(keyNonNullValue, valueNonNullValue);
return mapBlockOf(keyType, valueType, map);
}
if (type instanceof RowType) {
RowType rowType = (RowType) type;
List<Type> elementTypes = rowType.getTypeParameters();
Object[] elementNonNullValues = elementTypes.stream().map(AbstractTestType::getNonNullValueForType).toArray(Object[]::new);
return toRow(elementTypes, elementNonNullValues);
}
throw new IllegalStateException("Unsupported Java type " + type.getJavaType() + " (for type " + type + ")");
}
use of io.prestosql.spi.type.MapType in project hetu-core by openlookeng.
the class TestArrayOperators method testArrayHashOperator.
@Test
public void testArrayHashOperator() {
assertArrayHashOperator("ARRAY[1, 2]", INTEGER, ImmutableList.of(1, 2));
assertArrayHashOperator("ARRAY[true, false]", BOOLEAN, ImmutableList.of(true, false));
// test with ARRAY[ MAP( ARRAY[1], ARRAY[2] ) ]
MapType mapType = mapType(INTEGER, INTEGER);
assertArrayHashOperator("ARRAY[MAP(ARRAY[1], ARRAY[2])]", mapType, ImmutableList.of(mapBlockOf(INTEGER, INTEGER, ImmutableMap.of(1L, 2L))));
}
Aggregations