@Override public List<String> map(Types.MapType readMap, Supplier<List<String>> keyErrors, Supplier<List<String>> valueErrors) { if (!currentType.isMapType()) { return ImmutableList.of(String.format(": %s cannot be read as a map", currentType)); } Types.MapType map = currentType.asNestedType().asMapType(); List<String> errors = Lists.newArrayList(); try { if (readMap.isValueRequired() && map.isValueOptional()) { errors.add(": values should be required, but are optional"); } this.currentType = map.keyType(); errors.addAll(keyErrors.get()); this.currentType = map.valueType(); errors.addAll(valueErrors.get()); return errors; } finally { this.currentType = map; } }
(current.isMapType() && isKeyValueSchema(array.getElementType()))) { Preconditions.checkArgument(current.isMapType(), "Incompatible projected type: %s", current); Types.MapType m = current.asNestedType().asMapType(); this.current = Types.StructType.of(m.fields()); // create a struct to correspond to element try {
if (nested.isMapType()) { parentField = nested.asMapType().fields().get(1); } else if (nested.isListType()) {
Map<String, ?> asMap = mapAsJavaMapConverter( (scala.collection.Map<String, ?>) actual).asJava(); assertEqualsSafe(type.asNestedType().asMapType(), (Map<String, ?>) expected, asMap); break; case TIME:
Assert.assertTrue("Should expect a Map", expected instanceof Map); Assert.assertTrue("Should be an ArrayBasedMapData", actual instanceof MapData); assertEqualsUnsafe(type.asNestedType().asMapType(), (Map) expected, (MapData) actual); break; case TIME:
Types.MapType map = type.asNestedType().asMapType(); T keyResult; T valueResult;
Types.MapType map = type.asNestedType().asMapType(); return visitor.map(map, new VisitFuture<>(map.keyType(), visitor),
static void toJson(Type type, JsonGenerator generator) throws IOException { if (type.isPrimitiveType()) { toJson(type.asPrimitiveType(), generator); } else { Type.NestedType nested = type.asNestedType(); switch (type.typeId()) { case STRUCT: toJson(nested.asStructType(), generator); break; case LIST: toJson(nested.asListType(), generator); break; case MAP: toJson(nested.asMapType(), generator); break; default: throw new IllegalArgumentException("Cannot write unknown type: " + type); } } }
public Type field(NestedField field) { Type.Repetition repetition = field.isOptional() ? Type.Repetition.OPTIONAL : Type.Repetition.REQUIRED; int id = field.fieldId(); String name = field.name(); if (field.type().isPrimitiveType()) { return primitive(field.type().asPrimitiveType(), repetition, id, name); } else { NestedType nested = field.type().asNestedType(); if (nested.isStructType()) { return struct(nested.asStructType(), repetition, id, name); } else if (nested.isMapType()) { return map(nested.asMapType(), repetition, id, name); } else if (nested.isListType()) { return list(nested.asListType(), repetition, id, name); } throw new UnsupportedOperationException("Can't convert unknown type: " + nested); } }
@Override public Schema map(Schema map, Supplier<Schema> value) { Preconditions.checkArgument(current.isNestedType() && current.asNestedType().isMapType(), "Incompatible projected type: %s", current); Types.MapType m = current.asNestedType().asMapType(); Preconditions.checkArgument(m.keyType() == Types.StringType.get(), "Incompatible projected type: key type %s is not string", m.keyType()); this.current = m.valueType(); try { Schema valueSchema = value.get(); // element was changed, create a new map if (valueSchema != map.getValueType()) { return Schema.createMap(valueSchema); } return map; } finally { this.current = m; } }
@Test public void testMaps() throws Exception { Type[] maps = new Type[] { Types.MapType.ofOptional(1, 2, Types.StringType.get(), Types.LongType.get()), Types.MapType.ofRequired(4, 5, Types.StringType.get(), Types.LongType.get()) }; for (Type map : maps) { Type copy = TestHelpers.roundTripSerialize(map); Assert.assertEquals("Map serialization should be equal to starting type", map, copy); Assert.assertSame("Map serialization should preserve identity type", Types.LongType.get(), map.asNestedType().asMapType().valueType()); } }