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/arrow-datafusion.git
The following commit(s) were added to refs/heads/main by this push:
new 39f4aaf5cd feat: Between expr to sql string (#9803)
39f4aaf5cd is described below
commit 39f4aaf5cd1abfc9204c3eb96effdb4ebcf5b882
Author: Sebastian Espinosa <[email protected]>
AuthorDate: Tue Mar 26 08:39:16 2024 -0500
feat: Between expr to sql string (#9803)
* feat: Between expr to sql string
* fix: use between logical expr
* fix: format using fmt
* fix: remove redundant field name
---
datafusion/sql/src/unparser/expr.rs | 35 +++++++++++++++++++++++++++++++----
1 file changed, 31 insertions(+), 4 deletions(-)
diff --git a/datafusion/sql/src/unparser/expr.rs
b/datafusion/sql/src/unparser/expr.rs
index 49a940060b..550b02cea7 100644
--- a/datafusion/sql/src/unparser/expr.rs
+++ b/datafusion/sql/src/unparser/expr.rs
@@ -97,11 +97,19 @@ impl Unparser<'_> {
}
Expr::Between(Between {
expr,
- negated: _,
- low: _,
- high: _,
+ negated,
+ low,
+ high,
}) => {
- not_impl_err!("Unsupported expression: {expr:?}")
+ let sql_parser_expr = self.expr_to_sql(expr)?;
+ let sql_low = self.expr_to_sql(low)?;
+ let sql_high = self.expr_to_sql(high)?;
+ Ok(ast::Expr::Nested(Box::new(self.between_op_to_sql(
+ sql_parser_expr,
+ *negated,
+ sql_low,
+ sql_high,
+ ))))
}
Expr::Column(col) => self.col_to_sql(col),
Expr::BinaryExpr(BinaryExpr { left, op, right }) => {
@@ -291,6 +299,21 @@ impl Unparser<'_> {
}
}
+ pub(super) fn between_op_to_sql(
+ &self,
+ expr: ast::Expr,
+ negated: bool,
+ low: ast::Expr,
+ high: ast::Expr,
+ ) -> ast::Expr {
+ ast::Expr::Between {
+ expr: Box::new(expr),
+ negated,
+ low: Box::new(low),
+ high: Box::new(high),
+ }
+ }
+
fn op_to_sql(&self, op: &Operator) -> Result<ast::BinaryOperator> {
match op {
Operator::Eq => Ok(ast::BinaryOperator::Eq),
@@ -746,6 +769,10 @@ mod tests {
(col("a") + col("b")).gt(lit(4)).is_unknown(),
r#"(("a" + "b") > 4) IS UNKNOWN"#,
),
+ (
+ Expr::between(col("a"), lit(1), lit(7)),
+ r#"("a" BETWEEN 1 AND 7)"#,
+ ),
];
for (expr, expected) in tests {