| 798 | } // namespace |
| 799 | |
| 800 | Result<std::shared_ptr<Field>> Field::MergeWith(const Field& other, |
| 801 | MergeOptions options) const { |
| 802 | if (name() != other.name()) { |
| 803 | return Status::Invalid("Field ", name(), " doesn't have the same name as ", |
| 804 | other.name()); |
| 805 | } |
| 806 | |
| 807 | if (Equals(other, /*check_metadata=*/false)) { |
| 808 | return Copy(); |
| 809 | } |
| 810 | |
| 811 | auto maybe_promoted_type = MergeTypes(type_, other.type(), options); |
| 812 | if (!maybe_promoted_type.ok()) { |
| 813 | return maybe_promoted_type.status().WithMessage( |
| 814 | "Unable to merge: Field ", name(), |
| 815 | " has incompatible types: ", type()->ToString(), " vs ", other.type()->ToString(), |
| 816 | ": ", maybe_promoted_type.status().message()); |
| 817 | } |
| 818 | auto promoted_type = *std::move(maybe_promoted_type); |
| 819 | if (promoted_type) { |
| 820 | bool nullable = nullable_; |
| 821 | if (options.promote_nullability) { |
| 822 | nullable = nullable || other.nullable() || type_->id() == Type::NA || |
| 823 | other.type()->id() == Type::NA; |
| 824 | } else if (nullable_ != other.nullable()) { |
| 825 | return Status::TypeError("Unable to merge: Field ", name(), |
| 826 | " has incompatible nullability: ", nullable_, " vs ", |
| 827 | other.nullable()); |
| 828 | } |
| 829 | |
| 830 | return std::make_shared<Field>(name_, std::move(promoted_type), nullable, metadata_); |
| 831 | } |
| 832 | return Status::TypeError("Unable to merge: Field ", name(), |
| 833 | " has incompatible types: ", type()->ToString(), " vs ", |
| 834 | other.type()->ToString()); |
| 835 | } |
| 836 | |
| 837 | Result<std::shared_ptr<Field>> Field::MergeWith(const std::shared_ptr<Field>& other, |
| 838 | MergeOptions options) const { |