datafusion_execution/stream.rs
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
3// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
4
5use arrow::{datatypes::SchemaRef, record_batch::RecordBatch};
6use datafusion_common::Result;
7use futures::Stream;
8use std::pin::Pin;
9
10/// Trait for types that stream [RecordBatch]
11///
12/// See [`SendableRecordBatchStream`] for more details.
13pub trait RecordBatchStream: Stream<Item = Result<RecordBatch>> {
14 /// Returns the schema of this `RecordBatchStream`.
15 ///
16 /// Implementation of this trait should guarantee that all `RecordBatch`'s returned by this
17 /// stream should have the same schema as returned from this method.
18 fn schema(&self) -> SchemaRef;
19}
20
21/// Trait for a [`Stream`] of [`RecordBatch`]es that can be passed between threads
22///
23/// This trait is used to retrieve the results of DataFusion execution plan nodes.
24///
25/// The trait is a specialized Rust Async [`Stream`] that also knows the schema
26/// of the data it will return (even if the stream has no data). Every
27/// `RecordBatch` returned by the stream should have the same schema as returned
28/// by [`schema`](`RecordBatchStream::schema`).
29///
30/// # See Also
31///
32/// * [`RecordBatchStreamAdapter`] to convert an existing [`Stream`]
33/// to [`SendableRecordBatchStream`]
34///
35/// [`RecordBatchStreamAdapter`]: https://docs.rs/datafusion/latest/datafusion/physical_plan/stream/struct.RecordBatchStreamAdapter.html
36///
37/// # Error Handling
38///
39/// Once a stream returns an error, it should not be polled again (the caller
40/// should stop calling `next`) and handle the error.
41///
42/// However, returning `Ready(None)` (end of stream) is likely the safest
43/// behavior after an error. Like [`Stream`]s, `RecordBatchStream`s should not
44/// be polled after end of stream or returning an error. However, also like
45/// [`Stream`]s there is no mechanism to prevent callers polling so returning
46/// `Ready(None)` is recommended.
47pub type SendableRecordBatchStream = Pin<Box<dyn RecordBatchStream + Send>>;