mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-09-03 20:50:33 +00:00
Support arbitrary INSERT sources
INSERT takes arbitrary queries as sources, not just VALUES clauses. For example, `INSERT INTO foo SELECT * FROM bar` is perfectly valid.
This commit is contained in:
parent
9420070a0d
commit
14e07ebdda
3 changed files with 17 additions and 11 deletions
|
@ -342,8 +342,8 @@ pub enum SQLStatement {
|
||||||
table_name: SQLObjectName,
|
table_name: SQLObjectName,
|
||||||
/// COLUMNS
|
/// COLUMNS
|
||||||
columns: Vec<SQLIdent>,
|
columns: Vec<SQLIdent>,
|
||||||
/// VALUES (vector of rows to insert)
|
/// A SQL query that specifies what to insert
|
||||||
values: SQLValues,
|
source: Box<SQLQuery>,
|
||||||
},
|
},
|
||||||
SQLCopy {
|
SQLCopy {
|
||||||
/// TABLE
|
/// TABLE
|
||||||
|
@ -409,13 +409,13 @@ impl ToString for SQLStatement {
|
||||||
SQLStatement::SQLInsert {
|
SQLStatement::SQLInsert {
|
||||||
table_name,
|
table_name,
|
||||||
columns,
|
columns,
|
||||||
values,
|
source,
|
||||||
} => {
|
} => {
|
||||||
let mut s = format!("INSERT INTO {}", table_name.to_string());
|
let mut s = format!("INSERT INTO {} ", table_name.to_string());
|
||||||
if !columns.is_empty() {
|
if !columns.is_empty() {
|
||||||
s += &format!(" ({})", columns.join(", "));
|
s += &format!("({}) ", columns.join(", "));
|
||||||
}
|
}
|
||||||
s += &format!(" {}", values.to_string());
|
s += &source.to_string();
|
||||||
s
|
s
|
||||||
}
|
}
|
||||||
SQLStatement::SQLCopy {
|
SQLStatement::SQLCopy {
|
||||||
|
|
|
@ -1565,12 +1565,11 @@ impl Parser {
|
||||||
self.expect_keyword("INTO")?;
|
self.expect_keyword("INTO")?;
|
||||||
let table_name = self.parse_object_name()?;
|
let table_name = self.parse_object_name()?;
|
||||||
let columns = self.parse_parenthesized_column_list(Optional)?;
|
let columns = self.parse_parenthesized_column_list(Optional)?;
|
||||||
self.expect_keyword("VALUES")?;
|
let source = Box::new(self.parse_query()?);
|
||||||
let values = self.parse_values()?;
|
|
||||||
Ok(SQLStatement::SQLInsert {
|
Ok(SQLStatement::SQLInsert {
|
||||||
table_name,
|
table_name,
|
||||||
columns,
|
columns,
|
||||||
values,
|
source,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -52,16 +52,23 @@ fn parse_insert_values() {
|
||||||
SQLStatement::SQLInsert {
|
SQLStatement::SQLInsert {
|
||||||
table_name,
|
table_name,
|
||||||
columns,
|
columns,
|
||||||
values,
|
source,
|
||||||
..
|
..
|
||||||
} => {
|
} => {
|
||||||
assert_eq!(table_name.to_string(), expected_table_name);
|
assert_eq!(table_name.to_string(), expected_table_name);
|
||||||
assert_eq!(columns, expected_columns);
|
assert_eq!(columns, expected_columns);
|
||||||
assert_eq!(values.0.as_slice(), expected_rows);
|
match &source.body {
|
||||||
|
SQLSetExpr::Values(SQLValues(values)) => {
|
||||||
|
assert_eq!(values.as_slice(), expected_rows)
|
||||||
|
}
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
verified_stmt("INSERT INTO customer WITH foo AS (SELECT 1) SELECT * FROM foo UNION VALUES (1)");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue