Skip to content

Commit

Permalink
remove *requirement* for ROW
Browse files Browse the repository at this point in the history
  • Loading branch information
aljazerzen committed Dec 1, 2022
1 parent 4478808 commit 7fbd21b
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 47 deletions.
4 changes: 0 additions & 4 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,6 @@ pub trait Dialect: Debug + Any {
// return None to fall back to the default behavior
None
}
/// Returns true if VALUES requires ROW keywords in SELECT.
fn values_require_row_in_select(&self) -> bool {
false
}
}

impl dyn Dialect {
Expand Down
4 changes: 0 additions & 4 deletions src/dialect/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,4 @@ impl Dialect for MySqlDialect {
fn is_delimited_identifier_start(&self, ch: char) -> bool {
ch == '`'
}

fn values_require_row_in_select(&self) -> bool {
true
}
}
27 changes: 7 additions & 20 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4162,10 +4162,6 @@ impl<'a> Parser<'a> {
/// by `ORDER BY`. Unlike some other parse_... methods, this one doesn't
/// expect the initial keyword to be already consumed
pub fn parse_query(&mut self) -> Result<Query, ParserError> {
self.parse_query_impl(false)
}

pub fn parse_query_impl(&mut self, within_insert: bool) -> Result<Query, ParserError> {
let with = if self.parse_keyword(Keyword::WITH) {
Some(With {
recursive: self.parse_keyword(Keyword::RECURSIVE),
Expand All @@ -4176,7 +4172,7 @@ impl<'a> Parser<'a> {
};

if !self.parse_keyword(Keyword::INSERT) {
let body = Box::new(self.parse_query_body(0, within_insert)?);
let body = Box::new(self.parse_query_body(0)?);

let order_by = if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
self.parse_comma_separated(Parser::parse_order_by_expr)?
Expand Down Expand Up @@ -4291,11 +4287,7 @@ impl<'a> Parser<'a> {
/// subquery ::= query_body [ order_by_limit ]
/// set_operation ::= query_body { 'UNION' | 'EXCEPT' | 'INTERSECT' } [ 'ALL' ] query_body
/// ```
pub fn parse_query_body(
&mut self,
precedence: u8,
within_insert: bool,
) -> Result<SetExpr, ParserError> {
pub fn parse_query_body(&mut self, precedence: u8) -> Result<SetExpr, ParserError> {
// We parse the expression using a Pratt parser, as in `parse_expr()`.
// Start by parsing a restricted SELECT or a `(subquery)`:
let mut expr = if self.parse_keyword(Keyword::SELECT) {
Expand All @@ -4306,7 +4298,7 @@ impl<'a> Parser<'a> {
self.expect_token(&Token::RParen)?;
SetExpr::Query(Box::new(subquery))
} else if self.parse_keyword(Keyword::VALUES) {
SetExpr::Values(self.parse_values(within_insert)?)
SetExpr::Values(self.parse_values()?)
} else {
return self.expected(
"SELECT, VALUES, or a subquery in the query body",
Expand Down Expand Up @@ -4334,7 +4326,7 @@ impl<'a> Parser<'a> {
left: Box::new(expr),
op: op.unwrap(),
set_quantifier,
right: Box::new(self.parse_query_body(next_precedence, within_insert)?),
right: Box::new(self.parse_query_body(next_precedence)?),
};
}

Expand Down Expand Up @@ -5234,7 +5226,7 @@ impl<'a> Parser<'a> {
// Hive allows you to specify columns after partitions as well if you want.
let after_columns = self.parse_parenthesized_column_list(Optional)?;

let source = Box::new(self.parse_query_impl(true)?);
let source = Box::new(self.parse_query()?);
let on = if self.parse_keyword(Keyword::ON) {
if self.parse_keyword(Keyword::CONFLICT) {
let conflict_target =
Expand Down Expand Up @@ -5490,17 +5482,12 @@ impl<'a> Parser<'a> {
}
}

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

let rows = self.parse_comma_separated(|parser| {
if parser.parse_keyword(Keyword::ROW) {
explicit_row = true;
} else {
if !within_insert && parser.dialect.values_require_row_in_select() {
parser
.expected(format!("{:?}", &Keyword::ROW).as_str(), parser.peek_token())?;
}
}

parser.expect_token(&Token::LParen)?;
Expand Down Expand Up @@ -5674,7 +5661,7 @@ impl<'a> Parser<'a> {
}
let columns = self.parse_parenthesized_column_list(Optional)?;
self.expect_keyword(Keyword::VALUES)?;
let values = self.parse_values(true)?;
let values = self.parse_values()?;
MergeClause::NotMatched {
predicate,
columns,
Expand Down
25 changes: 6 additions & 19 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,8 @@ fn parse_top_level() {
verified_stmt("SELECT 1");
verified_stmt("(SELECT 1)");
verified_stmt("((SELECT 1))");
all_but_mysql().verified_stmt("VALUES (1)");
verified_stmt("VALUES (1)");
verified_stmt("VALUES ROW(1, true, 'a'), ROW(2, false, 'b')");
}

#[test]
Expand Down Expand Up @@ -4235,9 +4236,10 @@ fn parse_union_except_intersect() {

#[test]
fn parse_values() {
all_but_mysql().verified_stmt("SELECT * FROM (VALUES (1), (2), (3))");
all_but_mysql().verified_stmt("SELECT * FROM (VALUES (1), (2), (3)), (VALUES (1, 2, 3))");
all_but_mysql().verified_stmt("SELECT * FROM (VALUES (1)) UNION VALUES (1)");
verified_stmt("SELECT * FROM (VALUES (1), (2), (3))");
verified_stmt("SELECT * FROM (VALUES (1), (2), (3)), (VALUES (1, 2, 3))");
verified_stmt("SELECT * FROM (VALUES (1)) UNION VALUES (1)");
verified_stmt("SELECT * FROM (VALUES ROW(1, true, 'a'), ROW(2, false, 'b')) AS t (a, b, c)");
}

#[test]
Expand Down Expand Up @@ -5685,21 +5687,6 @@ fn verified_expr(query: &str) -> Expr {
all_dialects().verified_expr(query)
}

fn all_but_mysql() -> TestedDialects {
TestedDialects {
dialects: vec![
Box::new(GenericDialect {}),
Box::new(PostgreSqlDialect {}),
Box::new(MsSqlDialect {}),
Box::new(AnsiDialect {}),
Box::new(SnowflakeDialect {}),
Box::new(HiveDialect {}),
Box::new(RedshiftSqlDialect {}),
Box::new(BigQueryDialect {}),
],
}
}

#[test]
fn parse_offset_and_limit() {
let sql = "SELECT foo FROM bar LIMIT 2 OFFSET 2";
Expand Down

0 comments on commit 7fbd21b

Please sign in to comment.