public void mergeStatistics(SizeStatistics other) {
    if (!valid) {
      return;
    }

    // Stop merge if other is invalid.
    if (other == null || !other.isValid()) {
      valid = false;
      unencodedByteArrayDataBytes = 0L;
      repetitionLevelHistogram.clear();
      definitionLevelHistogram.clear();
      return;
    }

    Preconditions.checkArgument(type.equals(other.type), "Cannot merge SizeStatistics of different types");
    unencodedByteArrayDataBytes = Math.addExact(unencodedByteArrayDataBytes, other.unencodedByteArrayDataBytes);
    for (int i = 0; i < repetitionLevelHistogram.size(); i++) {
      repetitionLevelHistogram.set(
          i, Math.addExact(repetitionLevelHistogram.get(i), other.repetitionLevelHistogram.get(i)));
    }
    for (int i = 0; i < definitionLevelHistogram.size(); i++) {
      definitionLevelHistogram.set(
          i,
          Math.addExact(
              definitionLevelHistogram.get(i),
              other.getDefinitionLevelHistogram().get(i)));
    }
  }
@Test
  public void testMergeStatistics() {
    PrimitiveType type = Types.optional(PrimitiveType.PrimitiveTypeName.BINARY)
        .as(LogicalTypeAnnotation.stringType())
        .named("a");
    final int maxRepetitionLevel = 2;
    final int maxDefinitionLevel = 2;
    SizeStatistics.Builder builder1 = SizeStatistics.newBuilder(type, maxRepetitionLevel, maxDefinitionLevel);
    builder1.add(0, 0, Binary.fromString("a"));
    builder1.add(1, 1, Binary.fromString("b"));
    builder1.add(2, 2, Binary.fromString("c"));
    SizeStatistics statistics1 = builder1.build();
    SizeStatistics.Builder builder2 = SizeStatistics.newBuilder(type, maxRepetitionLevel, maxDefinitionLevel);
    builder2.add(0, 1, Binary.fromString("d"));
    builder2.add(0, 1, Binary.fromString("e"));
    SizeStatistics statistics2 = builder2.build();
    statistics1.mergeStatistics(statistics2);
    Assert.assertEquals(Optional.of(5L), statistics1.getUnencodedByteArrayDataBytes());
    Assert.assertEquals(Arrays.asList(3L, 1L, 1L), statistics1.getRepetitionLevelHistogram());
    Assert.assertEquals(Arrays.asList(1L, 3L, 1L), statistics1.getDefinitionLevelHistogram());
  }