Skip to content

Basic support for converting Expr to SQL string #9495

@alamb

Description

@alamb

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:

  1. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions