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