@@ -55,8 +55,8 @@ use datafusion::{
5555} ;
5656use datafusion_common:: file_options:: file_type:: FileType ;
5757use datafusion_common:: {
58- context, internal_datafusion_err, internal_err, not_impl_err, DataFusionError ,
59- Result , TableReference ,
58+ context, internal_datafusion_err, internal_err, not_impl_err, plan_err ,
59+ DataFusionError , Result , TableReference , ToDFSchema ,
6060} ;
6161use datafusion_expr:: {
6262 dml,
@@ -66,10 +66,10 @@ use datafusion_expr::{
6666 EmptyRelation , Extension , Join , JoinConstraint , Limit , Prepare , Projection ,
6767 Repartition , Sort , SubqueryAlias , TableScan , Values , Window ,
6868 } ,
69- DistinctOn , DropView , Expr , LogicalPlan , LogicalPlanBuilder , ScalarUDF , SortExpr ,
70- WindowUDF ,
69+ AggregateUDF , ColumnUnnestList , DistinctOn , DmlStatement , DropView , Expr , FetchType ,
70+ LogicalPlan , LogicalPlanBuilder , RecursiveQuery , ScalarUDF , SkipType , SortExpr ,
71+ Statement , TableSource , Unnest , WindowUDF ,
7172} ;
72- use datafusion_expr:: { AggregateUDF , DmlStatement , RecursiveQuery , Unnest } ;
7373
7474use self :: to_proto:: { serialize_expr, serialize_exprs} ;
7575use crate :: logical_plan:: to_proto:: serialize_sorts;
@@ -233,6 +233,45 @@ fn from_table_reference(
233233 Ok ( table_ref. clone ( ) . try_into ( ) ?)
234234}
235235
236+ /// Converts [LogicalPlan::TableScan] to [TableSource]
237+ /// method to be used to deserialize nodes
238+ /// serialized by [from_table_source]
239+ fn to_table_source (
240+ node : & Option < Box < LogicalPlanNode > > ,
241+ ctx : & SessionContext ,
242+ extension_codec : & dyn LogicalExtensionCodec ,
243+ ) -> Result < Arc < dyn TableSource > > {
244+ if let Some ( node) = node {
245+ match node. try_into_logical_plan ( ctx, extension_codec) ? {
246+ LogicalPlan :: TableScan ( TableScan { source, .. } ) => Ok ( source) ,
247+ _ => plan_err ! ( "expected TableScan node" ) ,
248+ }
249+ } else {
250+ plan_err ! ( "LogicalPlanNode should be provided" )
251+ }
252+ }
253+
254+ /// converts [TableSource] to [LogicalPlan::TableScan]
255+ /// using [LogicalPlan::TableScan] was the best approach to
256+ /// serialize [TableSource] to [LogicalPlan::TableScan]
257+ fn from_table_source (
258+ table_name : TableReference ,
259+ target : Arc < dyn TableSource > ,
260+ extension_codec : & dyn LogicalExtensionCodec ,
261+ ) -> Result < LogicalPlanNode > {
262+ let projected_schema = target. schema ( ) . to_dfschema_ref ( ) ?;
263+ let r = LogicalPlan :: TableScan ( TableScan {
264+ table_name,
265+ source : target,
266+ projection : None ,
267+ projected_schema,
268+ filters : vec ! [ ] ,
269+ fetch : None ,
270+ } ) ;
271+
272+ LogicalPlanNode :: try_from_logical_plan ( & r, extension_codec)
273+ }
274+
236275impl AsLogicalPlan for LogicalPlanNode {
237276 fn try_decode ( buf : & [ u8 ] ) -> Result < Self >
238277 where
@@ -922,7 +961,7 @@ impl AsLogicalPlan for LogicalPlanNode {
922961 LogicalPlanType :: Dml ( dml_node) => Ok ( LogicalPlan :: Dml (
923962 datafusion:: logical_expr:: DmlStatement :: new (
924963 from_table_reference ( dml_node. table_name . as_ref ( ) , "DML " ) ?,
925- Arc :: new ( convert_required ! ( dml_node. schema ) ? ) ,
964+ to_table_source ( & dml_node. target , ctx , extension_codec ) ? ,
926965 dml_node. dml_type ( ) . into ( ) ,
927966 Arc :: new ( into_logical_plan ! ( dml_node. input, ctx, extension_codec) ?) ,
928967 ) ,
@@ -1656,7 +1695,7 @@ impl AsLogicalPlan for LogicalPlanNode {
16561695 ) ) ,
16571696 LogicalPlan :: Dml ( DmlStatement {
16581697 table_name,
1659- table_schema ,
1698+ target ,
16601699 op,
16611700 input,
16621701 ..
@@ -1667,7 +1706,11 @@ impl AsLogicalPlan for LogicalPlanNode {
16671706 Ok ( LogicalPlanNode {
16681707 logical_plan_type : Some ( LogicalPlanType :: Dml ( Box :: new ( DmlNode {
16691708 input : Some ( Box :: new ( input) ) ,
1670- schema : Some ( table_schema. try_into ( ) ?) ,
1709+ target : Some ( Box :: new ( from_table_source (
1710+ table_name. clone ( ) ,
1711+ Arc :: clone ( target) ,
1712+ extension_codec,
1713+ ) ?) ) ,
16711714 table_name : Some ( table_name. clone ( ) . into ( ) ) ,
16721715 dml_type : dml_type. into ( ) ,
16731716 } ) ) ) ,
0 commit comments