Skip to content

Commit

Permalink
Implement unparse ScalarVariable to String
Browse files Browse the repository at this point in the history
  • Loading branch information
reswqa committed May 16, 2024
1 parent 7535d93 commit e207155
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions datafusion/sql/src/unparser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,18 @@ 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(ids[0].to_string()))
} else {
ast::Expr::CompoundIdentifier(
ids.iter().map(|i| self.new_ident(i.to_string())).collect(),
)
})
}
Expr::GetIndexedField(_) => {
not_impl_err!("Unsupported Expr conversion: {expr:?}")
Expand Down Expand Up @@ -870,6 +880,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,
Expand Down Expand Up @@ -1160,6 +1171,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""#,
),
];

for (expr, expected) in tests {
Expand Down

0 comments on commit e207155

Please sign in to comment.