Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
86a4989
docs: add design spec for batch stash shuffle optimization
andygrove Apr 12, 2026
488522c
docs: add implementation plan for batch stash shuffle optimization
andygrove Apr 12, 2026
ca8ee6a
feat: add BatchStash registry for native batch handle passing
andygrove Apr 12, 2026
e68d777
feat: add CometHandleBatchIterator Java class and JNI bridge
andygrove Apr 12, 2026
07c86c7
feat: add executePlanBatchHandle JNI function for stash-mode output
andygrove Apr 12, 2026
d2f87ab
feat: add stash mode to CometExecIterator for batch handle output
andygrove Apr 12, 2026
0a49ca6
feat: add handle-mode input path to ScanExec for batch stash retrieval
andygrove Apr 12, 2026
84862ef
feat: detect CometHandleBatchIterator in planner and enable handle mode
andygrove Apr 12, 2026
3588543
feat: preserve CometExecIterator reference through shuffle dependency…
andygrove Apr 12, 2026
db137ad
feat: integrate batch stash mode in CometNativeShuffleWriter
andygrove Apr 12, 2026
ed95e83
style: apply formatting fixes
andygrove Apr 12, 2026
a827437
fix: use scan source name for handle mode detection instead of JNI is…
andygrove Apr 12, 2026
3af0e9a
chore: remove JVM crash log files
andygrove Apr 12, 2026
e6c3a42
refactor: deduplicate executePlan logic and address code review findings
andygrove Apr 12, 2026
6cc0e78
refactor: use protobuf field for batch stash handle mode instead of s…
andygrove Apr 12, 2026
c42be90
chore: remove design docs from PR
andygrove Apr 12, 2026
f70119a
fix: add batch_stash_handle field to Scan struct literals in tests
andygrove Apr 12, 2026
c64d482
fix: use InputBatch::Complete for stashed batches to bypass schema re…
andygrove Apr 12, 2026
d37752a
feat: add spark.comet.exec.shuffle.batchStash.enabled config (default…
andygrove Apr 12, 2026
38af7d9
fix: apply schema reconciliation for stashed batches with type mismat…
andygrove Apr 12, 2026
ba07bf7
fix: address PR review feedback for batch stash
andygrove Apr 13, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions common/src/main/scala/org/apache/comet/CometConf.scala
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,16 @@ object CometConf extends ShimCometConf {
.intConf
.createWithDefault(1)

val COMET_SHUFFLE_BATCH_STASH_ENABLED: ConfigEntry[Boolean] =
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added config for now just in case we discover bugs, but I plan on removing this config in the future.

conf(s"$COMET_EXEC_CONFIG_PREFIX.shuffle.batchStash.enabled")
.category(CATEGORY_SHUFFLE)
.doc(
"When enabled, batches passed between a native child plan and a native shuffle " +
"writer are transferred via an opaque handle instead of Arrow FFI, avoiding " +
"unnecessary serialization overhead.")
.booleanConf
.createWithDefault(true)

val COMET_COLUMNAR_SHUFFLE_ASYNC_ENABLED: ConfigEntry[Boolean] =
conf("spark.comet.columnar.shuffle.async.enabled")
.category(CATEGORY_SHUFFLE)
Expand Down
4 changes: 1 addition & 3 deletions native/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 124 additions & 0 deletions native/core/src/execution/batch_stash.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

//! Global registry for passing RecordBatch values between native execution contexts
//! via opaque u64 handles, without Arrow FFI serialization.

use arrow::record_batch::RecordBatch;
use once_cell::sync::Lazy;
use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Mutex;

/// Counter for generating unique handles.
static NEXT_HANDLE: AtomicU64 = AtomicU64::new(1);

/// Global stash mapping handles to RecordBatch values.
/// Entries are removed by `take()` when the downstream ScanExec consumes them,
/// so there is no leak under normal operation. The stash lives for the process
/// lifetime but is effectively empty between query executions.
static STASH: Lazy<Mutex<HashMap<u64, RecordBatch>>> = Lazy::new(|| Mutex::new(HashMap::new()));
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.

Does this must be a global one? Any leak risk? Do we need some cleanup to remove the content?


/// Store a RecordBatch in the global stash and return a unique handle.
pub(crate) fn stash(batch: RecordBatch) -> u64 {
let handle = NEXT_HANDLE.fetch_add(1, Ordering::Relaxed);
STASH
.lock()
.unwrap_or_else(|e| e.into_inner())
.insert(handle, batch);
handle
}

/// Remove and return the RecordBatch associated with the given handle.
///
/// Returns `None` if the handle does not exist in the stash.
pub(crate) fn take(handle: u64) -> Option<RecordBatch> {
STASH
.lock()
.unwrap_or_else(|e| e.into_inner())
.remove(&handle)
}

#[cfg(test)]
mod tests {
use super::*;
use arrow::array::Int32Array;
use arrow::datatypes::{DataType, Field, Schema};
use std::sync::Arc;

fn make_batch(values: Vec<i32>) -> RecordBatch {
let schema = Arc::new(Schema::new(vec![Field::new("a", DataType::Int32, false)]));
let array = Arc::new(Int32Array::from(values));
RecordBatch::try_new(schema, vec![array]).unwrap()
}

#[test]
fn test_stash_and_take() {
let batch = make_batch(vec![1, 2, 3]);
let num_rows = batch.num_rows();

let handle = stash(batch);
let retrieved = take(handle).expect("expected batch to be present");

assert_eq!(retrieved.num_rows(), num_rows);
let col = retrieved
.column(0)
.as_any()
.downcast_ref::<Int32Array>()
.unwrap();
assert_eq!(col.values(), &[1, 2, 3]);
}

#[test]
fn test_take_removes_entry() {
let batch = make_batch(vec![10, 20]);
let handle = stash(batch);

// First take returns the batch.
assert!(take(handle).is_some());
// Second take finds nothing.
assert!(take(handle).is_none());
}

#[test]
fn test_take_unknown_handle() {
// Handle 0 is never issued (counter starts at 1).
assert!(take(0).is_none());
// A large handle that was never issued.
assert!(take(u64::MAX).is_none());
}

#[test]
fn test_handles_are_unique() {
let batch1 = make_batch(vec![1]);
let batch2 = make_batch(vec![2]);
let batch3 = make_batch(vec![3]);

let h1 = stash(batch1);
let h2 = stash(batch2);
let h3 = stash(batch3);

assert_ne!(h1, h2);
assert_ne!(h2, h3);
assert_ne!(h1, h3);

// Clean up.
take(h1);
take(h2);
take(h3);
}
}
Loading
Loading