Skip to content

[VL] Build Lance Arrow batches in Gluten's task allocator - #1

Draft
sezruby wants to merge 3 commits into
feat/lance-arrow-batchesfrom
feat/lance-arrow-batch-allocator
Draft

sezruby wants to merge 3 commits into
feat/lance-arrow-batchesfrom
feat/lance-arrow-batch-allocator

Conversation

@sezruby

@sezruby sezruby commented Nov 16, 2026 •

Copy link
Copy Markdown
Owner

Tracking PR in the fork, stacked on apache#13648 (base feat/lance-arrow-batches). It depends on lance-format/lance-spark#966 (LanceRuntime.setArrowBatchAllocatorProvider), which is not released yet. I'll fold it into apache#13648 once a lance-spark release includes it.

What changes are proposed in this pull request?

lance-spark allocates the Arrow batches of a Lance scan under Spark's root allocator, so OffloadArrowDataExec copies every batch into Gluten's task allocator (ArrowColumnVectors.adopt). Arrow moves buffers only within one root. A move into Gluten's allocator would also unbalance its listener: Arrow charges a transfer without calling the target's AllocationListener, then calls onRelease on the listener of whichever allocator owns the memory last (details in apache#12958, issuecomment-6904672648).

VeloxLanceComponent now supplies the task's Arrow allocator to lance-spark when the executor starts:

  • onExecutorStart sets LanceRuntime.setArrowBatchAllocatorProvider to ArrowBufferAllocators.contextInstance() (null outside a Spark task); onExecutorShutdown clears it.
  • A Lance reader builds its batches in that allocator, the one OffloadArrowDataExec offloads with, so adopt transfers within one allocator. There's no copy and no move between allocators, and the memory is charged to the task once.
  • TaskResources registers its completion listener at task start, before DataSourceRDD registers the reader's close, so the reader closes before the allocator is released.
  • Docs: VeloxLance.md drops the copy from the limitations.

Before enabling: memory refused during the import

Gluten's task allocator throws when Spark has no memory left to grant (ManagedAllocationListener over a throwOnOom target). With this change, lance-spark imports each batch's Arrow C stream into that allocator, so a refusal can happen inside Arrow Java's C Data import.

Gluten's Arrow Java has both leaks. That covers the 15.0.0 source build (dev/build-arrow.sh with modify_arrow.patch) and the Maven 18.x / 19.0 artifacts used for Spark 4.x. So this change should wait for an Arrow Java release with both fixes. Alternatively, the source build could carry the two small patches.

How was this patch tested?

All runs are on amd64 Linux against a local build of lance-format/lance-spark#966:

  • VeloxLanceSuite passes 13/13, including a new test: every vector of a Lance task's Arrow batches lives in the task's Arrow allocator. A row-based query still feeds Velox through OffloadArrowDataExec.
  • ArrowColumnVectorTest (7), ColumnarBatchTest (8) and VeloxTransitionSuite (24) pass.
  • No "Detected leaked Arrow allocator", "Managed memory leak" or unreleased-native-batch warnings.
  • Spark 4.0 / Scala 2.13 compiles; spotless and scalastyle are clean.
  • Benchmark: 400k rows of 768-d float vectors over 8 partitions, copy path vs. provider, interleaved, best of 9. Answers match in both modes, and the path check confirms the copy mode's batches are outside Gluten's allocator. Emulated amd64, so treat these as ratios only.
Query Copy Provider Speedup
vector filter 201 ms 151 ms 1.33×
vector group agg 279 ms 248 ms 1.12×
vector shuffle (distribute by) 208 ms 189 ms 1.10×
scalar group agg 77 ms 69 ms noise

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

Generated-by: Claude Opus 5.5

sezruby and others added 3 commits November 15, 2026 14:30
LanceArrowBatchScan switches a Lance BatchScanExec to lance-spark's
Arrow batch reads (LanceScan.withArrowBatches, lance-spark#908) when
Velox imports every column type of them, and VeloxLanceComponent
declares those batches as ArrowJavaBatchType, so Gluten offloads them
with its Arrow transitions. The vanilla BatchScanExec stays in the plan
and keeps lance-spark's runtime filters, output partitioning and
metrics. LanceScanTransformer and OffloadLanceScan are removed.

OffloadArrowDataExec now closes the batches that ColumnarBatches.offload
builds from Spark ArrowColumnVectors, which nothing closed before.

Generated-by: Claude Opus 5.5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Velox imports both, and Gluten runs TIMESTAMP_NTZ natively by default.
ArrowColumnVectors.adopt and copy now handle a NullVector, whose
getAllocator() throws.

Generated-by: Claude Opus 5.5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
lance-spark allocated the Arrow batches of a Lance scan under Spark's root
allocator, so OffloadArrowDataExec copied every batch into Gluten's task
allocator: Arrow moves buffers only within a root, and a move into Gluten's
allocator would leave its listener unbalanced (Arrow charges a transfer
without calling the target's AllocationListener, then calls onRelease on the
last owner's listener).

VeloxLanceComponent now supplies the task's Arrow allocator to lance-spark
(LanceRuntime.setArrowBatchAllocatorProvider) when the executor starts. A Lance
reader builds its batches in it, so the offload adopts their vectors in place,
and their memory is charged to the task once through Spark's memory manager.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant