use std::sync::Arc;
use std::{any::Any, borrow::Cow};
use crate::Session;
use arrow::datatypes::SchemaRef;
use async_trait::async_trait;
use datafusion_physical_plan::work_table::WorkTableExec;
use datafusion_physical_plan::ExecutionPlan;
use datafusion_common::error::Result;
use datafusion_expr::{Expr, LogicalPlan, TableProviderFilterPushDown, TableType};
use crate::TableProvider;
#[derive(Debug)]
pub struct CteWorkTable {
name: String,
table_schema: SchemaRef,
}
impl CteWorkTable {
pub fn new(name: &str, table_schema: SchemaRef) -> Self {
Self {
name: name.to_owned(),
table_schema,
}
}
pub fn name(&self) -> &str {
&self.name
}
pub fn schema(&self) -> SchemaRef {
Arc::clone(&self.table_schema)
}
}
#[async_trait]
impl TableProvider for CteWorkTable {
fn as_any(&self) -> &dyn Any {
self
}
fn get_logical_plan(&self) -> Option<Cow<LogicalPlan>> {
None
}
fn schema(&self) -> SchemaRef {
Arc::clone(&self.table_schema)
}
fn table_type(&self) -> TableType {
TableType::Temporary
}
async fn scan(
&self,
_state: &dyn Session,
_projection: Option<&Vec<usize>>,
_filters: &[Expr],
_limit: Option<usize>,
) -> Result<Arc<dyn ExecutionPlan>> {
Ok(Arc::new(WorkTableExec::new(
self.name.clone(),
Arc::clone(&self.table_schema),
)))
}
fn supports_filters_pushdown(
&self,
filters: &[&Expr],
) -> Result<Vec<TableProviderFilterPushDown>> {
Ok(vec![
TableProviderFilterPushDown::Unsupported;
filters.len()
])
}
}