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