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