Skip to content

Commit 5c946a0

Browse files
committed
rebased, updated to not allow values as valid subquery
1 parent 7443e10 commit 5c946a0

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

src/parser/mod.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7135,6 +7135,16 @@ impl<'a> Parser<'a> {
71357135
} else if self.consume_token(&Token::LParen) {
71367136
// CTEs are not allowed here, but the parser currently accepts them
71377137
let subquery = self.parse_query()?;
7138+
7139+
let is_generic = dialect_of!(self is GenericDialect);
7140+
if !is_generic {
7141+
if let SetExpr::Values(_) = *subquery.body {
7142+
return Err(ParserError::ParserError(format!(
7143+
"VALUES is not a recognized subquery"
7144+
)))
7145+
}
7146+
}
7147+
71387148
self.expect_token(&Token::RParen)?;
71397149
SetExpr::Query(Box::new(subquery))
71407150
} else if self.parse_keyword(Keyword::VALUES) {
@@ -10152,7 +10162,35 @@ mod tests {
1015210162
fn test_replace_into_select() {
1015310163
let sql = r#"REPLACE INTO t1 (a, b, c) (SELECT * FROM t2)"#;
1015410164

10155-
assert!(Parser::parse_sql(&GenericDialect {}, sql).is_err());
10165+
assert!(Parser::parse_sql(&GenericDialect {}, sql).is_ok());
10166+
}
10167+
10168+
#[test]
10169+
fn test_insert_into_select() {
10170+
let sql = r#"INSERT INTO t1 (a, b, c) (SELECT * FROM t2)"#;
10171+
10172+
assert!(Parser::parse_sql(&GenericDialect {}, sql).is_ok());
10173+
}
10174+
10175+
#[test]
10176+
fn test_insert_into_values() {
10177+
let sql = r#"INSERT INTO t1 (a) VALUES(1)"#;
10178+
10179+
assert!(Parser::parse_sql(&GenericDialect {}, sql).is_ok());
10180+
}
10181+
10182+
#[test]
10183+
fn test_insert_into_values_wrapped() {
10184+
let sql = r#"INSERT INTO t1 (a) (VALUES(1))"#;
10185+
10186+
assert!(Parser::parse_sql(&GenericDialect {}, sql).is_ok());
10187+
}
10188+
10189+
#[test]
10190+
fn test_insert_into_values_wrapped_not_generic() {
10191+
let sql = r#"INSERT INTO t1 (a) (VALUES(1))"#;
10192+
10193+
assert!(Parser::parse_sql(&MySqlDialect {}, sql).is_err());
1015610194
}
1015710195

1015810196
#[test]

tests/sqlparser_common.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,6 @@ fn parse_insert_values() {
110110

111111
// allow parenthesis query after insert into
112112
verified_stmt("INSERT INTO tbla (cola) (SELECT cola FROM tblb)");
113-
114-
// allow parenthesis query after insert into
115-
verified_stmt("INSERT INTO tbla (cola) (VALUES (1))");
116113
}
117114

118115
#[test]

0 commit comments

Comments
 (0)