Struct datafusion_sql::unparser::Unparser

source ·
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<'_>

source

pub fn expr_to_sql(&self, expr: &Expr) -> Result<Expr>

source

pub fn expr_to_unparsed(&self, expr: &Expr) -> Result<Unparsed>

This function can convert more Expr types than expr_to_sql, returning an Unparsed like Sort expressions to OrderByExpr expressions.

source§

impl Unparser<'_>

source

pub fn plan_to_sql(&self, plan: &LogicalPlan) -> Result<Statement>

source§

impl<'a> Unparser<'a>

source

pub fn new(dialect: &'a dyn Dialect) -> Self

source

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§

source§

impl<'a> Default for Unparser<'a>

source§

fn default() -> Self

Returns the “default value” for a type. Read more

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> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.