| 862 | }; |
| 863 | |
| 864 | Status FieldFromFlatbuffer(const flatbuf::Field* field, FieldPosition field_pos, |
| 865 | DictionaryMemo* dictionary_memo, std::shared_ptr<Field>* out) { |
| 866 | std::shared_ptr<DataType> type; |
| 867 | |
| 868 | std::shared_ptr<KeyValueMetadata> metadata; |
| 869 | ARROW_ASSIGN_OR_RAISE(metadata, |
| 870 | internal::GetKeyValueMetadata(field->custom_metadata())); |
| 871 | |
| 872 | // Reconstruct the data type |
| 873 | // 1. Data type children |
| 874 | FieldVector child_fields; |
| 875 | const auto& children = field->children(); |
| 876 | // As a tolerance, allow for a null children field meaning "no children" (ARROW-12100) |
| 877 | if (children != nullptr) { |
| 878 | child_fields.resize(children->size()); |
| 879 | for (int i = 0; i < static_cast<int>(children->size()); ++i) { |
| 880 | RETURN_NOT_OK(FieldFromFlatbuffer(children->Get(i), field_pos.child(i), |
| 881 | dictionary_memo, &child_fields[i])); |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | // 2. Top-level concrete data type |
| 886 | auto type_data = field->type(); |
| 887 | CHECK_FLATBUFFERS_NOT_NULL(type_data, "Field.type"); |
| 888 | RETURN_NOT_OK(ConcreteTypeFromFlatbuffer(field->type_type(), type_data, |
| 889 | std::move(child_fields), &type)); |
| 890 | |
| 891 | // 3. Is it a dictionary type? |
| 892 | int64_t dictionary_id = -1; |
| 893 | std::shared_ptr<DataType> dict_value_type; |
| 894 | const flatbuf::DictionaryEncoding* encoding = field->dictionary(); |
| 895 | if (encoding != nullptr) { |
| 896 | // The field is dictionary-encoded. Construct the DictionaryType |
| 897 | // based on the DictionaryEncoding metadata and record in the |
| 898 | // dictionary_memo |
| 899 | std::shared_ptr<DataType> index_type; |
| 900 | auto int_data = encoding->indexType(); |
| 901 | CHECK_FLATBUFFERS_NOT_NULL(int_data, "DictionaryEncoding.indexType"); |
| 902 | RETURN_NOT_OK(IntFromFlatbuffer(int_data, &index_type)); |
| 903 | dict_value_type = type; |
| 904 | ARROW_ASSIGN_OR_RAISE(type, |
| 905 | DictionaryType::Make(index_type, type, encoding->isOrdered())); |
| 906 | dictionary_id = encoding->id(); |
| 907 | } |
| 908 | |
| 909 | // 4. Is it an extension type? |
| 910 | if (metadata != nullptr) { |
| 911 | // Look for extension metadata in custom_metadata field |
| 912 | int name_index = metadata->FindKey(kExtensionTypeKeyName); |
| 913 | if (name_index != -1) { |
| 914 | std::shared_ptr<ExtensionType> ext_type = |
| 915 | GetExtensionType(metadata->value(name_index)); |
| 916 | if (ext_type != nullptr) { |
| 917 | int data_index = metadata->FindKey(kExtensionMetadataKeyName); |
| 918 | std::string type_data = data_index == -1 ? "" : metadata->value(data_index); |
| 919 | |
| 920 | ARROW_ASSIGN_OR_RAISE(type, ext_type->Deserialize(type, type_data)); |
| 921 | // Remove the metadata, for faithful roundtripping |
no test coverage detected