mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-10-10 05:52:13 +00:00

Before this commit there was a single `parse_expr(u8)` method, which was called both 1) from within the expression parser (to parse subexpression consisting of operators with higher priority than the current one), and 2) from the top-down parser both a) to parse true expressions (such as an item of the SELECT list or the condition after WHERE or after ON), and b) to parse sequences which are not exactly "expressions". This starts cleaning this up by renaming the `parse_expr(u8)` method to `parse_subexpr()` and using it only for (1) - i.e. usually providing a non-zero precedence parameter. The non-intuitively called `parse()` method is renamed to `parse_expr()`, which became available and is used for (2a). While reviewing the existing callers of `parse_expr`, four points to follow up on were identified (marked "TBD (#)" in the commit): 1) Do not lose parens (e.g. `(1+2)*3`) when roundtripping String->AST->String by using SQLNested. 2) Incorrect precedence of the NOT unary 3) `parse_table_factor` accepts any expression where a SELECT subquery is expected. 4) parse_delete uses parse_expr() to retrieve a table name These are dealt with in the commits to follow.
28 lines
774 B
Rust
28 lines
774 B
Rust
extern crate log;
|
|
extern crate sqlparser;
|
|
|
|
use sqlparser::dialect::AnsiSqlDialect;
|
|
use sqlparser::sqlast::*;
|
|
use sqlparser::sqlparser::*;
|
|
use sqlparser::sqltokenizer::*;
|
|
|
|
#[test]
|
|
fn parse_simple_select() {
|
|
let sql = String::from("SELECT id, fname, lname FROM customer WHERE id = 1");
|
|
let ast = parse_sql_expr(&sql);
|
|
match ast {
|
|
ASTNode::SQLSelect(SQLSelect { projection, .. }) => {
|
|
assert_eq!(3, projection.len());
|
|
}
|
|
_ => assert!(false),
|
|
}
|
|
}
|
|
|
|
fn parse_sql_expr(sql: &str) -> ASTNode {
|
|
let dialect = AnsiSqlDialect {};
|
|
let mut tokenizer = Tokenizer::new(&dialect, &sql);
|
|
let tokens = tokenizer.tokenize().unwrap();
|
|
let mut parser = Parser::new(tokens);
|
|
let ast = parser.parse_expr().unwrap();
|
|
ast
|
|
}
|