-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-54134][SQL] Optimize Arrow memory usage #52747
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,9 +23,11 @@ import java.nio.channels.{Channels, ReadableByteChannel} | |
| import scala.collection.mutable.ArrayBuffer | ||
| import scala.jdk.CollectionConverters._ | ||
|
|
||
| import org.apache.arrow.compression.{Lz4CompressionCodec, ZstdCompressionCodec} | ||
| import org.apache.arrow.flatbuf.MessageHeader | ||
| import org.apache.arrow.memory.BufferAllocator | ||
| import org.apache.arrow.vector._ | ||
| import org.apache.arrow.vector.compression.{CompressionCodec, NoCompressionCodec} | ||
| import org.apache.arrow.vector.ipc.{ArrowStreamReader, ArrowStreamWriter, ReadChannel, WriteChannel} | ||
| import org.apache.arrow.vector.ipc.message.{ArrowRecordBatch, IpcOption, MessageSerializer} | ||
|
|
||
|
|
@@ -37,6 +39,7 @@ import org.apache.spark.sql.catalyst.expressions.{UnsafeProjection, UnsafeRow} | |
| import org.apache.spark.sql.catalyst.plans.logical.LocalRelation | ||
| import org.apache.spark.sql.catalyst.types.DataTypeUtils.toAttributes | ||
| import org.apache.spark.sql.classic.{DataFrame, Dataset, SparkSession} | ||
| import org.apache.spark.sql.internal.SQLConf | ||
| import org.apache.spark.sql.types._ | ||
| import org.apache.spark.sql.util.ArrowUtils | ||
| import org.apache.spark.sql.vectorized.{ArrowColumnVector, ColumnarBatch, ColumnVector} | ||
|
|
@@ -92,8 +95,25 @@ private[sql] object ArrowConverters extends Logging { | |
| ArrowUtils.rootAllocator.newChildAllocator( | ||
| s"to${this.getClass.getSimpleName}", 0, Long.MaxValue) | ||
|
|
||
| private val root = VectorSchemaRoot.create(arrowSchema, allocator) | ||
| protected val unloader = new VectorUnloader(root) | ||
| protected val root = VectorSchemaRoot.create(arrowSchema, allocator) | ||
|
|
||
| // Create compression codec based on config | ||
| private val compressionCodecName = SQLConf.get.arrowCompressionCodec | ||
| private val codec = compressionCodecName match { | ||
| case "none" => NoCompressionCodec.INSTANCE | ||
| case "zstd" => | ||
| val factory = CompressionCodec.Factory.INSTANCE | ||
| val codecType = new ZstdCompressionCodec().getCodecType() | ||
| factory.createCodec(codecType) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be great that we can have an option to add compression levels
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, we can add compression level option together.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am going to add the option in #52925 along with Pandas UDF support. |
||
| case "lz4" => | ||
| val factory = CompressionCodec.Factory.INSTANCE | ||
| val codecType = new Lz4CompressionCodec().getCodecType() | ||
| factory.createCodec(codecType) | ||
| case other => | ||
| throw new IllegalArgumentException( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea, that would be better.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will change to |
||
| s"Unsupported Arrow compression codec: $other. Supported values: none, zstd, lz4") | ||
| } | ||
| protected val unloader = new VectorUnloader(root, true, codec, true) | ||
| protected val arrowWriter = ArrowWriter.create(root) | ||
|
|
||
| Option(context).foreach {_.addTaskCompletionListener[Unit] { _ => | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does this optimization take effect in pandas udf?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think no, it is currently applied on
toArrowandtoPandaswhich is on the reported issue. It should be also available to arrow udf and pandas udf. I will try to extend this to such cases.