mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-10-13 15:32:03 +00:00
Stricter parsing for subqueries (3/4)
This makes the parser more strict when handling SELECTs nested somewhere in the main statement: 1) instead of accepting SELECT anywhere in the expression where an operand was expected, we only accept it inside parens. (I've added a test for the currently supported syntax, <scalar subquery> in ANSI SQL terms) 2) instead of accepting any expression in the derived table context: `FROM ( ... )` - we only look for a SELECT subquery there. Due to #1, I had to swith the 'ansi' test from invoking the expression parser to the statement parser.
This commit is contained in:
parent
82dc581639
commit
215820ef66
4 changed files with 48 additions and 21 deletions
|
@ -664,6 +664,13 @@ fn parse_join_syntax_variants() {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_derived_tables() {
|
||||
let sql = "SELECT a.x, b.y FROM (SELECT x FROM foo) AS a CROSS JOIN (SELECT y FROM bar) AS b";
|
||||
let _ = verified_only_select(sql);
|
||||
//TODO: add assertions
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_multiple_statements() {
|
||||
fn test_with(sql1: &str, sql2_kw: &str, sql2_rest: &str) {
|
||||
|
@ -695,6 +702,29 @@ fn parse_multiple_statements() {
|
|||
assert_eq!(0, res.unwrap().len());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_scalar_subqueries() {
|
||||
use self::ASTNode::*;
|
||||
let sql = "(SELECT 1) + (SELECT 2)";
|
||||
match verified_expr(sql) {
|
||||
SQLBinaryExpr {
|
||||
op: SQLOperator::Plus, ..
|
||||
//left: box SQLSubquery { .. },
|
||||
//right: box SQLSubquery { .. },
|
||||
} => assert!(true),
|
||||
_ => assert!(false),
|
||||
};
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_invalid_subquery_without_parens() {
|
||||
let res = parse_sql_statements("SELECT SELECT 1 FROM bar WHERE 1=1 FROM baz");
|
||||
assert_eq!(
|
||||
ParserError::ParserError("Expected end of statement, found: 1".to_string()),
|
||||
res.unwrap_err()
|
||||
);
|
||||
}
|
||||
|
||||
fn only<'a, T>(v: &'a Vec<T>) -> &'a T {
|
||||
assert_eq!(1, v.len());
|
||||
v.first().unwrap()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue