Search in sources :

Example 66 with MapType

use of io.trino.spi.type.MapType in project trino by trinodb.

the class RcFileTester method assertColumnValueEquals.

private static void assertColumnValueEquals(Type type, Object actual, Object expected) {
    if (actual == null) {
        assertNull(expected);
        return;
    }
    if (type instanceof ArrayType) {
        List<?> actualArray = (List<?>) actual;
        List<?> expectedArray = (List<?>) expected;
        assertEquals(actualArray.size(), expectedArray.size());
        Type elementType = type.getTypeParameters().get(0);
        for (int i = 0; i < actualArray.size(); i++) {
            Object actualElement = actualArray.get(i);
            Object expectedElement = expectedArray.get(i);
            assertColumnValueEquals(elementType, actualElement, expectedElement);
        }
    } else if (type instanceof MapType) {
        Map<?, ?> actualMap = (Map<?, ?>) actual;
        Map<?, ?> expectedMap = (Map<?, ?>) expected;
        assertEquals(actualMap.size(), expectedMap.size());
        Type keyType = type.getTypeParameters().get(0);
        Type valueType = type.getTypeParameters().get(1);
        List<Entry<?, ?>> expectedEntries = new ArrayList<>(expectedMap.entrySet());
        for (Entry<?, ?> actualEntry : actualMap.entrySet()) {
            Iterator<Entry<?, ?>> iterator = expectedEntries.iterator();
            while (iterator.hasNext()) {
                Entry<?, ?> expectedEntry = iterator.next();
                try {
                    assertColumnValueEquals(keyType, actualEntry.getKey(), expectedEntry.getKey());
                    assertColumnValueEquals(valueType, actualEntry.getValue(), expectedEntry.getValue());
                    iterator.remove();
                } catch (AssertionError ignored) {
                }
            }
        }
        assertTrue(expectedEntries.isEmpty(), "Unmatched entries " + expectedEntries);
    } else if (type instanceof RowType) {
        List<Type> fieldTypes = type.getTypeParameters();
        List<?> actualRow = (List<?>) actual;
        List<?> expectedRow = (List<?>) expected;
        assertEquals(actualRow.size(), fieldTypes.size());
        assertEquals(actualRow.size(), expectedRow.size());
        for (int fieldId = 0; fieldId < actualRow.size(); fieldId++) {
            Type fieldType = fieldTypes.get(fieldId);
            Object actualElement = actualRow.get(fieldId);
            Object expectedElement = expectedRow.get(fieldId);
            assertColumnValueEquals(fieldType, actualElement, expectedElement);
        }
    } else if (type.equals(DOUBLE)) {
        Double actualDouble = (Double) actual;
        Double expectedDouble = (Double) expected;
        if (actualDouble.isNaN()) {
            assertTrue(expectedDouble.isNaN(), "expected double to be NaN");
        } else {
            assertEquals(actualDouble, expectedDouble, 0.001);
        }
    } else if (!Objects.equals(actual, expected)) {
        assertEquals(actual, expected);
    }
}
Also used : RowType(io.trino.spi.type.RowType) MapType(io.trino.spi.type.MapType) ArrayType(io.trino.spi.type.ArrayType) MapType(io.trino.spi.type.MapType) RowType(io.trino.spi.type.RowType) ArrayType(io.trino.spi.type.ArrayType) DecimalType(io.trino.spi.type.DecimalType) Type(io.trino.spi.type.Type) VarcharType(io.trino.spi.type.VarcharType) Entry(java.util.Map.Entry) AbstractIterator(com.google.common.collect.AbstractIterator) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Collectors.toList(java.util.stream.Collectors.toList) StructObject(org.apache.hadoop.hive.serde2.StructObject) Map(java.util.Map) LazyMap(org.apache.hadoop.hive.serde2.lazy.LazyMap) ImmutableMap(com.google.common.collect.ImmutableMap) LazyBinaryMap(org.apache.hadoop.hive.serde2.lazybinary.LazyBinaryMap) HashMap(java.util.HashMap)

Example 67 with MapType

use of io.trino.spi.type.MapType in project trino by trinodb.

the class RcFileTester method getJavaObjectInspector.

private static ObjectInspector getJavaObjectInspector(Type type) {
    if (type.equals(BOOLEAN)) {
        return javaBooleanObjectInspector;
    }
    if (type.equals(BIGINT)) {
        return javaLongObjectInspector;
    }
    if (type.equals(INTEGER)) {
        return javaIntObjectInspector;
    }
    if (type.equals(SMALLINT)) {
        return javaShortObjectInspector;
    }
    if (type.equals(TINYINT)) {
        return javaByteObjectInspector;
    }
    if (type.equals(REAL)) {
        return javaFloatObjectInspector;
    }
    if (type.equals(DOUBLE)) {
        return javaDoubleObjectInspector;
    }
    if (type instanceof VarcharType) {
        return javaStringObjectInspector;
    }
    if (type.equals(VARBINARY)) {
        return javaByteArrayObjectInspector;
    }
    if (type.equals(DATE)) {
        return javaDateObjectInspector;
    }
    if (type.equals(TIMESTAMP_MILLIS)) {
        return javaTimestampObjectInspector;
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        return getPrimitiveJavaObjectInspector(new DecimalTypeInfo(decimalType.getPrecision(), decimalType.getScale()));
    }
    if (type instanceof ArrayType) {
        return ObjectInspectorFactory.getStandardListObjectInspector(getJavaObjectInspector(type.getTypeParameters().get(0)));
    }
    if (type instanceof MapType) {
        ObjectInspector keyObjectInspector = getJavaObjectInspector(type.getTypeParameters().get(0));
        ObjectInspector valueObjectInspector = getJavaObjectInspector(type.getTypeParameters().get(1));
        return ObjectInspectorFactory.getStandardMapObjectInspector(keyObjectInspector, valueObjectInspector);
    }
    if (type instanceof RowType) {
        return getStandardStructObjectInspector(type.getTypeSignature().getParameters().stream().map(parameter -> parameter.getNamedTypeSignature().getName().get()).collect(toList()), type.getTypeParameters().stream().map(RcFileTester::getJavaObjectInspector).collect(toList()));
    }
    throw new IllegalArgumentException("unsupported type: " + type);
}
Also used : DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) ArrayType(io.trino.spi.type.ArrayType) PrimitiveObjectInspectorFactory.javaByteObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaByteObjectInspector) PrimitiveObjectInspectorFactory.javaLongObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaLongObjectInspector) PrimitiveObjectInspectorFactory.javaTimestampObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaTimestampObjectInspector) PrimitiveObjectInspectorFactory.javaDateObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaDateObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) PrimitiveObjectInspectorFactory.javaByteArrayObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaByteArrayObjectInspector) PrimitiveObjectInspectorFactory.javaFloatObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaFloatObjectInspector) PrimitiveObjectInspectorFactory.javaDoubleObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaDoubleObjectInspector) PrimitiveObjectInspectorFactory.javaIntObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaIntObjectInspector) PrimitiveObjectInspectorFactory.javaShortObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaShortObjectInspector) ObjectInspectorFactory.getStandardStructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory.getStandardStructObjectInspector) SettableStructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.SettableStructObjectInspector) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) PrimitiveObjectInspectorFactory.javaBooleanObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaBooleanObjectInspector) PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector) PrimitiveObjectInspectorFactory.javaStringObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaStringObjectInspector) VarcharType(io.trino.spi.type.VarcharType) DecimalType(io.trino.spi.type.DecimalType) RowType(io.trino.spi.type.RowType) MapType(io.trino.spi.type.MapType)

Example 68 with MapType

use of io.trino.spi.type.MapType in project trino by trinodb.

the class AvroColumnDecoder method isSupportedType.

private boolean isSupportedType(Type type) {
    if (isSupportedPrimitive(type)) {
        return true;
    }
    if (type instanceof ArrayType) {
        checkArgument(type.getTypeParameters().size() == 1, "expecting exactly one type parameter for array");
        return isSupportedType(type.getTypeParameters().get(0));
    }
    if (type instanceof MapType) {
        List<Type> typeParameters = type.getTypeParameters();
        checkArgument(typeParameters.size() == 2, "expecting exactly two type parameters for map");
        checkArgument(typeParameters.get(0) instanceof VarcharType, "Unsupported column type '%s' for map key", typeParameters.get(0));
        return isSupportedType(type.getTypeParameters().get(1));
    }
    if (type instanceof RowType) {
        for (Type fieldType : type.getTypeParameters()) {
            if (!isSupportedType(fieldType)) {
                return false;
            }
        }
        return true;
    }
    return false;
}
Also used : ArrayType(io.trino.spi.type.ArrayType) DoubleType(io.trino.spi.type.DoubleType) Type(io.trino.spi.type.Type) BooleanType(io.trino.spi.type.BooleanType) BigintType(io.trino.spi.type.BigintType) VarcharType(io.trino.spi.type.VarcharType) TinyintType(io.trino.spi.type.TinyintType) SmallintType(io.trino.spi.type.SmallintType) RowType(io.trino.spi.type.RowType) IntegerType(io.trino.spi.type.IntegerType) MapType(io.trino.spi.type.MapType) ArrayType(io.trino.spi.type.ArrayType) VarbinaryType(io.trino.spi.type.VarbinaryType) RealType(io.trino.spi.type.RealType) VarcharType(io.trino.spi.type.VarcharType) RowType(io.trino.spi.type.RowType) MapType(io.trino.spi.type.MapType)

Example 69 with MapType

use of io.trino.spi.type.MapType in project trino by trinodb.

the class MapParametricType method createType.

@Override
public Type createType(TypeManager typeManager, List<TypeParameter> parameters) {
    checkArgument(parameters.size() == 2, "Expected two parameters, got %s", parameters);
    TypeParameter firstParameter = parameters.get(0);
    TypeParameter secondParameter = parameters.get(1);
    checkArgument(firstParameter.getKind() == ParameterKind.TYPE && secondParameter.getKind() == ParameterKind.TYPE, "Expected key and type to be types, got %s", parameters);
    return new MapType(firstParameter.getType(), secondParameter.getType(), typeManager.getTypeOperators());
}
Also used : TypeParameter(io.trino.spi.type.TypeParameter) MapType(io.trino.spi.type.MapType)

Example 70 with MapType

use of io.trino.spi.type.MapType in project trino by trinodb.

the class ParquetSchemaConverter method getMapType.

private org.apache.parquet.schema.Type getMapType(MapType type, String name, List<String> parent) {
    parent = ImmutableList.<String>builder().addAll(parent).add(name).add("key_value").build();
    Type keyType = type.getKeyType();
    Type valueType = type.getValueType();
    return Types.map(OPTIONAL).key(convert(keyType, "key", parent)).value(convert(valueType, "value", parent)).named(name);
}
Also used : PrimitiveType(org.apache.parquet.schema.PrimitiveType) Type(io.trino.spi.type.Type) TimestampType(io.trino.spi.type.TimestampType) VarcharType(io.trino.spi.type.VarcharType) OriginalType(org.apache.parquet.schema.OriginalType) RowType(io.trino.spi.type.RowType) GroupType(org.apache.parquet.schema.GroupType) MapType(io.trino.spi.type.MapType) ArrayType(io.trino.spi.type.ArrayType) MessageType(org.apache.parquet.schema.MessageType) VarbinaryType(io.trino.spi.type.VarbinaryType) CharType(io.trino.spi.type.CharType) RealType(io.trino.spi.type.RealType) DecimalType(io.trino.spi.type.DecimalType)

Aggregations

MapType (io.trino.spi.type.MapType)85 Type (io.trino.spi.type.Type)45 ArrayType (io.trino.spi.type.ArrayType)42 RowType (io.trino.spi.type.RowType)38 BlockBuilder (io.trino.spi.block.BlockBuilder)28 VarcharType (io.trino.spi.type.VarcharType)26 Block (io.trino.spi.block.Block)17 DecimalType (io.trino.spi.type.DecimalType)17 Map (java.util.Map)17 Test (org.testng.annotations.Test)17 ImmutableList (com.google.common.collect.ImmutableList)16 List (java.util.List)14 TypeSignature.mapType (io.trino.spi.type.TypeSignature.mapType)13 CharType (io.trino.spi.type.CharType)12 ArrayList (java.util.ArrayList)12 HashMap (java.util.HashMap)11 TypeOperators (io.trino.spi.type.TypeOperators)10 ImmutableMap (com.google.common.collect.ImmutableMap)9 VarbinaryType (io.trino.spi.type.VarbinaryType)8 Collectors.toList (java.util.stream.Collectors.toList)8