/** * Creates a MapType by specifying the data type of keys ({@code keyType}) and values * ({@code keyType}). The field of {@code valueContainsNull} is set to {@code true}. */ public static MapType createMapType(DataType keyType, DataType valueType) { if (keyType == null) { throw new IllegalArgumentException("keyType should not be null."); } if (valueType == null) { throw new IllegalArgumentException("valueType should not be null."); } return new MapType(keyType, valueType, true); }
@Override public Type map(Types.MapType map, Supplier<Type> keyResult, Supplier<Type> valueResult) { Preconditions.checkArgument(current instanceof MapType, "Not a map: %s", current); MapType m = (MapType) current; Preconditions.checkArgument(m.valueContainsNull() || !map.isValueOptional(), "Cannot project a map of optional values as required values: %s", map); Preconditions.checkArgument(StringType.class.isInstance(m.keyType()), "Invalid map key type (not string): %s", m.keyType()); this.current = m.valueType(); try { Type valueType = valueResult.get(); if (map.valueType() == valueType) { return map; } if (map.isValueOptional()) { return Types.MapType.ofOptional(map.keyId(), map.valueId(), map.keyType(), valueType); } else { return Types.MapType.ofRequired(map.keyId(), map.valueId(), map.keyType(), valueType); } } finally { this.current = m; } }
@Override public Type map(Types.MapType map, Supplier<Type> keyResult, Supplier<Type> valueResult) { Preconditions.checkArgument(current instanceof MapType, "Not a map: %s", current); MapType m = (MapType) current; Preconditions.checkArgument(m.valueContainsNull() || !map.isValueOptional(), "Cannot project a map of optional values as required values: %s", map); this.current = m.valueType(); try { Type valueType = valueResult.get(); if (map.valueType() == valueType) { return map; } if (map.isValueOptional()) { return Types.MapType.ofOptional(map.keyId(), map.valueId(), map.keyType(), valueType); } else { return Types.MapType.ofRequired(map.keyId(), map.valueId(), map.keyType(), valueType); } } finally { this.current = m; } }
@Override public Type map(MapType map, Type keyType, Type valueType) { if (map.valueContainsNull()) { return Types.MapType.ofOptional(getNextId(), getNextId(), keyType, valueType); } else { return Types.MapType.ofRequired(getNextId(), getNextId(), keyType, valueType); } }
Object mapKey = toRowValue(entry.getKey(), mapType.keyType(), mapPath); if (mapKey == null) { throw new IllegalArgumentException("Null key is not allowed for map at " + mapPath); Object mapValue = toRowValue(entry.getValue(), mapType.valueType(), mapPath); if (mapValue == null && !mapType.valueContainsNull()) { throw new IllegalArgumentException("Null value is not allowed for map at " + mapPath);
MapType mapType = (MapType) dataType; typeSchema = typeBuilder.map().values(typeFor(mapType.valueType(), mapType.valueContainsNull(), recordCount)); break; case "struct":
Object mapKey = toRowValue(entry.getKey(), mapType.keyType(), mapPath); if (mapKey == null) { throw new IllegalArgumentException("Null key is not allowed for map at " + mapPath); Object mapValue = toRowValue(entry.getValue(), mapType.valueType(), mapPath); if (mapValue == null && !mapType.valueContainsNull()) { throw new IllegalArgumentException("Null value is not allowed for map at " + mapPath);
/** * Creates a MapType by specifying the data type of keys ({@code keyType}) and values * ({@code keyType}). The field of {@code valueContainsNull} is set to {@code true}. */ public static MapType createMapType(DataType keyType, DataType valueType) { if (keyType == null) { throw new IllegalArgumentException("keyType should not be null."); } if (valueType == null) { throw new IllegalArgumentException("valueType should not be null."); } return new MapType(keyType, valueType, true); }
Object mapKey = toRowValue(entry.getKey(), mapType.keyType(), mapPath); if (mapKey == null) { throw new IllegalArgumentException("Null key is not allowed for map at " + mapPath); Object mapValue = toRowValue(entry.getValue(), mapType.valueType(), mapPath); if (mapValue == null && !mapType.valueContainsNull()) { throw new IllegalArgumentException("Null value is not allowed for map at " + mapPath);
static <T> T visit(DataType type, SparkTypeVisitor<T> visitor) { if (type instanceof StructType) { StructField[] fields = ((StructType) type).fields(); List<T> fieldResults = Lists.newArrayListWithExpectedSize(fields.length); for (StructField field : fields) { fieldResults.add(visitor.field( field, visit(field.dataType(), visitor))); } return visitor.struct((StructType) type, fieldResults); } else if (type instanceof MapType) { return visitor.map((MapType) type, visit(((MapType) type).keyType(), visitor), visit(((MapType) type).valueType(), visitor)); } else if (type instanceof ArrayType) { return visitor.array( (ArrayType) type, visit(((ArrayType) type).elementType(), visitor)); } else if (type instanceof UserDefinedType){ throw new UnsupportedOperationException( "User-defined types are not supported"); } else { return visitor.atomic(type); } }
/** * Creates a MapType by specifying the data type of keys ({@code keyType}) and values * ({@code keyType}). The field of {@code valueContainsNull} is set to {@code true}. */ public static MapType createMapType(DataType keyType, DataType valueType) { if (keyType == null) { throw new IllegalArgumentException("keyType should not be null."); } if (valueType == null) { throw new IllegalArgumentException("valueType should not be null."); } return new MapType(keyType, valueType, true); }
DataType keyType = mapType.keyType(); DataType valueType = mapType.valueType(); HashMap<Object, Object> hashMap = new HashMap<>(); type, valueType, value)); } else if (!mapType.valueContainsNull()) { throw new RuntimeException(String.format("Type[%s] - Value cannot be 'null': %s[null]", type, valueType)); type, valueType, value)); } else if (!mapType.valueContainsNull()) { throw new RuntimeException(String.format("Type[%s] - Value cannot be 'null': %s[null]", type, valueType));
/** * Creates a MapType by specifying the data type of keys ({@code keyType}), the data type of * values ({@code keyType}), and whether values contain any null value * ({@code valueContainsNull}). */ public static MapType createMapType( DataType keyType, DataType valueType, boolean valueContainsNull) { if (keyType == null) { throw new IllegalArgumentException("keyType should not be null."); } if (valueType == null) { throw new IllegalArgumentException("valueType should not be null."); } return new MapType(keyType, valueType, valueContainsNull); }
Schema valueSchema = dataTypeToSchema(mapType.valueType(), recordCounter); return Schema.mapOf(dataTypeToSchema(mapType.keyType(), recordCounter), mapType.valueContainsNull() ? Schema.nullableOf(valueSchema) : valueSchema);
/** * Creates a MapType by specifying the data type of keys ({@code keyType}), the data type of * values ({@code keyType}), and whether values contain any null value * ({@code valueContainsNull}). */ public static MapType createMapType( DataType keyType, DataType valueType, boolean valueContainsNull) { if (keyType == null) { throw new IllegalArgumentException("keyType should not be null."); } if (valueType == null) { throw new IllegalArgumentException("valueType should not be null."); } return new MapType(keyType, valueType, valueContainsNull); }
Schema valueSchema = dataTypeToSchema(mapType.valueType(), recordCounter); return Schema.mapOf(dataTypeToSchema(mapType.keyType(), recordCounter), mapType.valueContainsNull() ? Schema.nullableOf(valueSchema) : valueSchema);
/** * Creates a MapType by specifying the data type of keys ({@code keyType}), the data type of * values ({@code keyType}), and whether values contain any null value * ({@code valueContainsNull}). */ public static MapType createMapType( DataType keyType, DataType valueType, boolean valueContainsNull) { if (keyType == null) { throw new IllegalArgumentException("keyType should not be null."); } if (valueType == null) { throw new IllegalArgumentException("valueType should not be null."); } return new MapType(keyType, valueType, valueContainsNull); }