[ 
https://issues.apache.org/jira/browse/SPARK-62256?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Tengfei Huang updated SPARK-62256:
----------------------------------
    Attachment: binary read.png

> Reduce memory amplification when reading large binary files
> -----------------------------------------------------------
>
>                 Key: SPARK-62256
>                 URL: https://issues.apache.org/jira/browse/SPARK-62256
>             Project: Spark
>          Issue Type: Task
>          Components: SQL
>    Affects Versions: 4.2.1
>            Reporter: Tengfei Huang
>            Priority: Major
>         Attachments: binary read.png
>
>
> The {{binaryFile}} data source reads each file into a single row. Its current 
> implementation creates multiple copies of the file content and overallocates 
> row buffers, causing excessive heap usage and potential executor OOMs even 
> when the file itself fits comfortably in memory.
> Two operations contribute to this:
>  * The reader materializes the entire content through {{{}readAllBytes(){}}}, 
> then copies it into an {{UnsafeRow}} buffer that can grow to approximately 
> twice the required size.
>  * {{FileFormat.buildReaderWithPartitionValues}} applies an unsafe projection 
> even when there are no partition columns. Since the binary reader already 
> produces an {{{}UnsafeRow{}}}, this introduces another unnecessary copy.
> For an N-byte file, these operations can produce a peak of approximately 5N 
> bytes.
> !image-2026-11-23-09-42-48-403.png!
> Proposed improvement
>  # Size the row buffer precisely using the required columns, file length, and 
> alignment requirements.
>  # Read large-file content in bounded chunks and copy those chunks into the 
> row buffer, avoiding an intermediate whole-file array.
>  # Return the reader’s existing {{UnsafeRow}} directly for content scans 
> without partition columns.
>  # Handle unknown or unreliable lengths safely. In particular, treat archive 
> entry sizes as growth hints rather than allocating the declared size upfront.
> For large standalone files with accurate length metadata, the optimized 
> reader should hold approximately one copy of the content plus bounded read 
> scratch space. This target applies to scans without additional partition or 
> file metadata projections; downstream operators may introduce further copies.
> Preserve the existing schema, column pruning, filter behavior, maximum-length 
> checks, stream cleanup, and behavior when actual content length differs from 
> filesystem metadata.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to