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);
}
}
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);
}
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;
}
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());
}
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);
}
Aggregations