You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/04/14 16:19:46 UTC

[GitHub] [arrow-datafusion] Dandandan commented on a diff in pull request #2226: Morsel-driven Parallelism using rayon (#2199)

Dandandan commented on code in PR #2226:
URL: https://github.com/apache/arrow-datafusion/pull/2226#discussion_r850605009


##########
datafusion/scheduler/src/pipeline/repartition.rs:
##########
@@ -0,0 +1,222 @@
+// 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.
+
+use std::collections::VecDeque;
+use std::sync::Arc;
+use std::task::{Context, Poll, Waker};
+
+use arrow::array::ArrayRef;
+use parking_lot::Mutex;
+
+use datafusion::arrow::record_batch::RecordBatch;
+use datafusion::error::{DataFusionError, Result};
+use datafusion::physical_plan::{Partitioning, PhysicalExpr};
+
+use crate::pipeline::Pipeline;
+use crate::ArrowResult;
+
+/// A [`Pipeline`] that can repartition its input
+#[derive(Debug)]
+pub struct RepartitionPipeline {
+    output: Partitioning,
+    state: Mutex<RepartitionState>,
+}
+
+impl RepartitionPipeline {
+    /// Create a new [`RepartitionPipeline`] with the given `input` and `output` partitioning
+    pub fn new(input: Partitioning, output: Partitioning) -> Self {
+        let input_count = input.partition_count();
+        assert_ne!(input_count, 0);
+
+        let num_partitions = match output {
+            Partitioning::RoundRobinBatch(num_partitions) => num_partitions,
+            Partitioning::Hash(_, num_partitions) => num_partitions,
+            Partitioning::UnknownPartitioning(_) => unreachable!(),
+        };
+        assert_ne!(num_partitions, 0);
+
+        let state = Mutex::new(RepartitionState {
+            next_idx: 0,
+            hash_buffer: vec![],
+            partition_closed: vec![false; input_count],
+            input_closed: false,
+            output_buffers: (0..num_partitions).map(|_| Default::default()).collect(),
+        });
+
+        Self { output, state }
+    }
+}
+
+struct RepartitionState {
+    next_idx: usize,
+    hash_buffer: Vec<u64>,
+    partition_closed: Vec<bool>,
+    input_closed: bool,
+    output_buffers: Vec<OutputBuffer>,
+}
+
+impl std::fmt::Debug for RepartitionState {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        f.debug_struct("RepartitionState")
+            .field("next_idx", &self.next_idx)
+            .field("partition_closed", &self.partition_closed)
+            .field("input_closed", &self.input_closed)
+            .finish()
+    }
+}
+
+impl RepartitionState {
+    fn push_batch(&mut self, partition: usize, batch: RecordBatch) {
+        let buffer = &mut self.output_buffers[partition];
+
+        buffer.batches.push_back(batch);
+
+        for waker in buffer.wait_list.drain(..) {
+            waker.wake()
+        }
+    }
+
+    fn hash_batch(
+        &mut self,
+        exprs: &[Arc<dyn PhysicalExpr>],
+        input: RecordBatch,
+    ) -> Result<()> {
+        let arrays = exprs

Review Comment:
   By now we have three (similar) copies of this code (at least datafusion / ballista and now the scheduler that I know of).
   I think we should extract this at some time to some common place (datafusion or even arrow-rs).



-- 
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.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org