Skip to content

[SPARK-55802][SQL] Fix integer overflow when computing Arrow batch bytes - #54584

Closed
viirya wants to merge 1 commit into
apache:masterfrom
viirya:fix-arrow-batch-bytes-overflow
Closed

[SPARK-55802][SQL] Fix integer overflow when computing Arrow batch bytes#54584
viirya wants to merge 1 commit into
apache:masterfrom
viirya:fix-arrow-batch-bytes-overflow

Conversation

@viirya

Copy link
Copy Markdown
Member

What changes were proposed in this pull request?

ArrowWriter.sizeInBytes() and SliceBytesArrowOutputProcessorImpl .getBatchBytes() both accumulated per-column buffer sizes (each an Int) into an Int accumulator. When the total exceeds 2 GB the sum silently wraps negative, causing the byte-limit checks controlled by spark.sql.execution.arrow.maxBytesPerBatch and
spark.sql.execution.arrow.maxBytesPerOutputBatch to behave incorrectly and potentially allow oversized batches through.

Fix by changing both accumulators and return types to Long.

Why are the changes needed?

Fix possible overflow when calculating Arrow batch bytes.

Does this PR introduce any user-facing change?

No

How was this patch tested?

Existing tests.

Was this patch authored or co-authored using generative AI tooling?

Generated-by: Claude Sonnet 4.6 noreply@anthropic.com

@viirya

Copy link
Copy Markdown
MemberAuthor

This issue is reported by @sunchao.

@sunchao

Copy link
Copy Markdown
Member

Thanks @viirya !

@dongjoon-hyundongjoon-hyun left a comment

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.

+1, LGTM.

@viirya
viiryaforce-pushed the fix-arrow-batch-bytes-overflow branch from 6a9089f to edca3d5CompareMarch 3, 2026 03:10
@viirya
viiryaforce-pushed the fix-arrow-batch-bytes-overflow branch 4 times, most recently from 8208dfb to 36e6738CompareMarch 3, 2026 21:42
@viirya
viiryaforce-pushed the fix-arrow-batch-bytes-overflow branch from 36e6738 to 0de3d67CompareMarch 4, 2026 02:48
`ArrowWriter.sizeInBytes()` and `SliceBytesArrowOutputProcessorImpl
.getBatchBytes()` both accumulated per-column buffer sizes (each an
`Int`) into an `Int` accumulator. When the total exceeds 2 GB the sum
silently wraps negative, causing the byte-limit checks controlled by
`spark.sql.execution.arrow.maxBytesPerBatch` and
`spark.sql.execution.arrow.maxBytesPerOutputBatch` to behave
incorrectly and potentially allow oversized batches through.
Fix by changing both accumulators and return types to `Long`.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@viirya
viiryaforce-pushed the fix-arrow-batch-bytes-overflow branch from 0de3d67 to 076746fCompareMarch 4, 2026 07:11
@viiryaviirya closed this in df195acMar 4, 2026
viirya added a commit that referenced this pull request Mar 4, 2026
### What changes were proposed in this pull request?
`ArrowWriter.sizeInBytes()` and `SliceBytesArrowOutputProcessorImpl .getBatchBytes()` both accumulated per-column buffer sizes (each an `Int`) into an `Int` accumulator. When the total exceeds 2 GB the sum silently wraps negative, causing the byte-limit checks controlled by `spark.sql.execution.arrow.maxBytesPerBatch` and
`spark.sql.execution.arrow.maxBytesPerOutputBatch` to behave incorrectly and potentially allow oversized batches through.
Fix by changing both accumulators and return types to `Long`.
### Why are the changes needed?
Fix possible overflow when calculating Arrow batch bytes.
### Does this PR introduce _any_ user-facing change?
No
### How was this patch tested?
Existing tests.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Sonnet 4.6 <noreplyanthropic.com>
Closes#54584 from viirya/fix-arrow-batch-bytes-overflow.
Authored-by: Liang-Chi Hsieh <viirya@gmail.com>
Signed-off-by: Liang-Chi Hsieh <viirya@gmail.com>
(cherry picked from commit df195ac)
Signed-off-by: Liang-Chi Hsieh <viirya@gmail.com>
@viirya

Copy link
Copy Markdown
MemberAuthor

Merged to master/4.1.

Manually backport to 4.0 in #54624 because some conflicts.

Thanks @HyukjinKwon@sunchao@zhengruifeng@dongjoon-hyun@yaooqinn@Yicong-Huang

@viirya
viirya deleted the fix-arrow-batch-bytes-overflow branch March 4, 2026 18:18
Sign up for freeto 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.

7 participants

@viirya@sunchao@HyukjinKwon@zhengruifeng@yaooqinn@dongjoon-hyun@Yicong-Huang