Skip to content

GH-3913: Avoid Comparing Equal Binary Statistics Values - #3915

Open
arouel wants to merge 1 commit into
apache:masterfrom
arouel:avoid-comp-equal-binary-stats
Open

arouel wants to merge 1 commit into
apache:masterfrom
arouel:avoid-comp-equal-binary-stats

Conversation

@arouel

@arouel arouel commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

Rationale for this change

BinaryStatistics.updateStats(Binary) compares each value with the current minimum and the current maximum. Binary.copy() keeps the same instance when the backing bytes are not reused. Thus, the minimum or the maximum can be the same instance as a value that the writer sends again. For example, values that come from a dictionary repeat the same constant Binary instance. For these values, the comparisons always return "equal" and do not change the statistics. Each comparison reads the bytes of the two values, so the writer does work that it does not need.

What changes are included in this PR?

  • In BinaryStatistics.updateStats(Binary), return early when the value is the same instance as min or max. In this case, the comparator is not called.
  • Use an early return for the first value. This removes the nested else block.

The subclasses Float16Statistics and IEEE754Float16Statistics override updateStats(Binary). Thus, this change does not apply to them.

Are these changes tested?

Yes. The new tests in TestStatistics make sure that the statistics do not change:

  • testBinaryMinMaxForRepeatedSingleConstantInstance: The same constant instance repeats, and it is both the minimum and the maximum.
  • testBinaryMinMaxKeepsBoundInstancesForConstantValues: The bound instances repeat. A different instance with equal content does not replace a bound. Lower and higher values change the bounds.

The existing test testBinaryMinMaxForReusedBackingByteArray continues to make sure that a reused byte array that changes between updates gives correct statistics.

These tests also pass without the change, because the skipped comparisons do not change the result. The tests make sure that the behavior stays the same.

Are there any user-facing changes?

No. The statistics are the same as before. There are no API changes.

Closes #3913

BinaryStatistics.updateStats(Binary) compares each value with the current minimum and maximum. When the value is the same instance as one of these bounds, the result of the comparison is always equal. This occurs when a writer sends the same constant Binary instance again, for example values decoded from a dictionary.

Update BinaryStatistics in the parquet-column module to skip the comparisons when the value is the same instance as the minimum or the maximum. The resulting statistics do not change.

Add tests in TestStatistics that make sure the behavior does not change: a single constant instance that repeats, bound instances that repeat, and other instances with equal, lower, or higher values. Unit tests cannot detect the skipped comparisons, because the result is the same with or without them.
@arouel

arouel commented Oct 30, 2026

Copy link
Copy Markdown
Contributor Author

@Fokko this is an improvement where I recently stumbled upon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Avoid Comparing Equal Binary Statistics Values

1 participant