@@ -355,6 +355,11 @@ pub struct Parser<'a> {
355355 /// `parse_table_factor`. See [`Parser::parse_table_factor`] for the 2^N
356356 /// pattern this guards.
357357 failed_derived_table_factor_positions: BTreeSet<usize>,
358+ /// Cached failures from the speculative subquery arm of [`Parser::parse_in`],
359+ /// which `IN` lists nested in a leading subquery would otherwise retry at
360+ /// every level, taking 2^N time. Maps the arm's start to where a parsed
361+ /// query lacked its `)`.
362+ failed_in_subquery_positions: BTreeMap<usize, Option<usize>>,
358363}
359364
360365/// Copy marker for a [`ParserError`] cached by the `parse_prefix` failure
@@ -401,6 +406,7 @@ impl<'a> Parser<'a> {
401406 failed_prefix_positions: BTreeMap::new(),
402407 failed_reserved_word_prefix_positions: BTreeMap::new(),
403408 failed_derived_table_factor_positions: BTreeSet::new(),
409+ failed_in_subquery_positions: BTreeMap::new(),
404410 }
405411 }
406412
@@ -469,6 +475,7 @@ impl<'a> Parser<'a> {
469475 self.failed_prefix_positions.clear();
470476 self.failed_reserved_word_prefix_positions.clear();
471477 self.failed_derived_table_factor_positions.clear();
478+ self.failed_in_subquery_positions.clear();
472479 self
473480 }
474481
@@ -4464,24 +4471,51 @@ impl<'a> Parser<'a> {
44644471 });
44654472 }
44664473 self.expect_token(&Token::LParen)?;
4467- let in_op = match self.maybe_parse(|p| p.parse_query())? {
4468- Some(subquery) => Expr::InSubquery {
4469- expr: Box::new(expr),
4470- subquery,
4471- negated,
4472- },
4473- None => Expr::InList {
4474+ // A leading query is the whole subquery only when `)` follows it,
4475+ // otherwise it is the first item of a list, as in `IN ((SELECT 1), 2)`.
4476+ let start = self.index;
4477+ // Where a parsed query lacked its `)`. Reported if the list fails no
4478+ // further in, since that error would sit inside the query.
4479+ let missing_rparen = match self.failed_in_subquery_positions.get(&start) {
4480+ Some(&cached) => cached,
4481+ None => {
4482+ let mut missing_rparen = None;
4483+ if let Some(subquery) = self.maybe_parse(|p| p.parse_query())? {
4484+ if self.consume_token(&Token::RParen) {
4485+ return Ok(Expr::InSubquery {
4486+ expr: Box::new(expr),
4487+ subquery,
4488+ negated,
4489+ });
4490+ }
4491+ missing_rparen = Some(self.index);
4492+ self.index = start;
4493+ }
4494+ self.failed_in_subquery_positions
4495+ .insert(start, missing_rparen);
4496+ missing_rparen
4497+ }
4498+ };
4499+ let list = if self.dialect.supports_in_empty_list() {
4500+ self.parse_comma_separated0(Parser::parse_expr, Token::RParen)
4501+ } else {
4502+ self.parse_comma_separated(Parser::parse_expr)
4503+ };
4504+ match list.and_then(|list| self.expect_token(&Token::RParen).map(|_| list)) {
4505+ Ok(list) => Ok(Expr::InList {
44744506 expr: Box::new(expr),
4475- list: if self.dialect.supports_in_empty_list() {
4476- self.parse_comma_separated0(Parser::parse_expr, Token::RParen)?
4477- } else {
4478- self.parse_comma_separated(Parser::parse_expr)?
4479- },
4507+ list,
44804508 negated,
4509+ }),
4510+ Err(ParserError::RecursionLimitExceeded) => Err(ParserError::RecursionLimitExceeded),
4511+ Err(e) => match missing_rparen {
4512+ Some(index) if self.index <= index => {
4513+ self.index = index;
4514+ self.expected_ref(")", self.peek_token_ref())
4515+ }
4516+ _ => Err(e),
44814517 },
4482- };
4483- self.expect_token(&Token::RParen)?;
4484- Ok(in_op)
4518+ }
44854519 }
44864520
44874521 /// Parses `BETWEEN <low> AND <high>`, assuming the `BETWEEN` keyword was already consumed.
@@ -18968,7 +19002,13 @@ impl<'a> Parser<'a> {
1896819002 /// Parse a `var = expr` assignment, used in an UPDATE statement
1896919003 pub fn parse_assignment(&mut self) -> Result<Assignment, ParserError> {
1897019004 let target = self.parse_assignment_target()?;
18971- self.expect_token(&Token::Eq)?;
19005+ if self.dialect.supports_double_eq_assignment() {
19006+ if !self.consume_token(&Token::Eq) && !self.consume_token(&Token::DoubleEq) {
19007+ return self.expected("= or == (for assignment)", self.peek_token());
19008+ }
19009+ } else {
19010+ self.expect_token(&Token::Eq)?;
19011+ }
1897219012 let value = self.parse_expr()?;
1897319013 Ok(Assignment { target, value })
1897419014 }
0 commit comments