Skip to content

GH-2720: Don't check the row group size after every record smaller than a byte - #3966

Open
raviston wants to merge 1 commit into
apache:masterfrom
raviston:GH-2720-block-size-check
Open

raviston wants to merge 1 commit into
apache:masterfrom
raviston:GH-2720-block-size-check

Conversation

@raviston

@raviston raviston commented Nov 16, 2026 •

Copy link
Copy Markdown

Rationale for this change

InternalParquetRecordWriter.checkBlockSizeReached() estimates when to check the buffered size again from the average record size. That average is computed with an integer division (memSize / recordCount), so it is 0 for records smaller than a byte, for example a single boolean column or highly compressible data. In that case:

  1. nextRowGroupSize / (float) 0 is Infinity, and (long) Infinity is Long.MAX_VALUE.
  2. recordCount + Long.MAX_VALUE overflows to a negative value.
  3. max(minRowCountForPageSizeCheck, negative) gives the minimum (100), which the record count has already passed.

From then on, columnStore.getBufferedSize(), which is relatively expensive, runs after every record until the row group is flushed.

An earlier fix (#1004) was auto-closed as stale. It also still overflowed when the buffered size is 0, because the average stays 0.

What changes are included in this PR?

The estimate of the next check moves into a package-private nextMemCheck helper and is computed in floating point. An infinite estimate now saturates instead of overflowing, so the next check is scheduled at most maxRowCountForPageSizeCheck records ahead, as the existing cap intends. Otherwise the result is unchanged: halfway to the estimated record count, clamped between the minimum and maximum. The flush decision is untouched.

Are these changes tested?

Yes. The new TestInternalParquetRecordWriter tests:

  • nextMemCheck for the halfway estimate, the minimum, the maximum, and records smaller than a byte.
  • An end-to-end write of 100,000 one-bit records, which checks that the next size check is scheduled after the current record count.

Both regression tests fail with the previous formula. The parquet-hadoop writer tests also pass (150 tests: TestParquetWriter*, TestParquetFileWriter, TestMemoryManager, TestColumnChunkPageWriteStore, TestInputOutputFormat*, TestDataPageChecksums, TestStatistics, TestSizeStatisticsRoundTrip, TestParquetOutputFormatJobSummaryLevel).

Are there any user-facing changes?

No API changes. Writers of very small records no longer compute the buffered size after every record.

Closes #2720

…ler than a byte

InternalParquetRecordWriter estimates when to check the buffered size again from
the average record size, computed with an integer division. For records smaller
than a byte on average, the average size is 0: the estimated number of records
in the row group was cast from infinity to Long.MAX_VALUE, adding the record
count overflowed to a negative value, and the next check was scheduled at the
minimum row count, already reached. The buffered size, which is relatively
expensive to compute, was then checked after every record.

The estimate is now computed in floating point, so that it saturates and the
next check is scheduled at most the maximum row count for a size check ahead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Fokko
Fokko self-requested a review November 25, 2026 15:14

@Fokko Fokko left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks @raviston, nice catch on the integer-division overflow. The floating-point nextMemCheck with saturation reads clean, and the regression tests cover the zero-average case well. One small thing below about the halfway estimate when the average record size grows.

One question: TestInternalParquetRecordWriter reaches the writer internals via reflection (getField on recordCount / recordCountForNextMemCheck). Is that on purpose, or would exposing a package-private getter on InternalParquetRecordWriter be cleaner?

* Estimates the record count at which the buffered size should be checked again: halfway to the number of records
* that fit in the row group, at least the minimum and at most the maximum row count for a size check ahead.
* <p>
* The estimate is computed in floating point. Records smaller than a byte on average have a {@code recordSize} of

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit: when recordSize is small but nonzero (for example 1 byte) and the row group is large, (recordCount + rowGroupSize / recordSize) / 2 can exceed Long.MAX_VALUE as a double before the cast, and (long) of a too-large double saturates at Long.MAX_VALUE in Java, which is fine here since the outer min clamps it to recordCount + maxRowCountForPageSizeCheck. Just confirming the saturation behavior is intended for that case too, not only for recordSize == 0.

Suggested change
* The estimate is computed in floating point. Records smaller than a byte on average have a {@code recordSize} of
static long nextMemCheck(long recordCount, long recordSize, long rowGroupSize, ParquetProperties props) {
double estimatedRowGroupRecordCount = rowGroupSize / (double) recordSize; // +Inf when recordSize == 0
long halfway = (long) ((recordCount + estimatedRowGroupRecordCount) / 2); // saturates at Long.MAX_VALUE
return min(
max(props.getMinRowCountForPageSizeCheck(), halfway),
recordCount + props.getMaxRowCountForPageSizeCheck()); // will not look more than max records ahead
}

@Fokko Fokko left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks @raviston, nice catch on the integer-division overflow. The floating-point nextMemCheck with saturation reads clean, and the regression tests cover the zero-average case well. One small thing below about the halfway estimate when the average record size grows.

One question: TestInternalParquetRecordWriter reaches the writer internals via reflection (getField on recordCount / recordCountForNextMemCheck). Is that on purpose, or would exposing a package-private getter on InternalParquetRecordWriter be cleaner?

* Estimates the record count at which the buffered size should be checked again: halfway to the number of records
* that fit in the row group, at least the minimum and at most the maximum row count for a size check ahead.
* <p>
* The estimate is computed in floating point. Records smaller than a byte on average have a {@code recordSize} of

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit: when recordSize is small but nonzero (for example 1 byte) and the row group is large, (recordCount + rowGroupSize / recordSize) / 2 can exceed Long.MAX_VALUE as a double before the cast, and (long) of a too-large double saturates at Long.MAX_VALUE in Java, which is fine here since the outer min clamps it to recordCount + maxRowCountForPageSizeCheck. Just confirming the saturation behavior is intended for that case too, not only for recordSize == 0.

Suggested change
* The estimate is computed in floating point. Records smaller than a byte on average have a {@code recordSize} of
static long nextMemCheck(long recordCount, long recordSize, long rowGroupSize, ParquetProperties props) {
double estimatedRowGroupRecordCount = rowGroupSize / (double) recordSize; // +Inf when recordSize == 0
long halfway = (long) ((recordCount + estimatedRowGroupRecordCount) / 2); // saturates at Long.MAX_VALUE
return min(
max(props.getMinRowCountForPageSizeCheck(), halfway),
recordCount + props.getMaxRowCountForPageSizeCheck()); // will not look more than max records ahead
}

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.

checkBlockSizeReached zero record size perf issue

3 participants