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/24 11:43:38 UTC

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

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



##########
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:
       I would expect the deadlock problem to be most acute when trying to keep the data sorted (e.g a traditional merge). I didn't think we had any operators like that (yet) in DataFusion.
   
   Maybe we need to use `try_recv` when reading from channels rather than `recv`so as not to block on empty channels
   
   When we do actually have something that is trying to keep the data sorted, the behavior you want is "keep producing until every output channel has at least one record batch"
   
   Using round robin repartitioning, you can probably avoid infinite channels. Using hash re-partitioning, however, I don't think in general there is any way to ensure you have evenly distributed rows 




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