-
Notifications
You must be signed in to change notification settings - Fork 447
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Ion Koutsouris <[email protected]>
- Loading branch information
1 parent
1befab9
commit 81166d7
Showing
13 changed files
with
323 additions
and
240 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,119 @@ | ||
use std::any::Any; | ||
use std::fmt::Formatter; | ||
use std::sync::Arc; | ||
|
||
use arrow_schema::SchemaRef; | ||
use datafusion::execution::{SendableRecordBatchStream, TaskContext}; | ||
use datafusion_physical_plan::{DisplayAs, DisplayFormatType, ExecutionPlan}; | ||
use arrow_schema::{Schema, SchemaRef}; | ||
use async_trait::async_trait; | ||
use datafusion::catalog::Session; | ||
use datafusion::catalog::TableProvider; | ||
use datafusion::execution::SessionState; | ||
use datafusion_common::{exec_datafusion_err, Column, DFSchema, Result as DataFusionResult}; | ||
use datafusion_expr::utils::conjunction; | ||
use datafusion_expr::{Expr, TableProviderFilterPushDown, TableType}; | ||
use datafusion_physical_expr::PhysicalExpr; | ||
use datafusion_physical_plan::filter::FilterExec; | ||
use datafusion_physical_plan::limit::GlobalLimitExec; | ||
use datafusion_physical_plan::projection::ProjectionExec; | ||
use datafusion_physical_plan::ExecutionPlan; | ||
|
||
/// Physical execution of a scan | ||
#[derive(Debug, Clone)] | ||
pub struct DeltaCdfScan { | ||
plan: Arc<dyn ExecutionPlan>, | ||
} | ||
use crate::DeltaTableError; | ||
use crate::{ | ||
delta_datafusion::DataFusionMixins, operations::load_cdf::CdfLoadBuilder, DeltaResult, | ||
}; | ||
|
||
impl DeltaCdfScan { | ||
/// Creates a new scan | ||
pub fn new(plan: Arc<dyn ExecutionPlan>) -> Self { | ||
Self { plan } | ||
} | ||
use super::ADD_PARTITION_SCHEMA; | ||
|
||
fn session_state_from_session(session: &dyn Session) -> DataFusionResult<&SessionState> { | ||
session | ||
.as_any() | ||
.downcast_ref::<SessionState>() | ||
.ok_or_else(|| exec_datafusion_err!("Failed to downcast Session to SessionState")) | ||
} | ||
|
||
impl DisplayAs for DeltaCdfScan { | ||
fn fmt_as(&self, _t: DisplayFormatType, f: &mut Formatter) -> std::fmt::Result { | ||
write!(f, "{:?}", self) | ||
} | ||
#[derive(Debug)] | ||
pub struct DeltaCdfTableProvider { | ||
cdf_builder: CdfLoadBuilder, | ||
schema: SchemaRef, | ||
} | ||
|
||
impl ExecutionPlan for DeltaCdfScan { | ||
fn name(&self) -> &str { | ||
Self::static_name() | ||
impl DeltaCdfTableProvider { | ||
/// Build a DeltaCDFTableProvider | ||
pub fn try_new(cdf_builder: CdfLoadBuilder) -> DeltaResult<Self> { | ||
let mut fields = cdf_builder.snapshot.input_schema()?.fields().to_vec(); | ||
for f in ADD_PARTITION_SCHEMA.clone() { | ||
fields.push(f.into()); | ||
} | ||
Ok(DeltaCdfTableProvider { | ||
cdf_builder, | ||
schema: Schema::new(fields).into(), | ||
}) | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl TableProvider for DeltaCdfTableProvider { | ||
fn as_any(&self) -> &dyn Any { | ||
self | ||
} | ||
|
||
fn schema(&self) -> SchemaRef { | ||
self.plan.schema().clone() | ||
self.schema.clone() | ||
} | ||
|
||
fn properties(&self) -> &datafusion::physical_plan::PlanProperties { | ||
self.plan.properties() | ||
fn table_type(&self) -> TableType { | ||
TableType::Base | ||
} | ||
|
||
fn children(&self) -> Vec<&Arc<dyn ExecutionPlan>> { | ||
vec![] | ||
} | ||
async fn scan( | ||
&self, | ||
session: &dyn Session, | ||
projection: Option<&Vec<usize>>, | ||
filters: &[Expr], | ||
limit: Option<usize>, | ||
) -> DataFusionResult<Arc<dyn ExecutionPlan>> { | ||
let session_state = session_state_from_session(session)?; | ||
let mut plan = self.cdf_builder.build(session_state).await?; | ||
|
||
let df_schema: DFSchema = plan.schema().try_into()?; | ||
|
||
if let Some(filter_expr) = conjunction(filters.iter().cloned()) { | ||
let physical_expr = session.create_physical_expr(filter_expr, &df_schema)?; | ||
plan = Arc::new(FilterExec::try_new(physical_expr, plan)?); | ||
} | ||
|
||
if let Some(projection) = projection { | ||
let current_projection = (0..plan.schema().fields().len()).collect::<Vec<usize>>(); | ||
if projection != ¤t_projection { | ||
let fields: DeltaResult<Vec<(Arc<dyn PhysicalExpr>, String)>> = projection | ||
.iter() | ||
.map(|i| { | ||
let (table_ref, field) = df_schema.qualified_field(*i); | ||
session | ||
.create_physical_expr( | ||
Expr::Column(Column::from((table_ref, field))), | ||
&df_schema, | ||
) | ||
.map(|expr| (expr, field.name().clone())) | ||
.map_err(DeltaTableError::from) | ||
}) | ||
.collect(); | ||
let fields = fields?; | ||
plan = Arc::new(ProjectionExec::try_new(fields, plan)?); | ||
} | ||
} | ||
|
||
fn with_new_children( | ||
self: Arc<Self>, | ||
_children: Vec<Arc<dyn ExecutionPlan>>, | ||
) -> datafusion_common::Result<Arc<dyn ExecutionPlan>> { | ||
self.plan.clone().with_new_children(_children) | ||
if let Some(limit) = limit { | ||
plan = Arc::new(GlobalLimitExec::new(plan, 0, Some(limit))) | ||
}; | ||
Ok(plan) | ||
} | ||
|
||
fn execute( | ||
fn supports_filters_pushdown( | ||
&self, | ||
partition: usize, | ||
context: Arc<TaskContext>, | ||
) -> datafusion_common::Result<SendableRecordBatchStream> { | ||
self.plan.execute(partition, context) | ||
filter: &[&Expr], | ||
) -> DataFusionResult<Vec<TableProviderFilterPushDown>> { | ||
Ok(filter | ||
.iter() | ||
.map(|_| TableProviderFilterPushDown::Exact) // maybe exact | ||
.collect()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.