Skip to content

Commit

Permalink
feat: support target table alias in update statement (#8080)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonahgao authored Nov 9, 2023
1 parent e54894c commit a564af5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
18 changes: 14 additions & 4 deletions datafusion/sql/src/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ 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,
not_impl_err, plan_datafusion_err, plan_err, unqualified_field_not_found, Column,
Constraints, DFField, DFSchema, DFSchemaRef, DataFusionError, OwnedTableReference,
Result, SchemaReference, TableReference, ToDFSchema,
};
Expand Down Expand Up @@ -970,8 +970,8 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
from: Option<TableWithJoins>,
predicate_expr: Option<Expr>,
) -> Result<LogicalPlan> {
let table_name = match &table.relation {
TableFactor::Table { name, .. } => name.clone(),
let (table_name, table_alias) = match &table.relation {
TableFactor::Table { name, alias, .. } => (name.clone(), alias.clone()),
_ => plan_err!("Cannot update non-table relation!")?,
};

Expand Down Expand Up @@ -1047,7 +1047,17 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
// Cast to target column type, if necessary
expr.cast_to(field.data_type(), source.schema())?
}
None => datafusion_expr::Expr::Column(field.qualified_column()),
None => {
// If the target table has an alias, use it to qualify the column name
if let Some(alias) = &table_alias {
datafusion_expr::Expr::Column(Column::new(
Some(self.normalizer.normalize(alias.name.clone())),
field.name(),
))
} else {
datafusion_expr::Expr::Column(field.qualified_column())
}
}
};
Ok(expr.alias(field.name()))
})
Expand Down
15 changes: 14 additions & 1 deletion datafusion/sqllogictest/test_files/update.slt
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,17 @@ create table t3(a int, b varchar, c double, d int);

# set from mutiple tables, sqlparser only supports from one table
query error DataFusion error: SQL error: ParserError\("Expected end of statement, found: ,"\)
explain update t1 set b = t2.b, c = t3.a, d = 1 from t2, t3 where t1.a = t2.a and t1.a = t3.a;
explain update t1 set b = t2.b, c = t3.a, d = 1 from t2, t3 where t1.a = t2.a and t1.a = t3.a;

# test table alias
query TT
explain update t1 as T set b = t2.b, c = t.a, d = 1 from t2 where t.a = t2.a and t.b > 'foo' and t2.c > 1.0;
----
logical_plan
Dml: op=[Update] table=[t1]
--Projection: t.a AS a, t2.b AS b, CAST(t.a AS Float64) AS c, CAST(Int64(1) AS Int32) AS d
----Filter: t.a = t2.a AND t.b > Utf8("foo") AND t2.c > Float64(1)
------CrossJoin:
--------SubqueryAlias: t
----------TableScan: t1
--------TableScan: t2

0 comments on commit a564af5

Please sign in to comment.