pub struct Unparser<'a> { /* private fields */ }
Expand description
Convert a DataFusion Expr
to sqlparser::ast::Expr
See expr_to_sql
for background. Unparser
allows greater control of
the conversion, but with a more complicated API.
To get more human-readable output, see Self::with_pretty
§Example
use datafusion_expr::{col, lit};
use datafusion_sql::unparser::Unparser;
let expr = col("a").gt(lit(4)); // form an expression `a > 4`
let unparser = Unparser::default();
let sql = unparser.expr_to_sql(&expr).unwrap();// convert to AST
// use the Display impl to convert to SQL text
assert_eq!(sql.to_string(), "(a > 4)");
// now convert to pretty sql
let unparser = unparser.with_pretty(true);
let sql = unparser.expr_to_sql(&expr).unwrap();
assert_eq!(sql.to_string(), "a > 4"); // note lack of parenthesis
Implementations§
Source§impl Unparser<'_>
impl Unparser<'_>
pub fn expr_to_sql(&self, expr: &Expr) -> Result<Expr>
pub fn scalar_function_to_sql( &self, func_name: &str, args: &[Expr], ) -> Result<Expr>
pub fn sort_to_sql(&self, sort: &Sort) -> Result<OrderByExpr>
Source§impl Unparser<'_>
impl Unparser<'_>
pub fn plan_to_sql(&self, plan: &LogicalPlan) -> Result<Statement>
Source§impl<'a> Unparser<'a>
impl<'a> Unparser<'a>
pub fn new(dialect: &'a dyn Dialect) -> Self
Sourcepub fn with_pretty(self, pretty: bool) -> Self
pub fn with_pretty(self, pretty: bool) -> Self
Create pretty SQL output, better suited for human consumption
See example on the struct level documentation
§Pretty Output
By default, Unparser
generates SQL text that will parse back to the
same parsed Expr
, which is useful for creating machine readable
expressions to send to other systems. However, the resulting expressions are
not always nice to read for humans.
For example
((a + 4) > 5)
This method removes parenthesis using to the precedence rules of
DataFusion. If the output is reparsed, the resulting Expr
produces
same value as the original in DataFusion, but with a potentially
different order of operations.
Note that this setting may create invalid SQL for other SQL query engines with different precedence rules
§Example
use datafusion_expr::{col, lit};
use datafusion_sql::unparser::Unparser;
let expr = col("a").gt(lit(4)).and(col("b").lt(lit(5))); // form an expression `a > 4 AND b < 5`
let unparser = Unparser::default().with_pretty(true);
let sql = unparser.expr_to_sql(&expr).unwrap();
assert_eq!(sql.to_string(), "a > 4 AND b < 5"); // note lack of parenthesis
Trait Implementations§
Auto Trait Implementations§
impl<'a> Freeze for Unparser<'a>
impl<'a> !RefUnwindSafe for Unparser<'a>
impl<'a> Send for Unparser<'a>
impl<'a> Sync for Unparser<'a>
impl<'a> Unpin for Unparser<'a>
impl<'a> !UnwindSafe for Unparser<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more