mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-08-08 08:18:02 +00:00
Add SQLOrderBy struct to replace ASTNode::OrderByExpr
This commit is contained in:
parent
e19d559073
commit
d58e59324b
2 changed files with 21 additions and 10 deletions
|
@ -72,7 +72,7 @@ pub enum ASTNode {
|
|||
/// WHERE
|
||||
selection: Option<Box<ASTNode>>,
|
||||
/// ORDER BY
|
||||
order_by: Option<Vec<ASTNode>>,
|
||||
order_by: Option<Vec<SQLOrderByExpr>>,
|
||||
/// GROUP BY
|
||||
group_by: Option<Vec<ASTNode>>,
|
||||
/// HAVING
|
||||
|
@ -105,7 +105,7 @@ pub enum ASTNode {
|
|||
/// WHERE
|
||||
selection: Option<Box<ASTNode>>,
|
||||
/// ORDER BY
|
||||
order_by: Option<Vec<ASTNode>>,
|
||||
order_by: Option<Vec<SQLOrderByExpr>>,
|
||||
limit: Option<Box<ASTNode>>,
|
||||
},
|
||||
/// CREATE TABLE
|
||||
|
@ -124,6 +124,20 @@ pub struct SQLAssigment {
|
|||
value: Box<ASTNode>
|
||||
}
|
||||
|
||||
/// SQL ORDER BY expression
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct SQLOrderByExpr {
|
||||
pub expr: Box<ASTNode>,
|
||||
pub asc: bool
|
||||
}
|
||||
|
||||
impl SQLOrderByExpr {
|
||||
pub fn new(expr: Box<ASTNode>,
|
||||
asc: bool) -> Self {
|
||||
SQLOrderByExpr { expr, asc }
|
||||
}
|
||||
}
|
||||
|
||||
/// SQL column definition
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct SQLColumnDef {
|
||||
|
|
|
@ -563,8 +563,8 @@ impl Parser {
|
|||
}
|
||||
|
||||
/// Parse a comma-delimited list of SQL ORDER BY expressions
|
||||
pub fn parse_order_by_expr_list(&mut self) -> Result<Vec<ASTNode>, ParserError> {
|
||||
let mut expr_list: Vec<ASTNode> = vec![];
|
||||
pub fn parse_order_by_expr_list(&mut self) -> Result<Vec<SQLOrderByExpr>, ParserError> {
|
||||
let mut expr_list: Vec<SQLOrderByExpr> = vec![];
|
||||
loop {
|
||||
let expr = self.parse_expr(0)?;
|
||||
|
||||
|
@ -590,10 +590,7 @@ impl Parser {
|
|||
None => true,
|
||||
};
|
||||
|
||||
expr_list.push(ASTNode::SQLOrderBy {
|
||||
expr: Box::new(expr),
|
||||
asc,
|
||||
});
|
||||
expr_list.push(SQLOrderByExpr::new(Box::new(expr),asc));
|
||||
|
||||
if let Some(t) = self.peek_token() {
|
||||
if t == Token::Comma {
|
||||
|
@ -805,11 +802,11 @@ mod tests {
|
|||
ASTNode::SQLSelect { order_by, .. } => {
|
||||
assert_eq!(
|
||||
Some(vec![
|
||||
ASTNode::SQLOrderBy {
|
||||
SQLOrderByExpr {
|
||||
expr: Box::new(ASTNode::SQLIdentifier("lname".to_string())),
|
||||
asc: true,
|
||||
},
|
||||
ASTNode::SQLOrderBy {
|
||||
SQLOrderByExpr {
|
||||
expr: Box::new(ASTNode::SQLIdentifier("fname".to_string())),
|
||||
asc: false,
|
||||
},
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue