Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minor: add a datatype casting for the updated value #7922

Merged
merged 5 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 18 additions & 23 deletions datafusion/sql/src/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ use arrow_schema::DataType;
use datafusion_common::file_options::StatementOptions;
use datafusion_common::parsers::CompressionTypeVariant;
use datafusion_common::{
not_impl_err, plan_datafusion_err, plan_err, unqualified_field_not_found, Column,
Constraints, DFField, DFSchema, DFSchemaRef, DataFusionError, ExprSchema,
OwnedTableReference, Result, SchemaReference, TableReference, ToDFSchema,
not_impl_err, plan_datafusion_err, plan_err, unqualified_field_not_found,
Constraints, DFField, DFSchema, DFSchemaRef, DataFusionError, OwnedTableReference,
Result, SchemaReference, TableReference, ToDFSchema,
};
use datafusion_expr::dml::{CopyOptions, CopyTo};
use datafusion_expr::expr::Placeholder;
Expand Down Expand Up @@ -969,12 +969,6 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
table_name.clone(),
&arrow_schema,
)?);
let values = table_schema.fields().iter().map(|f| {
(
f.name().clone(),
ast::Expr::Identifier(ast::Ident::from(f.name().as_str())),
)
});

// Overwrite with assignment expressions
let mut planner_context = PlannerContext::new();
Expand All @@ -992,11 +986,15 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
})
.collect::<Result<HashMap<String, Expr>>>()?;

let values = values
.into_iter()
.map(|(k, v)| {
let val = assign_map.remove(&k).unwrap_or(v);
(k, val)
let values_and_types = table_schema
.fields()
.iter()
.map(|f| {
let col_name = f.name();
let val = assign_map.remove(col_name).unwrap_or_else(|| {
ast::Expr::Identifier(ast::Ident::from(col_name.as_str()))
});
(col_name, val, f.data_type())
})
.collect::<Vec<_>>();

Expand Down Expand Up @@ -1026,25 +1024,22 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {

// Projection
let mut exprs = vec![];
for (col_name, expr) in values.into_iter() {
for (col_name, expr, dt) in values_and_types.into_iter() {
let expr = self.sql_to_expr(expr, &table_schema, &mut planner_context)?;
let expr = match expr {
datafusion_expr::Expr::Placeholder(Placeholder {
ref id,
ref data_type,
}) => match data_type {
None => {
let dt = table_schema.data_type(&Column::from_name(&col_name))?;
datafusion_expr::Expr::Placeholder(Placeholder::new(
id.clone(),
Some(dt.clone()),
))
}
None => datafusion_expr::Expr::Placeholder(Placeholder::new(
id.clone(),
Some(dt.clone()),
)),
Some(_) => expr,
},
_ => expr,
};
let expr = expr.alias(col_name);
let expr = expr.cast_to(dt, source.schema())?.alias(col_name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will is still cast if datatypes are the same?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exprs.push(expr);
}
let source = project(source, exprs)?;
Expand Down
43 changes: 43 additions & 0 deletions datafusion/sqllogictest/test_files/update.slt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

##########
## Update Tests
##########

statement ok
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to say it would be better to actually run an update query, but then I realized that DataFusion doesn't actually plan them 😆

create table t1(a int, b varchar, c double, d int);

# Turn off the optimizer to make the logical plan closer to the initial one
statement ok
set datafusion.optimizer.max_passes = 0;

query TT
explain update t1 set a=1, b=2, c=3.0, d=NULL;
----
logical_plan
Dml: op=[Update] table=[t1]
--Projection: CAST(Int64(1) AS Int32) AS a, CAST(Int64(2) AS Utf8) AS b, Float64(3) AS c, CAST(NULL AS Int32) AS d
----TableScan: t1

query TT
explain update t1 set a=c+1, b=a, c=c+1.0, d=b;
----
logical_plan
Dml: op=[Update] table=[t1]
--Projection: CAST(t1.c + CAST(Int64(1) AS Float64) AS Int32) AS a, CAST(t1.a AS Utf8) AS b, t1.c + Float64(1) AS c, CAST(t1.b AS Int32) AS d
----TableScan: t1