Repository navigation
Conversation
…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
left a comment
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
| * 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
left a comment
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
| * 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 | |
| } |
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:nextRowGroupSize / (float) 0isInfinity, and(long) InfinityisLong.MAX_VALUE.recordCount + Long.MAX_VALUEoverflows to a negative value.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
nextMemCheckhelper and is computed in floating point. An infinite estimate now saturates instead of overflowing, so the next check is scheduled at mostmaxRowCountForPageSizeCheckrecords 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
TestInternalParquetRecordWritertests:nextMemCheckfor the halfway estimate, the minimum, the maximum, and records smaller than a byte.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