bkietz commented on a change in pull request #7115:
URL: https://github.com/apache/arrow/pull/7115#discussion_r434119089
##########
File path: cpp/src/arrow/type.cc
##########
@@ -380,25 +380,68 @@ std::string LargeListType::ToString() const {
return s.str();
}
-MapType::MapType(const std::shared_ptr<DataType>& key_type,
- const std::shared_ptr<DataType>& item_type, bool keys_sorted)
- : MapType(key_type, ::arrow::field("value", item_type), keys_sorted) {}
-
-MapType::MapType(const std::shared_ptr<DataType>& key_type,
- const std::shared_ptr<Field>& item_field, bool keys_sorted)
- : ListType(::arrow::field(
- "entries",
- struct_({std::make_shared<Field>("key", key_type, false),
item_field}), false)),
- keys_sorted_(keys_sorted) {
+MapType::MapType(std::shared_ptr<DataType> key_type, std::shared_ptr<DataType>
item_type,
+ bool keys_sorted)
+ : MapType(::arrow::field("key", std::move(key_type), false),
+ ::arrow::field("value", std::move(item_type)), keys_sorted) {}
+
+MapType::MapType(std::shared_ptr<DataType> key_type, std::shared_ptr<Field>
item_field,
+ bool keys_sorted)
+ : MapType(::arrow::field("key", std::move(key_type), false),
std::move(item_field),
+ keys_sorted) {}
+
+MapType::MapType(std::shared_ptr<Field> key_field, std::shared_ptr<Field>
item_field,
+ bool keys_sorted)
+ : MapType(
+ ::arrow::field("entries",
+ struct_({std::move(key_field),
std::move(item_field)}), false),
+ keys_sorted) {}
+
+MapType::MapType(std::shared_ptr<Field> value_field, bool keys_sorted)
+ : ListType(std::move(value_field)), keys_sorted_(keys_sorted) {
id_ = type_id;
}
+Result<std::shared_ptr<DataType>> MapType::Make(std::shared_ptr<Field>
value_field,
+ bool keys_sorted) {
+ const auto& value_type = *value_field->type();
+ if (value_field->nullable() || value_type.id() != Type::STRUCT) {
+ return Status::TypeError("Map entry field should be non-nullable struct");
+ }
+ const auto& struct_type = checked_cast<const StructType&>(value_type);
+ if (struct_type.num_fields() != 2) {
+ return Status::TypeError("Map entry field should have two children (got ",
+ struct_type.num_fields(), ")");
+ }
+ if (struct_type.field(0)->nullable()) {
+ return Status::TypeError("Map key field should be non-nullable");
+ }
+ return std::make_shared<MapType>(std::move(value_field), keys_sorted);
+}
+
std::string MapType::ToString() const {
std::stringstream s;
- s << "map<" << key_type()->ToString() << ", " << item_type()->ToString();
+
+ const auto print_field_name = [](std::ostream& os, const Field& field,
Review comment:
I'm not sure these lambdas add to readability
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]