risingwave_sqlparser/
parser.rs

1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License at
4//
5//     http://www.apache.org/licenses/LICENSE-2.0
6//
7// Unless required by applicable law or agreed to in writing, software
8// distributed under the License is distributed on an "AS IS" BASIS,
9// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10// See the License for the specific language governing permissions and
11// limitations under the License.
12
13//! SQL Parser
14
15#[cfg(not(feature = "std"))]
16use alloc::{
17    boxed::Box,
18    format,
19    string::{String, ToString},
20    vec,
21    vec::Vec,
22};
23use core::fmt;
24
25use ddl::WebhookSourceInfo;
26use itertools::Itertools;
27use tracing::{debug, instrument};
28use winnow::combinator::{alt, cut_err, dispatch, fail, opt, peek, preceded, repeat, separated};
29use winnow::{ModalResult, Parser as _};
30
31use crate::ast::*;
32use crate::keywords::{self, Keyword};
33use crate::parser_v2::{
34    ParserExt as _, dollar_quoted_string, keyword, literal_i64, literal_uint, single_quoted_string,
35    token_number,
36};
37use crate::tokenizer::*;
38use crate::{impl_parse_to, parser_v2};
39
40pub(crate) const UPSTREAM_SOURCE_KEY: &str = "connector";
41pub(crate) const WEBHOOK_CONNECTOR: &str = "webhook";
42
43const WEBHOOK_WAIT_FOR_PERSISTENCE: &str = "webhook.wait_for_persistence";
44
45#[derive(Debug, Clone, PartialEq)]
46pub enum ParserError {
47    TokenizerError(String),
48    ParserError(String),
49}
50
51impl ParserError {
52    pub fn inner_msg(self) -> String {
53        match self {
54            ParserError::TokenizerError(s) | ParserError::ParserError(s) => s,
55        }
56    }
57}
58
59#[derive(Debug, thiserror::Error)]
60#[error("{0}")]
61pub struct StrError(pub String);
62
63// Use `Parser::expected` instead, if possible
64#[macro_export]
65macro_rules! parser_err {
66    ($($arg:tt)*) => {
67        return Err(winnow::error::ErrMode::Backtrack(<winnow::error::ContextError as winnow::error::FromExternalError<_, _>>::from_external_error(
68            &Parser::default(),
69            $crate::parser::StrError(format!($($arg)*)),
70        )))
71    };
72}
73
74impl From<StrError> for winnow::error::ErrMode<winnow::error::ContextError> {
75    fn from(e: StrError) -> Self {
76        winnow::error::ErrMode::Backtrack(<winnow::error::ContextError as winnow::error::FromExternalError<_, _>>::from_external_error(
77            &Parser::default(),
78            e,
79        ))
80    }
81}
82
83// Returns a successful result if the optional expression is some
84macro_rules! return_ok_if_some {
85    ($e:expr) => {{
86        if let Some(v) = $e {
87            return Ok(v);
88        }
89    }};
90}
91
92#[derive(PartialEq)]
93pub enum IsOptional {
94    Optional,
95    Mandatory,
96}
97
98use IsOptional::*;
99
100pub enum IsLateral {
101    Lateral,
102    NotLateral,
103}
104
105use IsLateral::*;
106
107use crate::ast::ddl::AlterFragmentOperation;
108
109pub type IncludeOption = Vec<IncludeOptionItem>;
110
111#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
112#[derive(Eq, Clone, Debug, PartialEq, Hash)]
113pub struct IncludeOptionItem {
114    pub column_type: Ident,
115    pub column_alias: Option<Ident>,
116    pub inner_field: Option<String>,
117    pub header_inner_expect_type: Option<DataType>,
118}
119
120#[derive(Debug)]
121pub enum WildcardOrExpr {
122    Expr(Expr),
123    /// Expr is an arbitrary expression, returning either a table or a column.
124    /// Idents are the prefix of `*`, which are consecutive field accesses.
125    /// e.g. `(table.v1).*` or `(table).v1.*`
126    ///
127    /// See also [`Expr::FieldIdentifier`] for behaviors of parentheses.
128    ExprQualifiedWildcard(Expr, Vec<Ident>),
129    /// `QualifiedWildcard` and `Wildcard` can be followed by EXCEPT (columns)
130    QualifiedWildcard(ObjectName, Option<Vec<Expr>>),
131    Wildcard(Option<Vec<Expr>>),
132}
133
134impl From<WildcardOrExpr> for FunctionArgExpr {
135    fn from(wildcard_expr: WildcardOrExpr) -> Self {
136        match wildcard_expr {
137            WildcardOrExpr::Expr(expr) => Self::Expr(expr),
138            WildcardOrExpr::ExprQualifiedWildcard(expr, prefix) => {
139                Self::ExprQualifiedWildcard(expr, prefix)
140            }
141            WildcardOrExpr::QualifiedWildcard(prefix, except) => {
142                Self::QualifiedWildcard(prefix, except)
143            }
144            WildcardOrExpr::Wildcard(except) => Self::Wildcard(except),
145        }
146    }
147}
148
149impl From<TokenizerError> for ParserError {
150    fn from(e: TokenizerError) -> Self {
151        ParserError::TokenizerError(e.to_string())
152    }
153}
154
155impl fmt::Display for ParserError {
156    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
157        write!(
158            f,
159            "sql parser error: {}",
160            match self {
161                ParserError::TokenizerError(s) => s,
162                ParserError::ParserError(s) => s,
163            }
164        )
165    }
166}
167
168#[cfg(feature = "std")]
169impl std::error::Error for ParserError {}
170
171type ColumnsDefTuple = (
172    Vec<ColumnDef>,
173    Vec<TableConstraint>,
174    Vec<SourceWatermark>,
175    Option<usize>,
176);
177
178/// Reference:
179/// <https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-PRECEDENCE>
180#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
181pub enum Precedence {
182    Zero = 0,
183    LogicalOr, // 5 in upstream
184    LogicalXor,
185    LogicalAnd, // 10 in upstream
186    UnaryNot,   // 15 in upstream
187    Is,         // 17 in upstream
188    Cmp,
189    Like,    // 19 in upstream
190    Between, // 20 in upstream
191    Other,
192    PlusMinus, // 30 in upstream
193    MulDiv,    // 40 in upstream
194    Exp,
195    At,
196    Collate,
197    UnaryPosNeg,
198    PostfixFactorial,
199    Array,
200    DoubleColon, // 50 in upstream
201}
202
203#[derive(Clone, Copy, Default)]
204pub struct Parser<'a>(pub(crate) &'a [TokenWithLocation]);
205
206impl Parser<'_> {
207    /// Parse a SQL statement and produce an Abstract Syntax Tree (AST)
208    #[instrument(level = "debug")]
209    pub fn parse_sql(sql: &str) -> Result<Vec<Statement>, ParserError> {
210        let mut tokenizer = Tokenizer::new(sql);
211        let tokens = tokenizer.tokenize_with_location()?;
212        let parser = Parser(&tokens);
213        let stmts = Parser::parse_statements.parse(parser).map_err(|e| {
214            // append SQL context to the error message, e.g.:
215            // LINE 1: SELECT 1::int(2);
216            let loc = match tokens.get(e.offset()) {
217                Some(token) => token.location.clone(),
218                None => {
219                    // get location of EOF
220                    Location {
221                        line: sql.lines().count() as u64,
222                        column: sql.lines().last().map_or(0, |l| l.len() as u64) + 1,
223                    }
224                }
225            };
226            let prefix = format!("LINE {}: ", loc.line);
227            let sql_line = sql.split('\n').nth(loc.line as usize - 1).unwrap();
228            let cursor = " ".repeat(prefix.len() + loc.column as usize - 1);
229            ParserError::ParserError(format!(
230                "{}\n{}{}\n{}^",
231                e.inner().to_string().replace('\n', ": "),
232                prefix,
233                sql_line,
234                cursor
235            ))
236        })?;
237        Ok(stmts)
238    }
239
240    /// Parse exactly one statement from a string.
241    pub fn parse_exactly_one(sql: &str) -> Result<Statement, ParserError> {
242        Parser::parse_sql(sql)
243            .map_err(|e| {
244                ParserError::ParserError(format!("failed to parse definition sql: {}", e))
245            })?
246            .into_iter()
247            .exactly_one()
248            .map_err(|e| {
249                ParserError::ParserError(format!(
250                    "expecting exactly one statement in definition: {}",
251                    e
252                ))
253            })
254    }
255
256    /// Parse object name from a string.
257    pub fn parse_object_name_str(s: &str) -> Result<ObjectName, ParserError> {
258        let mut tokenizer = Tokenizer::new(s);
259        let tokens = tokenizer.tokenize_with_location()?;
260        let parser = Parser(&tokens);
261        Parser::parse_object_name
262            .parse(parser)
263            .map_err(|e| ParserError::ParserError(e.inner().to_string()))
264    }
265
266    /// Parse function description from a string.
267    pub fn parse_function_desc_str(func: &str) -> Result<FunctionDesc, ParserError> {
268        let mut tokenizer = Tokenizer::new(func);
269        let tokens = tokenizer.tokenize_with_location()?;
270        let parser = Parser(&tokens);
271        Parser::parse_function_desc
272            .parse(parser)
273            .map_err(|e| ParserError::ParserError(e.inner().to_string()))
274    }
275
276    /// Parse a list of semicolon-separated statements.
277    fn parse_statements(&mut self) -> ModalResult<Vec<Statement>> {
278        let mut stmts = Vec::new();
279        let mut expecting_statement_delimiter = false;
280        loop {
281            // ignore empty statements (between successive statement delimiters)
282            while self.consume_token(&Token::SemiColon) {
283                expecting_statement_delimiter = false;
284            }
285
286            if self.peek_token() == Token::EOF {
287                break;
288            }
289            if expecting_statement_delimiter {
290                return self.expected("end of statement");
291            }
292
293            let statement = self.parse_statement()?;
294            stmts.push(statement);
295            expecting_statement_delimiter = true;
296        }
297        debug!("parsed statements:\n{:#?}", stmts);
298        Ok(stmts)
299    }
300
301    /// Parse a single top-level statement (such as SELECT, INSERT, CREATE, etc.),
302    /// stopping before the statement separator, if any.
303    pub fn parse_statement(&mut self) -> ModalResult<Statement> {
304        let checkpoint = *self;
305        let token = self.next_token();
306        match token.token {
307            Token::Word(w) => match w.keyword {
308                Keyword::EXPLAIN => Ok(self.parse_explain()?),
309                Keyword::ANALYZE => Ok(self.parse_analyze()?),
310                Keyword::SELECT | Keyword::WITH | Keyword::VALUES => {
311                    *self = checkpoint;
312                    Ok(Statement::Query(Box::new(self.parse_query()?)))
313                }
314                Keyword::DECLARE => Ok(self.parse_declare()?),
315                Keyword::FETCH => Ok(self.parse_fetch_cursor()?),
316                Keyword::CLOSE => Ok(self.parse_close_cursor()?),
317                Keyword::TRUNCATE => Ok(self.parse_truncate()?),
318                Keyword::CREATE => Ok(self.parse_create()?),
319                Keyword::DISCARD => Ok(self.parse_discard()?),
320                Keyword::DROP => Ok(self.parse_drop()?),
321                Keyword::DELETE => Ok(self.parse_delete()?),
322                Keyword::INSERT => Ok(self.parse_insert()?),
323                Keyword::UPDATE => Ok(self.parse_update()?),
324                Keyword::ALTER => Ok(self.parse_alter()?),
325                Keyword::COPY => Ok(self.parse_copy()?),
326                Keyword::SET => Ok(self.parse_set()?),
327                Keyword::SHOW => {
328                    if self.parse_keyword(Keyword::CREATE) {
329                        Ok(self.parse_show_create()?)
330                    } else {
331                        Ok(self.parse_show()?)
332                    }
333                }
334                Keyword::CANCEL => Ok(self.parse_cancel_job()?),
335                Keyword::KILL => Ok(self.parse_kill_process()?),
336                Keyword::DESCRIBE => Ok(self.parse_describe()?),
337                Keyword::GRANT => Ok(self.parse_grant()?),
338                Keyword::REVOKE => Ok(self.parse_revoke()?),
339                Keyword::START => Ok(self.parse_start_transaction()?),
340                Keyword::ABORT => Ok(Statement::Abort),
341                // `BEGIN` is a nonstandard but common alias for the
342                // standard `START TRANSACTION` statement. It is supported
343                // by at least PostgreSQL and MySQL.
344                Keyword::BEGIN => Ok(self.parse_begin()?),
345                Keyword::COMMIT => Ok(self.parse_commit()?),
346                Keyword::ROLLBACK => Ok(self.parse_rollback()?),
347                // `PREPARE`, `EXECUTE` and `DEALLOCATE` are Postgres-specific
348                // syntaxes. They are used for Postgres prepared statement.
349                Keyword::DEALLOCATE => Ok(self.parse_deallocate()?),
350                Keyword::EXECUTE => Ok(self.parse_execute()?),
351                Keyword::PREPARE => Ok(self.parse_prepare()?),
352                Keyword::COMMENT => Ok(self.parse_comment()?),
353                Keyword::FLUSH => Ok(Statement::Flush),
354                Keyword::WAIT => Ok(Statement::Wait),
355                Keyword::RECOVER => Ok(Statement::Recover),
356                Keyword::USE => Ok(self.parse_use()?),
357                _ => self.expected_at(checkpoint, "statement"),
358            },
359            Token::LParen => {
360                *self = checkpoint;
361                Ok(Statement::Query(Box::new(self.parse_query()?)))
362            }
363            _ => self.expected_at(checkpoint, "statement"),
364        }
365    }
366
367    pub fn parse_truncate(&mut self) -> ModalResult<Statement> {
368        let _ = self.parse_keyword(Keyword::TABLE);
369        let table_name = self.parse_object_name()?;
370        Ok(Statement::Truncate { table_name })
371    }
372
373    pub fn parse_analyze(&mut self) -> ModalResult<Statement> {
374        let table_name = self.parse_object_name()?;
375
376        Ok(Statement::Analyze { table_name })
377    }
378
379    /// Tries to parse a wildcard expression. If it is not a wildcard, parses an expression.
380    ///
381    /// A wildcard expression either means:
382    /// - Selecting all fields from a struct. In this case, it is a
383    ///   [`WildcardOrExpr::ExprQualifiedWildcard`]. Similar to [`Expr::FieldIdentifier`], It must
384    ///   contain parentheses.
385    /// - Selecting all columns from a table. In this case, it is a
386    ///   [`WildcardOrExpr::QualifiedWildcard`] or a [`WildcardOrExpr::Wildcard`].
387    pub fn parse_wildcard_or_expr(&mut self) -> ModalResult<WildcardOrExpr> {
388        let checkpoint = *self;
389
390        match self.next_token().token {
391            Token::Word(w) if self.peek_token() == Token::Period => {
392                // Since there's no parenthesis, `w` must be a column or a table
393                // So what follows must be dot-delimited identifiers, e.g. `a.b.c.*`
394                let wildcard_expr = self.parse_simple_wildcard_expr(checkpoint)?;
395                return self.word_concat_wildcard_expr(w.to_ident()?, wildcard_expr);
396            }
397            Token::Mul => {
398                return Ok(WildcardOrExpr::Wildcard(self.parse_except()?));
399            }
400            // parses wildcard field selection expression.
401            // Code is similar to `parse_struct_selection`
402            Token::LParen => {
403                let mut expr = self.parse_expr()?;
404                if self.consume_token(&Token::RParen) {
405                    // Unwrap parentheses
406                    while let Expr::Nested(inner) = expr {
407                        expr = *inner;
408                    }
409                    // Now that we have an expr, what follows must be
410                    // dot-delimited identifiers, e.g. `b.c.*` in `(a).b.c.*`
411                    let wildcard_expr = self.parse_simple_wildcard_expr(checkpoint)?;
412                    return self.expr_concat_wildcard_expr(expr, wildcard_expr);
413                }
414            }
415            _ => (),
416        };
417
418        *self = checkpoint;
419        self.parse_expr().map(WildcardOrExpr::Expr)
420    }
421
422    /// Concats `ident` and `wildcard_expr` in `ident.wildcard_expr`
423    pub fn word_concat_wildcard_expr(
424        &mut self,
425        ident: Ident,
426        simple_wildcard_expr: WildcardOrExpr,
427    ) -> ModalResult<WildcardOrExpr> {
428        let mut idents = vec![ident];
429        let mut except_cols = vec![];
430        match simple_wildcard_expr {
431            WildcardOrExpr::QualifiedWildcard(ids, except) => {
432                idents.extend(ids.0);
433                if let Some(cols) = except {
434                    except_cols = cols;
435                }
436            }
437            WildcardOrExpr::Wildcard(except) => {
438                if let Some(cols) = except {
439                    except_cols = cols;
440                }
441            }
442            WildcardOrExpr::ExprQualifiedWildcard(_, _) => unreachable!(),
443            WildcardOrExpr::Expr(e) => return Ok(WildcardOrExpr::Expr(e)),
444        }
445        Ok(WildcardOrExpr::QualifiedWildcard(
446            ObjectName(idents),
447            if except_cols.is_empty() {
448                None
449            } else {
450                Some(except_cols)
451            },
452        ))
453    }
454
455    /// Concats `expr` and `wildcard_expr` in `(expr).wildcard_expr`.
456    pub fn expr_concat_wildcard_expr(
457        &mut self,
458        expr: Expr,
459        simple_wildcard_expr: WildcardOrExpr,
460    ) -> ModalResult<WildcardOrExpr> {
461        if let WildcardOrExpr::Expr(e) = simple_wildcard_expr {
462            return Ok(WildcardOrExpr::Expr(e));
463        }
464
465        // similar to `parse_struct_selection`
466        let mut idents = vec![];
467        let expr = match expr {
468            // expr is `(foo)`
469            Expr::Identifier(_) => expr,
470            // expr is `(foo.v1)`
471            Expr::CompoundIdentifier(_) => expr,
472            // expr is `((1,2,3)::foo)`
473            Expr::Cast { .. } => expr,
474            // expr is `(func())`
475            Expr::Function(_) => expr,
476            // expr is `((foo.v1).v2)`
477            Expr::FieldIdentifier(expr, ids) => {
478                // Put `ids` to the latter part!
479                idents.extend(ids);
480                *expr
481            }
482            // expr is other things, e.g., `(1+2)`. It will become an unexpected period error at
483            // upper level.
484            _ => return Ok(WildcardOrExpr::Expr(expr)),
485        };
486
487        match simple_wildcard_expr {
488            WildcardOrExpr::QualifiedWildcard(ids, except) => {
489                if except.is_some() {
490                    return self.expected("Expr quantified wildcard does not support except");
491                }
492                idents.extend(ids.0);
493            }
494            WildcardOrExpr::Wildcard(except) => {
495                if except.is_some() {
496                    return self.expected("Expr quantified wildcard does not support except");
497                }
498            }
499            WildcardOrExpr::ExprQualifiedWildcard(_, _) => unreachable!(),
500            WildcardOrExpr::Expr(_) => unreachable!(),
501        }
502        Ok(WildcardOrExpr::ExprQualifiedWildcard(expr, idents))
503    }
504
505    /// Tries to parses a wildcard expression without any parentheses.
506    ///
507    /// If wildcard is not found, go back to `index` and parse an expression.
508    pub fn parse_simple_wildcard_expr(&mut self, checkpoint: Self) -> ModalResult<WildcardOrExpr> {
509        let mut id_parts = vec![];
510        while self.consume_token(&Token::Period) {
511            let ckpt = *self;
512            let token = self.next_token();
513            match token.token {
514                Token::Word(w) => id_parts.push(w.to_ident()?),
515                Token::Mul => {
516                    return if id_parts.is_empty() {
517                        Ok(WildcardOrExpr::Wildcard(self.parse_except()?))
518                    } else {
519                        Ok(WildcardOrExpr::QualifiedWildcard(
520                            ObjectName(id_parts),
521                            self.parse_except()?,
522                        ))
523                    };
524                }
525                _ => {
526                    *self = ckpt;
527                    return self.expected("an identifier or a '*' after '.'");
528                }
529            }
530        }
531        *self = checkpoint;
532        self.parse_expr().map(WildcardOrExpr::Expr)
533    }
534
535    pub fn parse_except(&mut self) -> ModalResult<Option<Vec<Expr>>> {
536        if !self.parse_keyword(Keyword::EXCEPT) {
537            return Ok(None);
538        }
539        if !self.consume_token(&Token::LParen) {
540            return self.expected("EXCEPT should be followed by (");
541        }
542        let exprs = self.parse_comma_separated(Parser::parse_expr)?;
543        if self.consume_token(&Token::RParen) {
544            Ok(Some(exprs))
545        } else {
546            self.expected("( should be followed by ) after column names")
547        }
548    }
549
550    /// Parse a new expression
551    pub fn parse_expr(&mut self) -> ModalResult<Expr> {
552        self.parse_subexpr(Precedence::Zero)
553    }
554
555    /// Parse tokens until the precedence changes
556    pub fn parse_subexpr(&mut self, precedence: Precedence) -> ModalResult<Expr> {
557        debug!("parsing expr, current token: {:?}", self.peek_token().token);
558        let mut expr = self.parse_prefix()?;
559        debug!("prefix: {:?}", expr);
560        loop {
561            let next_precedence = self.get_next_precedence()?;
562            debug!("precedence: {precedence:?}, next precedence: {next_precedence:?}");
563
564            if precedence >= next_precedence {
565                break;
566            }
567
568            expr = self.parse_infix(expr, next_precedence)?;
569        }
570        Ok(expr)
571    }
572
573    /// Parse an expression prefix
574    pub fn parse_prefix(&mut self) -> ModalResult<Expr> {
575        // PostgreSQL allows any string literal to be preceded by a type name, indicating that the
576        // string literal represents a literal of that type. Some examples:
577        //
578        //      DATE '2020-05-20'
579        //      TIMESTAMP WITH TIME ZONE '2020-05-20 7:43:54'
580        //      BOOL 'true'
581        //
582        // The first two are standard SQL, while the latter is a PostgreSQL extension. Complicating
583        // matters is the fact that INTERVAL string literals may optionally be followed by special
584        // keywords, e.g.:
585        //
586        //      INTERVAL '7' DAY
587        //
588        // Note also that naively `SELECT date` looks like a syntax error because the `date` type
589        // name is not followed by a string literal, but in fact in PostgreSQL it is a valid
590        // expression that should parse as the column name "date".
591        return_ok_if_some!(self.maybe_parse(|parser| {
592            match parser.parse_data_type()? {
593                DataType::Interval => parser.parse_literal_interval(),
594                // PostgreSQL allows almost any identifier to be used as custom data type name,
595                // and we support that in `parse_data_type()`. But unlike Postgres we don't
596                // have a list of globally reserved keywords (since they vary across dialects),
597                // so given `NOT 'a' LIKE 'b'`, we'd accept `NOT` as a possible custom data type
598                // name, resulting in `NOT 'a'` being recognized as a `TypedString` instead of
599                // an unary negation `NOT ('a' LIKE 'b')`. To solve this, we don't accept the
600                // `type 'string'` syntax for the custom data types at all.
601                DataType::Custom(..) => parser_err!("dummy"),
602                data_type => Ok(Expr::TypedString {
603                    data_type,
604                    value: parser.parse_literal_string()?,
605                }),
606            }
607        }));
608
609        let checkpoint = *self;
610        let token = self.next_token();
611        let expr = match token.token.clone() {
612            Token::Word(w) => match w.keyword {
613                Keyword::TRUE | Keyword::FALSE | Keyword::NULL => {
614                    *self = checkpoint;
615                    Ok(Expr::Value(self.ensure_parse_value()?))
616                }
617                Keyword::CASE => self.parse_case_expr(),
618                Keyword::CAST => self.parse_cast_expr(),
619                Keyword::TRY_CAST => self.parse_try_cast_expr(),
620                Keyword::EXISTS => self.parse_exists_expr(),
621                Keyword::EXTRACT => self.parse_extract_expr(),
622                Keyword::SUBSTRING => self.parse_substring_expr(),
623                Keyword::POSITION => self.parse_position_expr(),
624                Keyword::OVERLAY => self.parse_overlay_expr(),
625                Keyword::TRIM => self.parse_trim_expr(),
626                Keyword::INTERVAL => self.parse_literal_interval(),
627                Keyword::NOT => Ok(Expr::UnaryOp {
628                    op: UnaryOperator::Not,
629                    expr: Box::new(self.parse_subexpr(Precedence::UnaryNot)?),
630                }),
631                Keyword::ROW => self.parse_row_expr(),
632                Keyword::ARRAY if self.peek_token() == Token::LParen => {
633                    // similar to `exists(subquery)`
634                    self.expect_token(&Token::LParen)?;
635                    let exists_node = Expr::ArraySubquery(Box::new(self.parse_query()?));
636                    self.expect_token(&Token::RParen)?;
637                    Ok(exists_node)
638                }
639                Keyword::ARRAY if self.peek_token() == Token::LBracket => self.parse_array_expr(),
640                Keyword::MAP if self.peek_token() == Token::LBrace => self.parse_map_expr(),
641                // `LEFT` and `RIGHT` are reserved as identifier but okay as function
642                Keyword::LEFT | Keyword::RIGHT => {
643                    *self = checkpoint;
644                    self.parse_function()
645                }
646                Keyword::OPERATOR if self.peek_token().token == Token::LParen => {
647                    let op = UnaryOperator::PGQualified(Box::new(self.parse_qualified_operator()?));
648                    Ok(Expr::UnaryOp {
649                        op,
650                        expr: Box::new(self.parse_subexpr(Precedence::Other)?),
651                    })
652                }
653                keyword @ (Keyword::ALL | Keyword::ANY | Keyword::SOME) => {
654                    self.expect_token(&Token::LParen)?;
655                    // In upstream's PR of parser-rs, there is `self.parser_subexpr(precedence)` here.
656                    // But it will fail to parse `select 1 = any(null and true);`.
657                    let sub = self.parse_expr()?;
658                    self.expect_token(&Token::RParen)?;
659
660                    // TODO: support `all/any/some(subquery)`.
661                    if let Expr::Subquery(_) = &sub {
662                        parser_err!("ANY/SOME/ALL(Subquery) is not implemented");
663                    }
664
665                    Ok(match keyword {
666                        Keyword::ALL => Expr::AllOp(Box::new(sub)),
667                        // `SOME` is a synonym for `ANY`.
668                        Keyword::ANY | Keyword::SOME => Expr::SomeOp(Box::new(sub)),
669                        _ => unreachable!(),
670                    })
671                }
672                k if keywords::RESERVED_FOR_COLUMN_OR_TABLE_NAME.contains(&k) => {
673                    parser_err!("syntax error at or near {token}")
674                }
675                // Here `w` is a word, check if it's a part of a multi-part
676                // identifier, a function call, or a simple identifier:
677                _ => match self.peek_token().token {
678                    Token::LParen | Token::Period | Token::Colon => {
679                        *self = checkpoint;
680                        if let Ok(object_name) = self.parse_object_name()
681                            && !matches!(self.peek_token().token, Token::LParen | Token::Colon)
682                        {
683                            Ok(Expr::CompoundIdentifier(object_name.0))
684                        } else {
685                            *self = checkpoint;
686                            self.parse_function()
687                        }
688                    }
689                    _ => Ok(Expr::Identifier(w.to_ident()?)),
690                },
691            }, // End of Token::Word
692
693            tok @ Token::Minus | tok @ Token::Plus => {
694                let op = if tok == Token::Plus {
695                    UnaryOperator::Plus
696                } else {
697                    UnaryOperator::Minus
698                };
699                let mut sub_expr = self.parse_subexpr(Precedence::UnaryPosNeg)?;
700                if let Expr::Value(Value::Number(ref mut s)) = sub_expr {
701                    if tok == Token::Minus {
702                        *s = format!("-{}", s);
703                    }
704                    return Ok(sub_expr);
705                }
706                Ok(Expr::UnaryOp {
707                    op,
708                    expr: Box::new(sub_expr),
709                })
710            }
711            tok @ Token::DoubleExclamationMark
712            | tok @ Token::PGSquareRoot
713            | tok @ Token::PGCubeRoot
714            | tok @ Token::AtSign
715            | tok @ Token::Tilde => {
716                let op = match tok {
717                    Token::DoubleExclamationMark => UnaryOperator::PGPrefixFactorial,
718                    Token::PGSquareRoot => UnaryOperator::PGSquareRoot,
719                    Token::PGCubeRoot => UnaryOperator::PGCubeRoot,
720                    Token::AtSign => UnaryOperator::PGAbs,
721                    Token::Tilde => UnaryOperator::PGBitwiseNot,
722                    _ => unreachable!(),
723                };
724                // Counter-intuitively, `|/ 4 + 12` means `|/ (4+12)` rather than `(|/4) + 12` in
725                // PostgreSQL.
726                Ok(Expr::UnaryOp {
727                    op,
728                    expr: Box::new(self.parse_subexpr(Precedence::Other)?),
729                })
730            }
731            Token::Number(_)
732            | Token::SingleQuotedString(_)
733            | Token::DollarQuotedString(_)
734            | Token::NationalStringLiteral(_)
735            | Token::HexStringLiteral(_)
736            | Token::CstyleEscapesString(_) => {
737                *self = checkpoint;
738                Ok(Expr::Value(self.ensure_parse_value()?))
739            }
740            Token::Parameter(number) => self.parse_param(number),
741            Token::Pipe => {
742                let args = self.parse_comma_separated(Parser::parse_identifier)?;
743                self.expect_token(&Token::Pipe)?;
744                let body = self.parse_expr()?;
745                Ok(Expr::LambdaFunction {
746                    args,
747                    body: Box::new(body),
748                })
749            }
750            Token::LParen => {
751                let expr = if matches!(self.peek_token().token, Token::Word(w) if w.keyword == Keyword::SELECT || w.keyword == Keyword::WITH)
752                {
753                    Expr::Subquery(Box::new(self.parse_query()?))
754                } else {
755                    let mut exprs = self.parse_comma_separated(Parser::parse_expr)?;
756                    if exprs.len() == 1 {
757                        Expr::Nested(Box::new(exprs.pop().unwrap()))
758                    } else {
759                        Expr::Row(exprs)
760                    }
761                };
762                self.expect_token(&Token::RParen)?;
763                if self.peek_token() == Token::Period && matches!(expr, Expr::Nested(_)) {
764                    self.parse_struct_selection(expr)
765                } else {
766                    Ok(expr)
767                }
768            }
769            _ => self.expected_at(checkpoint, "an expression"),
770        }?;
771
772        if self.parse_keyword(Keyword::COLLATE) {
773            Ok(Expr::Collate {
774                expr: Box::new(expr),
775                collation: self.parse_object_name()?,
776            })
777        } else {
778            Ok(expr)
779        }
780    }
781
782    fn parse_param(&mut self, param: String) -> ModalResult<Expr> {
783        let Ok(index) = param.parse() else {
784            parser_err!("Parameter symbol has a invalid index {}.", param);
785        };
786        Ok(Expr::Parameter { index })
787    }
788
789    /// Parses a field selection expression. See also [`Expr::FieldIdentifier`].
790    pub fn parse_struct_selection(&mut self, expr: Expr) -> ModalResult<Expr> {
791        let mut nested_expr = expr;
792        // Unwrap parentheses
793        while let Expr::Nested(inner) = nested_expr {
794            nested_expr = *inner;
795        }
796        let fields = self.parse_fields()?;
797        Ok(Expr::FieldIdentifier(Box::new(nested_expr), fields))
798    }
799
800    /// Parses consecutive field identifiers after a period. i.e., `.foo.bar.baz`
801    pub fn parse_fields(&mut self) -> ModalResult<Vec<Ident>> {
802        repeat(.., preceded(Token::Period, cut_err(Self::parse_identifier))).parse_next(self)
803    }
804
805    pub fn parse_qualified_operator(&mut self) -> ModalResult<QualifiedOperator> {
806        self.expect_token(&Token::LParen)?;
807
808        let checkpoint = *self;
809        let schema = match self.parse_identifier_non_reserved() {
810            Ok(ident) => {
811                self.expect_token(&Token::Period)?;
812                Some(ident)
813            }
814            Err(_) => {
815                *self = checkpoint;
816                None
817            }
818        };
819
820        // https://www.postgresql.org/docs/15/sql-syntax-lexical.html#SQL-SYNTAX-OPERATORS
821        const OP_CHARS: &[char] = &[
822            '+', '-', '*', '/', '<', '>', '=', '~', '!', '@', '#', '%', '^', '&', '|', '`', '?',
823        ];
824        let name = {
825            // Unlike PostgreSQL, we only take 1 token here rather than any sequence of `OP_CHARS`.
826            // This is enough because we do not support custom operators like `x *@ y` anyways,
827            // and all builtin sequences are already single tokens.
828            //
829            // To support custom operators and be fully compatible with PostgreSQL later, the
830            // tokenizer should also be updated.
831            let checkpoint = *self;
832            let token = self.next_token();
833            let name = token.token.to_string();
834            if !name.trim_matches(OP_CHARS).is_empty() {
835                return self
836                    .expected_at(checkpoint, &format!("one of {}", OP_CHARS.iter().join(" ")));
837            }
838            name
839        };
840
841        self.expect_token(&Token::RParen)?;
842        Ok(QualifiedOperator { schema, name })
843    }
844
845    /// Parse a function call.
846    pub fn parse_function(&mut self) -> ModalResult<Expr> {
847        // [aggregate:]
848        let scalar_as_agg = if self.parse_keyword(Keyword::AGGREGATE) {
849            self.expect_token(&Token::Colon)?;
850            true
851        } else {
852            false
853        };
854        let name = self.parse_object_name()?;
855        let arg_list = self.parse_argument_list()?;
856
857        let within_group = if self.parse_keywords(&[Keyword::WITHIN, Keyword::GROUP]) {
858            self.expect_token(&Token::LParen)?;
859            self.expect_keywords(&[Keyword::ORDER, Keyword::BY])?;
860            let order_by = self.parse_order_by_expr()?;
861            self.expect_token(&Token::RParen)?;
862            Some(Box::new(order_by.clone()))
863        } else {
864            None
865        };
866
867        let filter = if self.parse_keyword(Keyword::FILTER) {
868            self.expect_token(&Token::LParen)?;
869            self.expect_keyword(Keyword::WHERE)?;
870            let filter_expr = self.parse_expr()?;
871            self.expect_token(&Token::RParen)?;
872            Some(Box::new(filter_expr))
873        } else {
874            None
875        };
876
877        let over = if self.parse_keyword(Keyword::OVER) {
878            // TODO: support window names (`OVER mywin`) in place of inline specification
879            self.expect_token(&Token::LParen)?;
880            let partition_by = if self.parse_keywords(&[Keyword::PARTITION, Keyword::BY]) {
881                // a list of possibly-qualified column names
882                self.parse_comma_separated(Parser::parse_expr)?
883            } else {
884                vec![]
885            };
886            let order_by_window = if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
887                self.parse_comma_separated(Parser::parse_order_by_expr)?
888            } else {
889                vec![]
890            };
891            let window_frame = if !self.consume_token(&Token::RParen) {
892                let window_frame = self.parse_window_frame()?;
893                self.expect_token(&Token::RParen)?;
894                Some(window_frame)
895            } else {
896                None
897            };
898
899            Some(WindowSpec {
900                partition_by,
901                order_by: order_by_window,
902                window_frame,
903            })
904        } else {
905            None
906        };
907
908        Ok(Expr::Function(Function {
909            scalar_as_agg,
910            name,
911            arg_list,
912            within_group,
913            filter,
914            over,
915        }))
916    }
917
918    pub fn parse_window_frame_units(&mut self) -> ModalResult<WindowFrameUnits> {
919        dispatch! { peek(keyword);
920            Keyword::ROWS => keyword.value(WindowFrameUnits::Rows),
921            Keyword::RANGE => keyword.value(WindowFrameUnits::Range),
922            Keyword::GROUPS => keyword.value(WindowFrameUnits::Groups),
923            Keyword::SESSION => keyword.value(WindowFrameUnits::Session),
924            _ => fail,
925        }
926        .expect("ROWS, RANGE, or GROUPS")
927        .parse_next(self)
928    }
929
930    pub fn parse_window_frame(&mut self) -> ModalResult<WindowFrame> {
931        let units = self.parse_window_frame_units()?;
932        let bounds = if self.parse_keyword(Keyword::BETWEEN) {
933            // `BETWEEN <frame_start> AND <frame_end>`
934            let start = self.parse_window_frame_bound()?;
935            self.expect_keyword(Keyword::AND)?;
936            let end = Some(self.parse_window_frame_bound()?);
937            WindowFrameBounds::Bounds { start, end }
938        } else if self.parse_keywords(&[Keyword::WITH, Keyword::GAP]) {
939            // `WITH GAP <gap>`, only for session frames
940            WindowFrameBounds::Gap(Box::new(self.parse_expr()?))
941        } else {
942            // `<frame_start>`
943            WindowFrameBounds::Bounds {
944                start: self.parse_window_frame_bound()?,
945                end: None,
946            }
947        };
948        let exclusion = if self.parse_keyword(Keyword::EXCLUDE) {
949            Some(self.parse_window_frame_exclusion()?)
950        } else {
951            None
952        };
953        Ok(WindowFrame {
954            units,
955            bounds,
956            exclusion,
957        })
958    }
959
960    /// Parse `CURRENT ROW` or `{ <non-negative numeric | datetime | interval> | UNBOUNDED } { PRECEDING | FOLLOWING }`
961    pub fn parse_window_frame_bound(&mut self) -> ModalResult<WindowFrameBound> {
962        if self.parse_keywords(&[Keyword::CURRENT, Keyword::ROW]) {
963            Ok(WindowFrameBound::CurrentRow)
964        } else {
965            let rows = if self.parse_keyword(Keyword::UNBOUNDED) {
966                None
967            } else {
968                Some(Box::new(self.parse_expr()?))
969            };
970            if self.parse_keyword(Keyword::PRECEDING) {
971                Ok(WindowFrameBound::Preceding(rows))
972            } else if self.parse_keyword(Keyword::FOLLOWING) {
973                Ok(WindowFrameBound::Following(rows))
974            } else {
975                self.expected("PRECEDING or FOLLOWING")
976            }
977        }
978    }
979
980    pub fn parse_window_frame_exclusion(&mut self) -> ModalResult<WindowFrameExclusion> {
981        if self.parse_keywords(&[Keyword::CURRENT, Keyword::ROW]) {
982            Ok(WindowFrameExclusion::CurrentRow)
983        } else if self.parse_keyword(Keyword::GROUP) {
984            Ok(WindowFrameExclusion::Group)
985        } else if self.parse_keyword(Keyword::TIES) {
986            Ok(WindowFrameExclusion::Ties)
987        } else if self.parse_keywords(&[Keyword::NO, Keyword::OTHERS]) {
988            Ok(WindowFrameExclusion::NoOthers)
989        } else {
990            self.expected("CURRENT ROW, GROUP, TIES, or NO OTHERS")
991        }
992    }
993
994    /// parse a group by expr. a group by expr can be one of group sets, roll up, cube, or simple
995    /// expr.
996    fn parse_group_by_expr(&mut self) -> ModalResult<Expr> {
997        if self.parse_keywords(&[Keyword::GROUPING, Keyword::SETS]) {
998            self.expect_token(&Token::LParen)?;
999            let result = self.parse_comma_separated(|p| p.parse_tuple(true, true))?;
1000            self.expect_token(&Token::RParen)?;
1001            Ok(Expr::GroupingSets(result))
1002        } else if self.parse_keyword(Keyword::CUBE) {
1003            self.expect_token(&Token::LParen)?;
1004            let result = self.parse_comma_separated(|p| p.parse_tuple(true, false))?;
1005            self.expect_token(&Token::RParen)?;
1006            Ok(Expr::Cube(result))
1007        } else if self.parse_keyword(Keyword::ROLLUP) {
1008            self.expect_token(&Token::LParen)?;
1009            let result = self.parse_comma_separated(|p| p.parse_tuple(true, false))?;
1010            self.expect_token(&Token::RParen)?;
1011            Ok(Expr::Rollup(result))
1012        } else {
1013            self.parse_expr()
1014        }
1015    }
1016
1017    /// parse a tuple with `(` and `)`.
1018    /// If `lift_singleton` is true, then a singleton tuple is lifted to a tuple of length 1,
1019    /// otherwise it will fail. If `allow_empty` is true, then an empty tuple is allowed.
1020    fn parse_tuple(&mut self, lift_singleton: bool, allow_empty: bool) -> ModalResult<Vec<Expr>> {
1021        if lift_singleton {
1022            if self.consume_token(&Token::LParen) {
1023                let result = if allow_empty && self.consume_token(&Token::RParen) {
1024                    vec![]
1025                } else {
1026                    let result = self.parse_comma_separated(Parser::parse_expr)?;
1027                    self.expect_token(&Token::RParen)?;
1028                    result
1029                };
1030                Ok(result)
1031            } else {
1032                Ok(vec![self.parse_expr()?])
1033            }
1034        } else {
1035            self.expect_token(&Token::LParen)?;
1036            let result = if allow_empty && self.consume_token(&Token::RParen) {
1037                vec![]
1038            } else {
1039                let result = self.parse_comma_separated(Parser::parse_expr)?;
1040                self.expect_token(&Token::RParen)?;
1041                result
1042            };
1043            Ok(result)
1044        }
1045    }
1046
1047    pub fn parse_case_expr(&mut self) -> ModalResult<Expr> {
1048        parser_v2::expr_case(self)
1049    }
1050
1051    /// Parse a SQL CAST function e.g. `CAST(expr AS FLOAT)`
1052    pub fn parse_cast_expr(&mut self) -> ModalResult<Expr> {
1053        parser_v2::expr_cast(self)
1054    }
1055
1056    /// Parse a SQL TRY_CAST function e.g. `TRY_CAST(expr AS FLOAT)`
1057    pub fn parse_try_cast_expr(&mut self) -> ModalResult<Expr> {
1058        parser_v2::expr_try_cast(self)
1059    }
1060
1061    /// Parse a SQL EXISTS expression e.g. `WHERE EXISTS(SELECT ...)`.
1062    pub fn parse_exists_expr(&mut self) -> ModalResult<Expr> {
1063        self.expect_token(&Token::LParen)?;
1064        let exists_node = Expr::Exists(Box::new(self.parse_query()?));
1065        self.expect_token(&Token::RParen)?;
1066        Ok(exists_node)
1067    }
1068
1069    pub fn parse_extract_expr(&mut self) -> ModalResult<Expr> {
1070        parser_v2::expr_extract(self)
1071    }
1072
1073    pub fn parse_substring_expr(&mut self) -> ModalResult<Expr> {
1074        parser_v2::expr_substring(self)
1075    }
1076
1077    /// `POSITION(<expr> IN <expr>)`
1078    pub fn parse_position_expr(&mut self) -> ModalResult<Expr> {
1079        parser_v2::expr_position(self)
1080    }
1081
1082    /// `OVERLAY(<expr> PLACING <expr> FROM <expr> [ FOR <expr> ])`
1083    pub fn parse_overlay_expr(&mut self) -> ModalResult<Expr> {
1084        parser_v2::expr_overlay(self)
1085    }
1086
1087    /// `TRIM ([WHERE] ['text'] FROM 'text')`\
1088    /// `TRIM ([WHERE] [FROM] 'text' [, 'text'])`
1089    pub fn parse_trim_expr(&mut self) -> ModalResult<Expr> {
1090        self.expect_token(&Token::LParen)?;
1091        let mut trim_where = None;
1092        if let Token::Word(word) = self.peek_token().token {
1093            if [Keyword::BOTH, Keyword::LEADING, Keyword::TRAILING].contains(&word.keyword) {
1094                trim_where = Some(self.parse_trim_where()?);
1095            }
1096        }
1097
1098        let (mut trim_what, expr) = if self.parse_keyword(Keyword::FROM) {
1099            (None, self.parse_expr()?)
1100        } else {
1101            let mut expr = self.parse_expr()?;
1102            if self.parse_keyword(Keyword::FROM) {
1103                let trim_what = std::mem::replace(&mut expr, self.parse_expr()?);
1104                (Some(Box::new(trim_what)), expr)
1105            } else {
1106                (None, expr)
1107            }
1108        };
1109
1110        if trim_what.is_none() && self.consume_token(&Token::Comma) {
1111            trim_what = Some(Box::new(self.parse_expr()?));
1112        }
1113        self.expect_token(&Token::RParen)?;
1114
1115        Ok(Expr::Trim {
1116            expr: Box::new(expr),
1117            trim_where,
1118            trim_what,
1119        })
1120    }
1121
1122    pub fn parse_trim_where(&mut self) -> ModalResult<TrimWhereField> {
1123        dispatch! { peek(keyword);
1124            Keyword::BOTH => keyword.value(TrimWhereField::Both),
1125            Keyword::LEADING => keyword.value(TrimWhereField::Leading),
1126            Keyword::TRAILING => keyword.value(TrimWhereField::Trailing),
1127            _ => fail
1128        }
1129        .expect("BOTH, LEADING, or TRAILING")
1130        .parse_next(self)
1131    }
1132
1133    /// Parses an array expression `[ex1, ex2, ..]`
1134    pub fn parse_array_expr(&mut self) -> ModalResult<Expr> {
1135        let mut expected_depth = None;
1136        let exprs = self.parse_array_inner(0, &mut expected_depth)?;
1137        Ok(Expr::Array(Array {
1138            elem: exprs,
1139            // Top-level array is named.
1140            named: true,
1141        }))
1142    }
1143
1144    fn parse_array_inner(
1145        &mut self,
1146        depth: usize,
1147        expected_depth: &mut Option<usize>,
1148    ) -> ModalResult<Vec<Expr>> {
1149        self.expect_token(&Token::LBracket)?;
1150        if let Some(expected_depth) = *expected_depth
1151            && depth > expected_depth
1152        {
1153            return self.expected("]");
1154        }
1155        let exprs = if self.peek_token() == Token::LBracket {
1156            self.parse_comma_separated(|parser| {
1157                let exprs = parser.parse_array_inner(depth + 1, expected_depth)?;
1158                Ok(Expr::Array(Array {
1159                    elem: exprs,
1160                    named: false,
1161                }))
1162            })?
1163        } else {
1164            if let Some(expected_depth) = *expected_depth {
1165                if depth < expected_depth {
1166                    return self.expected("[");
1167                }
1168            } else {
1169                *expected_depth = Some(depth);
1170            }
1171            if self.consume_token(&Token::RBracket) {
1172                return Ok(vec![]);
1173            }
1174            self.parse_comma_separated(Self::parse_expr)?
1175        };
1176        self.expect_token(&Token::RBracket)?;
1177        Ok(exprs)
1178    }
1179
1180    /// Parses a map expression `MAP {k1:v1, k2:v2, ..}`
1181    pub fn parse_map_expr(&mut self) -> ModalResult<Expr> {
1182        self.expect_token(&Token::LBrace)?;
1183        if self.consume_token(&Token::RBrace) {
1184            return Ok(Expr::Map { entries: vec![] });
1185        }
1186        let entries = self.parse_comma_separated(|parser| {
1187            let key = parser.parse_expr()?;
1188            parser.expect_token(&Token::Colon)?;
1189            let value = parser.parse_expr()?;
1190            Ok((key, value))
1191        })?;
1192        self.expect_token(&Token::RBrace)?;
1193        Ok(Expr::Map { entries })
1194    }
1195
1196    // This function parses date/time fields for interval qualifiers.
1197    pub fn parse_date_time_field(&mut self) -> ModalResult<DateTimeField> {
1198        dispatch! { peek(keyword);
1199            Keyword::YEAR => keyword.value(DateTimeField::Year),
1200            Keyword::MONTH => keyword.value(DateTimeField::Month),
1201            Keyword::DAY => keyword.value(DateTimeField::Day),
1202            Keyword::HOUR => keyword.value(DateTimeField::Hour),
1203            Keyword::MINUTE => keyword.value(DateTimeField::Minute),
1204            Keyword::SECOND => keyword.value(DateTimeField::Second),
1205            _ => fail,
1206        }
1207        .expect("date/time field")
1208        .parse_next(self)
1209    }
1210
1211    // This function parses date/time fields for the EXTRACT function-like operator. PostgreSQL
1212    // allows arbitrary inputs including invalid ones.
1213    //
1214    // ```
1215    //   select extract(day from null::date);
1216    //   select extract(invalid from null::date);
1217    //   select extract("invaLId" from null::date);
1218    //   select extract('invaLId' from null::date);
1219    // ```
1220    pub fn parse_date_time_field_in_extract(&mut self) -> ModalResult<String> {
1221        let checkpoint = *self;
1222        let token = self.next_token();
1223        match token.token {
1224            Token::Word(w) => Ok(w.value.to_uppercase()),
1225            Token::SingleQuotedString(s) => Ok(s.to_uppercase()),
1226            _ => {
1227                *self = checkpoint;
1228                self.expected("date/time field")
1229            }
1230        }
1231    }
1232
1233    /// Parse an INTERVAL literal.
1234    ///
1235    /// Some syntactically valid intervals:
1236    ///
1237    ///   1. `INTERVAL '1' DAY`
1238    ///   2. `INTERVAL '1-1' YEAR TO MONTH`
1239    ///   3. `INTERVAL '1' SECOND`
1240    ///   4. `INTERVAL '1:1:1.1' HOUR (5) TO SECOND (5)`
1241    ///   5. `INTERVAL '1.1' SECOND (2, 2)`
1242    ///   6. `INTERVAL '1:1' HOUR (5) TO MINUTE (5)`
1243    ///
1244    /// Note that we do not currently attempt to parse the quoted value.
1245    pub fn parse_literal_interval(&mut self) -> ModalResult<Expr> {
1246        // The SQL standard allows an optional sign before the value string, but
1247        // it is not clear if any implementations support that syntax, so we
1248        // don't currently try to parse it. (The sign can instead be included
1249        // inside the value string.)
1250
1251        // The first token in an interval is a string literal which specifies
1252        // the duration of the interval.
1253        let value = self.parse_literal_string()?;
1254
1255        // Following the string literal is a qualifier which indicates the units
1256        // of the duration specified in the string literal.
1257        //
1258        // Note that PostgreSQL allows omitting the qualifier, so we provide
1259        // this more general implementation.
1260        let leading_field = match self.peek_token().token {
1261            Token::Word(kw)
1262                if [
1263                    Keyword::YEAR,
1264                    Keyword::MONTH,
1265                    Keyword::DAY,
1266                    Keyword::HOUR,
1267                    Keyword::MINUTE,
1268                    Keyword::SECOND,
1269                ]
1270                .contains(&kw.keyword) =>
1271            {
1272                Some(self.parse_date_time_field()?)
1273            }
1274            _ => None,
1275        };
1276
1277        let (leading_precision, last_field, fsec_precision) =
1278            if leading_field == Some(DateTimeField::Second) {
1279                // SQL mandates special syntax for `SECOND TO SECOND` literals.
1280                // Instead of
1281                //     `SECOND [(<leading precision>)] TO SECOND[(<fractional seconds precision>)]`
1282                // one must use the special format:
1283                //     `SECOND [( <leading precision> [ , <fractional seconds precision>] )]`
1284                let last_field = None;
1285                let (leading_precision, fsec_precision) = self.parse_optional_precision_scale()?;
1286                (leading_precision, last_field, fsec_precision)
1287            } else {
1288                let leading_precision = self.parse_optional_precision()?;
1289                if self.parse_keyword(Keyword::TO) {
1290                    let last_field = Some(self.parse_date_time_field()?);
1291                    let fsec_precision = if last_field == Some(DateTimeField::Second) {
1292                        self.parse_optional_precision()?
1293                    } else {
1294                        None
1295                    };
1296                    (leading_precision, last_field, fsec_precision)
1297                } else {
1298                    (leading_precision, None, None)
1299                }
1300            };
1301
1302        Ok(Expr::Value(Value::Interval {
1303            value,
1304            leading_field,
1305            leading_precision,
1306            last_field,
1307            fractional_seconds_precision: fsec_precision,
1308        }))
1309    }
1310
1311    /// Parse an operator following an expression
1312    pub fn parse_infix(&mut self, expr: Expr, precedence: Precedence) -> ModalResult<Expr> {
1313        let checkpoint = *self;
1314        let tok = self.next_token();
1315        debug!("parsing infix {:?}", tok.token);
1316        let regular_binary_operator = match &tok.token {
1317            Token::Spaceship => Some(BinaryOperator::Spaceship),
1318            Token::DoubleEq => Some(BinaryOperator::Eq),
1319            Token::Eq => Some(BinaryOperator::Eq),
1320            Token::Neq => Some(BinaryOperator::NotEq),
1321            Token::Gt => Some(BinaryOperator::Gt),
1322            Token::GtEq => Some(BinaryOperator::GtEq),
1323            Token::Lt => Some(BinaryOperator::Lt),
1324            Token::LtEq => Some(BinaryOperator::LtEq),
1325            Token::Plus => Some(BinaryOperator::Plus),
1326            Token::Minus => Some(BinaryOperator::Minus),
1327            Token::Mul => Some(BinaryOperator::Multiply),
1328            Token::Mod => Some(BinaryOperator::Modulo),
1329            Token::Concat => Some(BinaryOperator::Concat),
1330            Token::Pipe => Some(BinaryOperator::BitwiseOr),
1331            Token::Caret => Some(BinaryOperator::BitwiseXor),
1332            Token::Prefix => Some(BinaryOperator::Prefix),
1333            Token::Ampersand => Some(BinaryOperator::BitwiseAnd),
1334            Token::Div => Some(BinaryOperator::Divide),
1335            Token::ShiftLeft => Some(BinaryOperator::PGBitwiseShiftLeft),
1336            Token::ShiftRight => Some(BinaryOperator::PGBitwiseShiftRight),
1337            Token::Sharp => Some(BinaryOperator::PGBitwiseXor),
1338            Token::Tilde => Some(BinaryOperator::PGRegexMatch),
1339            Token::TildeAsterisk => Some(BinaryOperator::PGRegexIMatch),
1340            Token::ExclamationMarkTilde => Some(BinaryOperator::PGRegexNotMatch),
1341            Token::ExclamationMarkTildeAsterisk => Some(BinaryOperator::PGRegexNotIMatch),
1342            Token::DoubleTilde => Some(BinaryOperator::PGLikeMatch),
1343            Token::DoubleTildeAsterisk => Some(BinaryOperator::PGILikeMatch),
1344            Token::ExclamationMarkDoubleTilde => Some(BinaryOperator::PGNotLikeMatch),
1345            Token::ExclamationMarkDoubleTildeAsterisk => Some(BinaryOperator::PGNotILikeMatch),
1346            Token::Arrow => Some(BinaryOperator::Arrow),
1347            Token::LongArrow => Some(BinaryOperator::LongArrow),
1348            Token::HashArrow => Some(BinaryOperator::HashArrow),
1349            Token::HashLongArrow => Some(BinaryOperator::HashLongArrow),
1350            Token::HashMinus => Some(BinaryOperator::HashMinus),
1351            Token::AtArrow => Some(BinaryOperator::Contains),
1352            Token::ArrowAt => Some(BinaryOperator::Contained),
1353            Token::QuestionMark => Some(BinaryOperator::Exists),
1354            Token::QuestionMarkPipe => Some(BinaryOperator::ExistsAny),
1355            Token::QuestionMarkAmpersand => Some(BinaryOperator::ExistsAll),
1356            Token::AtQuestionMark => Some(BinaryOperator::PathExists),
1357            Token::AtAt => Some(BinaryOperator::PathMatch),
1358            Token::Word(w) => match w.keyword {
1359                Keyword::AND => Some(BinaryOperator::And),
1360                Keyword::OR => Some(BinaryOperator::Or),
1361                Keyword::XOR => Some(BinaryOperator::Xor),
1362                Keyword::OPERATOR if self.peek_token() == Token::LParen => Some(
1363                    BinaryOperator::PGQualified(Box::new(self.parse_qualified_operator()?)),
1364                ),
1365                _ => None,
1366            },
1367            _ => None,
1368        };
1369
1370        if let Some(op) = regular_binary_operator {
1371            // // `all/any/some` only appears to the right of the binary op.
1372            // if let Some(keyword) =
1373            //     self.parse_one_of_keywords(&[Keyword::ANY, Keyword::ALL, Keyword::SOME])
1374            // {
1375            //     self.expect_token(&Token::LParen)?;
1376            //     // In upstream's PR of parser-rs, there is `self.parser_subexpr(precedence)` here.
1377            //     // But it will fail to parse `select 1 = any(null and true);`.
1378            //     let right = self.parse_expr()?;
1379            //     self.expect_token(&Token::RParen)?;
1380
1381            //     // TODO: support `all/any/some(subquery)`.
1382            //     if let Expr::Subquery(_) = &right {
1383            //         parser_err!("ANY/SOME/ALL(Subquery) is not implemented");
1384            //     }
1385
1386            //     let right = match keyword {
1387            //         Keyword::ALL => Box::new(Expr::AllOp(Box::new(right))),
1388            //         // `SOME` is a synonym for `ANY`.
1389            //         Keyword::ANY | Keyword::SOME => Box::new(Expr::SomeOp(Box::new(right))),
1390            //         _ => unreachable!(),
1391            //     };
1392
1393            //     Ok(Expr::BinaryOp {
1394            //         left: Box::new(expr),
1395            //         op,
1396            //         right,
1397            //     })
1398            // } else {
1399            Ok(Expr::BinaryOp {
1400                left: Box::new(expr),
1401                op,
1402                right: Box::new(self.parse_subexpr(precedence)?),
1403            })
1404            // }
1405        } else if let Token::Word(w) = &tok.token {
1406            match w.keyword {
1407                Keyword::IS => {
1408                    if self.parse_keyword(Keyword::TRUE) {
1409                        Ok(Expr::IsTrue(Box::new(expr)))
1410                    } else if self.parse_keywords(&[Keyword::NOT, Keyword::TRUE]) {
1411                        Ok(Expr::IsNotTrue(Box::new(expr)))
1412                    } else if self.parse_keyword(Keyword::FALSE) {
1413                        Ok(Expr::IsFalse(Box::new(expr)))
1414                    } else if self.parse_keywords(&[Keyword::NOT, Keyword::FALSE]) {
1415                        Ok(Expr::IsNotFalse(Box::new(expr)))
1416                    } else if self.parse_keyword(Keyword::UNKNOWN) {
1417                        Ok(Expr::IsUnknown(Box::new(expr)))
1418                    } else if self.parse_keywords(&[Keyword::NOT, Keyword::UNKNOWN]) {
1419                        Ok(Expr::IsNotUnknown(Box::new(expr)))
1420                    } else if self.parse_keyword(Keyword::NULL) {
1421                        Ok(Expr::IsNull(Box::new(expr)))
1422                    } else if self.parse_keywords(&[Keyword::NOT, Keyword::NULL]) {
1423                        Ok(Expr::IsNotNull(Box::new(expr)))
1424                    } else if self.parse_keywords(&[Keyword::DISTINCT, Keyword::FROM]) {
1425                        let expr2 = self.parse_expr()?;
1426                        Ok(Expr::IsDistinctFrom(Box::new(expr), Box::new(expr2)))
1427                    } else if self.parse_keywords(&[Keyword::NOT, Keyword::DISTINCT, Keyword::FROM])
1428                    {
1429                        let expr2 = self.parse_expr()?;
1430                        Ok(Expr::IsNotDistinctFrom(Box::new(expr), Box::new(expr2)))
1431                    } else {
1432                        let negated = self.parse_keyword(Keyword::NOT);
1433
1434                        if self.parse_keyword(Keyword::JSON) {
1435                            self.parse_is_json(expr, negated)
1436                        } else {
1437                            self.expected(
1438                                "[NOT] { TRUE | FALSE | UNKNOWN | NULL | DISTINCT FROM | JSON } after IS",
1439                            )
1440                        }
1441                    }
1442                }
1443                Keyword::AT => {
1444                    assert_eq!(precedence, Precedence::At);
1445                    let time_zone = Box::new(
1446                        preceded(
1447                            (Keyword::TIME, Keyword::ZONE),
1448                            cut_err(|p: &mut Self| p.parse_subexpr(precedence)),
1449                        )
1450                        .parse_next(self)?,
1451                    );
1452                    Ok(Expr::AtTimeZone {
1453                        timestamp: Box::new(expr),
1454                        time_zone,
1455                    })
1456                }
1457                keyword @ (Keyword::ALL | Keyword::ANY | Keyword::SOME) => {
1458                    self.expect_token(&Token::LParen)?;
1459                    // In upstream's PR of parser-rs, there is `self.parser_subexpr(precedence)` here.
1460                    // But it will fail to parse `select 1 = any(null and true);`.
1461                    let sub = self.parse_expr()?;
1462                    self.expect_token(&Token::RParen)?;
1463
1464                    // TODO: support `all/any/some(subquery)`.
1465                    if let Expr::Subquery(_) = &sub {
1466                        parser_err!("ANY/SOME/ALL(Subquery) is not implemented");
1467                    }
1468
1469                    Ok(match keyword {
1470                        Keyword::ALL => Expr::AllOp(Box::new(sub)),
1471                        // `SOME` is a synonym for `ANY`.
1472                        Keyword::ANY | Keyword::SOME => Expr::SomeOp(Box::new(sub)),
1473                        _ => unreachable!(),
1474                    })
1475                }
1476                Keyword::NOT
1477                | Keyword::IN
1478                | Keyword::BETWEEN
1479                | Keyword::LIKE
1480                | Keyword::ILIKE
1481                | Keyword::SIMILAR => {
1482                    *self = checkpoint;
1483                    let negated = self.parse_keyword(Keyword::NOT);
1484                    if self.parse_keyword(Keyword::IN) {
1485                        self.parse_in(expr, negated)
1486                    } else if self.parse_keyword(Keyword::BETWEEN) {
1487                        self.parse_between(expr, negated)
1488                    } else if self.parse_keyword(Keyword::LIKE) {
1489                        Ok(Expr::Like {
1490                            negated,
1491                            expr: Box::new(expr),
1492                            pattern: Box::new(self.parse_subexpr(Precedence::Like)?),
1493                            escape_char: self.parse_escape()?,
1494                        })
1495                    } else if self.parse_keyword(Keyword::ILIKE) {
1496                        Ok(Expr::ILike {
1497                            negated,
1498                            expr: Box::new(expr),
1499                            pattern: Box::new(self.parse_subexpr(Precedence::Like)?),
1500                            escape_char: self.parse_escape()?,
1501                        })
1502                    } else if self.parse_keywords(&[Keyword::SIMILAR, Keyword::TO]) {
1503                        Ok(Expr::SimilarTo {
1504                            negated,
1505                            expr: Box::new(expr),
1506                            pattern: Box::new(self.parse_subexpr(Precedence::Like)?),
1507                            escape_char: self.parse_escape()?,
1508                        })
1509                    } else {
1510                        self.expected("IN, BETWEEN or SIMILAR TO after NOT")
1511                    }
1512                }
1513                // Can only happen if `get_next_precedence` got out of sync with this function
1514                _ => parser_err!("No infix parser for token {:?}", tok),
1515            }
1516        } else if Token::DoubleColon == tok {
1517            self.parse_pg_cast(expr)
1518        } else if Token::ExclamationMark == tok {
1519            // PostgreSQL factorial operation
1520            Ok(Expr::UnaryOp {
1521                op: UnaryOperator::PGPostfixFactorial,
1522                expr: Box::new(expr),
1523            })
1524        } else if Token::LBracket == tok {
1525            self.parse_array_index(expr)
1526        } else {
1527            // Can only happen if `get_next_precedence` got out of sync with this function
1528            parser_err!("No infix parser for token {:?}", tok)
1529        }
1530    }
1531
1532    /// parse the ESCAPE CHAR portion of LIKE, ILIKE, and SIMILAR TO
1533    pub fn parse_escape(&mut self) -> ModalResult<Option<EscapeChar>> {
1534        if self.parse_keyword(Keyword::ESCAPE) {
1535            let s = self.parse_literal_string()?;
1536            let mut chs = s.chars();
1537            if let Some(ch) = chs.next() {
1538                if chs.next().is_some() {
1539                    parser_err!("Escape string must be empty or one character, found {s:?}")
1540                } else {
1541                    Ok(Some(EscapeChar::escape(ch)))
1542                }
1543            } else {
1544                Ok(Some(EscapeChar::empty()))
1545            }
1546        } else {
1547            Ok(None)
1548        }
1549    }
1550
1551    /// We parse both `array[1,9][1]`, `array[1,9][1:2]`, `array[1,9][:2]`, `array[1,9][1:]` and
1552    /// `array[1,9][:]` in this function.
1553    pub fn parse_array_index(&mut self, expr: Expr) -> ModalResult<Expr> {
1554        let new_expr = match self.peek_token().token {
1555            Token::Colon => {
1556                // [:] or [:N]
1557                assert!(self.consume_token(&Token::Colon));
1558                let end = match self.peek_token().token {
1559                    Token::RBracket => None,
1560                    _ => {
1561                        let end_index = Box::new(self.parse_expr()?);
1562                        Some(end_index)
1563                    }
1564                };
1565                Expr::ArrayRangeIndex {
1566                    obj: Box::new(expr),
1567                    start: None,
1568                    end,
1569                }
1570            }
1571            _ => {
1572                // [N], [N:], [N:M]
1573                let index = Box::new(self.parse_expr()?);
1574                match self.peek_token().token {
1575                    Token::Colon => {
1576                        // [N:], [N:M]
1577                        assert!(self.consume_token(&Token::Colon));
1578                        match self.peek_token().token {
1579                            Token::RBracket => {
1580                                // [N:]
1581                                Expr::ArrayRangeIndex {
1582                                    obj: Box::new(expr),
1583                                    start: Some(index),
1584                                    end: None,
1585                                }
1586                            }
1587                            _ => {
1588                                // [N:M]
1589                                let end = Some(Box::new(self.parse_expr()?));
1590                                Expr::ArrayRangeIndex {
1591                                    obj: Box::new(expr),
1592                                    start: Some(index),
1593                                    end,
1594                                }
1595                            }
1596                        }
1597                    }
1598                    _ => {
1599                        // [N]
1600                        Expr::Index {
1601                            obj: Box::new(expr),
1602                            index,
1603                        }
1604                    }
1605                }
1606            }
1607        };
1608        self.expect_token(&Token::RBracket)?;
1609        // recursively checking for more indices
1610        if self.consume_token(&Token::LBracket) {
1611            self.parse_array_index(new_expr)
1612        } else {
1613            Ok(new_expr)
1614        }
1615    }
1616
1617    /// Parses the optional constraints following the `IS [NOT] JSON` predicate
1618    pub fn parse_is_json(&mut self, expr: Expr, negated: bool) -> ModalResult<Expr> {
1619        let item_type = match self.peek_token().token {
1620            Token::Word(w) => match w.keyword {
1621                Keyword::VALUE => Some(JsonPredicateType::Value),
1622                Keyword::ARRAY => Some(JsonPredicateType::Array),
1623                Keyword::OBJECT => Some(JsonPredicateType::Object),
1624                Keyword::SCALAR => Some(JsonPredicateType::Scalar),
1625                _ => None,
1626            },
1627            _ => None,
1628        };
1629        if item_type.is_some() {
1630            self.next_token();
1631        }
1632        let item_type = item_type.unwrap_or_default();
1633
1634        let unique_keys = self.parse_one_of_keywords(&[Keyword::WITH, Keyword::WITHOUT]);
1635        if unique_keys.is_some() {
1636            self.expect_keyword(Keyword::UNIQUE)?;
1637            _ = self.parse_keyword(Keyword::KEYS);
1638        }
1639        let unique_keys = unique_keys.is_some_and(|w| w == Keyword::WITH);
1640
1641        Ok(Expr::IsJson {
1642            expr: Box::new(expr),
1643            negated,
1644            item_type,
1645            unique_keys,
1646        })
1647    }
1648
1649    /// Parses the parens following the `[ NOT ] IN` operator
1650    pub fn parse_in(&mut self, expr: Expr, negated: bool) -> ModalResult<Expr> {
1651        self.expect_token(&Token::LParen)?;
1652        let in_op = if matches!(self.peek_token().token, Token::Word(w) if w.keyword == Keyword::SELECT || w.keyword == Keyword::WITH)
1653        {
1654            Expr::InSubquery {
1655                expr: Box::new(expr),
1656                subquery: Box::new(self.parse_query()?),
1657                negated,
1658            }
1659        } else {
1660            Expr::InList {
1661                expr: Box::new(expr),
1662                list: self.parse_comma_separated(Parser::parse_expr)?,
1663                negated,
1664            }
1665        };
1666        self.expect_token(&Token::RParen)?;
1667        Ok(in_op)
1668    }
1669
1670    /// Parses `BETWEEN <low> AND <high>`, assuming the `BETWEEN` keyword was already consumed
1671    pub fn parse_between(&mut self, expr: Expr, negated: bool) -> ModalResult<Expr> {
1672        // Stop parsing subexpressions for <low> and <high> on tokens with
1673        // precedence lower than that of `BETWEEN`, such as `AND`, `IS`, etc.
1674        let low = self.parse_subexpr(Precedence::Between)?;
1675        self.expect_keyword(Keyword::AND)?;
1676        let high = self.parse_subexpr(Precedence::Between)?;
1677        Ok(Expr::Between {
1678            expr: Box::new(expr),
1679            negated,
1680            low: Box::new(low),
1681            high: Box::new(high),
1682        })
1683    }
1684
1685    /// Parse a postgresql casting style which is in the form of `expr::datatype`
1686    pub fn parse_pg_cast(&mut self, expr: Expr) -> ModalResult<Expr> {
1687        Ok(Expr::Cast {
1688            expr: Box::new(expr),
1689            data_type: self.parse_data_type()?,
1690        })
1691    }
1692
1693    /// Get the precedence of the next token
1694    pub fn get_next_precedence(&self) -> ModalResult<Precedence> {
1695        use Precedence as P;
1696
1697        let token = self.peek_token();
1698        debug!("get_next_precedence() {:?}", token);
1699        match token.token {
1700            Token::Word(w) if w.keyword == Keyword::OR => Ok(P::LogicalOr),
1701            Token::Word(w) if w.keyword == Keyword::XOR => Ok(P::LogicalXor),
1702            Token::Word(w) if w.keyword == Keyword::AND => Ok(P::LogicalAnd),
1703            Token::Word(w) if w.keyword == Keyword::AT => {
1704                match (self.peek_nth_token(1).token, self.peek_nth_token(2).token) {
1705                    (Token::Word(w), Token::Word(w2))
1706                        if w.keyword == Keyword::TIME && w2.keyword == Keyword::ZONE =>
1707                    {
1708                        Ok(P::At)
1709                    }
1710                    _ => Ok(P::Zero),
1711                }
1712            }
1713
1714            Token::Word(w) if w.keyword == Keyword::NOT => match self.peek_nth_token(1).token {
1715                // The precedence of NOT varies depending on keyword that
1716                // follows it. If it is followed by IN, BETWEEN, or LIKE,
1717                // it takes on the precedence of those tokens. Otherwise it
1718                // is not an infix operator, and therefore has zero
1719                // precedence.
1720                Token::Word(w) if w.keyword == Keyword::BETWEEN => Ok(P::Between),
1721                Token::Word(w) if w.keyword == Keyword::IN => Ok(P::Between),
1722                Token::Word(w) if w.keyword == Keyword::LIKE => Ok(P::Like),
1723                Token::Word(w) if w.keyword == Keyword::ILIKE => Ok(P::Like),
1724                Token::Word(w) if w.keyword == Keyword::SIMILAR => Ok(P::Like),
1725                _ => Ok(P::Zero),
1726            },
1727
1728            Token::Word(w) if w.keyword == Keyword::IS => Ok(P::Is),
1729            Token::Word(w) if w.keyword == Keyword::ISNULL => Ok(P::Is),
1730            Token::Word(w) if w.keyword == Keyword::NOTNULL => Ok(P::Is),
1731            Token::Eq
1732            | Token::Lt
1733            | Token::LtEq
1734            | Token::Neq
1735            | Token::Gt
1736            | Token::GtEq
1737            | Token::DoubleEq
1738            | Token::Spaceship => Ok(P::Cmp),
1739            Token::Word(w) if w.keyword == Keyword::IN => Ok(P::Between),
1740            Token::Word(w) if w.keyword == Keyword::BETWEEN => Ok(P::Between),
1741            Token::Word(w) if w.keyword == Keyword::LIKE => Ok(P::Like),
1742            Token::Word(w) if w.keyword == Keyword::ILIKE => Ok(P::Like),
1743            Token::Word(w) if w.keyword == Keyword::SIMILAR => Ok(P::Like),
1744            Token::Word(w) if w.keyword == Keyword::ALL => Ok(P::Other),
1745            Token::Word(w) if w.keyword == Keyword::ANY => Ok(P::Other),
1746            Token::Word(w) if w.keyword == Keyword::SOME => Ok(P::Other),
1747            Token::Tilde
1748            | Token::TildeAsterisk
1749            | Token::ExclamationMarkTilde
1750            | Token::ExclamationMarkTildeAsterisk
1751            | Token::DoubleTilde
1752            | Token::DoubleTildeAsterisk
1753            | Token::ExclamationMarkDoubleTilde
1754            | Token::ExclamationMarkDoubleTildeAsterisk
1755            | Token::Concat
1756            | Token::Prefix
1757            | Token::Arrow
1758            | Token::LongArrow
1759            | Token::HashArrow
1760            | Token::HashLongArrow
1761            | Token::HashMinus
1762            | Token::AtArrow
1763            | Token::ArrowAt
1764            | Token::QuestionMark
1765            | Token::QuestionMarkPipe
1766            | Token::QuestionMarkAmpersand
1767            | Token::AtQuestionMark
1768            | Token::AtAt => Ok(P::Other),
1769            Token::Word(w)
1770                if w.keyword == Keyword::OPERATOR && self.peek_nth_token(1) == Token::LParen =>
1771            {
1772                Ok(P::Other)
1773            }
1774            // In some languages (incl. rust, c), bitwise operators have precedence:
1775            //   or < xor < and < shift
1776            // But in PostgreSQL, they are just left to right. So `2 | 3 & 4` is 0.
1777            Token::Pipe => Ok(P::Other),
1778            Token::Sharp => Ok(P::Other),
1779            Token::Ampersand => Ok(P::Other),
1780            Token::ShiftRight | Token::ShiftLeft => Ok(P::Other),
1781            Token::Plus | Token::Minus => Ok(P::PlusMinus),
1782            Token::Mul | Token::Div | Token::Mod => Ok(P::MulDiv),
1783            Token::Caret => Ok(P::Exp),
1784            Token::ExclamationMark => Ok(P::PostfixFactorial),
1785            Token::LBracket => Ok(P::Array),
1786            Token::DoubleColon => Ok(P::DoubleColon),
1787            _ => Ok(P::Zero),
1788        }
1789    }
1790
1791    /// Return the first non-whitespace token that has not yet been processed
1792    /// (or None if reached end-of-file)
1793    pub fn peek_token(&self) -> TokenWithLocation {
1794        self.peek_nth_token(0)
1795    }
1796
1797    /// Return nth non-whitespace token that has not yet been processed
1798    pub fn peek_nth_token(&self, mut n: usize) -> TokenWithLocation {
1799        let mut index = 0;
1800        loop {
1801            let token = self.0.get(index);
1802            index += 1;
1803            match token.map(|x| &x.token) {
1804                Some(Token::Whitespace(_)) => continue,
1805                _ => {
1806                    if n == 0 {
1807                        return token.cloned().unwrap_or(TokenWithLocation::eof());
1808                    }
1809                    n -= 1;
1810                }
1811            }
1812        }
1813    }
1814
1815    /// Return the first non-whitespace token that has not yet been processed
1816    /// (or None if reached end-of-file) and mark it as processed. OK to call
1817    /// repeatedly after reaching EOF.
1818    pub fn next_token(&mut self) -> TokenWithLocation {
1819        loop {
1820            let Some(token) = self.0.first() else {
1821                return TokenWithLocation::eof();
1822            };
1823            self.0 = &self.0[1..];
1824            match token.token {
1825                Token::Whitespace(_) => continue,
1826                _ => return token.clone(),
1827            }
1828        }
1829    }
1830
1831    /// Return the first unprocessed token, possibly whitespace.
1832    pub fn next_token_no_skip(&mut self) -> Option<&TokenWithLocation> {
1833        if self.0.is_empty() {
1834            None
1835        } else {
1836            let (first, rest) = self.0.split_at(1);
1837            self.0 = rest;
1838            Some(&first[0])
1839        }
1840    }
1841
1842    /// Report an expected error at the current position.
1843    pub fn expected<T>(&self, expected: &str) -> ModalResult<T> {
1844        parser_err!("expected {}, found: {}", expected, self.peek_token().token)
1845    }
1846
1847    /// Revert the parser to a previous position and report an expected error.
1848    pub fn expected_at<T>(&mut self, checkpoint: Self, expected: &str) -> ModalResult<T> {
1849        *self = checkpoint;
1850        self.expected(expected)
1851    }
1852
1853    /// Check if the expected match is the next token.
1854    /// The equality check is case-insensitive.
1855    pub fn parse_word(&mut self, expected: &str) -> bool {
1856        match self.peek_token().token {
1857            Token::Word(w) if w.value.to_uppercase() == expected => {
1858                self.next_token();
1859                true
1860            }
1861            _ => false,
1862        }
1863    }
1864
1865    /// Look for an expected keyword and consume it if it exists
1866    #[must_use]
1867    pub fn parse_keyword(&mut self, expected: Keyword) -> bool {
1868        match self.peek_token().token {
1869            Token::Word(w) if expected == w.keyword => {
1870                self.next_token();
1871                true
1872            }
1873            _ => false,
1874        }
1875    }
1876
1877    /// Look for an expected sequence of keywords and consume them if they exist
1878    #[must_use]
1879    pub fn parse_keywords(&mut self, keywords: &[Keyword]) -> bool {
1880        let checkpoint = *self;
1881        for &keyword in keywords {
1882            if !self.parse_keyword(keyword) {
1883                // println!("parse_keywords aborting .. did not find {:?}", keyword);
1884                // reset index and return immediately
1885                *self = checkpoint;
1886                return false;
1887            }
1888        }
1889        true
1890    }
1891
1892    /// Look for one of the given keywords and return the one that matches.
1893    #[must_use]
1894    pub fn parse_one_of_keywords(&mut self, keywords: &[Keyword]) -> Option<Keyword> {
1895        match self.peek_token().token {
1896            Token::Word(w) => {
1897                keywords
1898                    .iter()
1899                    .find(|keyword| **keyword == w.keyword)
1900                    .map(|keyword| {
1901                        self.next_token();
1902                        *keyword
1903                    })
1904            }
1905            _ => None,
1906        }
1907    }
1908
1909    pub fn peek_nth_any_of_keywords(&mut self, n: usize, keywords: &[Keyword]) -> bool {
1910        match self.peek_nth_token(n).token {
1911            Token::Word(w) => keywords.contains(&w.keyword),
1912            _ => false,
1913        }
1914    }
1915
1916    /// Bail out if the current token is not one of the expected keywords, or consume it if it is
1917    pub fn expect_one_of_keywords(&mut self, keywords: &[Keyword]) -> ModalResult<Keyword> {
1918        if let Some(keyword) = self.parse_one_of_keywords(keywords) {
1919            Ok(keyword)
1920        } else {
1921            let keywords: Vec<String> = keywords.iter().map(|x| format!("{:?}", x)).collect();
1922            self.expected(&format!("one of {}", keywords.join(" or ")))
1923        }
1924    }
1925
1926    /// Bail out if the current token is not an expected keyword, or consume it if it is
1927    pub fn expect_keyword(&mut self, expected: Keyword) -> ModalResult<()> {
1928        if self.parse_keyword(expected) {
1929            Ok(())
1930        } else {
1931            self.expected(format!("{:?}", &expected).as_str())
1932        }
1933    }
1934
1935    /// Bail out if the following tokens are not the expected sequence of
1936    /// keywords, or consume them if they are.
1937    pub fn expect_keywords(&mut self, expected: &[Keyword]) -> ModalResult<()> {
1938        for &kw in expected {
1939            self.expect_keyword(kw)?;
1940        }
1941        Ok(())
1942    }
1943
1944    /// Consume the next token if it matches the expected token, otherwise return false
1945    #[must_use]
1946    pub fn consume_token(&mut self, expected: &Token) -> bool {
1947        if self.peek_token() == *expected {
1948            self.next_token();
1949            true
1950        } else {
1951            false
1952        }
1953    }
1954
1955    /// Bail out if the current token is not an expected keyword, or consume it if it is
1956    pub fn expect_token(&mut self, expected: &Token) -> ModalResult<()> {
1957        if self.consume_token(expected) {
1958            Ok(())
1959        } else {
1960            self.expected(&expected.to_string())
1961        }
1962    }
1963
1964    /// Parse a comma-separated list of 1+ items accepted by `F`
1965    pub fn parse_comma_separated<T, F>(&mut self, mut f: F) -> ModalResult<Vec<T>>
1966    where
1967        F: FnMut(&mut Self) -> ModalResult<T>,
1968    {
1969        let mut values = vec![];
1970        loop {
1971            values.push(f(self)?);
1972            if !self.consume_token(&Token::Comma) {
1973                break;
1974            }
1975        }
1976        Ok(values)
1977    }
1978
1979    /// Run a parser method `f`, reverting back to the current position
1980    /// if unsuccessful.
1981    #[must_use]
1982    fn maybe_parse<T, F>(&mut self, mut f: F) -> Option<T>
1983    where
1984        F: FnMut(&mut Self) -> ModalResult<T>,
1985    {
1986        let checkpoint = *self;
1987        match f(self) {
1988            Ok(t) => Some(t),
1989            _ => {
1990                *self = checkpoint;
1991                None
1992            }
1993        }
1994    }
1995
1996    /// Parse either `ALL` or `DISTINCT`. Returns `true` if `DISTINCT` is parsed and results in a
1997    /// `ParserError` if both `ALL` and `DISTINCT` are fround.
1998    pub fn parse_all_or_distinct(&mut self) -> ModalResult<bool> {
1999        let all = self.parse_keyword(Keyword::ALL);
2000        let distinct = self.parse_keyword(Keyword::DISTINCT);
2001        if all && distinct {
2002            parser_err!("Cannot specify both ALL and DISTINCT")
2003        } else {
2004            Ok(distinct)
2005        }
2006    }
2007
2008    /// Parse either `ALL` or `DISTINCT` or `DISTINCT ON (<expr>)`.
2009    pub fn parse_all_or_distinct_on(&mut self) -> ModalResult<Distinct> {
2010        if self.parse_keywords(&[Keyword::DISTINCT, Keyword::ON]) {
2011            self.expect_token(&Token::LParen)?;
2012            let exprs = self.parse_comma_separated(Parser::parse_expr)?;
2013            self.expect_token(&Token::RParen)?;
2014            return Ok(Distinct::DistinctOn(exprs));
2015        } else if self.parse_keyword(Keyword::DISTINCT) {
2016            return Ok(Distinct::Distinct);
2017        };
2018        _ = self.parse_keyword(Keyword::ALL);
2019        Ok(Distinct::All)
2020    }
2021
2022    /// Parse a SQL CREATE statement
2023    pub fn parse_create(&mut self) -> ModalResult<Statement> {
2024        let or_replace = self.parse_keywords(&[Keyword::OR, Keyword::REPLACE]);
2025        let temporary = self
2026            .parse_one_of_keywords(&[Keyword::TEMP, Keyword::TEMPORARY])
2027            .is_some();
2028        if self.parse_keyword(Keyword::TABLE) {
2029            self.parse_create_table(or_replace, temporary)
2030        } else if self.parse_keyword(Keyword::VIEW) {
2031            self.parse_create_view(false, or_replace)
2032        } else if self.parse_keywords(&[Keyword::MATERIALIZED, Keyword::VIEW]) {
2033            self.parse_create_view(true, or_replace)
2034        } else if self.parse_keywords(&[Keyword::MATERIALIZED, Keyword::SOURCE]) {
2035            parser_err!("CREATE MATERIALIZED SOURCE has been deprecated, use CREATE TABLE instead")
2036        } else if self.parse_keyword(Keyword::SOURCE) {
2037            self.parse_create_source(or_replace, temporary)
2038        } else if self.parse_keyword(Keyword::SINK) {
2039            self.parse_create_sink(or_replace)
2040        } else if self.parse_keyword(Keyword::SUBSCRIPTION) {
2041            self.parse_create_subscription(or_replace)
2042        } else if self.parse_keyword(Keyword::CONNECTION) {
2043            self.parse_create_connection()
2044        } else if self.parse_keyword(Keyword::FUNCTION) {
2045            self.parse_create_function(or_replace, temporary)
2046        } else if self.parse_keyword(Keyword::AGGREGATE) {
2047            self.parse_create_aggregate(or_replace)
2048        } else if or_replace {
2049            self.expected(
2050                "[EXTERNAL] TABLE or [MATERIALIZED] VIEW or [MATERIALIZED] SOURCE or SINK or FUNCTION after CREATE OR REPLACE",
2051            )
2052        } else if self.parse_keyword(Keyword::INDEX) {
2053            self.parse_create_index(false)
2054        } else if self.parse_keywords(&[Keyword::UNIQUE, Keyword::INDEX]) {
2055            self.parse_create_index(true)
2056        } else if self.parse_keyword(Keyword::SCHEMA) {
2057            self.parse_create_schema()
2058        } else if self.parse_keyword(Keyword::DATABASE) {
2059            self.parse_create_database()
2060        } else if self.parse_keyword(Keyword::USER) {
2061            self.parse_create_user()
2062        } else if self.parse_keyword(Keyword::SECRET) {
2063            self.parse_create_secret()
2064        } else {
2065            self.expected("an object type after CREATE")
2066        }
2067    }
2068
2069    pub fn parse_create_schema(&mut self) -> ModalResult<Statement> {
2070        let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
2071        let (schema_name, owner) = if self.parse_keyword(Keyword::AUTHORIZATION) {
2072            let owner = self.parse_object_name()?;
2073            (owner.clone(), Some(owner))
2074        } else {
2075            let schema_name = self.parse_object_name()?;
2076            let owner = if self.parse_keyword(Keyword::AUTHORIZATION) {
2077                Some(self.parse_object_name()?)
2078            } else {
2079                None
2080            };
2081            (schema_name, owner)
2082        };
2083        Ok(Statement::CreateSchema {
2084            schema_name,
2085            if_not_exists,
2086            owner,
2087        })
2088    }
2089
2090    pub fn parse_create_database(&mut self) -> ModalResult<Statement> {
2091        let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
2092        let db_name = self.parse_object_name()?;
2093        let _ = self.parse_keyword(Keyword::WITH);
2094
2095        let mut owner = None;
2096        let mut resource_group = None;
2097
2098        while let Some(keyword) =
2099            self.parse_one_of_keywords(&[Keyword::OWNER, Keyword::RESOURCE_GROUP])
2100        {
2101            match keyword {
2102                Keyword::OWNER => {
2103                    if owner.is_some() {
2104                        parser_err!("duplicate OWNER clause in CREATE DATABASE");
2105                    }
2106
2107                    let _ = self.consume_token(&Token::Eq);
2108                    owner = Some(self.parse_object_name()?);
2109                }
2110                Keyword::RESOURCE_GROUP => {
2111                    if resource_group.is_some() {
2112                        parser_err!("duplicate RESOURCE_GROUP clause in CREATE DATABASE");
2113                    }
2114
2115                    let _ = self.consume_token(&Token::Eq);
2116                    resource_group = Some(self.parse_set_variable()?);
2117                }
2118                _ => unreachable!(),
2119            }
2120        }
2121
2122        Ok(Statement::CreateDatabase {
2123            db_name,
2124            if_not_exists,
2125            owner,
2126            resource_group,
2127        })
2128    }
2129
2130    pub fn parse_create_view(
2131        &mut self,
2132        materialized: bool,
2133        or_replace: bool,
2134    ) -> ModalResult<Statement> {
2135        let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
2136        // Many dialects support `OR ALTER` right after `CREATE`, but we don't (yet).
2137        // ANSI SQL and Postgres support RECURSIVE here, but we don't support it either.
2138        let name = self.parse_object_name()?;
2139        let columns = self.parse_parenthesized_column_list(Optional)?;
2140        let with_options = self.parse_options_with_preceding_keyword(Keyword::WITH)?;
2141        self.expect_keyword(Keyword::AS)?;
2142        let query = Box::new(self.parse_query()?);
2143        let emit_mode = if materialized {
2144            self.parse_emit_mode()?
2145        } else {
2146            None
2147        };
2148        // Optional `WITH [ CASCADED | LOCAL ] CHECK OPTION` is widely supported here.
2149        Ok(Statement::CreateView {
2150            if_not_exists,
2151            name,
2152            columns,
2153            query,
2154            materialized,
2155            or_replace,
2156            with_options,
2157            emit_mode,
2158        })
2159    }
2160
2161    // CREATE [OR REPLACE]?
2162    // [TEMPORARY] SOURCE
2163    // [IF NOT EXISTS]?
2164    // <source_name: Ident>
2165    // [COLUMNS]?
2166    // [WITH (properties)]?
2167    // ROW FORMAT <row_format: Ident>
2168    // [ROW SCHEMA LOCATION <row_schema_location: String>]?
2169    pub fn parse_create_source(
2170        &mut self,
2171        _or_replace: bool,
2172        temporary: bool,
2173    ) -> ModalResult<Statement> {
2174        impl_parse_to!(if_not_exists => [Keyword::IF, Keyword::NOT, Keyword::EXISTS], self);
2175        impl_parse_to!(source_name: ObjectName, self);
2176
2177        // parse columns
2178        let (columns, constraints, source_watermarks, wildcard_idx) =
2179            self.parse_columns_with_watermark()?;
2180        let include_options = self.parse_include_options()?;
2181
2182        let with_options = self.parse_with_properties()?;
2183        let option = with_options
2184            .iter()
2185            .find(|&opt| opt.name.real_value() == UPSTREAM_SOURCE_KEY);
2186        let connector: String = option.map(|opt| opt.value.to_string()).unwrap_or_default();
2187        let cdc_source_job = connector.contains("-cdc");
2188        if cdc_source_job && (!columns.is_empty() || !constraints.is_empty()) {
2189            parser_err!("CDC source cannot define columns and constraints");
2190        }
2191
2192        // row format for nexmark source must be native
2193        // default row format for datagen source is native
2194        let format_encode = self.parse_format_encode_with_connector(&connector, cdc_source_job)?;
2195
2196        let stmt = CreateSourceStatement {
2197            temporary,
2198            if_not_exists,
2199            columns,
2200            wildcard_idx,
2201            constraints,
2202            source_name,
2203            with_properties: WithProperties(with_options),
2204            format_encode,
2205            source_watermarks,
2206            include_column_options: include_options,
2207        };
2208
2209        Ok(Statement::CreateSource { stmt })
2210    }
2211
2212    // CREATE [OR REPLACE]?
2213    // SINK
2214    // [IF NOT EXISTS]?
2215    // <sink_name: Ident>
2216    // FROM
2217    // <materialized_view: Ident>
2218    // [WITH (properties)]?
2219    pub fn parse_create_sink(&mut self, _or_replace: bool) -> ModalResult<Statement> {
2220        Ok(Statement::CreateSink {
2221            stmt: CreateSinkStatement::parse_to(self)?,
2222        })
2223    }
2224
2225    // CREATE
2226    // SUBSCRIPTION
2227    // [IF NOT EXISTS]?
2228    // <subscription_name: Ident>
2229    // FROM
2230    // <materialized_view: Ident>
2231    // [WITH (properties)]?
2232    pub fn parse_create_subscription(&mut self, _or_replace: bool) -> ModalResult<Statement> {
2233        Ok(Statement::CreateSubscription {
2234            stmt: CreateSubscriptionStatement::parse_to(self)?,
2235        })
2236    }
2237
2238    // CREATE
2239    // CONNECTION
2240    // [IF NOT EXISTS]?
2241    // <connection_name: Ident>
2242    // [WITH (properties)]?
2243    pub fn parse_create_connection(&mut self) -> ModalResult<Statement> {
2244        Ok(Statement::CreateConnection {
2245            stmt: CreateConnectionStatement::parse_to(self)?,
2246        })
2247    }
2248
2249    pub fn parse_create_function(
2250        &mut self,
2251        or_replace: bool,
2252        temporary: bool,
2253    ) -> ModalResult<Statement> {
2254        impl_parse_to!(if_not_exists => [Keyword::IF, Keyword::NOT, Keyword::EXISTS], self);
2255
2256        let FunctionDesc { name, args } = self.parse_function_desc()?;
2257
2258        let return_type = if self.parse_keyword(Keyword::RETURNS) {
2259            if self.parse_keyword(Keyword::TABLE) {
2260                self.expect_token(&Token::LParen)?;
2261                let mut values = vec![];
2262                loop {
2263                    values.push(self.parse_table_column_def()?);
2264                    let comma = self.consume_token(&Token::Comma);
2265                    if self.consume_token(&Token::RParen) {
2266                        // allow a trailing comma, even though it's not in standard
2267                        break;
2268                    } else if !comma {
2269                        return self.expected("',' or ')'");
2270                    }
2271                }
2272                Some(CreateFunctionReturns::Table(values))
2273            } else {
2274                Some(CreateFunctionReturns::Value(self.parse_data_type()?))
2275            }
2276        } else {
2277            None
2278        };
2279
2280        let params = self.parse_create_function_body()?;
2281        let with_options = self.parse_options_with_preceding_keyword(Keyword::WITH)?;
2282        let with_options = with_options.try_into()?;
2283        Ok(Statement::CreateFunction {
2284            or_replace,
2285            temporary,
2286            if_not_exists,
2287            name,
2288            args,
2289            returns: return_type,
2290            params,
2291            with_options,
2292        })
2293    }
2294
2295    fn parse_create_aggregate(&mut self, or_replace: bool) -> ModalResult<Statement> {
2296        impl_parse_to!(if_not_exists => [Keyword::IF, Keyword::NOT, Keyword::EXISTS], self);
2297
2298        let name = self.parse_object_name()?;
2299        self.expect_token(&Token::LParen)?;
2300        let args = self.parse_comma_separated(Parser::parse_function_arg)?;
2301        self.expect_token(&Token::RParen)?;
2302
2303        self.expect_keyword(Keyword::RETURNS)?;
2304        let returns = self.parse_data_type()?;
2305
2306        let append_only = self.parse_keywords(&[Keyword::APPEND, Keyword::ONLY]);
2307        let params = self.parse_create_function_body()?;
2308
2309        Ok(Statement::CreateAggregate {
2310            or_replace,
2311            if_not_exists,
2312            name,
2313            args,
2314            returns,
2315            append_only,
2316            params,
2317        })
2318    }
2319
2320    pub fn parse_declare(&mut self) -> ModalResult<Statement> {
2321        Ok(Statement::DeclareCursor {
2322            stmt: DeclareCursorStatement::parse_to(self)?,
2323        })
2324    }
2325
2326    pub fn parse_fetch_cursor(&mut self) -> ModalResult<Statement> {
2327        Ok(Statement::FetchCursor {
2328            stmt: FetchCursorStatement::parse_to(self)?,
2329        })
2330    }
2331
2332    pub fn parse_close_cursor(&mut self) -> ModalResult<Statement> {
2333        Ok(Statement::CloseCursor {
2334            stmt: CloseCursorStatement::parse_to(self)?,
2335        })
2336    }
2337
2338    fn parse_table_column_def(&mut self) -> ModalResult<TableColumnDef> {
2339        Ok(TableColumnDef {
2340            name: self.parse_identifier_non_reserved()?,
2341            data_type: self.parse_data_type()?,
2342        })
2343    }
2344
2345    fn parse_function_arg(&mut self) -> ModalResult<OperateFunctionArg> {
2346        let mode = if self.parse_keyword(Keyword::IN) {
2347            Some(ArgMode::In)
2348        } else if self.parse_keyword(Keyword::OUT) {
2349            Some(ArgMode::Out)
2350        } else if self.parse_keyword(Keyword::INOUT) {
2351            Some(ArgMode::InOut)
2352        } else {
2353            None
2354        };
2355
2356        // parse: [ argname ] argtype
2357        let mut name = None;
2358        let mut data_type = self.parse_data_type()?;
2359        if let DataType::Custom(n) = &data_type
2360            && !matches!(self.peek_token().token, Token::Comma | Token::RParen)
2361        {
2362            // the first token is actually a name
2363            name = Some(n.0[0].clone());
2364            data_type = self.parse_data_type()?;
2365        }
2366
2367        let default_expr = if self.parse_keyword(Keyword::DEFAULT) || self.consume_token(&Token::Eq)
2368        {
2369            Some(self.parse_expr()?)
2370        } else {
2371            None
2372        };
2373        Ok(OperateFunctionArg {
2374            mode,
2375            name,
2376            data_type,
2377            default_expr,
2378        })
2379    }
2380
2381    fn parse_create_function_body(&mut self) -> ModalResult<CreateFunctionBody> {
2382        let mut body = CreateFunctionBody::default();
2383        loop {
2384            fn ensure_not_set<T>(field: &Option<T>, name: &str) -> ModalResult<()> {
2385                if field.is_some() {
2386                    parser_err!("{name} specified more than once");
2387                }
2388                Ok(())
2389            }
2390            if self.parse_keyword(Keyword::AS) {
2391                ensure_not_set(&body.as_, "AS")?;
2392                body.as_ = Some(self.parse_function_definition()?);
2393            } else if self.parse_keyword(Keyword::LANGUAGE) {
2394                ensure_not_set(&body.language, "LANGUAGE")?;
2395                body.language = Some(self.parse_identifier()?);
2396            } else if self.parse_keyword(Keyword::RUNTIME) {
2397                ensure_not_set(&body.runtime, "RUNTIME")?;
2398                body.runtime = Some(self.parse_identifier()?);
2399            } else if self.parse_keyword(Keyword::IMMUTABLE) {
2400                ensure_not_set(&body.behavior, "IMMUTABLE | STABLE | VOLATILE")?;
2401                body.behavior = Some(FunctionBehavior::Immutable);
2402            } else if self.parse_keyword(Keyword::STABLE) {
2403                ensure_not_set(&body.behavior, "IMMUTABLE | STABLE | VOLATILE")?;
2404                body.behavior = Some(FunctionBehavior::Stable);
2405            } else if self.parse_keyword(Keyword::VOLATILE) {
2406                ensure_not_set(&body.behavior, "IMMUTABLE | STABLE | VOLATILE")?;
2407                body.behavior = Some(FunctionBehavior::Volatile);
2408            } else if self.parse_keyword(Keyword::RETURN) {
2409                ensure_not_set(&body.return_, "RETURN")?;
2410                body.return_ = Some(self.parse_expr()?);
2411            } else if self.parse_keyword(Keyword::USING) {
2412                ensure_not_set(&body.using, "USING")?;
2413                body.using = Some(self.parse_create_function_using()?);
2414            } else {
2415                return Ok(body);
2416            }
2417        }
2418    }
2419
2420    fn parse_create_function_using(&mut self) -> ModalResult<CreateFunctionUsing> {
2421        let keyword = self.expect_one_of_keywords(&[Keyword::LINK, Keyword::BASE64])?;
2422
2423        match keyword {
2424            Keyword::LINK => {
2425                let uri = self.parse_literal_string()?;
2426                Ok(CreateFunctionUsing::Link(uri))
2427            }
2428            Keyword::BASE64 => {
2429                let base64 = self.parse_literal_string()?;
2430                Ok(CreateFunctionUsing::Base64(base64))
2431            }
2432            _ => unreachable!("{}", keyword),
2433        }
2434    }
2435
2436    // CREATE USER name [ [ WITH ] option [ ... ] ]
2437    // where option can be:
2438    //       SUPERUSER | NOSUPERUSER
2439    //     | CREATEDB | NOCREATEDB
2440    //     | CREATEUSER | NOCREATEUSER
2441    //     | LOGIN | NOLOGIN
2442    //     | [ ENCRYPTED ] PASSWORD 'password' | PASSWORD NULL | OAUTH
2443    fn parse_create_user(&mut self) -> ModalResult<Statement> {
2444        Ok(Statement::CreateUser(CreateUserStatement::parse_to(self)?))
2445    }
2446
2447    fn parse_create_secret(&mut self) -> ModalResult<Statement> {
2448        Ok(Statement::CreateSecret {
2449            stmt: CreateSecretStatement::parse_to(self)?,
2450        })
2451    }
2452
2453    pub fn parse_with_properties(&mut self) -> ModalResult<Vec<SqlOption>> {
2454        Ok(self
2455            .parse_options_with_preceding_keyword(Keyword::WITH)?
2456            .to_vec())
2457    }
2458
2459    pub fn parse_discard(&mut self) -> ModalResult<Statement> {
2460        self.expect_keyword(Keyword::ALL)?;
2461        Ok(Statement::Discard(DiscardType::All))
2462    }
2463
2464    pub fn parse_drop(&mut self) -> ModalResult<Statement> {
2465        if self.parse_keyword(Keyword::FUNCTION) {
2466            return self.parse_drop_function();
2467        } else if self.parse_keyword(Keyword::AGGREGATE) {
2468            return self.parse_drop_aggregate();
2469        }
2470        Ok(Statement::Drop(DropStatement::parse_to(self)?))
2471    }
2472
2473    /// ```sql
2474    /// DROP FUNCTION [ IF EXISTS ] name [ ( [ [ argmode ] [ argname ] argtype [, ...] ] ) ] [, ...]
2475    /// [ CASCADE | RESTRICT ]
2476    /// ```
2477    fn parse_drop_function(&mut self) -> ModalResult<Statement> {
2478        let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
2479        let func_desc = self.parse_comma_separated(Parser::parse_function_desc)?;
2480        let option = match self.parse_one_of_keywords(&[Keyword::CASCADE, Keyword::RESTRICT]) {
2481            Some(Keyword::CASCADE) => Some(ReferentialAction::Cascade),
2482            Some(Keyword::RESTRICT) => Some(ReferentialAction::Restrict),
2483            _ => None,
2484        };
2485        Ok(Statement::DropFunction {
2486            if_exists,
2487            func_desc,
2488            option,
2489        })
2490    }
2491
2492    /// ```sql
2493    /// DROP AGGREGATE [ IF EXISTS ] name [ ( [ [ argmode ] [ argname ] argtype [, ...] ] ) ] [, ...]
2494    /// [ CASCADE | RESTRICT ]
2495    /// ```
2496    fn parse_drop_aggregate(&mut self) -> ModalResult<Statement> {
2497        let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
2498        let func_desc = self.parse_comma_separated(Parser::parse_function_desc)?;
2499        let option = match self.parse_one_of_keywords(&[Keyword::CASCADE, Keyword::RESTRICT]) {
2500            Some(Keyword::CASCADE) => Some(ReferentialAction::Cascade),
2501            Some(Keyword::RESTRICT) => Some(ReferentialAction::Restrict),
2502            _ => None,
2503        };
2504        Ok(Statement::DropAggregate {
2505            if_exists,
2506            func_desc,
2507            option,
2508        })
2509    }
2510
2511    fn parse_function_desc(&mut self) -> ModalResult<FunctionDesc> {
2512        let name = self.parse_object_name()?;
2513
2514        let args = if self.consume_token(&Token::LParen) {
2515            if self.consume_token(&Token::RParen) {
2516                Some(vec![])
2517            } else {
2518                let args = self.parse_comma_separated(Parser::parse_function_arg)?;
2519                self.expect_token(&Token::RParen)?;
2520                Some(args)
2521            }
2522        } else {
2523            None
2524        };
2525
2526        Ok(FunctionDesc { name, args })
2527    }
2528
2529    pub fn parse_create_index(&mut self, unique: bool) -> ModalResult<Statement> {
2530        let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
2531        let index_name = self.parse_object_name()?;
2532        self.expect_keyword(Keyword::ON)?;
2533        let table_name = self.parse_object_name()?;
2534        self.expect_token(&Token::LParen)?;
2535        let columns = self.parse_comma_separated(Parser::parse_order_by_expr)?;
2536        self.expect_token(&Token::RParen)?;
2537        let mut include = vec![];
2538        if self.parse_keyword(Keyword::INCLUDE) {
2539            self.expect_token(&Token::LParen)?;
2540            include = self.parse_comma_separated(Parser::parse_identifier_non_reserved)?;
2541            self.expect_token(&Token::RParen)?;
2542        }
2543        let mut distributed_by = vec![];
2544        if self.parse_keywords(&[Keyword::DISTRIBUTED, Keyword::BY]) {
2545            self.expect_token(&Token::LParen)?;
2546            distributed_by = self.parse_comma_separated(Parser::parse_expr)?;
2547            self.expect_token(&Token::RParen)?;
2548        }
2549        Ok(Statement::CreateIndex {
2550            name: index_name,
2551            table_name,
2552            columns,
2553            include,
2554            distributed_by,
2555            unique,
2556            if_not_exists,
2557        })
2558    }
2559
2560    pub fn parse_with_version_column(&mut self) -> ModalResult<Option<Ident>> {
2561        if self.parse_keywords(&[Keyword::WITH, Keyword::VERSION, Keyword::COLUMN]) {
2562            self.expect_token(&Token::LParen)?;
2563            let name = self.parse_identifier_non_reserved()?;
2564            self.expect_token(&Token::RParen)?;
2565            Ok(Some(name))
2566        } else {
2567            Ok(None)
2568        }
2569    }
2570
2571    pub fn parse_on_conflict(&mut self) -> ModalResult<Option<OnConflict>> {
2572        if self.parse_keywords(&[Keyword::ON, Keyword::CONFLICT]) {
2573            self.parse_handle_conflict_behavior()
2574        } else {
2575            Ok(None)
2576        }
2577    }
2578
2579    pub fn parse_create_table(
2580        &mut self,
2581        or_replace: bool,
2582        temporary: bool,
2583    ) -> ModalResult<Statement> {
2584        let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
2585        let table_name = self.parse_object_name()?;
2586        // parse optional column list (schema) and watermarks on source.
2587        let (columns, constraints, source_watermarks, wildcard_idx) =
2588            self.parse_columns_with_watermark()?;
2589
2590        let append_only = if self.parse_keyword(Keyword::APPEND) {
2591            self.expect_keyword(Keyword::ONLY)?;
2592            true
2593        } else {
2594            false
2595        };
2596
2597        let on_conflict = self.parse_on_conflict()?;
2598
2599        let with_version_column = self.parse_with_version_column()?;
2600        let include_options = self.parse_include_options()?;
2601
2602        // PostgreSQL supports `WITH ( options )`, before `AS`
2603        let with_options = self.parse_with_properties()?;
2604
2605        let option = with_options
2606            .iter()
2607            .find(|&opt| opt.name.real_value() == UPSTREAM_SOURCE_KEY);
2608        let connector = option.map(|opt| opt.value.to_string());
2609        let contain_webhook =
2610            connector.is_some() && connector.as_ref().unwrap().contains(WEBHOOK_CONNECTOR);
2611
2612        // webhook connector does not require row format
2613        let format_encode = if let Some(connector) = connector
2614            && !contain_webhook
2615        {
2616            Some(self.parse_format_encode_with_connector(&connector, false)?)
2617        } else {
2618            None // Table is NOT created with an external connector.
2619        };
2620        // Parse optional `AS ( query )`
2621        let query = if self.parse_keyword(Keyword::AS) {
2622            if !source_watermarks.is_empty() {
2623                parser_err!("Watermarks can't be defined on table created by CREATE TABLE AS");
2624            }
2625            Some(Box::new(self.parse_query()?))
2626        } else {
2627            None
2628        };
2629
2630        let cdc_table_info = if self.parse_keyword(Keyword::FROM) {
2631            let source_name = self.parse_object_name()?;
2632            self.expect_keyword(Keyword::TABLE)?;
2633            let external_table_name = self.parse_literal_string()?;
2634            Some(CdcTableInfo {
2635                source_name,
2636                external_table_name,
2637            })
2638        } else {
2639            None
2640        };
2641
2642        let webhook_info = if self.parse_keyword(Keyword::VALIDATE) {
2643            if !contain_webhook {
2644                parser_err!("VALIDATE is only supported for tables created with webhook source");
2645            }
2646
2647            let wait_for_persistence = with_options
2648                .iter()
2649                .find(|&opt| opt.name.real_value() == WEBHOOK_WAIT_FOR_PERSISTENCE)
2650                .map(|opt| opt.value.to_string().eq_ignore_ascii_case("true"))
2651                .unwrap_or(true);
2652            let secret_ref = if self.parse_keyword(Keyword::SECRET) {
2653                let secret_ref = self.parse_secret_ref()?;
2654                if secret_ref.ref_as == SecretRefAsType::File {
2655                    parser_err!("Secret for SECURE_COMPARE() does not support AS FILE");
2656                };
2657                Some(secret_ref)
2658            } else {
2659                None
2660            };
2661
2662            self.expect_keyword(Keyword::AS)?;
2663            let signature_expr = self.parse_function()?;
2664
2665            Some(WebhookSourceInfo {
2666                secret_ref,
2667                signature_expr,
2668                wait_for_persistence,
2669            })
2670        } else {
2671            None
2672        };
2673
2674        let engine = if self.parse_keyword(Keyword::ENGINE) {
2675            self.expect_token(&Token::Eq)?;
2676            let engine_name = self.parse_object_name()?;
2677            if "iceberg".eq_ignore_ascii_case(&engine_name.real_value()) {
2678                Engine::Iceberg
2679            } else if "hummock".eq_ignore_ascii_case(&engine_name.real_value()) {
2680                Engine::Hummock
2681            } else {
2682                parser_err!("Unsupported engine: {}", engine_name);
2683            }
2684        } else {
2685            Engine::Hummock
2686        };
2687
2688        Ok(Statement::CreateTable {
2689            name: table_name,
2690            temporary,
2691            columns,
2692            wildcard_idx,
2693            constraints,
2694            with_options,
2695            or_replace,
2696            if_not_exists,
2697            format_encode,
2698            source_watermarks,
2699            append_only,
2700            on_conflict,
2701            with_version_column,
2702            query,
2703            cdc_table_info,
2704            include_column_options: include_options,
2705            webhook_info,
2706            engine,
2707        })
2708    }
2709
2710    pub fn parse_include_options(&mut self) -> ModalResult<IncludeOption> {
2711        let mut options = vec![];
2712        while self.parse_keyword(Keyword::INCLUDE) {
2713            let column_type = self.parse_identifier()?;
2714
2715            let mut column_inner_field = None;
2716            let mut header_inner_expect_type = None;
2717            if let Token::SingleQuotedString(inner_field) = self.peek_token().token {
2718                self.next_token();
2719                column_inner_field = Some(inner_field);
2720
2721                // `verify` rejects `DataType::Custom` so that a following `INCLUDE` (or even `WITH`)
2722                // will not be misrecognized as a DataType.
2723                //
2724                // For example, the following look structurally the same because `INCLUDE` is not a
2725                // reserved keyword. (`AS` is reserved.)
2726                // * `INCLUDE header 'foo' varchar`
2727                // * `INCLUDE header 'foo' INCLUDE`
2728                //
2729                // To be honest `bytea` shall be a `DataType::Custom` rather than a keyword, and the
2730                // logic here shall be:
2731                // ```
2732                // match dt {
2733                //     DataType::Custom(name) => allowed.contains(name.real_value()),
2734                //     _ => true,
2735                // }
2736                // ```
2737                // An allowlist is better than a denylist, as the following token may be other than
2738                // `INCLUDE` or `WITH` in the future.
2739                //
2740                // If this sounds too complicated - it means we should have designed this extension
2741                // syntax differently to make ambiguity handling easier.
2742                header_inner_expect_type =
2743                    opt(parser_v2::data_type.verify(|dt| !matches!(dt, DataType::Custom(_))))
2744                        .parse_next(self)?;
2745            }
2746
2747            let mut column_alias = None;
2748            if self.parse_keyword(Keyword::AS) {
2749                column_alias = Some(self.parse_identifier()?);
2750            }
2751
2752            options.push(IncludeOptionItem {
2753                column_type,
2754                inner_field: column_inner_field,
2755                column_alias,
2756                header_inner_expect_type,
2757            });
2758
2759            // tolerate previous bug #18800 of displaying with comma separation
2760            let _ = self.consume_token(&Token::Comma);
2761        }
2762        Ok(options)
2763    }
2764
2765    pub fn parse_columns_with_watermark(&mut self) -> ModalResult<ColumnsDefTuple> {
2766        let mut columns = vec![];
2767        let mut constraints = vec![];
2768        let mut watermarks = vec![];
2769        let mut wildcard_idx = None;
2770        if !self.consume_token(&Token::LParen) || self.consume_token(&Token::RParen) {
2771            return Ok((columns, constraints, watermarks, wildcard_idx));
2772        }
2773
2774        loop {
2775            if self.consume_token(&Token::Mul) {
2776                if wildcard_idx.is_none() {
2777                    wildcard_idx = Some(columns.len());
2778                } else {
2779                    parser_err!("At most 1 wildcard is allowed in source definetion");
2780                }
2781            } else if let Some(constraint) = self.parse_optional_table_constraint()? {
2782                constraints.push(constraint);
2783            } else if let Some(watermark) = self.parse_optional_watermark()? {
2784                watermarks.push(watermark);
2785                if watermarks.len() > 1 {
2786                    // TODO(yuhao): allow multiple watermark on source.
2787                    parser_err!("Only 1 watermark is allowed to be defined on source.");
2788                }
2789            } else if let Token::Word(_) = self.peek_token().token {
2790                columns.push(self.parse_column_def()?);
2791            } else {
2792                return self.expected("column name or constraint definition");
2793            }
2794            let comma = self.consume_token(&Token::Comma);
2795            if self.consume_token(&Token::RParen) {
2796                // allow a trailing comma, even though it's not in standard
2797                break;
2798            } else if !comma {
2799                return self.expected("',' or ')' after column definition");
2800            }
2801        }
2802
2803        Ok((columns, constraints, watermarks, wildcard_idx))
2804    }
2805
2806    fn parse_column_def(&mut self) -> ModalResult<ColumnDef> {
2807        let name = self.parse_identifier_non_reserved()?;
2808        let data_type = if let Token::Word(_) = self.peek_token().token {
2809            Some(self.parse_data_type()?)
2810        } else {
2811            None
2812        };
2813
2814        let collation = if self.parse_keyword(Keyword::COLLATE) {
2815            Some(self.parse_object_name()?)
2816        } else {
2817            None
2818        };
2819        let mut options = vec![];
2820        loop {
2821            if self.parse_keyword(Keyword::CONSTRAINT) {
2822                let name = Some(self.parse_identifier_non_reserved()?);
2823                if let Some(option) = self.parse_optional_column_option()? {
2824                    options.push(ColumnOptionDef { name, option });
2825                } else {
2826                    return self.expected("constraint details after CONSTRAINT <name>");
2827                }
2828            } else if let Some(option) = self.parse_optional_column_option()? {
2829                options.push(ColumnOptionDef { name: None, option });
2830            } else {
2831                break;
2832            };
2833        }
2834        Ok(ColumnDef {
2835            name,
2836            data_type,
2837            collation,
2838            options,
2839        })
2840    }
2841
2842    pub fn parse_optional_column_option(&mut self) -> ModalResult<Option<ColumnOption>> {
2843        if self.parse_keywords(&[Keyword::NOT, Keyword::NULL]) {
2844            Ok(Some(ColumnOption::NotNull))
2845        } else if self.parse_keyword(Keyword::NULL) {
2846            Ok(Some(ColumnOption::Null))
2847        } else if self.parse_keyword(Keyword::DEFAULT) {
2848            if self.parse_keyword(Keyword::INTERNAL) {
2849                Ok(Some(ColumnOption::DefaultValueInternal {
2850                    // Placeholder. Will fill during definition purification for schema change.
2851                    persisted: Default::default(),
2852                    expr: None,
2853                }))
2854            } else {
2855                Ok(Some(ColumnOption::DefaultValue(self.parse_expr()?)))
2856            }
2857        } else if self.parse_keywords(&[Keyword::PRIMARY, Keyword::KEY]) {
2858            Ok(Some(ColumnOption::Unique { is_primary: true }))
2859        } else if self.parse_keyword(Keyword::UNIQUE) {
2860            Ok(Some(ColumnOption::Unique { is_primary: false }))
2861        } else if self.parse_keyword(Keyword::REFERENCES) {
2862            let foreign_table = self.parse_object_name()?;
2863            // PostgreSQL allows omitting the column list and
2864            // uses the primary key column of the foreign table by default
2865            let referred_columns = self.parse_parenthesized_column_list(Optional)?;
2866            let mut on_delete = None;
2867            let mut on_update = None;
2868            loop {
2869                if on_delete.is_none() && self.parse_keywords(&[Keyword::ON, Keyword::DELETE]) {
2870                    on_delete = Some(self.parse_referential_action()?);
2871                } else if on_update.is_none()
2872                    && self.parse_keywords(&[Keyword::ON, Keyword::UPDATE])
2873                {
2874                    on_update = Some(self.parse_referential_action()?);
2875                } else {
2876                    break;
2877                }
2878            }
2879            Ok(Some(ColumnOption::ForeignKey {
2880                foreign_table,
2881                referred_columns,
2882                on_delete,
2883                on_update,
2884            }))
2885        } else if self.parse_keyword(Keyword::CHECK) {
2886            self.expect_token(&Token::LParen)?;
2887            let expr = self.parse_expr()?;
2888            self.expect_token(&Token::RParen)?;
2889            Ok(Some(ColumnOption::Check(expr)))
2890        } else if self.parse_keyword(Keyword::AS) {
2891            Ok(Some(ColumnOption::GeneratedColumns(self.parse_expr()?)))
2892        } else {
2893            Ok(None)
2894        }
2895    }
2896
2897    pub fn parse_handle_conflict_behavior(&mut self) -> ModalResult<Option<OnConflict>> {
2898        if self.parse_keyword(Keyword::OVERWRITE) {
2899            // compatible with v1.9 - v2.0
2900            Ok(Some(OnConflict::UpdateFull))
2901        } else if self.parse_keyword(Keyword::IGNORE) {
2902            // compatible with v1.9 - v2.0
2903            Ok(Some(OnConflict::Nothing))
2904        } else if self.parse_keywords(&[
2905            Keyword::DO,
2906            Keyword::UPDATE,
2907            Keyword::IF,
2908            Keyword::NOT,
2909            Keyword::NULL,
2910        ]) {
2911            Ok(Some(OnConflict::UpdateIfNotNull))
2912        } else if self.parse_keywords(&[Keyword::DO, Keyword::UPDATE, Keyword::FULL]) {
2913            Ok(Some(OnConflict::UpdateFull))
2914        } else if self.parse_keywords(&[Keyword::DO, Keyword::NOTHING]) {
2915            Ok(Some(OnConflict::Nothing))
2916        } else {
2917            Ok(None)
2918        }
2919    }
2920
2921    pub fn parse_referential_action(&mut self) -> ModalResult<ReferentialAction> {
2922        if self.parse_keyword(Keyword::RESTRICT) {
2923            Ok(ReferentialAction::Restrict)
2924        } else if self.parse_keyword(Keyword::CASCADE) {
2925            Ok(ReferentialAction::Cascade)
2926        } else if self.parse_keywords(&[Keyword::SET, Keyword::NULL]) {
2927            Ok(ReferentialAction::SetNull)
2928        } else if self.parse_keywords(&[Keyword::NO, Keyword::ACTION]) {
2929            Ok(ReferentialAction::NoAction)
2930        } else if self.parse_keywords(&[Keyword::SET, Keyword::DEFAULT]) {
2931            Ok(ReferentialAction::SetDefault)
2932        } else {
2933            self.expected("one of RESTRICT, CASCADE, SET NULL, NO ACTION or SET DEFAULT")
2934        }
2935    }
2936
2937    pub fn parse_optional_watermark(&mut self) -> ModalResult<Option<SourceWatermark>> {
2938        if self.parse_keyword(Keyword::WATERMARK) {
2939            self.expect_keyword(Keyword::FOR)?;
2940            let column = self.parse_identifier_non_reserved()?;
2941            self.expect_keyword(Keyword::AS)?;
2942            let expr = self.parse_expr()?;
2943            Ok(Some(SourceWatermark { column, expr }))
2944        } else {
2945            Ok(None)
2946        }
2947    }
2948
2949    pub fn parse_optional_table_constraint(&mut self) -> ModalResult<Option<TableConstraint>> {
2950        let name = if self.parse_keyword(Keyword::CONSTRAINT) {
2951            Some(self.parse_identifier_non_reserved()?)
2952        } else {
2953            None
2954        };
2955        let checkpoint = *self;
2956        let token = self.next_token();
2957        match token.token {
2958            Token::Word(w) if w.keyword == Keyword::PRIMARY || w.keyword == Keyword::UNIQUE => {
2959                let is_primary = w.keyword == Keyword::PRIMARY;
2960                if is_primary {
2961                    self.expect_keyword(Keyword::KEY)?;
2962                }
2963                let columns = self.parse_parenthesized_column_list(Mandatory)?;
2964                Ok(Some(TableConstraint::Unique {
2965                    name,
2966                    columns,
2967                    is_primary,
2968                }))
2969            }
2970            Token::Word(w) if w.keyword == Keyword::FOREIGN => {
2971                self.expect_keyword(Keyword::KEY)?;
2972                let columns = self.parse_parenthesized_column_list(Mandatory)?;
2973                self.expect_keyword(Keyword::REFERENCES)?;
2974                let foreign_table = self.parse_object_name()?;
2975                let referred_columns = self.parse_parenthesized_column_list(Mandatory)?;
2976                let mut on_delete = None;
2977                let mut on_update = None;
2978                loop {
2979                    if on_delete.is_none() && self.parse_keywords(&[Keyword::ON, Keyword::DELETE]) {
2980                        on_delete = Some(self.parse_referential_action()?);
2981                    } else if on_update.is_none()
2982                        && self.parse_keywords(&[Keyword::ON, Keyword::UPDATE])
2983                    {
2984                        on_update = Some(self.parse_referential_action()?);
2985                    } else {
2986                        break;
2987                    }
2988                }
2989                Ok(Some(TableConstraint::ForeignKey {
2990                    name,
2991                    columns,
2992                    foreign_table,
2993                    referred_columns,
2994                    on_delete,
2995                    on_update,
2996                }))
2997            }
2998            Token::Word(w) if w.keyword == Keyword::CHECK => {
2999                self.expect_token(&Token::LParen)?;
3000                let expr = Box::new(self.parse_expr()?);
3001                self.expect_token(&Token::RParen)?;
3002                Ok(Some(TableConstraint::Check { name, expr }))
3003            }
3004            _ => {
3005                *self = checkpoint;
3006                if name.is_some() {
3007                    self.expected("PRIMARY, UNIQUE, FOREIGN, or CHECK")
3008                } else {
3009                    Ok(None)
3010                }
3011            }
3012        }
3013    }
3014
3015    pub fn parse_options_with_preceding_keyword(
3016        &mut self,
3017        keyword: Keyword,
3018    ) -> ModalResult<Vec<SqlOption>> {
3019        if self.parse_keyword(keyword) {
3020            self.expect_token(&Token::LParen)?;
3021            self.parse_options_inner()
3022        } else {
3023            Ok(vec![])
3024        }
3025    }
3026
3027    pub fn parse_options(&mut self) -> ModalResult<Vec<SqlOption>> {
3028        if self.peek_token() == Token::LParen {
3029            self.next_token();
3030            self.parse_options_inner()
3031        } else {
3032            Ok(vec![])
3033        }
3034    }
3035
3036    // has parsed a LParen
3037    pub fn parse_options_inner(&mut self) -> ModalResult<Vec<SqlOption>> {
3038        let mut values = vec![];
3039        loop {
3040            values.push(Parser::parse_sql_option(self)?);
3041            let comma = self.consume_token(&Token::Comma);
3042            if self.consume_token(&Token::RParen) {
3043                // allow a trailing comma, even though it's not in standard
3044                break;
3045            } else if !comma {
3046                return self.expected("',' or ')' after option definition");
3047            }
3048        }
3049        Ok(values)
3050    }
3051
3052    pub fn parse_sql_option(&mut self) -> ModalResult<SqlOption> {
3053        let name = self.parse_object_name()?;
3054        self.expect_token(&Token::Eq)?;
3055        let value = {
3056            const CONNECTION_REF_KEY: &str = "connection";
3057            if name.real_value().eq_ignore_ascii_case(CONNECTION_REF_KEY) {
3058                let connection_name = self.parse_object_name()?;
3059                // tolerate previous buggy Display that outputs `connection = connection foo`
3060                let connection_name = match connection_name.0.as_slice() {
3061                    [ident] if ident.real_value() == CONNECTION_REF_KEY => {
3062                        self.parse_object_name()?
3063                    }
3064                    _ => connection_name,
3065                };
3066                SqlOptionValue::ConnectionRef(ConnectionRefValue { connection_name })
3067            } else {
3068                self.parse_value_and_obj_ref::<false>()?
3069            }
3070        };
3071        Ok(SqlOption { name, value })
3072    }
3073
3074    pub fn parse_since(&mut self) -> ModalResult<Since> {
3075        if self.parse_keyword(Keyword::SINCE) {
3076            let checkpoint = *self;
3077            let token = self.next_token();
3078            match token.token {
3079                Token::Word(w) => {
3080                    let ident = w.to_ident()?;
3081                    // Backward compatibility for now.
3082                    if ident.real_value() == "proctime" || ident.real_value() == "now" {
3083                        self.expect_token(&Token::LParen)?;
3084                        self.expect_token(&Token::RParen)?;
3085                        Ok(Since::ProcessTime)
3086                    } else if ident.real_value() == "begin" {
3087                        self.expect_token(&Token::LParen)?;
3088                        self.expect_token(&Token::RParen)?;
3089                        Ok(Since::Begin)
3090                    } else {
3091                        parser_err!(
3092                            "Expected proctime(), begin() or now(), found: {}",
3093                            ident.real_value()
3094                        )
3095                    }
3096                }
3097                Token::Number(s) => {
3098                    let num = s
3099                        .parse::<u64>()
3100                        .map_err(|e| StrError(format!("Could not parse '{}' as u64: {}", s, e)))?;
3101                    Ok(Since::TimestampMsNum(num))
3102                }
3103                _ => self.expected_at(checkpoint, "proctime(), begin() , now(), Number"),
3104            }
3105        } else if self.parse_word("FULL") {
3106            Ok(Since::Full)
3107        } else {
3108            Ok(Since::ProcessTime)
3109        }
3110    }
3111
3112    pub fn parse_emit_mode(&mut self) -> ModalResult<Option<EmitMode>> {
3113        if self.parse_keyword(Keyword::EMIT) {
3114            match self.parse_one_of_keywords(&[Keyword::IMMEDIATELY, Keyword::ON]) {
3115                Some(Keyword::IMMEDIATELY) => Ok(Some(EmitMode::Immediately)),
3116                Some(Keyword::ON) => {
3117                    self.expect_keywords(&[Keyword::WINDOW, Keyword::CLOSE])?;
3118                    Ok(Some(EmitMode::OnWindowClose))
3119                }
3120                Some(_) => unreachable!(),
3121                None => self.expected("IMMEDIATELY or ON WINDOW CLOSE after EMIT"),
3122            }
3123        } else {
3124            Ok(None)
3125        }
3126    }
3127
3128    pub fn parse_alter(&mut self) -> ModalResult<Statement> {
3129        if self.parse_keyword(Keyword::DATABASE) {
3130            self.parse_alter_database()
3131        } else if self.parse_keyword(Keyword::SCHEMA) {
3132            self.parse_alter_schema()
3133        } else if self.parse_keyword(Keyword::TABLE) {
3134            self.parse_alter_table()
3135        } else if self.parse_keyword(Keyword::INDEX) {
3136            self.parse_alter_index()
3137        } else if self.parse_keyword(Keyword::VIEW) {
3138            self.parse_alter_view(false)
3139        } else if self.parse_keywords(&[Keyword::MATERIALIZED, Keyword::VIEW]) {
3140            self.parse_alter_view(true)
3141        } else if self.parse_keyword(Keyword::SINK) {
3142            self.parse_alter_sink()
3143        } else if self.parse_keyword(Keyword::SOURCE) {
3144            self.parse_alter_source()
3145        } else if self.parse_keyword(Keyword::FUNCTION) {
3146            self.parse_alter_function()
3147        } else if self.parse_keyword(Keyword::CONNECTION) {
3148            self.parse_alter_connection()
3149        } else if self.parse_keyword(Keyword::USER) {
3150            self.parse_alter_user()
3151        } else if self.parse_keyword(Keyword::SYSTEM) {
3152            self.parse_alter_system()
3153        } else if self.parse_keyword(Keyword::SUBSCRIPTION) {
3154            self.parse_alter_subscription()
3155        } else if self.parse_keyword(Keyword::SECRET) {
3156            self.parse_alter_secret()
3157        } else if self.parse_word("FRAGMENT") {
3158            self.parse_alter_fragment()
3159        } else {
3160            self.expected(
3161                "DATABASE, FRAGMENT, SCHEMA, TABLE, INDEX, MATERIALIZED, VIEW, SINK, SUBSCRIPTION, SOURCE, FUNCTION, USER, SECRET or SYSTEM after ALTER"
3162            )
3163        }
3164    }
3165
3166    pub fn parse_alter_database(&mut self) -> ModalResult<Statement> {
3167        let database_name = self.parse_object_name()?;
3168        let operation = if self.parse_keywords(&[Keyword::OWNER, Keyword::TO]) {
3169            let owner_name: Ident = self.parse_identifier()?;
3170            AlterDatabaseOperation::ChangeOwner {
3171                new_owner_name: owner_name,
3172            }
3173        } else if self.parse_keyword(Keyword::RENAME) {
3174            if self.parse_keyword(Keyword::TO) {
3175                let database_name = self.parse_object_name()?;
3176                AlterDatabaseOperation::RenameDatabase { database_name }
3177            } else {
3178                return self.expected("TO after RENAME");
3179            }
3180        } else {
3181            return self.expected("OWNER TO after ALTER DATABASE");
3182        };
3183
3184        Ok(Statement::AlterDatabase {
3185            name: database_name,
3186            operation,
3187        })
3188    }
3189
3190    pub fn parse_alter_schema(&mut self) -> ModalResult<Statement> {
3191        let schema_name = self.parse_object_name()?;
3192        let operation = if self.parse_keywords(&[Keyword::OWNER, Keyword::TO]) {
3193            let owner_name: Ident = self.parse_identifier()?;
3194            AlterSchemaOperation::ChangeOwner {
3195                new_owner_name: owner_name,
3196            }
3197        } else if self.parse_keyword(Keyword::RENAME) {
3198            self.expect_keyword(Keyword::TO)?;
3199            let schema_name = self.parse_object_name()?;
3200            AlterSchemaOperation::RenameSchema { schema_name }
3201        } else if self.parse_keywords(&[Keyword::SWAP, Keyword::WITH]) {
3202            let target_schema = self.parse_object_name()?;
3203            AlterSchemaOperation::SwapRenameSchema { target_schema }
3204        } else {
3205            return self.expected("RENAME, OWNER TO, OR SWAP WITH after ALTER SCHEMA");
3206        };
3207
3208        Ok(Statement::AlterSchema {
3209            name: schema_name,
3210            operation,
3211        })
3212    }
3213
3214    pub fn parse_alter_user(&mut self) -> ModalResult<Statement> {
3215        Ok(Statement::AlterUser(AlterUserStatement::parse_to(self)?))
3216    }
3217
3218    pub fn parse_alter_table(&mut self) -> ModalResult<Statement> {
3219        let _ = self.parse_keyword(Keyword::ONLY);
3220        let table_name = self.parse_object_name()?;
3221        let operation = if self.parse_keyword(Keyword::ADD) {
3222            if let Some(constraint) = self.parse_optional_table_constraint()? {
3223                AlterTableOperation::AddConstraint(constraint)
3224            } else {
3225                let _ = self.parse_keyword(Keyword::COLUMN);
3226                let _if_not_exists =
3227                    self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
3228                let column_def = self.parse_column_def()?;
3229                AlterTableOperation::AddColumn { column_def }
3230            }
3231        } else if self.parse_keywords(&[Keyword::DROP, Keyword::CONNECTOR]) {
3232            AlterTableOperation::DropConnector
3233        } else if self.parse_keyword(Keyword::RENAME) {
3234            if self.parse_keyword(Keyword::CONSTRAINT) {
3235                let old_name = self.parse_identifier_non_reserved()?;
3236                self.expect_keyword(Keyword::TO)?;
3237                let new_name = self.parse_identifier_non_reserved()?;
3238                AlterTableOperation::RenameConstraint { old_name, new_name }
3239            } else if self.parse_keyword(Keyword::TO) {
3240                let table_name = self.parse_object_name()?;
3241                AlterTableOperation::RenameTable { table_name }
3242            } else {
3243                let _ = self.parse_keyword(Keyword::COLUMN);
3244                let old_column_name = self.parse_identifier_non_reserved()?;
3245                self.expect_keyword(Keyword::TO)?;
3246                let new_column_name = self.parse_identifier_non_reserved()?;
3247                AlterTableOperation::RenameColumn {
3248                    old_column_name,
3249                    new_column_name,
3250                }
3251            }
3252        } else if self.parse_keywords(&[Keyword::OWNER, Keyword::TO]) {
3253            let owner_name: Ident = self.parse_identifier()?;
3254            AlterTableOperation::ChangeOwner {
3255                new_owner_name: owner_name,
3256            }
3257        } else if self.parse_keyword(Keyword::SET) {
3258            if self.parse_keyword(Keyword::SCHEMA) {
3259                let schema_name = self.parse_object_name()?;
3260                AlterTableOperation::SetSchema {
3261                    new_schema_name: schema_name,
3262                }
3263            } else if self.parse_keyword(Keyword::PARALLELISM) {
3264                if self.expect_keyword(Keyword::TO).is_err()
3265                    && self.expect_token(&Token::Eq).is_err()
3266                {
3267                    return self.expected("TO or = after ALTER TABLE SET PARALLELISM");
3268                }
3269
3270                let value = self.parse_set_variable()?;
3271
3272                let deferred = self.parse_keyword(Keyword::DEFERRED);
3273
3274                AlterTableOperation::SetParallelism {
3275                    parallelism: value,
3276                    deferred,
3277                }
3278            } else if let Some(rate_limit) = self.parse_alter_source_rate_limit(true)? {
3279                AlterTableOperation::SetSourceRateLimit { rate_limit }
3280            } else if let Some(rate_limit) = self.parse_alter_backfill_rate_limit()? {
3281                AlterTableOperation::SetBackfillRateLimit { rate_limit }
3282            } else if let Some(rate_limit) = self.parse_alter_dml_rate_limit()? {
3283                AlterTableOperation::SetDmlRateLimit { rate_limit }
3284            } else {
3285                return self
3286                    .expected("SCHEMA/PARALLELISM/SOURCE_RATE_LIMIT/DML_RATE_LIMIT after SET");
3287            }
3288        } else if self.parse_keyword(Keyword::DROP) {
3289            let _ = self.parse_keyword(Keyword::COLUMN);
3290            let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
3291            let column_name = self.parse_identifier_non_reserved()?;
3292            let cascade = self.parse_keyword(Keyword::CASCADE);
3293            AlterTableOperation::DropColumn {
3294                column_name,
3295                if_exists,
3296                cascade,
3297            }
3298        } else if self.parse_keyword(Keyword::ALTER) {
3299            let _ = self.parse_keyword(Keyword::COLUMN);
3300            let column_name = self.parse_identifier_non_reserved()?;
3301
3302            let op = if self.parse_keywords(&[Keyword::SET, Keyword::NOT, Keyword::NULL]) {
3303                AlterColumnOperation::SetNotNull {}
3304            } else if self.parse_keywords(&[Keyword::DROP, Keyword::NOT, Keyword::NULL]) {
3305                AlterColumnOperation::DropNotNull {}
3306            } else if self.parse_keywords(&[Keyword::SET, Keyword::DEFAULT]) {
3307                AlterColumnOperation::SetDefault {
3308                    value: self.parse_expr()?,
3309                }
3310            } else if self.parse_keywords(&[Keyword::DROP, Keyword::DEFAULT]) {
3311                AlterColumnOperation::DropDefault {}
3312            } else if self.parse_keywords(&[Keyword::SET, Keyword::DATA, Keyword::TYPE])
3313                || (self.parse_keyword(Keyword::TYPE))
3314            {
3315                let data_type = self.parse_data_type()?;
3316                let using = if self.parse_keyword(Keyword::USING) {
3317                    Some(self.parse_expr()?)
3318                } else {
3319                    None
3320                };
3321                AlterColumnOperation::SetDataType { data_type, using }
3322            } else {
3323                return self
3324                    .expected("SET/DROP NOT NULL, SET DEFAULT, SET DATA TYPE after ALTER COLUMN");
3325            };
3326            AlterTableOperation::AlterColumn { column_name, op }
3327        } else if self.parse_keywords(&[Keyword::REFRESH, Keyword::SCHEMA]) {
3328            AlterTableOperation::RefreshSchema
3329        } else if self.parse_keywords(&[Keyword::SWAP, Keyword::WITH]) {
3330            let target_table = self.parse_object_name()?;
3331            AlterTableOperation::SwapRenameTable { target_table }
3332        } else {
3333            return self
3334                .expected("ADD or RENAME or OWNER TO or SET or DROP or SWAP after ALTER TABLE");
3335        };
3336        Ok(Statement::AlterTable {
3337            name: table_name,
3338            operation,
3339        })
3340    }
3341
3342    /// BACKFILL_RATE_LIMIT = default | NUMBER
3343    /// BACKFILL_RATE_LIMIT TO default | NUMBER
3344    pub fn parse_alter_backfill_rate_limit(&mut self) -> ModalResult<Option<i32>> {
3345        if !self.parse_word("BACKFILL_RATE_LIMIT") {
3346            return Ok(None);
3347        }
3348        if self.expect_keyword(Keyword::TO).is_err() && self.expect_token(&Token::Eq).is_err() {
3349            return self.expected("TO or = after ALTER TABLE SET BACKFILL_RATE_LIMIT");
3350        }
3351        let rate_limit = if self.parse_keyword(Keyword::DEFAULT) {
3352            -1
3353        } else {
3354            let s = self.parse_number_value()?;
3355            if let Ok(n) = s.parse::<i32>() {
3356                n
3357            } else {
3358                return self.expected("number or DEFAULT");
3359            }
3360        };
3361        Ok(Some(rate_limit))
3362    }
3363
3364    /// DML_RATE_LIMIT = default | NUMBER
3365    /// DML_RATE_LIMIT TO default | NUMBER
3366    pub fn parse_alter_dml_rate_limit(&mut self) -> ModalResult<Option<i32>> {
3367        if !self.parse_word("DML_RATE_LIMIT") {
3368            return Ok(None);
3369        }
3370        if self.expect_keyword(Keyword::TO).is_err() && self.expect_token(&Token::Eq).is_err() {
3371            return self.expected("TO or = after ALTER TABLE SET DML_RATE_LIMIT");
3372        }
3373        let rate_limit = if self.parse_keyword(Keyword::DEFAULT) {
3374            -1
3375        } else {
3376            let s = self.parse_number_value()?;
3377            if let Ok(n) = s.parse::<i32>() {
3378                n
3379            } else {
3380                return self.expected("number or DEFAULT");
3381            }
3382        };
3383        Ok(Some(rate_limit))
3384    }
3385
3386    /// SOURCE_RATE_LIMIT = default | NUMBER
3387    /// SOURCE_RATE_LIMIT TO default | NUMBER
3388    pub fn parse_alter_source_rate_limit(&mut self, is_table: bool) -> ModalResult<Option<i32>> {
3389        if !self.parse_word("SOURCE_RATE_LIMIT") {
3390            return Ok(None);
3391        }
3392        if self.expect_keyword(Keyword::TO).is_err() && self.expect_token(&Token::Eq).is_err() {
3393            let ddl = if is_table { "TABLE" } else { "SOURCE" };
3394            return self.expected(&format!("TO or = after ALTER {ddl} SET SOURCE_RATE_LIMIT"));
3395        }
3396        let rate_limit = if self.parse_keyword(Keyword::DEFAULT) {
3397            -1
3398        } else {
3399            let s = self.parse_number_value()?;
3400            if let Ok(n) = s.parse::<i32>() {
3401                n
3402            } else {
3403                return self.expected("number or DEFAULT");
3404            }
3405        };
3406        Ok(Some(rate_limit))
3407    }
3408
3409    pub fn parse_alter_index(&mut self) -> ModalResult<Statement> {
3410        let index_name = self.parse_object_name()?;
3411        let operation = if self.parse_keyword(Keyword::RENAME) {
3412            if self.parse_keyword(Keyword::TO) {
3413                let index_name = self.parse_object_name()?;
3414                AlterIndexOperation::RenameIndex { index_name }
3415            } else {
3416                return self.expected("TO after RENAME");
3417            }
3418        } else if self.parse_keyword(Keyword::SET) {
3419            if self.parse_keyword(Keyword::PARALLELISM) {
3420                if self.expect_keyword(Keyword::TO).is_err()
3421                    && self.expect_token(&Token::Eq).is_err()
3422                {
3423                    return self.expected("TO or = after ALTER TABLE SET PARALLELISM");
3424                }
3425
3426                let value = self.parse_set_variable()?;
3427
3428                let deferred = self.parse_keyword(Keyword::DEFERRED);
3429
3430                AlterIndexOperation::SetParallelism {
3431                    parallelism: value,
3432                    deferred,
3433                }
3434            } else {
3435                return self.expected("PARALLELISM after SET");
3436            }
3437        } else {
3438            return self.expected("RENAME after ALTER INDEX");
3439        };
3440
3441        Ok(Statement::AlterIndex {
3442            name: index_name,
3443            operation,
3444        })
3445    }
3446
3447    pub fn parse_alter_view(&mut self, materialized: bool) -> ModalResult<Statement> {
3448        let view_name = self.parse_object_name()?;
3449        let operation = if self.parse_keyword(Keyword::RENAME) {
3450            if self.parse_keyword(Keyword::TO) {
3451                let view_name = self.parse_object_name()?;
3452                AlterViewOperation::RenameView { view_name }
3453            } else {
3454                return self.expected("TO after RENAME");
3455            }
3456        } else if self.parse_keywords(&[Keyword::OWNER, Keyword::TO]) {
3457            let owner_name: Ident = self.parse_identifier()?;
3458            AlterViewOperation::ChangeOwner {
3459                new_owner_name: owner_name,
3460            }
3461        } else if self.parse_keywords(&[Keyword::SWAP, Keyword::WITH]) {
3462            let target_view = self.parse_object_name()?;
3463            AlterViewOperation::SwapRenameView { target_view }
3464        } else if self.parse_keyword(Keyword::SET) {
3465            if self.parse_keyword(Keyword::SCHEMA) {
3466                let schema_name = self.parse_object_name()?;
3467                AlterViewOperation::SetSchema {
3468                    new_schema_name: schema_name,
3469                }
3470            } else if self.parse_keyword(Keyword::PARALLELISM) && materialized {
3471                if self.expect_keyword(Keyword::TO).is_err()
3472                    && self.expect_token(&Token::Eq).is_err()
3473                {
3474                    return self.expected("TO or = after ALTER TABLE SET PARALLELISM");
3475                }
3476
3477                let value = self.parse_set_variable()?;
3478
3479                let deferred = self.parse_keyword(Keyword::DEFERRED);
3480
3481                AlterViewOperation::SetParallelism {
3482                    parallelism: value,
3483                    deferred,
3484                }
3485            } else if self.parse_keyword(Keyword::RESOURCE_GROUP) && materialized {
3486                if self.expect_keyword(Keyword::TO).is_err()
3487                    && self.expect_token(&Token::Eq).is_err()
3488                {
3489                    return self
3490                        .expected("TO or = after ALTER MATERIALIZED VIEW SET RESOURCE_GROUP");
3491                }
3492                let value = self.parse_set_variable()?;
3493                let deferred = self.parse_keyword(Keyword::DEFERRED);
3494
3495                AlterViewOperation::SetResourceGroup {
3496                    resource_group: Some(value),
3497                    deferred,
3498                }
3499            } else if materialized
3500                && let Some(rate_limit) = self.parse_alter_backfill_rate_limit()?
3501            {
3502                AlterViewOperation::SetBackfillRateLimit { rate_limit }
3503            } else {
3504                return self.expected("SCHEMA/PARALLELISM/BACKFILL_RATE_LIMIT after SET");
3505            }
3506        } else if self.parse_keyword(Keyword::RESET) {
3507            if self.parse_keyword(Keyword::RESOURCE_GROUP) && materialized {
3508                let deferred = self.parse_keyword(Keyword::DEFERRED);
3509
3510                AlterViewOperation::SetResourceGroup {
3511                    resource_group: None,
3512                    deferred,
3513                }
3514            } else {
3515                return self.expected("RESOURCE_GROUP after RESET");
3516            }
3517        } else {
3518            return self.expected(&format!(
3519                "RENAME or OWNER TO or SET or SWAP after ALTER {}VIEW",
3520                if materialized { "MATERIALIZED " } else { "" }
3521            ));
3522        };
3523
3524        Ok(Statement::AlterView {
3525            materialized,
3526            name: view_name,
3527            operation,
3528        })
3529    }
3530
3531    /// SINK_RATE_LIMIT = default | NUMBER
3532    /// SINK_RATE_LIMIT TO default | NUMBER
3533    pub fn parse_alter_sink_rate_limit(&mut self) -> ModalResult<Option<i32>> {
3534        if !self.parse_word("SINK_RATE_LIMIT") {
3535            return Ok(None);
3536        }
3537        if self.expect_keyword(Keyword::TO).is_err() && self.expect_token(&Token::Eq).is_err() {
3538            return self.expected("TO or = after ALTER SINK SET SINK_RATE_LIMIT");
3539        }
3540        let rate_limit = if self.parse_keyword(Keyword::DEFAULT) {
3541            -1
3542        } else {
3543            let s = self.parse_number_value()?;
3544            if let Ok(n) = s.parse::<i32>() {
3545                n
3546            } else {
3547                return self.expected("number or DEFAULT");
3548            }
3549        };
3550        Ok(Some(rate_limit))
3551    }
3552
3553    pub fn parse_alter_sink(&mut self) -> ModalResult<Statement> {
3554        let sink_name = self.parse_object_name()?;
3555        let operation = if self.parse_keyword(Keyword::RENAME) {
3556            if self.parse_keyword(Keyword::TO) {
3557                let sink_name = self.parse_object_name()?;
3558                AlterSinkOperation::RenameSink { sink_name }
3559            } else {
3560                return self.expected("TO after RENAME");
3561            }
3562        } else if self.parse_keywords(&[Keyword::OWNER, Keyword::TO]) {
3563            let owner_name: Ident = self.parse_identifier()?;
3564            AlterSinkOperation::ChangeOwner {
3565                new_owner_name: owner_name,
3566            }
3567        } else if self.parse_keyword(Keyword::SET) {
3568            if self.parse_keyword(Keyword::SCHEMA) {
3569                let schema_name = self.parse_object_name()?;
3570                AlterSinkOperation::SetSchema {
3571                    new_schema_name: schema_name,
3572                }
3573            } else if self.parse_keyword(Keyword::PARALLELISM) {
3574                if self.expect_keyword(Keyword::TO).is_err()
3575                    && self.expect_token(&Token::Eq).is_err()
3576                {
3577                    return self.expected("TO or = after ALTER TABLE SET PARALLELISM");
3578                }
3579
3580                let value = self.parse_set_variable()?;
3581                let deferred = self.parse_keyword(Keyword::DEFERRED);
3582
3583                AlterSinkOperation::SetParallelism {
3584                    parallelism: value,
3585                    deferred,
3586                }
3587            } else if let Some(rate_limit) = self.parse_alter_sink_rate_limit()? {
3588                AlterSinkOperation::SetSinkRateLimit { rate_limit }
3589            } else {
3590                return self.expected("SCHEMA/PARALLELISM after SET");
3591            }
3592        } else if self.parse_keywords(&[Keyword::SWAP, Keyword::WITH]) {
3593            let target_sink = self.parse_object_name()?;
3594            AlterSinkOperation::SwapRenameSink { target_sink }
3595        } else if self.parse_keyword(Keyword::CONNECTOR) {
3596            let changed_props = self.parse_with_properties()?;
3597            AlterSinkOperation::SetSinkProps { changed_props }
3598        } else {
3599            return self.expected("RENAME or OWNER TO or SET or CONNECTOR WITH after ALTER SINK");
3600        };
3601
3602        Ok(Statement::AlterSink {
3603            name: sink_name,
3604            operation,
3605        })
3606    }
3607
3608    pub fn parse_alter_subscription(&mut self) -> ModalResult<Statement> {
3609        let subscription_name = self.parse_object_name()?;
3610        let operation = if self.parse_keyword(Keyword::RENAME) {
3611            if self.parse_keyword(Keyword::TO) {
3612                let subscription_name = self.parse_object_name()?;
3613                AlterSubscriptionOperation::RenameSubscription { subscription_name }
3614            } else {
3615                return self.expected("TO after RENAME");
3616            }
3617        } else if self.parse_keywords(&[Keyword::OWNER, Keyword::TO]) {
3618            let owner_name: Ident = self.parse_identifier()?;
3619            AlterSubscriptionOperation::ChangeOwner {
3620                new_owner_name: owner_name,
3621            }
3622        } else if self.parse_keyword(Keyword::SET) {
3623            if self.parse_keyword(Keyword::SCHEMA) {
3624                let schema_name = self.parse_object_name()?;
3625                AlterSubscriptionOperation::SetSchema {
3626                    new_schema_name: schema_name,
3627                }
3628            } else {
3629                return self.expected("SCHEMA after SET");
3630            }
3631        } else if self.parse_keywords(&[Keyword::SWAP, Keyword::WITH]) {
3632            let target_subscription = self.parse_object_name()?;
3633            AlterSubscriptionOperation::SwapRenameSubscription {
3634                target_subscription,
3635            }
3636        } else {
3637            return self.expected("RENAME or OWNER TO or SET or SWAP after ALTER SUBSCRIPTION");
3638        };
3639
3640        Ok(Statement::AlterSubscription {
3641            name: subscription_name,
3642            operation,
3643        })
3644    }
3645
3646    pub fn parse_alter_source(&mut self) -> ModalResult<Statement> {
3647        let source_name = self.parse_object_name()?;
3648        let operation = if self.parse_keyword(Keyword::RENAME) {
3649            if self.parse_keyword(Keyword::TO) {
3650                let source_name = self.parse_object_name()?;
3651                AlterSourceOperation::RenameSource { source_name }
3652            } else {
3653                return self.expected("TO after RENAME");
3654            }
3655        } else if self.parse_keyword(Keyword::ADD) {
3656            let _ = self.parse_keyword(Keyword::COLUMN);
3657            let _if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
3658            let column_def = self.parse_column_def()?;
3659            AlterSourceOperation::AddColumn { column_def }
3660        } else if self.parse_keywords(&[Keyword::OWNER, Keyword::TO]) {
3661            let owner_name: Ident = self.parse_identifier()?;
3662            AlterSourceOperation::ChangeOwner {
3663                new_owner_name: owner_name,
3664            }
3665        } else if self.parse_keyword(Keyword::SET) {
3666            if self.parse_keyword(Keyword::SCHEMA) {
3667                let schema_name = self.parse_object_name()?;
3668                AlterSourceOperation::SetSchema {
3669                    new_schema_name: schema_name,
3670                }
3671            } else if let Some(rate_limit) = self.parse_alter_source_rate_limit(false)? {
3672                AlterSourceOperation::SetSourceRateLimit { rate_limit }
3673            } else if self.parse_keyword(Keyword::PARALLELISM) {
3674                if self.expect_keyword(Keyword::TO).is_err()
3675                    && self.expect_token(&Token::Eq).is_err()
3676                {
3677                    return self.expected("TO or = after ALTER SOURCE SET PARALLELISM");
3678                }
3679
3680                let value = self.parse_set_variable()?;
3681                let deferred = self.parse_keyword(Keyword::DEFERRED);
3682
3683                AlterSourceOperation::SetParallelism {
3684                    parallelism: value,
3685                    deferred,
3686                }
3687            } else {
3688                return self.expected("SCHEMA, SOURCE_RATE_LIMIT or PARALLELISM after SET");
3689            }
3690        } else if self.peek_nth_any_of_keywords(0, &[Keyword::FORMAT]) {
3691            let format_encode = self.parse_schema()?.unwrap();
3692            if format_encode.key_encode.is_some() {
3693                parser_err!("key encode clause is not supported in source schema");
3694            }
3695            AlterSourceOperation::FormatEncode { format_encode }
3696        } else if self.parse_keywords(&[Keyword::REFRESH, Keyword::SCHEMA]) {
3697            AlterSourceOperation::RefreshSchema
3698        } else if self.parse_keywords(&[Keyword::SWAP, Keyword::WITH]) {
3699            let target_source = self.parse_object_name()?;
3700            AlterSourceOperation::SwapRenameSource { target_source }
3701        } else {
3702            return self.expected("RENAME, ADD COLUMN, OWNER TO or SET after ALTER SOURCE");
3703        };
3704
3705        Ok(Statement::AlterSource {
3706            name: source_name,
3707            operation,
3708        })
3709    }
3710
3711    pub fn parse_alter_function(&mut self) -> ModalResult<Statement> {
3712        let FunctionDesc { name, args } = self.parse_function_desc()?;
3713
3714        let operation = if self.parse_keyword(Keyword::SET) {
3715            if self.parse_keyword(Keyword::SCHEMA) {
3716                let schema_name = self.parse_object_name()?;
3717                AlterFunctionOperation::SetSchema {
3718                    new_schema_name: schema_name,
3719                }
3720            } else {
3721                return self.expected("SCHEMA after SET");
3722            }
3723        } else {
3724            return self.expected("SET after ALTER FUNCTION");
3725        };
3726
3727        Ok(Statement::AlterFunction {
3728            name,
3729            args,
3730            operation,
3731        })
3732    }
3733
3734    pub fn parse_alter_connection(&mut self) -> ModalResult<Statement> {
3735        let connection_name = self.parse_object_name()?;
3736        let operation = if self.parse_keyword(Keyword::SET) {
3737            if self.parse_keyword(Keyword::SCHEMA) {
3738                let schema_name = self.parse_object_name()?;
3739                AlterConnectionOperation::SetSchema {
3740                    new_schema_name: schema_name,
3741                }
3742            } else {
3743                return self.expected("SCHEMA after SET");
3744            }
3745        } else if self.parse_keywords(&[Keyword::OWNER, Keyword::TO]) {
3746            let owner_name: Ident = self.parse_identifier()?;
3747            AlterConnectionOperation::ChangeOwner {
3748                new_owner_name: owner_name,
3749            }
3750        } else {
3751            return self.expected("SET, or OWNER TO after ALTER CONNECTION");
3752        };
3753
3754        Ok(Statement::AlterConnection {
3755            name: connection_name,
3756            operation,
3757        })
3758    }
3759
3760    pub fn parse_alter_system(&mut self) -> ModalResult<Statement> {
3761        self.expect_keyword(Keyword::SET)?;
3762        let param = self.parse_identifier()?;
3763        if self.expect_keyword(Keyword::TO).is_err() && self.expect_token(&Token::Eq).is_err() {
3764            return self.expected("TO or = after ALTER SYSTEM SET");
3765        }
3766        let value = self.parse_set_variable()?;
3767        Ok(Statement::AlterSystem { param, value })
3768    }
3769
3770    pub fn parse_alter_secret(&mut self) -> ModalResult<Statement> {
3771        let secret_name = self.parse_object_name()?;
3772        let with_options = self.parse_with_properties()?;
3773        self.expect_keyword(Keyword::AS)?;
3774        let new_credential = self.ensure_parse_value()?;
3775        let operation = AlterSecretOperation::ChangeCredential { new_credential };
3776        Ok(Statement::AlterSecret {
3777            name: secret_name,
3778            with_options,
3779            operation,
3780        })
3781    }
3782
3783    pub fn parse_alter_fragment(&mut self) -> ModalResult<Statement> {
3784        let fragment_id = self.parse_literal_uint()? as u32;
3785        if !self.parse_keyword(Keyword::SET) {
3786            return self.expected("SET after ALTER FRAGMENT");
3787        }
3788        let rate_limit = self.parse_alter_fragment_rate_limit()?;
3789        let operation = AlterFragmentOperation::AlterBackfillRateLimit { rate_limit };
3790        Ok(Statement::AlterFragment {
3791            fragment_id,
3792            operation,
3793        })
3794    }
3795
3796    fn parse_alter_fragment_rate_limit(&mut self) -> ModalResult<i32> {
3797        if !self.parse_word("RATE_LIMIT") {
3798            return self.expected("expected RATE_LIMIT after SET");
3799        }
3800        if self.expect_keyword(Keyword::TO).is_err() && self.expect_token(&Token::Eq).is_err() {
3801            return self.expected("TO or = after RATE_LIMIT");
3802        }
3803        let rate_limit = if self.parse_keyword(Keyword::DEFAULT) {
3804            -1
3805        } else {
3806            let s = self.parse_number_value()?;
3807            if let Ok(n) = s.parse::<i32>() {
3808                n
3809            } else {
3810                return self.expected("number or DEFAULT");
3811            }
3812        };
3813        Ok(rate_limit)
3814    }
3815
3816    /// Parse a copy statement
3817    pub fn parse_copy(&mut self) -> ModalResult<Statement> {
3818        let table_name = self.parse_object_name()?;
3819        let columns = self.parse_parenthesized_column_list(Optional)?;
3820        self.expect_keywords(&[Keyword::FROM, Keyword::STDIN])?;
3821        self.expect_token(&Token::SemiColon)?;
3822        let values = self.parse_tsv();
3823        Ok(Statement::Copy {
3824            table_name,
3825            columns,
3826            values,
3827        })
3828    }
3829
3830    /// Parse a tab separated values in
3831    /// COPY payload
3832    fn parse_tsv(&mut self) -> Vec<Option<String>> {
3833        self.parse_tab_value()
3834    }
3835
3836    fn parse_tab_value(&mut self) -> Vec<Option<String>> {
3837        let mut values = vec![];
3838        let mut content = String::from("");
3839        while let Some(t) = self.next_token_no_skip() {
3840            match t.token {
3841                Token::Whitespace(Whitespace::Tab) => {
3842                    values.push(Some(content.clone()));
3843                    content.clear();
3844                }
3845                Token::Whitespace(Whitespace::Newline) => {
3846                    values.push(Some(content.clone()));
3847                    content.clear();
3848                }
3849                Token::Backslash => {
3850                    if self.consume_token(&Token::Period) {
3851                        return values;
3852                    }
3853                    if let Token::Word(w) = self.next_token().token {
3854                        if w.value == "N" {
3855                            values.push(None);
3856                        }
3857                    }
3858                }
3859                _ => {
3860                    content.push_str(&t.to_string());
3861                }
3862            }
3863        }
3864        values
3865    }
3866
3867    pub fn ensure_parse_value(&mut self) -> ModalResult<Value> {
3868        match self.parse_value_and_obj_ref::<true>()? {
3869            SqlOptionValue::Value(value) => Ok(value),
3870            SqlOptionValue::SecretRef(_) | SqlOptionValue::ConnectionRef(_) => unreachable!(),
3871        }
3872    }
3873
3874    /// Parse a literal value (numbers, strings, date/time, booleans)
3875    pub fn parse_value_and_obj_ref<const FORBID_OBJ_REF: bool>(
3876        &mut self,
3877    ) -> ModalResult<SqlOptionValue> {
3878        let checkpoint = *self;
3879        let token = self.next_token();
3880        match token.token {
3881            Token::Word(w) => match w.keyword {
3882                Keyword::TRUE => Ok(Value::Boolean(true).into()),
3883                Keyword::FALSE => Ok(Value::Boolean(false).into()),
3884                Keyword::NULL => Ok(Value::Null.into()),
3885                Keyword::NoKeyword if w.quote_style.is_some() => match w.quote_style {
3886                    Some('"') => Ok(Value::DoubleQuotedString(w.value).into()),
3887                    Some('\'') => Ok(Value::SingleQuotedString(w.value).into()),
3888                    _ => self.expected_at(checkpoint, "A value")?,
3889                },
3890                Keyword::SECRET => {
3891                    if FORBID_OBJ_REF {
3892                        return self.expected_at(
3893                            checkpoint,
3894                            "a concrete value rather than a secret reference",
3895                        );
3896                    }
3897                    let secret = self.parse_secret_ref()?;
3898                    Ok(SqlOptionValue::SecretRef(secret))
3899                }
3900                _ => self.expected_at(checkpoint, "a concrete value"),
3901            },
3902            Token::Number(ref n) => Ok(Value::Number(n.clone()).into()),
3903            Token::SingleQuotedString(ref s) => Ok(Value::SingleQuotedString(s.to_string()).into()),
3904            Token::DollarQuotedString(ref s) => Ok(Value::DollarQuotedString(s.clone()).into()),
3905            Token::CstyleEscapesString(ref s) => Ok(Value::CstyleEscapedString(s.clone()).into()),
3906            Token::NationalStringLiteral(ref s) => {
3907                Ok(Value::NationalStringLiteral(s.to_string()).into())
3908            }
3909            Token::HexStringLiteral(ref s) => Ok(Value::HexStringLiteral(s.to_string()).into()),
3910            _ => self.expected_at(checkpoint, "a value"),
3911        }
3912    }
3913
3914    fn parse_secret_ref(&mut self) -> ModalResult<SecretRefValue> {
3915        let secret_name = self.parse_object_name()?;
3916        let ref_as = if self.parse_keywords(&[Keyword::AS, Keyword::FILE]) {
3917            SecretRefAsType::File
3918        } else {
3919            SecretRefAsType::Text
3920        };
3921        Ok(SecretRefValue {
3922            secret_name,
3923            ref_as,
3924        })
3925    }
3926
3927    fn parse_set_variable(&mut self) -> ModalResult<SetVariableValue> {
3928        alt((
3929            Keyword::DEFAULT.value(SetVariableValue::Default),
3930            separated(
3931                1..,
3932                alt((
3933                    Self::ensure_parse_value.map(SetVariableValueSingle::Literal),
3934                    |parser: &mut Self| {
3935                        let checkpoint = *parser;
3936                        let ident = parser.parse_identifier()?;
3937                        if ident.value == "default" {
3938                            *parser = checkpoint;
3939                            return parser.expected("parameter list value").map_err(|e| e.cut());
3940                        }
3941                        Ok(SetVariableValueSingle::Ident(ident))
3942                    },
3943                    fail.expect("parameter value"),
3944                )),
3945                Token::Comma,
3946            )
3947            .map(|list: Vec<SetVariableValueSingle>| {
3948                if list.len() == 1 {
3949                    SetVariableValue::Single(list[0].clone())
3950                } else {
3951                    SetVariableValue::List(list)
3952                }
3953            }),
3954        ))
3955        .parse_next(self)
3956    }
3957
3958    pub fn parse_number_value(&mut self) -> ModalResult<String> {
3959        let checkpoint = *self;
3960        match self.ensure_parse_value()? {
3961            Value::Number(v) => Ok(v),
3962            _ => self.expected_at(checkpoint, "literal number"),
3963        }
3964    }
3965
3966    /// Parse an unsigned literal integer/long
3967    pub fn parse_literal_uint(&mut self) -> ModalResult<u64> {
3968        literal_uint(self)
3969    }
3970
3971    pub fn parse_function_definition(&mut self) -> ModalResult<FunctionDefinition> {
3972        alt((
3973            single_quoted_string.map(FunctionDefinition::SingleQuotedDef),
3974            dollar_quoted_string.map(FunctionDefinition::DoubleDollarDef),
3975            Self::parse_identifier.map(|i| FunctionDefinition::Identifier(i.value)),
3976            fail.expect("function definition"),
3977        ))
3978        .parse_next(self)
3979    }
3980
3981    /// Parse a literal string
3982    pub fn parse_literal_string(&mut self) -> ModalResult<String> {
3983        let checkpoint = *self;
3984        let token = self.next_token();
3985        match token.token {
3986            Token::SingleQuotedString(s) => Ok(s),
3987            _ => self.expected_at(checkpoint, "literal string"),
3988        }
3989    }
3990
3991    /// Parse a map key string
3992    pub fn parse_map_key(&mut self) -> ModalResult<Expr> {
3993        alt((
3994            Self::parse_function,
3995            single_quoted_string.map(|s| Expr::Value(Value::SingleQuotedString(s))),
3996            token_number.map(|s| Expr::Value(Value::Number(s))),
3997            fail.expect("literal string, number or function"),
3998        ))
3999        .parse_next(self)
4000    }
4001
4002    /// Parse a SQL datatype (in the context of a CREATE TABLE statement for example)
4003    pub fn parse_data_type(&mut self) -> ModalResult<DataType> {
4004        parser_v2::data_type(self)
4005    }
4006
4007    /// Parse `AS identifier` (or simply `identifier` if it's not a reserved keyword)
4008    /// Some examples with aliases: `SELECT 1 foo`, `SELECT COUNT(*) AS cnt`,
4009    /// `SELECT ... FROM t1 foo, t2 bar`, `SELECT ... FROM (...) AS bar`
4010    pub fn parse_optional_alias(
4011        &mut self,
4012        reserved_kwds: &[Keyword],
4013    ) -> ModalResult<Option<Ident>> {
4014        let after_as = self.parse_keyword(Keyword::AS);
4015        let checkpoint = *self;
4016        let token = self.next_token();
4017        match token.token {
4018            // Accept any identifier after `AS` (though many dialects have restrictions on
4019            // keywords that may appear here). If there's no `AS`: don't parse keywords,
4020            // which may start a construct allowed in this position, to be parsed as aliases.
4021            // (For example, in `FROM t1 JOIN` the `JOIN` will always be parsed as a keyword,
4022            // not an alias.)
4023            Token::Word(w) if after_as || (!reserved_kwds.contains(&w.keyword)) => {
4024                Ok(Some(w.to_ident()?))
4025            }
4026            _ => {
4027                *self = checkpoint;
4028                if after_as {
4029                    return self.expected("an identifier after AS");
4030                }
4031                Ok(None) // no alias found
4032            }
4033        }
4034    }
4035
4036    /// Parse `AS identifier` when the AS is describing a table-valued object,
4037    /// like in `... FROM generate_series(1, 10) AS t (col)`. In this case
4038    /// the alias is allowed to optionally name the columns in the table, in
4039    /// addition to the table itself.
4040    pub fn parse_optional_table_alias(
4041        &mut self,
4042        reserved_kwds: &[Keyword],
4043    ) -> ModalResult<Option<TableAlias>> {
4044        match self.parse_optional_alias(reserved_kwds)? {
4045            Some(name) => {
4046                let columns = self.parse_parenthesized_column_list(Optional)?;
4047                Ok(Some(TableAlias { name, columns }))
4048            }
4049            None => Ok(None),
4050        }
4051    }
4052
4053    /// syntax `FOR SYSTEM_TIME AS OF PROCTIME()` is used for temporal join.
4054    pub fn parse_as_of(&mut self) -> ModalResult<AsOf> {
4055        Keyword::FOR.parse_next(self)?;
4056        alt((
4057            preceded(
4058                (Keyword::SYSTEM_TIME, Keyword::AS, Keyword::OF),
4059                cut_err(
4060                    alt((
4061                        preceded(
4062                            (
4063                                Self::parse_identifier.verify(|ident| ident.real_value() == "now"),
4064                                cut_err(Token::LParen),
4065                                cut_err(Token::RParen),
4066                                Token::Minus,
4067                            ),
4068                            Self::parse_literal_interval.try_map(|e| match e {
4069                                Expr::Value(v) => match v {
4070                                    Value::Interval {
4071                                        value,
4072                                        leading_field,
4073                                        ..
4074                                    } => {
4075                                        let Some(leading_field) = leading_field else {
4076                                            return Err(StrError("expect duration unit".into()));
4077                                        };
4078                                        Ok(AsOf::ProcessTimeWithInterval((value, leading_field)))
4079                                    }
4080                                    _ => Err(StrError("expect Value::Interval".into())),
4081                                },
4082                                _ => Err(StrError("expect Expr::Value".into())),
4083                            }),
4084                        ),
4085                        (
4086                            Self::parse_identifier.verify(|ident| ident.real_value() == "now"),
4087                            cut_err(Token::LParen),
4088                            cut_err(Token::RParen),
4089                        )
4090                            .value(AsOf::ProcessTimeWithInterval((
4091                                "0".to_owned(),
4092                                DateTimeField::Second,
4093                            ))),
4094                        (
4095                            Self::parse_identifier.verify(|ident| ident.real_value() == "proctime"),
4096                            cut_err(Token::LParen),
4097                            cut_err(Token::RParen),
4098                        )
4099                            .value(AsOf::ProcessTime),
4100                        literal_i64.map(AsOf::TimestampNum),
4101                        single_quoted_string.map(AsOf::TimestampString),
4102                    ))
4103                    .expect("proctime(), now(), number or string"),
4104                ),
4105            ),
4106            preceded(
4107                (Keyword::SYSTEM_VERSION, Keyword::AS, Keyword::OF),
4108                cut_err(
4109                    alt((
4110                        literal_i64.map(AsOf::VersionNum),
4111                        single_quoted_string.map(AsOf::VersionString),
4112                    ))
4113                    .expect("number or string"),
4114                ),
4115            ),
4116        ))
4117        .parse_next(self)
4118    }
4119
4120    /// Parse a possibly qualified, possibly quoted identifier, e.g.
4121    /// `foo` or `myschema."table"
4122    pub fn parse_object_name(&mut self) -> ModalResult<ObjectName> {
4123        let mut idents = vec![];
4124        loop {
4125            idents.push(self.parse_identifier()?);
4126            if !self.consume_token(&Token::Period) {
4127                break;
4128            }
4129        }
4130        Ok(ObjectName(idents))
4131    }
4132
4133    /// Parse identifiers strictly i.e. don't parse keywords
4134    pub fn parse_identifiers_non_keywords(&mut self) -> ModalResult<Vec<Ident>> {
4135        let mut idents = vec![];
4136        loop {
4137            match self.peek_token().token {
4138                Token::Word(w) => {
4139                    if w.keyword != Keyword::NoKeyword {
4140                        break;
4141                    }
4142
4143                    idents.push(w.to_ident()?);
4144                }
4145                Token::EOF | Token::Eq => break,
4146                _ => {}
4147            }
4148
4149            self.next_token();
4150        }
4151
4152        Ok(idents)
4153    }
4154
4155    /// Parse identifiers
4156    pub fn parse_identifiers(&mut self) -> ModalResult<Vec<Ident>> {
4157        let mut idents = vec![];
4158        loop {
4159            let token = self.next_token();
4160            match token.token {
4161                Token::Word(w) => {
4162                    idents.push(w.to_ident()?);
4163                }
4164                Token::EOF => break,
4165                _ => {}
4166            }
4167        }
4168
4169        Ok(idents)
4170    }
4171
4172    /// Parse a simple one-word identifier (possibly quoted, possibly a keyword)
4173    pub fn parse_identifier(&mut self) -> ModalResult<Ident> {
4174        let checkpoint = *self;
4175        let token = self.next_token();
4176        match token.token {
4177            Token::Word(w) => Ok(w.to_ident()?),
4178            _ => self.expected_at(checkpoint, "identifier"),
4179        }
4180    }
4181
4182    /// Parse a simple one-word identifier (possibly quoted, possibly a non-reserved keyword)
4183    pub fn parse_identifier_non_reserved(&mut self) -> ModalResult<Ident> {
4184        let checkpoint = *self;
4185        let token = self.next_token();
4186        match token.token {
4187            Token::Word(w) => {
4188                match keywords::RESERVED_FOR_COLUMN_OR_TABLE_NAME.contains(&w.keyword) {
4189                    true => parser_err!("syntax error at or near {w}"),
4190                    false => Ok(w.to_ident()?),
4191                }
4192            }
4193            _ => self.expected_at(checkpoint, "identifier"),
4194        }
4195    }
4196
4197    /// Parse a parenthesized comma-separated list of unqualified, possibly quoted identifiers
4198    pub fn parse_parenthesized_column_list(
4199        &mut self,
4200        optional: IsOptional,
4201    ) -> ModalResult<Vec<Ident>> {
4202        if self.consume_token(&Token::LParen) {
4203            let cols = self.parse_comma_separated(Parser::parse_identifier_non_reserved)?;
4204            self.expect_token(&Token::RParen)?;
4205            Ok(cols)
4206        } else if optional == Optional {
4207            Ok(vec![])
4208        } else {
4209            self.expected("a list of columns in parentheses")
4210        }
4211    }
4212
4213    pub fn parse_returning(&mut self, optional: IsOptional) -> ModalResult<Vec<SelectItem>> {
4214        if self.parse_keyword(Keyword::RETURNING) {
4215            let cols = self.parse_comma_separated(Parser::parse_select_item)?;
4216            Ok(cols)
4217        } else if optional == Optional {
4218            Ok(vec![])
4219        } else {
4220            self.expected("a list of columns or * after returning")
4221        }
4222    }
4223
4224    pub fn parse_row_expr(&mut self) -> ModalResult<Expr> {
4225        Ok(Expr::Row(self.parse_token_wrapped_exprs(
4226            &Token::LParen,
4227            &Token::RParen,
4228        )?))
4229    }
4230
4231    /// Parse a comma-separated list (maybe empty) from a wrapped expression
4232    pub fn parse_token_wrapped_exprs(
4233        &mut self,
4234        left: &Token,
4235        right: &Token,
4236    ) -> ModalResult<Vec<Expr>> {
4237        if self.consume_token(left) {
4238            let exprs = if self.consume_token(right) {
4239                vec![]
4240            } else {
4241                let exprs = self.parse_comma_separated(Parser::parse_expr)?;
4242                self.expect_token(right)?;
4243                exprs
4244            };
4245            Ok(exprs)
4246        } else {
4247            self.expected(left.to_string().as_str())
4248        }
4249    }
4250
4251    pub fn parse_optional_precision(&mut self) -> ModalResult<Option<u64>> {
4252        if self.consume_token(&Token::LParen) {
4253            let n = self.parse_literal_uint()?;
4254            self.expect_token(&Token::RParen)?;
4255            Ok(Some(n))
4256        } else {
4257            Ok(None)
4258        }
4259    }
4260
4261    pub fn parse_optional_precision_scale(&mut self) -> ModalResult<(Option<u64>, Option<u64>)> {
4262        if self.consume_token(&Token::LParen) {
4263            let n = self.parse_literal_uint()?;
4264            let scale = if self.consume_token(&Token::Comma) {
4265                Some(self.parse_literal_uint()?)
4266            } else {
4267                None
4268            };
4269            self.expect_token(&Token::RParen)?;
4270            Ok((Some(n), scale))
4271        } else {
4272            Ok((None, None))
4273        }
4274    }
4275
4276    pub fn parse_delete(&mut self) -> ModalResult<Statement> {
4277        self.expect_keyword(Keyword::FROM)?;
4278        let table_name = self.parse_object_name()?;
4279        let selection = if self.parse_keyword(Keyword::WHERE) {
4280            Some(self.parse_expr()?)
4281        } else {
4282            None
4283        };
4284        let returning = self.parse_returning(Optional)?;
4285
4286        Ok(Statement::Delete {
4287            table_name,
4288            selection,
4289            returning,
4290        })
4291    }
4292
4293    pub fn parse_optional_boolean(&mut self, default: bool) -> bool {
4294        if let Some(keyword) = self.parse_one_of_keywords(&[Keyword::TRUE, Keyword::FALSE]) {
4295            match keyword {
4296                Keyword::TRUE => true,
4297                Keyword::FALSE => false,
4298                _ => unreachable!(),
4299            }
4300        } else {
4301            default
4302        }
4303    }
4304
4305    fn parse_explain_options(&mut self) -> ModalResult<(ExplainOptions, Option<u64>)> {
4306        let mut options = ExplainOptions::default();
4307        let mut analyze_duration = None;
4308
4309        let explain_key_words = [
4310            Keyword::VERBOSE,
4311            Keyword::TRACE,
4312            Keyword::TYPE,
4313            Keyword::LOGICAL,
4314            Keyword::PHYSICAL,
4315            Keyword::DISTSQL,
4316            Keyword::FORMAT,
4317            Keyword::DURATION_SECS,
4318        ];
4319
4320        let parse_explain_option = |parser: &mut Parser<'_>| -> ModalResult<()> {
4321            let keyword = parser.expect_one_of_keywords(&explain_key_words)?;
4322            match keyword {
4323                Keyword::VERBOSE => options.verbose = parser.parse_optional_boolean(true),
4324                Keyword::TRACE => options.trace = parser.parse_optional_boolean(true),
4325                Keyword::TYPE => {
4326                    let explain_type = parser.expect_one_of_keywords(&[
4327                        Keyword::LOGICAL,
4328                        Keyword::PHYSICAL,
4329                        Keyword::DISTSQL,
4330                    ])?;
4331                    match explain_type {
4332                        Keyword::LOGICAL => options.explain_type = ExplainType::Logical,
4333                        Keyword::PHYSICAL => options.explain_type = ExplainType::Physical,
4334                        Keyword::DISTSQL => options.explain_type = ExplainType::DistSql,
4335                        _ => unreachable!("{}", keyword),
4336                    }
4337                }
4338                Keyword::LOGICAL => options.explain_type = ExplainType::Logical,
4339                Keyword::PHYSICAL => options.explain_type = ExplainType::Physical,
4340                Keyword::DISTSQL => options.explain_type = ExplainType::DistSql,
4341                Keyword::FORMAT => {
4342                    options.explain_format = {
4343                        match parser.expect_one_of_keywords(&[
4344                            Keyword::TEXT,
4345                            Keyword::JSON,
4346                            Keyword::XML,
4347                            Keyword::YAML,
4348                            Keyword::DOT,
4349                        ])? {
4350                            Keyword::TEXT => ExplainFormat::Text,
4351                            Keyword::JSON => ExplainFormat::Json,
4352                            Keyword::XML => ExplainFormat::Xml,
4353                            Keyword::YAML => ExplainFormat::Yaml,
4354                            Keyword::DOT => ExplainFormat::Dot,
4355                            _ => unreachable!("{}", keyword),
4356                        }
4357                    }
4358                }
4359                Keyword::DURATION_SECS => {
4360                    analyze_duration = Some(parser.parse_literal_uint()?);
4361                }
4362                _ => unreachable!("{}", keyword),
4363            };
4364            Ok(())
4365        };
4366
4367        // In order to support following statement, we need to peek before consume.
4368        // explain (select 1) union (select 1)
4369        if self.peek_token() == Token::LParen
4370            && self.peek_nth_any_of_keywords(1, &explain_key_words)
4371            && self.consume_token(&Token::LParen)
4372        {
4373            self.parse_comma_separated(parse_explain_option)?;
4374            self.expect_token(&Token::RParen)?;
4375        }
4376
4377        Ok((options, analyze_duration))
4378    }
4379
4380    pub fn parse_explain(&mut self) -> ModalResult<Statement> {
4381        let analyze = self.parse_keyword(Keyword::ANALYZE);
4382        let (options, analyze_duration) = self.parse_explain_options()?;
4383
4384        if analyze {
4385            fn parse_analyze_target(parser: &mut Parser<'_>) -> ModalResult<Option<AnalyzeTarget>> {
4386                if parser.parse_keyword(Keyword::TABLE) {
4387                    let table_name = parser.parse_object_name()?;
4388                    Ok(Some(AnalyzeTarget::Table(table_name)))
4389                } else if parser.parse_keyword(Keyword::INDEX) {
4390                    let index_name = parser.parse_object_name()?;
4391                    Ok(Some(AnalyzeTarget::Index(index_name)))
4392                } else if parser.parse_keywords(&[Keyword::MATERIALIZED, Keyword::VIEW]) {
4393                    let view_name = parser.parse_object_name()?;
4394                    Ok(Some(AnalyzeTarget::MaterializedView(view_name)))
4395                } else if parser.parse_keyword(Keyword::INDEX) {
4396                    let index_name = parser.parse_object_name()?;
4397                    Ok(Some(AnalyzeTarget::Index(index_name)))
4398                } else if parser.parse_keyword(Keyword::SINK) {
4399                    let sink_name = parser.parse_object_name()?;
4400                    Ok(Some(AnalyzeTarget::Sink(sink_name)))
4401                } else if parser.parse_word("ID") {
4402                    let job_id = parser.parse_literal_uint()? as u32;
4403                    Ok(Some(AnalyzeTarget::Id(job_id)))
4404                } else {
4405                    Ok(None)
4406                }
4407            }
4408            if let Some(target) = parse_analyze_target(self)? {
4409                let statement = Statement::ExplainAnalyzeStreamJob {
4410                    target,
4411                    duration_secs: analyze_duration,
4412                };
4413                return Ok(statement);
4414            }
4415        }
4416
4417        let statement = match self.parse_statement() {
4418            Ok(statement) => statement,
4419            error @ Err(_) => {
4420                return if analyze {
4421                    self.expected_at(
4422                        *self,
4423                        "SINK, TABLE, MATERIALIZED VIEW, INDEX or a statement after ANALYZE",
4424                    )
4425                } else {
4426                    error
4427                };
4428            }
4429        };
4430        Ok(Statement::Explain {
4431            analyze,
4432            statement: Box::new(statement),
4433            options,
4434        })
4435    }
4436
4437    pub fn parse_describe(&mut self) -> ModalResult<Statement> {
4438        let kind = match self.parse_one_of_keywords(&[Keyword::FRAGMENTS]) {
4439            Some(Keyword::FRAGMENTS) => DescribeKind::Fragments,
4440            None => DescribeKind::Plain,
4441            Some(_) => unreachable!(),
4442        };
4443        let name = self.parse_object_name()?;
4444        Ok(Statement::Describe { name, kind })
4445    }
4446
4447    /// Parse a query expression, i.e. a `SELECT` statement optionally
4448    /// preceded with some `WITH` CTE declarations and optionally followed
4449    /// by `ORDER BY`. Unlike some other parse_... methods, this one doesn't
4450    /// expect the initial keyword to be already consumed
4451    pub fn parse_query(&mut self) -> ModalResult<Query> {
4452        let with = if self.parse_keyword(Keyword::WITH) {
4453            Some(With {
4454                recursive: self.parse_keyword(Keyword::RECURSIVE),
4455                cte_tables: self.parse_comma_separated(Parser::parse_cte)?,
4456            })
4457        } else {
4458            None
4459        };
4460
4461        let body = self.parse_query_body(0)?;
4462
4463        let order_by = if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
4464            self.parse_comma_separated(Parser::parse_order_by_expr)?
4465        } else {
4466            vec![]
4467        };
4468
4469        let mut limit = None;
4470        let mut offset = None;
4471        for _x in 0..2 {
4472            if limit.is_none() && self.parse_keyword(Keyword::LIMIT) {
4473                limit = self.parse_limit()?
4474            }
4475
4476            if offset.is_none() && self.parse_keyword(Keyword::OFFSET) {
4477                offset = Some(self.parse_offset()?)
4478            }
4479        }
4480
4481        let fetch = if self.parse_keyword(Keyword::FETCH) {
4482            if limit.is_some() {
4483                parser_err!("Cannot specify both LIMIT and FETCH");
4484            }
4485            let fetch = self.parse_fetch()?;
4486            if fetch.with_ties && order_by.is_empty() {
4487                parser_err!("WITH TIES cannot be specified without ORDER BY clause");
4488            }
4489            Some(fetch)
4490        } else {
4491            None
4492        };
4493
4494        Ok(Query {
4495            with,
4496            body,
4497            order_by,
4498            limit,
4499            offset,
4500            fetch,
4501        })
4502    }
4503
4504    /// Parse a CTE (`alias [( col1, col2, ... )] AS (subquery)`)
4505    fn parse_cte(&mut self) -> ModalResult<Cte> {
4506        let name = self.parse_identifier_non_reserved()?;
4507        let cte = if self.parse_keyword(Keyword::AS) {
4508            let cte_inner = self.parse_cte_inner()?;
4509            let alias = TableAlias {
4510                name,
4511                columns: vec![],
4512            };
4513            Cte { alias, cte_inner }
4514        } else {
4515            let columns = self.parse_parenthesized_column_list(Optional)?;
4516            self.expect_keyword(Keyword::AS)?;
4517            let cte_inner = self.parse_cte_inner()?;
4518            let alias = TableAlias { name, columns };
4519            Cte { alias, cte_inner }
4520        };
4521        Ok(cte)
4522    }
4523
4524    fn parse_cte_inner(&mut self) -> ModalResult<CteInner> {
4525        match self.expect_token(&Token::LParen) {
4526            Ok(()) => {
4527                let query = self.parse_query()?;
4528                self.expect_token(&Token::RParen)?;
4529                Ok(CteInner::Query(Box::new(query)))
4530            }
4531            _ => {
4532                let changelog = self.parse_identifier_non_reserved()?;
4533                if changelog.to_string().to_lowercase() != "changelog" {
4534                    parser_err!("Expected 'changelog' but found '{}'", changelog);
4535                }
4536                self.expect_keyword(Keyword::FROM)?;
4537                Ok(CteInner::ChangeLog(self.parse_object_name()?))
4538            }
4539        }
4540    }
4541
4542    /// Parse a "query body", which is an expression with roughly the
4543    /// following grammar:
4544    /// ```text
4545    ///   query_body ::= restricted_select | '(' subquery ')' | set_operation
4546    ///   restricted_select ::= 'SELECT' [expr_list] [ from ] [ where ] [ groupby_having ]
4547    ///   subquery ::= query_body [ order_by_limit ]
4548    ///   set_operation ::= query_body { 'UNION' | 'EXCEPT' | 'INTERSECT' } [ 'ALL' ] query_body
4549    /// ```
4550    fn parse_query_body(&mut self, precedence: u8) -> ModalResult<SetExpr> {
4551        // We parse the expression using a Pratt parser, as in `parse_expr()`.
4552        // Start by parsing a restricted SELECT or a `(subquery)`:
4553        let mut expr = if self.parse_keyword(Keyword::SELECT) {
4554            SetExpr::Select(Box::new(self.parse_select()?))
4555        } else if self.consume_token(&Token::LParen) {
4556            // CTEs are not allowed here, but the parser currently accepts them
4557            let subquery = self.parse_query()?;
4558            self.expect_token(&Token::RParen)?;
4559            SetExpr::Query(Box::new(subquery))
4560        } else if self.parse_keyword(Keyword::VALUES) {
4561            SetExpr::Values(self.parse_values()?)
4562        } else {
4563            return self.expected("SELECT, VALUES, or a subquery in the query body");
4564        };
4565
4566        loop {
4567            // The query can be optionally followed by a set operator:
4568            let op = self.parse_set_operator(&self.peek_token().token);
4569            let next_precedence = match op {
4570                // UNION and EXCEPT have the same binding power and evaluate left-to-right
4571                Some(SetOperator::Union) | Some(SetOperator::Except) => 10,
4572                // INTERSECT has higher precedence than UNION/EXCEPT
4573                Some(SetOperator::Intersect) => 20,
4574                // Unexpected token or EOF => stop parsing the query body
4575                None => break,
4576            };
4577            if precedence >= next_precedence {
4578                break;
4579            }
4580            self.next_token(); // skip past the set operator
4581
4582            let all = self.parse_keyword(Keyword::ALL);
4583            let corresponding = self.parse_corresponding()?;
4584
4585            expr = SetExpr::SetOperation {
4586                left: Box::new(expr),
4587                op: op.unwrap(),
4588                corresponding,
4589                all,
4590                right: Box::new(self.parse_query_body(next_precedence)?),
4591            };
4592        }
4593
4594        Ok(expr)
4595    }
4596
4597    fn parse_set_operator(&mut self, token: &Token) -> Option<SetOperator> {
4598        match token {
4599            Token::Word(w) if w.keyword == Keyword::UNION => Some(SetOperator::Union),
4600            Token::Word(w) if w.keyword == Keyword::EXCEPT => Some(SetOperator::Except),
4601            Token::Word(w) if w.keyword == Keyword::INTERSECT => Some(SetOperator::Intersect),
4602            _ => None,
4603        }
4604    }
4605
4606    fn parse_corresponding(&mut self) -> ModalResult<Corresponding> {
4607        let corresponding = if self.parse_keyword(Keyword::CORRESPONDING) {
4608            let column_list = if self.parse_keyword(Keyword::BY) {
4609                Some(self.parse_parenthesized_column_list(IsOptional::Mandatory)?)
4610            } else {
4611                None
4612            };
4613            Corresponding::with_column_list(column_list)
4614        } else {
4615            Corresponding::none()
4616        };
4617        Ok(corresponding)
4618    }
4619
4620    /// Parse a restricted `SELECT` statement (no CTEs / `UNION` / `ORDER BY`),
4621    /// assuming the initial `SELECT` was already consumed
4622    pub fn parse_select(&mut self) -> ModalResult<Select> {
4623        let distinct = self.parse_all_or_distinct_on()?;
4624
4625        let projection = self.parse_comma_separated(Parser::parse_select_item)?;
4626
4627        // Note that for keywords to be properly handled here, they need to be
4628        // added to `RESERVED_FOR_COLUMN_ALIAS` / `RESERVED_FOR_TABLE_ALIAS`,
4629        // otherwise they may be parsed as an alias as part of the `projection`
4630        // or `from`.
4631
4632        let from = if self.parse_keyword(Keyword::FROM) {
4633            self.parse_comma_separated(Parser::parse_table_and_joins)?
4634        } else {
4635            vec![]
4636        };
4637        let mut lateral_views = vec![];
4638        loop {
4639            if self.parse_keywords(&[Keyword::LATERAL, Keyword::VIEW]) {
4640                let outer = self.parse_keyword(Keyword::OUTER);
4641                let lateral_view = self.parse_expr()?;
4642                let lateral_view_name = self.parse_object_name()?;
4643                let lateral_col_alias = self
4644                    .parse_comma_separated(|parser| {
4645                        parser.parse_optional_alias(&[
4646                            Keyword::WHERE,
4647                            Keyword::GROUP,
4648                            Keyword::CLUSTER,
4649                            Keyword::HAVING,
4650                            Keyword::LATERAL,
4651                        ]) // This couldn't possibly be a bad idea
4652                    })?
4653                    .into_iter()
4654                    .flatten()
4655                    .collect();
4656
4657                lateral_views.push(LateralView {
4658                    lateral_view,
4659                    lateral_view_name,
4660                    lateral_col_alias,
4661                    outer,
4662                });
4663            } else {
4664                break;
4665            }
4666        }
4667
4668        let selection = if self.parse_keyword(Keyword::WHERE) {
4669            Some(self.parse_expr()?)
4670        } else {
4671            None
4672        };
4673
4674        let group_by = if self.parse_keywords(&[Keyword::GROUP, Keyword::BY]) {
4675            self.parse_comma_separated(Parser::parse_group_by_expr)?
4676        } else {
4677            vec![]
4678        };
4679
4680        let having = if self.parse_keyword(Keyword::HAVING) {
4681            Some(self.parse_expr()?)
4682        } else {
4683            None
4684        };
4685
4686        Ok(Select {
4687            distinct,
4688            projection,
4689            from,
4690            lateral_views,
4691            selection,
4692            group_by,
4693            having,
4694        })
4695    }
4696
4697    pub fn parse_set(&mut self) -> ModalResult<Statement> {
4698        let modifier = self.parse_one_of_keywords(&[Keyword::SESSION, Keyword::LOCAL]);
4699        if self.parse_keywords(&[Keyword::TIME, Keyword::ZONE]) {
4700            let value = alt((
4701                Keyword::DEFAULT.value(SetTimeZoneValue::Default),
4702                Keyword::LOCAL.value(SetTimeZoneValue::Local),
4703                preceded(
4704                    Keyword::INTERVAL,
4705                    cut_err(Self::parse_literal_interval.try_map(|e| match e {
4706                        // support a special case for clients which would send when initializing the connection
4707                        // like: SET TIME ZONE INTERVAL '+00:00' HOUR TO MINUTE;
4708                        Expr::Value(v) => match v {
4709                            Value::Interval { value, .. } => {
4710                                if value != "+00:00" {
4711                                    return Err(StrError("only support \"+00:00\" ".into()));
4712                                }
4713                                Ok(SetTimeZoneValue::Ident(Ident::with_quote_unchecked(
4714                                    '\'',
4715                                    "UTC".to_owned(),
4716                                )))
4717                            }
4718                            _ => Err(StrError("expect Value::Interval".into())),
4719                        },
4720                        _ => Err(StrError("expect Expr::Value".into())),
4721                    })),
4722                ),
4723                Self::parse_identifier.map(SetTimeZoneValue::Ident),
4724                Self::ensure_parse_value.map(SetTimeZoneValue::Literal),
4725            ))
4726            .expect("variable")
4727            .parse_next(self)?;
4728
4729            Ok(Statement::SetTimeZone {
4730                local: modifier == Some(Keyword::LOCAL),
4731                value,
4732            })
4733        } else if self.parse_keyword(Keyword::CHARACTERISTICS) && modifier == Some(Keyword::SESSION)
4734        {
4735            self.expect_keywords(&[Keyword::AS, Keyword::TRANSACTION])?;
4736            Ok(Statement::SetTransaction {
4737                modes: self.parse_transaction_modes()?,
4738                snapshot: None,
4739                session: true,
4740            })
4741        } else if self.parse_keyword(Keyword::TRANSACTION) && modifier.is_none() {
4742            if self.parse_keyword(Keyword::SNAPSHOT) {
4743                let snapshot_id = self.ensure_parse_value()?;
4744                return Ok(Statement::SetTransaction {
4745                    modes: vec![],
4746                    snapshot: Some(snapshot_id),
4747                    session: false,
4748                });
4749            }
4750            Ok(Statement::SetTransaction {
4751                modes: self.parse_transaction_modes()?,
4752                snapshot: None,
4753                session: false,
4754            })
4755        } else {
4756            let variable = self.parse_identifier()?;
4757
4758            if self.consume_token(&Token::Eq) || self.parse_keyword(Keyword::TO) {
4759                let value = self.parse_set_variable()?;
4760                Ok(Statement::SetVariable {
4761                    local: modifier == Some(Keyword::LOCAL),
4762                    variable,
4763                    value,
4764                })
4765            } else {
4766                self.expected("equals sign or TO")
4767            }
4768        }
4769    }
4770
4771    /// If have `databases`,`tables`,`columns`,`schemas` and `materialized views` after show,
4772    /// return `Statement::ShowCommand` or `Statement::ShowColumn`,
4773    /// otherwise, return `Statement::ShowVariable`.
4774    pub fn parse_show(&mut self) -> ModalResult<Statement> {
4775        let checkpoint = *self;
4776        if let Token::Word(w) = self.next_token().token {
4777            match w.keyword {
4778                Keyword::TABLES => {
4779                    return Ok(Statement::ShowObjects {
4780                        object: ShowObject::Table {
4781                            schema: self.parse_from_and_identifier()?,
4782                        },
4783                        filter: self.parse_show_statement_filter()?,
4784                    });
4785                }
4786                Keyword::INTERNAL => {
4787                    self.expect_keyword(Keyword::TABLES)?;
4788                    return Ok(Statement::ShowObjects {
4789                        object: ShowObject::InternalTable {
4790                            schema: self.parse_from_and_identifier()?,
4791                        },
4792                        filter: self.parse_show_statement_filter()?,
4793                    });
4794                }
4795                Keyword::SOURCES => {
4796                    return Ok(Statement::ShowObjects {
4797                        object: ShowObject::Source {
4798                            schema: self.parse_from_and_identifier()?,
4799                        },
4800                        filter: self.parse_show_statement_filter()?,
4801                    });
4802                }
4803                Keyword::SINKS => {
4804                    return Ok(Statement::ShowObjects {
4805                        object: ShowObject::Sink {
4806                            schema: self.parse_from_and_identifier()?,
4807                        },
4808                        filter: self.parse_show_statement_filter()?,
4809                    });
4810                }
4811                Keyword::SUBSCRIPTIONS => {
4812                    return Ok(Statement::ShowObjects {
4813                        object: ShowObject::Subscription {
4814                            schema: self.parse_from_and_identifier()?,
4815                        },
4816                        filter: self.parse_show_statement_filter()?,
4817                    });
4818                }
4819                Keyword::DATABASES => {
4820                    return Ok(Statement::ShowObjects {
4821                        object: ShowObject::Database,
4822                        filter: self.parse_show_statement_filter()?,
4823                    });
4824                }
4825                Keyword::SCHEMAS => {
4826                    return Ok(Statement::ShowObjects {
4827                        object: ShowObject::Schema,
4828                        filter: self.parse_show_statement_filter()?,
4829                    });
4830                }
4831                Keyword::VIEWS => {
4832                    return Ok(Statement::ShowObjects {
4833                        object: ShowObject::View {
4834                            schema: self.parse_from_and_identifier()?,
4835                        },
4836                        filter: self.parse_show_statement_filter()?,
4837                    });
4838                }
4839                Keyword::MATERIALIZED => {
4840                    if self.parse_keyword(Keyword::VIEWS) {
4841                        return Ok(Statement::ShowObjects {
4842                            object: ShowObject::MaterializedView {
4843                                schema: self.parse_from_and_identifier()?,
4844                            },
4845                            filter: self.parse_show_statement_filter()?,
4846                        });
4847                    } else {
4848                        return self.expected("VIEWS after MATERIALIZED");
4849                    }
4850                }
4851                Keyword::COLUMNS => {
4852                    if self.parse_keyword(Keyword::FROM) {
4853                        return Ok(Statement::ShowObjects {
4854                            object: ShowObject::Columns {
4855                                table: self.parse_object_name()?,
4856                            },
4857                            filter: self.parse_show_statement_filter()?,
4858                        });
4859                    } else {
4860                        return self.expected("from after columns");
4861                    }
4862                }
4863                Keyword::SECRETS => {
4864                    return Ok(Statement::ShowObjects {
4865                        object: ShowObject::Secret {
4866                            schema: self.parse_from_and_identifier()?,
4867                        },
4868                        filter: self.parse_show_statement_filter()?,
4869                    });
4870                }
4871                Keyword::CONNECTIONS => {
4872                    return Ok(Statement::ShowObjects {
4873                        object: ShowObject::Connection {
4874                            schema: self.parse_from_and_identifier()?,
4875                        },
4876                        filter: self.parse_show_statement_filter()?,
4877                    });
4878                }
4879                Keyword::FUNCTIONS => {
4880                    return Ok(Statement::ShowObjects {
4881                        object: ShowObject::Function {
4882                            schema: self.parse_from_and_identifier()?,
4883                        },
4884                        filter: self.parse_show_statement_filter()?,
4885                    });
4886                }
4887                Keyword::INDEXES => {
4888                    if self.parse_keyword(Keyword::FROM) {
4889                        return Ok(Statement::ShowObjects {
4890                            object: ShowObject::Indexes {
4891                                table: self.parse_object_name()?,
4892                            },
4893                            filter: self.parse_show_statement_filter()?,
4894                        });
4895                    } else {
4896                        return self.expected("from after indexes");
4897                    }
4898                }
4899                Keyword::CLUSTER => {
4900                    return Ok(Statement::ShowObjects {
4901                        object: ShowObject::Cluster,
4902                        filter: self.parse_show_statement_filter()?,
4903                    });
4904                }
4905                Keyword::JOBS => {
4906                    return Ok(Statement::ShowObjects {
4907                        object: ShowObject::Jobs,
4908                        filter: self.parse_show_statement_filter()?,
4909                    });
4910                }
4911                Keyword::PROCESSLIST => {
4912                    return Ok(Statement::ShowObjects {
4913                        object: ShowObject::ProcessList,
4914                        filter: self.parse_show_statement_filter()?,
4915                    });
4916                }
4917                Keyword::TRANSACTION => {
4918                    self.expect_keywords(&[Keyword::ISOLATION, Keyword::LEVEL])?;
4919                    return Ok(Statement::ShowTransactionIsolationLevel);
4920                }
4921                Keyword::CURSORS => {
4922                    return Ok(Statement::ShowObjects {
4923                        object: ShowObject::Cursor,
4924                        filter: None,
4925                    });
4926                }
4927                Keyword::SUBSCRIPTION => {
4928                    self.expect_keyword(Keyword::CURSORS)?;
4929                    return Ok(Statement::ShowObjects {
4930                        object: ShowObject::SubscriptionCursor,
4931                        filter: None,
4932                    });
4933                }
4934                _ => {}
4935            }
4936        }
4937        *self = checkpoint;
4938        Ok(Statement::ShowVariable {
4939            variable: self.parse_identifiers()?,
4940        })
4941    }
4942
4943    pub fn parse_cancel_job(&mut self) -> ModalResult<Statement> {
4944        // CANCEL [JOBS|JOB] job_ids
4945        match self.peek_token().token {
4946            Token::Word(w) if Keyword::JOBS == w.keyword || Keyword::JOB == w.keyword => {
4947                self.next_token();
4948            }
4949            _ => return self.expected("JOBS or JOB after CANCEL"),
4950        }
4951
4952        let mut job_ids = vec![];
4953        loop {
4954            job_ids.push(self.parse_literal_uint()? as u32);
4955            if !self.consume_token(&Token::Comma) {
4956                break;
4957            }
4958        }
4959        Ok(Statement::CancelJobs(JobIdents(job_ids)))
4960    }
4961
4962    pub fn parse_kill_process(&mut self) -> ModalResult<Statement> {
4963        let process_id = self.parse_literal_uint()? as i32;
4964        Ok(Statement::Kill(process_id))
4965    }
4966
4967    /// Parser `from schema` after `show tables` and `show materialized views`, if not conclude
4968    /// `from` then use default schema name.
4969    pub fn parse_from_and_identifier(&mut self) -> ModalResult<Option<Ident>> {
4970        if self.parse_keyword(Keyword::FROM) {
4971            Ok(Some(self.parse_identifier_non_reserved()?))
4972        } else {
4973            Ok(None)
4974        }
4975    }
4976
4977    /// Parse object type and name after `show create`.
4978    pub fn parse_show_create(&mut self) -> ModalResult<Statement> {
4979        if let Token::Word(w) = self.next_token().token {
4980            let show_type = match w.keyword {
4981                Keyword::TABLE => ShowCreateType::Table,
4982                Keyword::MATERIALIZED => {
4983                    if self.parse_keyword(Keyword::VIEW) {
4984                        ShowCreateType::MaterializedView
4985                    } else {
4986                        return self.expected("VIEW after MATERIALIZED");
4987                    }
4988                }
4989                Keyword::VIEW => ShowCreateType::View,
4990                Keyword::INDEX => ShowCreateType::Index,
4991                Keyword::SOURCE => ShowCreateType::Source,
4992                Keyword::SINK => ShowCreateType::Sink,
4993                Keyword::SUBSCRIPTION => ShowCreateType::Subscription,
4994                Keyword::FUNCTION => ShowCreateType::Function,
4995                _ => return self.expected(
4996                    "TABLE, MATERIALIZED VIEW, VIEW, INDEX, FUNCTION, SOURCE, SUBSCRIPTION or SINK",
4997                ),
4998            };
4999            return Ok(Statement::ShowCreateObject {
5000                create_type: show_type,
5001                name: self.parse_object_name()?,
5002            });
5003        }
5004        self.expected(
5005            "TABLE, MATERIALIZED VIEW, VIEW, INDEX, FUNCTION, SOURCE, SUBSCRIPTION or SINK",
5006        )
5007    }
5008
5009    pub fn parse_show_statement_filter(&mut self) -> ModalResult<Option<ShowStatementFilter>> {
5010        if self.parse_keyword(Keyword::LIKE) {
5011            Ok(Some(ShowStatementFilter::Like(
5012                self.parse_literal_string()?,
5013            )))
5014        } else if self.parse_keyword(Keyword::ILIKE) {
5015            Ok(Some(ShowStatementFilter::ILike(
5016                self.parse_literal_string()?,
5017            )))
5018        } else if self.parse_keyword(Keyword::WHERE) {
5019            Ok(Some(ShowStatementFilter::Where(self.parse_expr()?)))
5020        } else {
5021            Ok(None)
5022        }
5023    }
5024
5025    pub fn parse_table_and_joins(&mut self) -> ModalResult<TableWithJoins> {
5026        let relation = self.parse_table_factor()?;
5027
5028        // Note that for keywords to be properly handled here, they need to be
5029        // added to `RESERVED_FOR_TABLE_ALIAS`, otherwise they may be parsed as
5030        // a table alias.
5031        let mut joins = vec![];
5032        loop {
5033            let join = if self.parse_keyword(Keyword::CROSS) {
5034                let join_operator = if self.parse_keyword(Keyword::JOIN) {
5035                    JoinOperator::CrossJoin
5036                } else {
5037                    return self.expected("JOIN after CROSS");
5038                };
5039                Join {
5040                    relation: self.parse_table_factor()?,
5041                    join_operator,
5042                }
5043            } else {
5044                let (natural, asof) =
5045                    match self.parse_one_of_keywords(&[Keyword::NATURAL, Keyword::ASOF]) {
5046                        Some(Keyword::NATURAL) => (true, false),
5047                        Some(Keyword::ASOF) => (false, true),
5048                        Some(_) => unreachable!(),
5049                        None => (false, false),
5050                    };
5051                let peek_keyword = if let Token::Word(w) = self.peek_token().token {
5052                    w.keyword
5053                } else {
5054                    Keyword::NoKeyword
5055                };
5056
5057                let join_operator_type = match peek_keyword {
5058                    Keyword::INNER | Keyword::JOIN => {
5059                        let _ = self.parse_keyword(Keyword::INNER);
5060                        self.expect_keyword(Keyword::JOIN)?;
5061                        if asof {
5062                            JoinOperator::AsOfInner
5063                        } else {
5064                            JoinOperator::Inner
5065                        }
5066                    }
5067                    kw @ Keyword::LEFT | kw @ Keyword::RIGHT | kw @ Keyword::FULL => {
5068                        let checkpoint = *self;
5069                        let _ = self.next_token();
5070                        let _ = self.parse_keyword(Keyword::OUTER);
5071                        self.expect_keyword(Keyword::JOIN)?;
5072                        if asof {
5073                            if Keyword::LEFT == kw {
5074                                JoinOperator::AsOfLeft
5075                            } else {
5076                                return self.expected_at(
5077                                    checkpoint,
5078                                    "LEFT after ASOF. RIGHT or FULL are not supported",
5079                                );
5080                            }
5081                        } else {
5082                            match kw {
5083                                Keyword::LEFT => JoinOperator::LeftOuter,
5084                                Keyword::RIGHT => JoinOperator::RightOuter,
5085                                Keyword::FULL => JoinOperator::FullOuter,
5086                                _ => unreachable!(),
5087                            }
5088                        }
5089                    }
5090                    Keyword::OUTER => {
5091                        return self.expected("LEFT, RIGHT, or FULL");
5092                    }
5093                    _ if natural => {
5094                        return self.expected("a join type after NATURAL");
5095                    }
5096                    _ if asof => {
5097                        return self.expected("a join type after ASOF");
5098                    }
5099                    _ => break,
5100                };
5101                let relation = self.parse_table_factor()?;
5102                let join_constraint = self.parse_join_constraint(natural)?;
5103                let join_operator = join_operator_type(join_constraint);
5104                let need_constraint = match join_operator {
5105                    JoinOperator::Inner(JoinConstraint::None) => Some("INNER JOIN"),
5106                    JoinOperator::AsOfInner(JoinConstraint::None) => Some("ASOF INNER JOIN"),
5107                    JoinOperator::AsOfLeft(JoinConstraint::None) => Some("ASOF LEFT JOIN"),
5108                    _ => None,
5109                };
5110                if let Some(join_type) = need_constraint {
5111                    return self.expected(&format!("join constraint after {join_type}"));
5112                }
5113
5114                Join {
5115                    relation,
5116                    join_operator,
5117                }
5118            };
5119            joins.push(join);
5120        }
5121        Ok(TableWithJoins { relation, joins })
5122    }
5123
5124    /// A table name or a parenthesized subquery, followed by optional `[AS] alias`
5125    pub fn parse_table_factor(&mut self) -> ModalResult<TableFactor> {
5126        if self.parse_keyword(Keyword::LATERAL) {
5127            // LATERAL must always be followed by a subquery.
5128            if !self.consume_token(&Token::LParen) {
5129                self.expected("subquery after LATERAL")?;
5130            }
5131            self.parse_derived_table_factor(Lateral)
5132        } else if self.consume_token(&Token::LParen) {
5133            // A left paren introduces either a derived table (i.e., a subquery)
5134            // or a nested join. It's nearly impossible to determine ahead of
5135            // time which it is... so we just try to parse both.
5136            //
5137            // Here's an example that demonstrates the complexity:
5138            //                     /-------------------------------------------------------\
5139            //                     | /-----------------------------------\                 |
5140            //     SELECT * FROM ( ( ( (SELECT 1) UNION (SELECT 2) ) AS t1 NATURAL JOIN t2 ) )
5141            //                   ^ ^ ^ ^
5142            //                   | | | |
5143            //                   | | | |
5144            //                   | | | (4) belongs to a SetExpr::Query inside the subquery
5145            //                   | | (3) starts a derived table (subquery)
5146            //                   | (2) starts a nested join
5147            //                   (1) an additional set of parens around a nested join
5148            //
5149
5150            // It can only be a subquery. We don't use `maybe_parse` so that a meaningful error can
5151            // be returned.
5152            match self.peek_token().token {
5153                Token::Word(w)
5154                    if [Keyword::SELECT, Keyword::WITH, Keyword::VALUES].contains(&w.keyword) =>
5155                {
5156                    return self.parse_derived_table_factor(NotLateral);
5157                }
5158                _ => {}
5159            };
5160            // It can still be a subquery, e.g., the case (3) in the example above:
5161            // (SELECT 1) UNION (SELECT 2)
5162            // TODO: how to produce a good error message here?
5163            if self.peek_token() == Token::LParen {
5164                return_ok_if_some!(
5165                    self.maybe_parse(|parser| parser.parse_derived_table_factor(NotLateral))
5166                );
5167            }
5168
5169            // A parsing error from `parse_derived_table_factor` indicates that the '(' we've
5170            // recently consumed does not start a derived table (cases 1, 2, or 4).
5171            // `maybe_parse` will ignore such an error and rewind to be after the opening '('.
5172
5173            // Inside the parentheses we expect to find an (A) table factor
5174            // followed by some joins or (B) another level of nesting.
5175            let table_and_joins = self.parse_table_and_joins()?;
5176
5177            #[allow(clippy::if_same_then_else)]
5178            if !table_and_joins.joins.is_empty() {
5179                self.expect_token(&Token::RParen)?;
5180                Ok(TableFactor::NestedJoin(Box::new(table_and_joins))) // (A)
5181            } else if let TableFactor::NestedJoin(_) = &table_and_joins.relation {
5182                // (B): `table_and_joins` (what we found inside the parentheses)
5183                // is a nested join `(foo JOIN bar)`, not followed by other joins.
5184                self.expect_token(&Token::RParen)?;
5185                Ok(TableFactor::NestedJoin(Box::new(table_and_joins)))
5186            } else {
5187                // The SQL spec prohibits derived tables and bare tables from
5188                // appearing alone in parentheses (e.g. `FROM (mytable)`)
5189                parser_err!(
5190                    "Expected joined table, found: {table_and_joins}, next_token: {}",
5191                    self.peek_token()
5192                );
5193            }
5194        } else {
5195            let name = self.parse_object_name()?;
5196            if self.peek_token() == Token::LParen {
5197                // table-valued function
5198
5199                let arg_list = self.parse_argument_list()?;
5200                if arg_list.distinct {
5201                    parser_err!("DISTINCT is not supported in table-valued function calls");
5202                }
5203                if !arg_list.order_by.is_empty() {
5204                    parser_err!("ORDER BY is not supported in table-valued function calls");
5205                }
5206                if arg_list.ignore_nulls {
5207                    parser_err!("IGNORE NULLS is not supported in table-valued function calls");
5208                }
5209
5210                let args = arg_list.args;
5211                let with_ordinality = self.parse_keywords(&[Keyword::WITH, Keyword::ORDINALITY]);
5212                let alias = self.parse_optional_table_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?;
5213
5214                Ok(TableFactor::TableFunction {
5215                    name,
5216                    alias,
5217                    args,
5218                    with_ordinality,
5219                })
5220            } else {
5221                let as_of = opt(Self::parse_as_of).parse_next(self)?;
5222                let alias = self.parse_optional_table_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?;
5223                Ok(TableFactor::Table { name, alias, as_of })
5224            }
5225        }
5226    }
5227
5228    pub fn parse_derived_table_factor(&mut self, lateral: IsLateral) -> ModalResult<TableFactor> {
5229        let subquery = Box::new(self.parse_query()?);
5230        self.expect_token(&Token::RParen)?;
5231        let alias = self.parse_optional_table_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?;
5232        Ok(TableFactor::Derived {
5233            lateral: match lateral {
5234                Lateral => true,
5235                NotLateral => false,
5236            },
5237            subquery,
5238            alias,
5239        })
5240    }
5241
5242    fn parse_join_constraint(&mut self, natural: bool) -> ModalResult<JoinConstraint> {
5243        if natural {
5244            Ok(JoinConstraint::Natural)
5245        } else if self.parse_keyword(Keyword::ON) {
5246            let constraint = self.parse_expr()?;
5247            Ok(JoinConstraint::On(constraint))
5248        } else if self.parse_keyword(Keyword::USING) {
5249            let columns = self.parse_parenthesized_column_list(Mandatory)?;
5250            Ok(JoinConstraint::Using(columns))
5251        } else {
5252            Ok(JoinConstraint::None)
5253            // self.expected("ON, or USING after JOIN")
5254        }
5255    }
5256
5257    /// Parse a GRANT statement.
5258    pub fn parse_grant(&mut self) -> ModalResult<Statement> {
5259        let (privileges, objects) = self.parse_grant_revoke_privileges_objects()?;
5260
5261        self.expect_keyword(Keyword::TO)?;
5262        let grantees = self.parse_comma_separated(Parser::parse_identifier)?;
5263
5264        let with_grant_option =
5265            self.parse_keywords(&[Keyword::WITH, Keyword::GRANT, Keyword::OPTION]);
5266
5267        let granted_by = self
5268            .parse_keywords(&[Keyword::GRANTED, Keyword::BY])
5269            .then(|| self.parse_identifier().unwrap());
5270
5271        Ok(Statement::Grant {
5272            privileges,
5273            objects,
5274            grantees,
5275            with_grant_option,
5276            granted_by,
5277        })
5278    }
5279
5280    fn parse_grant_revoke_privileges_objects(&mut self) -> ModalResult<(Privileges, GrantObjects)> {
5281        let privileges = if self.parse_keyword(Keyword::ALL) {
5282            Privileges::All {
5283                with_privileges_keyword: self.parse_keyword(Keyword::PRIVILEGES),
5284            }
5285        } else {
5286            Privileges::Actions(
5287                self.parse_comma_separated(Parser::parse_grant_permission)?
5288                    .into_iter()
5289                    .map(|(kw, columns)| match kw {
5290                        Keyword::CONNECT => Action::Connect,
5291                        Keyword::CREATE => Action::Create,
5292                        Keyword::DELETE => Action::Delete,
5293                        Keyword::EXECUTE => Action::Execute,
5294                        Keyword::INSERT => Action::Insert { columns },
5295                        Keyword::REFERENCES => Action::References { columns },
5296                        Keyword::SELECT => Action::Select { columns },
5297                        Keyword::TEMPORARY => Action::Temporary,
5298                        Keyword::TRIGGER => Action::Trigger,
5299                        Keyword::TRUNCATE => Action::Truncate,
5300                        Keyword::UPDATE => Action::Update { columns },
5301                        Keyword::USAGE => Action::Usage,
5302                        _ => unreachable!(),
5303                    })
5304                    .collect(),
5305            )
5306        };
5307
5308        self.expect_keyword(Keyword::ON)?;
5309
5310        let objects = if self.parse_keywords(&[
5311            Keyword::ALL,
5312            Keyword::TABLES,
5313            Keyword::IN,
5314            Keyword::SCHEMA,
5315        ]) {
5316            GrantObjects::AllTablesInSchema {
5317                schemas: self.parse_comma_separated(Parser::parse_object_name)?,
5318            }
5319        } else if self.parse_keywords(&[
5320            Keyword::ALL,
5321            Keyword::SEQUENCES,
5322            Keyword::IN,
5323            Keyword::SCHEMA,
5324        ]) {
5325            GrantObjects::AllSequencesInSchema {
5326                schemas: self.parse_comma_separated(Parser::parse_object_name)?,
5327            }
5328        } else if self.parse_keywords(&[
5329            Keyword::ALL,
5330            Keyword::SOURCES,
5331            Keyword::IN,
5332            Keyword::SCHEMA,
5333        ]) {
5334            GrantObjects::AllSourcesInSchema {
5335                schemas: self.parse_comma_separated(Parser::parse_object_name)?,
5336            }
5337        } else if self.parse_keywords(&[Keyword::ALL, Keyword::SINKS, Keyword::IN, Keyword::SCHEMA])
5338        {
5339            GrantObjects::AllSinksInSchema {
5340                schemas: self.parse_comma_separated(Parser::parse_object_name)?,
5341            }
5342        } else if self.parse_keywords(&[
5343            Keyword::ALL,
5344            Keyword::MATERIALIZED,
5345            Keyword::VIEWS,
5346            Keyword::IN,
5347            Keyword::SCHEMA,
5348        ]) {
5349            GrantObjects::AllMviewsInSchema {
5350                schemas: self.parse_comma_separated(Parser::parse_object_name)?,
5351            }
5352        } else if self.parse_keywords(&[Keyword::ALL, Keyword::VIEWS, Keyword::IN, Keyword::SCHEMA])
5353        {
5354            GrantObjects::AllViewsInSchema {
5355                schemas: self.parse_comma_separated(Parser::parse_object_name)?,
5356            }
5357        } else if self.parse_keywords(&[
5358            Keyword::ALL,
5359            Keyword::FUNCTIONS,
5360            Keyword::IN,
5361            Keyword::SCHEMA,
5362        ]) {
5363            GrantObjects::AllFunctionsInSchema {
5364                schemas: self.parse_comma_separated(Parser::parse_object_name)?,
5365            }
5366        } else if self.parse_keywords(&[
5367            Keyword::ALL,
5368            Keyword::SECRETS,
5369            Keyword::IN,
5370            Keyword::SCHEMA,
5371        ]) {
5372            GrantObjects::AllSecretsInSchema {
5373                schemas: self.parse_comma_separated(Parser::parse_object_name)?,
5374            }
5375        } else if self.parse_keywords(&[
5376            Keyword::ALL,
5377            Keyword::CONNECTIONS,
5378            Keyword::IN,
5379            Keyword::SCHEMA,
5380        ]) {
5381            GrantObjects::AllConnectionsInSchema {
5382                schemas: self.parse_comma_separated(Parser::parse_object_name)?,
5383            }
5384        } else if self.parse_keywords(&[
5385            Keyword::ALL,
5386            Keyword::SUBSCRIPTIONS,
5387            Keyword::IN,
5388            Keyword::SCHEMA,
5389        ]) {
5390            GrantObjects::AllSubscriptionsInSchema {
5391                schemas: self.parse_comma_separated(Parser::parse_object_name)?,
5392            }
5393        } else if self.parse_keywords(&[Keyword::MATERIALIZED, Keyword::VIEW]) {
5394            GrantObjects::Mviews(self.parse_comma_separated(Parser::parse_object_name)?)
5395        } else {
5396            let object_type = self.parse_one_of_keywords(&[
5397                Keyword::SEQUENCE,
5398                Keyword::DATABASE,
5399                Keyword::SCHEMA,
5400                Keyword::TABLE,
5401                Keyword::SOURCE,
5402                Keyword::SINK,
5403                Keyword::VIEW,
5404                Keyword::SUBSCRIPTION,
5405                Keyword::FUNCTION,
5406                Keyword::CONNECTION,
5407                Keyword::SECRET,
5408            ]);
5409            if let Some(Keyword::FUNCTION) = object_type {
5410                let func_descs = self.parse_comma_separated(Parser::parse_function_desc)?;
5411                GrantObjects::Functions(func_descs)
5412            } else {
5413                let objects = self.parse_comma_separated(Parser::parse_object_name);
5414                match object_type {
5415                    Some(Keyword::DATABASE) => GrantObjects::Databases(objects?),
5416                    Some(Keyword::SCHEMA) => GrantObjects::Schemas(objects?),
5417                    Some(Keyword::SEQUENCE) => GrantObjects::Sequences(objects?),
5418                    Some(Keyword::SOURCE) => GrantObjects::Sources(objects?),
5419                    Some(Keyword::SINK) => GrantObjects::Sinks(objects?),
5420                    Some(Keyword::VIEW) => GrantObjects::Views(objects?),
5421                    Some(Keyword::SUBSCRIPTION) => GrantObjects::Subscriptions(objects?),
5422                    Some(Keyword::CONNECTION) => GrantObjects::Connections(objects?),
5423                    Some(Keyword::SECRET) => GrantObjects::Secrets(objects?),
5424                    Some(Keyword::TABLE) | None => GrantObjects::Tables(objects?),
5425                    _ => unreachable!(),
5426                }
5427            }
5428        };
5429
5430        Ok((privileges, objects))
5431    }
5432
5433    fn parse_grant_permission(&mut self) -> ModalResult<(Keyword, Option<Vec<Ident>>)> {
5434        let kw = self.expect_one_of_keywords(&[
5435            Keyword::CONNECT,
5436            Keyword::CREATE,
5437            Keyword::DELETE,
5438            Keyword::EXECUTE,
5439            Keyword::INSERT,
5440            Keyword::REFERENCES,
5441            Keyword::SELECT,
5442            Keyword::TEMPORARY,
5443            Keyword::TRIGGER,
5444            Keyword::TRUNCATE,
5445            Keyword::UPDATE,
5446            Keyword::USAGE,
5447        ])?;
5448        let columns = match kw {
5449            Keyword::INSERT | Keyword::REFERENCES | Keyword::SELECT | Keyword::UPDATE => {
5450                let columns = self.parse_parenthesized_column_list(Optional)?;
5451                if columns.is_empty() {
5452                    None
5453                } else {
5454                    Some(columns)
5455                }
5456            }
5457            _ => None,
5458        };
5459        Ok((kw, columns))
5460    }
5461
5462    /// Parse a REVOKE statement
5463    pub fn parse_revoke(&mut self) -> ModalResult<Statement> {
5464        let revoke_grant_option =
5465            self.parse_keywords(&[Keyword::GRANT, Keyword::OPTION, Keyword::FOR]);
5466        let (privileges, objects) = self.parse_grant_revoke_privileges_objects()?;
5467
5468        self.expect_keyword(Keyword::FROM)?;
5469        let grantees = self.parse_comma_separated(Parser::parse_identifier)?;
5470
5471        let granted_by = self
5472            .parse_keywords(&[Keyword::GRANTED, Keyword::BY])
5473            .then(|| self.parse_identifier().unwrap());
5474
5475        let cascade = self.parse_keyword(Keyword::CASCADE);
5476        let restrict = self.parse_keyword(Keyword::RESTRICT);
5477        if cascade && restrict {
5478            parser_err!("Cannot specify both CASCADE and RESTRICT in REVOKE");
5479        }
5480
5481        Ok(Statement::Revoke {
5482            privileges,
5483            objects,
5484            grantees,
5485            granted_by,
5486            revoke_grant_option,
5487            cascade,
5488        })
5489    }
5490
5491    /// Parse an INSERT statement
5492    pub fn parse_insert(&mut self) -> ModalResult<Statement> {
5493        self.expect_keyword(Keyword::INTO)?;
5494
5495        let table_name = self.parse_object_name()?;
5496        let columns = self.parse_parenthesized_column_list(Optional)?;
5497
5498        let source = Box::new(self.parse_query()?);
5499        let returning = self.parse_returning(Optional)?;
5500        Ok(Statement::Insert {
5501            table_name,
5502            columns,
5503            source,
5504            returning,
5505        })
5506    }
5507
5508    pub fn parse_update(&mut self) -> ModalResult<Statement> {
5509        let table_name = self.parse_object_name()?;
5510
5511        self.expect_keyword(Keyword::SET)?;
5512        let assignments = self.parse_comma_separated(Parser::parse_assignment)?;
5513        let selection = if self.parse_keyword(Keyword::WHERE) {
5514            Some(self.parse_expr()?)
5515        } else {
5516            None
5517        };
5518        let returning = self.parse_returning(Optional)?;
5519        Ok(Statement::Update {
5520            table_name,
5521            assignments,
5522            selection,
5523            returning,
5524        })
5525    }
5526
5527    /// Parse a `var = expr` assignment, used in an UPDATE statement
5528    pub fn parse_assignment(&mut self) -> ModalResult<Assignment> {
5529        let id = self.parse_identifiers_non_keywords()?;
5530        self.expect_token(&Token::Eq)?;
5531
5532        let value = if self.parse_keyword(Keyword::DEFAULT) {
5533            AssignmentValue::Default
5534        } else {
5535            AssignmentValue::Expr(self.parse_expr()?)
5536        };
5537
5538        Ok(Assignment { id, value })
5539    }
5540
5541    /// Parse a `[VARIADIC] name => expr`.
5542    fn parse_function_args(&mut self) -> ModalResult<(bool, FunctionArg)> {
5543        let variadic = self.parse_keyword(Keyword::VARIADIC);
5544        let arg = if self.peek_nth_token(1) == Token::RArrow {
5545            let name = self.parse_identifier()?;
5546
5547            self.expect_token(&Token::RArrow)?;
5548            let arg = self.parse_wildcard_or_expr()?.into();
5549
5550            FunctionArg::Named { name, arg }
5551        } else {
5552            FunctionArg::Unnamed(self.parse_wildcard_or_expr()?.into())
5553        };
5554        Ok((variadic, arg))
5555    }
5556
5557    pub fn parse_argument_list(&mut self) -> ModalResult<FunctionArgList> {
5558        self.expect_token(&Token::LParen)?;
5559        if self.consume_token(&Token::RParen) {
5560            Ok(FunctionArgList::empty())
5561        } else {
5562            let distinct = self.parse_all_or_distinct()?;
5563            let args = self.parse_comma_separated(Parser::parse_function_args)?;
5564            if args
5565                .iter()
5566                .take(args.len() - 1)
5567                .any(|(variadic, _)| *variadic)
5568            {
5569                parser_err!("VARIADIC argument must be the last");
5570            }
5571            let variadic = args.last().map(|(variadic, _)| *variadic).unwrap_or(false);
5572            let args = args.into_iter().map(|(_, arg)| arg).collect();
5573
5574            let order_by = if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
5575                self.parse_comma_separated(Parser::parse_order_by_expr)?
5576            } else {
5577                vec![]
5578            };
5579
5580            let ignore_nulls = self.parse_keywords(&[Keyword::IGNORE, Keyword::NULLS]);
5581
5582            let arg_list = FunctionArgList {
5583                distinct,
5584                args,
5585                variadic,
5586                order_by,
5587                ignore_nulls,
5588            };
5589
5590            self.expect_token(&Token::RParen)?;
5591            Ok(arg_list)
5592        }
5593    }
5594
5595    /// Parse a comma-delimited list of projections after SELECT
5596    pub fn parse_select_item(&mut self) -> ModalResult<SelectItem> {
5597        match self.parse_wildcard_or_expr()? {
5598            WildcardOrExpr::Expr(expr) => self
5599                .parse_optional_alias(keywords::RESERVED_FOR_COLUMN_ALIAS)
5600                .map(|alias| match alias {
5601                    Some(alias) => SelectItem::ExprWithAlias { expr, alias },
5602                    None => SelectItem::UnnamedExpr(expr),
5603                }),
5604            WildcardOrExpr::QualifiedWildcard(prefix, except) => {
5605                Ok(SelectItem::QualifiedWildcard(prefix, except))
5606            }
5607            WildcardOrExpr::ExprQualifiedWildcard(expr, prefix) => {
5608                Ok(SelectItem::ExprQualifiedWildcard(expr, prefix))
5609            }
5610            WildcardOrExpr::Wildcard(except) => Ok(SelectItem::Wildcard(except)),
5611        }
5612    }
5613
5614    /// Parse an expression, optionally followed by ASC or DESC (used in ORDER BY)
5615    pub fn parse_order_by_expr(&mut self) -> ModalResult<OrderByExpr> {
5616        let expr = self.parse_expr()?;
5617
5618        let asc = if self.parse_keyword(Keyword::ASC) {
5619            Some(true)
5620        } else if self.parse_keyword(Keyword::DESC) {
5621            Some(false)
5622        } else {
5623            None
5624        };
5625
5626        let nulls_first = if self.parse_keywords(&[Keyword::NULLS, Keyword::FIRST]) {
5627            Some(true)
5628        } else if self.parse_keywords(&[Keyword::NULLS, Keyword::LAST]) {
5629            Some(false)
5630        } else {
5631            None
5632        };
5633
5634        Ok(OrderByExpr {
5635            expr,
5636            asc,
5637            nulls_first,
5638        })
5639    }
5640
5641    /// Parse a LIMIT clause
5642    pub fn parse_limit(&mut self) -> ModalResult<Option<Expr>> {
5643        if self.parse_keyword(Keyword::ALL) {
5644            Ok(None)
5645        } else {
5646            let expr = self.parse_expr()?;
5647            Ok(Some(expr))
5648        }
5649    }
5650
5651    /// Parse an OFFSET clause
5652    pub fn parse_offset(&mut self) -> ModalResult<String> {
5653        let value = self.parse_number_value()?;
5654        // TODO(Kexiang): support LIMIT expr
5655        if self.consume_token(&Token::DoubleColon) {
5656            self.expect_keyword(Keyword::BIGINT)?;
5657        }
5658        _ = self.parse_one_of_keywords(&[Keyword::ROW, Keyword::ROWS]);
5659        Ok(value)
5660    }
5661
5662    /// Parse a FETCH clause
5663    pub fn parse_fetch(&mut self) -> ModalResult<Fetch> {
5664        self.expect_one_of_keywords(&[Keyword::FIRST, Keyword::NEXT])?;
5665        let quantity = if self
5666            .parse_one_of_keywords(&[Keyword::ROW, Keyword::ROWS])
5667            .is_some()
5668        {
5669            None
5670        } else {
5671            let quantity = self.parse_number_value()?;
5672            self.expect_one_of_keywords(&[Keyword::ROW, Keyword::ROWS])?;
5673            Some(quantity)
5674        };
5675        let with_ties = if self.parse_keyword(Keyword::ONLY) {
5676            false
5677        } else if self.parse_keywords(&[Keyword::WITH, Keyword::TIES]) {
5678            true
5679        } else {
5680            return self.expected("one of ONLY or WITH TIES");
5681        };
5682        Ok(Fetch {
5683            with_ties,
5684            quantity,
5685        })
5686    }
5687
5688    pub fn parse_values(&mut self) -> ModalResult<Values> {
5689        let values = self.parse_comma_separated(|parser| {
5690            parser.expect_token(&Token::LParen)?;
5691            let exprs = parser.parse_comma_separated(Parser::parse_expr)?;
5692            parser.expect_token(&Token::RParen)?;
5693            Ok(exprs)
5694        })?;
5695        Ok(Values(values))
5696    }
5697
5698    pub fn parse_start_transaction(&mut self) -> ModalResult<Statement> {
5699        self.expect_keyword(Keyword::TRANSACTION)?;
5700        Ok(Statement::StartTransaction {
5701            modes: self.parse_transaction_modes()?,
5702        })
5703    }
5704
5705    pub fn parse_begin(&mut self) -> ModalResult<Statement> {
5706        let _ = self.parse_one_of_keywords(&[Keyword::TRANSACTION, Keyword::WORK]);
5707        Ok(Statement::Begin {
5708            modes: self.parse_transaction_modes()?,
5709        })
5710    }
5711
5712    pub fn parse_transaction_modes(&mut self) -> ModalResult<Vec<TransactionMode>> {
5713        let mut modes = vec![];
5714        let mut required = false;
5715        loop {
5716            let mode = if self.parse_keywords(&[Keyword::ISOLATION, Keyword::LEVEL]) {
5717                let iso_level = if self.parse_keywords(&[Keyword::READ, Keyword::UNCOMMITTED]) {
5718                    TransactionIsolationLevel::ReadUncommitted
5719                } else if self.parse_keywords(&[Keyword::READ, Keyword::COMMITTED]) {
5720                    TransactionIsolationLevel::ReadCommitted
5721                } else if self.parse_keywords(&[Keyword::REPEATABLE, Keyword::READ]) {
5722                    TransactionIsolationLevel::RepeatableRead
5723                } else if self.parse_keyword(Keyword::SERIALIZABLE) {
5724                    TransactionIsolationLevel::Serializable
5725                } else {
5726                    self.expected("isolation level")?
5727                };
5728                TransactionMode::IsolationLevel(iso_level)
5729            } else if self.parse_keywords(&[Keyword::READ, Keyword::ONLY]) {
5730                TransactionMode::AccessMode(TransactionAccessMode::ReadOnly)
5731            } else if self.parse_keywords(&[Keyword::READ, Keyword::WRITE]) {
5732                TransactionMode::AccessMode(TransactionAccessMode::ReadWrite)
5733            } else if required {
5734                self.expected("transaction mode")?
5735            } else {
5736                break;
5737            };
5738            modes.push(mode);
5739            // ANSI requires a comma after each transaction mode, but
5740            // PostgreSQL, for historical reasons, does not. We follow
5741            // PostgreSQL in making the comma optional, since that is strictly
5742            // more general.
5743            required = self.consume_token(&Token::Comma);
5744        }
5745        Ok(modes)
5746    }
5747
5748    pub fn parse_commit(&mut self) -> ModalResult<Statement> {
5749        Ok(Statement::Commit {
5750            chain: self.parse_commit_rollback_chain()?,
5751        })
5752    }
5753
5754    pub fn parse_rollback(&mut self) -> ModalResult<Statement> {
5755        Ok(Statement::Rollback {
5756            chain: self.parse_commit_rollback_chain()?,
5757        })
5758    }
5759
5760    pub fn parse_commit_rollback_chain(&mut self) -> ModalResult<bool> {
5761        let _ = self.parse_one_of_keywords(&[Keyword::TRANSACTION, Keyword::WORK]);
5762        if self.parse_keyword(Keyword::AND) {
5763            let chain = !self.parse_keyword(Keyword::NO);
5764            self.expect_keyword(Keyword::CHAIN)?;
5765            Ok(chain)
5766        } else {
5767            Ok(false)
5768        }
5769    }
5770
5771    fn parse_deallocate(&mut self) -> ModalResult<Statement> {
5772        let prepare = self.parse_keyword(Keyword::PREPARE);
5773        let name = self.parse_identifier()?;
5774        Ok(Statement::Deallocate { name, prepare })
5775    }
5776
5777    fn parse_execute(&mut self) -> ModalResult<Statement> {
5778        let name = self.parse_identifier()?;
5779
5780        let mut parameters = vec![];
5781        if self.consume_token(&Token::LParen) {
5782            parameters = self.parse_comma_separated(Parser::parse_expr)?;
5783            self.expect_token(&Token::RParen)?;
5784        }
5785
5786        Ok(Statement::Execute { name, parameters })
5787    }
5788
5789    fn parse_prepare(&mut self) -> ModalResult<Statement> {
5790        let name = self.parse_identifier()?;
5791
5792        let mut data_types = vec![];
5793        if self.consume_token(&Token::LParen) {
5794            data_types = self.parse_comma_separated(Parser::parse_data_type)?;
5795            self.expect_token(&Token::RParen)?;
5796        }
5797
5798        self.expect_keyword(Keyword::AS)?;
5799        let statement = Box::new(self.parse_statement()?);
5800        Ok(Statement::Prepare {
5801            name,
5802            data_types,
5803            statement,
5804        })
5805    }
5806
5807    fn parse_comment(&mut self) -> ModalResult<Statement> {
5808        self.expect_keyword(Keyword::ON)?;
5809        let checkpoint = *self;
5810        let token = self.next_token();
5811
5812        let (object_type, object_name) = match token.token {
5813            Token::Word(w) if w.keyword == Keyword::COLUMN => {
5814                let object_name = self.parse_object_name()?;
5815                (CommentObject::Column, object_name)
5816            }
5817            Token::Word(w) if w.keyword == Keyword::TABLE => {
5818                let object_name = self.parse_object_name()?;
5819                (CommentObject::Table, object_name)
5820            }
5821            _ => self.expected_at(checkpoint, "comment object_type")?,
5822        };
5823
5824        self.expect_keyword(Keyword::IS)?;
5825        let comment = if self.parse_keyword(Keyword::NULL) {
5826            None
5827        } else {
5828            Some(self.parse_literal_string()?)
5829        };
5830        Ok(Statement::Comment {
5831            object_type,
5832            object_name,
5833            comment,
5834        })
5835    }
5836
5837    fn parse_use(&mut self) -> ModalResult<Statement> {
5838        let db_name = self.parse_object_name()?;
5839        Ok(Statement::Use { db_name })
5840    }
5841}
5842
5843impl Word {
5844    /// Convert a Word to a Identifier, return ParserError when the Word's value is a empty string.
5845    pub fn to_ident(&self) -> ModalResult<Ident> {
5846        if self.value.is_empty() {
5847            parser_err!("zero-length delimited identifier at or near \"{self}\"")
5848        } else {
5849            Ok(Ident {
5850                value: self.value.clone(),
5851                quote_style: self.quote_style,
5852            })
5853        }
5854    }
5855}
5856
5857#[cfg(test)]
5858mod tests {
5859    use super::*;
5860    use crate::test_utils::run_parser_method;
5861
5862    #[test]
5863    fn test_parse_integer_min() {
5864        let min_bigint = "-9223372036854775808";
5865        run_parser_method(min_bigint, |parser| {
5866            assert_eq!(
5867                parser.parse_expr().unwrap(),
5868                Expr::Value(Value::Number("-9223372036854775808".to_owned()))
5869            )
5870        });
5871    }
5872}