andygrove commented on a change in pull request #7951:
URL: https://github.com/apache/arrow/pull/7951#discussion_r470036542



##########
File path: rust/datafusion/src/execution/physical_plan/merge.rs
##########
@@ -64,33 +75,51 @@ struct MergePartition {
     schema: SchemaRef,
     /// Input partitions
     partitions: Vec<Arc<dyn Partition>>,
+    /// Maximum number of concurrent threads
+    max_concurrency: usize,
+}
+
+fn collect_from_thread(
+    thread: JoinHandle<Result<Vec<RecordBatch>>>,
+    combined_results: &mut Vec<Arc<RecordBatch>>,
+) -> Result<()> {
+    match thread.join() {
+        Ok(join) => {
+            join?
+                .iter()
+                .for_each(|batch| 
combined_results.push(Arc::new(batch.clone())));
+            Ok(())
+        }
+        Err(e) => Err(ExecutionError::General(format!(
+            "Error collecting batches from thread: {:?}",
+            e
+        ))),
+    }
 }
 
 impl Partition for MergePartition {
     fn execute(&self) -> Result<Arc<Mutex<dyn RecordBatchReader + Send + 
Sync>>> {
-        let threads: Vec<JoinHandle<Result<Vec<RecordBatch>>>> = self
-            .partitions
-            .iter()
-            .map(|p| {
-                let p = p.clone();
-                thread::spawn(move || {
-                    let it = p.execute()?;
-                    common::collect(it)
-                })
-            })
-            .collect();
+        let mut combined_results: Vec<Arc<RecordBatch>> = vec![];
+        let mut threads: Vec<JoinHandle<Result<Vec<RecordBatch>>>> = vec![];
+
+        for partition in &self.partitions {
+            // limit number of concurrent threads
+            if threads.len() == self.max_concurrency {

Review comment:
       Sure, I use that approach already in Ballista. I can make that change 
here as well. 




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to