Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,17 @@ public void updateStats(Binary value) {
min = value.copy();
max = value.copy();
this.markAsNotEmpty();
} else {
if (comparator().compare(min, value) > 0) {
min = value.copy();
} else if (comparator().compare(max, value) < 0) {
max = value.copy();
}
return;
}
// copy() keeps the same instance when the backing bytes are not reused.
// Thus, a value can be the same instance as min or max. That value is equal to the bound, so do not compare it.
if (value == min || value == max) {
return;
}
if (comparator().compare(min, value) > 0) {
min = value.copy();
} else if (comparator().compare(max, value) < 0) {
max = value.copy();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.data.Offset.offset;
import static org.mockito.Mockito.clearInvocations;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verifyNoInteractions;

import java.nio.ByteBuffer;
import java.util.Locale;
Expand Down Expand Up @@ -521,6 +524,56 @@ public void testBinaryMinMaxForReusedBackingByteArray() {
assertThat(stats.getMinBytes()).isEqualTo(new byte[] {10});
}

@Test
public void testBinaryMinMaxForRepeatedSingleConstantInstance() {
BinaryStatistics stats = new BinaryStatistics();

// A spy records each call. A comparison calls methods on the value, so no calls means no comparison.
Binary value = spy(Binary.fromConstantByteArray(new byte[] {7}));
stats.updateStats(value);
clearInvocations(value);

stats.updateStats(value);
stats.updateStats(value);

verifyNoInteractions(value);
assertThat(stats.genericGetMin()).isSameAs(value);
assertThat(stats.genericGetMax()).isSameAs(value);
}

@Test
public void testBinaryMinMaxKeepsBoundInstancesForConstantValues() {
BinaryStatistics stats = new BinaryStatistics();

Binary low = spy(Binary.fromConstantByteArray(new byte[] {10}));
Binary high = spy(Binary.fromConstantByteArray(new byte[] {30}));
stats.updateStats(low);
stats.updateStats(high);
// The bounds keep the constant instances. An update with a bound instance does not compare the values.
assertThat(stats.genericGetMin()).isSameAs(low);
assertThat(stats.genericGetMax()).isSameAs(high);
clearInvocations(low, high);
stats.updateStats(low);
stats.updateStats(high);
verifyNoInteractions(low, high);
assertThat(stats.genericGetMin()).isSameAs(low);
assertThat(stats.genericGetMax()).isSameAs(high);
Comment on lines +556 to +560

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we also assert that repeating the min and max instances does not invoke the comparator? These tests still pass if the fast path is removed, so they do not protect the optimization itself.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wgtmac thank you for your feedback. I adapted the test case to protect the optimization.

stats.updateStats(Binary.fromConstantByteArray(new byte[] {20}));
assertThat(stats.genericGetMin()).isSameAs(low);
assertThat(stats.genericGetMax()).isSameAs(high);

// A different instance with equal content does not change the bounds. Lower or higher values change them.
stats.updateStats(Binary.fromConstantByteArray(new byte[] {10}));
assertThat(stats.genericGetMin()).isSameAs(low);
assertThat(stats.genericGetMax()).isSameAs(high);
stats.updateStats(Binary.fromConstantByteArray(new byte[] {5}));
assertThat(stats.getMinBytes()).isEqualTo(new byte[] {5});
assertThat(stats.genericGetMax()).isSameAs(high);
stats.updateStats(Binary.fromConstantByteArray(new byte[] {40}));
assertThat(stats.getMinBytes()).isEqualTo(new byte[] {5});
assertThat(stats.getMaxBytes()).isEqualTo(new byte[] {40});
}

@Test
public void testMergingStatistics() {
testMergingIntStats();
Expand Down
Loading