Skip to content

Commit 04271b0

Browse files
authored
Parse INSERT with subquery when lacking column names (#1586)
1 parent 00abaf2 commit 04271b0

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

src/parser/mod.rs

+19-6
Original file line numberDiff line numberDiff line change
@@ -11329,14 +11329,19 @@ impl<'a> Parser<'a> {
1132911329
if self.parse_keywords(&[Keyword::DEFAULT, Keyword::VALUES]) {
1133011330
(vec![], None, vec![], None)
1133111331
} else {
11332-
let columns = self.parse_parenthesized_column_list(Optional, is_mysql)?;
11332+
let (columns, partitioned, after_columns) = if !self.peek_subquery_start() {
11333+
let columns = self.parse_parenthesized_column_list(Optional, is_mysql)?;
1133311334

11334-
let partitioned = self.parse_insert_partition()?;
11335-
// Hive allows you to specify columns after partitions as well if you want.
11336-
let after_columns = if dialect_of!(self is HiveDialect) {
11337-
self.parse_parenthesized_column_list(Optional, false)?
11335+
let partitioned = self.parse_insert_partition()?;
11336+
// Hive allows you to specify columns after partitions as well if you want.
11337+
let after_columns = if dialect_of!(self is HiveDialect) {
11338+
self.parse_parenthesized_column_list(Optional, false)?
11339+
} else {
11340+
vec![]
11341+
};
11342+
(columns, partitioned, after_columns)
1133811343
} else {
11339-
vec![]
11344+
Default::default()
1134011345
};
1134111346

1134211347
let source = Some(self.parse_query()?);
@@ -11431,6 +11436,14 @@ impl<'a> Parser<'a> {
1143111436
}
1143211437
}
1143311438

11439+
/// Returns true if the immediate tokens look like the
11440+
/// beginning of a subquery. `(SELECT ...`
11441+
fn peek_subquery_start(&mut self) -> bool {
11442+
let [maybe_lparen, maybe_select] = self.peek_tokens();
11443+
Token::LParen == maybe_lparen
11444+
&& matches!(maybe_select, Token::Word(w) if w.keyword == Keyword::SELECT)
11445+
}
11446+
1143411447
fn parse_conflict_clause(&mut self) -> Option<SqliteOnConflict> {
1143511448
if self.parse_keywords(&[Keyword::OR, Keyword::REPLACE]) {
1143611449
Some(SqliteOnConflict::Replace)

tests/sqlparser_common.rs

+2
Original file line numberDiff line numberDiff line change
@@ -10964,6 +10964,8 @@ fn insert_into_with_parentheses() {
1096410964
Box::new(GenericDialect {}),
1096510965
]);
1096610966
dialects.verified_stmt("INSERT INTO t1 (id, name) (SELECT t2.id, t2.name FROM t2)");
10967+
dialects.verified_stmt("INSERT INTO t1 (SELECT t2.id, t2.name FROM t2)");
10968+
dialects.verified_stmt(r#"INSERT INTO t1 ("select", name) (SELECT t2.name FROM t2)"#);
1096710969
}
1096810970

1096910971
#[test]

0 commit comments

Comments
 (0)