mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-10-09 21:42:05 +00:00
Stop losing parens when roundtripping (1/4)
Before this change an expression like `(a+b)-(c+d)` was parsed correctly (as a Minus node with two Plus nodes as children), but when serializing back to an SQL string, it came up as a+b-c+d, since we don't store parens in AST and don't attempt to insert them when necessary during serialization. The latter would be hard, and we already had an SQLNested enum variant, so I changed the code to wrap the AST node for the parenthesized expression in it.
This commit is contained in:
parent
b57c60a78c
commit
29db619792
2 changed files with 7 additions and 8 deletions
|
@ -194,9 +194,9 @@ impl Parser {
|
|||
self.parse_sql_value()
|
||||
}
|
||||
Token::LParen => {
|
||||
let expr = self.parse_expr(); // TBD (1)
|
||||
let expr = self.parse_expr()?;
|
||||
self.expect_token(&Token::RParen)?;
|
||||
expr
|
||||
Ok(ASTNode::SQLNested(Box::new(expr)))
|
||||
}
|
||||
_ => parser_err!(format!(
|
||||
"Prefix parser expected a keyword but found {:?}",
|
||||
|
|
|
@ -420,22 +420,21 @@ fn parse_parens() {
|
|||
use self::ASTNode::*;
|
||||
use self::SQLOperator::*;
|
||||
let sql = "(a + b) - (c + d)";
|
||||
let ast = parse_sql_expr(&sql);
|
||||
assert_eq!(
|
||||
SQLBinaryExpr {
|
||||
left: Box::new(SQLBinaryExpr {
|
||||
left: Box::new(SQLNested(Box::new(SQLBinaryExpr {
|
||||
left: Box::new(SQLIdentifier("a".to_string())),
|
||||
op: Plus,
|
||||
right: Box::new(SQLIdentifier("b".to_string()))
|
||||
}),
|
||||
}))),
|
||||
op: Minus,
|
||||
right: Box::new(SQLBinaryExpr {
|
||||
right: Box::new(SQLNested(Box::new(SQLBinaryExpr {
|
||||
left: Box::new(SQLIdentifier("c".to_string())),
|
||||
op: Plus,
|
||||
right: Box::new(SQLIdentifier("d".to_string()))
|
||||
})
|
||||
})))
|
||||
},
|
||||
ast
|
||||
verified_expr(sql)
|
||||
);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue