-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
Part of #9494
Is your feature request related to a problem or challenge?
Sometimes people want to export DataFusion expressions as SQL strings, I think to use in other systems (e.g. to push predicates down to postgres)
Most recently this came up in discord: https://discord.com/channels/885562378132000778/1166447479609376850/1215387800715919390
But I am pretty sure I remember it coming up elsewhere
Describe the solution you'd like
I would like a way to convert from Expr
--> SQL string, something like:
// make a > 1 expr
let expr: Expr = col("a").gt(lit(1));
// convert to a string
let sql_string = to_sql(&expr, &schema)?;
assert_eq!(sql_string, "a > 1")
Describe alternatives you've considered
I think the easiest way to do this might be to use the Display
impl of sqlparser::AST
So that would look something like:
- write a transform of
Expr
--> `sqlparser::A
Additional context
#8661 covers the feature for converting entire LogicalPlans back to Expr
#8736 covers the converse converting SQL strings
to Expr
Remaining Tasks
-
feat: Introduce convert Expr to SQL string API and basic feature #9517
-
Upstream changes in
datafusion/sql/src/unparser/dialect.rs
to sqlparser-rs: Add identifier quote style toDialect
trait datafusion-sqlparser-rs#1170 -
Support remaining Expr variants
-
Sorting out binary expr nesting
-
Add Round trip tests