Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an example of how to use the SQL parser/unparser API #10524

Closed
Tracked by #9494
alamb opened this issue May 15, 2024 · 2 comments · Fixed by #10528
Closed
Tracked by #9494

Add an example of how to use the SQL parser/unparser API #10524

alamb opened this issue May 15, 2024 · 2 comments · Fixed by #10528

Comments

@alamb
Copy link
Contributor

alamb commented May 15, 2024

Part of #9494

Is your feature request related to a problem or challenge?

Having a good example helps to make features easier to use in DataFusion

In this case the usecase is programmatic construction of SQL using the DataFUsion Expr and LogicalPlan s using the api from #9726

Describe the solution you'd like

  1. Add a new examples directory https://github.com/apache/arrow-datafusion/blob/main/datafusion-examples/examples. Potential names plan_to_sql.rs
  2. Add an example that shows how to create an expression using the expr_fn api (e.g. col("x") > 5 AND col("b") = 'foo') and convert that to a string

Describe alternatives you've considered

No response

Additional context

I would expect to extend the same example to converting LogicalPlan --> SQL query as well once #8661 is sufficiently far along (it may be already, I just haven't checked)

@edmondop
Copy link
Contributor

@alamb I wasn't able to get a simple sql example to pass, if I convert the Expr to String directly, the literal is wrapped in a type like so.

     Running `/home/edmondo/Development/arrow-datafusion/target/debug/examples/plan_to_sql`
thread 'main' panicked at datafusion-examples/examples/plan_to_sql.rs:45:5:
assertion `left == right` failed
  left: "((\"a\" < 5) OR (\"a\" = 8))"
 right: "a < 5 OR a = 8"
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

However, if I use the expr_to_sql, the column names are quoted,

     Running `/home/edmondo/Development/arrow-datafusion/target/debug/examples/plan_to_sql`
thread 'main' panicked at datafusion-examples/examples/plan_to_sql.rs:45:5:
assertion `left == right` failed
  left: "((\"a\" < 5) OR (\"a\" = 8))"
 right: "a < 5 OR a = 8"
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

is it the expectation that column names are always quoted?

@alamb
Copy link
Contributor Author

alamb commented May 15, 2024

is it the expectation that column names are always quoted?

I think so. @phillipleblanc added a way to get the expression back without the quotes -- #10198

Perhaps you could show an example of both?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants