You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by av...@apache.org on 2023/01/24 22:45:07 UTC

[arrow-datafusion] branch master updated: Retain schema order in projection (#5053)

This is an automated email from the ASF dual-hosted git repository.

avantgardner pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow-datafusion.git


The following commit(s) were added to refs/heads/master by this push:
     new 43aca8903 Retain schema order in projection (#5053)
43aca8903 is described below

commit 43aca89032ff08c8e2876d82c3e4c981de037003
Author: Brent Gardner <bg...@squarelabs.net>
AuthorDate: Tue Jan 24 15:45:01 2023 -0700

    Retain schema order in projection (#5053)
---
 datafusion/sql/src/statement.rs          | 46 +++++++++++++++++++-------------
 datafusion/sql/tests/integration_test.rs |  6 ++---
 2 files changed, 30 insertions(+), 22 deletions(-)

diff --git a/datafusion/sql/src/statement.rs b/datafusion/sql/src/statement.rs
index 2d7791dd5..1d31e316a 100644
--- a/datafusion/sql/src/statement.rs
+++ b/datafusion/sql/src/statement.rs
@@ -45,7 +45,7 @@ use sqlparser::ast::{
     UnaryOperator, Value,
 };
 use sqlparser::parser::ParserError::ParserError;
-use std::collections::{BTreeMap, HashSet};
+use std::collections::{BTreeMap, HashMap, HashSet};
 use std::sync::Arc;
 
 impl<'a, S: ContextProvider> SqlToRel<'a, S> {
@@ -661,27 +661,35 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
             .get_table_provider((&table_name).into())?;
         let arrow_schema = (*provider.schema()).clone();
         let table_schema = Arc::new(DFSchema::try_from(arrow_schema)?);
-        let mut values: BTreeMap<_, _> = table_schema
-            .fields()
-            .iter()
-            .map(|f| {
-                (
-                    f.name().clone(),
-                    ast::Expr::Identifier(ast::Ident::from(f.name().as_str())),
-                )
-            })
-            .collect();
+        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();
-        for assign in assignments.iter() {
-            let col_name: &Ident = assign
-                .id
-                .iter()
-                .last()
-                .ok_or(DataFusionError::Plan("Empty column id".to_string()))?;
-            let _ = values.insert(col_name.value.clone(), assign.value.clone());
-        }
+        let assign_map = assignments
+            .iter()
+            .map(|assign| {
+                let col_name: &Ident = assign
+                    .id
+                    .iter()
+                    .last()
+                    .ok_or(DataFusionError::Plan("Empty column id".to_string()))?;
+                Ok((col_name.value.clone(), assign.value.clone()))
+            })
+            .collect::<Result<Vec<_>>>()?;
+        let mut assign_map: HashMap<String, ast::Expr> =
+            HashMap::from_iter(assign_map.into_iter());
+        let values = values
+            .into_iter()
+            .map(|(k, v)| {
+                let val = assign_map.remove(&k).unwrap_or(v);
+                (k, val)
+            })
+            .collect::<Vec<_>>();
 
         // Build scan
         let from = from.unwrap_or(table);
diff --git a/datafusion/sql/tests/integration_test.rs b/datafusion/sql/tests/integration_test.rs
index c3725ec05..9a1d5a883 100644
--- a/datafusion/sql/tests/integration_test.rs
+++ b/datafusion/sql/tests/integration_test.rs
@@ -175,7 +175,7 @@ fn plan_update() {
     let sql = "update person set last_name='Kay' where id=1";
     let plan = r#"
 Dml: op=[Update] table=[person]
-  Projection: person.age AS age, person.birth_date AS birth_date, person.first_name AS first_name, person.id AS id, Utf8("Kay") AS last_name, person.salary AS salary, person.state AS state, person.😀 AS 😀
+  Projection: person.id AS id, person.first_name AS first_name, Utf8("Kay") AS last_name, person.age AS age, person.state AS state, person.salary AS salary, person.birth_date AS birth_date, person.😀 AS 😀
     Filter: id = Int64(1)
       TableScan: person
       "#
@@ -3360,7 +3360,7 @@ fn test_prepare_statement_update_infer() {
 
     let expected_plan = r#"
 Dml: op=[Update] table=[person]
-  Projection: $1 AS age, person.birth_date AS birth_date, person.first_name AS first_name, person.id AS id, person.last_name AS last_name, person.salary AS salary, person.state AS state, person.😀 AS 😀
+  Projection: person.id AS id, person.first_name AS first_name, person.last_name AS last_name, $1 AS age, person.state AS state, person.salary AS salary, person.birth_date AS birth_date, person.😀 AS 😀
     Filter: id = $2
       TableScan: person
         "#
@@ -3380,7 +3380,7 @@ Dml: op=[Update] table=[person]
     let param_values = vec![ScalarValue::Int32(Some(42)), ScalarValue::UInt32(Some(1))];
     let expected_plan = r#"
 Dml: op=[Update] table=[person]
-  Projection: Int32(42) AS age, person.birth_date AS birth_date, person.first_name AS first_name, person.id AS id, person.last_name AS last_name, person.salary AS salary, person.state AS state, person.😀 AS 😀
+  Projection: person.id AS id, person.first_name AS first_name, person.last_name AS last_name, Int32(42) AS age, person.state AS state, person.salary AS salary, person.birth_date AS birth_date, person.😀 AS 😀
     Filter: id = UInt32(1)
       TableScan: person
         "#