| 1031 | } |
| 1032 | |
| 1033 | Result<bool> ApplyOriginalStorageMetadata(const Field& origin_field, |
| 1034 | SchemaField* inferred) { |
| 1035 | bool modified = false; |
| 1036 | |
| 1037 | auto& origin_type = origin_field.type(); |
| 1038 | auto& inferred_type = inferred->field->type(); |
| 1039 | |
| 1040 | const int num_children = inferred_type->num_fields(); |
| 1041 | |
| 1042 | if (num_children > 0 && origin_type->num_fields() == num_children) { |
| 1043 | DCHECK_EQ(static_cast<int>(inferred->children.size()), num_children); |
| 1044 | const auto factory = GetNestedFactory(*origin_type, *inferred_type); |
| 1045 | if (factory) { |
| 1046 | // The type may be modified (e.g. LargeList) while the children stay the same |
| 1047 | modified |= origin_type->id() != inferred_type->id(); |
| 1048 | |
| 1049 | // Apply original metadata recursively to children |
| 1050 | for (int i = 0; i < inferred_type->num_fields(); ++i) { |
| 1051 | ARROW_ASSIGN_OR_RAISE( |
| 1052 | const bool child_modified, |
| 1053 | ApplyOriginalMetadata(*origin_type->field(i), &inferred->children[i])); |
| 1054 | modified |= child_modified; |
| 1055 | } |
| 1056 | if (modified) { |
| 1057 | // Recreate this field using the modified child fields |
| 1058 | ::arrow::FieldVector modified_children(inferred_type->num_fields()); |
| 1059 | for (int i = 0; i < inferred_type->num_fields(); ++i) { |
| 1060 | modified_children[i] = inferred->children[i].field; |
| 1061 | } |
| 1062 | inferred->field = |
| 1063 | inferred->field->WithType(factory(std::move(modified_children))); |
| 1064 | } |
| 1065 | } |
| 1066 | } |
| 1067 | |
| 1068 | if (origin_type->id() == ::arrow::Type::TIMESTAMP && |
| 1069 | inferred_type->id() == ::arrow::Type::TIMESTAMP) { |
| 1070 | // Restore time zone, if any |
| 1071 | const auto& ts_type = checked_cast<const ::arrow::TimestampType&>(*inferred_type); |
| 1072 | const auto& ts_origin_type = |
| 1073 | checked_cast<const ::arrow::TimestampType&>(*origin_type); |
| 1074 | |
| 1075 | // If the data is tz-aware, then set the original time zone, since Parquet |
| 1076 | // has no native storage for timezones |
| 1077 | if (ts_type.timezone() == "UTC" && !ts_origin_type.timezone().empty()) { |
| 1078 | if (ts_type.unit() == ts_origin_type.unit()) { |
| 1079 | inferred->field = inferred->field->WithType(origin_type); |
| 1080 | } else { |
| 1081 | auto ts_type_new = ::arrow::timestamp(ts_type.unit(), ts_origin_type.timezone()); |
| 1082 | inferred->field = inferred->field->WithType(ts_type_new); |
| 1083 | } |
| 1084 | } |
| 1085 | modified = true; |
| 1086 | } |
| 1087 | |
| 1088 | if (origin_type->id() == ::arrow::Type::DURATION && |
| 1089 | inferred_type->id() == ::arrow::Type::INT64) { |
| 1090 | // Read back int64 arrays as duration. |
no test coverage detected