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 2020/12/22 12:15:23 UTC

[GitHub] [arrow] alamb commented on a change in pull request #8982: ARROW-10582: [Rust] [DataFusion] Implement "repartition" operator [WIP]

alamb commented on a change in pull request #8982:
URL: https://github.com/apache/arrow/pull/8982#discussion_r547242606



##########
File path: rust/datafusion/src/physical_plan/mod.rs
##########
@@ -107,6 +107,13 @@ pub async fn collect(plan: Arc<dyn ExecutionPlan>) -> Result<Vec<RecordBatch>> {
 /// Partitioning schemes supported by operators.
 #[derive(Debug, Clone)]
 pub enum Partitioning {
+    /// Allocate batches using a round-robin algorithm
+    RoundRobinBatch(usize),
+    /// Allocate rows using a round-robin algorithm. This provides finer-grained partitioning
+    /// than `RoundRobinBatch` but also has much more overhead.
+    RoundRobinRow(usize),

Review comment:
       Not that you asked, but if I had to pick 2 of these three schemes to implement, I would pick `RoundRobinBatch` and `Hash` and leave `RoundRobinRow` until later 
   
   The rationale being that I theorize `RoundRobinRow` usecase is much less common (e.g. maybe re-evening output of joins or filters, but I would expect most operators to respect the requested batch size if possible when creating their output)

##########
File path: rust/datafusion/src/physical_plan/repartition.rs
##########
@@ -0,0 +1,196 @@
+// 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.
+
+///! The repartition operator maps N input partitions to M output partitions based on a
+///! partitioning scheme.
+use std::any::Any;
+use std::pin::Pin;
+use std::sync::Arc;
+use std::task::{Context, Poll};
+
+use crate::error::{DataFusionError, Result};
+use crate::physical_plan::{ExecutionPlan, Partitioning};
+use arrow::datatypes::SchemaRef;
+use arrow::error::Result as ArrowResult;
+use arrow::record_batch::RecordBatch;
+
+use super::{RecordBatchStream, SendableRecordBatchStream};
+use async_trait::async_trait;
+
+use futures::channel::mpsc::{self, Receiver, Sender};
+use futures::stream::Stream;
+use futures::StreamExt;
+use tokio::sync::Mutex;
+use tokio::task::JoinHandle;
+
+/// partition. No guarantees are made about the order of the resulting partition.
+#[derive(Debug)]
+pub struct RepartitionExec {
+    /// Input execution plan
+    input: Arc<dyn ExecutionPlan>,
+    /// Partitioning scheme to use
+    partitioning: Partitioning,
+    /// Receivers for output batches
+    rx: Arc<Mutex<Vec<Receiver<ArrowResult<RecordBatch>>>>>,
+    /// Senders for output batches
+    tx: Arc<Mutex<Vec<Sender<ArrowResult<RecordBatch>>>>>,
+}
+
+#[async_trait]
+impl ExecutionPlan for RepartitionExec {
+    /// Return a reference to Any that can be used for downcasting
+    fn as_any(&self) -> &dyn Any {
+        self
+    }
+
+    /// Get the schema for this execution plan
+    fn schema(&self) -> SchemaRef {
+        self.input.schema()
+    }
+
+    fn children(&self) -> Vec<Arc<dyn ExecutionPlan>> {
+        vec![self.input.clone()]
+    }
+
+    fn with_new_children(
+        &self,
+        children: Vec<Arc<dyn ExecutionPlan>>,
+    ) -> Result<Arc<dyn ExecutionPlan>> {
+        match children.len() {
+            1 => Ok(Arc::new(RepartitionExec::try_new(
+                children[0].clone(),
+                self.partitioning.clone(),
+            )?)),
+            _ => Err(DataFusionError::Internal(
+                "RepartitionExec wrong number of children".to_string(),
+            )),
+        }
+    }
+
+    fn output_partitioning(&self) -> Partitioning {
+        self.partitioning.clone()
+    }
+
+    async fn execute(&self, partition: usize) -> Result<SendableRecordBatchStream> {
+        let mut tx = self.tx.lock().await;
+        let mut rx = self.rx.lock().await;
+        if tx.is_empty() {
+            // create one channel per *output* partition
+            let buffer_size = 64; // TODO: configurable?

Review comment:
       it seems to me that the biggest buffer we would want would be the total number of cores available for processing. Any larger and we are just wasting memory and cache size if the producer can make them faster than the consumer can consume them




----------------------------------------------------------------
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:
users@infra.apache.org