use of io.trino.spi.type.MapType in project trino by trinodb.
the class TestHiveBucketing method toNativeContainerValue.
private static Object toNativeContainerValue(Type type, Object hiveValue) {
if (hiveValue == null) {
return null;
}
if (type instanceof ArrayType) {
BlockBuilder blockBuilder = type.createBlockBuilder(null, 1);
BlockBuilder subBlockBuilder = blockBuilder.beginBlockEntry();
for (Object subElement : (Iterable<?>) hiveValue) {
appendToBlockBuilder(type.getTypeParameters().get(0), subElement, subBlockBuilder);
}
blockBuilder.closeEntry();
return type.getObject(blockBuilder, 0);
}
if (type instanceof RowType) {
BlockBuilder blockBuilder = type.createBlockBuilder(null, 1);
BlockBuilder subBlockBuilder = blockBuilder.beginBlockEntry();
int field = 0;
for (Object subElement : (Iterable<?>) hiveValue) {
appendToBlockBuilder(type.getTypeParameters().get(field), subElement, subBlockBuilder);
field++;
}
blockBuilder.closeEntry();
return type.getObject(blockBuilder, 0);
}
if (type instanceof MapType) {
BlockBuilder blockBuilder = type.createBlockBuilder(null, 1);
BlockBuilder subBlockBuilder = blockBuilder.beginBlockEntry();
for (Entry<?, ?> entry : ((Map<?, ?>) hiveValue).entrySet()) {
appendToBlockBuilder(type.getTypeParameters().get(0), entry.getKey(), subBlockBuilder);
appendToBlockBuilder(type.getTypeParameters().get(1), entry.getValue(), subBlockBuilder);
}
blockBuilder.closeEntry();
return type.getObject(blockBuilder, 0);
}
if (type instanceof BooleanType) {
return hiveValue;
}
if (type instanceof TinyintType) {
return (long) (byte) hiveValue;
}
if (type instanceof SmallintType) {
return (long) (short) hiveValue;
}
if (type instanceof IntegerType) {
return (long) (int) hiveValue;
}
if (type instanceof BigintType) {
return hiveValue;
}
if (type instanceof RealType) {
return (long) Float.floatToRawIntBits((float) hiveValue);
}
if (type instanceof DoubleType) {
return hiveValue;
}
if (type instanceof VarcharType) {
return Slices.utf8Slice(hiveValue.toString());
}
if (type instanceof DateType) {
return (long) ((Date) hiveValue).toEpochDay();
}
throw new IllegalArgumentException("Unsupported bucketing type: " + type);
}
use of io.trino.spi.type.MapType in project trino by trinodb.
the class TestParquetPredicateUtils method testParquetTupleDomainMap.
@Test
public void testParquetTupleDomainMap() {
MapType mapType = new MapType(INTEGER, INTEGER, new TypeOperators());
HiveColumnHandle columnHandle = createBaseColumn("my_map", 0, HiveType.valueOf("map<int,int>"), mapType, REGULAR, Optional.empty());
TupleDomain<HiveColumnHandle> domain = withColumnDomains(ImmutableMap.of(columnHandle, Domain.notNull(mapType)));
MessageType fileSchema = new MessageType("hive_schema", new GroupType(OPTIONAL, "my_map", new GroupType(REPEATED, "map", new PrimitiveType(REQUIRED, INT32, "key"), new PrimitiveType(OPTIONAL, INT32, "value"))));
Map<List<String>, RichColumnDescriptor> descriptorsByPath = getDescriptors(fileSchema, fileSchema);
TupleDomain<ColumnDescriptor> tupleDomain = getParquetTupleDomain(descriptorsByPath, domain, fileSchema, true);
assertTrue(tupleDomain.isAll());
}
use of io.trino.spi.type.MapType in project trino by trinodb.
the class TestSimplePagesHashStrategy method testCompareSortChannelPositionsWithMapType.
@Test
public void testCompareSortChannelPositionsWithMapType() {
MapType mapType = new MapType(INTEGER, INTEGER, new TypeOperators());
Block block = mapType.createBlockFromKeyValue(Optional.empty(), new int[] { 0, 1 }, new IntArrayBlock(1, Optional.empty(), new int[] { 1234 }), new IntArrayBlock(1, Optional.empty(), new int[] { 5678 }));
SimplePagesHashStrategy strategy = createSimplePagesHashStrategy(mapType, ImmutableList.of(block));
// This fails because MapType is not orderable.
assertThatThrownBy(() -> strategy.compareSortChannelPositions(0, 0, 0, 0)).isInstanceOf(IllegalArgumentException.class).hasMessageContaining("type is not orderable");
}
use of io.trino.spi.type.MapType in project trino by trinodb.
the class TestSimplePagesHashStrategy method testRowEqualsRowWithMapType.
@Test
public void testRowEqualsRowWithMapType() {
MapType mapType = new MapType(INTEGER, INTEGER, new TypeOperators());
SimplePagesHashStrategy strategy = createSimplePagesHashStrategy(mapType, ImmutableList.of());
Page leftPage = new Page(mapType.createBlockFromKeyValue(Optional.empty(), new int[] { 0, 1 }, new IntArrayBlock(1, Optional.empty(), new int[] { 1234 }), new IntArrayBlock(1, Optional.empty(), new int[] { 5678 })));
Page rightPage1 = new Page(mapType.createBlockFromKeyValue(Optional.empty(), new int[] { 0, 1 }, new IntArrayBlock(1, Optional.empty(), new int[] { 1234 }), new IntArrayBlock(1, Optional.empty(), new int[] { 5678 })));
Page rightPage2 = new Page(mapType.createBlockFromKeyValue(Optional.empty(), new int[] { 0, 1 }, new IntArrayBlock(1, Optional.empty(), new int[] { 1234 }), new IntArrayBlock(1, Optional.empty(), new int[] { 1234 })));
// This works because MapType is comparable.
assertTrue(strategy.rowEqualsRow(0, leftPage, 0, rightPage1));
assertFalse(strategy.rowEqualsRow(0, leftPage, 0, rightPage2));
}
use of io.trino.spi.type.MapType in project trino by trinodb.
the class StructuralTestUtil method mapBlockOf.
public static Block mapBlockOf(Type keyType, Type valueType, Map<?, ?> value) {
MapType mapType = mapType(keyType, valueType);
BlockBuilder mapArrayBuilder = mapType.createBlockBuilder(null, 1);
BlockBuilder singleMapWriter = mapArrayBuilder.beginBlockEntry();
for (Map.Entry<?, ?> entry : value.entrySet()) {
appendToBlockBuilder(keyType, entry.getKey(), singleMapWriter);
appendToBlockBuilder(valueType, entry.getValue(), singleMapWriter);
}
mapArrayBuilder.closeEntry();
return mapType.getObject(mapArrayBuilder, 0);
}
Aggregations