absl::Status MergeFeatureStatistics()

in tfx_bsl/cc/statistics/merge_util.cc [64:112]


absl::Status MergeFeatureStatistics(const FeatureNameStatistics& merge_from,
                                    FeatureNameStatistics* merge_to) {
  // Copy the path/name if not yet set; We don't need to check for matches here,
  // this will only be called on matching or newly initialized items.
  if (merge_from.has_path() && !merge_to->has_path())
    *merge_to->mutable_path() = merge_from.path();
  if (!merge_from.name().empty() && merge_to->name().empty())
    merge_to->set_name(merge_from.name());

  // Set the type.
  if (DistinctNonzeroValues<FeatureNameStatistics::Type>(
          merge_from.type(), merge_to->type(), FeatureNameStatistics::INT)) {
    return absl::InvalidArgumentError(
        "FeatureNameStatistics shards with different types");
  }
  if (merge_from.type() != 0) {
    merge_to->set_type(merge_from.type());
  }
  // Set the stats oneof.
  if (DistinctNonzeroValues<FeatureNameStatistics::StatsCase>(
          merge_from.stats_case(), merge_to->stats_case(),
          FeatureNameStatistics::STATS_NOT_SET)) {
    return absl::InvalidArgumentError(
        "FeatureNameStatistics shards with different stats");
  }
  // TODO(202910677): Consider making this stricter, to match the requirement
  // that we not merge two non-empty messages (other than common stats).
  switch (merge_from.stats_case()) {
    case FeatureNameStatistics::STATS_NOT_SET:
      break;
    case FeatureNameStatistics::kNumStats:
      merge_to->mutable_num_stats()->MergeFrom(merge_from.num_stats());
      break;
    case FeatureNameStatistics::kStringStats:
      merge_to->mutable_string_stats()->MergeFrom(merge_from.string_stats());
      break;
    case FeatureNameStatistics::kBytesStats:
      merge_to->mutable_bytes_stats()->MergeFrom(merge_from.bytes_stats());
      break;
    case FeatureNameStatistics::kStructStats:
      merge_to->mutable_struct_stats()->MergeFrom(merge_from.struct_stats());
      break;
  }
  // Concatenate common stats.
  for (const auto& custom_stat : merge_from.custom_stats())
    *merge_to->add_custom_stats() = custom_stat;

  return absl::OkStatus();
}