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

alamb pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion.git


The following commit(s) were added to refs/heads/main by this push:
     new ea023e2d48 Implement unparse `ScalarVariable` to String (#10541)
ea023e2d48 is described below

commit ea023e2d4878240eece870cf4b346c7a0667aeed
Author: Weijie Guo <[email protected]>
AuthorDate: Sat May 18 00:26:40 2024 +0800

    Implement unparse `ScalarVariable` to String (#10541)
    
    * Implement unparse `ScalarVariable` to String
    
    * remove quote_style
    
    ---------
    
    Co-authored-by: Andrew Lamb <[email protected]>
---
 datafusion/sql/src/unparser/expr.rs | 37 +++++++++++++++++++++++++++++++++++--
 1 file changed, 35 insertions(+), 2 deletions(-)

diff --git a/datafusion/sql/src/unparser/expr.rs 
b/datafusion/sql/src/unparser/expr.rs
index 416ab03d1f..a50bb7a698 100644
--- a/datafusion/sql/src/unparser/expr.rs
+++ b/datafusion/sql/src/unparser/expr.rs
@@ -394,8 +394,22 @@ impl Unparser<'_> {
                     expr: Box::new(sql_parser_expr),
                 })
             }
-            Expr::ScalarVariable(_, _) => {
-                not_impl_err!("Unsupported Expr conversion: {expr:?}")
+            Expr::ScalarVariable(_, ids) => {
+                if ids.is_empty() {
+                    return internal_err!("Not a valid ScalarVariable");
+                }
+
+                Ok(if ids.len() == 1 {
+                    ast::Expr::Identifier(
+                        self.new_ident_without_quote_style(ids[0].to_string()),
+                    )
+                } else {
+                    ast::Expr::CompoundIdentifier(
+                        ids.iter()
+                            .map(|i| 
self.new_ident_without_quote_style(i.to_string()))
+                            .collect(),
+                    )
+                })
             }
             Expr::GetIndexedField(_) => {
                 not_impl_err!("Unsupported Expr conversion: {expr:?}")
@@ -497,6 +511,13 @@ impl Unparser<'_> {
         }
     }
 
+    pub(super) fn new_ident_without_quote_style(&self, str: String) -> 
ast::Ident {
+        ast::Ident {
+            value: str,
+            quote_style: None,
+        }
+    }
+
     pub(super) fn binary_op_to_sql(
         &self,
         lhs: ast::Expr,
@@ -868,6 +889,7 @@ mod tests {
     use std::{any::Any, sync::Arc, vec};
 
     use arrow::datatypes::{Field, Schema};
+    use arrow_schema::DataType::Int8;
     use datafusion_common::TableReference;
     use datafusion_expr::{
         case, col, exists,
@@ -1158,6 +1180,17 @@ mod tests {
                 try_cast(col("a"), DataType::UInt32),
                 r#"TRY_CAST("a" AS INTEGER UNSIGNED)"#,
             ),
+            (
+                Expr::ScalarVariable(Int8, vec![String::from("@a")]),
+                r#"@a"#,
+            ),
+            (
+                Expr::ScalarVariable(
+                    Int8,
+                    vec![String::from("@root"), String::from("foo")],
+                ),
+                r#"@root.foo"#,
+            ),
             (col("x").eq(placeholder("$1")), r#"("x" = $1)"#),
             (
                 out_ref_col(DataType::Int32, "t.a").gt(lit(1)),


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to