Get the original Arrow schema, as serialized in the Parquet metadata
| 937 | |
| 938 | // Get the original Arrow schema, as serialized in the Parquet metadata |
| 939 | Status GetOriginSchema(const std::shared_ptr<const KeyValueMetadata>& metadata, |
| 940 | std::shared_ptr<const KeyValueMetadata>* clean_metadata, |
| 941 | std::shared_ptr<::arrow::Schema>* out) { |
| 942 | if (metadata == nullptr) { |
| 943 | *out = nullptr; |
| 944 | *clean_metadata = nullptr; |
| 945 | return Status::OK(); |
| 946 | } |
| 947 | |
| 948 | static const std::string kArrowSchemaKey = "ARROW:schema"; |
| 949 | int schema_index = metadata->FindKey(kArrowSchemaKey); |
| 950 | if (schema_index == -1) { |
| 951 | *out = nullptr; |
| 952 | *clean_metadata = metadata; |
| 953 | return Status::OK(); |
| 954 | } |
| 955 | |
| 956 | // The original Arrow schema was serialized using the store_schema option. |
| 957 | // We deserialize it here and use it to inform read options such as |
| 958 | // dictionary-encoded fields. |
| 959 | ARROW_ASSIGN_OR_RAISE(auto decoded, |
| 960 | ::arrow::util::base64_decode(metadata->value(schema_index))); |
| 961 | auto schema_buf = std::make_shared<Buffer>(decoded); |
| 962 | |
| 963 | ::arrow::ipc::DictionaryMemo dict_memo; |
| 964 | ::arrow::io::BufferReader input(schema_buf); |
| 965 | |
| 966 | ARROW_ASSIGN_OR_RAISE(*out, ::arrow::ipc::ReadSchema(&input, &dict_memo)); |
| 967 | |
| 968 | if (metadata->size() > 1) { |
| 969 | // Copy the metadata without the schema key |
| 970 | auto new_metadata = ::arrow::key_value_metadata({}, {}); |
| 971 | new_metadata->reserve(metadata->size() - 1); |
| 972 | for (int64_t i = 0; i < metadata->size(); ++i) { |
| 973 | if (i == schema_index) continue; |
| 974 | new_metadata->Append(metadata->key(i), metadata->value(i)); |
| 975 | } |
| 976 | *clean_metadata = new_metadata; |
| 977 | } else { |
| 978 | // No other keys, let metadata be null |
| 979 | *clean_metadata = nullptr; |
| 980 | } |
| 981 | return Status::OK(); |
| 982 | } |
| 983 | |
| 984 | // Restore original Arrow field information that was serialized as Parquet metadata |
| 985 | // but that is not necessarily present in the field reconstituted from Parquet data |
no test coverage detected