datafusion_catalog/
streaming.rs1use std::any::Any;
21use std::sync::Arc;
22
23use arrow::datatypes::SchemaRef;
24use async_trait::async_trait;
25use datafusion_common::{DFSchema, Result, plan_err};
26use datafusion_expr::{Expr, SortExpr, TableType};
27use datafusion_physical_expr::equivalence::project_ordering;
28use datafusion_physical_expr::{LexOrdering, create_physical_sort_exprs};
29use datafusion_physical_plan::ExecutionPlan;
30use datafusion_physical_plan::streaming::{PartitionStream, StreamingTableExec};
31use log::debug;
32
33use crate::{Session, TableProvider};
34
35#[derive(Debug)]
37pub struct StreamingTable {
38 schema: SchemaRef,
39 partitions: Vec<Arc<dyn PartitionStream>>,
40 infinite: bool,
41 sort_order: Vec<SortExpr>,
42}
43
44impl StreamingTable {
45 pub fn try_new(
47 schema: SchemaRef,
48 partitions: Vec<Arc<dyn PartitionStream>>,
49 ) -> Result<Self> {
50 for x in partitions.iter() {
51 let partition_schema = x.schema();
52 if !schema.contains(partition_schema) {
53 debug!(
54 "target schema does not contain partition schema. \
55 Target_schema: {schema:?}. Partition Schema: {partition_schema:?}"
56 );
57 return plan_err!("Mismatch between schema and batches");
58 }
59 }
60
61 Ok(Self {
62 schema,
63 partitions,
64 infinite: false,
65 sort_order: vec![],
66 })
67 }
68
69 pub fn with_infinite_table(mut self, infinite: bool) -> Self {
71 self.infinite = infinite;
72 self
73 }
74
75 pub fn with_sort_order(mut self, sort_order: Vec<SortExpr>) -> Self {
77 self.sort_order = sort_order;
78 self
79 }
80}
81
82#[async_trait]
83impl TableProvider for StreamingTable {
84 fn as_any(&self) -> &dyn Any {
85 self
86 }
87
88 fn schema(&self) -> SchemaRef {
89 Arc::clone(&self.schema)
90 }
91
92 fn table_type(&self) -> TableType {
93 TableType::View
94 }
95
96 async fn scan(
97 &self,
98 state: &dyn Session,
99 projection: Option<&Vec<usize>>,
100 _filters: &[Expr],
101 limit: Option<usize>,
102 ) -> Result<Arc<dyn ExecutionPlan>> {
103 let physical_sort = if !self.sort_order.is_empty() {
104 let df_schema = DFSchema::try_from(Arc::clone(&self.schema))?;
105 let eqp = state.execution_props();
106
107 let original_sort_exprs =
108 create_physical_sort_exprs(&self.sort_order, &df_schema, eqp)?;
109
110 if let Some(p) = projection {
111 let schema = Arc::new(self.schema.project(p)?);
116 LexOrdering::new(original_sort_exprs)
117 .and_then(|lex_ordering| project_ordering(&lex_ordering, &schema))
118 .map(|lex_ordering| lex_ordering.to_vec())
119 .unwrap_or_default()
120 } else {
121 original_sort_exprs
122 }
123 } else {
124 vec![]
125 };
126
127 Ok(Arc::new(StreamingTableExec::try_new(
128 Arc::clone(&self.schema),
129 self.partitions.clone(),
130 projection,
131 LexOrdering::new(physical_sort),
132 self.infinite,
133 limit,
134 )?))
135 }
136}