Skip to content

Commit c313601

Browse files
Ensure insert projections are of correct type (#5049)
* Ensure insert projections are of correct type
1 parent 13dfdd6 commit c313601

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

datafusion/sql/src/statement.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ use datafusion_expr::utils::expr_to_columns;
3535
use 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
};
4141
use sqlparser::ast;
4242
use 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
});

datafusion/sql/tests/integration_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ fn plan_insert() {
163163
"insert into person (id, first_name, last_name) values (1, 'Alan', 'Turing')";
164164
let plan = r#"
165165
Dml: op=[Insert] table=[person]
166-
Projection: column1 AS id, column2 AS first_name, column3 AS last_name
166+
Projection: CAST(column1 AS id AS UInt32), column2 AS first_name, column3 AS last_name
167167
Values: (Int64(1), Utf8("Alan"), Utf8("Turing"))
168168
"#
169169
.trim();

0 commit comments

Comments
 (0)