|
| 1 | +use std::{fmt::Formatter, sync::Arc}; |
| 2 | + |
| 3 | +use datafusion::{ |
| 4 | + common::internal_datafusion_err, |
| 5 | + error::Result, |
| 6 | + execution::SendableRecordBatchStream, |
| 7 | + physical_plan::{ |
| 8 | + DisplayAs, DisplayFormatType, EmptyRecordBatchStream, ExecutionPlan, |
| 9 | + ExecutionPlanProperties, Partitioning, PlanProperties, |
| 10 | + }, |
| 11 | +}; |
| 12 | + |
| 13 | +// TODO: REFACTOR NOT COMPLETE, just copied here and gutted uneccessary parts for now |
| 14 | +// IGNORE |
| 15 | + |
| 16 | +/// This is a simple execution plan that isolates a partition from the input |
| 17 | +/// plan It will advertise that it has a single partition and when |
| 18 | +/// asked to execute, it will execute a particular partition from the child |
| 19 | +/// input plan. |
| 20 | +/// |
| 21 | +/// This allows us to execute Repartition Exec's on different processes |
| 22 | +/// by showing each one only a single child partition |
| 23 | +#[derive(Debug)] |
| 24 | +pub struct PartitionIsolatorExec { |
| 25 | + pub input: Arc<dyn ExecutionPlan>, |
| 26 | + properties: PlanProperties, |
| 27 | + pub partition_count: usize, |
| 28 | +} |
| 29 | + |
| 30 | +impl PartitionIsolatorExec { |
| 31 | + pub fn new(input: Arc<dyn ExecutionPlan>, partition_count: usize) -> Self { |
| 32 | + // We advertise that we only have partition_count partitions |
| 33 | + let properties = input |
| 34 | + .properties() |
| 35 | + .clone() |
| 36 | + .with_partitioning(Partitioning::UnknownPartitioning(partition_count)); |
| 37 | + |
| 38 | + Self { |
| 39 | + input, |
| 40 | + properties, |
| 41 | + partition_count, |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +impl DisplayAs for PartitionIsolatorExec { |
| 47 | + fn fmt_as(&self, _t: DisplayFormatType, f: &mut Formatter) -> std::fmt::Result { |
| 48 | + write!( |
| 49 | + f, |
| 50 | + "PartitionIsolatorExec [providing upto {} partitions]", |
| 51 | + self.partition_count |
| 52 | + ) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +impl ExecutionPlan for PartitionIsolatorExec { |
| 57 | + fn name(&self) -> &str { |
| 58 | + "PartitionIsolatorExec" |
| 59 | + } |
| 60 | + |
| 61 | + fn as_any(&self) -> &dyn std::any::Any { |
| 62 | + self |
| 63 | + } |
| 64 | + |
| 65 | + fn properties(&self) -> &PlanProperties { |
| 66 | + &self.properties |
| 67 | + } |
| 68 | + |
| 69 | + fn children(&self) -> Vec<&std::sync::Arc<dyn ExecutionPlan>> { |
| 70 | + vec![&self.input] |
| 71 | + } |
| 72 | + |
| 73 | + fn with_new_children( |
| 74 | + self: std::sync::Arc<Self>, |
| 75 | + children: Vec<std::sync::Arc<dyn ExecutionPlan>>, |
| 76 | + ) -> Result<std::sync::Arc<dyn ExecutionPlan>> { |
| 77 | + // TODO: generalize this |
| 78 | + assert_eq!(children.len(), 1); |
| 79 | + Ok(Arc::new(Self::new( |
| 80 | + children[0].clone(), |
| 81 | + self.partition_count, |
| 82 | + ))) |
| 83 | + } |
| 84 | + |
| 85 | + fn execute( |
| 86 | + &self, |
| 87 | + partition: usize, |
| 88 | + context: std::sync::Arc<datafusion::execution::TaskContext>, |
| 89 | + ) -> Result<SendableRecordBatchStream> { |
| 90 | + let config = context.session_config(); |
| 91 | + let partition_group = &[0, 1]; |
| 92 | + |
| 93 | + let partitions_in_input = self.input.output_partitioning().partition_count() as u64; |
| 94 | + |
| 95 | + let output_stream = match partition_group.get(partition) { |
| 96 | + Some(actual_partition_number) => { |
| 97 | + if *actual_partition_number >= partitions_in_input { |
| 98 | + //trace!("{} returning empty stream", ctx_name); |
| 99 | + Ok(Box::pin(EmptyRecordBatchStream::new(self.input.schema())) |
| 100 | + as SendableRecordBatchStream) |
| 101 | + } else { |
| 102 | + //trace!("{} returning actual stream", ctx_name); |
| 103 | + self.input |
| 104 | + .execute(*actual_partition_number as usize, context) |
| 105 | + } |
| 106 | + } |
| 107 | + None => Ok(Box::pin(EmptyRecordBatchStream::new(self.input.schema())) |
| 108 | + as SendableRecordBatchStream), |
| 109 | + }; |
| 110 | + output_stream |
| 111 | + } |
| 112 | +} |
0 commit comments