docs: @Nullable
    public static Metadata merge(
        @Nullable List<Metadata> toBeMerged,
        @Nullable AggregatorFactory[] overrideMergedAggregators
    )
    {
      if (toBeMerged == null || toBeMerged.size() == 0) {
        return null;
      }
  
      boolean foundSomeMetadata = false;
      Map<String, Object> mergedContainer = new HashMap<>();
      List<AggregatorFactory[]> aggregatorsToMerge = overrideMergedAggregators == null
                                                     ? new ArrayList<>()
                                                     : null;
  
      List<TimestampSpec> timestampSpecsToMerge = new ArrayList<>();
      List<Granularity> gransToMerge = new ArrayList<>();
      List<Boolean> rollupToMerge = new ArrayList<>();
  
      for (Metadata metadata : toBeMerged) {
        if (metadata != null) {
          foundSomeMetadata = true;
          if (aggregatorsToMerge != null) {
            aggregatorsToMerge.add(metadata.getAggregators());
          }
  
          if (timestampSpecsToMerge != null && metadata.getTimestampSpec() != null) {
            timestampSpecsToMerge.add(metadata.getTimestampSpec());
          }
  
          if (gransToMerge != null) {
            gransToMerge.add(metadata.getQueryGranularity());
          }
  
          if (rollupToMerge != null) {
            rollupToMerge.add(metadata.isRollup());
          }
          mergedContainer.putAll(metadata.container);
        } else {
          //if metadata and hence aggregators and queryGranularity for some segment being merged are unknown then
          //final merged segment should not have same in metadata
          aggregatorsToMerge = null;
          timestampSpecsToMerge = null;
          gransToMerge = null;
          rollupToMerge = null;
        }
      }
  
      if (!foundSomeMetadata) {
        return null;
      }
  
      final AggregatorFactory[] mergedAggregators = aggregatorsToMerge == null ?
                                                    overrideMergedAggregators :
                                                    AggregatorFactory.mergeAggregators(aggregatorsToMerge);
  
      final TimestampSpec mergedTimestampSpec = timestampSpecsToMerge == null ?
                                                null :
                                                TimestampSpec.mergeTimestampSpec(timestampSpecsToMerge);
  
      final Granularity mergedGranularity = gransToMerge == null ?
                                            null :
                                            Granularity.mergeGranularities(gransToMerge);
  
      Boolean rollup = null;
      if (rollupToMerge != null && !rollupToMerge.isEmpty()) {
        rollup = rollupToMerge.get(0);
        for (Boolean r : rollupToMerge) {
          if (r == null) {
            rollup = null;
            break;
          } else if (!r.equals(rollup)) {
            rollup = null;
            break;
          } else {
            rollup = r;
          }
        }
      }
  
      return new Metadata(
          mergedContainer,
          mergedAggregators,
          mergedTimestampSpec,
          mergedGranularity,
          rollup
      );
    }