MCPcopy Create free account
hub / github.com/apache/arrow / GetOriginSchema

Function GetOriginSchema

cpp/src/parquet/arrow/schema.cc:936–978  ·  view source on GitHub ↗

Get the original Arrow schema, as serialized in the Parquet metadata

Source from the content-addressed store, hash-verified

934
935// Get the original Arrow schema, as serialized in the Parquet metadata
936Status 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

Callers 1

MakeMethod · 0.85

Calls 10

key_value_metadataFunction · 0.85
FindKeyMethod · 0.80
ARROW_ASSIGN_OR_RAISEFunction · 0.70
OKFunction · 0.50
ReadSchemaFunction · 0.50
valueMethod · 0.45
sizeMethod · 0.45
reserveMethod · 0.45
AppendMethod · 0.45
keyMethod · 0.45

Tested by

no test coverage detected