Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
56 changes: 55 additions & 1 deletion datafusion/sql/src/unparser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,9 @@ impl Unparser<'_> {
match func_name {
"make_array" => self.make_array_to_sql(args),
"array_element" => self.array_element_to_sql(args),
// TODO: support for the construct and access functions of the `map` and `struct` types
"named_struct" => self.named_struct_to_sql(args),
"get_field" => self.get_field_to_sql(args),
// TODO: support for the construct and access functions of the `map` type
_ => self.scalar_function_to_sql_internal(func_name, args),
}
}
Expand Down Expand Up @@ -514,6 +516,52 @@ impl Unparser<'_> {
})
}

fn named_struct_to_sql(&self, args: &[Expr]) -> Result<ast::Expr> {
if args.len() % 2 != 0 {
return internal_err!("named_struct must have an even number of arguments");
}

let args = args
.chunks_exact(2)
.map(|chunk| {
let key = match &chunk[0] {
Expr::Literal(lit) => self.new_ident_quoted_if_needs(lit.to_string()),
_ => return internal_err!("named_struct arg is invalid"),
};

Ok(ast::DictionaryField {
key,
value: Box::new(self.expr_to_sql(&chunk[1])?),
})
})
.collect::<Result<Vec<_>>>()?;

Ok(ast::Expr::Dictionary(args))
}

fn get_field_to_sql(&self, args: &[Expr]) -> Result<ast::Expr> {
if args.len() != 2 {
return internal_err!("get_field must have exactly 2 arguments");
}

let mut id = match &args[0] {
Expr::Column(col) => match self.col_to_sql(col)? {
ast::Expr::Identifier(ident) => vec![ident],
ast::Expr::CompoundIdentifier(idents) => idents,
_ => unreachable!(),
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
_ => unreachable!(),
_ => return internal_err!("get_field expects an Identifier or CompoundIdentifier, but received: {:?}", other)

I feel like unreachable should be avoided

},
_ => return internal_err!("get_field column is invalid"),
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
_ => return internal_err!("get_field column is invalid"),
_ => return internal_err!("get_field expects first argument to be column, but received {:?}", args[0]),

Error message should be more clear here for feedback

};

let field = match &args[1] {
Expr::Literal(lit) => self.new_ident_quoted_if_needs(lit.to_string()),
_ => return internal_err!("get_field field_name is invalid"),
};
id.push(field);

Ok(ast::Expr::CompoundIdentifier(id))
}

pub fn sort_to_sql(&self, sort: &Sort) -> Result<ast::OrderByExpr> {
let Sort {
expr,
Expand Down Expand Up @@ -1524,6 +1572,7 @@ mod tests {
Signature, Volatility, WindowFrame, WindowFunctionDefinition,
};
use datafusion_expr::{interval_month_day_nano_lit, ExprFunctionExt};
use datafusion_functions::expr_fn::{get_field, named_struct};
use datafusion_functions_aggregate::count::count_udaf;
use datafusion_functions_aggregate::expr_fn::sum;
use datafusion_functions_nested::expr_fn::{array_element, make_array};
Expand Down Expand Up @@ -1937,6 +1986,11 @@ mod tests {
array_element(make_array(vec![lit(1), lit(2), lit(3)]), lit(1)),
"[1, 2, 3][1]",
),
(
named_struct(vec![lit("a"), lit("1"), lit("b"), lit(2)]),
"{a: '1', b: 2}",
),
(get_field(col("a.b"), "c"), "a.b.c"),
];

for (expr, expected) in tests {
Expand Down
4 changes: 3 additions & 1 deletion datafusion/sql/tests/cases/plan_to_sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,9 @@ fn roundtrip_statement() -> Result<()> {
"SELECT ARRAY[1, 2, 3][1]",
"SELECT [1, 2, 3]",
"SELECT [1, 2, 3][1]",
"SELECT left[1] FROM array"
"SELECT left[1] FROM array",
"SELECT {a:1, b:2}",
"SELECT s.a FROM (SELECT {a:1, b:2} AS s)"
];

// For each test sql string, we transform as follows:
Expand Down