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