-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Support OrderBy and Sort in Expr->String #10256
Comments
@kevinmingtarja @alamb What do you think? 🤔 |
I don't think we should change SQL parser In general, For example, this isn't valid; SELECT x ORDER BY y FROM foo; ORDER BY can appear in certain places like SELECT x FROM foo ORDER BY y
SELECT agg(x ORDER BY y) FROM foo;
SELECT agg(..) OVER (ORDER BY y) FROM foo I think we should special case finding Maybe the code would be clearer if we made the conversion code return an Error if it encountered a SortExpr in an unexpected location 🤔 |
Yeah I was kinda wondering about this too last time (#9726 (comment)). I was thinking we don't change the SQL parser too. Instead, I think given that the "sources" of the conversion to So what if we extend the return type of For example in here, we know that we are converting a datafusion/datafusion/sql/src/expr/order_by.rs Lines 24 to 36 in f8c623f
|
Thank you all for your reply!
Yeah, I was just a little curious why for Expr::Sort we ignore the ORDER BY information. It seems that for the entire query, it could address the Sort. datafusion/datafusion/sql/src/unparser/plan.rs Lines 222 to 231 in f8c623f
Yeah, so if we encountered a
ast::Function is an enum item in ast::Expr, see |
I think so
Creating SQL from programatically created it seems like the issue is that Currently we have datafusion/datafusion/sql/src/unparser/expr.rs Lines 47 to 50 in dd56837
Maybe we could change the signature to something like /// DataFusion's Exprs can represent either an `Expr` or an `OrderByExpr`
enum Unparsed {
// SQL Expression
Expr(ast::Expr),
// SQL ORDER BY expression (e.g. `col ASC NULLS FIRST`)
OrderByExpr(ast::OrderByExpr),
}
pub fn expr_to_sql(expr: &Expr) -> Result<Unparsed> {
...
} |
Good, I can try to implement it. :) For example, There would be something like
|
Maybe we could make the API easier to use like: /// DataFusion's Exprs can represent either an `Expr` or an `OrderByExpr`
enum Unparsed {
// SQL Expression
Expr(ast::Expr),
// SQL ORDER BY expression (e.g. `col ASC NULLS FIRST`)
OrderByExpr(ast::OrderByExpr),
}
/// Throws an error if Expr can not be represented by an `ast::Expr`
pub fn expr_to_sql(expr: &Expr) -> Result<sql::ast::Expr> {
...
}
/// Returns an `Unparsed` based on the expression
pub fn expr_to_unparsed(expr: &Expr) -> Result<Unparsed> {
...
} And that way you example would be let expr = self.expr_to_sql(left.as_ref())?; In places where order by may be possible then the user could call the |
That looks better! Will try to implement it soon. |
Is your feature request related to a problem or challenge?
The current implementation in #9936 for
Sort
is probably not correct.It ignores
asc
andnull_first
field.IMO, the better struct for
Sort
andorder_by
isOrderByExpr
https://github.com/sqlparser-rs/sqlparser-rs/blob/deaa6d8151c8f90a7e9cbb624876fe0e8b8a165d/src/ast/query.rs#L1444-L1450
However,
OrderByExpr
is not an enum item inExpr
Describe the solution you'd like
two steps:
OrderByExpr
an enum inExpr
(change in sql-parser)Sort
return aOrderByExpr
during Expr->StringDescribe alternatives you've considered
No response
Additional context
No response
The text was updated successfully, but these errors were encountered: