|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use arrow_schema::SchemaRef; |
| 19 | +use datafusion_common::DataFusionError; |
| 20 | +use datafusion_execution::{SendableRecordBatchStream, TaskContext}; |
| 21 | +use datafusion_physical_expr::{EquivalenceProperties, Partitioning}; |
| 22 | +use datafusion_physical_plan::execution_plan::{Boundedness, EmissionType}; |
| 23 | +use datafusion_physical_plan::{ |
| 24 | + DisplayAs, DisplayFormatType, ExecutionPlan, PlanProperties, |
| 25 | +}; |
| 26 | +use std::any::Any; |
| 27 | +use std::fmt::{Debug, Formatter}; |
| 28 | +use std::sync::{Arc, Mutex}; |
| 29 | + |
| 30 | +/// Execution plan that return the stream on the call to `execute`. further calls to `execute` will |
| 31 | +/// return an error |
| 32 | +pub struct OnceExec { |
| 33 | + /// the results to send back |
| 34 | + stream: Mutex<Option<SendableRecordBatchStream>>, |
| 35 | + cache: PlanProperties, |
| 36 | +} |
| 37 | + |
| 38 | +impl Debug for OnceExec { |
| 39 | + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { |
| 40 | + write!(f, "OnceExec") |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +impl OnceExec { |
| 45 | + pub fn new(stream: SendableRecordBatchStream) -> Self { |
| 46 | + let cache = Self::compute_properties(stream.schema()); |
| 47 | + Self { |
| 48 | + stream: Mutex::new(Some(stream)), |
| 49 | + cache, |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /// This function creates the cache object that stores the plan properties such as schema, equivalence properties, ordering, partitioning, etc. |
| 54 | + fn compute_properties(schema: SchemaRef) -> PlanProperties { |
| 55 | + PlanProperties::new( |
| 56 | + EquivalenceProperties::new(schema), |
| 57 | + Partitioning::UnknownPartitioning(1), |
| 58 | + EmissionType::Incremental, |
| 59 | + Boundedness::Bounded, |
| 60 | + ) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +impl DisplayAs for OnceExec { |
| 65 | + fn fmt_as(&self, t: DisplayFormatType, f: &mut Formatter) -> std::fmt::Result { |
| 66 | + match t { |
| 67 | + DisplayFormatType::Default | DisplayFormatType::Verbose => { |
| 68 | + write!(f, "OnceExec:") |
| 69 | + } |
| 70 | + DisplayFormatType::TreeRender => { |
| 71 | + write!(f, "") |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +impl ExecutionPlan for OnceExec { |
| 78 | + fn name(&self) -> &'static str { |
| 79 | + Self::static_name() |
| 80 | + } |
| 81 | + |
| 82 | + fn as_any(&self) -> &dyn Any { |
| 83 | + self |
| 84 | + } |
| 85 | + |
| 86 | + fn properties(&self) -> &PlanProperties { |
| 87 | + &self.cache |
| 88 | + } |
| 89 | + |
| 90 | + fn children(&self) -> Vec<&Arc<dyn ExecutionPlan>> { |
| 91 | + vec![] |
| 92 | + } |
| 93 | + |
| 94 | + fn with_new_children( |
| 95 | + self: Arc<Self>, |
| 96 | + _: Vec<Arc<dyn ExecutionPlan>>, |
| 97 | + ) -> datafusion_common::Result<Arc<dyn ExecutionPlan>> { |
| 98 | + unimplemented!() |
| 99 | + } |
| 100 | + |
| 101 | + /// Returns a stream which yields data |
| 102 | + fn execute( |
| 103 | + &self, |
| 104 | + partition: usize, |
| 105 | + _context: Arc<TaskContext>, |
| 106 | + ) -> datafusion_common::Result<SendableRecordBatchStream> { |
| 107 | + assert_eq!(partition, 0); |
| 108 | + |
| 109 | + let stream = self.stream.lock().unwrap().take(); |
| 110 | + |
| 111 | + stream.ok_or(DataFusionError::Internal( |
| 112 | + "Stream already consumed".to_string(), |
| 113 | + )) |
| 114 | + } |
| 115 | +} |
0 commit comments