Skip to content

Commit

Permalink
feat: Between expr to sql string (#9803)
Browse files Browse the repository at this point in the history
* feat: Between expr to sql string

* fix: use between logical expr

* fix: format using fmt

* fix: remove redundant field name
  • Loading branch information
sebastian2296 authored Mar 26, 2024
1 parent 349c586 commit 39f4aaf
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions datafusion/sql/src/unparser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) => {
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 39f4aaf

Please sign in to comment.