|
| 1 | +use std::{any::Any, fmt, sync::Arc}; |
| 2 | + |
| 3 | +use arrow::array::RecordBatch; |
| 4 | +use datafusion::{ |
| 5 | + common::{DFSchemaRef, Statistics, internal_err}, |
| 6 | + error::Result, |
| 7 | + execution::{SendableRecordBatchStream, TaskContext}, |
| 8 | + physical_expr::EquivalenceProperties, |
| 9 | + physical_plan::{ |
| 10 | + DisplayAs, DisplayFormatType, Distribution, ExecutionPlan, Partitioning, PlanProperties, |
| 11 | + execution_plan::{Boundedness, EmissionType}, |
| 12 | + stream::RecordBatchStreamAdapter, |
| 13 | + }, |
| 14 | +}; |
| 15 | +use unitycatalog_common::client::UnityCatalogClient; |
| 16 | + |
| 17 | +use crate::KernelTaskContextExt; |
| 18 | + |
| 19 | +#[async_trait::async_trait] |
| 20 | +pub trait ExecutableUnityCatalogStement: std::fmt::Debug + Send + Sync + 'static { |
| 21 | + fn name(&self) -> &str; |
| 22 | + async fn execute(&self, client: UnityCatalogClient) -> Result<RecordBatch>; |
| 23 | + fn return_schema(&self) -> &DFSchemaRef; |
| 24 | +} |
| 25 | + |
| 26 | +pub struct UnityCatalogRequestExec { |
| 27 | + request: Arc<dyn ExecutableUnityCatalogStement>, |
| 28 | + cache: PlanProperties, |
| 29 | +} |
| 30 | + |
| 31 | +impl UnityCatalogRequestExec { |
| 32 | + pub fn new(request: Arc<dyn ExecutableUnityCatalogStement>) -> Self { |
| 33 | + Self { |
| 34 | + cache: PlanProperties::new( |
| 35 | + EquivalenceProperties::new(Arc::new(request.return_schema().as_arrow().clone())), |
| 36 | + Partitioning::UnknownPartitioning(1), |
| 37 | + EmissionType::Incremental, |
| 38 | + Boundedness::Bounded, |
| 39 | + ), |
| 40 | + request, |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +impl std::fmt::Debug for UnityCatalogRequestExec { |
| 46 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 47 | + f.debug_struct("UnityCatalogRequestExec") |
| 48 | + .field("request", &self.request) |
| 49 | + .finish() |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +impl DisplayAs for UnityCatalogRequestExec { |
| 54 | + fn fmt_as(&self, t: DisplayFormatType, f: &mut fmt::Formatter) -> fmt::Result { |
| 55 | + match t { |
| 56 | + DisplayFormatType::Default | DisplayFormatType::Verbose => { |
| 57 | + write!(f, "ExecuteUCStatement: statement={:?}", self.request) |
| 58 | + } |
| 59 | + DisplayFormatType::TreeRender => { |
| 60 | + // TODO: collect info |
| 61 | + write!(f, "") |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +#[async_trait::async_trait] |
| 68 | +impl ExecutionPlan for UnityCatalogRequestExec { |
| 69 | + fn name(&self) -> &'static str { |
| 70 | + Self::static_name() |
| 71 | + } |
| 72 | + |
| 73 | + /// Return a reference to Any that can be used for downcasting |
| 74 | + fn as_any(&self) -> &dyn Any { |
| 75 | + self |
| 76 | + } |
| 77 | + |
| 78 | + fn properties(&self) -> &PlanProperties { |
| 79 | + &self.cache |
| 80 | + } |
| 81 | + |
| 82 | + fn required_input_distribution(&self) -> Vec<Distribution> { |
| 83 | + vec![Distribution::SinglePartition] |
| 84 | + } |
| 85 | + |
| 86 | + fn children(&self) -> Vec<&Arc<dyn ExecutionPlan>> { |
| 87 | + vec![] |
| 88 | + } |
| 89 | + |
| 90 | + fn with_new_children( |
| 91 | + self: Arc<Self>, |
| 92 | + _: Vec<Arc<dyn ExecutionPlan>>, |
| 93 | + ) -> Result<Arc<dyn ExecutionPlan>> { |
| 94 | + Ok(self) |
| 95 | + } |
| 96 | + |
| 97 | + /// Execute one partition and return an iterator over RecordBatch |
| 98 | + fn execute( |
| 99 | + &self, |
| 100 | + partition: usize, |
| 101 | + context: Arc<TaskContext>, |
| 102 | + ) -> Result<SendableRecordBatchStream> { |
| 103 | + if 0 != partition { |
| 104 | + return internal_err!("CreateCatalogExec invalid partition {partition}"); |
| 105 | + } |
| 106 | + |
| 107 | + let uc_client = context.kernel_ext()?.unity_catalog_client()?; |
| 108 | + let request = self.request.clone(); |
| 109 | + |
| 110 | + Ok(Box::pin(RecordBatchStreamAdapter::new( |
| 111 | + self.schema(), |
| 112 | + Box::pin(futures::stream::once(async move { |
| 113 | + request.execute(uc_client).await |
| 114 | + })), |
| 115 | + ))) |
| 116 | + } |
| 117 | + |
| 118 | + fn statistics(&self) -> Result<Statistics> { |
| 119 | + Ok(Statistics::new_unknown(&self.schema())) |
| 120 | + } |
| 121 | +} |
0 commit comments