Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2787,10 +2787,11 @@ impl fmt::Display for Declare {
}

/// Sql options of a `CREATE TABLE` statement.
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub enum CreateTableOptions {
#[default]
None,
/// Options specified using the `WITH` keyword.
/// e.g. `WITH (description = "123")`
Expand Down Expand Up @@ -2819,12 +2820,6 @@ pub enum CreateTableOptions {
TableProperties(Vec<SqlOption>),
}

impl Default for CreateTableOptions {
fn default() -> Self {
Self::None
}
}

impl fmt::Display for CreateTableOptions {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Expand Down
8 changes: 7 additions & 1 deletion src/ast/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3135,12 +3135,18 @@ pub struct Values {
/// Was there an explicit ROWs keyword (MySQL)?
/// <https://dev.mysql.com/doc/refman/8.0/en/values.html>
pub explicit_row: bool,
// MySql supports both VALUES and VALUE keywords.
// <https://dev.mysql.com/doc/refman/9.2/en/insert.html>
pub value_keyword: bool,
pub rows: Vec<Vec<Expr>>,
}

impl fmt::Display for Values {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("VALUES")?;
match self.value_keyword {
true => f.write_str("VALUE")?,
false => f.write_str("VALUES")?,
};
let prefix = if self.explicit_row { "ROW" } else { "" };
let mut delim = "";
for row in &self.rows {
Expand Down
1 change: 1 addition & 0 deletions src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ impl Spanned for Values {
fn span(&self) -> Span {
let Values {
explicit_row: _, // bool,
value_keyword: _,
rows,
} = self;

Expand Down
21 changes: 16 additions & 5 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12528,7 +12528,10 @@ impl<'a> Parser<'a> {
SetExpr::Query(subquery)
} else if self.parse_keyword(Keyword::VALUES) {
let is_mysql = dialect_of!(self is MySqlDialect);
SetExpr::Values(self.parse_values(is_mysql)?)
SetExpr::Values(self.parse_values(is_mysql, false)?)
} else if self.parse_keyword(Keyword::VALUE) {
let is_mysql = dialect_of!(self is MySqlDialect);
SetExpr::Values(self.parse_values(is_mysql, true)?)
} else if self.parse_keyword(Keyword::TABLE) {
SetExpr::Table(Box::new(self.parse_as_table()?))
} else {
Expand Down Expand Up @@ -13832,7 +13835,7 @@ impl<'a> Parser<'a> {
// Snowflake and Databricks allow syntax like below:
// SELECT * FROM VALUES (1, 'a'), (2, 'b') AS t (col1, col2)
// where there are no parentheses around the VALUES clause.
let values = SetExpr::Values(self.parse_values(false)?);
let values = SetExpr::Values(self.parse_values(false, false)?);
let alias = self.maybe_parse_table_alias()?;
Ok(TableFactor::Derived {
lateral: false,
Expand Down Expand Up @@ -16499,7 +16502,11 @@ impl<'a> Parser<'a> {
})
}

pub fn parse_values(&mut self, allow_empty: bool) -> Result<Values, ParserError> {
pub fn parse_values(
&mut self,
allow_empty: bool,
value_keyword: bool,
) -> Result<Values, ParserError> {
let mut explicit_row = false;

let rows = self.parse_comma_separated(|parser| {
Expand All @@ -16517,7 +16524,11 @@ impl<'a> Parser<'a> {
Ok(exprs)
}
})?;
Ok(Values { explicit_row, rows })
Ok(Values {
explicit_row,
rows,
value_keyword,
})
}

pub fn parse_start_transaction(&mut self) -> Result<Statement, ParserError> {
Expand Down Expand Up @@ -16932,7 +16943,7 @@ impl<'a> Parser<'a> {
MergeInsertKind::Row
} else {
self.expect_keyword_is(Keyword::VALUES)?;
let values = self.parse_values(is_mysql)?;
let values = self.parse_values(is_mysql, false)?;
MergeInsertKind::Values(values)
};
MergeAction::Insert(MergeInsertExpr { columns, kind })
Expand Down
3 changes: 3 additions & 0 deletions tests/sqlparser_bigquery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1807,6 +1807,7 @@ fn parse_merge() {
let insert_action = MergeAction::Insert(MergeInsertExpr {
columns: vec![Ident::new("product"), Ident::new("quantity")],
kind: MergeInsertKind::Values(Values {
value_keyword: false,
explicit_row: false,
rows: vec![vec![Expr::value(number("1")), Expr::value(number("2"))]],
}),
Expand Down Expand Up @@ -1951,6 +1952,7 @@ fn parse_merge() {
action: MergeAction::Insert(MergeInsertExpr {
columns: vec![Ident::new("a"), Ident::new("b"),],
kind: MergeInsertKind::Values(Values {
value_keyword: false,
explicit_row: false,
rows: vec![vec![
Expr::value(number("1")),
Expand All @@ -1965,6 +1967,7 @@ fn parse_merge() {
action: MergeAction::Insert(MergeInsertExpr {
columns: vec![],
kind: MergeInsertKind::Values(Values {
value_keyword: false,
explicit_row: false,
rows: vec![vec![
Expr::value(number("1")),
Expand Down
31 changes: 24 additions & 7 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,33 +106,44 @@ fn parse_insert_values() {
let rows2 = vec![row.clone(), row];

let sql = "INSERT customer VALUES (1, 2, 3)";
check_one(sql, "customer", &[], &rows1);
check_one(sql, "customer", &[], &rows1, false);

let sql = "INSERT INTO customer VALUES (1, 2, 3)";
check_one(sql, "customer", &[], &rows1);
check_one(sql, "customer", &[], &rows1, false);

let sql = "INSERT INTO customer VALUES (1, 2, 3), (1, 2, 3)";
check_one(sql, "customer", &[], &rows2);
check_one(sql, "customer", &[], &rows2, false);

let sql = "INSERT INTO public.customer VALUES (1, 2, 3)";
check_one(sql, "public.customer", &[], &rows1);
check_one(sql, "public.customer", &[], &rows1, false);

let sql = "INSERT INTO db.public.customer VALUES (1, 2, 3)";
check_one(sql, "db.public.customer", &[], &rows1);
check_one(sql, "db.public.customer", &[], &rows1, false);

let sql = "INSERT INTO public.customer (id, name, active) VALUES (1, 2, 3)";
check_one(
sql,
"public.customer",
&["id".to_string(), "name".to_string(), "active".to_string()],
&rows1,
false,
);

let sql = r"INSERT INTO t (id, name, active) VALUE (1, 2, 3)";
check_one(
sql,
"t",
&["id".to_string(), "name".to_string(), "active".to_string()],
&rows1,
true,
);

fn check_one(
sql: &str,
expected_table_name: &str,
expected_columns: &[String],
expected_rows: &[Vec<Expr>],
expected_value_keyword: bool,
) {
match verified_stmt(sql) {
Statement::Insert(Insert {
Expand All @@ -147,8 +158,13 @@ fn parse_insert_values() {
assert_eq!(column, &Ident::new(expected_columns[index].clone()));
}
match *source.body {
SetExpr::Values(Values { rows, .. }) => {
assert_eq!(rows.as_slice(), expected_rows)
SetExpr::Values(Values {
rows,
value_keyword,
..
}) => {
assert_eq!(rows.as_slice(), expected_rows);
assert!(value_keyword == expected_value_keyword);
}
_ => unreachable!(),
}
Expand Down Expand Up @@ -9908,6 +9924,7 @@ fn parse_merge() {
action: MergeAction::Insert(MergeInsertExpr {
columns: vec![Ident::new("A"), Ident::new("B"), Ident::new("C")],
kind: MergeInsertKind::Values(Values {
value_keyword: false,
explicit_row: false,
rows: vec![vec![
Expr::CompoundIdentifier(vec![
Expand Down
1 change: 1 addition & 0 deletions tests/sqlparser_databricks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ fn test_databricks_lambdas() {
#[test]
fn test_values_clause() {
let values = Values {
value_keyword: false,
explicit_row: false,
rows: vec![
vec![
Expand Down
9 changes: 9 additions & 0 deletions tests/sqlparser_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1885,6 +1885,7 @@ fn parse_simple_insert() {
Some(Box::new(Query {
with: None,
body: Box::new(SetExpr::Values(Values {
value_keyword: false,
explicit_row: false,
rows: vec![
vec![
Expand Down Expand Up @@ -1950,6 +1951,7 @@ fn parse_ignore_insert() {
Some(Box::new(Query {
with: None,
body: Box::new(SetExpr::Values(Values {
value_keyword: false,
explicit_row: false,
rows: vec![vec![
Expr::Value(
Expand Down Expand Up @@ -1999,6 +2001,7 @@ fn parse_priority_insert() {
Some(Box::new(Query {
with: None,
body: Box::new(SetExpr::Values(Values {
value_keyword: false,
explicit_row: false,
rows: vec![vec![
Expr::Value(
Expand Down Expand Up @@ -2045,6 +2048,7 @@ fn parse_priority_insert() {
Some(Box::new(Query {
with: None,
body: Box::new(SetExpr::Values(Values {
value_keyword: false,
explicit_row: false,
rows: vec![vec![
Expr::Value(
Expand Down Expand Up @@ -2097,6 +2101,7 @@ fn parse_insert_as() {
Some(Box::new(Query {
with: None,
body: Box::new(SetExpr::Values(Values {
value_keyword: false,
explicit_row: false,
rows: vec![vec![Expr::Value(
(Value::SingleQuotedString("2024-01-01".to_string())).with_empty_span()
Expand Down Expand Up @@ -2156,6 +2161,7 @@ fn parse_insert_as() {
Some(Box::new(Query {
with: None,
body: Box::new(SetExpr::Values(Values {
value_keyword: false,
explicit_row: false,
rows: vec![vec![
Expr::value(number("1")),
Expand Down Expand Up @@ -2206,6 +2212,7 @@ fn parse_replace_insert() {
Some(Box::new(Query {
with: None,
body: Box::new(SetExpr::Values(Values {
value_keyword: false,
explicit_row: false,
rows: vec![vec![
Expr::Value(
Expand Down Expand Up @@ -2253,6 +2260,7 @@ fn parse_empty_row_insert() {
Some(Box::new(Query {
with: None,
body: Box::new(SetExpr::Values(Values {
value_keyword: false,
explicit_row: false,
rows: vec![vec![], vec![]]
})),
Expand Down Expand Up @@ -2303,6 +2311,7 @@ fn parse_insert_with_on_duplicate_update() {
Some(Box::new(Query {
with: None,
body: Box::new(SetExpr::Values(Values {
value_keyword: false,
explicit_row: false,
rows: vec![vec![
Expr::Value(
Expand Down
3 changes: 3 additions & 0 deletions tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5169,6 +5169,7 @@ fn test_simple_postgres_insert_with_alias() {
source: Some(Box::new(Query {
with: None,
body: Box::new(SetExpr::Values(Values {
value_keyword: false,
explicit_row: false,
rows: vec![vec![
Expr::Identifier(Ident::new("DEFAULT")),
Expand Down Expand Up @@ -5238,6 +5239,7 @@ fn test_simple_postgres_insert_with_alias() {
source: Some(Box::new(Query {
with: None,
body: Box::new(SetExpr::Values(Values {
value_keyword: false,
explicit_row: false,
rows: vec![vec![
Expr::Identifier(Ident::new("DEFAULT")),
Expand Down Expand Up @@ -5309,6 +5311,7 @@ fn test_simple_insert_with_quoted_alias() {
source: Some(Box::new(Query {
with: None,
body: Box::new(SetExpr::Values(Values {
value_keyword: false,
explicit_row: false,
rows: vec![vec![
Expr::Identifier(Ident::new("DEFAULT")),
Expand Down
Loading