-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Fix Binary & Binary View Unparsing #13427
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
Conversation
* Skip casting to binary when inner expr is value * Update datafusion/sql/src/unparser/expr.rs Co-authored-by: Jack Eadie <[email protected]> --------- Co-authored-by: Jack Eadie <[email protected]>
alamb
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @Sevenannn -- this makes sense to me. I have some small suggestions but I don't think they are needed to merge this PR
| Expr::Cast(Cast { | ||
| expr: Box::new(Expr::Literal(ScalarValue::Utf8(Some( | ||
| "blah".to_string(), | ||
| )))), | ||
| data_type: DataType::Binary, | ||
| }), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can write this more concisely with lit
| Expr::Cast(Cast { | |
| expr: Box::new(Expr::Literal(ScalarValue::Utf8(Some( | |
| "blah".to_string(), | |
| )))), | |
| data_type: DataType::Binary, | |
| }), | |
| Expr::Cast(Cast { | |
| expr: Box::new(lit("blah")) | |
| data_type: DataType::Binary, | |
| }), |
You could also try to use https://docs.rs/datafusion/latest/datafusion/logical_expr/enum.Expr.html#method.cast_to but that requires a valid schema
|
🚀 |
|
Thanks @Sevenannn @phillipleblanc and @Jeadie |
Which issue does this PR close?
N/A
Rationale for this change
Datafusion enforces an eager cast of value to
Binary/BinaryViewvalue in the logical plan where a comparison with binary value is presented. For example, in the following planFilter: value = CAST(Utf8("binary_value") AS BinaryHowever, when using unparser to convert the plan back to sql, where the sql will be sent to various query engine (e.g. DuckDB). the cast is not needed for the value, in the example above the value "binary_value", since the raw value can be directly used in SQL engines without casting to an engine specific dictionary type. Therefore, the plan can simply be rewritten into where value = 'binary_value'
What changes are included in this PR?
cast_to_sql, which directly pass inner_expr instead of cast inner_expr when casting to binary / binaryview / dictionary in the Expr::Cast, and keep the original casting logic in all other cases.Are these changes tested?
Yes
Are there any user-facing changes?
No