@@ -35,8 +35,8 @@ use datafusion_expr::utils::expr_to_columns;
3535use datafusion_expr:: {
3636 cast, col, CreateCatalog , CreateCatalogSchema ,
3737 CreateExternalTable as PlanCreateExternalTable , CreateMemoryTable , CreateView ,
38- DescribeTable , DmlStatement , DropTable , DropView , Explain , Filter , LogicalPlan ,
39- LogicalPlanBuilder , PlanType , SetVariable , ToStringifiedPlan , WriteOp ,
38+ DescribeTable , DmlStatement , DropTable , DropView , Explain , ExprSchemable , Filter ,
39+ LogicalPlan , LogicalPlanBuilder , PlanType , SetVariable , ToStringifiedPlan , WriteOp ,
4040} ;
4141use sqlparser:: ast;
4242use sqlparser:: ast:: {
@@ -753,7 +753,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
753753 . schema_provider
754754 . get_table_provider ( ( & table_name) . into ( ) ) ?;
755755 let arrow_schema = ( * provider. schema ( ) ) . clone ( ) ;
756- let table_schema = Arc :: new ( DFSchema :: try_from ( arrow_schema) ?) ;
756+ let table_schema = DFSchema :: try_from ( arrow_schema) ?;
757757
758758 // infer types for Values clause... other types should be resolvable the regular way
759759 let mut prepare_param_data_types = BTreeMap :: new ( ) ;
@@ -792,19 +792,24 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
792792 "Column count doesn't match insert query!" . to_owned ( ) ,
793793 ) ) ?;
794794 }
795- let exprs: Vec < _ > = columns
795+ let values_schema = source. schema ( ) ;
796+ let exprs = columns
796797 . iter ( )
797798 . zip ( source. schema ( ) . fields ( ) . iter ( ) )
798799 . map ( |( c, f) | {
799- datafusion_expr:: Expr :: Column ( Column :: from ( f. name ( ) . clone ( ) ) )
800- . alias ( c. value . clone ( ) )
800+ let col_name = c. value . clone ( ) ;
801+ let col = table_schema. field_with_name ( None , col_name. as_str ( ) ) ?;
802+ let expr = datafusion_expr:: Expr :: Column ( Column :: from ( f. name ( ) . clone ( ) ) )
803+ . alias ( col_name)
804+ . cast_to ( col. data_type ( ) , values_schema) ?;
805+ Ok ( expr)
801806 } )
802- . collect ( ) ;
807+ . collect :: < Result < Vec < datafusion_expr :: Expr > > > ( ) ? ;
803808 let source = project ( source, exprs) ?;
804809
805810 let plan = LogicalPlan :: Dml ( DmlStatement {
806811 table_name,
807- table_schema,
812+ table_schema : Arc :: new ( table_schema ) ,
808813 op : WriteOp :: Insert ,
809814 input : Arc :: new ( source) ,
810815 } ) ;
0 commit comments