datafusion_sql::unparser

Struct 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 scalar_function_to_sql( &self, func_name: &str, args: &[Expr], ) -> Result<Expr>

Source

pub fn sort_to_sql(&self, sort: &Sort) -> Result<OrderByExpr>

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

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

Source§

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>,

Source§

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.