use crate::{Expr, LogicalPlan};
use arrow::datatypes::SchemaRef;
use datafusion_common::{Constraints, Result};
use std::{any::Any, borrow::Cow};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TableProviderFilterPushDown {
Unsupported,
Inexact,
Exact,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TableType {
Base,
View,
Temporary,
}
impl std::fmt::Display for TableType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
TableType::Base => write!(f, "Base"),
TableType::View => write!(f, "View"),
TableType::Temporary => write!(f, "Temporary"),
}
}
}
pub trait TableSource: Any + Sync + Send {
fn schema(&self) -> SchemaRef;
fn constraints(&self) -> Option<&Constraints> {
None
}
fn table_type(&self) -> TableType {
TableType::Base
}
fn supports_filters_pushdown(
&self,
filters: &[&Expr],
) -> Result<Vec<TableProviderFilterPushDown>> {
Ok((0..filters.len())
.map(|_| TableProviderFilterPushDown::Unsupported)
.collect())
}
fn supports_skip_pushdown(&self) -> bool {
false
}
fn get_logical_plan(&'_ self) -> Option<Cow<'_, LogicalPlan>> {
None
}
fn get_column_default(&self, _column: &str) -> Option<&Expr> {
None
}
}
impl dyn TableSource {
pub fn is<T: TableSource>(&self) -> bool {
(self as &dyn Any).is::<T>()
}
pub fn downcast_ref<T: TableSource>(&self) -> Option<&T> {
(self as &dyn Any).downcast_ref()
}
}