fn parse_parenthesized_expr(&mut self) -> Result<Expr<Raw>, ParserError> {
        // The SQL grammar has an irritating ambiguity that presents here.
        // Consider these two expression fragments:
        //
        //     SELECT (((SELECT 2)) + 3)
        //     SELECT (((SELECT 2)) UNION SELECT 2)
        //             ^           ^
        //            (1)         (2)
        // When we see the parenthesis marked (1), we have no way to know ahead
        // of time whether that parenthesis is part of a `SetExpr::Query` inside
        // of an `Expr::Subquery` or whether it introduces an `Expr::Nested`.
        // The approach taken here avoids backtracking by deferring the decision
        // of whether to parse as a subquery or a nested expression until we get
        // to the point marked (2) above. Once there, we know that the presence
        // of a set operator implies that the parentheses belonged to a the
        // subquery; otherwise, they belonged to the expression.
        //
        // See also PostgreSQL's comments on the matter:
        // https://github.com/postgres/postgres/blob/42c63ab/src/backend/parser/gram.y#L11125-L11136

        enum Either {
            Query(Query<Raw>),
            Expr(Expr<Raw>),
        }

        impl Either {
            fn into_expr(self) -> Expr<Raw> {
                match self {
                    Either::Query(query) => Expr::Subquery(Box::new(query)),
                    Either::Expr(expr) => expr,
                }
            }

            fn nest(self) -> Either {
                // Need to be careful to maintain expression nesting to preserve
                // operator precedence. But there are no precedence concerns for
                // queries, so we flatten to match parse_query's behavior.
                match self {
                    Either::Expr(expr) => Either::Expr(Expr::Nested(Box::new(expr))),
                    Either::Query(_) => self,
                }
            }
        }

        // Recursive helper for parsing parenthesized expressions. Each call
        // handles one layer of parentheses. Before every call, the parser must
        // be positioned after an opening parenthesis; upon non-error return,
        // the parser will be positioned before the corresponding close
        // parenthesis. Somewhat weirdly, the returned expression semantically
        // includes the opening/closing parentheses, even though this function
        // is not responsible for parsing them.
        fn parse(parser: &mut Parser) -> Result<Either, ParserError> {
            if parser.peek_keyword(SELECT) || parser.peek_keyword(WITH) {
                // Easy case one: unambiguously a subquery.
                Ok(Either::Query(parser.parse_query()?))
            } else if !parser.consume_token(&Token::LParen) {
                // Easy case two: unambiguously an expression.
                let exprs = parser.parse_comma_separated(Parser::parse_expr)?;
                if exprs.len() == 1 {
                    Ok(Either::Expr(Expr::Nested(Box::new(exprs.into_element()))))
                } else {
                    Ok(Either::Expr(Expr::Row { exprs }))
                }
            } else {
                // Hard case: we have an open parenthesis, and we need to decide
                // whether it belongs to the query or the expression.

                // Parse to the closing parenthesis.
                let either = parser.checked_recur_mut(parse)?;
                parser.expect_token(&Token::RParen)?;

                // Decide if we need to associate any tokens after the closing
                // parenthesis with what we've parsed so far.
                match (either, parser.peek_token()) {
                    // The next token is another closing parenthesis. Can't
                    // resolve the amibiguity yet. Return to let our caller
                    // handle it.
                    (either, Some(Token::RParen)) => Ok(either.nest()),

                    // The next token is a comma, which means `either` was the
                    // first expression in an implicit row constructor.
                    (either, Some(Token::Comma)) => {
                        let mut exprs = vec![either.into_expr()];
                        while parser.consume_token(&Token::Comma) {
                            exprs.push(parser.parse_expr()?);
                        }
                        Ok(Either::Expr(Expr::Row { exprs }))
                    }

                    // We have a subquery and the next token is a set operator.
                    // That implies we have a partially-parsed subquery (or a
                    // syntax error). Hop into parsing a set expression where
                    // our subquery is the LHS of the set operator.
                    (Either::Query(query), Some(Token::Keyword(kw)))
                        if matches!(kw, UNION | INTERSECT | EXCEPT) =>
                    {
                        let query = SetExpr::Query(Box::new(query));
                        let ctes = vec![];
                        let body = parser.parse_query_body_seeded(SetPrecedence::Zero, query)?;
                        Ok(Either::Query(parser.parse_query_tail(ctes, body)?))
                    }

                    // The next token is something else. That implies we have a
                    // partially-parsed expression (or a syntax error). Hop into
                    // parsing an expression where `either` is the expression
                    // prefix.
                    (either, _) => {
                        let prefix = either.into_expr();
                        let expr = parser.parse_subexpr_seeded(Precedence::Zero, prefix)?;
                        Ok(Either::Expr(expr).nest())
                    }
                }
            }
        }

        Ok(parse(self)?.into_expr())
    }
/// Parses an expression that appears in parentheses, like `(1 + 1)` or
/// `(SELECT 1)`. Assumes that the opening parenthesis has already been
/// parsed. Parses up to the closing parenthesis without consuming it.
Parses an expression that appears in parentheses, like `(1 + 1)` or
`(SELECT 1)`. Assumes that the opening parenthesis has already been
parsed. Parses up to the closing parenthesis without consuming it.
Parses an expression that appears in parentheses like ` ( 1 + 1 ) ` or ` ( SELECT 1 ) ` . Assumes that the opening parenthesis has already been parsed . Parses up to the closing parenthesis without consuming it .
fn parse_parenthesized_expr(&mut self) -> Result<Expr<Raw>, ParserError> {
        // The SQL grammar has an irritating ambiguity that presents here.
        // Consider these two expression fragments:
        //
        //     SELECT (((SELECT 2)) + 3)
        //     SELECT (((SELECT 2)) UNION SELECT 2)
        //             ^           ^
        //            (1)         (2)
        // When we see the parenthesis marked (1), we have no way to know ahead
        // of time whether that parenthesis is part of a `SetExpr::Query` inside
        // of an `Expr::Subquery` or whether it introduces an `Expr::Nested`.
        // The approach taken here avoids backtracking by deferring the decision
        // of whether to parse as a subquery or a nested expression until we get
        // to the point marked (2) above. Once there, we know that the presence
        // of a set operator implies that the parentheses belonged to a the
        // subquery; otherwise, they belonged to the expression.
        //
        // See also PostgreSQL's comments on the matter:
        // https://github.com/postgres/postgres/blob/42c63ab/src/backend/parser/gram.y#L11125-L11136
        enum Either {
            Query(Query<Raw>),
            Expr(Expr<Raw>),
        }
        impl Either {
            fn into_expr(self) -> Expr<Raw> {
                match self {
                    Either::Query(query) => Expr::Subquery(Box::new(query)),
                    Either::Expr(expr) => expr,
                }
            }
            fn nest(self) -> Either {
                // Need to be careful to maintain expression nesting to preserve
                // operator precedence. But there are no precedence concerns for
                // queries, so we flatten to match parse_query's behavior.
                match self {
                    Either::Expr(expr) => Either::Expr(Expr::Nested(Box::new(expr))),
                    Either::Query(_) => self,
                }
            }
        }
        // Recursive helper for parsing parenthesized expressions. Each call
        // handles one layer of parentheses. Before every call, the parser must
        // be positioned after an opening parenthesis; upon non-error return,
        // the parser will be positioned before the corresponding close
        // parenthesis. Somewhat weirdly, the returned expression semantically
        // includes the opening/closing parentheses, even though this function
        // is not responsible for parsing them.
        fn parse(parser: &mut Parser) -> Result<Either, ParserError> {
            if parser.peek_keyword(SELECT) || parser.peek_keyword(WITH) {
                // Easy case one: unambiguously a subquery.
                Ok(Either::Query(parser.parse_query()?))
            } else if !parser.consume_token(&Token::LParen) {
                // Easy case two: unambiguously an expression.
                let exprs = parser.parse_comma_separated(Parser::parse_expr)?;
                if exprs.len() == 1 {
                    Ok(Either::Expr(Expr::Nested(Box::new(exprs.into_element()))))
                } else {
                    Ok(Either::Expr(Expr::Row { exprs }))
                }
            } else {
                // Hard case: we have an open parenthesis, and we need to decide
                // whether it belongs to the query or the expression.
                // Parse to the closing parenthesis.
                let either = parser.checked_recur_mut(parse)?;
                parser.expect_token(&Token::RParen)?;
                // Decide if we need to associate any tokens after the closing
                // parenthesis with what we've parsed so far.
                match (either, parser.peek_token()) {
                    // The next token is another closing parenthesis. Can't
                    // resolve the amibiguity yet. Return to let our caller
                    // handle it.
                    (either, Some(Token::RParen)) => Ok(either.nest()),
                    // The next token is a comma, which means `either` was the
                    // first expression in an implicit row constructor.
                    (either, Some(Token::Comma)) => {
                        let mut exprs = vec![either.into_expr()];
                        while parser.consume_token(&Token::Comma) {
                            exprs.push(parser.parse_expr()?);
                        }
                        Ok(Either::Expr(Expr::Row { exprs }))
                    }
                    // We have a subquery and the next token is a set operator.
                    // That implies we have a partially-parsed subquery (or a
                    // syntax error). Hop into parsing a set expression where
                    // our subquery is the LHS of the set operator.
                    (Either::Query(query), Some(Token::Keyword(kw)))
                        if matches!(kw, UNION | INTERSECT | EXCEPT) =>
                    {
                        let query = SetExpr::Query(Box::new(query));
                        let ctes = vec![];
                        let body = parser.parse_query_body_seeded(SetPrecedence::Zero, query)?;
                        Ok(Either::Query(parser.parse_query_tail(ctes, body)?))
                    }
                    // The next token is something else. That implies we have a
                    // partially-parsed expression (or a syntax error). Hop into
                    // parsing an expression where `either` is the expression
                    // prefix.
                    (either, _) => {
                        let prefix = either.into_expr();
                        let expr = parser.parse_subexpr_seeded(Precedence::Zero, prefix)?;
                        Ok(Either::Expr(expr).nest())
                    }
                }
            }
        }
        Ok(parse(self)?.into_expr())
    }
fn parse_parenthesized_expr ( & mut self ) -> Result < Expr < Raw > , ParserError > { // The SQL grammar has an irritating ambiguity that presents here. // Consider these two expression fragments: // //     SELECT (((SELECT 2)) + 3) //     SELECT (((SELECT 2)) UNION SELECT 2) //             ^           ^ //            (1)         (2) // When we see the parenthesis marked (1), we have no way to know ahead // of time whether that parenthesis is part of a `SetExpr::Query` inside // of an `Expr::Subquery` or whether it introduces an `Expr::Nested`. // The approach taken here avoids backtracking by deferring the decision // of whether to parse as a subquery or a nested expression until we get // to the point marked (2) above. Once there, we know that the presence // of a set operator implies that the parentheses belonged to a the // subquery; otherwise, they belonged to the expression. // // See also PostgreSQL's comments on the matter: // https://github.com/postgres/postgres/blob/42c63ab/src/backend/parser/gram.y#L11125-L11136 enum Either { Query ( Query < Raw > ) , Expr ( Expr < Raw > ) , } impl Either { fn into_expr ( self ) -> Expr < Raw > { match self { Either :: Query ( query ) => Expr :: Subquery ( Box :: new ( query ) ) , Either :: Expr ( expr ) => expr , } } fn nest ( self ) -> Either { // Need to be careful to maintain expression nesting to preserve // operator precedence. But there are no precedence concerns for // queries, so we flatten to match parse_query's behavior. match self { Either :: Expr ( expr ) => Either :: Expr ( Expr :: Nested ( Box :: new ( expr ) ) ) , Either :: Query ( _ ) => self , } } } // Recursive helper for parsing parenthesized expressions. Each call // handles one layer of parentheses. Before every call, the parser must // be positioned after an opening parenthesis; upon non-error return, // the parser will be positioned before the corresponding close // parenthesis. Somewhat weirdly, the returned expression semantically // includes the opening/closing parentheses, even though this function // is not responsible for parsing them. fn parse ( parser : & mut Parser ) -> Result < Either , ParserError > { if parser . peek_keyword ( SELECT ) || parser . peek_keyword ( WITH ) { // Easy case one: unambiguously a subquery. Ok ( Either :: Query ( parser . parse_query ( ) ? ) ) } else if ! parser . consume_token ( & Token :: LParen ) { // Easy case two: unambiguously an expression. let exprs = parser . parse_comma_separated ( Parser :: parse_expr ) ? ; if exprs . len ( ) == 1 { Ok ( Either :: Expr ( Expr :: Nested ( Box :: new ( exprs . into_element ( ) ) ) ) ) } else { Ok ( Either :: Expr ( Expr :: Row { exprs } ) ) } } else { // Hard case: we have an open parenthesis, and we need to decide // whether it belongs to the query or the expression. // Parse to the closing parenthesis. let either = parser . checked_recur_mut ( parse ) ? ; parser . expect_token ( & Token :: RParen ) ? ; // Decide if we need to associate any tokens after the closing // parenthesis with what we've parsed so far. match ( either , parser . peek_token ( ) ) { // The next token is another closing parenthesis. Can't // resolve the amibiguity yet. Return to let our caller // handle it. ( either , Some ( Token :: RParen ) ) => Ok ( either . nest ( ) ) , // The next token is a comma, which means `either` was the // first expression in an implicit row constructor. ( either , Some ( Token :: Comma ) ) => { let mut exprs = vec ! [ either into_expr ( ) ] ; while parser . consume_token ( & Token :: Comma ) { exprs . push ( parser . parse_expr ( ) ? ) ; } Ok ( Either :: Expr ( Expr :: Row { exprs } ) ) } // We have a subquery and the next token is a set operator. // That implies we have a partially-parsed subquery (or a // syntax error). Hop into parsing a set expression where // our subquery is the LHS of the set operator. ( Either :: Query ( query ) , Some ( Token :: Keyword ( kw ) ) ) if matches ! ( kw UNION INTERSECT EXCEPT ) => { let query = SetExpr :: Query ( Box :: new ( query ) ) ; let ctes = vec ! [ ] ; let body = parser . parse_query_body_seeded ( SetPrecedence :: Zero , query ) ? ; Ok ( Either :: Query ( parser . parse_query_tail ( ctes , body ) ? ) ) } // The next token is something else. That implies we have a // partially-parsed expression (or a syntax error). Hop into // parsing an expression where `either` is the expression // prefix. ( either , _ ) => { let prefix = either . into_expr ( ) ; let expr = parser . parse_subexpr_seeded ( Precedence :: Zero , prefix ) ? ; Ok ( Either :: Expr ( expr ) . nest ( ) ) } } } } Ok ( parse ( self ) ? . into_expr ( ) ) }
Parses an expression that appears in parentheses, like `(1 + 1)` or
`(SELECT 1)`.
Parses an expression that appears in parentheses like ` ( 1 + 1 ) ` or ` ( SELECT 1 ) ` .
&mut self
&mut self