You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The benfit of this approach over strings is that the compiler can check / ensure the exprs are constructed correctly. However, I think the usecase of usign strings to, for example, serialize Exprs and let users provide arbitrary filters makes a String --> Expr API still useful
I took a quick look at the impl for sql_to_expr and on down the code paths from there. Removing the requirement for a schema may be problematic since so much of the api requires a schema for handling identifiers, aliases and type information.
I took a quick look at the impl for sql_to_expr and on down the code paths from there. Removing the requirement for a schema may be problematic since so much of the api requires a schema for handling identifiers, aliases and type information.
I remade this comment as my previous example is not proper here. I am thinking of the usages of DataFrame::parse_sql, e.g.,
let ctx = SessionContext::new();// Read the data from a csv filelet df = ctx.read_csv("tests/data/example.csv",CsvReadOptions::new()).await?;let expr1 = df.parse_sql("a > 10");let expr2 = df.parse_sql("a < 10");
This is reasonable where df_schema is store local inside the dataframe.
Activity
alamb commentedon Jan 3, 2024
Also related #7165
I think this also came up a few days ago but I can't find the reference (maybe @devinjdangelo remembers 🤔 )
Jefffrey commentedon Jan 3, 2024
Also relates to #4890 I believe
devinjdangelo commentedon Jan 3, 2024
#8661 perhaps? Though that is talking about going in the opposite direction.
jiacai2050 commentedon Jan 4, 2024
If maintainers thinks this is a reasonable request, and no one is working on this, I will draft a PR in next few days.
alamb commentedon Jan 4, 2024
Making exprs from strings sounds like a good idea to me.
BTW you can also use the more fluent style API to make exprs, which while still tedious is still a bit better:
Rather than
You can write
The benfit of this approach over strings is that the compiler can check / ensure the exprs are constructed correctly. However, I think the usecase of usign strings to, for example, serialize Exprs and let users provide arbitrary filters makes a
String-->ExprAPI still usefulalamb commentedon Mar 7, 2024
I think you can do this today via
SqlToRel::sql_to_exprStep 1: Add an example to show how to use
SqlToRel::sql_to_exprSo add an example (perhaps
expr_to_sql.rs) inhttps://github.com/apache/arrow-datafusion/tree/main/datafusion-examples/examples that showed how to call
SqlToRelto parser the expression aboveStep 2: Add an API to DataFusion that made parsing SQL easier
The idea is to avoid requiring PlannerContext, a
DFSchema, etc).For example:
Note this doesn't provide a schema.
[-]Generate an Expr from string[/-][+]Support parsing SQL strings to `Expr`s[/+]Omega359 commentedon Mar 10, 2024
This is what I have been using in my POC (still a WIP, ignore the panic :) )
alamb commentedon Mar 11, 2024
Thanks @Omega359 -- that looks nice. I think the next step is to turn it into a function with some doc strings and an example
Omega359 commentedon Mar 11, 2024
I took a quick look at the impl for sql_to_expr and on down the code paths from there. Removing the requirement for a schema may be problematic since so much of the api requires a schema for handling identifiers, aliases and type information.
alamb commentedon Mar 11, 2024
Maybe we could use an empty schema (e.g. https://docs.rs/datafusion/latest/datafusion/common/struct.DFSchema.html#method.empty) if there were no column references
I agree that if there are column references in the expression, the user will need to provide a schema for parsing
backkem commentedon Mar 13, 2024
This new testcase has some similar code:
https://github.com/apache/arrow-datafusion/blob/3b61004a02978843ff02a06c8f1f3ce3e88e4a6d/datafusion/sql/tests/sql_integration.rs#L4501-L4509
Maybe it can be cleaned up when the new API is added.
alamb commentedon Jun 18, 2024
This came up again on discord: https://discord.com/channels/885562378132000778/1166447479609376850/1252616019357470731
Here is some code that @Omega359 shared
I think it would be great to add an API to
DataFrameandSessionContextto do this:Perhaps @xinlifoobar has time to help here 🤔
xinlifoobar commentedon Jun 19, 2024
I am trying to look into this.
xinlifoobar commentedon Jun 19, 2024
I remade this comment as my previous example is not proper here. I am thinking of the usages of
DataFrame::parse_sql, e.g.,This is reasonable where df_schema is store local inside the dataframe.
alamb commentedon Jun 21, 2024
@xinlifoobar has a very nice PR to add this API: #10995