1515// specific language governing permissions and limitations
1616// under the License.
1717
18+ //! [`Unparser`] for converting `Expr` to SQL text
19+
1820mod ast;
1921mod expr;
2022mod plan;
@@ -27,6 +29,29 @@ pub use plan::plan_to_sql;
2729use self :: dialect:: { DefaultDialect , Dialect } ;
2830pub mod dialect;
2931
32+ /// Convert a DataFusion [`Expr`] to [`sqlparser::ast::Expr`]
33+ ///
34+ /// See [`expr_to_sql`] for background. `Unparser` allows greater control of
35+ /// the conversion, but with a more complicated API.
36+ ///
37+ /// To get more human-readable output, see [`Self::with_pretty`]
38+ ///
39+ /// # Example
40+ /// ```
41+ /// use datafusion_expr::{col, lit};
42+ /// use datafusion_sql::unparser::Unparser;
43+ /// let expr = col("a").gt(lit(4)); // form an expression `a > 4`
44+ /// let unparser = Unparser::default();
45+ /// let sql = unparser.expr_to_sql(&expr).unwrap();// convert to AST
46+ /// // use the Display impl to convert to SQL text
47+ /// assert_eq!(sql.to_string(), "(a > 4)");
48+ /// // now convert to pretty sql
49+ /// let unparser = unparser.with_pretty(true);
50+ /// let sql = unparser.expr_to_sql(&expr).unwrap();
51+ /// assert_eq!(sql.to_string(), "a > 4"); // note lack of parenthesis
52+ /// ```
53+ ///
54+ /// [`Expr`]: datafusion_expr::Expr
3055pub struct Unparser < ' a > {
3156 dialect : & ' a dyn Dialect ,
3257 pretty : bool ,
@@ -40,9 +65,42 @@ impl<'a> Unparser<'a> {
4065 }
4166 }
4267
43- /// Allow unparser to remove parenthesis according to the precedence rules of DataFusion.
44- /// This might make it invalid SQL for other SQL query engines with different precedence
45- /// rules, even if its valid for DataFusion.
68+ /// Create pretty SQL output, better suited for human consumption
69+ ///
70+ /// See example on the struct level documentation
71+ ///
72+ /// # Pretty Output
73+ ///
74+ /// By default, `Unparser` generates SQL text that will parse back to the
75+ /// same parsed [`Expr`], which is useful for creating machine readable
76+ /// expressions to send to other systems. However, the resulting expressions are
77+ /// not always nice to read for humans.
78+ ///
79+ /// For example
80+ ///
81+ /// ```sql
82+ /// ((a + 4) > 5)
83+ /// ```
84+ ///
85+ /// This method removes parenthesis using to the precedence rules of
86+ /// DataFusion. If the output is reparsed, the resulting [`Expr`] produces
87+ /// same value as the original in DataFusion, but with a potentially
88+ /// different order of operations.
89+ ///
90+ /// Note that this setting may create invalid SQL for other SQL query
91+ /// engines with different precedence rules
92+ ///
93+ /// # Example
94+ /// ```
95+ /// use datafusion_expr::{col, lit};
96+ /// use datafusion_sql::unparser::Unparser;
97+ /// let expr = col("a").gt(lit(4)).and(col("b").lt(lit(5))); // form an expression `a > 4 AND b < 5`
98+ /// let unparser = Unparser::default().with_pretty(true);
99+ /// let sql = unparser.expr_to_sql(&expr).unwrap();
100+ /// assert_eq!(sql.to_string(), "a > 4 AND b < 5"); // note lack of parenthesis
101+ /// ```
102+ ///
103+ /// [`Expr`]: datafusion_expr::Expr
46104 pub fn with_pretty ( mut self , pretty : bool ) -> Self {
47105 self . pretty = pretty;
48106 self
0 commit comments