Thanks to visit codestin.com
Credit goes to docs.rs

sqlparser/parser/
mod.rs

1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License at
4//
5// http://www.apache.org/licenses/LICENSE-2.0
6//
7// Unless required by applicable law or agreed to in writing, software
8// distributed under the License is distributed on an "AS IS" BASIS,
9// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10// See the License for the specific language governing permissions and
11// limitations under the License.
12
13//! SQL Parser
14
15#[cfg(not(feature = "std"))]
16use alloc::{
17    boxed::Box,
18    format,
19    string::{String, ToString},
20    vec,
21    vec::Vec,
22};
23use core::{
24    fmt::{self, Display},
25    str::FromStr,
26};
27use helpers::attached_token::AttachedToken;
28
29use log::debug;
30
31use recursion::RecursionCounter;
32use IsLateral::*;
33use IsOptional::*;
34
35use crate::ast::helpers::stmt_create_table::{CreateTableBuilder, CreateTableConfiguration};
36use crate::ast::Statement::CreatePolicy;
37use crate::ast::*;
38use crate::dialect::*;
39use crate::keywords::{Keyword, ALL_KEYWORDS};
40use crate::tokenizer::*;
41
42mod alter;
43
44#[derive(Debug, Clone, PartialEq, Eq)]
45pub enum ParserError {
46    TokenizerError(String),
47    ParserError(String),
48    RecursionLimitExceeded,
49}
50
51// Use `Parser::expected` instead, if possible
52macro_rules! parser_err {
53    ($MSG:expr, $loc:expr) => {
54        Err(ParserError::ParserError(format!("{}{}", $MSG, $loc)))
55    };
56}
57
58#[cfg(feature = "std")]
59/// Implementation [`RecursionCounter`] if std is available
60mod recursion {
61    use std::cell::Cell;
62    use std::rc::Rc;
63
64    use super::ParserError;
65
66    /// Tracks remaining recursion depth. This value is decremented on
67    /// each call to [`RecursionCounter::try_decrease()`], when it reaches 0 an error will
68    /// be returned.
69    ///
70    /// Note: Uses an [`std::rc::Rc`] and [`std::cell::Cell`] in order to satisfy the Rust
71    /// borrow checker so the automatic [`DepthGuard`] decrement a
72    /// reference to the counter.
73    ///
74    /// Note: when "recursive-protection" feature is enabled, this crate uses additional stack overflow protection
75    /// for some of its recursive methods. See [`recursive::recursive`] for more information.
76    pub(crate) struct RecursionCounter {
77        remaining_depth: Rc<Cell<usize>>,
78    }
79
80    impl RecursionCounter {
81        /// Creates a [`RecursionCounter`] with the specified maximum
82        /// depth
83        pub fn new(remaining_depth: usize) -> Self {
84            Self {
85                remaining_depth: Rc::new(remaining_depth.into()),
86            }
87        }
88
89        /// Decreases the remaining depth by 1.
90        ///
91        /// Returns [`Err`] if the remaining depth falls to 0.
92        ///
93        /// Returns a [`DepthGuard`] which will adds 1 to the
94        /// remaining depth upon drop;
95        pub fn try_decrease(&self) -> Result<DepthGuard, ParserError> {
96            let old_value = self.remaining_depth.get();
97            // ran out of space
98            if old_value == 0 {
99                Err(ParserError::RecursionLimitExceeded)
100            } else {
101                self.remaining_depth.set(old_value - 1);
102                Ok(DepthGuard::new(Rc::clone(&self.remaining_depth)))
103            }
104        }
105    }
106
107    /// Guard that increases the remaining depth by 1 on drop
108    pub struct DepthGuard {
109        remaining_depth: Rc<Cell<usize>>,
110    }
111
112    impl DepthGuard {
113        fn new(remaining_depth: Rc<Cell<usize>>) -> Self {
114            Self { remaining_depth }
115        }
116    }
117    impl Drop for DepthGuard {
118        fn drop(&mut self) {
119            let old_value = self.remaining_depth.get();
120            self.remaining_depth.set(old_value + 1);
121        }
122    }
123}
124
125#[cfg(not(feature = "std"))]
126mod recursion {
127    /// Implementation [`RecursionCounter`] if std is NOT available (and does not
128    /// guard against stack overflow).
129    ///
130    /// Has the same API as the std [`RecursionCounter`] implementation
131    /// but does not actually limit stack depth.
132    pub(crate) struct RecursionCounter {}
133
134    impl RecursionCounter {
135        pub fn new(_remaining_depth: usize) -> Self {
136            Self {}
137        }
138        pub fn try_decrease(&self) -> Result<DepthGuard, super::ParserError> {
139            Ok(DepthGuard {})
140        }
141    }
142
143    pub struct DepthGuard {}
144}
145
146#[derive(PartialEq, Eq)]
147pub enum IsOptional {
148    Optional,
149    Mandatory,
150}
151
152pub enum IsLateral {
153    Lateral,
154    NotLateral,
155}
156
157pub enum WildcardExpr {
158    Expr(Expr),
159    QualifiedWildcard(ObjectName),
160    Wildcard,
161}
162
163impl From<TokenizerError> for ParserError {
164    fn from(e: TokenizerError) -> Self {
165        ParserError::TokenizerError(e.to_string())
166    }
167}
168
169impl fmt::Display for ParserError {
170    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
171        write!(
172            f,
173            "sql parser error: {}",
174            match self {
175                ParserError::TokenizerError(s) => s,
176                ParserError::ParserError(s) => s,
177                ParserError::RecursionLimitExceeded => "recursion limit exceeded",
178            }
179        )
180    }
181}
182
183#[cfg(feature = "std")]
184impl std::error::Error for ParserError {}
185
186// By default, allow expressions up to this deep before erroring
187const DEFAULT_REMAINING_DEPTH: usize = 50;
188
189// A constant EOF token that can be referenced.
190const EOF_TOKEN: TokenWithSpan = TokenWithSpan {
191    token: Token::EOF,
192    span: Span {
193        start: Location { line: 0, column: 0 },
194        end: Location { line: 0, column: 0 },
195    },
196};
197
198/// Composite types declarations using angle brackets syntax can be arbitrary
199/// nested such that the following declaration is possible:
200///      `ARRAY<ARRAY<INT>>`
201/// But the tokenizer recognizes the `>>` as a ShiftRight token.
202/// We work around that limitation when parsing a data type by accepting
203/// either a `>` or `>>` token in such cases, remembering which variant we
204/// matched.
205/// In the latter case having matched a `>>`, the parent type will not look to
206/// match its closing `>` as a result since that will have taken place at the
207/// child type.
208///
209/// See [Parser::parse_data_type] for details
210struct MatchedTrailingBracket(bool);
211
212impl From<bool> for MatchedTrailingBracket {
213    fn from(value: bool) -> Self {
214        Self(value)
215    }
216}
217
218/// Options that control how the [`Parser`] parses SQL text
219#[derive(Debug, Clone, PartialEq, Eq)]
220pub struct ParserOptions {
221    pub trailing_commas: bool,
222    /// Controls how literal values are unescaped. See
223    /// [`Tokenizer::with_unescape`] for more details.
224    pub unescape: bool,
225}
226
227impl Default for ParserOptions {
228    fn default() -> Self {
229        Self {
230            trailing_commas: false,
231            unescape: true,
232        }
233    }
234}
235
236impl ParserOptions {
237    /// Create a new [`ParserOptions`]
238    pub fn new() -> Self {
239        Default::default()
240    }
241
242    /// Set if trailing commas are allowed.
243    ///
244    /// If this option is `false` (the default), the following SQL will
245    /// not parse. If the option is `true`, the SQL will parse.
246    ///
247    /// ```sql
248    ///  SELECT
249    ///   foo,
250    ///   bar,
251    ///  FROM baz
252    /// ```
253    pub fn with_trailing_commas(mut self, trailing_commas: bool) -> Self {
254        self.trailing_commas = trailing_commas;
255        self
256    }
257
258    /// Set if literal values are unescaped. Defaults to true. See
259    /// [`Tokenizer::with_unescape`] for more details.
260    pub fn with_unescape(mut self, unescape: bool) -> Self {
261        self.unescape = unescape;
262        self
263    }
264}
265
266#[derive(Copy, Clone)]
267enum ParserState {
268    /// The default state of the parser.
269    Normal,
270    /// The state when parsing a CONNECT BY expression. This allows parsing
271    /// PRIOR expressions while still allowing prior as an identifier name
272    /// in other contexts.
273    ConnectBy,
274}
275
276/// A SQL Parser
277///
278/// This struct is the main entry point for parsing SQL queries.
279///
280/// # Functionality:
281/// * Parsing SQL: see examples on [`Parser::new`] and [`Parser::parse_sql`]
282/// * Controlling recursion: See [`Parser::with_recursion_limit`]
283/// * Controlling parser options: See [`Parser::with_options`]
284/// * Providing your own tokens: See [`Parser::with_tokens`]
285///
286/// # Internals
287///
288/// The parser uses a [`Tokenizer`] to tokenize the input SQL string into a
289/// `Vec` of [`TokenWithSpan`]s and maintains an `index` to the current token
290/// being processed. The token vec may contain multiple SQL statements.
291///
292/// * The "current" token is the token at `index - 1`
293/// * The "next" token is the token at `index`
294/// * The "previous" token is the token at `index - 2`
295///
296/// If `index` is equal to the length of the token stream, the 'next' token is
297/// [`Token::EOF`].
298///
299/// For example, the SQL string "SELECT * FROM foo" will be tokenized into
300/// following tokens:
301/// ```text
302///  [
303///    "SELECT", // token index 0
304///    " ",      // whitespace
305///    "*",
306///    " ",
307///    "FROM",
308///    " ",
309///    "foo"
310///   ]
311/// ```
312///
313///
314pub struct Parser<'a> {
315    /// The tokens
316    tokens: Vec<TokenWithSpan>,
317    /// The index of the first unprocessed token in [`Parser::tokens`].
318    index: usize,
319    /// The current state of the parser.
320    state: ParserState,
321    /// The SQL dialect to use.
322    dialect: &'a dyn Dialect,
323    /// Additional options that allow you to mix & match behavior
324    /// otherwise constrained to certain dialects (e.g. trailing
325    /// commas) and/or format of parse (e.g. unescaping).
326    options: ParserOptions,
327    /// Ensures the stack does not overflow by limiting recursion depth.
328    recursion_counter: RecursionCounter,
329}
330
331impl<'a> Parser<'a> {
332    /// Create a parser for a [`Dialect`]
333    ///
334    /// See also [`Parser::parse_sql`]
335    ///
336    /// Example:
337    /// ```
338    /// # use sqlparser::{parser::{Parser, ParserError}, dialect::GenericDialect};
339    /// # fn main() -> Result<(), ParserError> {
340    /// let dialect = GenericDialect{};
341    /// let statements = Parser::new(&dialect)
342    ///   .try_with_sql("SELECT * FROM foo")?
343    ///   .parse_statements()?;
344    /// # Ok(())
345    /// # }
346    /// ```
347    pub fn new(dialect: &'a dyn Dialect) -> Self {
348        Self {
349            tokens: vec![],
350            index: 0,
351            state: ParserState::Normal,
352            dialect,
353            recursion_counter: RecursionCounter::new(DEFAULT_REMAINING_DEPTH),
354            options: ParserOptions::new().with_trailing_commas(dialect.supports_trailing_commas()),
355        }
356    }
357
358    /// Specify the maximum recursion limit while parsing.
359    ///
360    /// [`Parser`] prevents stack overflows by returning
361    /// [`ParserError::RecursionLimitExceeded`] if the parser exceeds
362    /// this depth while processing the query.
363    ///
364    /// Example:
365    /// ```
366    /// # use sqlparser::{parser::{Parser, ParserError}, dialect::GenericDialect};
367    /// # fn main() -> Result<(), ParserError> {
368    /// let dialect = GenericDialect{};
369    /// let result = Parser::new(&dialect)
370    ///   .with_recursion_limit(1)
371    ///   .try_with_sql("SELECT * FROM foo WHERE (a OR (b OR (c OR d)))")?
372    ///   .parse_statements();
373    ///   assert_eq!(result, Err(ParserError::RecursionLimitExceeded));
374    /// # Ok(())
375    /// # }
376    /// ```
377    ///
378    /// Note: when "recursive-protection" feature is enabled, this crate uses additional stack overflow protection
379    //  for some of its recursive methods. See [`recursive::recursive`] for more information.
380    pub fn with_recursion_limit(mut self, recursion_limit: usize) -> Self {
381        self.recursion_counter = RecursionCounter::new(recursion_limit);
382        self
383    }
384
385    /// Specify additional parser options
386    ///
387    /// [`Parser`] supports additional options ([`ParserOptions`])
388    /// that allow you to mix & match behavior otherwise constrained
389    /// to certain dialects (e.g. trailing commas).
390    ///
391    /// Example:
392    /// ```
393    /// # use sqlparser::{parser::{Parser, ParserError, ParserOptions}, dialect::GenericDialect};
394    /// # fn main() -> Result<(), ParserError> {
395    /// let dialect = GenericDialect{};
396    /// let options = ParserOptions::new()
397    ///    .with_trailing_commas(true)
398    ///    .with_unescape(false);
399    /// let result = Parser::new(&dialect)
400    ///   .with_options(options)
401    ///   .try_with_sql("SELECT a, b, COUNT(*), FROM foo GROUP BY a, b,")?
402    ///   .parse_statements();
403    ///   assert!(matches!(result, Ok(_)));
404    /// # Ok(())
405    /// # }
406    /// ```
407    pub fn with_options(mut self, options: ParserOptions) -> Self {
408        self.options = options;
409        self
410    }
411
412    /// Reset this parser to parse the specified token stream
413    pub fn with_tokens_with_locations(mut self, tokens: Vec<TokenWithSpan>) -> Self {
414        self.tokens = tokens;
415        self.index = 0;
416        self
417    }
418
419    /// Reset this parser state to parse the specified tokens
420    pub fn with_tokens(self, tokens: Vec<Token>) -> Self {
421        // Put in dummy locations
422        let tokens_with_locations: Vec<TokenWithSpan> = tokens
423            .into_iter()
424            .map(|token| TokenWithSpan {
425                token,
426                span: Span::empty(),
427            })
428            .collect();
429        self.with_tokens_with_locations(tokens_with_locations)
430    }
431
432    /// Tokenize the sql string and sets this [`Parser`]'s state to
433    /// parse the resulting tokens
434    ///
435    /// Returns an error if there was an error tokenizing the SQL string.
436    ///
437    /// See example on [`Parser::new()`] for an example
438    pub fn try_with_sql(self, sql: &str) -> Result<Self, ParserError> {
439        debug!("Parsing sql '{}'...", sql);
440        let tokens = Tokenizer::new(self.dialect, sql)
441            .with_unescape(self.options.unescape)
442            .tokenize_with_location()?;
443        Ok(self.with_tokens_with_locations(tokens))
444    }
445
446    /// Parse potentially multiple statements
447    ///
448    /// Example
449    /// ```
450    /// # use sqlparser::{parser::{Parser, ParserError}, dialect::GenericDialect};
451    /// # fn main() -> Result<(), ParserError> {
452    /// let dialect = GenericDialect{};
453    /// let statements = Parser::new(&dialect)
454    ///   // Parse a SQL string with 2 separate statements
455    ///   .try_with_sql("SELECT * FROM foo; SELECT * FROM bar;")?
456    ///   .parse_statements()?;
457    /// assert_eq!(statements.len(), 2);
458    /// # Ok(())
459    /// # }
460    /// ```
461    pub fn parse_statements(&mut self) -> Result<Vec<Statement>, ParserError> {
462        let mut stmts = Vec::new();
463        let mut expecting_statement_delimiter = false;
464        loop {
465            // ignore empty statements (between successive statement delimiters)
466            while self.consume_token(&Token::SemiColon) {
467                expecting_statement_delimiter = false;
468            }
469
470            match self.peek_token().token {
471                Token::EOF => break,
472
473                // end of statement
474                Token::Word(word) => {
475                    if expecting_statement_delimiter && word.keyword == Keyword::END {
476                        break;
477                    }
478                }
479                _ => {}
480            }
481
482            if expecting_statement_delimiter {
483                return self.expected("end of statement", self.peek_token());
484            }
485
486            let statement = self.parse_statement()?;
487            stmts.push(statement);
488            expecting_statement_delimiter = true;
489        }
490        Ok(stmts)
491    }
492
493    /// Convenience method to parse a string with one or more SQL
494    /// statements into produce an Abstract Syntax Tree (AST).
495    ///
496    /// Example
497    /// ```
498    /// # use sqlparser::{parser::{Parser, ParserError}, dialect::GenericDialect};
499    /// # fn main() -> Result<(), ParserError> {
500    /// let dialect = GenericDialect{};
501    /// let statements = Parser::parse_sql(
502    ///   &dialect, "SELECT * FROM foo"
503    /// )?;
504    /// assert_eq!(statements.len(), 1);
505    /// # Ok(())
506    /// # }
507    /// ```
508    pub fn parse_sql(dialect: &dyn Dialect, sql: &str) -> Result<Vec<Statement>, ParserError> {
509        Parser::new(dialect).try_with_sql(sql)?.parse_statements()
510    }
511
512    /// Parse a single top-level statement (such as SELECT, INSERT, CREATE, etc.),
513    /// stopping before the statement separator, if any.
514    pub fn parse_statement(&mut self) -> Result<Statement, ParserError> {
515        let _guard = self.recursion_counter.try_decrease()?;
516
517        // allow the dialect to override statement parsing
518        if let Some(statement) = self.dialect.parse_statement(self) {
519            return statement;
520        }
521
522        let next_token = self.next_token();
523        match &next_token.token {
524            Token::Word(w) => match w.keyword {
525                Keyword::KILL => self.parse_kill(),
526                Keyword::FLUSH => self.parse_flush(),
527                Keyword::DESC => self.parse_explain(DescribeAlias::Desc),
528                Keyword::DESCRIBE => self.parse_explain(DescribeAlias::Describe),
529                Keyword::EXPLAIN => self.parse_explain(DescribeAlias::Explain),
530                Keyword::ANALYZE => self.parse_analyze(),
531                Keyword::CASE => {
532                    self.prev_token();
533                    self.parse_case_stmt()
534                }
535                Keyword::IF => {
536                    self.prev_token();
537                    self.parse_if_stmt()
538                }
539                Keyword::RAISE => {
540                    self.prev_token();
541                    self.parse_raise_stmt()
542                }
543                Keyword::SELECT | Keyword::WITH | Keyword::VALUES | Keyword::FROM => {
544                    self.prev_token();
545                    self.parse_query().map(Statement::Query)
546                }
547                Keyword::TRUNCATE => self.parse_truncate(),
548                Keyword::ATTACH => {
549                    if dialect_of!(self is DuckDbDialect) {
550                        self.parse_attach_duckdb_database()
551                    } else {
552                        self.parse_attach_database()
553                    }
554                }
555                Keyword::DETACH if dialect_of!(self is DuckDbDialect | GenericDialect) => {
556                    self.parse_detach_duckdb_database()
557                }
558                Keyword::MSCK => self.parse_msck(),
559                Keyword::CREATE => self.parse_create(),
560                Keyword::CACHE => self.parse_cache_table(),
561                Keyword::DROP => self.parse_drop(),
562                Keyword::DISCARD => self.parse_discard(),
563                Keyword::DECLARE => self.parse_declare(),
564                Keyword::FETCH => self.parse_fetch_statement(),
565                Keyword::DELETE => self.parse_delete(),
566                Keyword::INSERT => self.parse_insert(),
567                Keyword::REPLACE => self.parse_replace(),
568                Keyword::UNCACHE => self.parse_uncache_table(),
569                Keyword::UPDATE => self.parse_update(),
570                Keyword::ALTER => self.parse_alter(),
571                Keyword::CALL => self.parse_call(),
572                Keyword::COPY => self.parse_copy(),
573                Keyword::CLOSE => self.parse_close(),
574                Keyword::SET => self.parse_set(),
575                Keyword::SHOW => self.parse_show(),
576                Keyword::USE => self.parse_use(),
577                Keyword::GRANT => self.parse_grant(),
578                Keyword::REVOKE => self.parse_revoke(),
579                Keyword::START => self.parse_start_transaction(),
580                Keyword::BEGIN => self.parse_begin(),
581                Keyword::END => self.parse_end(),
582                Keyword::SAVEPOINT => self.parse_savepoint(),
583                Keyword::RELEASE => self.parse_release(),
584                Keyword::COMMIT => self.parse_commit(),
585                Keyword::RAISERROR => Ok(self.parse_raiserror()?),
586                Keyword::ROLLBACK => self.parse_rollback(),
587                Keyword::ASSERT => self.parse_assert(),
588                // `PREPARE`, `EXECUTE` and `DEALLOCATE` are Postgres-specific
589                // syntaxes. They are used for Postgres prepared statement.
590                Keyword::DEALLOCATE => self.parse_deallocate(),
591                Keyword::EXECUTE | Keyword::EXEC => self.parse_execute(),
592                Keyword::PREPARE => self.parse_prepare(),
593                Keyword::MERGE => self.parse_merge(),
594                // `LISTEN`, `UNLISTEN` and `NOTIFY` are Postgres-specific
595                // syntaxes. They are used for Postgres statement.
596                Keyword::LISTEN if self.dialect.supports_listen_notify() => self.parse_listen(),
597                Keyword::UNLISTEN if self.dialect.supports_listen_notify() => self.parse_unlisten(),
598                Keyword::NOTIFY if self.dialect.supports_listen_notify() => self.parse_notify(),
599                // `PRAGMA` is sqlite specific https://www.sqlite.org/pragma.html
600                Keyword::PRAGMA => self.parse_pragma(),
601                Keyword::UNLOAD => self.parse_unload(),
602                Keyword::RENAME => self.parse_rename(),
603                // `INSTALL` is duckdb specific https://duckdb.org/docs/extensions/overview
604                Keyword::INSTALL if dialect_of!(self is DuckDbDialect | GenericDialect) => {
605                    self.parse_install()
606                }
607                Keyword::LOAD => self.parse_load(),
608                // `OPTIMIZE` is clickhouse specific https://clickhouse.tech/docs/en/sql-reference/statements/optimize/
609                Keyword::OPTIMIZE if dialect_of!(self is ClickHouseDialect | GenericDialect) => {
610                    self.parse_optimize_table()
611                }
612                // `COMMENT` is snowflake specific https://docs.snowflake.com/en/sql-reference/sql/comment
613                Keyword::COMMENT if self.dialect.supports_comment_on() => self.parse_comment(),
614                Keyword::PRINT => self.parse_print(),
615                Keyword::RETURN => self.parse_return(),
616                _ => self.expected("an SQL statement", next_token),
617            },
618            Token::LParen => {
619                self.prev_token();
620                self.parse_query().map(Statement::Query)
621            }
622            _ => self.expected("an SQL statement", next_token),
623        }
624    }
625
626    /// Parse a `CASE` statement.
627    ///
628    /// See [Statement::Case]
629    pub fn parse_case_stmt(&mut self) -> Result<Statement, ParserError> {
630        let case_token = self.expect_keyword(Keyword::CASE)?;
631
632        let match_expr = if self.peek_keyword(Keyword::WHEN) {
633            None
634        } else {
635            Some(self.parse_expr()?)
636        };
637
638        self.expect_keyword_is(Keyword::WHEN)?;
639        let when_blocks = self.parse_keyword_separated(Keyword::WHEN, |parser| {
640            parser.parse_conditional_statement_block(&[Keyword::WHEN, Keyword::ELSE, Keyword::END])
641        })?;
642
643        let else_block = if self.parse_keyword(Keyword::ELSE) {
644            Some(self.parse_conditional_statement_block(&[Keyword::END])?)
645        } else {
646            None
647        };
648
649        let mut end_case_token = self.expect_keyword(Keyword::END)?;
650        if self.peek_keyword(Keyword::CASE) {
651            end_case_token = self.expect_keyword(Keyword::CASE)?;
652        }
653
654        Ok(Statement::Case(CaseStatement {
655            case_token: AttachedToken(case_token),
656            match_expr,
657            when_blocks,
658            else_block,
659            end_case_token: AttachedToken(end_case_token),
660        }))
661    }
662
663    /// Parse an `IF` statement.
664    ///
665    /// See [Statement::If]
666    pub fn parse_if_stmt(&mut self) -> Result<Statement, ParserError> {
667        self.expect_keyword_is(Keyword::IF)?;
668        let if_block = self.parse_conditional_statement_block(&[
669            Keyword::ELSE,
670            Keyword::ELSEIF,
671            Keyword::END,
672        ])?;
673
674        let elseif_blocks = if self.parse_keyword(Keyword::ELSEIF) {
675            self.parse_keyword_separated(Keyword::ELSEIF, |parser| {
676                parser.parse_conditional_statement_block(&[
677                    Keyword::ELSEIF,
678                    Keyword::ELSE,
679                    Keyword::END,
680                ])
681            })?
682        } else {
683            vec![]
684        };
685
686        let else_block = if self.parse_keyword(Keyword::ELSE) {
687            Some(self.parse_conditional_statement_block(&[Keyword::END])?)
688        } else {
689            None
690        };
691
692        self.expect_keyword_is(Keyword::END)?;
693        let end_token = self.expect_keyword(Keyword::IF)?;
694
695        Ok(Statement::If(IfStatement {
696            if_block,
697            elseif_blocks,
698            else_block,
699            end_token: Some(AttachedToken(end_token)),
700        }))
701    }
702
703    /// Parses an expression and associated list of statements
704    /// belonging to a conditional statement like `IF` or `WHEN`.
705    ///
706    /// Example:
707    /// ```sql
708    /// IF condition THEN statement1; statement2;
709    /// ```
710    fn parse_conditional_statement_block(
711        &mut self,
712        terminal_keywords: &[Keyword],
713    ) -> Result<ConditionalStatementBlock, ParserError> {
714        let start_token = self.get_current_token().clone(); // self.expect_keyword(keyword)?;
715        let mut then_token = None;
716
717        let condition = match &start_token.token {
718            Token::Word(w) if w.keyword == Keyword::ELSE => None,
719            _ => {
720                let expr = self.parse_expr()?;
721                then_token = Some(AttachedToken(self.expect_keyword(Keyword::THEN)?));
722                Some(expr)
723            }
724        };
725
726        let statements = self.parse_statement_list(terminal_keywords)?;
727
728        Ok(ConditionalStatementBlock {
729            start_token: AttachedToken(start_token),
730            condition,
731            then_token,
732            conditional_statements: ConditionalStatements::Sequence { statements },
733        })
734    }
735
736    /// Parse a `RAISE` statement.
737    ///
738    /// See [Statement::Raise]
739    pub fn parse_raise_stmt(&mut self) -> Result<Statement, ParserError> {
740        self.expect_keyword_is(Keyword::RAISE)?;
741
742        let value = if self.parse_keywords(&[Keyword::USING, Keyword::MESSAGE]) {
743            self.expect_token(&Token::Eq)?;
744            Some(RaiseStatementValue::UsingMessage(self.parse_expr()?))
745        } else {
746            self.maybe_parse(|parser| parser.parse_expr().map(RaiseStatementValue::Expr))?
747        };
748
749        Ok(Statement::Raise(RaiseStatement { value }))
750    }
751
752    pub fn parse_comment(&mut self) -> Result<Statement, ParserError> {
753        let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
754
755        self.expect_keyword_is(Keyword::ON)?;
756        let token = self.next_token();
757
758        let (object_type, object_name) = match token.token {
759            Token::Word(w) if w.keyword == Keyword::COLUMN => {
760                (CommentObject::Column, self.parse_object_name(false)?)
761            }
762            Token::Word(w) if w.keyword == Keyword::TABLE => {
763                (CommentObject::Table, self.parse_object_name(false)?)
764            }
765            Token::Word(w) if w.keyword == Keyword::EXTENSION => {
766                (CommentObject::Extension, self.parse_object_name(false)?)
767            }
768            Token::Word(w) if w.keyword == Keyword::SCHEMA => {
769                (CommentObject::Schema, self.parse_object_name(false)?)
770            }
771            Token::Word(w) if w.keyword == Keyword::DATABASE => {
772                (CommentObject::Database, self.parse_object_name(false)?)
773            }
774            Token::Word(w) if w.keyword == Keyword::USER => {
775                (CommentObject::User, self.parse_object_name(false)?)
776            }
777            Token::Word(w) if w.keyword == Keyword::ROLE => {
778                (CommentObject::Role, self.parse_object_name(false)?)
779            }
780            _ => self.expected("comment object_type", token)?,
781        };
782
783        self.expect_keyword_is(Keyword::IS)?;
784        let comment = if self.parse_keyword(Keyword::NULL) {
785            None
786        } else {
787            Some(self.parse_literal_string()?)
788        };
789        Ok(Statement::Comment {
790            object_type,
791            object_name,
792            comment,
793            if_exists,
794        })
795    }
796
797    pub fn parse_flush(&mut self) -> Result<Statement, ParserError> {
798        let mut channel = None;
799        let mut tables: Vec<ObjectName> = vec![];
800        let mut read_lock = false;
801        let mut export = false;
802
803        if !dialect_of!(self is MySqlDialect | GenericDialect) {
804            return parser_err!("Unsupported statement FLUSH", self.peek_token().span.start);
805        }
806
807        let location = if self.parse_keyword(Keyword::NO_WRITE_TO_BINLOG) {
808            Some(FlushLocation::NoWriteToBinlog)
809        } else if self.parse_keyword(Keyword::LOCAL) {
810            Some(FlushLocation::Local)
811        } else {
812            None
813        };
814
815        let object_type = if self.parse_keywords(&[Keyword::BINARY, Keyword::LOGS]) {
816            FlushType::BinaryLogs
817        } else if self.parse_keywords(&[Keyword::ENGINE, Keyword::LOGS]) {
818            FlushType::EngineLogs
819        } else if self.parse_keywords(&[Keyword::ERROR, Keyword::LOGS]) {
820            FlushType::ErrorLogs
821        } else if self.parse_keywords(&[Keyword::GENERAL, Keyword::LOGS]) {
822            FlushType::GeneralLogs
823        } else if self.parse_keywords(&[Keyword::HOSTS]) {
824            FlushType::Hosts
825        } else if self.parse_keyword(Keyword::PRIVILEGES) {
826            FlushType::Privileges
827        } else if self.parse_keyword(Keyword::OPTIMIZER_COSTS) {
828            FlushType::OptimizerCosts
829        } else if self.parse_keywords(&[Keyword::RELAY, Keyword::LOGS]) {
830            if self.parse_keywords(&[Keyword::FOR, Keyword::CHANNEL]) {
831                channel = Some(self.parse_object_name(false).unwrap().to_string());
832            }
833            FlushType::RelayLogs
834        } else if self.parse_keywords(&[Keyword::SLOW, Keyword::LOGS]) {
835            FlushType::SlowLogs
836        } else if self.parse_keyword(Keyword::STATUS) {
837            FlushType::Status
838        } else if self.parse_keyword(Keyword::USER_RESOURCES) {
839            FlushType::UserResources
840        } else if self.parse_keywords(&[Keyword::LOGS]) {
841            FlushType::Logs
842        } else if self.parse_keywords(&[Keyword::TABLES]) {
843            loop {
844                let next_token = self.next_token();
845                match &next_token.token {
846                    Token::Word(w) => match w.keyword {
847                        Keyword::WITH => {
848                            read_lock = self.parse_keywords(&[Keyword::READ, Keyword::LOCK]);
849                        }
850                        Keyword::FOR => {
851                            export = self.parse_keyword(Keyword::EXPORT);
852                        }
853                        Keyword::NoKeyword => {
854                            self.prev_token();
855                            tables = self.parse_comma_separated(|p| p.parse_object_name(false))?;
856                        }
857                        _ => {}
858                    },
859                    _ => {
860                        break;
861                    }
862                }
863            }
864
865            FlushType::Tables
866        } else {
867            return self.expected(
868                "BINARY LOGS, ENGINE LOGS, ERROR LOGS, GENERAL LOGS, HOSTS, LOGS, PRIVILEGES, OPTIMIZER_COSTS,\
869                 RELAY LOGS [FOR CHANNEL channel], SLOW LOGS, STATUS, USER_RESOURCES",
870                self.peek_token(),
871            );
872        };
873
874        Ok(Statement::Flush {
875            object_type,
876            location,
877            channel,
878            read_lock,
879            export,
880            tables,
881        })
882    }
883
884    pub fn parse_msck(&mut self) -> Result<Statement, ParserError> {
885        let repair = self.parse_keyword(Keyword::REPAIR);
886        self.expect_keyword_is(Keyword::TABLE)?;
887        let table_name = self.parse_object_name(false)?;
888        let partition_action = self
889            .maybe_parse(|parser| {
890                let pa = match parser.parse_one_of_keywords(&[
891                    Keyword::ADD,
892                    Keyword::DROP,
893                    Keyword::SYNC,
894                ]) {
895                    Some(Keyword::ADD) => Some(AddDropSync::ADD),
896                    Some(Keyword::DROP) => Some(AddDropSync::DROP),
897                    Some(Keyword::SYNC) => Some(AddDropSync::SYNC),
898                    _ => None,
899                };
900                parser.expect_keyword_is(Keyword::PARTITIONS)?;
901                Ok(pa)
902            })?
903            .unwrap_or_default();
904        Ok(Statement::Msck {
905            repair,
906            table_name,
907            partition_action,
908        })
909    }
910
911    pub fn parse_truncate(&mut self) -> Result<Statement, ParserError> {
912        let table = self.parse_keyword(Keyword::TABLE);
913        let only = self.parse_keyword(Keyword::ONLY);
914
915        let table_names = self
916            .parse_comma_separated(|p| p.parse_object_name(false))?
917            .into_iter()
918            .map(|n| TruncateTableTarget { name: n })
919            .collect();
920
921        let mut partitions = None;
922        if self.parse_keyword(Keyword::PARTITION) {
923            self.expect_token(&Token::LParen)?;
924            partitions = Some(self.parse_comma_separated(Parser::parse_expr)?);
925            self.expect_token(&Token::RParen)?;
926        }
927
928        let mut identity = None;
929        let mut cascade = None;
930
931        if dialect_of!(self is PostgreSqlDialect | GenericDialect) {
932            identity = if self.parse_keywords(&[Keyword::RESTART, Keyword::IDENTITY]) {
933                Some(TruncateIdentityOption::Restart)
934            } else if self.parse_keywords(&[Keyword::CONTINUE, Keyword::IDENTITY]) {
935                Some(TruncateIdentityOption::Continue)
936            } else {
937                None
938            };
939
940            cascade = self.parse_cascade_option();
941        };
942
943        let on_cluster = self.parse_optional_on_cluster()?;
944
945        Ok(Statement::Truncate {
946            table_names,
947            partitions,
948            table,
949            only,
950            identity,
951            cascade,
952            on_cluster,
953        })
954    }
955
956    fn parse_cascade_option(&mut self) -> Option<CascadeOption> {
957        if self.parse_keyword(Keyword::CASCADE) {
958            Some(CascadeOption::Cascade)
959        } else if self.parse_keyword(Keyword::RESTRICT) {
960            Some(CascadeOption::Restrict)
961        } else {
962            None
963        }
964    }
965
966    pub fn parse_attach_duckdb_database_options(
967        &mut self,
968    ) -> Result<Vec<AttachDuckDBDatabaseOption>, ParserError> {
969        if !self.consume_token(&Token::LParen) {
970            return Ok(vec![]);
971        }
972
973        let mut options = vec![];
974        loop {
975            if self.parse_keyword(Keyword::READ_ONLY) {
976                let boolean = if self.parse_keyword(Keyword::TRUE) {
977                    Some(true)
978                } else if self.parse_keyword(Keyword::FALSE) {
979                    Some(false)
980                } else {
981                    None
982                };
983                options.push(AttachDuckDBDatabaseOption::ReadOnly(boolean));
984            } else if self.parse_keyword(Keyword::TYPE) {
985                let ident = self.parse_identifier()?;
986                options.push(AttachDuckDBDatabaseOption::Type(ident));
987            } else {
988                return self.expected("expected one of: ), READ_ONLY, TYPE", self.peek_token());
989            };
990
991            if self.consume_token(&Token::RParen) {
992                return Ok(options);
993            } else if self.consume_token(&Token::Comma) {
994                continue;
995            } else {
996                return self.expected("expected one of: ')', ','", self.peek_token());
997            }
998        }
999    }
1000
1001    pub fn parse_attach_duckdb_database(&mut self) -> Result<Statement, ParserError> {
1002        let database = self.parse_keyword(Keyword::DATABASE);
1003        let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
1004        let database_path = self.parse_identifier()?;
1005        let database_alias = if self.parse_keyword(Keyword::AS) {
1006            Some(self.parse_identifier()?)
1007        } else {
1008            None
1009        };
1010
1011        let attach_options = self.parse_attach_duckdb_database_options()?;
1012        Ok(Statement::AttachDuckDBDatabase {
1013            if_not_exists,
1014            database,
1015            database_path,
1016            database_alias,
1017            attach_options,
1018        })
1019    }
1020
1021    pub fn parse_detach_duckdb_database(&mut self) -> Result<Statement, ParserError> {
1022        let database = self.parse_keyword(Keyword::DATABASE);
1023        let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
1024        let database_alias = self.parse_identifier()?;
1025        Ok(Statement::DetachDuckDBDatabase {
1026            if_exists,
1027            database,
1028            database_alias,
1029        })
1030    }
1031
1032    pub fn parse_attach_database(&mut self) -> Result<Statement, ParserError> {
1033        let database = self.parse_keyword(Keyword::DATABASE);
1034        let database_file_name = self.parse_expr()?;
1035        self.expect_keyword_is(Keyword::AS)?;
1036        let schema_name = self.parse_identifier()?;
1037        Ok(Statement::AttachDatabase {
1038            database,
1039            schema_name,
1040            database_file_name,
1041        })
1042    }
1043
1044    pub fn parse_analyze(&mut self) -> Result<Statement, ParserError> {
1045        let has_table_keyword = self.parse_keyword(Keyword::TABLE);
1046        let table_name = self.parse_object_name(false)?;
1047        let mut for_columns = false;
1048        let mut cache_metadata = false;
1049        let mut noscan = false;
1050        let mut partitions = None;
1051        let mut compute_statistics = false;
1052        let mut columns = vec![];
1053        loop {
1054            match self.parse_one_of_keywords(&[
1055                Keyword::PARTITION,
1056                Keyword::FOR,
1057                Keyword::CACHE,
1058                Keyword::NOSCAN,
1059                Keyword::COMPUTE,
1060            ]) {
1061                Some(Keyword::PARTITION) => {
1062                    self.expect_token(&Token::LParen)?;
1063                    partitions = Some(self.parse_comma_separated(Parser::parse_expr)?);
1064                    self.expect_token(&Token::RParen)?;
1065                }
1066                Some(Keyword::NOSCAN) => noscan = true,
1067                Some(Keyword::FOR) => {
1068                    self.expect_keyword_is(Keyword::COLUMNS)?;
1069
1070                    columns = self
1071                        .maybe_parse(|parser| {
1072                            parser.parse_comma_separated(|p| p.parse_identifier())
1073                        })?
1074                        .unwrap_or_default();
1075                    for_columns = true
1076                }
1077                Some(Keyword::CACHE) => {
1078                    self.expect_keyword_is(Keyword::METADATA)?;
1079                    cache_metadata = true
1080                }
1081                Some(Keyword::COMPUTE) => {
1082                    self.expect_keyword_is(Keyword::STATISTICS)?;
1083                    compute_statistics = true
1084                }
1085                _ => break,
1086            }
1087        }
1088
1089        Ok(Statement::Analyze {
1090            has_table_keyword,
1091            table_name,
1092            for_columns,
1093            columns,
1094            partitions,
1095            cache_metadata,
1096            noscan,
1097            compute_statistics,
1098        })
1099    }
1100
1101    /// Parse a new expression including wildcard & qualified wildcard.
1102    pub fn parse_wildcard_expr(&mut self) -> Result<Expr, ParserError> {
1103        let index = self.index;
1104
1105        let next_token = self.next_token();
1106        match next_token.token {
1107            t @ (Token::Word(_) | Token::SingleQuotedString(_)) => {
1108                if self.peek_token().token == Token::Period {
1109                    let mut id_parts: Vec<Ident> = vec![match t {
1110                        Token::Word(w) => w.into_ident(next_token.span),
1111                        Token::SingleQuotedString(s) => Ident::with_quote('\'', s),
1112                        _ => unreachable!(), // We matched above
1113                    }];
1114
1115                    while self.consume_token(&Token::Period) {
1116                        let next_token = self.next_token();
1117                        match next_token.token {
1118                            Token::Word(w) => id_parts.push(w.into_ident(next_token.span)),
1119                            Token::SingleQuotedString(s) => {
1120                                // SQLite has single-quoted identifiers
1121                                id_parts.push(Ident::with_quote('\'', s))
1122                            }
1123                            Token::Mul => {
1124                                return Ok(Expr::QualifiedWildcard(
1125                                    ObjectName::from(id_parts),
1126                                    AttachedToken(next_token),
1127                                ));
1128                            }
1129                            _ => {
1130                                return self
1131                                    .expected("an identifier or a '*' after '.'", next_token);
1132                            }
1133                        }
1134                    }
1135                }
1136            }
1137            Token::Mul => {
1138                return Ok(Expr::Wildcard(AttachedToken(next_token)));
1139            }
1140            _ => (),
1141        };
1142
1143        self.index = index;
1144        self.parse_expr()
1145    }
1146
1147    /// Parse a new expression.
1148    pub fn parse_expr(&mut self) -> Result<Expr, ParserError> {
1149        self.parse_subexpr(self.dialect.prec_unknown())
1150    }
1151
1152    /// Parse tokens until the precedence changes.
1153    pub fn parse_subexpr(&mut self, precedence: u8) -> Result<Expr, ParserError> {
1154        let _guard = self.recursion_counter.try_decrease()?;
1155        debug!("parsing expr");
1156        let mut expr = self.parse_prefix()?;
1157
1158        expr = self.parse_compound_expr(expr, vec![])?;
1159
1160        debug!("prefix: {:?}", expr);
1161        loop {
1162            let next_precedence = self.get_next_precedence()?;
1163            debug!("next precedence: {:?}", next_precedence);
1164
1165            if precedence >= next_precedence {
1166                break;
1167            }
1168
1169            // The period operator is handled exclusively by the
1170            // compound field access parsing.
1171            if Token::Period == self.peek_token_ref().token {
1172                break;
1173            }
1174
1175            expr = self.parse_infix(expr, next_precedence)?;
1176        }
1177        Ok(expr)
1178    }
1179
1180    pub fn parse_assert(&mut self) -> Result<Statement, ParserError> {
1181        let condition = self.parse_expr()?;
1182        let message = if self.parse_keyword(Keyword::AS) {
1183            Some(self.parse_expr()?)
1184        } else {
1185            None
1186        };
1187
1188        Ok(Statement::Assert { condition, message })
1189    }
1190
1191    pub fn parse_savepoint(&mut self) -> Result<Statement, ParserError> {
1192        let name = self.parse_identifier()?;
1193        Ok(Statement::Savepoint { name })
1194    }
1195
1196    pub fn parse_release(&mut self) -> Result<Statement, ParserError> {
1197        let _ = self.parse_keyword(Keyword::SAVEPOINT);
1198        let name = self.parse_identifier()?;
1199
1200        Ok(Statement::ReleaseSavepoint { name })
1201    }
1202
1203    pub fn parse_listen(&mut self) -> Result<Statement, ParserError> {
1204        let channel = self.parse_identifier()?;
1205        Ok(Statement::LISTEN { channel })
1206    }
1207
1208    pub fn parse_unlisten(&mut self) -> Result<Statement, ParserError> {
1209        let channel = if self.consume_token(&Token::Mul) {
1210            Ident::new(Expr::Wildcard(AttachedToken::empty()).to_string())
1211        } else {
1212            match self.parse_identifier() {
1213                Ok(expr) => expr,
1214                _ => {
1215                    self.prev_token();
1216                    return self.expected("wildcard or identifier", self.peek_token());
1217                }
1218            }
1219        };
1220        Ok(Statement::UNLISTEN { channel })
1221    }
1222
1223    pub fn parse_notify(&mut self) -> Result<Statement, ParserError> {
1224        let channel = self.parse_identifier()?;
1225        let payload = if self.consume_token(&Token::Comma) {
1226            Some(self.parse_literal_string()?)
1227        } else {
1228            None
1229        };
1230        Ok(Statement::NOTIFY { channel, payload })
1231    }
1232
1233    /// Parses a `RENAME TABLE` statement. See [Statement::RenameTable]
1234    pub fn parse_rename(&mut self) -> Result<Statement, ParserError> {
1235        if self.peek_keyword(Keyword::TABLE) {
1236            self.expect_keyword(Keyword::TABLE)?;
1237            let rename_tables = self.parse_comma_separated(|parser| {
1238                let old_name = parser.parse_object_name(false)?;
1239                parser.expect_keyword(Keyword::TO)?;
1240                let new_name = parser.parse_object_name(false)?;
1241
1242                Ok(RenameTable { old_name, new_name })
1243            })?;
1244            Ok(Statement::RenameTable(rename_tables))
1245        } else {
1246            self.expected("KEYWORD `TABLE` after RENAME", self.peek_token())
1247        }
1248    }
1249
1250    /// Tries to parse an expression by matching the specified word to known keywords that have a special meaning in the dialect.
1251    /// Returns `None if no match is found.
1252    fn parse_expr_prefix_by_reserved_word(
1253        &mut self,
1254        w: &Word,
1255        w_span: Span,
1256    ) -> Result<Option<Expr>, ParserError> {
1257        match w.keyword {
1258            Keyword::TRUE | Keyword::FALSE if self.dialect.supports_boolean_literals() => {
1259                self.prev_token();
1260                Ok(Some(Expr::Value(self.parse_value()?)))
1261            }
1262            Keyword::NULL => {
1263                self.prev_token();
1264                Ok(Some(Expr::Value(self.parse_value()?)))
1265            }
1266            Keyword::CURRENT_CATALOG
1267            | Keyword::CURRENT_USER
1268            | Keyword::SESSION_USER
1269            | Keyword::USER
1270            if dialect_of!(self is PostgreSqlDialect | GenericDialect) =>
1271                {
1272                    Ok(Some(Expr::Function(Function {
1273                        name: ObjectName::from(vec![w.clone().into_ident(w_span)]),
1274                        uses_odbc_syntax: false,
1275                        parameters: FunctionArguments::None,
1276                        args: FunctionArguments::None,
1277                        null_treatment: None,
1278                        filter: None,
1279                        over: None,
1280                        within_group: vec![],
1281                    })))
1282                }
1283            Keyword::CURRENT_TIMESTAMP
1284            | Keyword::CURRENT_TIME
1285            | Keyword::CURRENT_DATE
1286            | Keyword::LOCALTIME
1287            | Keyword::LOCALTIMESTAMP => {
1288                Ok(Some(self.parse_time_functions(ObjectName::from(vec![w.clone().into_ident(w_span)]))?))
1289            }
1290            Keyword::CASE => Ok(Some(self.parse_case_expr()?)),
1291            Keyword::CONVERT => Ok(Some(self.parse_convert_expr(false)?)),
1292            Keyword::TRY_CONVERT if self.dialect.supports_try_convert() => Ok(Some(self.parse_convert_expr(true)?)),
1293            Keyword::CAST => Ok(Some(self.parse_cast_expr(CastKind::Cast)?)),
1294            Keyword::TRY_CAST => Ok(Some(self.parse_cast_expr(CastKind::TryCast)?)),
1295            Keyword::SAFE_CAST => Ok(Some(self.parse_cast_expr(CastKind::SafeCast)?)),
1296            Keyword::EXISTS
1297            // Support parsing Databricks has a function named `exists`.
1298            if !dialect_of!(self is DatabricksDialect)
1299                || matches!(
1300                        self.peek_nth_token_ref(1).token,
1301                        Token::Word(Word {
1302                            keyword: Keyword::SELECT | Keyword::WITH,
1303                            ..
1304                        })
1305                    ) =>
1306                {
1307                    Ok(Some(self.parse_exists_expr(false)?))
1308                }
1309            Keyword::EXTRACT => Ok(Some(self.parse_extract_expr()?)),
1310            Keyword::CEIL => Ok(Some(self.parse_ceil_floor_expr(true)?)),
1311            Keyword::FLOOR => Ok(Some(self.parse_ceil_floor_expr(false)?)),
1312            Keyword::POSITION if self.peek_token_ref().token == Token::LParen => {
1313                Ok(Some(self.parse_position_expr(w.clone().into_ident(w_span))?))
1314            }
1315            Keyword::SUBSTR | Keyword::SUBSTRING => {
1316                self.prev_token();
1317                Ok(Some(self.parse_substring()?))
1318            }
1319            Keyword::OVERLAY => Ok(Some(self.parse_overlay_expr()?)),
1320            Keyword::TRIM => Ok(Some(self.parse_trim_expr()?)),
1321            Keyword::INTERVAL => Ok(Some(self.parse_interval()?)),
1322            // Treat ARRAY[1,2,3] as an array [1,2,3], otherwise try as subquery or a function call
1323            Keyword::ARRAY if *self.peek_token_ref() == Token::LBracket => {
1324                self.expect_token(&Token::LBracket)?;
1325                Ok(Some(self.parse_array_expr(true)?))
1326            }
1327            Keyword::ARRAY
1328            if self.peek_token() == Token::LParen
1329                && !dialect_of!(self is ClickHouseDialect | DatabricksDialect) =>
1330                {
1331                    self.expect_token(&Token::LParen)?;
1332                    let query = self.parse_query()?;
1333                    self.expect_token(&Token::RParen)?;
1334                    Ok(Some(Expr::Function(Function {
1335                        name: ObjectName::from(vec![w.clone().into_ident(w_span)]),
1336                        uses_odbc_syntax: false,
1337                        parameters: FunctionArguments::None,
1338                        args: FunctionArguments::Subquery(query),
1339                        filter: None,
1340                        null_treatment: None,
1341                        over: None,
1342                        within_group: vec![],
1343                    })))
1344                }
1345            Keyword::NOT => Ok(Some(self.parse_not()?)),
1346            Keyword::MATCH if self.dialect.supports_match_against() => {
1347                Ok(Some(self.parse_match_against()?))
1348            }
1349            Keyword::STRUCT if self.dialect.supports_struct_literal() => {
1350                let struct_expr = self.parse_struct_literal()?;
1351                Ok(Some(struct_expr))
1352            }
1353            Keyword::PRIOR if matches!(self.state, ParserState::ConnectBy) => {
1354                let expr = self.parse_subexpr(self.dialect.prec_value(Precedence::PlusMinus))?;
1355                Ok(Some(Expr::Prior(Box::new(expr))))
1356            }
1357            Keyword::MAP if *self.peek_token_ref() == Token::LBrace && self.dialect.support_map_literal_syntax() => {
1358                Ok(Some(self.parse_duckdb_map_literal()?))
1359            }
1360            _ if self.dialect.supports_geometric_types() => match w.keyword {
1361                Keyword::CIRCLE => Ok(Some(self.parse_geometric_type(GeometricTypeKind::Circle)?)),
1362                Keyword::BOX => Ok(Some(self.parse_geometric_type(GeometricTypeKind::GeometricBox)?)),
1363                Keyword::PATH => Ok(Some(self.parse_geometric_type(GeometricTypeKind::GeometricPath)?)),
1364                Keyword::LINE => Ok(Some(self.parse_geometric_type(GeometricTypeKind::Line)?)),
1365                Keyword::LSEG => Ok(Some(self.parse_geometric_type(GeometricTypeKind::LineSegment)?)),
1366                Keyword::POINT => Ok(Some(self.parse_geometric_type(GeometricTypeKind::Point)?)),
1367                Keyword::POLYGON => Ok(Some(self.parse_geometric_type(GeometricTypeKind::Polygon)?)),
1368                _ => Ok(None),
1369            },
1370            _ => Ok(None),
1371        }
1372    }
1373
1374    /// Tries to parse an expression by a word that is not known to have a special meaning in the dialect.
1375    fn parse_expr_prefix_by_unreserved_word(
1376        &mut self,
1377        w: &Word,
1378        w_span: Span,
1379    ) -> Result<Expr, ParserError> {
1380        match self.peek_token().token {
1381            Token::LParen if !self.peek_outer_join_operator() => {
1382                let id_parts = vec![w.clone().into_ident(w_span)];
1383                self.parse_function(ObjectName::from(id_parts))
1384            }
1385            // string introducer https://dev.mysql.com/doc/refman/8.0/en/charset-introducer.html
1386            Token::SingleQuotedString(_)
1387            | Token::DoubleQuotedString(_)
1388            | Token::HexStringLiteral(_)
1389                if w.value.starts_with('_') =>
1390            {
1391                Ok(Expr::Prefixed {
1392                    prefix: w.clone().into_ident(w_span),
1393                    value: self.parse_introduced_string_expr()?.into(),
1394                })
1395            }
1396            // string introducer https://dev.mysql.com/doc/refman/8.0/en/charset-introducer.html
1397            Token::SingleQuotedString(_)
1398            | Token::DoubleQuotedString(_)
1399            | Token::HexStringLiteral(_)
1400                if w.value.starts_with('_') =>
1401            {
1402                Ok(Expr::Prefixed {
1403                    prefix: w.clone().into_ident(w_span),
1404                    value: self.parse_introduced_string_expr()?.into(),
1405                })
1406            }
1407            Token::Arrow if self.dialect.supports_lambda_functions() => {
1408                self.expect_token(&Token::Arrow)?;
1409                Ok(Expr::Lambda(LambdaFunction {
1410                    params: OneOrManyWithParens::One(w.clone().into_ident(w_span)),
1411                    body: Box::new(self.parse_expr()?),
1412                }))
1413            }
1414            _ => Ok(Expr::Identifier(w.clone().into_ident(w_span))),
1415        }
1416    }
1417
1418    /// Parse an expression prefix.
1419    pub fn parse_prefix(&mut self) -> Result<Expr, ParserError> {
1420        // allow the dialect to override prefix parsing
1421        if let Some(prefix) = self.dialect.parse_prefix(self) {
1422            return prefix;
1423        }
1424
1425        // PostgreSQL allows any string literal to be preceded by a type name, indicating that the
1426        // string literal represents a literal of that type. Some examples:
1427        //
1428        //      DATE '2020-05-20'
1429        //      TIMESTAMP WITH TIME ZONE '2020-05-20 7:43:54'
1430        //      BOOL 'true'
1431        //
1432        // The first two are standard SQL, while the latter is a PostgreSQL extension. Complicating
1433        // matters is the fact that INTERVAL string literals may optionally be followed by special
1434        // keywords, e.g.:
1435        //
1436        //      INTERVAL '7' DAY
1437        //
1438        // Note also that naively `SELECT date` looks like a syntax error because the `date` type
1439        // name is not followed by a string literal, but in fact in PostgreSQL it is a valid
1440        // expression that should parse as the column name "date".
1441        let loc = self.peek_token_ref().span.start;
1442        let opt_expr = self.maybe_parse(|parser| {
1443            match parser.parse_data_type()? {
1444                DataType::Interval => parser.parse_interval(),
1445                // PostgreSQL allows almost any identifier to be used as custom data type name,
1446                // and we support that in `parse_data_type()`. But unlike Postgres we don't
1447                // have a list of globally reserved keywords (since they vary across dialects),
1448                // so given `NOT 'a' LIKE 'b'`, we'd accept `NOT` as a possible custom data type
1449                // name, resulting in `NOT 'a'` being recognized as a `TypedString` instead of
1450                // an unary negation `NOT ('a' LIKE 'b')`. To solve this, we don't accept the
1451                // `type 'string'` syntax for the custom data types at all.
1452                DataType::Custom(..) => parser_err!("dummy", loc),
1453                data_type => Ok(Expr::TypedString {
1454                    data_type,
1455                    value: parser.parse_value()?.value,
1456                }),
1457            }
1458        })?;
1459
1460        if let Some(expr) = opt_expr {
1461            return Ok(expr);
1462        }
1463
1464        // Cache some dialect properties to avoid lifetime issues with the
1465        // next_token reference.
1466
1467        let dialect = self.dialect;
1468
1469        self.advance_token();
1470        let next_token_index = self.get_current_index();
1471        let next_token = self.get_current_token();
1472        let span = next_token.span;
1473        let expr = match &next_token.token {
1474            Token::Word(w) => {
1475                // The word we consumed may fall into one of two cases: it has a special meaning, or not.
1476                // For example, in Snowflake, the word `interval` may have two meanings depending on the context:
1477                // `SELECT CURRENT_DATE() + INTERVAL '1 DAY', MAX(interval) FROM tbl;`
1478                //                          ^^^^^^^^^^^^^^^^      ^^^^^^^^
1479                //                         interval expression   identifier
1480                //
1481                // We first try to parse the word and following tokens as a special expression, and if that fails,
1482                // we rollback and try to parse it as an identifier.
1483                let w = w.clone();
1484                match self.try_parse(|parser| parser.parse_expr_prefix_by_reserved_word(&w, span)) {
1485                    // This word indicated an expression prefix and parsing was successful
1486                    Ok(Some(expr)) => Ok(expr),
1487
1488                    // No expression prefix associated with this word
1489                    Ok(None) => Ok(self.parse_expr_prefix_by_unreserved_word(&w, span)?),
1490
1491                    // If parsing of the word as a special expression failed, we are facing two options:
1492                    // 1. The statement is malformed, e.g. `SELECT INTERVAL '1 DAI` (`DAI` instead of `DAY`)
1493                    // 2. The word is used as an identifier, e.g. `SELECT MAX(interval) FROM tbl`
1494                    // We first try to parse the word as an identifier and if that fails
1495                    // we rollback and return the parsing error we got from trying to parse a
1496                    // special expression (to maintain backwards compatibility of parsing errors).
1497                    Err(e) => {
1498                        if !self.dialect.is_reserved_for_identifier(w.keyword) {
1499                            if let Ok(Some(expr)) = self.maybe_parse(|parser| {
1500                                parser.parse_expr_prefix_by_unreserved_word(&w, span)
1501                            }) {
1502                                return Ok(expr);
1503                            }
1504                        }
1505                        return Err(e);
1506                    }
1507                }
1508            } // End of Token::Word
1509            // array `[1, 2, 3]`
1510            Token::LBracket => self.parse_array_expr(false),
1511            tok @ Token::Minus | tok @ Token::Plus => {
1512                let op = if *tok == Token::Plus {
1513                    UnaryOperator::Plus
1514                } else {
1515                    UnaryOperator::Minus
1516                };
1517                Ok(Expr::UnaryOp {
1518                    op,
1519                    expr: Box::new(
1520                        self.parse_subexpr(self.dialect.prec_value(Precedence::MulDivModOp))?,
1521                    ),
1522                })
1523            }
1524            Token::ExclamationMark if dialect.supports_bang_not_operator() => Ok(Expr::UnaryOp {
1525                op: UnaryOperator::BangNot,
1526                expr: Box::new(self.parse_subexpr(self.dialect.prec_value(Precedence::UnaryNot))?),
1527            }),
1528            tok @ Token::DoubleExclamationMark
1529            | tok @ Token::PGSquareRoot
1530            | tok @ Token::PGCubeRoot
1531            | tok @ Token::AtSign
1532            | tok @ Token::Tilde
1533                if dialect_is!(dialect is PostgreSqlDialect) =>
1534            {
1535                let op = match tok {
1536                    Token::DoubleExclamationMark => UnaryOperator::PGPrefixFactorial,
1537                    Token::PGSquareRoot => UnaryOperator::PGSquareRoot,
1538                    Token::PGCubeRoot => UnaryOperator::PGCubeRoot,
1539                    Token::AtSign => UnaryOperator::PGAbs,
1540                    Token::Tilde => UnaryOperator::PGBitwiseNot,
1541                    _ => unreachable!(),
1542                };
1543                Ok(Expr::UnaryOp {
1544                    op,
1545                    expr: Box::new(
1546                        self.parse_subexpr(self.dialect.prec_value(Precedence::PlusMinus))?,
1547                    ),
1548                })
1549            }
1550            tok @ Token::Sharp
1551            | tok @ Token::AtDashAt
1552            | tok @ Token::AtAt
1553            | tok @ Token::QuestionMarkDash
1554            | tok @ Token::QuestionPipe
1555                if self.dialect.supports_geometric_types() =>
1556            {
1557                let op = match tok {
1558                    Token::Sharp => UnaryOperator::Hash,
1559                    Token::AtDashAt => UnaryOperator::AtDashAt,
1560                    Token::AtAt => UnaryOperator::DoubleAt,
1561                    Token::QuestionMarkDash => UnaryOperator::QuestionDash,
1562                    Token::QuestionPipe => UnaryOperator::QuestionPipe,
1563                    _ => {
1564                        return Err(ParserError::ParserError(format!(
1565                            "Unexpected token in unary operator parsing: {:?}",
1566                            tok
1567                        )))
1568                    }
1569                };
1570                Ok(Expr::UnaryOp {
1571                    op,
1572                    expr: Box::new(
1573                        self.parse_subexpr(self.dialect.prec_value(Precedence::PlusMinus))?,
1574                    ),
1575                })
1576            }
1577            Token::EscapedStringLiteral(_) if dialect_is!(dialect is PostgreSqlDialect | GenericDialect) =>
1578            {
1579                self.prev_token();
1580                Ok(Expr::Value(self.parse_value()?))
1581            }
1582            Token::UnicodeStringLiteral(_) => {
1583                self.prev_token();
1584                Ok(Expr::Value(self.parse_value()?))
1585            }
1586            Token::Number(_, _)
1587            | Token::SingleQuotedString(_)
1588            | Token::DoubleQuotedString(_)
1589            | Token::TripleSingleQuotedString(_)
1590            | Token::TripleDoubleQuotedString(_)
1591            | Token::DollarQuotedString(_)
1592            | Token::SingleQuotedByteStringLiteral(_)
1593            | Token::DoubleQuotedByteStringLiteral(_)
1594            | Token::TripleSingleQuotedByteStringLiteral(_)
1595            | Token::TripleDoubleQuotedByteStringLiteral(_)
1596            | Token::SingleQuotedRawStringLiteral(_)
1597            | Token::DoubleQuotedRawStringLiteral(_)
1598            | Token::TripleSingleQuotedRawStringLiteral(_)
1599            | Token::TripleDoubleQuotedRawStringLiteral(_)
1600            | Token::NationalStringLiteral(_)
1601            | Token::HexStringLiteral(_) => {
1602                self.prev_token();
1603                Ok(Expr::Value(self.parse_value()?))
1604            }
1605            Token::LParen => {
1606                let expr = if let Some(expr) = self.try_parse_expr_sub_query()? {
1607                    expr
1608                } else if let Some(lambda) = self.try_parse_lambda()? {
1609                    return Ok(lambda);
1610                } else {
1611                    let exprs = self.parse_comma_separated(Parser::parse_expr)?;
1612                    match exprs.len() {
1613                        0 => unreachable!(), // parse_comma_separated ensures 1 or more
1614                        1 => Expr::Nested(Box::new(exprs.into_iter().next().unwrap())),
1615                        _ => Expr::Tuple(exprs),
1616                    }
1617                };
1618                self.expect_token(&Token::RParen)?;
1619                Ok(expr)
1620            }
1621            Token::Placeholder(_) | Token::Colon | Token::AtSign => {
1622                self.prev_token();
1623                Ok(Expr::Value(self.parse_value()?))
1624            }
1625            Token::LBrace => {
1626                self.prev_token();
1627                self.parse_lbrace_expr()
1628            }
1629            _ => self.expected_at("an expression", next_token_index),
1630        }?;
1631
1632        if self.parse_keyword(Keyword::COLLATE) {
1633            Ok(Expr::Collate {
1634                expr: Box::new(expr),
1635                collation: self.parse_object_name(false)?,
1636            })
1637        } else {
1638            Ok(expr)
1639        }
1640    }
1641
1642    fn parse_geometric_type(&mut self, kind: GeometricTypeKind) -> Result<Expr, ParserError> {
1643        let value: Value = self.parse_value()?.value;
1644        Ok(Expr::TypedString {
1645            data_type: DataType::GeometricType(kind),
1646            value,
1647        })
1648    }
1649
1650    /// Try to parse an [Expr::CompoundFieldAccess] like `a.b.c` or `a.b[1].c`.
1651    /// If all the fields are `Expr::Identifier`s, return an [Expr::CompoundIdentifier] instead.
1652    /// If only the root exists, return the root.
1653    /// Parses compound expressions which may be delimited by period
1654    /// or bracket notation.
1655    /// For example: `a.b.c`, `a.b[1]`.
1656    pub fn parse_compound_expr(
1657        &mut self,
1658        root: Expr,
1659        mut chain: Vec<AccessExpr>,
1660    ) -> Result<Expr, ParserError> {
1661        let mut ending_wildcard: Option<TokenWithSpan> = None;
1662        loop {
1663            if self.consume_token(&Token::Period) {
1664                let next_token = self.peek_token_ref();
1665                match &next_token.token {
1666                    Token::Mul => {
1667                        // Postgres explicitly allows funcnm(tablenm.*) and the
1668                        // function array_agg traverses this control flow
1669                        if dialect_of!(self is PostgreSqlDialect) {
1670                            ending_wildcard = Some(self.next_token());
1671                        } else {
1672                            // Put back the consumed `.` tokens before exiting.
1673                            // If this expression is being parsed in the
1674                            // context of a projection, then the `.*` could imply
1675                            // a wildcard expansion. For example:
1676                            // `SELECT STRUCT('foo').* FROM T`
1677                            self.prev_token(); // .
1678                        }
1679
1680                        break;
1681                    }
1682                    Token::SingleQuotedString(s) => {
1683                        let expr =
1684                            Expr::Identifier(Ident::with_quote_and_span('\'', next_token.span, s));
1685                        chain.push(AccessExpr::Dot(expr));
1686                        self.advance_token(); // The consumed string
1687                    }
1688                    // Fallback to parsing an arbitrary expression.
1689                    _ => match self.parse_subexpr(self.dialect.prec_value(Precedence::Period))? {
1690                        // If we get back a compound field access or identifier,
1691                        // we flatten the nested expression.
1692                        // For example if the current root is `foo`
1693                        // and we get back a compound identifier expression `bar.baz`
1694                        // The full expression should be `foo.bar.baz` (i.e.
1695                        // a root with an access chain with 2 entries) and not
1696                        // `foo.(bar.baz)` (i.e. a root with an access chain with
1697                        // 1 entry`).
1698                        Expr::CompoundFieldAccess { root, access_chain } => {
1699                            chain.push(AccessExpr::Dot(*root));
1700                            chain.extend(access_chain);
1701                        }
1702                        Expr::CompoundIdentifier(parts) => chain
1703                            .extend(parts.into_iter().map(Expr::Identifier).map(AccessExpr::Dot)),
1704                        expr => {
1705                            chain.push(AccessExpr::Dot(expr));
1706                        }
1707                    },
1708                }
1709            } else if !self.dialect.supports_partiql()
1710                && self.peek_token_ref().token == Token::LBracket
1711            {
1712                self.parse_multi_dim_subscript(&mut chain)?;
1713            } else {
1714                break;
1715            }
1716        }
1717
1718        let tok_index = self.get_current_index();
1719        if let Some(wildcard_token) = ending_wildcard {
1720            if !Self::is_all_ident(&root, &chain) {
1721                return self.expected("an identifier or a '*' after '.'", self.peek_token());
1722            };
1723            Ok(Expr::QualifiedWildcard(
1724                ObjectName::from(Self::exprs_to_idents(root, chain)?),
1725                AttachedToken(wildcard_token),
1726            ))
1727        } else if self.maybe_parse_outer_join_operator() {
1728            if !Self::is_all_ident(&root, &chain) {
1729                return self.expected_at("column identifier before (+)", tok_index);
1730            };
1731            let expr = if chain.is_empty() {
1732                root
1733            } else {
1734                Expr::CompoundIdentifier(Self::exprs_to_idents(root, chain)?)
1735            };
1736            Ok(Expr::OuterJoin(expr.into()))
1737        } else {
1738            Self::build_compound_expr(root, chain)
1739        }
1740    }
1741
1742    /// Combines a root expression and access chain to form
1743    /// a compound expression. Which may be a [Expr::CompoundFieldAccess]
1744    /// or other special cased expressions like [Expr::CompoundIdentifier],
1745    /// [Expr::OuterJoin].
1746    fn build_compound_expr(
1747        root: Expr,
1748        mut access_chain: Vec<AccessExpr>,
1749    ) -> Result<Expr, ParserError> {
1750        if access_chain.is_empty() {
1751            return Ok(root);
1752        }
1753
1754        if Self::is_all_ident(&root, &access_chain) {
1755            return Ok(Expr::CompoundIdentifier(Self::exprs_to_idents(
1756                root,
1757                access_chain,
1758            )?));
1759        }
1760
1761        // Flatten qualified function calls.
1762        // For example, the expression `a.b.c.foo(1,2,3)` should
1763        // represent a function called `a.b.c.foo`, rather than
1764        // a composite expression.
1765        if matches!(root, Expr::Identifier(_))
1766            && matches!(
1767                access_chain.last(),
1768                Some(AccessExpr::Dot(Expr::Function(_)))
1769            )
1770            && access_chain
1771                .iter()
1772                .rev()
1773                .skip(1) // All except the Function
1774                .all(|access| matches!(access, AccessExpr::Dot(Expr::Identifier(_))))
1775        {
1776            let Some(AccessExpr::Dot(Expr::Function(mut func))) = access_chain.pop() else {
1777                return parser_err!("expected function expression", root.span().start);
1778            };
1779
1780            let compound_func_name = [root]
1781                .into_iter()
1782                .chain(access_chain.into_iter().flat_map(|access| match access {
1783                    AccessExpr::Dot(expr) => Some(expr),
1784                    _ => None,
1785                }))
1786                .flat_map(|expr| match expr {
1787                    Expr::Identifier(ident) => Some(ident),
1788                    _ => None,
1789                })
1790                .map(ObjectNamePart::Identifier)
1791                .chain(func.name.0)
1792                .collect::<Vec<_>>();
1793            func.name = ObjectName(compound_func_name);
1794
1795            return Ok(Expr::Function(func));
1796        }
1797
1798        // Flatten qualified outer join expressions.
1799        // For example, the expression `T.foo(+)` should
1800        // represent an outer join on the column name `T.foo`
1801        // rather than a composite expression.
1802        if access_chain.len() == 1
1803            && matches!(
1804                access_chain.last(),
1805                Some(AccessExpr::Dot(Expr::OuterJoin(_)))
1806            )
1807        {
1808            let Some(AccessExpr::Dot(Expr::OuterJoin(inner_expr))) = access_chain.pop() else {
1809                return parser_err!("expected (+) expression", root.span().start);
1810            };
1811
1812            if !Self::is_all_ident(&root, &[]) {
1813                return parser_err!("column identifier before (+)", root.span().start);
1814            };
1815
1816            let token_start = root.span().start;
1817            let mut idents = Self::exprs_to_idents(root, vec![])?;
1818            match *inner_expr {
1819                Expr::CompoundIdentifier(suffix) => idents.extend(suffix),
1820                Expr::Identifier(suffix) => idents.push(suffix),
1821                _ => {
1822                    return parser_err!("column identifier before (+)", token_start);
1823                }
1824            }
1825
1826            return Ok(Expr::OuterJoin(Expr::CompoundIdentifier(idents).into()));
1827        }
1828
1829        Ok(Expr::CompoundFieldAccess {
1830            root: Box::new(root),
1831            access_chain,
1832        })
1833    }
1834
1835    fn keyword_to_modifier(k: Keyword) -> Option<ContextModifier> {
1836        match k {
1837            Keyword::LOCAL => Some(ContextModifier::Local),
1838            Keyword::GLOBAL => Some(ContextModifier::Global),
1839            Keyword::SESSION => Some(ContextModifier::Session),
1840            _ => None,
1841        }
1842    }
1843
1844    /// Check if the root is an identifier and all fields are identifiers.
1845    fn is_all_ident(root: &Expr, fields: &[AccessExpr]) -> bool {
1846        if !matches!(root, Expr::Identifier(_)) {
1847            return false;
1848        }
1849        fields
1850            .iter()
1851            .all(|x| matches!(x, AccessExpr::Dot(Expr::Identifier(_))))
1852    }
1853
1854    /// Convert a root and a list of fields to a list of identifiers.
1855    fn exprs_to_idents(root: Expr, fields: Vec<AccessExpr>) -> Result<Vec<Ident>, ParserError> {
1856        let mut idents = vec![];
1857        if let Expr::Identifier(root) = root {
1858            idents.push(root);
1859            for x in fields {
1860                if let AccessExpr::Dot(Expr::Identifier(ident)) = x {
1861                    idents.push(ident);
1862                } else {
1863                    return parser_err!(
1864                        format!("Expected identifier, found: {}", x),
1865                        x.span().start
1866                    );
1867                }
1868            }
1869            Ok(idents)
1870        } else {
1871            parser_err!(
1872                format!("Expected identifier, found: {}", root),
1873                root.span().start
1874            )
1875        }
1876    }
1877
1878    /// Returns true if the next tokens indicate the outer join operator `(+)`.
1879    fn peek_outer_join_operator(&mut self) -> bool {
1880        if !self.dialect.supports_outer_join_operator() {
1881            return false;
1882        }
1883
1884        let [maybe_lparen, maybe_plus, maybe_rparen] = self.peek_tokens_ref();
1885        Token::LParen == maybe_lparen.token
1886            && Token::Plus == maybe_plus.token
1887            && Token::RParen == maybe_rparen.token
1888    }
1889
1890    /// If the next tokens indicates the outer join operator `(+)`, consume
1891    /// the tokens and return true.
1892    fn maybe_parse_outer_join_operator(&mut self) -> bool {
1893        self.dialect.supports_outer_join_operator()
1894            && self.consume_tokens(&[Token::LParen, Token::Plus, Token::RParen])
1895    }
1896
1897    pub fn parse_utility_options(&mut self) -> Result<Vec<UtilityOption>, ParserError> {
1898        self.expect_token(&Token::LParen)?;
1899        let options = self.parse_comma_separated(Self::parse_utility_option)?;
1900        self.expect_token(&Token::RParen)?;
1901
1902        Ok(options)
1903    }
1904
1905    fn parse_utility_option(&mut self) -> Result<UtilityOption, ParserError> {
1906        let name = self.parse_identifier()?;
1907
1908        let next_token = self.peek_token();
1909        if next_token == Token::Comma || next_token == Token::RParen {
1910            return Ok(UtilityOption { name, arg: None });
1911        }
1912        let arg = self.parse_expr()?;
1913
1914        Ok(UtilityOption {
1915            name,
1916            arg: Some(arg),
1917        })
1918    }
1919
1920    fn try_parse_expr_sub_query(&mut self) -> Result<Option<Expr>, ParserError> {
1921        if !self.peek_sub_query() {
1922            return Ok(None);
1923        }
1924
1925        Ok(Some(Expr::Subquery(self.parse_query()?)))
1926    }
1927
1928    fn try_parse_lambda(&mut self) -> Result<Option<Expr>, ParserError> {
1929        if !self.dialect.supports_lambda_functions() {
1930            return Ok(None);
1931        }
1932        self.maybe_parse(|p| {
1933            let params = p.parse_comma_separated(|p| p.parse_identifier())?;
1934            p.expect_token(&Token::RParen)?;
1935            p.expect_token(&Token::Arrow)?;
1936            let expr = p.parse_expr()?;
1937            Ok(Expr::Lambda(LambdaFunction {
1938                params: OneOrManyWithParens::Many(params),
1939                body: Box::new(expr),
1940            }))
1941        })
1942    }
1943
1944    /// Tries to parse the body of an [ODBC function] call.
1945    /// i.e. without the enclosing braces
1946    ///
1947    /// ```sql
1948    /// fn myfunc(1,2,3)
1949    /// ```
1950    ///
1951    /// [ODBC function]: https://learn.microsoft.com/en-us/sql/odbc/reference/develop-app/scalar-function-calls?view=sql-server-2017
1952    fn maybe_parse_odbc_fn_body(&mut self) -> Result<Option<Expr>, ParserError> {
1953        self.maybe_parse(|p| {
1954            p.expect_keyword(Keyword::FN)?;
1955            let fn_name = p.parse_object_name(false)?;
1956            let mut fn_call = p.parse_function_call(fn_name)?;
1957            fn_call.uses_odbc_syntax = true;
1958            Ok(Expr::Function(fn_call))
1959        })
1960    }
1961
1962    pub fn parse_function(&mut self, name: ObjectName) -> Result<Expr, ParserError> {
1963        self.parse_function_call(name).map(Expr::Function)
1964    }
1965
1966    fn parse_function_call(&mut self, name: ObjectName) -> Result<Function, ParserError> {
1967        self.expect_token(&Token::LParen)?;
1968
1969        // Snowflake permits a subquery to be passed as an argument without
1970        // an enclosing set of parens if it's the only argument.
1971        if dialect_of!(self is SnowflakeDialect) && self.peek_sub_query() {
1972            let subquery = self.parse_query()?;
1973            self.expect_token(&Token::RParen)?;
1974            return Ok(Function {
1975                name,
1976                uses_odbc_syntax: false,
1977                parameters: FunctionArguments::None,
1978                args: FunctionArguments::Subquery(subquery),
1979                filter: None,
1980                null_treatment: None,
1981                over: None,
1982                within_group: vec![],
1983            });
1984        }
1985
1986        let mut args = self.parse_function_argument_list()?;
1987        let mut parameters = FunctionArguments::None;
1988        // ClickHouse aggregations support parametric functions like `HISTOGRAM(0.5, 0.6)(x, y)`
1989        // which (0.5, 0.6) is a parameter to the function.
1990        if dialect_of!(self is ClickHouseDialect | GenericDialect)
1991            && self.consume_token(&Token::LParen)
1992        {
1993            parameters = FunctionArguments::List(args);
1994            args = self.parse_function_argument_list()?;
1995        }
1996
1997        let within_group = if self.parse_keywords(&[Keyword::WITHIN, Keyword::GROUP]) {
1998            self.expect_token(&Token::LParen)?;
1999            self.expect_keywords(&[Keyword::ORDER, Keyword::BY])?;
2000            let order_by = self.parse_comma_separated(Parser::parse_order_by_expr)?;
2001            self.expect_token(&Token::RParen)?;
2002            order_by
2003        } else {
2004            vec![]
2005        };
2006
2007        let filter = if self.dialect.supports_filter_during_aggregation()
2008            && self.parse_keyword(Keyword::FILTER)
2009            && self.consume_token(&Token::LParen)
2010            && self.parse_keyword(Keyword::WHERE)
2011        {
2012            let filter = Some(Box::new(self.parse_expr()?));
2013            self.expect_token(&Token::RParen)?;
2014            filter
2015        } else {
2016            None
2017        };
2018
2019        // Syntax for null treatment shows up either in the args list
2020        // or after the function call, but not both.
2021        let null_treatment = if args
2022            .clauses
2023            .iter()
2024            .all(|clause| !matches!(clause, FunctionArgumentClause::IgnoreOrRespectNulls(_)))
2025        {
2026            self.parse_null_treatment()?
2027        } else {
2028            None
2029        };
2030
2031        let over = if self.parse_keyword(Keyword::OVER) {
2032            if self.consume_token(&Token::LParen) {
2033                let window_spec = self.parse_window_spec()?;
2034                Some(WindowType::WindowSpec(window_spec))
2035            } else {
2036                Some(WindowType::NamedWindow(self.parse_identifier()?))
2037            }
2038        } else {
2039            None
2040        };
2041
2042        Ok(Function {
2043            name,
2044            uses_odbc_syntax: false,
2045            parameters,
2046            args: FunctionArguments::List(args),
2047            null_treatment,
2048            filter,
2049            over,
2050            within_group,
2051        })
2052    }
2053
2054    /// Optionally parses a null treatment clause.
2055    fn parse_null_treatment(&mut self) -> Result<Option<NullTreatment>, ParserError> {
2056        match self.parse_one_of_keywords(&[Keyword::RESPECT, Keyword::IGNORE]) {
2057            Some(keyword) => {
2058                self.expect_keyword_is(Keyword::NULLS)?;
2059
2060                Ok(match keyword {
2061                    Keyword::RESPECT => Some(NullTreatment::RespectNulls),
2062                    Keyword::IGNORE => Some(NullTreatment::IgnoreNulls),
2063                    _ => None,
2064                })
2065            }
2066            None => Ok(None),
2067        }
2068    }
2069
2070    pub fn parse_time_functions(&mut self, name: ObjectName) -> Result<Expr, ParserError> {
2071        let args = if self.consume_token(&Token::LParen) {
2072            FunctionArguments::List(self.parse_function_argument_list()?)
2073        } else {
2074            FunctionArguments::None
2075        };
2076        Ok(Expr::Function(Function {
2077            name,
2078            uses_odbc_syntax: false,
2079            parameters: FunctionArguments::None,
2080            args,
2081            filter: None,
2082            over: None,
2083            null_treatment: None,
2084            within_group: vec![],
2085        }))
2086    }
2087
2088    pub fn parse_window_frame_units(&mut self) -> Result<WindowFrameUnits, ParserError> {
2089        let next_token = self.next_token();
2090        match &next_token.token {
2091            Token::Word(w) => match w.keyword {
2092                Keyword::ROWS => Ok(WindowFrameUnits::Rows),
2093                Keyword::RANGE => Ok(WindowFrameUnits::Range),
2094                Keyword::GROUPS => Ok(WindowFrameUnits::Groups),
2095                _ => self.expected("ROWS, RANGE, GROUPS", next_token)?,
2096            },
2097            _ => self.expected("ROWS, RANGE, GROUPS", next_token),
2098        }
2099    }
2100
2101    pub fn parse_window_frame(&mut self) -> Result<WindowFrame, ParserError> {
2102        let units = self.parse_window_frame_units()?;
2103        let (start_bound, end_bound) = if self.parse_keyword(Keyword::BETWEEN) {
2104            let start_bound = self.parse_window_frame_bound()?;
2105            self.expect_keyword_is(Keyword::AND)?;
2106            let end_bound = Some(self.parse_window_frame_bound()?);
2107            (start_bound, end_bound)
2108        } else {
2109            (self.parse_window_frame_bound()?, None)
2110        };
2111        Ok(WindowFrame {
2112            units,
2113            start_bound,
2114            end_bound,
2115        })
2116    }
2117
2118    /// Parse `CURRENT ROW` or `{ <positive number> | UNBOUNDED } { PRECEDING | FOLLOWING }`
2119    pub fn parse_window_frame_bound(&mut self) -> Result<WindowFrameBound, ParserError> {
2120        if self.parse_keywords(&[Keyword::CURRENT, Keyword::ROW]) {
2121            Ok(WindowFrameBound::CurrentRow)
2122        } else {
2123            let rows = if self.parse_keyword(Keyword::UNBOUNDED) {
2124                None
2125            } else {
2126                Some(Box::new(match self.peek_token().token {
2127                    Token::SingleQuotedString(_) => self.parse_interval()?,
2128                    _ => self.parse_expr()?,
2129                }))
2130            };
2131            if self.parse_keyword(Keyword::PRECEDING) {
2132                Ok(WindowFrameBound::Preceding(rows))
2133            } else if self.parse_keyword(Keyword::FOLLOWING) {
2134                Ok(WindowFrameBound::Following(rows))
2135            } else {
2136                self.expected("PRECEDING or FOLLOWING", self.peek_token())
2137            }
2138        }
2139    }
2140
2141    /// Parse a group by expr. Group by expr can be one of group sets, roll up, cube, or simple expr.
2142    fn parse_group_by_expr(&mut self) -> Result<Expr, ParserError> {
2143        if self.dialect.supports_group_by_expr() {
2144            if self.parse_keywords(&[Keyword::GROUPING, Keyword::SETS]) {
2145                self.expect_token(&Token::LParen)?;
2146                let result = self.parse_comma_separated(|p| p.parse_tuple(false, true))?;
2147                self.expect_token(&Token::RParen)?;
2148                Ok(Expr::GroupingSets(result))
2149            } else if self.parse_keyword(Keyword::CUBE) {
2150                self.expect_token(&Token::LParen)?;
2151                let result = self.parse_comma_separated(|p| p.parse_tuple(true, true))?;
2152                self.expect_token(&Token::RParen)?;
2153                Ok(Expr::Cube(result))
2154            } else if self.parse_keyword(Keyword::ROLLUP) {
2155                self.expect_token(&Token::LParen)?;
2156                let result = self.parse_comma_separated(|p| p.parse_tuple(true, true))?;
2157                self.expect_token(&Token::RParen)?;
2158                Ok(Expr::Rollup(result))
2159            } else if self.consume_tokens(&[Token::LParen, Token::RParen]) {
2160                // PostgreSQL allow to use empty tuple as a group by expression,
2161                // e.g. `GROUP BY (), name`. Please refer to GROUP BY Clause section in
2162                // [PostgreSQL](https://www.postgresql.org/docs/16/sql-select.html)
2163                Ok(Expr::Tuple(vec![]))
2164            } else {
2165                self.parse_expr()
2166            }
2167        } else {
2168            // TODO parse rollup for other dialects
2169            self.parse_expr()
2170        }
2171    }
2172
2173    /// Parse a tuple with `(` and `)`.
2174    /// If `lift_singleton` is true, then a singleton tuple is lifted to a tuple of length 1, otherwise it will fail.
2175    /// If `allow_empty` is true, then an empty tuple is allowed.
2176    fn parse_tuple(
2177        &mut self,
2178        lift_singleton: bool,
2179        allow_empty: bool,
2180    ) -> Result<Vec<Expr>, ParserError> {
2181        if lift_singleton {
2182            if self.consume_token(&Token::LParen) {
2183                let result = if allow_empty && self.consume_token(&Token::RParen) {
2184                    vec![]
2185                } else {
2186                    let result = self.parse_comma_separated(Parser::parse_expr)?;
2187                    self.expect_token(&Token::RParen)?;
2188                    result
2189                };
2190                Ok(result)
2191            } else {
2192                Ok(vec![self.parse_expr()?])
2193            }
2194        } else {
2195            self.expect_token(&Token::LParen)?;
2196            let result = if allow_empty && self.consume_token(&Token::RParen) {
2197                vec![]
2198            } else {
2199                let result = self.parse_comma_separated(Parser::parse_expr)?;
2200                self.expect_token(&Token::RParen)?;
2201                result
2202            };
2203            Ok(result)
2204        }
2205    }
2206
2207    pub fn parse_case_expr(&mut self) -> Result<Expr, ParserError> {
2208        let mut operand = None;
2209        if !self.parse_keyword(Keyword::WHEN) {
2210            operand = Some(Box::new(self.parse_expr()?));
2211            self.expect_keyword_is(Keyword::WHEN)?;
2212        }
2213        let mut conditions = vec![];
2214        loop {
2215            let condition = self.parse_expr()?;
2216            self.expect_keyword_is(Keyword::THEN)?;
2217            let result = self.parse_expr()?;
2218            conditions.push(CaseWhen { condition, result });
2219            if !self.parse_keyword(Keyword::WHEN) {
2220                break;
2221            }
2222        }
2223        let else_result = if self.parse_keyword(Keyword::ELSE) {
2224            Some(Box::new(self.parse_expr()?))
2225        } else {
2226            None
2227        };
2228        self.expect_keyword_is(Keyword::END)?;
2229        Ok(Expr::Case {
2230            operand,
2231            conditions,
2232            else_result,
2233        })
2234    }
2235
2236    pub fn parse_optional_cast_format(&mut self) -> Result<Option<CastFormat>, ParserError> {
2237        if self.parse_keyword(Keyword::FORMAT) {
2238            let value = self.parse_value()?.value;
2239            match self.parse_optional_time_zone()? {
2240                Some(tz) => Ok(Some(CastFormat::ValueAtTimeZone(value, tz))),
2241                None => Ok(Some(CastFormat::Value(value))),
2242            }
2243        } else {
2244            Ok(None)
2245        }
2246    }
2247
2248    pub fn parse_optional_time_zone(&mut self) -> Result<Option<Value>, ParserError> {
2249        if self.parse_keywords(&[Keyword::AT, Keyword::TIME, Keyword::ZONE]) {
2250            self.parse_value().map(|v| Some(v.value))
2251        } else {
2252            Ok(None)
2253        }
2254    }
2255
2256    /// mssql-like convert function
2257    fn parse_mssql_convert(&mut self, is_try: bool) -> Result<Expr, ParserError> {
2258        self.expect_token(&Token::LParen)?;
2259        let data_type = self.parse_data_type()?;
2260        self.expect_token(&Token::Comma)?;
2261        let expr = self.parse_expr()?;
2262        let styles = if self.consume_token(&Token::Comma) {
2263            self.parse_comma_separated(Parser::parse_expr)?
2264        } else {
2265            Default::default()
2266        };
2267        self.expect_token(&Token::RParen)?;
2268        Ok(Expr::Convert {
2269            is_try,
2270            expr: Box::new(expr),
2271            data_type: Some(data_type),
2272            charset: None,
2273            target_before_value: true,
2274            styles,
2275        })
2276    }
2277
2278    /// Parse a SQL CONVERT function:
2279    ///  - `CONVERT('héhé' USING utf8mb4)` (MySQL)
2280    ///  - `CONVERT('héhé', CHAR CHARACTER SET utf8mb4)` (MySQL)
2281    ///  - `CONVERT(DECIMAL(10, 5), 42)` (MSSQL) - the type comes first
2282    pub fn parse_convert_expr(&mut self, is_try: bool) -> Result<Expr, ParserError> {
2283        if self.dialect.convert_type_before_value() {
2284            return self.parse_mssql_convert(is_try);
2285        }
2286        self.expect_token(&Token::LParen)?;
2287        let expr = self.parse_expr()?;
2288        if self.parse_keyword(Keyword::USING) {
2289            let charset = self.parse_object_name(false)?;
2290            self.expect_token(&Token::RParen)?;
2291            return Ok(Expr::Convert {
2292                is_try,
2293                expr: Box::new(expr),
2294                data_type: None,
2295                charset: Some(charset),
2296                target_before_value: false,
2297                styles: vec![],
2298            });
2299        }
2300        self.expect_token(&Token::Comma)?;
2301        let data_type = self.parse_data_type()?;
2302        let charset = if self.parse_keywords(&[Keyword::CHARACTER, Keyword::SET]) {
2303            Some(self.parse_object_name(false)?)
2304        } else {
2305            None
2306        };
2307        self.expect_token(&Token::RParen)?;
2308        Ok(Expr::Convert {
2309            is_try,
2310            expr: Box::new(expr),
2311            data_type: Some(data_type),
2312            charset,
2313            target_before_value: false,
2314            styles: vec![],
2315        })
2316    }
2317
2318    /// Parse a SQL CAST function e.g. `CAST(expr AS FLOAT)`
2319    pub fn parse_cast_expr(&mut self, kind: CastKind) -> Result<Expr, ParserError> {
2320        self.expect_token(&Token::LParen)?;
2321        let expr = self.parse_expr()?;
2322        self.expect_keyword_is(Keyword::AS)?;
2323        let data_type = self.parse_data_type()?;
2324        let format = self.parse_optional_cast_format()?;
2325        self.expect_token(&Token::RParen)?;
2326        Ok(Expr::Cast {
2327            kind,
2328            expr: Box::new(expr),
2329            data_type,
2330            format,
2331        })
2332    }
2333
2334    /// Parse a SQL EXISTS expression e.g. `WHERE EXISTS(SELECT ...)`.
2335    pub fn parse_exists_expr(&mut self, negated: bool) -> Result<Expr, ParserError> {
2336        self.expect_token(&Token::LParen)?;
2337        let exists_node = Expr::Exists {
2338            negated,
2339            subquery: self.parse_query()?,
2340        };
2341        self.expect_token(&Token::RParen)?;
2342        Ok(exists_node)
2343    }
2344
2345    pub fn parse_extract_expr(&mut self) -> Result<Expr, ParserError> {
2346        self.expect_token(&Token::LParen)?;
2347        let field = self.parse_date_time_field()?;
2348
2349        let syntax = if self.parse_keyword(Keyword::FROM) {
2350            ExtractSyntax::From
2351        } else if self.consume_token(&Token::Comma)
2352            && dialect_of!(self is SnowflakeDialect | GenericDialect)
2353        {
2354            ExtractSyntax::Comma
2355        } else {
2356            return Err(ParserError::ParserError(
2357                "Expected 'FROM' or ','".to_string(),
2358            ));
2359        };
2360
2361        let expr = self.parse_expr()?;
2362        self.expect_token(&Token::RParen)?;
2363        Ok(Expr::Extract {
2364            field,
2365            expr: Box::new(expr),
2366            syntax,
2367        })
2368    }
2369
2370    pub fn parse_ceil_floor_expr(&mut self, is_ceil: bool) -> Result<Expr, ParserError> {
2371        self.expect_token(&Token::LParen)?;
2372        let expr = self.parse_expr()?;
2373        // Parse `CEIL/FLOOR(expr)`
2374        let field = if self.parse_keyword(Keyword::TO) {
2375            // Parse `CEIL/FLOOR(expr TO DateTimeField)`
2376            CeilFloorKind::DateTimeField(self.parse_date_time_field()?)
2377        } else if self.consume_token(&Token::Comma) {
2378            // Parse `CEIL/FLOOR(expr, scale)`
2379            match self.parse_value()?.value {
2380                Value::Number(n, s) => CeilFloorKind::Scale(Value::Number(n, s)),
2381                _ => {
2382                    return Err(ParserError::ParserError(
2383                        "Scale field can only be of number type".to_string(),
2384                    ))
2385                }
2386            }
2387        } else {
2388            CeilFloorKind::DateTimeField(DateTimeField::NoDateTime)
2389        };
2390        self.expect_token(&Token::RParen)?;
2391        if is_ceil {
2392            Ok(Expr::Ceil {
2393                expr: Box::new(expr),
2394                field,
2395            })
2396        } else {
2397            Ok(Expr::Floor {
2398                expr: Box::new(expr),
2399                field,
2400            })
2401        }
2402    }
2403
2404    pub fn parse_position_expr(&mut self, ident: Ident) -> Result<Expr, ParserError> {
2405        let between_prec = self.dialect.prec_value(Precedence::Between);
2406        let position_expr = self.maybe_parse(|p| {
2407            // PARSE SELECT POSITION('@' in field)
2408            p.expect_token(&Token::LParen)?;
2409
2410            // Parse the subexpr till the IN keyword
2411            let expr = p.parse_subexpr(between_prec)?;
2412            p.expect_keyword_is(Keyword::IN)?;
2413            let from = p.parse_expr()?;
2414            p.expect_token(&Token::RParen)?;
2415            Ok(Expr::Position {
2416                expr: Box::new(expr),
2417                r#in: Box::new(from),
2418            })
2419        })?;
2420        match position_expr {
2421            Some(expr) => Ok(expr),
2422            // Snowflake supports `position` as an ordinary function call
2423            // without the special `IN` syntax.
2424            None => self.parse_function(ObjectName::from(vec![ident])),
2425        }
2426    }
2427
2428    // { SUBSTRING | SUBSTR } (<EXPR> [FROM 1] [FOR 3])
2429    pub fn parse_substring(&mut self) -> Result<Expr, ParserError> {
2430        let shorthand = match self.expect_one_of_keywords(&[Keyword::SUBSTR, Keyword::SUBSTRING])? {
2431            Keyword::SUBSTR => true,
2432            Keyword::SUBSTRING => false,
2433            _ => {
2434                self.prev_token();
2435                return self.expected("SUBSTR or SUBSTRING", self.peek_token());
2436            }
2437        };
2438        self.expect_token(&Token::LParen)?;
2439        let expr = self.parse_expr()?;
2440        let mut from_expr = None;
2441        let special = self.consume_token(&Token::Comma);
2442        if special || self.parse_keyword(Keyword::FROM) {
2443            from_expr = Some(self.parse_expr()?);
2444        }
2445
2446        let mut to_expr = None;
2447        if self.parse_keyword(Keyword::FOR) || self.consume_token(&Token::Comma) {
2448            to_expr = Some(self.parse_expr()?);
2449        }
2450        self.expect_token(&Token::RParen)?;
2451
2452        Ok(Expr::Substring {
2453            expr: Box::new(expr),
2454            substring_from: from_expr.map(Box::new),
2455            substring_for: to_expr.map(Box::new),
2456            special,
2457            shorthand,
2458        })
2459    }
2460
2461    pub fn parse_overlay_expr(&mut self) -> Result<Expr, ParserError> {
2462        // PARSE OVERLAY (EXPR PLACING EXPR FROM 1 [FOR 3])
2463        self.expect_token(&Token::LParen)?;
2464        let expr = self.parse_expr()?;
2465        self.expect_keyword_is(Keyword::PLACING)?;
2466        let what_expr = self.parse_expr()?;
2467        self.expect_keyword_is(Keyword::FROM)?;
2468        let from_expr = self.parse_expr()?;
2469        let mut for_expr = None;
2470        if self.parse_keyword(Keyword::FOR) {
2471            for_expr = Some(self.parse_expr()?);
2472        }
2473        self.expect_token(&Token::RParen)?;
2474
2475        Ok(Expr::Overlay {
2476            expr: Box::new(expr),
2477            overlay_what: Box::new(what_expr),
2478            overlay_from: Box::new(from_expr),
2479            overlay_for: for_expr.map(Box::new),
2480        })
2481    }
2482
2483    /// ```sql
2484    /// TRIM ([WHERE] ['text' FROM] 'text')
2485    /// TRIM ('text')
2486    /// TRIM(<expr>, [, characters]) -- only Snowflake or BigQuery
2487    /// ```
2488    pub fn parse_trim_expr(&mut self) -> Result<Expr, ParserError> {
2489        self.expect_token(&Token::LParen)?;
2490        let mut trim_where = None;
2491        if let Token::Word(word) = self.peek_token().token {
2492            if [Keyword::BOTH, Keyword::LEADING, Keyword::TRAILING]
2493                .iter()
2494                .any(|d| word.keyword == *d)
2495            {
2496                trim_where = Some(self.parse_trim_where()?);
2497            }
2498        }
2499        let expr = self.parse_expr()?;
2500        if self.parse_keyword(Keyword::FROM) {
2501            let trim_what = Box::new(expr);
2502            let expr = self.parse_expr()?;
2503            self.expect_token(&Token::RParen)?;
2504            Ok(Expr::Trim {
2505                expr: Box::new(expr),
2506                trim_where,
2507                trim_what: Some(trim_what),
2508                trim_characters: None,
2509            })
2510        } else if self.consume_token(&Token::Comma)
2511            && dialect_of!(self is SnowflakeDialect | BigQueryDialect | GenericDialect)
2512        {
2513            let characters = self.parse_comma_separated(Parser::parse_expr)?;
2514            self.expect_token(&Token::RParen)?;
2515            Ok(Expr::Trim {
2516                expr: Box::new(expr),
2517                trim_where: None,
2518                trim_what: None,
2519                trim_characters: Some(characters),
2520            })
2521        } else {
2522            self.expect_token(&Token::RParen)?;
2523            Ok(Expr::Trim {
2524                expr: Box::new(expr),
2525                trim_where,
2526                trim_what: None,
2527                trim_characters: None,
2528            })
2529        }
2530    }
2531
2532    pub fn parse_trim_where(&mut self) -> Result<TrimWhereField, ParserError> {
2533        let next_token = self.next_token();
2534        match &next_token.token {
2535            Token::Word(w) => match w.keyword {
2536                Keyword::BOTH => Ok(TrimWhereField::Both),
2537                Keyword::LEADING => Ok(TrimWhereField::Leading),
2538                Keyword::TRAILING => Ok(TrimWhereField::Trailing),
2539                _ => self.expected("trim_where field", next_token)?,
2540            },
2541            _ => self.expected("trim_where field", next_token),
2542        }
2543    }
2544
2545    /// Parses an array expression `[ex1, ex2, ..]`
2546    /// if `named` is `true`, came from an expression like  `ARRAY[ex1, ex2]`
2547    pub fn parse_array_expr(&mut self, named: bool) -> Result<Expr, ParserError> {
2548        let exprs = self.parse_comma_separated0(Parser::parse_expr, Token::RBracket)?;
2549        self.expect_token(&Token::RBracket)?;
2550        Ok(Expr::Array(Array { elem: exprs, named }))
2551    }
2552
2553    pub fn parse_listagg_on_overflow(&mut self) -> Result<Option<ListAggOnOverflow>, ParserError> {
2554        if self.parse_keywords(&[Keyword::ON, Keyword::OVERFLOW]) {
2555            if self.parse_keyword(Keyword::ERROR) {
2556                Ok(Some(ListAggOnOverflow::Error))
2557            } else {
2558                self.expect_keyword_is(Keyword::TRUNCATE)?;
2559                let filler = match self.peek_token().token {
2560                    Token::Word(w)
2561                        if w.keyword == Keyword::WITH || w.keyword == Keyword::WITHOUT =>
2562                    {
2563                        None
2564                    }
2565                    Token::SingleQuotedString(_)
2566                    | Token::EscapedStringLiteral(_)
2567                    | Token::UnicodeStringLiteral(_)
2568                    | Token::NationalStringLiteral(_)
2569                    | Token::HexStringLiteral(_) => Some(Box::new(self.parse_expr()?)),
2570                    _ => self.expected(
2571                        "either filler, WITH, or WITHOUT in LISTAGG",
2572                        self.peek_token(),
2573                    )?,
2574                };
2575                let with_count = self.parse_keyword(Keyword::WITH);
2576                if !with_count && !self.parse_keyword(Keyword::WITHOUT) {
2577                    self.expected("either WITH or WITHOUT in LISTAGG", self.peek_token())?;
2578                }
2579                self.expect_keyword_is(Keyword::COUNT)?;
2580                Ok(Some(ListAggOnOverflow::Truncate { filler, with_count }))
2581            }
2582        } else {
2583            Ok(None)
2584        }
2585    }
2586
2587    // This function parses date/time fields for the EXTRACT function-like
2588    // operator, interval qualifiers, and the ceil/floor operations.
2589    // EXTRACT supports a wider set of date/time fields than interval qualifiers,
2590    // so this function may need to be split in two.
2591    pub fn parse_date_time_field(&mut self) -> Result<DateTimeField, ParserError> {
2592        let next_token = self.next_token();
2593        match &next_token.token {
2594            Token::Word(w) => match w.keyword {
2595                Keyword::YEAR => Ok(DateTimeField::Year),
2596                Keyword::YEARS => Ok(DateTimeField::Years),
2597                Keyword::MONTH => Ok(DateTimeField::Month),
2598                Keyword::MONTHS => Ok(DateTimeField::Months),
2599                Keyword::WEEK => {
2600                    let week_day = if dialect_of!(self is BigQueryDialect | GenericDialect)
2601                        && self.consume_token(&Token::LParen)
2602                    {
2603                        let week_day = self.parse_identifier()?;
2604                        self.expect_token(&Token::RParen)?;
2605                        Some(week_day)
2606                    } else {
2607                        None
2608                    };
2609                    Ok(DateTimeField::Week(week_day))
2610                }
2611                Keyword::WEEKS => Ok(DateTimeField::Weeks),
2612                Keyword::DAY => Ok(DateTimeField::Day),
2613                Keyword::DAYOFWEEK => Ok(DateTimeField::DayOfWeek),
2614                Keyword::DAYOFYEAR => Ok(DateTimeField::DayOfYear),
2615                Keyword::DAYS => Ok(DateTimeField::Days),
2616                Keyword::DATE => Ok(DateTimeField::Date),
2617                Keyword::DATETIME => Ok(DateTimeField::Datetime),
2618                Keyword::HOUR => Ok(DateTimeField::Hour),
2619                Keyword::HOURS => Ok(DateTimeField::Hours),
2620                Keyword::MINUTE => Ok(DateTimeField::Minute),
2621                Keyword::MINUTES => Ok(DateTimeField::Minutes),
2622                Keyword::SECOND => Ok(DateTimeField::Second),
2623                Keyword::SECONDS => Ok(DateTimeField::Seconds),
2624                Keyword::CENTURY => Ok(DateTimeField::Century),
2625                Keyword::DECADE => Ok(DateTimeField::Decade),
2626                Keyword::DOY => Ok(DateTimeField::Doy),
2627                Keyword::DOW => Ok(DateTimeField::Dow),
2628                Keyword::EPOCH => Ok(DateTimeField::Epoch),
2629                Keyword::ISODOW => Ok(DateTimeField::Isodow),
2630                Keyword::ISOYEAR => Ok(DateTimeField::Isoyear),
2631                Keyword::ISOWEEK => Ok(DateTimeField::IsoWeek),
2632                Keyword::JULIAN => Ok(DateTimeField::Julian),
2633                Keyword::MICROSECOND => Ok(DateTimeField::Microsecond),
2634                Keyword::MICROSECONDS => Ok(DateTimeField::Microseconds),
2635                Keyword::MILLENIUM => Ok(DateTimeField::Millenium),
2636                Keyword::MILLENNIUM => Ok(DateTimeField::Millennium),
2637                Keyword::MILLISECOND => Ok(DateTimeField::Millisecond),
2638                Keyword::MILLISECONDS => Ok(DateTimeField::Milliseconds),
2639                Keyword::NANOSECOND => Ok(DateTimeField::Nanosecond),
2640                Keyword::NANOSECONDS => Ok(DateTimeField::Nanoseconds),
2641                Keyword::QUARTER => Ok(DateTimeField::Quarter),
2642                Keyword::TIME => Ok(DateTimeField::Time),
2643                Keyword::TIMEZONE => Ok(DateTimeField::Timezone),
2644                Keyword::TIMEZONE_ABBR => Ok(DateTimeField::TimezoneAbbr),
2645                Keyword::TIMEZONE_HOUR => Ok(DateTimeField::TimezoneHour),
2646                Keyword::TIMEZONE_MINUTE => Ok(DateTimeField::TimezoneMinute),
2647                Keyword::TIMEZONE_REGION => Ok(DateTimeField::TimezoneRegion),
2648                _ if self.dialect.allow_extract_custom() => {
2649                    self.prev_token();
2650                    let custom = self.parse_identifier()?;
2651                    Ok(DateTimeField::Custom(custom))
2652                }
2653                _ => self.expected("date/time field", next_token),
2654            },
2655            Token::SingleQuotedString(_) if self.dialect.allow_extract_single_quotes() => {
2656                self.prev_token();
2657                let custom = self.parse_identifier()?;
2658                Ok(DateTimeField::Custom(custom))
2659            }
2660            _ => self.expected("date/time field", next_token),
2661        }
2662    }
2663
2664    pub fn parse_not(&mut self) -> Result<Expr, ParserError> {
2665        match self.peek_token().token {
2666            Token::Word(w) => match w.keyword {
2667                Keyword::EXISTS => {
2668                    let negated = true;
2669                    let _ = self.parse_keyword(Keyword::EXISTS);
2670                    self.parse_exists_expr(negated)
2671                }
2672                _ => Ok(Expr::UnaryOp {
2673                    op: UnaryOperator::Not,
2674                    expr: Box::new(
2675                        self.parse_subexpr(self.dialect.prec_value(Precedence::UnaryNot))?,
2676                    ),
2677                }),
2678            },
2679            _ => Ok(Expr::UnaryOp {
2680                op: UnaryOperator::Not,
2681                expr: Box::new(self.parse_subexpr(self.dialect.prec_value(Precedence::UnaryNot))?),
2682            }),
2683        }
2684    }
2685
2686    /// Parse expression types that start with a left brace '{'.
2687    /// Examples:
2688    /// ```sql
2689    /// -- Dictionary expr.
2690    /// {'key1': 'value1', 'key2': 'value2'}
2691    ///
2692    /// -- Function call using the ODBC syntax.
2693    /// { fn CONCAT('foo', 'bar') }
2694    /// ```
2695    fn parse_lbrace_expr(&mut self) -> Result<Expr, ParserError> {
2696        let token = self.expect_token(&Token::LBrace)?;
2697
2698        if let Some(fn_expr) = self.maybe_parse_odbc_fn_body()? {
2699            self.expect_token(&Token::RBrace)?;
2700            return Ok(fn_expr);
2701        }
2702
2703        if self.dialect.supports_dictionary_syntax() {
2704            self.prev_token(); // Put back the '{'
2705            return self.parse_duckdb_struct_literal();
2706        }
2707
2708        self.expected("an expression", token)
2709    }
2710
2711    /// Parses fulltext expressions [`sqlparser::ast::Expr::MatchAgainst`]
2712    ///
2713    /// # Errors
2714    /// This method will raise an error if the column list is empty or with invalid identifiers,
2715    /// the match expression is not a literal string, or if the search modifier is not valid.
2716    pub fn parse_match_against(&mut self) -> Result<Expr, ParserError> {
2717        let columns = self.parse_parenthesized_qualified_column_list(Mandatory, false)?;
2718
2719        self.expect_keyword_is(Keyword::AGAINST)?;
2720
2721        self.expect_token(&Token::LParen)?;
2722
2723        // MySQL is too permissive about the value, IMO we can't validate it perfectly on syntax level.
2724        let match_value = self.parse_value()?.value;
2725
2726        let in_natural_language_mode_keywords = &[
2727            Keyword::IN,
2728            Keyword::NATURAL,
2729            Keyword::LANGUAGE,
2730            Keyword::MODE,
2731        ];
2732
2733        let with_query_expansion_keywords = &[Keyword::WITH, Keyword::QUERY, Keyword::EXPANSION];
2734
2735        let in_boolean_mode_keywords = &[Keyword::IN, Keyword::BOOLEAN, Keyword::MODE];
2736
2737        let opt_search_modifier = if self.parse_keywords(in_natural_language_mode_keywords) {
2738            if self.parse_keywords(with_query_expansion_keywords) {
2739                Some(SearchModifier::InNaturalLanguageModeWithQueryExpansion)
2740            } else {
2741                Some(SearchModifier::InNaturalLanguageMode)
2742            }
2743        } else if self.parse_keywords(in_boolean_mode_keywords) {
2744            Some(SearchModifier::InBooleanMode)
2745        } else if self.parse_keywords(with_query_expansion_keywords) {
2746            Some(SearchModifier::WithQueryExpansion)
2747        } else {
2748            None
2749        };
2750
2751        self.expect_token(&Token::RParen)?;
2752
2753        Ok(Expr::MatchAgainst {
2754            columns,
2755            match_value,
2756            opt_search_modifier,
2757        })
2758    }
2759
2760    /// Parse an `INTERVAL` expression.
2761    ///
2762    /// Some syntactically valid intervals:
2763    ///
2764    /// ```sql
2765    ///   1. INTERVAL '1' DAY
2766    ///   2. INTERVAL '1-1' YEAR TO MONTH
2767    ///   3. INTERVAL '1' SECOND
2768    ///   4. INTERVAL '1:1:1.1' HOUR (5) TO SECOND (5)
2769    ///   5. INTERVAL '1.1' SECOND (2, 2)
2770    ///   6. INTERVAL '1:1' HOUR (5) TO MINUTE (5)
2771    ///   7. (MySql & BigQuery only): INTERVAL 1 DAY
2772    /// ```
2773    ///
2774    /// Note that we do not currently attempt to parse the quoted value.
2775    pub fn parse_interval(&mut self) -> Result<Expr, ParserError> {
2776        // The SQL standard allows an optional sign before the value string, but
2777        // it is not clear if any implementations support that syntax, so we
2778        // don't currently try to parse it. (The sign can instead be included
2779        // inside the value string.)
2780
2781        // to match the different flavours of INTERVAL syntax, we only allow expressions
2782        // if the dialect requires an interval qualifier,
2783        // see https://github.com/sqlparser-rs/sqlparser-rs/pull/1398 for more details
2784        let value = if self.dialect.require_interval_qualifier() {
2785            // parse a whole expression so `INTERVAL 1 + 1 DAY` is valid
2786            self.parse_expr()?
2787        } else {
2788            // parse a prefix expression so `INTERVAL 1 DAY` is valid, but `INTERVAL 1 + 1 DAY` is not
2789            // this also means that `INTERVAL '5 days' > INTERVAL '1 day'` treated properly
2790            self.parse_prefix()?
2791        };
2792
2793        // Following the string literal is a qualifier which indicates the units
2794        // of the duration specified in the string literal.
2795        //
2796        // Note that PostgreSQL allows omitting the qualifier, so we provide
2797        // this more general implementation.
2798        let leading_field = if self.next_token_is_temporal_unit() {
2799            Some(self.parse_date_time_field()?)
2800        } else if self.dialect.require_interval_qualifier() {
2801            return parser_err!(
2802                "INTERVAL requires a unit after the literal value",
2803                self.peek_token().span.start
2804            );
2805        } else {
2806            None
2807        };
2808
2809        let (leading_precision, last_field, fsec_precision) =
2810            if leading_field == Some(DateTimeField::Second) {
2811                // SQL mandates special syntax for `SECOND TO SECOND` literals.
2812                // Instead of
2813                //     `SECOND [(<leading precision>)] TO SECOND[(<fractional seconds precision>)]`
2814                // one must use the special format:
2815                //     `SECOND [( <leading precision> [ , <fractional seconds precision>] )]`
2816                let last_field = None;
2817                let (leading_precision, fsec_precision) = self.parse_optional_precision_scale()?;
2818                (leading_precision, last_field, fsec_precision)
2819            } else {
2820                let leading_precision = self.parse_optional_precision()?;
2821                if self.parse_keyword(Keyword::TO) {
2822                    let last_field = Some(self.parse_date_time_field()?);
2823                    let fsec_precision = if last_field == Some(DateTimeField::Second) {
2824                        self.parse_optional_precision()?
2825                    } else {
2826                        None
2827                    };
2828                    (leading_precision, last_field, fsec_precision)
2829                } else {
2830                    (leading_precision, None, None)
2831                }
2832            };
2833
2834        Ok(Expr::Interval(Interval {
2835            value: Box::new(value),
2836            leading_field,
2837            leading_precision,
2838            last_field,
2839            fractional_seconds_precision: fsec_precision,
2840        }))
2841    }
2842
2843    /// Peek at the next token and determine if it is a temporal unit
2844    /// like `second`.
2845    pub fn next_token_is_temporal_unit(&mut self) -> bool {
2846        if let Token::Word(word) = self.peek_token().token {
2847            matches!(
2848                word.keyword,
2849                Keyword::YEAR
2850                    | Keyword::YEARS
2851                    | Keyword::MONTH
2852                    | Keyword::MONTHS
2853                    | Keyword::WEEK
2854                    | Keyword::WEEKS
2855                    | Keyword::DAY
2856                    | Keyword::DAYS
2857                    | Keyword::HOUR
2858                    | Keyword::HOURS
2859                    | Keyword::MINUTE
2860                    | Keyword::MINUTES
2861                    | Keyword::SECOND
2862                    | Keyword::SECONDS
2863                    | Keyword::CENTURY
2864                    | Keyword::DECADE
2865                    | Keyword::DOW
2866                    | Keyword::DOY
2867                    | Keyword::EPOCH
2868                    | Keyword::ISODOW
2869                    | Keyword::ISOYEAR
2870                    | Keyword::JULIAN
2871                    | Keyword::MICROSECOND
2872                    | Keyword::MICROSECONDS
2873                    | Keyword::MILLENIUM
2874                    | Keyword::MILLENNIUM
2875                    | Keyword::MILLISECOND
2876                    | Keyword::MILLISECONDS
2877                    | Keyword::NANOSECOND
2878                    | Keyword::NANOSECONDS
2879                    | Keyword::QUARTER
2880                    | Keyword::TIMEZONE
2881                    | Keyword::TIMEZONE_HOUR
2882                    | Keyword::TIMEZONE_MINUTE
2883            )
2884        } else {
2885            false
2886        }
2887    }
2888
2889    /// Syntax
2890    /// ```sql
2891    /// -- typed
2892    /// STRUCT<[field_name] field_type, ...>( expr1 [, ... ])
2893    /// -- typeless
2894    /// STRUCT( expr1 [AS field_name] [, ... ])
2895    /// ```
2896    fn parse_struct_literal(&mut self) -> Result<Expr, ParserError> {
2897        // Parse the fields definition if exist `<[field_name] field_type, ...>`
2898        self.prev_token();
2899        let (fields, trailing_bracket) =
2900            self.parse_struct_type_def(Self::parse_struct_field_def)?;
2901        if trailing_bracket.0 {
2902            return parser_err!(
2903                "unmatched > in STRUCT literal",
2904                self.peek_token().span.start
2905            );
2906        }
2907
2908        // Parse the struct values `(expr1 [, ... ])`
2909        self.expect_token(&Token::LParen)?;
2910        let values = self
2911            .parse_comma_separated(|parser| parser.parse_struct_field_expr(!fields.is_empty()))?;
2912        self.expect_token(&Token::RParen)?;
2913
2914        Ok(Expr::Struct { values, fields })
2915    }
2916
2917    /// Parse an expression value for a struct literal
2918    /// Syntax
2919    /// ```sql
2920    /// expr [AS name]
2921    /// ```
2922    ///
2923    /// For biquery [1], Parameter typed_syntax is set to true if the expression
2924    /// is to be parsed as a field expression declared using typed
2925    /// struct syntax [2], and false if using typeless struct syntax [3].
2926    ///
2927    /// [1]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#constructing_a_struct
2928    /// [2]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#typed_struct_syntax
2929    /// [3]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#typeless_struct_syntax
2930    fn parse_struct_field_expr(&mut self, typed_syntax: bool) -> Result<Expr, ParserError> {
2931        let expr = self.parse_expr()?;
2932        if self.parse_keyword(Keyword::AS) {
2933            if typed_syntax {
2934                return parser_err!("Typed syntax does not allow AS", {
2935                    self.prev_token();
2936                    self.peek_token().span.start
2937                });
2938            }
2939            let field_name = self.parse_identifier()?;
2940            Ok(Expr::Named {
2941                expr: expr.into(),
2942                name: field_name,
2943            })
2944        } else {
2945            Ok(expr)
2946        }
2947    }
2948
2949    /// Parse a Struct type definition as a sequence of field-value pairs.
2950    /// The syntax of the Struct elem differs by dialect so it is customised
2951    /// by the `elem_parser` argument.
2952    ///
2953    /// Syntax
2954    /// ```sql
2955    /// Hive:
2956    /// STRUCT<field_name: field_type>
2957    ///
2958    /// BigQuery:
2959    /// STRUCT<[field_name] field_type>
2960    /// ```
2961    fn parse_struct_type_def<F>(
2962        &mut self,
2963        mut elem_parser: F,
2964    ) -> Result<(Vec<StructField>, MatchedTrailingBracket), ParserError>
2965    where
2966        F: FnMut(&mut Parser<'a>) -> Result<(StructField, MatchedTrailingBracket), ParserError>,
2967    {
2968        let start_token = self.peek_token();
2969        self.expect_keyword_is(Keyword::STRUCT)?;
2970
2971        // Nothing to do if we have no type information.
2972        if Token::Lt != self.peek_token() {
2973            return Ok((Default::default(), false.into()));
2974        }
2975        self.next_token();
2976
2977        let mut field_defs = vec![];
2978        let trailing_bracket = loop {
2979            let (def, trailing_bracket) = elem_parser(self)?;
2980            field_defs.push(def);
2981            if !self.consume_token(&Token::Comma) {
2982                break trailing_bracket;
2983            }
2984
2985            // Angle brackets are balanced so we only expect the trailing `>>` after
2986            // we've matched all field types for the current struct.
2987            // e.g. this is invalid syntax `STRUCT<STRUCT<INT>>>, INT>(NULL)`
2988            if trailing_bracket.0 {
2989                return parser_err!("unmatched > in STRUCT definition", start_token.span.start);
2990            }
2991        };
2992
2993        Ok((
2994            field_defs,
2995            self.expect_closing_angle_bracket(trailing_bracket)?,
2996        ))
2997    }
2998
2999    /// Duckdb Struct Data Type <https://duckdb.org/docs/sql/data_types/struct.html#retrieving-from-structs>
3000    fn parse_duckdb_struct_type_def(&mut self) -> Result<Vec<StructField>, ParserError> {
3001        self.expect_keyword_is(Keyword::STRUCT)?;
3002        self.expect_token(&Token::LParen)?;
3003        let struct_body = self.parse_comma_separated(|parser| {
3004            let field_name = parser.parse_identifier()?;
3005            let field_type = parser.parse_data_type()?;
3006
3007            Ok(StructField {
3008                field_name: Some(field_name),
3009                field_type,
3010            })
3011        });
3012        self.expect_token(&Token::RParen)?;
3013        struct_body
3014    }
3015
3016    /// Parse a field definition in a [struct] or [tuple].
3017    /// Syntax:
3018    ///
3019    /// ```sql
3020    /// [field_name] field_type
3021    /// ```
3022    ///
3023    /// [struct]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#declaring_a_struct_type
3024    /// [tuple]: https://clickhouse.com/docs/en/sql-reference/data-types/tuple
3025    fn parse_struct_field_def(
3026        &mut self,
3027    ) -> Result<(StructField, MatchedTrailingBracket), ParserError> {
3028        // Look beyond the next item to infer whether both field name
3029        // and type are specified.
3030        let is_anonymous_field = !matches!(
3031            (self.peek_nth_token(0).token, self.peek_nth_token(1).token),
3032            (Token::Word(_), Token::Word(_))
3033        );
3034
3035        let field_name = if is_anonymous_field {
3036            None
3037        } else {
3038            Some(self.parse_identifier()?)
3039        };
3040
3041        let (field_type, trailing_bracket) = self.parse_data_type_helper()?;
3042
3043        Ok((
3044            StructField {
3045                field_name,
3046                field_type,
3047            },
3048            trailing_bracket,
3049        ))
3050    }
3051
3052    /// DuckDB specific: Parse a Union type definition as a sequence of field-value pairs.
3053    ///
3054    /// Syntax:
3055    ///
3056    /// ```sql
3057    /// UNION(field_name field_type[,...])
3058    /// ```
3059    ///
3060    /// [1]: https://duckdb.org/docs/sql/data_types/union.html
3061    fn parse_union_type_def(&mut self) -> Result<Vec<UnionField>, ParserError> {
3062        self.expect_keyword_is(Keyword::UNION)?;
3063
3064        self.expect_token(&Token::LParen)?;
3065
3066        let fields = self.parse_comma_separated(|p| {
3067            Ok(UnionField {
3068                field_name: p.parse_identifier()?,
3069                field_type: p.parse_data_type()?,
3070            })
3071        })?;
3072
3073        self.expect_token(&Token::RParen)?;
3074
3075        Ok(fields)
3076    }
3077
3078    /// DuckDB specific: Parse a duckdb [dictionary]
3079    ///
3080    /// Syntax:
3081    ///
3082    /// ```sql
3083    /// {'field_name': expr1[, ... ]}
3084    /// ```
3085    ///
3086    /// [dictionary]: https://duckdb.org/docs/sql/data_types/struct#creating-structs
3087    fn parse_duckdb_struct_literal(&mut self) -> Result<Expr, ParserError> {
3088        self.expect_token(&Token::LBrace)?;
3089
3090        let fields =
3091            self.parse_comma_separated0(Self::parse_duckdb_dictionary_field, Token::RBrace)?;
3092
3093        self.expect_token(&Token::RBrace)?;
3094
3095        Ok(Expr::Dictionary(fields))
3096    }
3097
3098    /// Parse a field for a duckdb [dictionary]
3099    ///
3100    /// Syntax
3101    ///
3102    /// ```sql
3103    /// 'name': expr
3104    /// ```
3105    ///
3106    /// [dictionary]: https://duckdb.org/docs/sql/data_types/struct#creating-structs
3107    fn parse_duckdb_dictionary_field(&mut self) -> Result<DictionaryField, ParserError> {
3108        let key = self.parse_identifier()?;
3109
3110        self.expect_token(&Token::Colon)?;
3111
3112        let expr = self.parse_expr()?;
3113
3114        Ok(DictionaryField {
3115            key,
3116            value: Box::new(expr),
3117        })
3118    }
3119
3120    /// DuckDB specific: Parse a duckdb [map]
3121    ///
3122    /// Syntax:
3123    ///
3124    /// ```sql
3125    /// Map {key1: value1[, ... ]}
3126    /// ```
3127    ///
3128    /// [map]: https://duckdb.org/docs/sql/data_types/map.html#creating-maps
3129    fn parse_duckdb_map_literal(&mut self) -> Result<Expr, ParserError> {
3130        self.expect_token(&Token::LBrace)?;
3131        let fields = self.parse_comma_separated0(Self::parse_duckdb_map_field, Token::RBrace)?;
3132        self.expect_token(&Token::RBrace)?;
3133        Ok(Expr::Map(Map { entries: fields }))
3134    }
3135
3136    /// Parse a field for a duckdb [map]
3137    ///
3138    /// Syntax
3139    ///
3140    /// ```sql
3141    /// key: value
3142    /// ```
3143    ///
3144    /// [map]: https://duckdb.org/docs/sql/data_types/map.html#creating-maps
3145    fn parse_duckdb_map_field(&mut self) -> Result<MapEntry, ParserError> {
3146        let key = self.parse_expr()?;
3147
3148        self.expect_token(&Token::Colon)?;
3149
3150        let value = self.parse_expr()?;
3151
3152        Ok(MapEntry {
3153            key: Box::new(key),
3154            value: Box::new(value),
3155        })
3156    }
3157
3158    /// Parse clickhouse [map]
3159    ///
3160    /// Syntax
3161    ///
3162    /// ```sql
3163    /// Map(key_data_type, value_data_type)
3164    /// ```
3165    ///
3166    /// [map]: https://clickhouse.com/docs/en/sql-reference/data-types/map
3167    fn parse_click_house_map_def(&mut self) -> Result<(DataType, DataType), ParserError> {
3168        self.expect_keyword_is(Keyword::MAP)?;
3169        self.expect_token(&Token::LParen)?;
3170        let key_data_type = self.parse_data_type()?;
3171        self.expect_token(&Token::Comma)?;
3172        let value_data_type = self.parse_data_type()?;
3173        self.expect_token(&Token::RParen)?;
3174
3175        Ok((key_data_type, value_data_type))
3176    }
3177
3178    /// Parse clickhouse [tuple]
3179    ///
3180    /// Syntax
3181    ///
3182    /// ```sql
3183    /// Tuple([field_name] field_type, ...)
3184    /// ```
3185    ///
3186    /// [tuple]: https://clickhouse.com/docs/en/sql-reference/data-types/tuple
3187    fn parse_click_house_tuple_def(&mut self) -> Result<Vec<StructField>, ParserError> {
3188        self.expect_keyword_is(Keyword::TUPLE)?;
3189        self.expect_token(&Token::LParen)?;
3190        let mut field_defs = vec![];
3191        loop {
3192            let (def, _) = self.parse_struct_field_def()?;
3193            field_defs.push(def);
3194            if !self.consume_token(&Token::Comma) {
3195                break;
3196            }
3197        }
3198        self.expect_token(&Token::RParen)?;
3199
3200        Ok(field_defs)
3201    }
3202
3203    /// For nested types that use the angle bracket syntax, this matches either
3204    /// `>`, `>>` or nothing depending on which variant is expected (specified by the previously
3205    /// matched `trailing_bracket` argument). It returns whether there is a trailing
3206    /// left to be matched - (i.e. if '>>' was matched).
3207    fn expect_closing_angle_bracket(
3208        &mut self,
3209        trailing_bracket: MatchedTrailingBracket,
3210    ) -> Result<MatchedTrailingBracket, ParserError> {
3211        let trailing_bracket = if !trailing_bracket.0 {
3212            match self.peek_token().token {
3213                Token::Gt => {
3214                    self.next_token();
3215                    false.into()
3216                }
3217                Token::ShiftRight => {
3218                    self.next_token();
3219                    true.into()
3220                }
3221                _ => return self.expected(">", self.peek_token()),
3222            }
3223        } else {
3224            false.into()
3225        };
3226
3227        Ok(trailing_bracket)
3228    }
3229
3230    /// Parse an operator following an expression
3231    pub fn parse_infix(&mut self, expr: Expr, precedence: u8) -> Result<Expr, ParserError> {
3232        // allow the dialect to override infix parsing
3233        if let Some(infix) = self.dialect.parse_infix(self, &expr, precedence) {
3234            return infix;
3235        }
3236
3237        let dialect = self.dialect;
3238
3239        self.advance_token();
3240        let tok = self.get_current_token();
3241        let tok_index = self.get_current_index();
3242        let span = tok.span;
3243        let regular_binary_operator = match &tok.token {
3244            Token::Spaceship => Some(BinaryOperator::Spaceship),
3245            Token::DoubleEq => Some(BinaryOperator::Eq),
3246            Token::Assignment => Some(BinaryOperator::Assignment),
3247            Token::Eq => Some(BinaryOperator::Eq),
3248            Token::Neq => Some(BinaryOperator::NotEq),
3249            Token::Gt => Some(BinaryOperator::Gt),
3250            Token::GtEq => Some(BinaryOperator::GtEq),
3251            Token::Lt => Some(BinaryOperator::Lt),
3252            Token::LtEq => Some(BinaryOperator::LtEq),
3253            Token::Plus => Some(BinaryOperator::Plus),
3254            Token::Minus => Some(BinaryOperator::Minus),
3255            Token::Mul => Some(BinaryOperator::Multiply),
3256            Token::Mod => Some(BinaryOperator::Modulo),
3257            Token::StringConcat => Some(BinaryOperator::StringConcat),
3258            Token::Pipe => Some(BinaryOperator::BitwiseOr),
3259            Token::Caret => {
3260                // In PostgreSQL, ^ stands for the exponentiation operation,
3261                // and # stands for XOR. See https://www.postgresql.org/docs/current/functions-math.html
3262                if dialect_is!(dialect is PostgreSqlDialect) {
3263                    Some(BinaryOperator::PGExp)
3264                } else {
3265                    Some(BinaryOperator::BitwiseXor)
3266                }
3267            }
3268            Token::Ampersand => Some(BinaryOperator::BitwiseAnd),
3269            Token::Div => Some(BinaryOperator::Divide),
3270            Token::DuckIntDiv if dialect_is!(dialect is DuckDbDialect | GenericDialect) => {
3271                Some(BinaryOperator::DuckIntegerDivide)
3272            }
3273            Token::ShiftLeft if dialect_is!(dialect is PostgreSqlDialect | DuckDbDialect | GenericDialect | RedshiftSqlDialect) => {
3274                Some(BinaryOperator::PGBitwiseShiftLeft)
3275            }
3276            Token::ShiftRight if dialect_is!(dialect is PostgreSqlDialect | DuckDbDialect | GenericDialect | RedshiftSqlDialect) => {
3277                Some(BinaryOperator::PGBitwiseShiftRight)
3278            }
3279            Token::Sharp if dialect_is!(dialect is PostgreSqlDialect | RedshiftSqlDialect) => {
3280                Some(BinaryOperator::PGBitwiseXor)
3281            }
3282            Token::Overlap if dialect_is!(dialect is PostgreSqlDialect | RedshiftSqlDialect) => {
3283                Some(BinaryOperator::PGOverlap)
3284            }
3285            Token::Overlap if dialect_is!(dialect is PostgreSqlDialect | GenericDialect) => {
3286                Some(BinaryOperator::PGOverlap)
3287            }
3288            Token::CaretAt if dialect_is!(dialect is PostgreSqlDialect | GenericDialect) => {
3289                Some(BinaryOperator::PGStartsWith)
3290            }
3291            Token::Tilde => Some(BinaryOperator::PGRegexMatch),
3292            Token::TildeAsterisk => Some(BinaryOperator::PGRegexIMatch),
3293            Token::ExclamationMarkTilde => Some(BinaryOperator::PGRegexNotMatch),
3294            Token::ExclamationMarkTildeAsterisk => Some(BinaryOperator::PGRegexNotIMatch),
3295            Token::DoubleTilde => Some(BinaryOperator::PGLikeMatch),
3296            Token::DoubleTildeAsterisk => Some(BinaryOperator::PGILikeMatch),
3297            Token::ExclamationMarkDoubleTilde => Some(BinaryOperator::PGNotLikeMatch),
3298            Token::ExclamationMarkDoubleTildeAsterisk => Some(BinaryOperator::PGNotILikeMatch),
3299            Token::Arrow => Some(BinaryOperator::Arrow),
3300            Token::LongArrow => Some(BinaryOperator::LongArrow),
3301            Token::HashArrow => Some(BinaryOperator::HashArrow),
3302            Token::HashLongArrow => Some(BinaryOperator::HashLongArrow),
3303            Token::AtArrow => Some(BinaryOperator::AtArrow),
3304            Token::ArrowAt => Some(BinaryOperator::ArrowAt),
3305            Token::HashMinus => Some(BinaryOperator::HashMinus),
3306            Token::AtQuestion => Some(BinaryOperator::AtQuestion),
3307            Token::AtAt => Some(BinaryOperator::AtAt),
3308            Token::Question => Some(BinaryOperator::Question),
3309            Token::QuestionAnd => Some(BinaryOperator::QuestionAnd),
3310            Token::QuestionPipe => Some(BinaryOperator::QuestionPipe),
3311            Token::CustomBinaryOperator(s) => Some(BinaryOperator::Custom(s.clone())),
3312            Token::DoubleSharp if self.dialect.supports_geometric_types() => {
3313                Some(BinaryOperator::DoubleHash)
3314            }
3315
3316            Token::AmpersandLeftAngleBracket if self.dialect.supports_geometric_types() => {
3317                Some(BinaryOperator::AndLt)
3318            }
3319            Token::AmpersandRightAngleBracket if self.dialect.supports_geometric_types() => {
3320                Some(BinaryOperator::AndGt)
3321            }
3322            Token::QuestionMarkDash if self.dialect.supports_geometric_types() => {
3323                Some(BinaryOperator::QuestionDash)
3324            }
3325            Token::AmpersandLeftAngleBracketVerticalBar
3326                if self.dialect.supports_geometric_types() =>
3327            {
3328                Some(BinaryOperator::AndLtPipe)
3329            }
3330            Token::VerticalBarAmpersandRightAngleBracket
3331                if self.dialect.supports_geometric_types() =>
3332            {
3333                Some(BinaryOperator::PipeAndGt)
3334            }
3335            Token::TwoWayArrow if self.dialect.supports_geometric_types() => {
3336                Some(BinaryOperator::LtDashGt)
3337            }
3338            Token::LeftAngleBracketCaret if self.dialect.supports_geometric_types() => {
3339                Some(BinaryOperator::LtCaret)
3340            }
3341            Token::RightAngleBracketCaret if self.dialect.supports_geometric_types() => {
3342                Some(BinaryOperator::GtCaret)
3343            }
3344            Token::QuestionMarkSharp if self.dialect.supports_geometric_types() => {
3345                Some(BinaryOperator::QuestionHash)
3346            }
3347            Token::QuestionMarkDoubleVerticalBar if self.dialect.supports_geometric_types() => {
3348                Some(BinaryOperator::QuestionDoublePipe)
3349            }
3350            Token::QuestionMarkDashVerticalBar if self.dialect.supports_geometric_types() => {
3351                Some(BinaryOperator::QuestionDashPipe)
3352            }
3353            Token::TildeEqual if self.dialect.supports_geometric_types() => {
3354                Some(BinaryOperator::TildeEq)
3355            }
3356            Token::ShiftLeftVerticalBar if self.dialect.supports_geometric_types() => {
3357                Some(BinaryOperator::LtLtPipe)
3358            }
3359            Token::VerticalBarShiftRight if self.dialect.supports_geometric_types() => {
3360                Some(BinaryOperator::PipeGtGt)
3361            }
3362            Token::AtSign if self.dialect.supports_geometric_types() => Some(BinaryOperator::At),
3363
3364            Token::Word(w) => match w.keyword {
3365                Keyword::AND => Some(BinaryOperator::And),
3366                Keyword::OR => Some(BinaryOperator::Or),
3367                Keyword::XOR => Some(BinaryOperator::Xor),
3368                Keyword::OVERLAPS => Some(BinaryOperator::Overlaps),
3369                Keyword::OPERATOR if dialect_is!(dialect is PostgreSqlDialect | GenericDialect) => {
3370                    self.expect_token(&Token::LParen)?;
3371                    // there are special rules for operator names in
3372                    // postgres so we can not use 'parse_object'
3373                    // or similar.
3374                    // See https://www.postgresql.org/docs/current/sql-createoperator.html
3375                    let mut idents = vec![];
3376                    loop {
3377                        self.advance_token();
3378                        idents.push(self.get_current_token().to_string());
3379                        if !self.consume_token(&Token::Period) {
3380                            break;
3381                        }
3382                    }
3383                    self.expect_token(&Token::RParen)?;
3384                    Some(BinaryOperator::PGCustomBinaryOperator(idents))
3385                }
3386                _ => None,
3387            },
3388            _ => None,
3389        };
3390
3391        let tok = self.token_at(tok_index);
3392        if let Some(op) = regular_binary_operator {
3393            if let Some(keyword) =
3394                self.parse_one_of_keywords(&[Keyword::ANY, Keyword::ALL, Keyword::SOME])
3395            {
3396                self.expect_token(&Token::LParen)?;
3397                let right = if self.peek_sub_query() {
3398                    // We have a subquery ahead (SELECT\WITH ...) need to rewind and
3399                    // use the parenthesis for parsing the subquery as an expression.
3400                    self.prev_token(); // LParen
3401                    self.parse_subexpr(precedence)?
3402                } else {
3403                    // Non-subquery expression
3404                    let right = self.parse_subexpr(precedence)?;
3405                    self.expect_token(&Token::RParen)?;
3406                    right
3407                };
3408
3409                if !matches!(
3410                    op,
3411                    BinaryOperator::Gt
3412                        | BinaryOperator::Lt
3413                        | BinaryOperator::GtEq
3414                        | BinaryOperator::LtEq
3415                        | BinaryOperator::Eq
3416                        | BinaryOperator::NotEq
3417                ) {
3418                    return parser_err!(
3419                        format!(
3420                        "Expected one of [=, >, <, =>, =<, !=] as comparison operator, found: {op}"
3421                    ),
3422                        span.start
3423                    );
3424                };
3425
3426                Ok(match keyword {
3427                    Keyword::ALL => Expr::AllOp {
3428                        left: Box::new(expr),
3429                        compare_op: op,
3430                        right: Box::new(right),
3431                    },
3432                    Keyword::ANY | Keyword::SOME => Expr::AnyOp {
3433                        left: Box::new(expr),
3434                        compare_op: op,
3435                        right: Box::new(right),
3436                        is_some: keyword == Keyword::SOME,
3437                    },
3438                    _ => unreachable!(),
3439                })
3440            } else {
3441                Ok(Expr::BinaryOp {
3442                    left: Box::new(expr),
3443                    op,
3444                    right: Box::new(self.parse_subexpr(precedence)?),
3445                })
3446            }
3447        } else if let Token::Word(w) = &tok.token {
3448            match w.keyword {
3449                Keyword::IS => {
3450                    if self.parse_keyword(Keyword::NULL) {
3451                        Ok(Expr::IsNull(Box::new(expr)))
3452                    } else if self.parse_keywords(&[Keyword::NOT, Keyword::NULL]) {
3453                        Ok(Expr::IsNotNull(Box::new(expr)))
3454                    } else if self.parse_keywords(&[Keyword::TRUE]) {
3455                        Ok(Expr::IsTrue(Box::new(expr)))
3456                    } else if self.parse_keywords(&[Keyword::NOT, Keyword::TRUE]) {
3457                        Ok(Expr::IsNotTrue(Box::new(expr)))
3458                    } else if self.parse_keywords(&[Keyword::FALSE]) {
3459                        Ok(Expr::IsFalse(Box::new(expr)))
3460                    } else if self.parse_keywords(&[Keyword::NOT, Keyword::FALSE]) {
3461                        Ok(Expr::IsNotFalse(Box::new(expr)))
3462                    } else if self.parse_keywords(&[Keyword::UNKNOWN]) {
3463                        Ok(Expr::IsUnknown(Box::new(expr)))
3464                    } else if self.parse_keywords(&[Keyword::NOT, Keyword::UNKNOWN]) {
3465                        Ok(Expr::IsNotUnknown(Box::new(expr)))
3466                    } else if self.parse_keywords(&[Keyword::DISTINCT, Keyword::FROM]) {
3467                        let expr2 = self.parse_expr()?;
3468                        Ok(Expr::IsDistinctFrom(Box::new(expr), Box::new(expr2)))
3469                    } else if self.parse_keywords(&[Keyword::NOT, Keyword::DISTINCT, Keyword::FROM])
3470                    {
3471                        let expr2 = self.parse_expr()?;
3472                        Ok(Expr::IsNotDistinctFrom(Box::new(expr), Box::new(expr2)))
3473                    } else if let Ok(is_normalized) = self.parse_unicode_is_normalized(expr) {
3474                        Ok(is_normalized)
3475                    } else {
3476                        self.expected(
3477                            "[NOT] NULL | TRUE | FALSE | DISTINCT | [form] NORMALIZED FROM after IS",
3478                            self.peek_token(),
3479                        )
3480                    }
3481                }
3482                Keyword::AT => {
3483                    self.expect_keywords(&[Keyword::TIME, Keyword::ZONE])?;
3484                    Ok(Expr::AtTimeZone {
3485                        timestamp: Box::new(expr),
3486                        time_zone: Box::new(self.parse_subexpr(precedence)?),
3487                    })
3488                }
3489                Keyword::NOT
3490                | Keyword::IN
3491                | Keyword::BETWEEN
3492                | Keyword::LIKE
3493                | Keyword::ILIKE
3494                | Keyword::SIMILAR
3495                | Keyword::REGEXP
3496                | Keyword::RLIKE => {
3497                    self.prev_token();
3498                    let negated = self.parse_keyword(Keyword::NOT);
3499                    let regexp = self.parse_keyword(Keyword::REGEXP);
3500                    let rlike = self.parse_keyword(Keyword::RLIKE);
3501                    if regexp || rlike {
3502                        Ok(Expr::RLike {
3503                            negated,
3504                            expr: Box::new(expr),
3505                            pattern: Box::new(
3506                                self.parse_subexpr(self.dialect.prec_value(Precedence::Like))?,
3507                            ),
3508                            regexp,
3509                        })
3510                    } else if self.parse_keyword(Keyword::IN) {
3511                        self.parse_in(expr, negated)
3512                    } else if self.parse_keyword(Keyword::BETWEEN) {
3513                        self.parse_between(expr, negated)
3514                    } else if self.parse_keyword(Keyword::LIKE) {
3515                        Ok(Expr::Like {
3516                            negated,
3517                            any: self.parse_keyword(Keyword::ANY),
3518                            expr: Box::new(expr),
3519                            pattern: Box::new(
3520                                self.parse_subexpr(self.dialect.prec_value(Precedence::Like))?,
3521                            ),
3522                            escape_char: self.parse_escape_char()?,
3523                        })
3524                    } else if self.parse_keyword(Keyword::ILIKE) {
3525                        Ok(Expr::ILike {
3526                            negated,
3527                            any: self.parse_keyword(Keyword::ANY),
3528                            expr: Box::new(expr),
3529                            pattern: Box::new(
3530                                self.parse_subexpr(self.dialect.prec_value(Precedence::Like))?,
3531                            ),
3532                            escape_char: self.parse_escape_char()?,
3533                        })
3534                    } else if self.parse_keywords(&[Keyword::SIMILAR, Keyword::TO]) {
3535                        Ok(Expr::SimilarTo {
3536                            negated,
3537                            expr: Box::new(expr),
3538                            pattern: Box::new(
3539                                self.parse_subexpr(self.dialect.prec_value(Precedence::Like))?,
3540                            ),
3541                            escape_char: self.parse_escape_char()?,
3542                        })
3543                    } else {
3544                        self.expected("IN or BETWEEN after NOT", self.peek_token())
3545                    }
3546                }
3547                // Can only happen if `get_next_precedence` got out of sync with this function
3548                _ => parser_err!(
3549                    format!("No infix parser for token {:?}", tok.token),
3550                    tok.span.start
3551                ),
3552            }
3553        } else if Token::DoubleColon == *tok {
3554            Ok(Expr::Cast {
3555                kind: CastKind::DoubleColon,
3556                expr: Box::new(expr),
3557                data_type: self.parse_data_type()?,
3558                format: None,
3559            })
3560        } else if Token::ExclamationMark == *tok && self.dialect.supports_factorial_operator() {
3561            Ok(Expr::UnaryOp {
3562                op: UnaryOperator::PGPostfixFactorial,
3563                expr: Box::new(expr),
3564            })
3565        } else if Token::LBracket == *tok && self.dialect.supports_partiql()
3566            || (dialect_of!(self is SnowflakeDialect | GenericDialect) && Token::Colon == *tok)
3567        {
3568            self.prev_token();
3569            self.parse_json_access(expr)
3570        } else {
3571            // Can only happen if `get_next_precedence` got out of sync with this function
3572            parser_err!(
3573                format!("No infix parser for token {:?}", tok.token),
3574                tok.span.start
3575            )
3576        }
3577    }
3578
3579    /// Parse the `ESCAPE CHAR` portion of `LIKE`, `ILIKE`, and `SIMILAR TO`
3580    pub fn parse_escape_char(&mut self) -> Result<Option<String>, ParserError> {
3581        if self.parse_keyword(Keyword::ESCAPE) {
3582            Ok(Some(self.parse_literal_string()?))
3583        } else {
3584            Ok(None)
3585        }
3586    }
3587
3588    /// Parses an array subscript like
3589    /// * `[:]`
3590    /// * `[l]`
3591    /// * `[l:]`
3592    /// * `[:u]`
3593    /// * `[l:u]`
3594    /// * `[l:u:s]`
3595    ///
3596    /// Parser is right after `[`
3597    fn parse_subscript_inner(&mut self) -> Result<Subscript, ParserError> {
3598        // at either `<lower>:(rest)` or `:(rest)]`
3599        let lower_bound = if self.consume_token(&Token::Colon) {
3600            None
3601        } else {
3602            Some(self.parse_expr()?)
3603        };
3604
3605        // check for end
3606        if self.consume_token(&Token::RBracket) {
3607            if let Some(lower_bound) = lower_bound {
3608                return Ok(Subscript::Index { index: lower_bound });
3609            };
3610            return Ok(Subscript::Slice {
3611                lower_bound,
3612                upper_bound: None,
3613                stride: None,
3614            });
3615        }
3616
3617        // consume the `:`
3618        if lower_bound.is_some() {
3619            self.expect_token(&Token::Colon)?;
3620        }
3621
3622        // we are now at either `]`, `<upper>(rest)]`
3623        let upper_bound = if self.consume_token(&Token::RBracket) {
3624            return Ok(Subscript::Slice {
3625                lower_bound,
3626                upper_bound: None,
3627                stride: None,
3628            });
3629        } else {
3630            Some(self.parse_expr()?)
3631        };
3632
3633        // check for end
3634        if self.consume_token(&Token::RBracket) {
3635            return Ok(Subscript::Slice {
3636                lower_bound,
3637                upper_bound,
3638                stride: None,
3639            });
3640        }
3641
3642        // we are now at `:]` or `:stride]`
3643        self.expect_token(&Token::Colon)?;
3644        let stride = if self.consume_token(&Token::RBracket) {
3645            None
3646        } else {
3647            Some(self.parse_expr()?)
3648        };
3649
3650        if stride.is_some() {
3651            self.expect_token(&Token::RBracket)?;
3652        }
3653
3654        Ok(Subscript::Slice {
3655            lower_bound,
3656            upper_bound,
3657            stride,
3658        })
3659    }
3660
3661    /// Parse a multi-dimension array accessing like `[1:3][1][1]`
3662    pub fn parse_multi_dim_subscript(
3663        &mut self,
3664        chain: &mut Vec<AccessExpr>,
3665    ) -> Result<(), ParserError> {
3666        while self.consume_token(&Token::LBracket) {
3667            self.parse_subscript(chain)?;
3668        }
3669        Ok(())
3670    }
3671
3672    /// Parses an array subscript like `[1:3]`
3673    ///
3674    /// Parser is right after `[`
3675    fn parse_subscript(&mut self, chain: &mut Vec<AccessExpr>) -> Result<(), ParserError> {
3676        let subscript = self.parse_subscript_inner()?;
3677        chain.push(AccessExpr::Subscript(subscript));
3678        Ok(())
3679    }
3680
3681    fn parse_json_path_object_key(&mut self) -> Result<JsonPathElem, ParserError> {
3682        let token = self.next_token();
3683        match token.token {
3684            Token::Word(Word {
3685                value,
3686                // path segments in SF dot notation can be unquoted or double-quoted
3687                quote_style: quote_style @ (Some('"') | None),
3688                // some experimentation suggests that snowflake permits
3689                // any keyword here unquoted.
3690                keyword: _,
3691            }) => Ok(JsonPathElem::Dot {
3692                key: value,
3693                quoted: quote_style.is_some(),
3694            }),
3695
3696            // This token should never be generated on snowflake or generic
3697            // dialects, but we handle it just in case this is used on future
3698            // dialects.
3699            Token::DoubleQuotedString(key) => Ok(JsonPathElem::Dot { key, quoted: true }),
3700
3701            _ => self.expected("variant object key name", token),
3702        }
3703    }
3704
3705    fn parse_json_access(&mut self, expr: Expr) -> Result<Expr, ParserError> {
3706        let path = self.parse_json_path()?;
3707        Ok(Expr::JsonAccess {
3708            value: Box::new(expr),
3709            path,
3710        })
3711    }
3712
3713    fn parse_json_path(&mut self) -> Result<JsonPath, ParserError> {
3714        let mut path = Vec::new();
3715        loop {
3716            match self.next_token().token {
3717                Token::Colon if path.is_empty() => {
3718                    path.push(self.parse_json_path_object_key()?);
3719                }
3720                Token::Period if !path.is_empty() => {
3721                    path.push(self.parse_json_path_object_key()?);
3722                }
3723                Token::LBracket => {
3724                    let key = self.parse_expr()?;
3725                    self.expect_token(&Token::RBracket)?;
3726
3727                    path.push(JsonPathElem::Bracket { key });
3728                }
3729                _ => {
3730                    self.prev_token();
3731                    break;
3732                }
3733            };
3734        }
3735
3736        debug_assert!(!path.is_empty());
3737        Ok(JsonPath { path })
3738    }
3739
3740    /// Parses the parens following the `[ NOT ] IN` operator.
3741    pub fn parse_in(&mut self, expr: Expr, negated: bool) -> Result<Expr, ParserError> {
3742        // BigQuery allows `IN UNNEST(array_expression)`
3743        // https://cloud.google.com/bigquery/docs/reference/standard-sql/operators#in_operators
3744        if self.parse_keyword(Keyword::UNNEST) {
3745            self.expect_token(&Token::LParen)?;
3746            let array_expr = self.parse_expr()?;
3747            self.expect_token(&Token::RParen)?;
3748            return Ok(Expr::InUnnest {
3749                expr: Box::new(expr),
3750                array_expr: Box::new(array_expr),
3751                negated,
3752            });
3753        }
3754        self.expect_token(&Token::LParen)?;
3755        let in_op = match self.maybe_parse(|p| p.parse_query_body(p.dialect.prec_unknown()))? {
3756            Some(subquery) => Expr::InSubquery {
3757                expr: Box::new(expr),
3758                subquery,
3759                negated,
3760            },
3761            None => Expr::InList {
3762                expr: Box::new(expr),
3763                list: if self.dialect.supports_in_empty_list() {
3764                    self.parse_comma_separated0(Parser::parse_expr, Token::RParen)?
3765                } else {
3766                    self.parse_comma_separated(Parser::parse_expr)?
3767                },
3768                negated,
3769            },
3770        };
3771        self.expect_token(&Token::RParen)?;
3772        Ok(in_op)
3773    }
3774
3775    /// Parses `BETWEEN <low> AND <high>`, assuming the `BETWEEN` keyword was already consumed.
3776    pub fn parse_between(&mut self, expr: Expr, negated: bool) -> Result<Expr, ParserError> {
3777        // Stop parsing subexpressions for <low> and <high> on tokens with
3778        // precedence lower than that of `BETWEEN`, such as `AND`, `IS`, etc.
3779        let low = self.parse_subexpr(self.dialect.prec_value(Precedence::Between))?;
3780        self.expect_keyword_is(Keyword::AND)?;
3781        let high = self.parse_subexpr(self.dialect.prec_value(Precedence::Between))?;
3782        Ok(Expr::Between {
3783            expr: Box::new(expr),
3784            negated,
3785            low: Box::new(low),
3786            high: Box::new(high),
3787        })
3788    }
3789
3790    /// Parse a PostgreSQL casting style which is in the form of `expr::datatype`.
3791    pub fn parse_pg_cast(&mut self, expr: Expr) -> Result<Expr, ParserError> {
3792        Ok(Expr::Cast {
3793            kind: CastKind::DoubleColon,
3794            expr: Box::new(expr),
3795            data_type: self.parse_data_type()?,
3796            format: None,
3797        })
3798    }
3799
3800    /// Get the precedence of the next token
3801    pub fn get_next_precedence(&self) -> Result<u8, ParserError> {
3802        self.dialect.get_next_precedence_default(self)
3803    }
3804
3805    /// Return the token at the given location, or EOF if the index is beyond
3806    /// the length of the current set of tokens.
3807    pub fn token_at(&self, index: usize) -> &TokenWithSpan {
3808        self.tokens.get(index).unwrap_or(&EOF_TOKEN)
3809    }
3810
3811    /// Return the first non-whitespace token that has not yet been processed
3812    /// or Token::EOF
3813    ///
3814    /// See [`Self::peek_token_ref`] to avoid the copy.
3815    pub fn peek_token(&self) -> TokenWithSpan {
3816        self.peek_nth_token(0)
3817    }
3818
3819    /// Return a reference to the first non-whitespace token that has not yet
3820    /// been processed or Token::EOF
3821    pub fn peek_token_ref(&self) -> &TokenWithSpan {
3822        self.peek_nth_token_ref(0)
3823    }
3824
3825    /// Returns the `N` next non-whitespace tokens that have not yet been
3826    /// processed.
3827    ///
3828    /// Example:
3829    /// ```rust
3830    /// # use sqlparser::dialect::GenericDialect;
3831    /// # use sqlparser::parser::Parser;
3832    /// # use sqlparser::keywords::Keyword;
3833    /// # use sqlparser::tokenizer::{Token, Word};
3834    /// let dialect = GenericDialect {};
3835    /// let mut parser = Parser::new(&dialect).try_with_sql("ORDER BY foo, bar").unwrap();
3836    ///
3837    /// // Note that Rust infers the number of tokens to peek based on the
3838    /// // length of the slice pattern!
3839    /// assert!(matches!(
3840    ///     parser.peek_tokens(),
3841    ///     [
3842    ///         Token::Word(Word { keyword: Keyword::ORDER, .. }),
3843    ///         Token::Word(Word { keyword: Keyword::BY, .. }),
3844    ///     ]
3845    /// ));
3846    /// ```
3847    pub fn peek_tokens<const N: usize>(&self) -> [Token; N] {
3848        self.peek_tokens_with_location()
3849            .map(|with_loc| with_loc.token)
3850    }
3851
3852    /// Returns the `N` next non-whitespace tokens with locations that have not
3853    /// yet been processed.
3854    ///
3855    /// See [`Self::peek_token`] for an example.
3856    pub fn peek_tokens_with_location<const N: usize>(&self) -> [TokenWithSpan; N] {
3857        let mut index = self.index;
3858        core::array::from_fn(|_| loop {
3859            let token = self.tokens.get(index);
3860            index += 1;
3861            if let Some(TokenWithSpan {
3862                token: Token::Whitespace(_),
3863                span: _,
3864            }) = token
3865            {
3866                continue;
3867            }
3868            break token.cloned().unwrap_or(TokenWithSpan {
3869                token: Token::EOF,
3870                span: Span::empty(),
3871            });
3872        })
3873    }
3874
3875    /// Returns references to the `N` next non-whitespace tokens
3876    /// that have not yet been processed.
3877    ///
3878    /// See [`Self::peek_tokens`] for an example.
3879    pub fn peek_tokens_ref<const N: usize>(&self) -> [&TokenWithSpan; N] {
3880        let mut index = self.index;
3881        core::array::from_fn(|_| loop {
3882            let token = self.tokens.get(index);
3883            index += 1;
3884            if let Some(TokenWithSpan {
3885                token: Token::Whitespace(_),
3886                span: _,
3887            }) = token
3888            {
3889                continue;
3890            }
3891            break token.unwrap_or(&EOF_TOKEN);
3892        })
3893    }
3894
3895    /// Return nth non-whitespace token that has not yet been processed
3896    pub fn peek_nth_token(&self, n: usize) -> TokenWithSpan {
3897        self.peek_nth_token_ref(n).clone()
3898    }
3899
3900    /// Return nth non-whitespace token that has not yet been processed
3901    pub fn peek_nth_token_ref(&self, mut n: usize) -> &TokenWithSpan {
3902        let mut index = self.index;
3903        loop {
3904            index += 1;
3905            match self.tokens.get(index - 1) {
3906                Some(TokenWithSpan {
3907                    token: Token::Whitespace(_),
3908                    span: _,
3909                }) => continue,
3910                non_whitespace => {
3911                    if n == 0 {
3912                        return non_whitespace.unwrap_or(&EOF_TOKEN);
3913                    }
3914                    n -= 1;
3915                }
3916            }
3917        }
3918    }
3919
3920    /// Return the first token, possibly whitespace, that has not yet been processed
3921    /// (or None if reached end-of-file).
3922    pub fn peek_token_no_skip(&self) -> TokenWithSpan {
3923        self.peek_nth_token_no_skip(0)
3924    }
3925
3926    /// Return nth token, possibly whitespace, that has not yet been processed.
3927    pub fn peek_nth_token_no_skip(&self, n: usize) -> TokenWithSpan {
3928        self.tokens
3929            .get(self.index + n)
3930            .cloned()
3931            .unwrap_or(TokenWithSpan {
3932                token: Token::EOF,
3933                span: Span::empty(),
3934            })
3935    }
3936
3937    /// Return true if the next tokens exactly `expected`
3938    ///
3939    /// Does not advance the current token.
3940    fn peek_keywords(&mut self, expected: &[Keyword]) -> bool {
3941        let index = self.index;
3942        let matched = self.parse_keywords(expected);
3943        self.index = index;
3944        matched
3945    }
3946
3947    /// Advances to the next non-whitespace token and returns a copy.
3948    ///
3949    /// Please use [`Self::advance_token`] and [`Self::get_current_token`] to
3950    /// avoid the copy.
3951    pub fn next_token(&mut self) -> TokenWithSpan {
3952        self.advance_token();
3953        self.get_current_token().clone()
3954    }
3955
3956    /// Returns the index of the current token
3957    ///
3958    /// This can be used with APIs that expect an index, such as
3959    /// [`Self::token_at`]
3960    pub fn get_current_index(&self) -> usize {
3961        self.index.saturating_sub(1)
3962    }
3963
3964    /// Return the next unprocessed token, possibly whitespace.
3965    pub fn next_token_no_skip(&mut self) -> Option<&TokenWithSpan> {
3966        self.index += 1;
3967        self.tokens.get(self.index - 1)
3968    }
3969
3970    /// Advances the current token to the next non-whitespace token
3971    ///
3972    /// See [`Self::get_current_token`] to get the current token after advancing
3973    pub fn advance_token(&mut self) {
3974        loop {
3975            self.index += 1;
3976            match self.tokens.get(self.index - 1) {
3977                Some(TokenWithSpan {
3978                    token: Token::Whitespace(_),
3979                    span: _,
3980                }) => continue,
3981                _ => break,
3982            }
3983        }
3984    }
3985
3986    /// Returns a reference to the current token
3987    ///
3988    /// Does not advance the current token.
3989    pub fn get_current_token(&self) -> &TokenWithSpan {
3990        self.token_at(self.index.saturating_sub(1))
3991    }
3992
3993    /// Returns a reference to the previous token
3994    ///
3995    /// Does not advance the current token.
3996    pub fn get_previous_token(&self) -> &TokenWithSpan {
3997        self.token_at(self.index.saturating_sub(2))
3998    }
3999
4000    /// Returns a reference to the next token
4001    ///
4002    /// Does not advance the current token.
4003    pub fn get_next_token(&self) -> &TokenWithSpan {
4004        self.token_at(self.index)
4005    }
4006
4007    /// Seek back the last one non-whitespace token.
4008    ///
4009    /// Must be called after `next_token()`, otherwise might panic. OK to call
4010    /// after `next_token()` indicates an EOF.
4011    ///
4012    // TODO rename to backup_token and deprecate prev_token?
4013    pub fn prev_token(&mut self) {
4014        loop {
4015            assert!(self.index > 0);
4016            self.index -= 1;
4017            if let Some(TokenWithSpan {
4018                token: Token::Whitespace(_),
4019                span: _,
4020            }) = self.tokens.get(self.index)
4021            {
4022                continue;
4023            }
4024            return;
4025        }
4026    }
4027
4028    /// Report `found` was encountered instead of `expected`
4029    pub fn expected<T>(&self, expected: &str, found: TokenWithSpan) -> Result<T, ParserError> {
4030        parser_err!(
4031            format!("Expected: {expected}, found: {found}"),
4032            found.span.start
4033        )
4034    }
4035
4036    /// report `found` was encountered instead of `expected`
4037    pub fn expected_ref<T>(&self, expected: &str, found: &TokenWithSpan) -> Result<T, ParserError> {
4038        parser_err!(
4039            format!("Expected: {expected}, found: {found}"),
4040            found.span.start
4041        )
4042    }
4043
4044    /// Report that the token at `index` was found instead of `expected`.
4045    pub fn expected_at<T>(&self, expected: &str, index: usize) -> Result<T, ParserError> {
4046        let found = self.tokens.get(index).unwrap_or(&EOF_TOKEN);
4047        parser_err!(
4048            format!("Expected: {expected}, found: {found}"),
4049            found.span.start
4050        )
4051    }
4052
4053    /// If the current token is the `expected` keyword, consume it and returns
4054    /// true. Otherwise, no tokens are consumed and returns false.
4055    #[must_use]
4056    pub fn parse_keyword(&mut self, expected: Keyword) -> bool {
4057        if self.peek_keyword(expected) {
4058            self.advance_token();
4059            true
4060        } else {
4061            false
4062        }
4063    }
4064
4065    #[must_use]
4066    pub fn peek_keyword(&self, expected: Keyword) -> bool {
4067        matches!(&self.peek_token_ref().token, Token::Word(w) if expected == w.keyword)
4068    }
4069
4070    /// If the current token is the `expected` keyword followed by
4071    /// specified tokens, consume them and returns true.
4072    /// Otherwise, no tokens are consumed and returns false.
4073    ///
4074    /// Note that if the length of `tokens` is too long, this function will
4075    /// not be efficient as it does a loop on the tokens with `peek_nth_token`
4076    /// each time.
4077    pub fn parse_keyword_with_tokens(&mut self, expected: Keyword, tokens: &[Token]) -> bool {
4078        match &self.peek_token_ref().token {
4079            Token::Word(w) if expected == w.keyword => {
4080                for (idx, token) in tokens.iter().enumerate() {
4081                    if self.peek_nth_token_ref(idx + 1).token != *token {
4082                        return false;
4083                    }
4084                }
4085                // consume all tokens
4086                for _ in 0..(tokens.len() + 1) {
4087                    self.advance_token();
4088                }
4089                true
4090            }
4091            _ => false,
4092        }
4093    }
4094
4095    /// If the current and subsequent tokens exactly match the `keywords`
4096    /// sequence, consume them and returns true. Otherwise, no tokens are
4097    /// consumed and returns false
4098    #[must_use]
4099    pub fn parse_keywords(&mut self, keywords: &[Keyword]) -> bool {
4100        let index = self.index;
4101        for &keyword in keywords {
4102            if !self.parse_keyword(keyword) {
4103                // println!("parse_keywords aborting .. did not find {:?}", keyword);
4104                // reset index and return immediately
4105                self.index = index;
4106                return false;
4107            }
4108        }
4109        true
4110    }
4111
4112    /// If the current token is one of the given `keywords`, returns the keyword
4113    /// that matches, without consuming the token. Otherwise, returns [`None`].
4114    #[must_use]
4115    pub fn peek_one_of_keywords(&self, keywords: &[Keyword]) -> Option<Keyword> {
4116        for keyword in keywords {
4117            if self.peek_keyword(*keyword) {
4118                return Some(*keyword);
4119            }
4120        }
4121        None
4122    }
4123
4124    /// If the current token is one of the given `keywords`, consume the token
4125    /// and return the keyword that matches. Otherwise, no tokens are consumed
4126    /// and returns [`None`].
4127    #[must_use]
4128    pub fn parse_one_of_keywords(&mut self, keywords: &[Keyword]) -> Option<Keyword> {
4129        match &self.peek_token_ref().token {
4130            Token::Word(w) => {
4131                keywords
4132                    .iter()
4133                    .find(|keyword| **keyword == w.keyword)
4134                    .map(|keyword| {
4135                        self.advance_token();
4136                        *keyword
4137                    })
4138            }
4139            _ => None,
4140        }
4141    }
4142
4143    /// If the current token is one of the expected keywords, consume the token
4144    /// and return the keyword that matches. Otherwise, return an error.
4145    pub fn expect_one_of_keywords(&mut self, keywords: &[Keyword]) -> Result<Keyword, ParserError> {
4146        if let Some(keyword) = self.parse_one_of_keywords(keywords) {
4147            Ok(keyword)
4148        } else {
4149            let keywords: Vec<String> = keywords.iter().map(|x| format!("{x:?}")).collect();
4150            self.expected_ref(
4151                &format!("one of {}", keywords.join(" or ")),
4152                self.peek_token_ref(),
4153            )
4154        }
4155    }
4156
4157    /// If the current token is the `expected` keyword, consume the token.
4158    /// Otherwise, return an error.
4159    ///
4160    // todo deprecate in favor of expected_keyword_is
4161    pub fn expect_keyword(&mut self, expected: Keyword) -> Result<TokenWithSpan, ParserError> {
4162        if self.parse_keyword(expected) {
4163            Ok(self.get_current_token().clone())
4164        } else {
4165            self.expected_ref(format!("{:?}", &expected).as_str(), self.peek_token_ref())
4166        }
4167    }
4168
4169    /// If the current token is the `expected` keyword, consume the token.
4170    /// Otherwise, return an error.
4171    ///
4172    /// This differs from expect_keyword only in that the matched keyword
4173    /// token is not returned.
4174    pub fn expect_keyword_is(&mut self, expected: Keyword) -> Result<(), ParserError> {
4175        if self.parse_keyword(expected) {
4176            Ok(())
4177        } else {
4178            self.expected_ref(format!("{:?}", &expected).as_str(), self.peek_token_ref())
4179        }
4180    }
4181
4182    /// If the current and subsequent tokens exactly match the `keywords`
4183    /// sequence, consume them and returns Ok. Otherwise, return an Error.
4184    pub fn expect_keywords(&mut self, expected: &[Keyword]) -> Result<(), ParserError> {
4185        for &kw in expected {
4186            self.expect_keyword_is(kw)?;
4187        }
4188        Ok(())
4189    }
4190
4191    /// Consume the next token if it matches the expected token, otherwise return false
4192    ///
4193    /// See [Self::advance_token] to consume the token unconditionally
4194    #[must_use]
4195    pub fn consume_token(&mut self, expected: &Token) -> bool {
4196        if self.peek_token_ref() == expected {
4197            self.advance_token();
4198            true
4199        } else {
4200            false
4201        }
4202    }
4203
4204    /// If the current and subsequent tokens exactly match the `tokens`
4205    /// sequence, consume them and returns true. Otherwise, no tokens are
4206    /// consumed and returns false
4207    #[must_use]
4208    pub fn consume_tokens(&mut self, tokens: &[Token]) -> bool {
4209        let index = self.index;
4210        for token in tokens {
4211            if !self.consume_token(token) {
4212                self.index = index;
4213                return false;
4214            }
4215        }
4216        true
4217    }
4218
4219    /// Bail out if the current token is not an expected keyword, or consume it if it is
4220    pub fn expect_token(&mut self, expected: &Token) -> Result<TokenWithSpan, ParserError> {
4221        if self.peek_token_ref() == expected {
4222            Ok(self.next_token())
4223        } else {
4224            self.expected_ref(&expected.to_string(), self.peek_token_ref())
4225        }
4226    }
4227
4228    fn parse<T: FromStr>(s: String, loc: Location) -> Result<T, ParserError>
4229    where
4230        <T as FromStr>::Err: Display,
4231    {
4232        s.parse::<T>().map_err(|e| {
4233            ParserError::ParserError(format!(
4234                "Could not parse '{s}' as {}: {e}{loc}",
4235                core::any::type_name::<T>()
4236            ))
4237        })
4238    }
4239
4240    /// Parse a comma-separated list of 1+ SelectItem
4241    pub fn parse_projection(&mut self) -> Result<Vec<SelectItem>, ParserError> {
4242        // BigQuery and Snowflake allow trailing commas, but only in project lists
4243        // e.g. `SELECT 1, 2, FROM t`
4244        // https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical#trailing_commas
4245        // https://docs.snowflake.com/en/release-notes/2024/8_11#select-supports-trailing-commas
4246
4247        let trailing_commas =
4248            self.options.trailing_commas | self.dialect.supports_projection_trailing_commas();
4249
4250        self.parse_comma_separated_with_trailing_commas(
4251            |p| p.parse_select_item(),
4252            trailing_commas,
4253            Self::is_reserved_for_column_alias,
4254        )
4255    }
4256
4257    pub fn parse_actions_list(&mut self) -> Result<Vec<Action>, ParserError> {
4258        let mut values = vec![];
4259        loop {
4260            values.push(self.parse_grant_permission()?);
4261            if !self.consume_token(&Token::Comma) {
4262                break;
4263            } else if self.options.trailing_commas {
4264                match self.peek_token().token {
4265                    Token::Word(kw) if kw.keyword == Keyword::ON => {
4266                        break;
4267                    }
4268                    Token::RParen
4269                    | Token::SemiColon
4270                    | Token::EOF
4271                    | Token::RBracket
4272                    | Token::RBrace => break,
4273                    _ => continue,
4274                }
4275            }
4276        }
4277        Ok(values)
4278    }
4279
4280    /// Parse a list of [TableWithJoins]
4281    fn parse_table_with_joins(&mut self) -> Result<Vec<TableWithJoins>, ParserError> {
4282        let trailing_commas = self.dialect.supports_from_trailing_commas();
4283
4284        self.parse_comma_separated_with_trailing_commas(
4285            Parser::parse_table_and_joins,
4286            trailing_commas,
4287            |kw, _parser| {
4288                self.dialect
4289                    .get_reserved_keywords_for_table_factor()
4290                    .contains(kw)
4291            },
4292        )
4293    }
4294
4295    /// Parse the comma of a comma-separated syntax element.
4296    /// `R` is a predicate that should return true if the next
4297    /// keyword is a reserved keyword.
4298    /// Allows for control over trailing commas
4299    ///
4300    /// Returns true if there is a next element
4301    fn is_parse_comma_separated_end_with_trailing_commas<R>(
4302        &mut self,
4303        trailing_commas: bool,
4304        is_reserved_keyword: &R,
4305    ) -> bool
4306    where
4307        R: Fn(&Keyword, &mut Parser) -> bool,
4308    {
4309        if !self.consume_token(&Token::Comma) {
4310            true
4311        } else if trailing_commas {
4312            let token = self.next_token().token;
4313            let is_end = match token {
4314                Token::Word(ref kw) if is_reserved_keyword(&kw.keyword, self) => true,
4315                Token::RParen | Token::SemiColon | Token::EOF | Token::RBracket | Token::RBrace => {
4316                    true
4317                }
4318                _ => false,
4319            };
4320            self.prev_token();
4321
4322            is_end
4323        } else {
4324            false
4325        }
4326    }
4327
4328    /// Parse the comma of a comma-separated syntax element.
4329    /// Returns true if there is a next element
4330    fn is_parse_comma_separated_end(&mut self) -> bool {
4331        self.is_parse_comma_separated_end_with_trailing_commas(
4332            self.options.trailing_commas,
4333            &Self::is_reserved_for_column_alias,
4334        )
4335    }
4336
4337    /// Parse a comma-separated list of 1+ items accepted by `F`
4338    pub fn parse_comma_separated<T, F>(&mut self, f: F) -> Result<Vec<T>, ParserError>
4339    where
4340        F: FnMut(&mut Parser<'a>) -> Result<T, ParserError>,
4341    {
4342        self.parse_comma_separated_with_trailing_commas(
4343            f,
4344            self.options.trailing_commas,
4345            Self::is_reserved_for_column_alias,
4346        )
4347    }
4348
4349    /// Parse a comma-separated list of 1+ items accepted by `F`.
4350    /// `R` is a predicate that should return true if the next
4351    /// keyword is a reserved keyword.
4352    /// Allows for control over trailing commas.
4353    fn parse_comma_separated_with_trailing_commas<T, F, R>(
4354        &mut self,
4355        mut f: F,
4356        trailing_commas: bool,
4357        is_reserved_keyword: R,
4358    ) -> Result<Vec<T>, ParserError>
4359    where
4360        F: FnMut(&mut Parser<'a>) -> Result<T, ParserError>,
4361        R: Fn(&Keyword, &mut Parser) -> bool,
4362    {
4363        let mut values = vec![];
4364        loop {
4365            values.push(f(self)?);
4366            if self.is_parse_comma_separated_end_with_trailing_commas(
4367                trailing_commas,
4368                &is_reserved_keyword,
4369            ) {
4370                break;
4371            }
4372        }
4373        Ok(values)
4374    }
4375
4376    /// Parse a period-separated list of 1+ items accepted by `F`
4377    fn parse_period_separated<T, F>(&mut self, mut f: F) -> Result<Vec<T>, ParserError>
4378    where
4379        F: FnMut(&mut Parser<'a>) -> Result<T, ParserError>,
4380    {
4381        let mut values = vec![];
4382        loop {
4383            values.push(f(self)?);
4384            if !self.consume_token(&Token::Period) {
4385                break;
4386            }
4387        }
4388        Ok(values)
4389    }
4390
4391    /// Parse a keyword-separated list of 1+ items accepted by `F`
4392    pub fn parse_keyword_separated<T, F>(
4393        &mut self,
4394        keyword: Keyword,
4395        mut f: F,
4396    ) -> Result<Vec<T>, ParserError>
4397    where
4398        F: FnMut(&mut Parser<'a>) -> Result<T, ParserError>,
4399    {
4400        let mut values = vec![];
4401        loop {
4402            values.push(f(self)?);
4403            if !self.parse_keyword(keyword) {
4404                break;
4405            }
4406        }
4407        Ok(values)
4408    }
4409
4410    pub fn parse_parenthesized<T, F>(&mut self, mut f: F) -> Result<T, ParserError>
4411    where
4412        F: FnMut(&mut Parser<'a>) -> Result<T, ParserError>,
4413    {
4414        self.expect_token(&Token::LParen)?;
4415        let res = f(self)?;
4416        self.expect_token(&Token::RParen)?;
4417        Ok(res)
4418    }
4419
4420    /// Parse a comma-separated list of 0+ items accepted by `F`
4421    /// * `end_token` - expected end token for the closure (e.g. [Token::RParen], [Token::RBrace] ...)
4422    pub fn parse_comma_separated0<T, F>(
4423        &mut self,
4424        f: F,
4425        end_token: Token,
4426    ) -> Result<Vec<T>, ParserError>
4427    where
4428        F: FnMut(&mut Parser<'a>) -> Result<T, ParserError>,
4429    {
4430        if self.peek_token().token == end_token {
4431            return Ok(vec![]);
4432        }
4433
4434        if self.options.trailing_commas && self.peek_tokens() == [Token::Comma, end_token] {
4435            let _ = self.consume_token(&Token::Comma);
4436            return Ok(vec![]);
4437        }
4438
4439        self.parse_comma_separated(f)
4440    }
4441
4442    /// Parses 0 or more statements, each followed by a semicolon.
4443    /// If the next token is any of `terminal_keywords` then no more
4444    /// statements will be parsed.
4445    pub(crate) fn parse_statement_list(
4446        &mut self,
4447        terminal_keywords: &[Keyword],
4448    ) -> Result<Vec<Statement>, ParserError> {
4449        let mut values = vec![];
4450        loop {
4451            if let Token::Word(w) = &self.peek_nth_token_ref(0).token {
4452                if w.quote_style.is_none() && terminal_keywords.contains(&w.keyword) {
4453                    break;
4454                }
4455            }
4456            values.push(self.parse_statement()?);
4457            self.expect_token(&Token::SemiColon)?;
4458        }
4459        Ok(values)
4460    }
4461
4462    /// Default implementation of a predicate that returns true if
4463    /// the specified keyword is reserved for column alias.
4464    /// See [Dialect::is_column_alias]
4465    fn is_reserved_for_column_alias(kw: &Keyword, parser: &mut Parser) -> bool {
4466        !parser.dialect.is_column_alias(kw, parser)
4467    }
4468
4469    /// Run a parser method `f`, reverting back to the current position if unsuccessful.
4470    /// Returns `ParserError::RecursionLimitExceeded` if `f` returns a `RecursionLimitExceeded`.
4471    /// Returns `Ok(None)` if `f` returns any other error.
4472    pub fn maybe_parse<T, F>(&mut self, f: F) -> Result<Option<T>, ParserError>
4473    where
4474        F: FnMut(&mut Parser) -> Result<T, ParserError>,
4475    {
4476        match self.try_parse(f) {
4477            Ok(t) => Ok(Some(t)),
4478            Err(ParserError::RecursionLimitExceeded) => Err(ParserError::RecursionLimitExceeded),
4479            _ => Ok(None),
4480        }
4481    }
4482
4483    /// Run a parser method `f`, reverting back to the current position if unsuccessful.
4484    pub fn try_parse<T, F>(&mut self, mut f: F) -> Result<T, ParserError>
4485    where
4486        F: FnMut(&mut Parser) -> Result<T, ParserError>,
4487    {
4488        let index = self.index;
4489        match f(self) {
4490            Ok(t) => Ok(t),
4491            Err(e) => {
4492                // Unwind stack if limit exceeded
4493                self.index = index;
4494                Err(e)
4495            }
4496        }
4497    }
4498
4499    /// Parse either `ALL`, `DISTINCT` or `DISTINCT ON (...)`. Returns [`None`] if `ALL` is parsed
4500    /// and results in a [`ParserError`] if both `ALL` and `DISTINCT` are found.
4501    pub fn parse_all_or_distinct(&mut self) -> Result<Option<Distinct>, ParserError> {
4502        let loc = self.peek_token().span.start;
4503        let all = self.parse_keyword(Keyword::ALL);
4504        let distinct = self.parse_keyword(Keyword::DISTINCT);
4505        if !distinct {
4506            return Ok(None);
4507        }
4508        if all {
4509            return parser_err!("Cannot specify both ALL and DISTINCT".to_string(), loc);
4510        }
4511        let on = self.parse_keyword(Keyword::ON);
4512        if !on {
4513            return Ok(Some(Distinct::Distinct));
4514        }
4515
4516        self.expect_token(&Token::LParen)?;
4517        let col_names = if self.consume_token(&Token::RParen) {
4518            self.prev_token();
4519            Vec::new()
4520        } else {
4521            self.parse_comma_separated(Parser::parse_expr)?
4522        };
4523        self.expect_token(&Token::RParen)?;
4524        Ok(Some(Distinct::On(col_names)))
4525    }
4526
4527    /// Parse a SQL CREATE statement
4528    pub fn parse_create(&mut self) -> Result<Statement, ParserError> {
4529        let or_replace = self.parse_keywords(&[Keyword::OR, Keyword::REPLACE]);
4530        let or_alter = self.parse_keywords(&[Keyword::OR, Keyword::ALTER]);
4531        let local = self.parse_one_of_keywords(&[Keyword::LOCAL]).is_some();
4532        let global = self.parse_one_of_keywords(&[Keyword::GLOBAL]).is_some();
4533        let transient = self.parse_one_of_keywords(&[Keyword::TRANSIENT]).is_some();
4534        let global: Option<bool> = if global {
4535            Some(true)
4536        } else if local {
4537            Some(false)
4538        } else {
4539            None
4540        };
4541        let temporary = self
4542            .parse_one_of_keywords(&[Keyword::TEMP, Keyword::TEMPORARY])
4543            .is_some();
4544        let persistent = dialect_of!(self is DuckDbDialect)
4545            && self.parse_one_of_keywords(&[Keyword::PERSISTENT]).is_some();
4546        let create_view_params = self.parse_create_view_params()?;
4547        if self.parse_keyword(Keyword::TABLE) {
4548            self.parse_create_table(or_replace, temporary, global, transient)
4549        } else if self.parse_keyword(Keyword::MATERIALIZED) || self.parse_keyword(Keyword::VIEW) {
4550            self.prev_token();
4551            self.parse_create_view(or_alter, or_replace, temporary, create_view_params)
4552        } else if self.parse_keyword(Keyword::POLICY) {
4553            self.parse_create_policy()
4554        } else if self.parse_keyword(Keyword::EXTERNAL) {
4555            self.parse_create_external_table(or_replace)
4556        } else if self.parse_keyword(Keyword::FUNCTION) {
4557            self.parse_create_function(or_alter, or_replace, temporary)
4558        } else if self.parse_keyword(Keyword::TRIGGER) {
4559            self.parse_create_trigger(or_replace, false)
4560        } else if self.parse_keywords(&[Keyword::CONSTRAINT, Keyword::TRIGGER]) {
4561            self.parse_create_trigger(or_replace, true)
4562        } else if self.parse_keyword(Keyword::MACRO) {
4563            self.parse_create_macro(or_replace, temporary)
4564        } else if self.parse_keyword(Keyword::SECRET) {
4565            self.parse_create_secret(or_replace, temporary, persistent)
4566        } else if or_replace {
4567            self.expected(
4568                "[EXTERNAL] TABLE or [MATERIALIZED] VIEW or FUNCTION after CREATE OR REPLACE",
4569                self.peek_token(),
4570            )
4571        } else if self.parse_keyword(Keyword::EXTENSION) {
4572            self.parse_create_extension()
4573        } else if self.parse_keyword(Keyword::INDEX) {
4574            self.parse_create_index(false)
4575        } else if self.parse_keywords(&[Keyword::UNIQUE, Keyword::INDEX]) {
4576            self.parse_create_index(true)
4577        } else if self.parse_keyword(Keyword::VIRTUAL) {
4578            self.parse_create_virtual_table()
4579        } else if self.parse_keyword(Keyword::SCHEMA) {
4580            self.parse_create_schema()
4581        } else if self.parse_keyword(Keyword::DATABASE) {
4582            self.parse_create_database()
4583        } else if self.parse_keyword(Keyword::ROLE) {
4584            self.parse_create_role()
4585        } else if self.parse_keyword(Keyword::SEQUENCE) {
4586            self.parse_create_sequence(temporary)
4587        } else if self.parse_keyword(Keyword::TYPE) {
4588            self.parse_create_type()
4589        } else if self.parse_keyword(Keyword::PROCEDURE) {
4590            self.parse_create_procedure(or_alter)
4591        } else if self.parse_keyword(Keyword::CONNECTOR) {
4592            self.parse_create_connector()
4593        } else {
4594            self.expected("an object type after CREATE", self.peek_token())
4595        }
4596    }
4597
4598    /// See [DuckDB Docs](https://duckdb.org/docs/sql/statements/create_secret.html) for more details.
4599    pub fn parse_create_secret(
4600        &mut self,
4601        or_replace: bool,
4602        temporary: bool,
4603        persistent: bool,
4604    ) -> Result<Statement, ParserError> {
4605        let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
4606
4607        let mut storage_specifier = None;
4608        let mut name = None;
4609        if self.peek_token() != Token::LParen {
4610            if self.parse_keyword(Keyword::IN) {
4611                storage_specifier = self.parse_identifier().ok()
4612            } else {
4613                name = self.parse_identifier().ok();
4614            }
4615
4616            // Storage specifier may follow the name
4617            if storage_specifier.is_none()
4618                && self.peek_token() != Token::LParen
4619                && self.parse_keyword(Keyword::IN)
4620            {
4621                storage_specifier = self.parse_identifier().ok();
4622            }
4623        }
4624
4625        self.expect_token(&Token::LParen)?;
4626        self.expect_keyword_is(Keyword::TYPE)?;
4627        let secret_type = self.parse_identifier()?;
4628
4629        let mut options = Vec::new();
4630        if self.consume_token(&Token::Comma) {
4631            options.append(&mut self.parse_comma_separated(|p| {
4632                let key = p.parse_identifier()?;
4633                let value = p.parse_identifier()?;
4634                Ok(SecretOption { key, value })
4635            })?);
4636        }
4637        self.expect_token(&Token::RParen)?;
4638
4639        let temp = match (temporary, persistent) {
4640            (true, false) => Some(true),
4641            (false, true) => Some(false),
4642            (false, false) => None,
4643            _ => self.expected("TEMPORARY or PERSISTENT", self.peek_token())?,
4644        };
4645
4646        Ok(Statement::CreateSecret {
4647            or_replace,
4648            temporary: temp,
4649            if_not_exists,
4650            name,
4651            storage_specifier,
4652            secret_type,
4653            options,
4654        })
4655    }
4656
4657    /// Parse a CACHE TABLE statement
4658    pub fn parse_cache_table(&mut self) -> Result<Statement, ParserError> {
4659        let (mut table_flag, mut options, mut has_as, mut query) = (None, vec![], false, None);
4660        if self.parse_keyword(Keyword::TABLE) {
4661            let table_name = self.parse_object_name(false)?;
4662            if self.peek_token().token != Token::EOF {
4663                if let Token::Word(word) = self.peek_token().token {
4664                    if word.keyword == Keyword::OPTIONS {
4665                        options = self.parse_options(Keyword::OPTIONS)?
4666                    }
4667                };
4668
4669                if self.peek_token().token != Token::EOF {
4670                    let (a, q) = self.parse_as_query()?;
4671                    has_as = a;
4672                    query = Some(q);
4673                }
4674
4675                Ok(Statement::Cache {
4676                    table_flag,
4677                    table_name,
4678                    has_as,
4679                    options,
4680                    query,
4681                })
4682            } else {
4683                Ok(Statement::Cache {
4684                    table_flag,
4685                    table_name,
4686                    has_as,
4687                    options,
4688                    query,
4689                })
4690            }
4691        } else {
4692            table_flag = Some(self.parse_object_name(false)?);
4693            if self.parse_keyword(Keyword::TABLE) {
4694                let table_name = self.parse_object_name(false)?;
4695                if self.peek_token() != Token::EOF {
4696                    if let Token::Word(word) = self.peek_token().token {
4697                        if word.keyword == Keyword::OPTIONS {
4698                            options = self.parse_options(Keyword::OPTIONS)?
4699                        }
4700                    };
4701
4702                    if self.peek_token() != Token::EOF {
4703                        let (a, q) = self.parse_as_query()?;
4704                        has_as = a;
4705                        query = Some(q);
4706                    }
4707
4708                    Ok(Statement::Cache {
4709                        table_flag,
4710                        table_name,
4711                        has_as,
4712                        options,
4713                        query,
4714                    })
4715                } else {
4716                    Ok(Statement::Cache {
4717                        table_flag,
4718                        table_name,
4719                        has_as,
4720                        options,
4721                        query,
4722                    })
4723                }
4724            } else {
4725                if self.peek_token() == Token::EOF {
4726                    self.prev_token();
4727                }
4728                self.expected("a `TABLE` keyword", self.peek_token())
4729            }
4730        }
4731    }
4732
4733    /// Parse 'AS' before as query,such as `WITH XXX AS SELECT XXX` oer `CACHE TABLE AS SELECT XXX`
4734    pub fn parse_as_query(&mut self) -> Result<(bool, Box<Query>), ParserError> {
4735        match self.peek_token().token {
4736            Token::Word(word) => match word.keyword {
4737                Keyword::AS => {
4738                    self.next_token();
4739                    Ok((true, self.parse_query()?))
4740                }
4741                _ => Ok((false, self.parse_query()?)),
4742            },
4743            _ => self.expected("a QUERY statement", self.peek_token()),
4744        }
4745    }
4746
4747    /// Parse a UNCACHE TABLE statement
4748    pub fn parse_uncache_table(&mut self) -> Result<Statement, ParserError> {
4749        self.expect_keyword_is(Keyword::TABLE)?;
4750        let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
4751        let table_name = self.parse_object_name(false)?;
4752        Ok(Statement::UNCache {
4753            table_name,
4754            if_exists,
4755        })
4756    }
4757
4758    /// SQLite-specific `CREATE VIRTUAL TABLE`
4759    pub fn parse_create_virtual_table(&mut self) -> Result<Statement, ParserError> {
4760        self.expect_keyword_is(Keyword::TABLE)?;
4761        let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
4762        let table_name = self.parse_object_name(false)?;
4763        self.expect_keyword_is(Keyword::USING)?;
4764        let module_name = self.parse_identifier()?;
4765        // SQLite docs note that module "arguments syntax is sufficiently
4766        // general that the arguments can be made to appear as column
4767        // definitions in a traditional CREATE TABLE statement", but
4768        // we don't implement that.
4769        let module_args = self.parse_parenthesized_column_list(Optional, false)?;
4770        Ok(Statement::CreateVirtualTable {
4771            name: table_name,
4772            if_not_exists,
4773            module_name,
4774            module_args,
4775        })
4776    }
4777
4778    pub fn parse_create_schema(&mut self) -> Result<Statement, ParserError> {
4779        let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
4780
4781        let schema_name = self.parse_schema_name()?;
4782
4783        let default_collate_spec = if self.parse_keywords(&[Keyword::DEFAULT, Keyword::COLLATE]) {
4784            Some(self.parse_expr()?)
4785        } else {
4786            None
4787        };
4788
4789        let options = if self.peek_keyword(Keyword::OPTIONS) {
4790            Some(self.parse_options(Keyword::OPTIONS)?)
4791        } else {
4792            None
4793        };
4794
4795        Ok(Statement::CreateSchema {
4796            schema_name,
4797            if_not_exists,
4798            options,
4799            default_collate_spec,
4800        })
4801    }
4802
4803    fn parse_schema_name(&mut self) -> Result<SchemaName, ParserError> {
4804        if self.parse_keyword(Keyword::AUTHORIZATION) {
4805            Ok(SchemaName::UnnamedAuthorization(self.parse_identifier()?))
4806        } else {
4807            let name = self.parse_object_name(false)?;
4808
4809            if self.parse_keyword(Keyword::AUTHORIZATION) {
4810                Ok(SchemaName::NamedAuthorization(
4811                    name,
4812                    self.parse_identifier()?,
4813                ))
4814            } else {
4815                Ok(SchemaName::Simple(name))
4816            }
4817        }
4818    }
4819
4820    pub fn parse_create_database(&mut self) -> Result<Statement, ParserError> {
4821        let ine = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
4822        let db_name = self.parse_object_name(false)?;
4823        let mut location = None;
4824        let mut managed_location = None;
4825        loop {
4826            match self.parse_one_of_keywords(&[Keyword::LOCATION, Keyword::MANAGEDLOCATION]) {
4827                Some(Keyword::LOCATION) => location = Some(self.parse_literal_string()?),
4828                Some(Keyword::MANAGEDLOCATION) => {
4829                    managed_location = Some(self.parse_literal_string()?)
4830                }
4831                _ => break,
4832            }
4833        }
4834        Ok(Statement::CreateDatabase {
4835            db_name,
4836            if_not_exists: ine,
4837            location,
4838            managed_location,
4839        })
4840    }
4841
4842    pub fn parse_optional_create_function_using(
4843        &mut self,
4844    ) -> Result<Option<CreateFunctionUsing>, ParserError> {
4845        if !self.parse_keyword(Keyword::USING) {
4846            return Ok(None);
4847        };
4848        let keyword =
4849            self.expect_one_of_keywords(&[Keyword::JAR, Keyword::FILE, Keyword::ARCHIVE])?;
4850
4851        let uri = self.parse_literal_string()?;
4852
4853        match keyword {
4854            Keyword::JAR => Ok(Some(CreateFunctionUsing::Jar(uri))),
4855            Keyword::FILE => Ok(Some(CreateFunctionUsing::File(uri))),
4856            Keyword::ARCHIVE => Ok(Some(CreateFunctionUsing::Archive(uri))),
4857            _ => self.expected(
4858                "JAR, FILE or ARCHIVE, got {:?}",
4859                TokenWithSpan::wrap(Token::make_keyword(format!("{keyword:?}").as_str())),
4860            ),
4861        }
4862    }
4863
4864    pub fn parse_create_function(
4865        &mut self,
4866        or_alter: bool,
4867        or_replace: bool,
4868        temporary: bool,
4869    ) -> Result<Statement, ParserError> {
4870        if dialect_of!(self is HiveDialect) {
4871            self.parse_hive_create_function(or_replace, temporary)
4872        } else if dialect_of!(self is PostgreSqlDialect | GenericDialect) {
4873            self.parse_postgres_create_function(or_replace, temporary)
4874        } else if dialect_of!(self is DuckDbDialect) {
4875            self.parse_create_macro(or_replace, temporary)
4876        } else if dialect_of!(self is BigQueryDialect) {
4877            self.parse_bigquery_create_function(or_replace, temporary)
4878        } else if dialect_of!(self is MsSqlDialect) {
4879            self.parse_mssql_create_function(or_alter, or_replace, temporary)
4880        } else {
4881            self.prev_token();
4882            self.expected("an object type after CREATE", self.peek_token())
4883        }
4884    }
4885
4886    /// Parse `CREATE FUNCTION` for [PostgreSQL]
4887    ///
4888    /// [PostgreSQL]: https://www.postgresql.org/docs/15/sql-createfunction.html
4889    fn parse_postgres_create_function(
4890        &mut self,
4891        or_replace: bool,
4892        temporary: bool,
4893    ) -> Result<Statement, ParserError> {
4894        let name = self.parse_object_name(false)?;
4895
4896        self.expect_token(&Token::LParen)?;
4897        let args = if Token::RParen != self.peek_token_ref().token {
4898            self.parse_comma_separated(Parser::parse_function_arg)?
4899        } else {
4900            vec![]
4901        };
4902        self.expect_token(&Token::RParen)?;
4903
4904        let return_type = if self.parse_keyword(Keyword::RETURNS) {
4905            Some(self.parse_data_type()?)
4906        } else {
4907            None
4908        };
4909
4910        #[derive(Default)]
4911        struct Body {
4912            language: Option<Ident>,
4913            behavior: Option<FunctionBehavior>,
4914            function_body: Option<CreateFunctionBody>,
4915            called_on_null: Option<FunctionCalledOnNull>,
4916            parallel: Option<FunctionParallel>,
4917        }
4918        let mut body = Body::default();
4919        loop {
4920            fn ensure_not_set<T>(field: &Option<T>, name: &str) -> Result<(), ParserError> {
4921                if field.is_some() {
4922                    return Err(ParserError::ParserError(format!(
4923                        "{name} specified more than once",
4924                    )));
4925                }
4926                Ok(())
4927            }
4928            if self.parse_keyword(Keyword::AS) {
4929                ensure_not_set(&body.function_body, "AS")?;
4930                body.function_body = Some(CreateFunctionBody::AsBeforeOptions(
4931                    self.parse_create_function_body_string()?,
4932                ));
4933            } else if self.parse_keyword(Keyword::LANGUAGE) {
4934                ensure_not_set(&body.language, "LANGUAGE")?;
4935                body.language = Some(self.parse_identifier()?);
4936            } else if self.parse_keyword(Keyword::IMMUTABLE) {
4937                ensure_not_set(&body.behavior, "IMMUTABLE | STABLE | VOLATILE")?;
4938                body.behavior = Some(FunctionBehavior::Immutable);
4939            } else if self.parse_keyword(Keyword::STABLE) {
4940                ensure_not_set(&body.behavior, "IMMUTABLE | STABLE | VOLATILE")?;
4941                body.behavior = Some(FunctionBehavior::Stable);
4942            } else if self.parse_keyword(Keyword::VOLATILE) {
4943                ensure_not_set(&body.behavior, "IMMUTABLE | STABLE | VOLATILE")?;
4944                body.behavior = Some(FunctionBehavior::Volatile);
4945            } else if self.parse_keywords(&[
4946                Keyword::CALLED,
4947                Keyword::ON,
4948                Keyword::NULL,
4949                Keyword::INPUT,
4950            ]) {
4951                ensure_not_set(
4952                    &body.called_on_null,
4953                    "CALLED ON NULL INPUT | RETURNS NULL ON NULL INPUT | STRICT",
4954                )?;
4955                body.called_on_null = Some(FunctionCalledOnNull::CalledOnNullInput);
4956            } else if self.parse_keywords(&[
4957                Keyword::RETURNS,
4958                Keyword::NULL,
4959                Keyword::ON,
4960                Keyword::NULL,
4961                Keyword::INPUT,
4962            ]) {
4963                ensure_not_set(
4964                    &body.called_on_null,
4965                    "CALLED ON NULL INPUT | RETURNS NULL ON NULL INPUT | STRICT",
4966                )?;
4967                body.called_on_null = Some(FunctionCalledOnNull::ReturnsNullOnNullInput);
4968            } else if self.parse_keyword(Keyword::STRICT) {
4969                ensure_not_set(
4970                    &body.called_on_null,
4971                    "CALLED ON NULL INPUT | RETURNS NULL ON NULL INPUT | STRICT",
4972                )?;
4973                body.called_on_null = Some(FunctionCalledOnNull::Strict);
4974            } else if self.parse_keyword(Keyword::PARALLEL) {
4975                ensure_not_set(&body.parallel, "PARALLEL { UNSAFE | RESTRICTED | SAFE }")?;
4976                if self.parse_keyword(Keyword::UNSAFE) {
4977                    body.parallel = Some(FunctionParallel::Unsafe);
4978                } else if self.parse_keyword(Keyword::RESTRICTED) {
4979                    body.parallel = Some(FunctionParallel::Restricted);
4980                } else if self.parse_keyword(Keyword::SAFE) {
4981                    body.parallel = Some(FunctionParallel::Safe);
4982                } else {
4983                    return self.expected("one of UNSAFE | RESTRICTED | SAFE", self.peek_token());
4984                }
4985            } else if self.parse_keyword(Keyword::RETURN) {
4986                ensure_not_set(&body.function_body, "RETURN")?;
4987                body.function_body = Some(CreateFunctionBody::Return(self.parse_expr()?));
4988            } else {
4989                break;
4990            }
4991        }
4992
4993        Ok(Statement::CreateFunction(CreateFunction {
4994            or_alter: false,
4995            or_replace,
4996            temporary,
4997            name,
4998            args: Some(args),
4999            return_type,
5000            behavior: body.behavior,
5001            called_on_null: body.called_on_null,
5002            parallel: body.parallel,
5003            language: body.language,
5004            function_body: body.function_body,
5005            if_not_exists: false,
5006            using: None,
5007            determinism_specifier: None,
5008            options: None,
5009            remote_connection: None,
5010        }))
5011    }
5012
5013    /// Parse `CREATE FUNCTION` for [Hive]
5014    ///
5015    /// [Hive]: https://cwiki.apache.org/confluence/display/hive/languagemanual+ddl#LanguageManualDDL-Create/Drop/ReloadFunction
5016    fn parse_hive_create_function(
5017        &mut self,
5018        or_replace: bool,
5019        temporary: bool,
5020    ) -> Result<Statement, ParserError> {
5021        let name = self.parse_object_name(false)?;
5022        self.expect_keyword_is(Keyword::AS)?;
5023
5024        let as_ = self.parse_create_function_body_string()?;
5025        let using = self.parse_optional_create_function_using()?;
5026
5027        Ok(Statement::CreateFunction(CreateFunction {
5028            or_alter: false,
5029            or_replace,
5030            temporary,
5031            name,
5032            function_body: Some(CreateFunctionBody::AsBeforeOptions(as_)),
5033            using,
5034            if_not_exists: false,
5035            args: None,
5036            return_type: None,
5037            behavior: None,
5038            called_on_null: None,
5039            parallel: None,
5040            language: None,
5041            determinism_specifier: None,
5042            options: None,
5043            remote_connection: None,
5044        }))
5045    }
5046
5047    /// Parse `CREATE FUNCTION` for [BigQuery]
5048    ///
5049    /// [BigQuery]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_function_statement
5050    fn parse_bigquery_create_function(
5051        &mut self,
5052        or_replace: bool,
5053        temporary: bool,
5054    ) -> Result<Statement, ParserError> {
5055        let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
5056        let (name, args) = self.parse_create_function_name_and_params()?;
5057
5058        let return_type = if self.parse_keyword(Keyword::RETURNS) {
5059            Some(self.parse_data_type()?)
5060        } else {
5061            None
5062        };
5063
5064        let determinism_specifier = if self.parse_keyword(Keyword::DETERMINISTIC) {
5065            Some(FunctionDeterminismSpecifier::Deterministic)
5066        } else if self.parse_keywords(&[Keyword::NOT, Keyword::DETERMINISTIC]) {
5067            Some(FunctionDeterminismSpecifier::NotDeterministic)
5068        } else {
5069            None
5070        };
5071
5072        let language = if self.parse_keyword(Keyword::LANGUAGE) {
5073            Some(self.parse_identifier()?)
5074        } else {
5075            None
5076        };
5077
5078        let remote_connection =
5079            if self.parse_keywords(&[Keyword::REMOTE, Keyword::WITH, Keyword::CONNECTION]) {
5080                Some(self.parse_object_name(false)?)
5081            } else {
5082                None
5083            };
5084
5085        // `OPTIONS` may come before of after the function body but
5086        // may be specified at most once.
5087        let mut options = self.maybe_parse_options(Keyword::OPTIONS)?;
5088
5089        let function_body = if remote_connection.is_none() {
5090            self.expect_keyword_is(Keyword::AS)?;
5091            let expr = self.parse_expr()?;
5092            if options.is_none() {
5093                options = self.maybe_parse_options(Keyword::OPTIONS)?;
5094                Some(CreateFunctionBody::AsBeforeOptions(expr))
5095            } else {
5096                Some(CreateFunctionBody::AsAfterOptions(expr))
5097            }
5098        } else {
5099            None
5100        };
5101
5102        Ok(Statement::CreateFunction(CreateFunction {
5103            or_alter: false,
5104            or_replace,
5105            temporary,
5106            if_not_exists,
5107            name,
5108            args: Some(args),
5109            return_type,
5110            function_body,
5111            language,
5112            determinism_specifier,
5113            options,
5114            remote_connection,
5115            using: None,
5116            behavior: None,
5117            called_on_null: None,
5118            parallel: None,
5119        }))
5120    }
5121
5122    /// Parse `CREATE FUNCTION` for [MsSql]
5123    ///
5124    /// [MsSql]: https://learn.microsoft.com/en-us/sql/t-sql/statements/create-function-transact-sql
5125    fn parse_mssql_create_function(
5126        &mut self,
5127        or_alter: bool,
5128        or_replace: bool,
5129        temporary: bool,
5130    ) -> Result<Statement, ParserError> {
5131        let (name, args) = self.parse_create_function_name_and_params()?;
5132
5133        self.expect_keyword(Keyword::RETURNS)?;
5134        let return_type = Some(self.parse_data_type()?);
5135
5136        self.expect_keyword_is(Keyword::AS)?;
5137
5138        let begin_token = self.expect_keyword(Keyword::BEGIN)?;
5139        let statements = self.parse_statement_list(&[Keyword::END])?;
5140        let end_token = self.expect_keyword(Keyword::END)?;
5141
5142        let function_body = Some(CreateFunctionBody::AsBeginEnd(BeginEndStatements {
5143            begin_token: AttachedToken(begin_token),
5144            statements,
5145            end_token: AttachedToken(end_token),
5146        }));
5147
5148        Ok(Statement::CreateFunction(CreateFunction {
5149            or_alter,
5150            or_replace,
5151            temporary,
5152            if_not_exists: false,
5153            name,
5154            args: Some(args),
5155            return_type,
5156            function_body,
5157            language: None,
5158            determinism_specifier: None,
5159            options: None,
5160            remote_connection: None,
5161            using: None,
5162            behavior: None,
5163            called_on_null: None,
5164            parallel: None,
5165        }))
5166    }
5167
5168    fn parse_create_function_name_and_params(
5169        &mut self,
5170    ) -> Result<(ObjectName, Vec<OperateFunctionArg>), ParserError> {
5171        let name = self.parse_object_name(false)?;
5172        let parse_function_param =
5173            |parser: &mut Parser| -> Result<OperateFunctionArg, ParserError> {
5174                let name = parser.parse_identifier()?;
5175                let data_type = parser.parse_data_type()?;
5176                Ok(OperateFunctionArg {
5177                    mode: None,
5178                    name: Some(name),
5179                    data_type,
5180                    default_expr: None,
5181                })
5182            };
5183        self.expect_token(&Token::LParen)?;
5184        let args = self.parse_comma_separated0(parse_function_param, Token::RParen)?;
5185        self.expect_token(&Token::RParen)?;
5186        Ok((name, args))
5187    }
5188
5189    fn parse_function_arg(&mut self) -> Result<OperateFunctionArg, ParserError> {
5190        let mode = if self.parse_keyword(Keyword::IN) {
5191            Some(ArgMode::In)
5192        } else if self.parse_keyword(Keyword::OUT) {
5193            Some(ArgMode::Out)
5194        } else if self.parse_keyword(Keyword::INOUT) {
5195            Some(ArgMode::InOut)
5196        } else {
5197            None
5198        };
5199
5200        // parse: [ argname ] argtype
5201        let mut name = None;
5202        let mut data_type = self.parse_data_type()?;
5203        if let DataType::Custom(n, _) = &data_type {
5204            // the first token is actually a name
5205            match n.0[0].clone() {
5206                ObjectNamePart::Identifier(ident) => name = Some(ident),
5207            }
5208            data_type = self.parse_data_type()?;
5209        }
5210
5211        let default_expr = if self.parse_keyword(Keyword::DEFAULT) || self.consume_token(&Token::Eq)
5212        {
5213            Some(self.parse_expr()?)
5214        } else {
5215            None
5216        };
5217        Ok(OperateFunctionArg {
5218            mode,
5219            name,
5220            data_type,
5221            default_expr,
5222        })
5223    }
5224
5225    /// Parse statements of the DropTrigger type such as:
5226    ///
5227    /// ```sql
5228    /// DROP TRIGGER [ IF EXISTS ] name ON table_name [ CASCADE | RESTRICT ]
5229    /// ```
5230    pub fn parse_drop_trigger(&mut self) -> Result<Statement, ParserError> {
5231        if !dialect_of!(self is PostgreSqlDialect | GenericDialect | MySqlDialect | MsSqlDialect) {
5232            self.prev_token();
5233            return self.expected("an object type after DROP", self.peek_token());
5234        }
5235        let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
5236        let trigger_name = self.parse_object_name(false)?;
5237        let table_name = if self.parse_keyword(Keyword::ON) {
5238            Some(self.parse_object_name(false)?)
5239        } else {
5240            None
5241        };
5242        let option = self
5243            .parse_one_of_keywords(&[Keyword::CASCADE, Keyword::RESTRICT])
5244            .map(|keyword| match keyword {
5245                Keyword::CASCADE => ReferentialAction::Cascade,
5246                Keyword::RESTRICT => ReferentialAction::Restrict,
5247                _ => unreachable!(),
5248            });
5249        Ok(Statement::DropTrigger {
5250            if_exists,
5251            trigger_name,
5252            table_name,
5253            option,
5254        })
5255    }
5256
5257    pub fn parse_create_trigger(
5258        &mut self,
5259        or_replace: bool,
5260        is_constraint: bool,
5261    ) -> Result<Statement, ParserError> {
5262        if !dialect_of!(self is PostgreSqlDialect | GenericDialect | MySqlDialect) {
5263            self.prev_token();
5264            return self.expected("an object type after CREATE", self.peek_token());
5265        }
5266
5267        let name = self.parse_object_name(false)?;
5268        let period = self.parse_trigger_period()?;
5269
5270        let events = self.parse_keyword_separated(Keyword::OR, Parser::parse_trigger_event)?;
5271        self.expect_keyword_is(Keyword::ON)?;
5272        let table_name = self.parse_object_name(false)?;
5273
5274        let referenced_table_name = if self.parse_keyword(Keyword::FROM) {
5275            self.parse_object_name(true).ok()
5276        } else {
5277            None
5278        };
5279
5280        let characteristics = self.parse_constraint_characteristics()?;
5281
5282        let mut referencing = vec![];
5283        if self.parse_keyword(Keyword::REFERENCING) {
5284            while let Some(refer) = self.parse_trigger_referencing()? {
5285                referencing.push(refer);
5286            }
5287        }
5288
5289        self.expect_keyword_is(Keyword::FOR)?;
5290        let include_each = self.parse_keyword(Keyword::EACH);
5291        let trigger_object =
5292            match self.expect_one_of_keywords(&[Keyword::ROW, Keyword::STATEMENT])? {
5293                Keyword::ROW => TriggerObject::Row,
5294                Keyword::STATEMENT => TriggerObject::Statement,
5295                _ => unreachable!(),
5296            };
5297
5298        let condition = self
5299            .parse_keyword(Keyword::WHEN)
5300            .then(|| self.parse_expr())
5301            .transpose()?;
5302
5303        self.expect_keyword_is(Keyword::EXECUTE)?;
5304
5305        let exec_body = self.parse_trigger_exec_body()?;
5306
5307        Ok(Statement::CreateTrigger {
5308            or_replace,
5309            is_constraint,
5310            name,
5311            period,
5312            events,
5313            table_name,
5314            referenced_table_name,
5315            referencing,
5316            trigger_object,
5317            include_each,
5318            condition,
5319            exec_body,
5320            characteristics,
5321        })
5322    }
5323
5324    pub fn parse_trigger_period(&mut self) -> Result<TriggerPeriod, ParserError> {
5325        Ok(
5326            match self.expect_one_of_keywords(&[
5327                Keyword::BEFORE,
5328                Keyword::AFTER,
5329                Keyword::INSTEAD,
5330            ])? {
5331                Keyword::BEFORE => TriggerPeriod::Before,
5332                Keyword::AFTER => TriggerPeriod::After,
5333                Keyword::INSTEAD => self
5334                    .expect_keyword_is(Keyword::OF)
5335                    .map(|_| TriggerPeriod::InsteadOf)?,
5336                _ => unreachable!(),
5337            },
5338        )
5339    }
5340
5341    pub fn parse_trigger_event(&mut self) -> Result<TriggerEvent, ParserError> {
5342        Ok(
5343            match self.expect_one_of_keywords(&[
5344                Keyword::INSERT,
5345                Keyword::UPDATE,
5346                Keyword::DELETE,
5347                Keyword::TRUNCATE,
5348            ])? {
5349                Keyword::INSERT => TriggerEvent::Insert,
5350                Keyword::UPDATE => {
5351                    if self.parse_keyword(Keyword::OF) {
5352                        let cols = self.parse_comma_separated(Parser::parse_identifier)?;
5353                        TriggerEvent::Update(cols)
5354                    } else {
5355                        TriggerEvent::Update(vec![])
5356                    }
5357                }
5358                Keyword::DELETE => TriggerEvent::Delete,
5359                Keyword::TRUNCATE => TriggerEvent::Truncate,
5360                _ => unreachable!(),
5361            },
5362        )
5363    }
5364
5365    pub fn parse_trigger_referencing(&mut self) -> Result<Option<TriggerReferencing>, ParserError> {
5366        let refer_type = match self.parse_one_of_keywords(&[Keyword::OLD, Keyword::NEW]) {
5367            Some(Keyword::OLD) if self.parse_keyword(Keyword::TABLE) => {
5368                TriggerReferencingType::OldTable
5369            }
5370            Some(Keyword::NEW) if self.parse_keyword(Keyword::TABLE) => {
5371                TriggerReferencingType::NewTable
5372            }
5373            _ => {
5374                return Ok(None);
5375            }
5376        };
5377
5378        let is_as = self.parse_keyword(Keyword::AS);
5379        let transition_relation_name = self.parse_object_name(false)?;
5380        Ok(Some(TriggerReferencing {
5381            refer_type,
5382            is_as,
5383            transition_relation_name,
5384        }))
5385    }
5386
5387    pub fn parse_trigger_exec_body(&mut self) -> Result<TriggerExecBody, ParserError> {
5388        Ok(TriggerExecBody {
5389            exec_type: match self
5390                .expect_one_of_keywords(&[Keyword::FUNCTION, Keyword::PROCEDURE])?
5391            {
5392                Keyword::FUNCTION => TriggerExecBodyType::Function,
5393                Keyword::PROCEDURE => TriggerExecBodyType::Procedure,
5394                _ => unreachable!(),
5395            },
5396            func_desc: self.parse_function_desc()?,
5397        })
5398    }
5399
5400    pub fn parse_create_macro(
5401        &mut self,
5402        or_replace: bool,
5403        temporary: bool,
5404    ) -> Result<Statement, ParserError> {
5405        if dialect_of!(self is DuckDbDialect |  GenericDialect) {
5406            let name = self.parse_object_name(false)?;
5407            self.expect_token(&Token::LParen)?;
5408            let args = if self.consume_token(&Token::RParen) {
5409                self.prev_token();
5410                None
5411            } else {
5412                Some(self.parse_comma_separated(Parser::parse_macro_arg)?)
5413            };
5414
5415            self.expect_token(&Token::RParen)?;
5416            self.expect_keyword_is(Keyword::AS)?;
5417
5418            Ok(Statement::CreateMacro {
5419                or_replace,
5420                temporary,
5421                name,
5422                args,
5423                definition: if self.parse_keyword(Keyword::TABLE) {
5424                    MacroDefinition::Table(self.parse_query()?)
5425                } else {
5426                    MacroDefinition::Expr(self.parse_expr()?)
5427                },
5428            })
5429        } else {
5430            self.prev_token();
5431            self.expected("an object type after CREATE", self.peek_token())
5432        }
5433    }
5434
5435    fn parse_macro_arg(&mut self) -> Result<MacroArg, ParserError> {
5436        let name = self.parse_identifier()?;
5437
5438        let default_expr =
5439            if self.consume_token(&Token::Assignment) || self.consume_token(&Token::RArrow) {
5440                Some(self.parse_expr()?)
5441            } else {
5442                None
5443            };
5444        Ok(MacroArg { name, default_expr })
5445    }
5446
5447    pub fn parse_create_external_table(
5448        &mut self,
5449        or_replace: bool,
5450    ) -> Result<Statement, ParserError> {
5451        self.expect_keyword_is(Keyword::TABLE)?;
5452        let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
5453        let table_name = self.parse_object_name(false)?;
5454        let (columns, constraints) = self.parse_columns()?;
5455
5456        let hive_distribution = self.parse_hive_distribution()?;
5457        let hive_formats = self.parse_hive_formats()?;
5458
5459        let file_format = if let Some(ff) = &hive_formats.storage {
5460            match ff {
5461                HiveIOFormat::FileFormat { format } => Some(*format),
5462                _ => None,
5463            }
5464        } else {
5465            None
5466        };
5467        let location = hive_formats.location.clone();
5468        let table_properties = self.parse_options(Keyword::TBLPROPERTIES)?;
5469        Ok(CreateTableBuilder::new(table_name)
5470            .columns(columns)
5471            .constraints(constraints)
5472            .hive_distribution(hive_distribution)
5473            .hive_formats(Some(hive_formats))
5474            .table_properties(table_properties)
5475            .or_replace(or_replace)
5476            .if_not_exists(if_not_exists)
5477            .external(true)
5478            .file_format(file_format)
5479            .location(location)
5480            .build())
5481    }
5482
5483    pub fn parse_file_format(&mut self) -> Result<FileFormat, ParserError> {
5484        let next_token = self.next_token();
5485        match &next_token.token {
5486            Token::Word(w) => match w.keyword {
5487                Keyword::AVRO => Ok(FileFormat::AVRO),
5488                Keyword::JSONFILE => Ok(FileFormat::JSONFILE),
5489                Keyword::ORC => Ok(FileFormat::ORC),
5490                Keyword::PARQUET => Ok(FileFormat::PARQUET),
5491                Keyword::RCFILE => Ok(FileFormat::RCFILE),
5492                Keyword::SEQUENCEFILE => Ok(FileFormat::SEQUENCEFILE),
5493                Keyword::TEXTFILE => Ok(FileFormat::TEXTFILE),
5494                _ => self.expected("fileformat", next_token),
5495            },
5496            _ => self.expected("fileformat", next_token),
5497        }
5498    }
5499
5500    pub fn parse_analyze_format(&mut self) -> Result<AnalyzeFormat, ParserError> {
5501        let next_token = self.next_token();
5502        match &next_token.token {
5503            Token::Word(w) => match w.keyword {
5504                Keyword::TEXT => Ok(AnalyzeFormat::TEXT),
5505                Keyword::GRAPHVIZ => Ok(AnalyzeFormat::GRAPHVIZ),
5506                Keyword::JSON => Ok(AnalyzeFormat::JSON),
5507                _ => self.expected("fileformat", next_token),
5508            },
5509            _ => self.expected("fileformat", next_token),
5510        }
5511    }
5512
5513    pub fn parse_create_view(
5514        &mut self,
5515        or_alter: bool,
5516        or_replace: bool,
5517        temporary: bool,
5518        create_view_params: Option<CreateViewParams>,
5519    ) -> Result<Statement, ParserError> {
5520        let materialized = self.parse_keyword(Keyword::MATERIALIZED);
5521        self.expect_keyword_is(Keyword::VIEW)?;
5522        let if_not_exists = dialect_of!(self is BigQueryDialect|SQLiteDialect|GenericDialect)
5523            && self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
5524        // Many dialects support `OR ALTER` right after `CREATE`, but we don't (yet).
5525        // ANSI SQL and Postgres support RECURSIVE here, but we don't support it either.
5526        let allow_unquoted_hyphen = dialect_of!(self is BigQueryDialect);
5527        let name = self.parse_object_name(allow_unquoted_hyphen)?;
5528        let columns = self.parse_view_columns()?;
5529        let mut options = CreateTableOptions::None;
5530        let with_options = self.parse_options(Keyword::WITH)?;
5531        if !with_options.is_empty() {
5532            options = CreateTableOptions::With(with_options);
5533        }
5534
5535        let cluster_by = if self.parse_keyword(Keyword::CLUSTER) {
5536            self.expect_keyword_is(Keyword::BY)?;
5537            self.parse_parenthesized_column_list(Optional, false)?
5538        } else {
5539            vec![]
5540        };
5541
5542        if dialect_of!(self is BigQueryDialect | GenericDialect) {
5543            if let Some(opts) = self.maybe_parse_options(Keyword::OPTIONS)? {
5544                if !opts.is_empty() {
5545                    options = CreateTableOptions::Options(opts);
5546                }
5547            };
5548        }
5549
5550        let to = if dialect_of!(self is ClickHouseDialect | GenericDialect)
5551            && self.parse_keyword(Keyword::TO)
5552        {
5553            Some(self.parse_object_name(false)?)
5554        } else {
5555            None
5556        };
5557
5558        let comment = if dialect_of!(self is SnowflakeDialect | GenericDialect)
5559            && self.parse_keyword(Keyword::COMMENT)
5560        {
5561            self.expect_token(&Token::Eq)?;
5562            Some(self.parse_comment_value()?)
5563        } else {
5564            None
5565        };
5566
5567        self.expect_keyword_is(Keyword::AS)?;
5568        let query = self.parse_query()?;
5569        // Optional `WITH [ CASCADED | LOCAL ] CHECK OPTION` is widely supported here.
5570
5571        let with_no_schema_binding = dialect_of!(self is RedshiftSqlDialect | GenericDialect)
5572            && self.parse_keywords(&[
5573                Keyword::WITH,
5574                Keyword::NO,
5575                Keyword::SCHEMA,
5576                Keyword::BINDING,
5577            ]);
5578
5579        Ok(Statement::CreateView {
5580            or_alter,
5581            name,
5582            columns,
5583            query,
5584            materialized,
5585            or_replace,
5586            options,
5587            cluster_by,
5588            comment,
5589            with_no_schema_binding,
5590            if_not_exists,
5591            temporary,
5592            to,
5593            params: create_view_params,
5594        })
5595    }
5596
5597    /// Parse optional parameters for the `CREATE VIEW` statement supported by [MySQL].
5598    ///
5599    /// [MySQL]: https://dev.mysql.com/doc/refman/9.1/en/create-view.html
5600    fn parse_create_view_params(&mut self) -> Result<Option<CreateViewParams>, ParserError> {
5601        let algorithm = if self.parse_keyword(Keyword::ALGORITHM) {
5602            self.expect_token(&Token::Eq)?;
5603            Some(
5604                match self.expect_one_of_keywords(&[
5605                    Keyword::UNDEFINED,
5606                    Keyword::MERGE,
5607                    Keyword::TEMPTABLE,
5608                ])? {
5609                    Keyword::UNDEFINED => CreateViewAlgorithm::Undefined,
5610                    Keyword::MERGE => CreateViewAlgorithm::Merge,
5611                    Keyword::TEMPTABLE => CreateViewAlgorithm::TempTable,
5612                    _ => {
5613                        self.prev_token();
5614                        let found = self.next_token();
5615                        return self
5616                            .expected("UNDEFINED or MERGE or TEMPTABLE after ALGORITHM =", found);
5617                    }
5618                },
5619            )
5620        } else {
5621            None
5622        };
5623        let definer = if self.parse_keyword(Keyword::DEFINER) {
5624            self.expect_token(&Token::Eq)?;
5625            Some(self.parse_grantee_name()?)
5626        } else {
5627            None
5628        };
5629        let security = if self.parse_keywords(&[Keyword::SQL, Keyword::SECURITY]) {
5630            Some(
5631                match self.expect_one_of_keywords(&[Keyword::DEFINER, Keyword::INVOKER])? {
5632                    Keyword::DEFINER => CreateViewSecurity::Definer,
5633                    Keyword::INVOKER => CreateViewSecurity::Invoker,
5634                    _ => {
5635                        self.prev_token();
5636                        let found = self.next_token();
5637                        return self.expected("DEFINER or INVOKER after SQL SECURITY", found);
5638                    }
5639                },
5640            )
5641        } else {
5642            None
5643        };
5644        if algorithm.is_some() || definer.is_some() || security.is_some() {
5645            Ok(Some(CreateViewParams {
5646                algorithm,
5647                definer,
5648                security,
5649            }))
5650        } else {
5651            Ok(None)
5652        }
5653    }
5654
5655    pub fn parse_create_role(&mut self) -> Result<Statement, ParserError> {
5656        let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
5657        let names = self.parse_comma_separated(|p| p.parse_object_name(false))?;
5658
5659        let _ = self.parse_keyword(Keyword::WITH); // [ WITH ]
5660
5661        let optional_keywords = if dialect_of!(self is MsSqlDialect) {
5662            vec![Keyword::AUTHORIZATION]
5663        } else if dialect_of!(self is PostgreSqlDialect) {
5664            vec![
5665                Keyword::LOGIN,
5666                Keyword::NOLOGIN,
5667                Keyword::INHERIT,
5668                Keyword::NOINHERIT,
5669                Keyword::BYPASSRLS,
5670                Keyword::NOBYPASSRLS,
5671                Keyword::PASSWORD,
5672                Keyword::CREATEDB,
5673                Keyword::NOCREATEDB,
5674                Keyword::CREATEROLE,
5675                Keyword::NOCREATEROLE,
5676                Keyword::SUPERUSER,
5677                Keyword::NOSUPERUSER,
5678                Keyword::REPLICATION,
5679                Keyword::NOREPLICATION,
5680                Keyword::CONNECTION,
5681                Keyword::VALID,
5682                Keyword::IN,
5683                Keyword::ROLE,
5684                Keyword::ADMIN,
5685                Keyword::USER,
5686            ]
5687        } else {
5688            vec![]
5689        };
5690
5691        // MSSQL
5692        let mut authorization_owner = None;
5693        // Postgres
5694        let mut login = None;
5695        let mut inherit = None;
5696        let mut bypassrls = None;
5697        let mut password = None;
5698        let mut create_db = None;
5699        let mut create_role = None;
5700        let mut superuser = None;
5701        let mut replication = None;
5702        let mut connection_limit = None;
5703        let mut valid_until = None;
5704        let mut in_role = vec![];
5705        let mut in_group = vec![];
5706        let mut role = vec![];
5707        let mut user = vec![];
5708        let mut admin = vec![];
5709
5710        while let Some(keyword) = self.parse_one_of_keywords(&optional_keywords) {
5711            let loc = self
5712                .tokens
5713                .get(self.index - 1)
5714                .map_or(Location { line: 0, column: 0 }, |t| t.span.start);
5715            match keyword {
5716                Keyword::AUTHORIZATION => {
5717                    if authorization_owner.is_some() {
5718                        parser_err!("Found multiple AUTHORIZATION", loc)
5719                    } else {
5720                        authorization_owner = Some(self.parse_object_name(false)?);
5721                        Ok(())
5722                    }
5723                }
5724                Keyword::LOGIN | Keyword::NOLOGIN => {
5725                    if login.is_some() {
5726                        parser_err!("Found multiple LOGIN or NOLOGIN", loc)
5727                    } else {
5728                        login = Some(keyword == Keyword::LOGIN);
5729                        Ok(())
5730                    }
5731                }
5732                Keyword::INHERIT | Keyword::NOINHERIT => {
5733                    if inherit.is_some() {
5734                        parser_err!("Found multiple INHERIT or NOINHERIT", loc)
5735                    } else {
5736                        inherit = Some(keyword == Keyword::INHERIT);
5737                        Ok(())
5738                    }
5739                }
5740                Keyword::BYPASSRLS | Keyword::NOBYPASSRLS => {
5741                    if bypassrls.is_some() {
5742                        parser_err!("Found multiple BYPASSRLS or NOBYPASSRLS", loc)
5743                    } else {
5744                        bypassrls = Some(keyword == Keyword::BYPASSRLS);
5745                        Ok(())
5746                    }
5747                }
5748                Keyword::CREATEDB | Keyword::NOCREATEDB => {
5749                    if create_db.is_some() {
5750                        parser_err!("Found multiple CREATEDB or NOCREATEDB", loc)
5751                    } else {
5752                        create_db = Some(keyword == Keyword::CREATEDB);
5753                        Ok(())
5754                    }
5755                }
5756                Keyword::CREATEROLE | Keyword::NOCREATEROLE => {
5757                    if create_role.is_some() {
5758                        parser_err!("Found multiple CREATEROLE or NOCREATEROLE", loc)
5759                    } else {
5760                        create_role = Some(keyword == Keyword::CREATEROLE);
5761                        Ok(())
5762                    }
5763                }
5764                Keyword::SUPERUSER | Keyword::NOSUPERUSER => {
5765                    if superuser.is_some() {
5766                        parser_err!("Found multiple SUPERUSER or NOSUPERUSER", loc)
5767                    } else {
5768                        superuser = Some(keyword == Keyword::SUPERUSER);
5769                        Ok(())
5770                    }
5771                }
5772                Keyword::REPLICATION | Keyword::NOREPLICATION => {
5773                    if replication.is_some() {
5774                        parser_err!("Found multiple REPLICATION or NOREPLICATION", loc)
5775                    } else {
5776                        replication = Some(keyword == Keyword::REPLICATION);
5777                        Ok(())
5778                    }
5779                }
5780                Keyword::PASSWORD => {
5781                    if password.is_some() {
5782                        parser_err!("Found multiple PASSWORD", loc)
5783                    } else {
5784                        password = if self.parse_keyword(Keyword::NULL) {
5785                            Some(Password::NullPassword)
5786                        } else {
5787                            Some(Password::Password(Expr::Value(self.parse_value()?)))
5788                        };
5789                        Ok(())
5790                    }
5791                }
5792                Keyword::CONNECTION => {
5793                    self.expect_keyword_is(Keyword::LIMIT)?;
5794                    if connection_limit.is_some() {
5795                        parser_err!("Found multiple CONNECTION LIMIT", loc)
5796                    } else {
5797                        connection_limit = Some(Expr::Value(self.parse_number_value()?));
5798                        Ok(())
5799                    }
5800                }
5801                Keyword::VALID => {
5802                    self.expect_keyword_is(Keyword::UNTIL)?;
5803                    if valid_until.is_some() {
5804                        parser_err!("Found multiple VALID UNTIL", loc)
5805                    } else {
5806                        valid_until = Some(Expr::Value(self.parse_value()?));
5807                        Ok(())
5808                    }
5809                }
5810                Keyword::IN => {
5811                    if self.parse_keyword(Keyword::ROLE) {
5812                        if !in_role.is_empty() {
5813                            parser_err!("Found multiple IN ROLE", loc)
5814                        } else {
5815                            in_role = self.parse_comma_separated(|p| p.parse_identifier())?;
5816                            Ok(())
5817                        }
5818                    } else if self.parse_keyword(Keyword::GROUP) {
5819                        if !in_group.is_empty() {
5820                            parser_err!("Found multiple IN GROUP", loc)
5821                        } else {
5822                            in_group = self.parse_comma_separated(|p| p.parse_identifier())?;
5823                            Ok(())
5824                        }
5825                    } else {
5826                        self.expected("ROLE or GROUP after IN", self.peek_token())
5827                    }
5828                }
5829                Keyword::ROLE => {
5830                    if !role.is_empty() {
5831                        parser_err!("Found multiple ROLE", loc)
5832                    } else {
5833                        role = self.parse_comma_separated(|p| p.parse_identifier())?;
5834                        Ok(())
5835                    }
5836                }
5837                Keyword::USER => {
5838                    if !user.is_empty() {
5839                        parser_err!("Found multiple USER", loc)
5840                    } else {
5841                        user = self.parse_comma_separated(|p| p.parse_identifier())?;
5842                        Ok(())
5843                    }
5844                }
5845                Keyword::ADMIN => {
5846                    if !admin.is_empty() {
5847                        parser_err!("Found multiple ADMIN", loc)
5848                    } else {
5849                        admin = self.parse_comma_separated(|p| p.parse_identifier())?;
5850                        Ok(())
5851                    }
5852                }
5853                _ => break,
5854            }?
5855        }
5856
5857        Ok(Statement::CreateRole {
5858            names,
5859            if_not_exists,
5860            login,
5861            inherit,
5862            bypassrls,
5863            password,
5864            create_db,
5865            create_role,
5866            replication,
5867            superuser,
5868            connection_limit,
5869            valid_until,
5870            in_role,
5871            in_group,
5872            role,
5873            user,
5874            admin,
5875            authorization_owner,
5876        })
5877    }
5878
5879    pub fn parse_owner(&mut self) -> Result<Owner, ParserError> {
5880        let owner = match self.parse_one_of_keywords(&[Keyword::CURRENT_USER, Keyword::CURRENT_ROLE, Keyword::SESSION_USER]) {
5881            Some(Keyword::CURRENT_USER) => Owner::CurrentUser,
5882            Some(Keyword::CURRENT_ROLE) => Owner::CurrentRole,
5883            Some(Keyword::SESSION_USER) => Owner::SessionUser,
5884            Some(_) => unreachable!(),
5885            None => {
5886                match self.parse_identifier() {
5887                    Ok(ident) => Owner::Ident(ident),
5888                    Err(e) => {
5889                        return Err(ParserError::ParserError(format!("Expected: CURRENT_USER, CURRENT_ROLE, SESSION_USER or identifier after OWNER TO. {e}")))
5890                    }
5891                }
5892            }
5893        };
5894        Ok(owner)
5895    }
5896
5897    /// ```sql
5898    ///     CREATE POLICY name ON table_name [ AS { PERMISSIVE | RESTRICTIVE } ]
5899    ///     [ FOR { ALL | SELECT | INSERT | UPDATE | DELETE } ]
5900    ///     [ TO { role_name | PUBLIC | CURRENT_USER | CURRENT_ROLE | SESSION_USER } [, ...] ]
5901    ///     [ USING ( using_expression ) ]
5902    ///     [ WITH CHECK ( with_check_expression ) ]
5903    /// ```
5904    ///
5905    /// [PostgreSQL Documentation](https://www.postgresql.org/docs/current/sql-createpolicy.html)
5906    pub fn parse_create_policy(&mut self) -> Result<Statement, ParserError> {
5907        let name = self.parse_identifier()?;
5908        self.expect_keyword_is(Keyword::ON)?;
5909        let table_name = self.parse_object_name(false)?;
5910
5911        let policy_type = if self.parse_keyword(Keyword::AS) {
5912            let keyword =
5913                self.expect_one_of_keywords(&[Keyword::PERMISSIVE, Keyword::RESTRICTIVE])?;
5914            Some(match keyword {
5915                Keyword::PERMISSIVE => CreatePolicyType::Permissive,
5916                Keyword::RESTRICTIVE => CreatePolicyType::Restrictive,
5917                _ => unreachable!(),
5918            })
5919        } else {
5920            None
5921        };
5922
5923        let command = if self.parse_keyword(Keyword::FOR) {
5924            let keyword = self.expect_one_of_keywords(&[
5925                Keyword::ALL,
5926                Keyword::SELECT,
5927                Keyword::INSERT,
5928                Keyword::UPDATE,
5929                Keyword::DELETE,
5930            ])?;
5931            Some(match keyword {
5932                Keyword::ALL => CreatePolicyCommand::All,
5933                Keyword::SELECT => CreatePolicyCommand::Select,
5934                Keyword::INSERT => CreatePolicyCommand::Insert,
5935                Keyword::UPDATE => CreatePolicyCommand::Update,
5936                Keyword::DELETE => CreatePolicyCommand::Delete,
5937                _ => unreachable!(),
5938            })
5939        } else {
5940            None
5941        };
5942
5943        let to = if self.parse_keyword(Keyword::TO) {
5944            Some(self.parse_comma_separated(|p| p.parse_owner())?)
5945        } else {
5946            None
5947        };
5948
5949        let using = if self.parse_keyword(Keyword::USING) {
5950            self.expect_token(&Token::LParen)?;
5951            let expr = self.parse_expr()?;
5952            self.expect_token(&Token::RParen)?;
5953            Some(expr)
5954        } else {
5955            None
5956        };
5957
5958        let with_check = if self.parse_keywords(&[Keyword::WITH, Keyword::CHECK]) {
5959            self.expect_token(&Token::LParen)?;
5960            let expr = self.parse_expr()?;
5961            self.expect_token(&Token::RParen)?;
5962            Some(expr)
5963        } else {
5964            None
5965        };
5966
5967        Ok(CreatePolicy {
5968            name,
5969            table_name,
5970            policy_type,
5971            command,
5972            to,
5973            using,
5974            with_check,
5975        })
5976    }
5977
5978    /// ```sql
5979    /// CREATE CONNECTOR [IF NOT EXISTS] connector_name
5980    /// [TYPE datasource_type]
5981    /// [URL datasource_url]
5982    /// [COMMENT connector_comment]
5983    /// [WITH DCPROPERTIES(property_name=property_value, ...)]
5984    /// ```
5985    ///
5986    /// [Hive Documentation](https://cwiki.apache.org/confluence/pages/viewpage.action?pageId=27362034#LanguageManualDDL-CreateDataConnectorCreateConnector)
5987    pub fn parse_create_connector(&mut self) -> Result<Statement, ParserError> {
5988        let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
5989        let name = self.parse_identifier()?;
5990
5991        let connector_type = if self.parse_keyword(Keyword::TYPE) {
5992            Some(self.parse_literal_string()?)
5993        } else {
5994            None
5995        };
5996
5997        let url = if self.parse_keyword(Keyword::URL) {
5998            Some(self.parse_literal_string()?)
5999        } else {
6000            None
6001        };
6002
6003        let comment = self.parse_optional_inline_comment()?;
6004
6005        let with_dcproperties =
6006            match self.parse_options_with_keywords(&[Keyword::WITH, Keyword::DCPROPERTIES])? {
6007                properties if !properties.is_empty() => Some(properties),
6008                _ => None,
6009            };
6010
6011        Ok(Statement::CreateConnector(CreateConnector {
6012            name,
6013            if_not_exists,
6014            connector_type,
6015            url,
6016            comment,
6017            with_dcproperties,
6018        }))
6019    }
6020
6021    pub fn parse_drop(&mut self) -> Result<Statement, ParserError> {
6022        // MySQL dialect supports `TEMPORARY`
6023        let temporary = dialect_of!(self is MySqlDialect | GenericDialect | DuckDbDialect)
6024            && self.parse_keyword(Keyword::TEMPORARY);
6025        let persistent = dialect_of!(self is DuckDbDialect)
6026            && self.parse_one_of_keywords(&[Keyword::PERSISTENT]).is_some();
6027
6028        let object_type = if self.parse_keyword(Keyword::TABLE) {
6029            ObjectType::Table
6030        } else if self.parse_keyword(Keyword::VIEW) {
6031            ObjectType::View
6032        } else if self.parse_keywords(&[Keyword::MATERIALIZED, Keyword::VIEW]) {
6033            ObjectType::MaterializedView
6034        } else if self.parse_keyword(Keyword::INDEX) {
6035            ObjectType::Index
6036        } else if self.parse_keyword(Keyword::ROLE) {
6037            ObjectType::Role
6038        } else if self.parse_keyword(Keyword::SCHEMA) {
6039            ObjectType::Schema
6040        } else if self.parse_keyword(Keyword::DATABASE) {
6041            ObjectType::Database
6042        } else if self.parse_keyword(Keyword::SEQUENCE) {
6043            ObjectType::Sequence
6044        } else if self.parse_keyword(Keyword::STAGE) {
6045            ObjectType::Stage
6046        } else if self.parse_keyword(Keyword::TYPE) {
6047            ObjectType::Type
6048        } else if self.parse_keyword(Keyword::FUNCTION) {
6049            return self.parse_drop_function();
6050        } else if self.parse_keyword(Keyword::POLICY) {
6051            return self.parse_drop_policy();
6052        } else if self.parse_keyword(Keyword::CONNECTOR) {
6053            return self.parse_drop_connector();
6054        } else if self.parse_keyword(Keyword::PROCEDURE) {
6055            return self.parse_drop_procedure();
6056        } else if self.parse_keyword(Keyword::SECRET) {
6057            return self.parse_drop_secret(temporary, persistent);
6058        } else if self.parse_keyword(Keyword::TRIGGER) {
6059            return self.parse_drop_trigger();
6060        } else if self.parse_keyword(Keyword::EXTENSION) {
6061            return self.parse_drop_extension();
6062        } else {
6063            return self.expected(
6064                "CONNECTOR, DATABASE, EXTENSION, FUNCTION, INDEX, POLICY, PROCEDURE, ROLE, SCHEMA, SECRET, SEQUENCE, STAGE, TABLE, TRIGGER, TYPE, VIEW, or MATERIALIZED VIEW after DROP",
6065                self.peek_token(),
6066            );
6067        };
6068        // Many dialects support the non-standard `IF EXISTS` clause and allow
6069        // specifying multiple objects to delete in a single statement
6070        let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
6071        let names = self.parse_comma_separated(|p| p.parse_object_name(false))?;
6072
6073        let loc = self.peek_token().span.start;
6074        let cascade = self.parse_keyword(Keyword::CASCADE);
6075        let restrict = self.parse_keyword(Keyword::RESTRICT);
6076        let purge = self.parse_keyword(Keyword::PURGE);
6077        if cascade && restrict {
6078            return parser_err!("Cannot specify both CASCADE and RESTRICT in DROP", loc);
6079        }
6080        if object_type == ObjectType::Role && (cascade || restrict || purge) {
6081            return parser_err!(
6082                "Cannot specify CASCADE, RESTRICT, or PURGE in DROP ROLE",
6083                loc
6084            );
6085        }
6086        Ok(Statement::Drop {
6087            object_type,
6088            if_exists,
6089            names,
6090            cascade,
6091            restrict,
6092            purge,
6093            temporary,
6094        })
6095    }
6096
6097    fn parse_optional_drop_behavior(&mut self) -> Option<DropBehavior> {
6098        match self.parse_one_of_keywords(&[Keyword::CASCADE, Keyword::RESTRICT]) {
6099            Some(Keyword::CASCADE) => Some(DropBehavior::Cascade),
6100            Some(Keyword::RESTRICT) => Some(DropBehavior::Restrict),
6101            _ => None,
6102        }
6103    }
6104
6105    /// ```sql
6106    /// DROP FUNCTION [ IF EXISTS ] name [ ( [ [ argmode ] [ argname ] argtype [, ...] ] ) ] [, ...]
6107    /// [ CASCADE | RESTRICT ]
6108    /// ```
6109    fn parse_drop_function(&mut self) -> Result<Statement, ParserError> {
6110        let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
6111        let func_desc = self.parse_comma_separated(Parser::parse_function_desc)?;
6112        let drop_behavior = self.parse_optional_drop_behavior();
6113        Ok(Statement::DropFunction {
6114            if_exists,
6115            func_desc,
6116            drop_behavior,
6117        })
6118    }
6119
6120    /// ```sql
6121    /// DROP POLICY [ IF EXISTS ] name ON table_name [ CASCADE | RESTRICT ]
6122    /// ```
6123    ///
6124    /// [PostgreSQL Documentation](https://www.postgresql.org/docs/current/sql-droppolicy.html)
6125    fn parse_drop_policy(&mut self) -> Result<Statement, ParserError> {
6126        let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
6127        let name = self.parse_identifier()?;
6128        self.expect_keyword_is(Keyword::ON)?;
6129        let table_name = self.parse_object_name(false)?;
6130        let drop_behavior = self.parse_optional_drop_behavior();
6131        Ok(Statement::DropPolicy {
6132            if_exists,
6133            name,
6134            table_name,
6135            drop_behavior,
6136        })
6137    }
6138    /// ```sql
6139    /// DROP CONNECTOR [IF EXISTS] name
6140    /// ```
6141    ///
6142    /// See [Hive](https://cwiki.apache.org/confluence/pages/viewpage.action?pageId=27362034#LanguageManualDDL-DropConnector)
6143    fn parse_drop_connector(&mut self) -> Result<Statement, ParserError> {
6144        let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
6145        let name = self.parse_identifier()?;
6146        Ok(Statement::DropConnector { if_exists, name })
6147    }
6148
6149    /// ```sql
6150    /// DROP PROCEDURE [ IF EXISTS ] name [ ( [ [ argmode ] [ argname ] argtype [, ...] ] ) ] [, ...]
6151    /// [ CASCADE | RESTRICT ]
6152    /// ```
6153    fn parse_drop_procedure(&mut self) -> Result<Statement, ParserError> {
6154        let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
6155        let proc_desc = self.parse_comma_separated(Parser::parse_function_desc)?;
6156        let drop_behavior = self.parse_optional_drop_behavior();
6157        Ok(Statement::DropProcedure {
6158            if_exists,
6159            proc_desc,
6160            drop_behavior,
6161        })
6162    }
6163
6164    fn parse_function_desc(&mut self) -> Result<FunctionDesc, ParserError> {
6165        let name = self.parse_object_name(false)?;
6166
6167        let args = if self.consume_token(&Token::LParen) {
6168            if self.consume_token(&Token::RParen) {
6169                None
6170            } else {
6171                let args = self.parse_comma_separated(Parser::parse_function_arg)?;
6172                self.expect_token(&Token::RParen)?;
6173                Some(args)
6174            }
6175        } else {
6176            None
6177        };
6178
6179        Ok(FunctionDesc { name, args })
6180    }
6181
6182    /// See [DuckDB Docs](https://duckdb.org/docs/sql/statements/create_secret.html) for more details.
6183    fn parse_drop_secret(
6184        &mut self,
6185        temporary: bool,
6186        persistent: bool,
6187    ) -> Result<Statement, ParserError> {
6188        let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
6189        let name = self.parse_identifier()?;
6190        let storage_specifier = if self.parse_keyword(Keyword::FROM) {
6191            self.parse_identifier().ok()
6192        } else {
6193            None
6194        };
6195        let temp = match (temporary, persistent) {
6196            (true, false) => Some(true),
6197            (false, true) => Some(false),
6198            (false, false) => None,
6199            _ => self.expected("TEMPORARY or PERSISTENT", self.peek_token())?,
6200        };
6201
6202        Ok(Statement::DropSecret {
6203            if_exists,
6204            temporary: temp,
6205            name,
6206            storage_specifier,
6207        })
6208    }
6209
6210    /// Parse a `DECLARE` statement.
6211    ///
6212    /// ```sql
6213    /// DECLARE name [ BINARY ] [ ASENSITIVE | INSENSITIVE ] [ [ NO ] SCROLL ]
6214    ///     CURSOR [ { WITH | WITHOUT } HOLD ] FOR query
6215    /// ```
6216    ///
6217    /// The syntax can vary significantly between warehouses. See the grammar
6218    /// on the warehouse specific function in such cases.
6219    pub fn parse_declare(&mut self) -> Result<Statement, ParserError> {
6220        if dialect_of!(self is BigQueryDialect) {
6221            return self.parse_big_query_declare();
6222        }
6223        if dialect_of!(self is SnowflakeDialect) {
6224            return self.parse_snowflake_declare();
6225        }
6226        if dialect_of!(self is MsSqlDialect) {
6227            return self.parse_mssql_declare();
6228        }
6229
6230        let name = self.parse_identifier()?;
6231
6232        let binary = Some(self.parse_keyword(Keyword::BINARY));
6233        let sensitive = if self.parse_keyword(Keyword::INSENSITIVE) {
6234            Some(true)
6235        } else if self.parse_keyword(Keyword::ASENSITIVE) {
6236            Some(false)
6237        } else {
6238            None
6239        };
6240        let scroll = if self.parse_keyword(Keyword::SCROLL) {
6241            Some(true)
6242        } else if self.parse_keywords(&[Keyword::NO, Keyword::SCROLL]) {
6243            Some(false)
6244        } else {
6245            None
6246        };
6247
6248        self.expect_keyword_is(Keyword::CURSOR)?;
6249        let declare_type = Some(DeclareType::Cursor);
6250
6251        let hold = match self.parse_one_of_keywords(&[Keyword::WITH, Keyword::WITHOUT]) {
6252            Some(keyword) => {
6253                self.expect_keyword_is(Keyword::HOLD)?;
6254
6255                match keyword {
6256                    Keyword::WITH => Some(true),
6257                    Keyword::WITHOUT => Some(false),
6258                    _ => unreachable!(),
6259                }
6260            }
6261            None => None,
6262        };
6263
6264        self.expect_keyword_is(Keyword::FOR)?;
6265
6266        let query = Some(self.parse_query()?);
6267
6268        Ok(Statement::Declare {
6269            stmts: vec![Declare {
6270                names: vec![name],
6271                data_type: None,
6272                assignment: None,
6273                declare_type,
6274                binary,
6275                sensitive,
6276                scroll,
6277                hold,
6278                for_query: query,
6279            }],
6280        })
6281    }
6282
6283    /// Parse a [BigQuery] `DECLARE` statement.
6284    ///
6285    /// Syntax:
6286    /// ```text
6287    /// DECLARE variable_name[, ...] [{ <variable_type> | <DEFAULT expression> }];
6288    /// ```
6289    /// [BigQuery]: https://cloud.google.com/bigquery/docs/reference/standard-sql/procedural-language#declare
6290    pub fn parse_big_query_declare(&mut self) -> Result<Statement, ParserError> {
6291        let names = self.parse_comma_separated(Parser::parse_identifier)?;
6292
6293        let data_type = match self.peek_token().token {
6294            Token::Word(w) if w.keyword == Keyword::DEFAULT => None,
6295            _ => Some(self.parse_data_type()?),
6296        };
6297
6298        let expr = if data_type.is_some() {
6299            if self.parse_keyword(Keyword::DEFAULT) {
6300                Some(self.parse_expr()?)
6301            } else {
6302                None
6303            }
6304        } else {
6305            // If no variable type - default expression must be specified, per BQ docs.
6306            // i.e `DECLARE foo;` is invalid.
6307            self.expect_keyword_is(Keyword::DEFAULT)?;
6308            Some(self.parse_expr()?)
6309        };
6310
6311        Ok(Statement::Declare {
6312            stmts: vec![Declare {
6313                names,
6314                data_type,
6315                assignment: expr.map(|expr| DeclareAssignment::Default(Box::new(expr))),
6316                declare_type: None,
6317                binary: None,
6318                sensitive: None,
6319                scroll: None,
6320                hold: None,
6321                for_query: None,
6322            }],
6323        })
6324    }
6325
6326    /// Parse a [Snowflake] `DECLARE` statement.
6327    ///
6328    /// Syntax:
6329    /// ```text
6330    /// DECLARE
6331    ///   [{ <variable_declaration>
6332    ///      | <cursor_declaration>
6333    ///      | <resultset_declaration>
6334    ///      | <exception_declaration> }; ... ]
6335    ///
6336    /// <variable_declaration>
6337    /// <variable_name> [<type>] [ { DEFAULT | := } <expression>]
6338    ///
6339    /// <cursor_declaration>
6340    /// <cursor_name> CURSOR FOR <query>
6341    ///
6342    /// <resultset_declaration>
6343    /// <resultset_name> RESULTSET [ { DEFAULT | := } ( <query> ) ] ;
6344    ///
6345    /// <exception_declaration>
6346    /// <exception_name> EXCEPTION [ ( <exception_number> , '<exception_message>' ) ] ;
6347    /// ```
6348    ///
6349    /// [Snowflake]: https://docs.snowflake.com/en/sql-reference/snowflake-scripting/declare
6350    pub fn parse_snowflake_declare(&mut self) -> Result<Statement, ParserError> {
6351        let mut stmts = vec![];
6352        loop {
6353            let name = self.parse_identifier()?;
6354            let (declare_type, for_query, assigned_expr, data_type) =
6355                if self.parse_keyword(Keyword::CURSOR) {
6356                    self.expect_keyword_is(Keyword::FOR)?;
6357                    match self.peek_token().token {
6358                        Token::Word(w) if w.keyword == Keyword::SELECT => (
6359                            Some(DeclareType::Cursor),
6360                            Some(self.parse_query()?),
6361                            None,
6362                            None,
6363                        ),
6364                        _ => (
6365                            Some(DeclareType::Cursor),
6366                            None,
6367                            Some(DeclareAssignment::For(Box::new(self.parse_expr()?))),
6368                            None,
6369                        ),
6370                    }
6371                } else if self.parse_keyword(Keyword::RESULTSET) {
6372                    let assigned_expr = if self.peek_token().token != Token::SemiColon {
6373                        self.parse_snowflake_variable_declaration_expression()?
6374                    } else {
6375                        // Nothing more to do. The statement has no further parameters.
6376                        None
6377                    };
6378
6379                    (Some(DeclareType::ResultSet), None, assigned_expr, None)
6380                } else if self.parse_keyword(Keyword::EXCEPTION) {
6381                    let assigned_expr = if self.peek_token().token == Token::LParen {
6382                        Some(DeclareAssignment::Expr(Box::new(self.parse_expr()?)))
6383                    } else {
6384                        // Nothing more to do. The statement has no further parameters.
6385                        None
6386                    };
6387
6388                    (Some(DeclareType::Exception), None, assigned_expr, None)
6389                } else {
6390                    // Without an explicit keyword, the only valid option is variable declaration.
6391                    let (assigned_expr, data_type) = if let Some(assigned_expr) =
6392                        self.parse_snowflake_variable_declaration_expression()?
6393                    {
6394                        (Some(assigned_expr), None)
6395                    } else if let Token::Word(_) = self.peek_token().token {
6396                        let data_type = self.parse_data_type()?;
6397                        (
6398                            self.parse_snowflake_variable_declaration_expression()?,
6399                            Some(data_type),
6400                        )
6401                    } else {
6402                        (None, None)
6403                    };
6404                    (None, None, assigned_expr, data_type)
6405                };
6406            let stmt = Declare {
6407                names: vec![name],
6408                data_type,
6409                assignment: assigned_expr,
6410                declare_type,
6411                binary: None,
6412                sensitive: None,
6413                scroll: None,
6414                hold: None,
6415                for_query,
6416            };
6417
6418            stmts.push(stmt);
6419            if self.consume_token(&Token::SemiColon) {
6420                match self.peek_token().token {
6421                    Token::Word(w)
6422                        if ALL_KEYWORDS
6423                            .binary_search(&w.value.to_uppercase().as_str())
6424                            .is_err() =>
6425                    {
6426                        // Not a keyword - start of a new declaration.
6427                        continue;
6428                    }
6429                    _ => {
6430                        // Put back the semicolon, this is the end of the DECLARE statement.
6431                        self.prev_token();
6432                    }
6433                }
6434            }
6435
6436            break;
6437        }
6438
6439        Ok(Statement::Declare { stmts })
6440    }
6441
6442    /// Parse a [MsSql] `DECLARE` statement.
6443    ///
6444    /// Syntax:
6445    /// ```text
6446    /// DECLARE
6447    // {
6448    //   { @local_variable [AS] data_type [ = value ] }
6449    //   | { @cursor_variable_name CURSOR [ FOR ] }
6450    // } [ ,...n ]
6451    /// ```
6452    /// [MsSql]: https://learn.microsoft.com/en-us/sql/t-sql/language-elements/declare-local-variable-transact-sql?view=sql-server-ver16
6453    pub fn parse_mssql_declare(&mut self) -> Result<Statement, ParserError> {
6454        let stmts = self.parse_comma_separated(Parser::parse_mssql_declare_stmt)?;
6455
6456        Ok(Statement::Declare { stmts })
6457    }
6458
6459    /// Parse the body of a [MsSql] `DECLARE`statement.
6460    ///
6461    /// Syntax:
6462    /// ```text
6463    // {
6464    //   { @local_variable [AS] data_type [ = value ] }
6465    //   | { @cursor_variable_name CURSOR [ FOR ]}
6466    // } [ ,...n ]
6467    /// ```
6468    /// [MsSql]: https://learn.microsoft.com/en-us/sql/t-sql/language-elements/declare-local-variable-transact-sql?view=sql-server-ver16
6469    pub fn parse_mssql_declare_stmt(&mut self) -> Result<Declare, ParserError> {
6470        let name = {
6471            let ident = self.parse_identifier()?;
6472            if !ident.value.starts_with('@')
6473                && !matches!(
6474                    self.peek_token().token,
6475                    Token::Word(w) if w.keyword == Keyword::CURSOR
6476                )
6477            {
6478                Err(ParserError::TokenizerError(
6479                    "Invalid MsSql variable declaration.".to_string(),
6480                ))
6481            } else {
6482                Ok(ident)
6483            }
6484        }?;
6485
6486        let (declare_type, data_type) = match self.peek_token().token {
6487            Token::Word(w) => match w.keyword {
6488                Keyword::CURSOR => {
6489                    self.next_token();
6490                    (Some(DeclareType::Cursor), None)
6491                }
6492                Keyword::AS => {
6493                    self.next_token();
6494                    (None, Some(self.parse_data_type()?))
6495                }
6496                _ => (None, Some(self.parse_data_type()?)),
6497            },
6498            _ => (None, Some(self.parse_data_type()?)),
6499        };
6500
6501        let (for_query, assignment) = if self.peek_keyword(Keyword::FOR) {
6502            self.next_token();
6503            let query = Some(self.parse_query()?);
6504            (query, None)
6505        } else {
6506            let assignment = self.parse_mssql_variable_declaration_expression()?;
6507            (None, assignment)
6508        };
6509
6510        Ok(Declare {
6511            names: vec![name],
6512            data_type,
6513            assignment,
6514            declare_type,
6515            binary: None,
6516            sensitive: None,
6517            scroll: None,
6518            hold: None,
6519            for_query,
6520        })
6521    }
6522
6523    /// Parses the assigned expression in a variable declaration.
6524    ///
6525    /// Syntax:
6526    /// ```text
6527    /// [ { DEFAULT | := } <expression>]
6528    /// ```
6529    /// <https://docs.snowflake.com/en/sql-reference/snowflake-scripting/declare#variable-declaration-syntax>
6530    pub fn parse_snowflake_variable_declaration_expression(
6531        &mut self,
6532    ) -> Result<Option<DeclareAssignment>, ParserError> {
6533        Ok(match self.peek_token().token {
6534            Token::Word(w) if w.keyword == Keyword::DEFAULT => {
6535                self.next_token(); // Skip `DEFAULT`
6536                Some(DeclareAssignment::Default(Box::new(self.parse_expr()?)))
6537            }
6538            Token::Assignment => {
6539                self.next_token(); // Skip `:=`
6540                Some(DeclareAssignment::DuckAssignment(Box::new(
6541                    self.parse_expr()?,
6542                )))
6543            }
6544            _ => None,
6545        })
6546    }
6547
6548    /// Parses the assigned expression in a variable declaration.
6549    ///
6550    /// Syntax:
6551    /// ```text
6552    /// [ = <expression>]
6553    /// ```
6554    pub fn parse_mssql_variable_declaration_expression(
6555        &mut self,
6556    ) -> Result<Option<DeclareAssignment>, ParserError> {
6557        Ok(match self.peek_token().token {
6558            Token::Eq => {
6559                self.next_token(); // Skip `=`
6560                Some(DeclareAssignment::MsSqlAssignment(Box::new(
6561                    self.parse_expr()?,
6562                )))
6563            }
6564            _ => None,
6565        })
6566    }
6567
6568    // FETCH [ direction { FROM | IN } ] cursor INTO target;
6569    pub fn parse_fetch_statement(&mut self) -> Result<Statement, ParserError> {
6570        let direction = if self.parse_keyword(Keyword::NEXT) {
6571            FetchDirection::Next
6572        } else if self.parse_keyword(Keyword::PRIOR) {
6573            FetchDirection::Prior
6574        } else if self.parse_keyword(Keyword::FIRST) {
6575            FetchDirection::First
6576        } else if self.parse_keyword(Keyword::LAST) {
6577            FetchDirection::Last
6578        } else if self.parse_keyword(Keyword::ABSOLUTE) {
6579            FetchDirection::Absolute {
6580                limit: self.parse_number_value()?.value,
6581            }
6582        } else if self.parse_keyword(Keyword::RELATIVE) {
6583            FetchDirection::Relative {
6584                limit: self.parse_number_value()?.value,
6585            }
6586        } else if self.parse_keyword(Keyword::FORWARD) {
6587            if self.parse_keyword(Keyword::ALL) {
6588                FetchDirection::ForwardAll
6589            } else {
6590                FetchDirection::Forward {
6591                    // TODO: Support optional
6592                    limit: Some(self.parse_number_value()?.value),
6593                }
6594            }
6595        } else if self.parse_keyword(Keyword::BACKWARD) {
6596            if self.parse_keyword(Keyword::ALL) {
6597                FetchDirection::BackwardAll
6598            } else {
6599                FetchDirection::Backward {
6600                    // TODO: Support optional
6601                    limit: Some(self.parse_number_value()?.value),
6602                }
6603            }
6604        } else if self.parse_keyword(Keyword::ALL) {
6605            FetchDirection::All
6606        } else {
6607            FetchDirection::Count {
6608                limit: self.parse_number_value()?.value,
6609            }
6610        };
6611
6612        self.expect_one_of_keywords(&[Keyword::FROM, Keyword::IN])?;
6613
6614        let name = self.parse_identifier()?;
6615
6616        let into = if self.parse_keyword(Keyword::INTO) {
6617            Some(self.parse_object_name(false)?)
6618        } else {
6619            None
6620        };
6621
6622        Ok(Statement::Fetch {
6623            name,
6624            direction,
6625            into,
6626        })
6627    }
6628
6629    pub fn parse_discard(&mut self) -> Result<Statement, ParserError> {
6630        let object_type = if self.parse_keyword(Keyword::ALL) {
6631            DiscardObject::ALL
6632        } else if self.parse_keyword(Keyword::PLANS) {
6633            DiscardObject::PLANS
6634        } else if self.parse_keyword(Keyword::SEQUENCES) {
6635            DiscardObject::SEQUENCES
6636        } else if self.parse_keyword(Keyword::TEMP) || self.parse_keyword(Keyword::TEMPORARY) {
6637            DiscardObject::TEMP
6638        } else {
6639            return self.expected(
6640                "ALL, PLANS, SEQUENCES, TEMP or TEMPORARY after DISCARD",
6641                self.peek_token(),
6642            );
6643        };
6644        Ok(Statement::Discard { object_type })
6645    }
6646
6647    pub fn parse_create_index(&mut self, unique: bool) -> Result<Statement, ParserError> {
6648        let concurrently = self.parse_keyword(Keyword::CONCURRENTLY);
6649        let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
6650        let index_name = if if_not_exists || !self.parse_keyword(Keyword::ON) {
6651            let index_name = self.parse_object_name(false)?;
6652            self.expect_keyword_is(Keyword::ON)?;
6653            Some(index_name)
6654        } else {
6655            None
6656        };
6657        let table_name = self.parse_object_name(false)?;
6658        let using = if self.parse_keyword(Keyword::USING) {
6659            Some(self.parse_index_type()?)
6660        } else {
6661            None
6662        };
6663
6664        self.expect_token(&Token::LParen)?;
6665        let columns = self.parse_comma_separated(Parser::parse_create_index_expr)?;
6666        self.expect_token(&Token::RParen)?;
6667
6668        let include = if self.parse_keyword(Keyword::INCLUDE) {
6669            self.expect_token(&Token::LParen)?;
6670            let columns = self.parse_comma_separated(|p| p.parse_identifier())?;
6671            self.expect_token(&Token::RParen)?;
6672            columns
6673        } else {
6674            vec![]
6675        };
6676
6677        let nulls_distinct = if self.parse_keyword(Keyword::NULLS) {
6678            let not = self.parse_keyword(Keyword::NOT);
6679            self.expect_keyword_is(Keyword::DISTINCT)?;
6680            Some(!not)
6681        } else {
6682            None
6683        };
6684
6685        let with = if self.dialect.supports_create_index_with_clause()
6686            && self.parse_keyword(Keyword::WITH)
6687        {
6688            self.expect_token(&Token::LParen)?;
6689            let with_params = self.parse_comma_separated(Parser::parse_expr)?;
6690            self.expect_token(&Token::RParen)?;
6691            with_params
6692        } else {
6693            Vec::new()
6694        };
6695
6696        let predicate = if self.parse_keyword(Keyword::WHERE) {
6697            Some(self.parse_expr()?)
6698        } else {
6699            None
6700        };
6701
6702        Ok(Statement::CreateIndex(CreateIndex {
6703            name: index_name,
6704            table_name,
6705            using,
6706            columns,
6707            unique,
6708            concurrently,
6709            if_not_exists,
6710            include,
6711            nulls_distinct,
6712            with,
6713            predicate,
6714        }))
6715    }
6716
6717    pub fn parse_create_extension(&mut self) -> Result<Statement, ParserError> {
6718        let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
6719        let name = self.parse_identifier()?;
6720
6721        let (schema, version, cascade) = if self.parse_keyword(Keyword::WITH) {
6722            let schema = if self.parse_keyword(Keyword::SCHEMA) {
6723                Some(self.parse_identifier()?)
6724            } else {
6725                None
6726            };
6727
6728            let version = if self.parse_keyword(Keyword::VERSION) {
6729                Some(self.parse_identifier()?)
6730            } else {
6731                None
6732            };
6733
6734            let cascade = self.parse_keyword(Keyword::CASCADE);
6735
6736            (schema, version, cascade)
6737        } else {
6738            (None, None, false)
6739        };
6740
6741        Ok(Statement::CreateExtension {
6742            name,
6743            if_not_exists,
6744            schema,
6745            version,
6746            cascade,
6747        })
6748    }
6749
6750    /// Parse a PostgreSQL-specific [Statement::DropExtension] statement.
6751    pub fn parse_drop_extension(&mut self) -> Result<Statement, ParserError> {
6752        let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
6753        let names = self.parse_comma_separated(|p| p.parse_identifier())?;
6754        let cascade_or_restrict =
6755            self.parse_one_of_keywords(&[Keyword::CASCADE, Keyword::RESTRICT]);
6756        Ok(Statement::DropExtension {
6757            names,
6758            if_exists,
6759            cascade_or_restrict: cascade_or_restrict
6760                .map(|k| match k {
6761                    Keyword::CASCADE => Ok(ReferentialAction::Cascade),
6762                    Keyword::RESTRICT => Ok(ReferentialAction::Restrict),
6763                    _ => self.expected("CASCADE or RESTRICT", self.peek_token()),
6764                })
6765                .transpose()?,
6766        })
6767    }
6768
6769    //TODO: Implement parsing for Skewed
6770    pub fn parse_hive_distribution(&mut self) -> Result<HiveDistributionStyle, ParserError> {
6771        if self.parse_keywords(&[Keyword::PARTITIONED, Keyword::BY]) {
6772            self.expect_token(&Token::LParen)?;
6773            let columns = self.parse_comma_separated(Parser::parse_column_def)?;
6774            self.expect_token(&Token::RParen)?;
6775            Ok(HiveDistributionStyle::PARTITIONED { columns })
6776        } else {
6777            Ok(HiveDistributionStyle::NONE)
6778        }
6779    }
6780
6781    pub fn parse_hive_formats(&mut self) -> Result<HiveFormat, ParserError> {
6782        let mut hive_format = HiveFormat::default();
6783        loop {
6784            match self.parse_one_of_keywords(&[
6785                Keyword::ROW,
6786                Keyword::STORED,
6787                Keyword::LOCATION,
6788                Keyword::WITH,
6789            ]) {
6790                Some(Keyword::ROW) => {
6791                    hive_format.row_format = Some(self.parse_row_format()?);
6792                }
6793                Some(Keyword::STORED) => {
6794                    self.expect_keyword_is(Keyword::AS)?;
6795                    if self.parse_keyword(Keyword::INPUTFORMAT) {
6796                        let input_format = self.parse_expr()?;
6797                        self.expect_keyword_is(Keyword::OUTPUTFORMAT)?;
6798                        let output_format = self.parse_expr()?;
6799                        hive_format.storage = Some(HiveIOFormat::IOF {
6800                            input_format,
6801                            output_format,
6802                        });
6803                    } else {
6804                        let format = self.parse_file_format()?;
6805                        hive_format.storage = Some(HiveIOFormat::FileFormat { format });
6806                    }
6807                }
6808                Some(Keyword::LOCATION) => {
6809                    hive_format.location = Some(self.parse_literal_string()?);
6810                }
6811                Some(Keyword::WITH) => {
6812                    self.prev_token();
6813                    let properties = self
6814                        .parse_options_with_keywords(&[Keyword::WITH, Keyword::SERDEPROPERTIES])?;
6815                    if !properties.is_empty() {
6816                        hive_format.serde_properties = Some(properties);
6817                    } else {
6818                        break;
6819                    }
6820                }
6821                None => break,
6822                _ => break,
6823            }
6824        }
6825
6826        Ok(hive_format)
6827    }
6828
6829    pub fn parse_row_format(&mut self) -> Result<HiveRowFormat, ParserError> {
6830        self.expect_keyword_is(Keyword::FORMAT)?;
6831        match self.parse_one_of_keywords(&[Keyword::SERDE, Keyword::DELIMITED]) {
6832            Some(Keyword::SERDE) => {
6833                let class = self.parse_literal_string()?;
6834                Ok(HiveRowFormat::SERDE { class })
6835            }
6836            _ => {
6837                let mut row_delimiters = vec![];
6838
6839                loop {
6840                    match self.parse_one_of_keywords(&[
6841                        Keyword::FIELDS,
6842                        Keyword::COLLECTION,
6843                        Keyword::MAP,
6844                        Keyword::LINES,
6845                        Keyword::NULL,
6846                    ]) {
6847                        Some(Keyword::FIELDS) => {
6848                            if self.parse_keywords(&[Keyword::TERMINATED, Keyword::BY]) {
6849                                row_delimiters.push(HiveRowDelimiter {
6850                                    delimiter: HiveDelimiter::FieldsTerminatedBy,
6851                                    char: self.parse_identifier()?,
6852                                });
6853
6854                                if self.parse_keywords(&[Keyword::ESCAPED, Keyword::BY]) {
6855                                    row_delimiters.push(HiveRowDelimiter {
6856                                        delimiter: HiveDelimiter::FieldsEscapedBy,
6857                                        char: self.parse_identifier()?,
6858                                    });
6859                                }
6860                            } else {
6861                                break;
6862                            }
6863                        }
6864                        Some(Keyword::COLLECTION) => {
6865                            if self.parse_keywords(&[
6866                                Keyword::ITEMS,
6867                                Keyword::TERMINATED,
6868                                Keyword::BY,
6869                            ]) {
6870                                row_delimiters.push(HiveRowDelimiter {
6871                                    delimiter: HiveDelimiter::CollectionItemsTerminatedBy,
6872                                    char: self.parse_identifier()?,
6873                                });
6874                            } else {
6875                                break;
6876                            }
6877                        }
6878                        Some(Keyword::MAP) => {
6879                            if self.parse_keywords(&[
6880                                Keyword::KEYS,
6881                                Keyword::TERMINATED,
6882                                Keyword::BY,
6883                            ]) {
6884                                row_delimiters.push(HiveRowDelimiter {
6885                                    delimiter: HiveDelimiter::MapKeysTerminatedBy,
6886                                    char: self.parse_identifier()?,
6887                                });
6888                            } else {
6889                                break;
6890                            }
6891                        }
6892                        Some(Keyword::LINES) => {
6893                            if self.parse_keywords(&[Keyword::TERMINATED, Keyword::BY]) {
6894                                row_delimiters.push(HiveRowDelimiter {
6895                                    delimiter: HiveDelimiter::LinesTerminatedBy,
6896                                    char: self.parse_identifier()?,
6897                                });
6898                            } else {
6899                                break;
6900                            }
6901                        }
6902                        Some(Keyword::NULL) => {
6903                            if self.parse_keywords(&[Keyword::DEFINED, Keyword::AS]) {
6904                                row_delimiters.push(HiveRowDelimiter {
6905                                    delimiter: HiveDelimiter::NullDefinedAs,
6906                                    char: self.parse_identifier()?,
6907                                });
6908                            } else {
6909                                break;
6910                            }
6911                        }
6912                        _ => {
6913                            break;
6914                        }
6915                    }
6916                }
6917
6918                Ok(HiveRowFormat::DELIMITED {
6919                    delimiters: row_delimiters,
6920                })
6921            }
6922        }
6923    }
6924
6925    fn parse_optional_on_cluster(&mut self) -> Result<Option<Ident>, ParserError> {
6926        if self.parse_keywords(&[Keyword::ON, Keyword::CLUSTER]) {
6927            Ok(Some(self.parse_identifier()?))
6928        } else {
6929            Ok(None)
6930        }
6931    }
6932
6933    pub fn parse_create_table(
6934        &mut self,
6935        or_replace: bool,
6936        temporary: bool,
6937        global: Option<bool>,
6938        transient: bool,
6939    ) -> Result<Statement, ParserError> {
6940        let allow_unquoted_hyphen = dialect_of!(self is BigQueryDialect);
6941        let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
6942        let table_name = self.parse_object_name(allow_unquoted_hyphen)?;
6943
6944        // Clickhouse has `ON CLUSTER 'cluster'` syntax for DDLs
6945        let on_cluster = self.parse_optional_on_cluster()?;
6946
6947        let like = if self.parse_keyword(Keyword::LIKE) || self.parse_keyword(Keyword::ILIKE) {
6948            self.parse_object_name(allow_unquoted_hyphen).ok()
6949        } else {
6950            None
6951        };
6952
6953        let clone = if self.parse_keyword(Keyword::CLONE) {
6954            self.parse_object_name(allow_unquoted_hyphen).ok()
6955        } else {
6956            None
6957        };
6958
6959        // parse optional column list (schema)
6960        let (columns, constraints) = self.parse_columns()?;
6961        let mut comment = if dialect_of!(self is HiveDialect)
6962            && self.parse_keyword(Keyword::COMMENT)
6963        {
6964            let next_token = self.next_token();
6965            match next_token.token {
6966                Token::SingleQuotedString(str) => Some(CommentDef::AfterColumnDefsWithoutEq(str)),
6967                _ => self.expected("comment", next_token)?,
6968            }
6969        } else {
6970            None
6971        };
6972
6973        // SQLite supports `WITHOUT ROWID` at the end of `CREATE TABLE`
6974        let without_rowid = self.parse_keywords(&[Keyword::WITHOUT, Keyword::ROWID]);
6975
6976        let hive_distribution = self.parse_hive_distribution()?;
6977        let clustered_by = self.parse_optional_clustered_by()?;
6978        let hive_formats = self.parse_hive_formats()?;
6979        // PostgreSQL supports `WITH ( options )`, before `AS`
6980        let with_options = self.parse_options(Keyword::WITH)?;
6981        let table_properties = self.parse_options(Keyword::TBLPROPERTIES)?;
6982
6983        let engine = if self.parse_keyword(Keyword::ENGINE) {
6984            self.expect_token(&Token::Eq)?;
6985            let next_token = self.next_token();
6986            match next_token.token {
6987                Token::Word(w) => {
6988                    let name = w.value;
6989                    let parameters = if self.peek_token() == Token::LParen {
6990                        Some(self.parse_parenthesized_identifiers()?)
6991                    } else {
6992                        None
6993                    };
6994                    Some(TableEngine { name, parameters })
6995                }
6996                _ => self.expected("identifier", next_token)?,
6997            }
6998        } else {
6999            None
7000        };
7001
7002        let auto_increment_offset = if self.parse_keyword(Keyword::AUTO_INCREMENT) {
7003            let _ = self.consume_token(&Token::Eq);
7004            let next_token = self.next_token();
7005            match next_token.token {
7006                Token::Number(s, _) => Some(Self::parse::<u32>(s, next_token.span.start)?),
7007                _ => self.expected("literal int", next_token)?,
7008            }
7009        } else {
7010            None
7011        };
7012
7013        // ClickHouse supports `PRIMARY KEY`, before `ORDER BY`
7014        // https://clickhouse.com/docs/en/sql-reference/statements/create/table#primary-key
7015        let primary_key = if dialect_of!(self is ClickHouseDialect | GenericDialect)
7016            && self.parse_keywords(&[Keyword::PRIMARY, Keyword::KEY])
7017        {
7018            Some(Box::new(self.parse_expr()?))
7019        } else {
7020            None
7021        };
7022
7023        let order_by = if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
7024            if self.consume_token(&Token::LParen) {
7025                let columns = if self.peek_token() != Token::RParen {
7026                    self.parse_comma_separated(|p| p.parse_expr())?
7027                } else {
7028                    vec![]
7029                };
7030                self.expect_token(&Token::RParen)?;
7031                Some(OneOrManyWithParens::Many(columns))
7032            } else {
7033                Some(OneOrManyWithParens::One(self.parse_expr()?))
7034            }
7035        } else {
7036            None
7037        };
7038
7039        let create_table_config = self.parse_optional_create_table_config()?;
7040
7041        let default_charset = if self.parse_keywords(&[Keyword::DEFAULT, Keyword::CHARSET]) {
7042            self.expect_token(&Token::Eq)?;
7043            let next_token = self.next_token();
7044            match next_token.token {
7045                Token::Word(w) => Some(w.value),
7046                _ => self.expected("identifier", next_token)?,
7047            }
7048        } else {
7049            None
7050        };
7051
7052        let collation = if self.parse_keywords(&[Keyword::COLLATE]) {
7053            self.expect_token(&Token::Eq)?;
7054            let next_token = self.next_token();
7055            match next_token.token {
7056                Token::Word(w) => Some(w.value),
7057                _ => self.expected("identifier", next_token)?,
7058            }
7059        } else {
7060            None
7061        };
7062
7063        let on_commit = if self.parse_keywords(&[Keyword::ON, Keyword::COMMIT]) {
7064            Some(self.parse_create_table_on_commit()?)
7065        } else {
7066            None
7067        };
7068
7069        let strict = self.parse_keyword(Keyword::STRICT);
7070
7071        // Excludes Hive dialect here since it has been handled after table column definitions.
7072        if !dialect_of!(self is HiveDialect) && self.parse_keyword(Keyword::COMMENT) {
7073            // rewind the COMMENT keyword
7074            self.prev_token();
7075            comment = self.parse_optional_inline_comment()?
7076        };
7077
7078        // Parse optional `AS ( query )`
7079        let query = if self.parse_keyword(Keyword::AS) {
7080            Some(self.parse_query()?)
7081        } else if self.dialect.supports_create_table_select() && self.parse_keyword(Keyword::SELECT)
7082        {
7083            // rewind the SELECT keyword
7084            self.prev_token();
7085            Some(self.parse_query()?)
7086        } else {
7087            None
7088        };
7089
7090        Ok(CreateTableBuilder::new(table_name)
7091            .temporary(temporary)
7092            .columns(columns)
7093            .constraints(constraints)
7094            .with_options(with_options)
7095            .table_properties(table_properties)
7096            .or_replace(or_replace)
7097            .if_not_exists(if_not_exists)
7098            .transient(transient)
7099            .hive_distribution(hive_distribution)
7100            .hive_formats(Some(hive_formats))
7101            .global(global)
7102            .query(query)
7103            .without_rowid(without_rowid)
7104            .like(like)
7105            .clone_clause(clone)
7106            .engine(engine)
7107            .comment(comment)
7108            .auto_increment_offset(auto_increment_offset)
7109            .order_by(order_by)
7110            .default_charset(default_charset)
7111            .collation(collation)
7112            .on_commit(on_commit)
7113            .on_cluster(on_cluster)
7114            .clustered_by(clustered_by)
7115            .partition_by(create_table_config.partition_by)
7116            .cluster_by(create_table_config.cluster_by)
7117            .options(create_table_config.options)
7118            .inherits(create_table_config.inherits)
7119            .primary_key(primary_key)
7120            .strict(strict)
7121            .build())
7122    }
7123
7124    pub(crate) fn parse_create_table_on_commit(&mut self) -> Result<OnCommit, ParserError> {
7125        if self.parse_keywords(&[Keyword::DELETE, Keyword::ROWS]) {
7126            Ok(OnCommit::DeleteRows)
7127        } else if self.parse_keywords(&[Keyword::PRESERVE, Keyword::ROWS]) {
7128            Ok(OnCommit::PreserveRows)
7129        } else if self.parse_keywords(&[Keyword::DROP]) {
7130            Ok(OnCommit::Drop)
7131        } else {
7132            parser_err!(
7133                "Expecting DELETE ROWS, PRESERVE ROWS or DROP",
7134                self.peek_token()
7135            )
7136        }
7137    }
7138
7139    /// Parse configuration like inheritance, partitioning, clustering information during the table creation.
7140    ///
7141    /// [BigQuery](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#syntax_2)
7142    /// [PostgreSQL Partitioning](https://www.postgresql.org/docs/current/ddl-partitioning.html)
7143    /// [PostgreSQL Inheritance](https://www.postgresql.org/docs/current/ddl-inherit.html)
7144    fn parse_optional_create_table_config(
7145        &mut self,
7146    ) -> Result<CreateTableConfiguration, ParserError> {
7147        let inherits = if self.parse_keyword(Keyword::INHERITS) {
7148            Some(self.parse_parenthesized_qualified_column_list(IsOptional::Mandatory, false)?)
7149        } else {
7150            None
7151        };
7152
7153        let partition_by = if dialect_of!(self is BigQueryDialect | PostgreSqlDialect | GenericDialect)
7154            && self.parse_keywords(&[Keyword::PARTITION, Keyword::BY])
7155        {
7156            Some(Box::new(self.parse_expr()?))
7157        } else {
7158            None
7159        };
7160
7161        let mut cluster_by = None;
7162        let mut options = None;
7163        if dialect_of!(self is BigQueryDialect | GenericDialect) {
7164            if self.parse_keywords(&[Keyword::CLUSTER, Keyword::BY]) {
7165                cluster_by = Some(WrappedCollection::NoWrapping(
7166                    self.parse_comma_separated(|p| p.parse_identifier())?,
7167                ));
7168            };
7169
7170            if let Token::Word(word) = self.peek_token().token {
7171                if word.keyword == Keyword::OPTIONS {
7172                    options = Some(self.parse_options(Keyword::OPTIONS)?);
7173                }
7174            };
7175        }
7176
7177        Ok(CreateTableConfiguration {
7178            partition_by,
7179            cluster_by,
7180            options,
7181            inherits,
7182        })
7183    }
7184
7185    pub fn parse_optional_inline_comment(&mut self) -> Result<Option<CommentDef>, ParserError> {
7186        let comment = if self.parse_keyword(Keyword::COMMENT) {
7187            let has_eq = self.consume_token(&Token::Eq);
7188            let comment = self.parse_comment_value()?;
7189            Some(if has_eq {
7190                CommentDef::WithEq(comment)
7191            } else {
7192                CommentDef::WithoutEq(comment)
7193            })
7194        } else {
7195            None
7196        };
7197        Ok(comment)
7198    }
7199
7200    pub fn parse_comment_value(&mut self) -> Result<String, ParserError> {
7201        let next_token = self.next_token();
7202        let value = match next_token.token {
7203            Token::SingleQuotedString(str) => str,
7204            Token::DollarQuotedString(str) => str.value,
7205            _ => self.expected("string literal", next_token)?,
7206        };
7207        Ok(value)
7208    }
7209
7210    pub fn parse_optional_procedure_parameters(
7211        &mut self,
7212    ) -> Result<Option<Vec<ProcedureParam>>, ParserError> {
7213        let mut params = vec![];
7214        if !self.consume_token(&Token::LParen) || self.consume_token(&Token::RParen) {
7215            return Ok(Some(params));
7216        }
7217        loop {
7218            if let Token::Word(_) = self.peek_token().token {
7219                params.push(self.parse_procedure_param()?)
7220            }
7221            let comma = self.consume_token(&Token::Comma);
7222            if self.consume_token(&Token::RParen) {
7223                // allow a trailing comma, even though it's not in standard
7224                break;
7225            } else if !comma {
7226                return self.expected("',' or ')' after parameter definition", self.peek_token());
7227            }
7228        }
7229        Ok(Some(params))
7230    }
7231
7232    pub fn parse_columns(&mut self) -> Result<(Vec<ColumnDef>, Vec<TableConstraint>), ParserError> {
7233        let mut columns = vec![];
7234        let mut constraints = vec![];
7235        if !self.consume_token(&Token::LParen) || self.consume_token(&Token::RParen) {
7236            return Ok((columns, constraints));
7237        }
7238
7239        loop {
7240            if let Some(constraint) = self.parse_optional_table_constraint()? {
7241                constraints.push(constraint);
7242            } else if let Token::Word(_) = self.peek_token().token {
7243                columns.push(self.parse_column_def()?);
7244            } else {
7245                return self.expected("column name or constraint definition", self.peek_token());
7246            }
7247
7248            let comma = self.consume_token(&Token::Comma);
7249            let rparen = self.peek_token().token == Token::RParen;
7250
7251            if !comma && !rparen {
7252                return self.expected("',' or ')' after column definition", self.peek_token());
7253            };
7254
7255            if rparen
7256                && (!comma
7257                    || self.dialect.supports_column_definition_trailing_commas()
7258                    || self.options.trailing_commas)
7259            {
7260                let _ = self.consume_token(&Token::RParen);
7261                break;
7262            }
7263        }
7264
7265        Ok((columns, constraints))
7266    }
7267
7268    pub fn parse_procedure_param(&mut self) -> Result<ProcedureParam, ParserError> {
7269        let name = self.parse_identifier()?;
7270        let data_type = self.parse_data_type()?;
7271        Ok(ProcedureParam { name, data_type })
7272    }
7273
7274    pub fn parse_column_def(&mut self) -> Result<ColumnDef, ParserError> {
7275        let name = self.parse_identifier()?;
7276        let data_type = if self.is_column_type_sqlite_unspecified() {
7277            DataType::Unspecified
7278        } else {
7279            self.parse_data_type()?
7280        };
7281        let mut options = vec![];
7282        loop {
7283            if self.parse_keyword(Keyword::CONSTRAINT) {
7284                let name = Some(self.parse_identifier()?);
7285                if let Some(option) = self.parse_optional_column_option()? {
7286                    options.push(ColumnOptionDef { name, option });
7287                } else {
7288                    return self.expected(
7289                        "constraint details after CONSTRAINT <name>",
7290                        self.peek_token(),
7291                    );
7292                }
7293            } else if let Some(option) = self.parse_optional_column_option()? {
7294                options.push(ColumnOptionDef { name: None, option });
7295            } else {
7296                break;
7297            };
7298        }
7299        Ok(ColumnDef {
7300            name,
7301            data_type,
7302            options,
7303        })
7304    }
7305
7306    fn is_column_type_sqlite_unspecified(&mut self) -> bool {
7307        if dialect_of!(self is SQLiteDialect) {
7308            match self.peek_token().token {
7309                Token::Word(word) => matches!(
7310                    word.keyword,
7311                    Keyword::CONSTRAINT
7312                        | Keyword::PRIMARY
7313                        | Keyword::NOT
7314                        | Keyword::UNIQUE
7315                        | Keyword::CHECK
7316                        | Keyword::DEFAULT
7317                        | Keyword::COLLATE
7318                        | Keyword::REFERENCES
7319                        | Keyword::GENERATED
7320                        | Keyword::AS
7321                ),
7322                _ => true, // e.g. comma immediately after column name
7323            }
7324        } else {
7325            false
7326        }
7327    }
7328
7329    pub fn parse_optional_column_option(&mut self) -> Result<Option<ColumnOption>, ParserError> {
7330        if let Some(option) = self.dialect.parse_column_option(self)? {
7331            return option;
7332        }
7333
7334        if self.parse_keywords(&[Keyword::CHARACTER, Keyword::SET]) {
7335            Ok(Some(ColumnOption::CharacterSet(
7336                self.parse_object_name(false)?,
7337            )))
7338        } else if self.parse_keywords(&[Keyword::COLLATE]) {
7339            Ok(Some(ColumnOption::Collation(
7340                self.parse_object_name(false)?,
7341            )))
7342        } else if self.parse_keywords(&[Keyword::NOT, Keyword::NULL]) {
7343            Ok(Some(ColumnOption::NotNull))
7344        } else if self.parse_keywords(&[Keyword::COMMENT]) {
7345            Ok(Some(ColumnOption::Comment(self.parse_comment_value()?)))
7346        } else if self.parse_keyword(Keyword::NULL) {
7347            Ok(Some(ColumnOption::Null))
7348        } else if self.parse_keyword(Keyword::DEFAULT) {
7349            Ok(Some(ColumnOption::Default(self.parse_expr()?)))
7350        } else if dialect_of!(self is ClickHouseDialect| GenericDialect)
7351            && self.parse_keyword(Keyword::MATERIALIZED)
7352        {
7353            Ok(Some(ColumnOption::Materialized(self.parse_expr()?)))
7354        } else if dialect_of!(self is ClickHouseDialect| GenericDialect)
7355            && self.parse_keyword(Keyword::ALIAS)
7356        {
7357            Ok(Some(ColumnOption::Alias(self.parse_expr()?)))
7358        } else if dialect_of!(self is ClickHouseDialect| GenericDialect)
7359            && self.parse_keyword(Keyword::EPHEMERAL)
7360        {
7361            // The expression is optional for the EPHEMERAL syntax, so we need to check
7362            // if the column definition has remaining tokens before parsing the expression.
7363            if matches!(self.peek_token().token, Token::Comma | Token::RParen) {
7364                Ok(Some(ColumnOption::Ephemeral(None)))
7365            } else {
7366                Ok(Some(ColumnOption::Ephemeral(Some(self.parse_expr()?))))
7367            }
7368        } else if self.parse_keywords(&[Keyword::PRIMARY, Keyword::KEY]) {
7369            let characteristics = self.parse_constraint_characteristics()?;
7370            Ok(Some(ColumnOption::Unique {
7371                is_primary: true,
7372                characteristics,
7373            }))
7374        } else if self.parse_keyword(Keyword::UNIQUE) {
7375            let characteristics = self.parse_constraint_characteristics()?;
7376            Ok(Some(ColumnOption::Unique {
7377                is_primary: false,
7378                characteristics,
7379            }))
7380        } else if self.parse_keyword(Keyword::REFERENCES) {
7381            let foreign_table = self.parse_object_name(false)?;
7382            // PostgreSQL allows omitting the column list and
7383            // uses the primary key column of the foreign table by default
7384            let referred_columns = self.parse_parenthesized_column_list(Optional, false)?;
7385            let mut on_delete = None;
7386            let mut on_update = None;
7387            loop {
7388                if on_delete.is_none() && self.parse_keywords(&[Keyword::ON, Keyword::DELETE]) {
7389                    on_delete = Some(self.parse_referential_action()?);
7390                } else if on_update.is_none()
7391                    && self.parse_keywords(&[Keyword::ON, Keyword::UPDATE])
7392                {
7393                    on_update = Some(self.parse_referential_action()?);
7394                } else {
7395                    break;
7396                }
7397            }
7398            let characteristics = self.parse_constraint_characteristics()?;
7399
7400            Ok(Some(ColumnOption::ForeignKey {
7401                foreign_table,
7402                referred_columns,
7403                on_delete,
7404                on_update,
7405                characteristics,
7406            }))
7407        } else if self.parse_keyword(Keyword::CHECK) {
7408            self.expect_token(&Token::LParen)?;
7409            let expr = self.parse_expr()?;
7410            self.expect_token(&Token::RParen)?;
7411            Ok(Some(ColumnOption::Check(expr)))
7412        } else if self.parse_keyword(Keyword::AUTO_INCREMENT)
7413            && dialect_of!(self is MySqlDialect | GenericDialect)
7414        {
7415            // Support AUTO_INCREMENT for MySQL
7416            Ok(Some(ColumnOption::DialectSpecific(vec![
7417                Token::make_keyword("AUTO_INCREMENT"),
7418            ])))
7419        } else if self.parse_keyword(Keyword::AUTOINCREMENT)
7420            && dialect_of!(self is SQLiteDialect |  GenericDialect)
7421        {
7422            // Support AUTOINCREMENT for SQLite
7423            Ok(Some(ColumnOption::DialectSpecific(vec![
7424                Token::make_keyword("AUTOINCREMENT"),
7425            ])))
7426        } else if self.parse_keyword(Keyword::ASC)
7427            && self.dialect.supports_asc_desc_in_column_definition()
7428        {
7429            // Support ASC for SQLite
7430            Ok(Some(ColumnOption::DialectSpecific(vec![
7431                Token::make_keyword("ASC"),
7432            ])))
7433        } else if self.parse_keyword(Keyword::DESC)
7434            && self.dialect.supports_asc_desc_in_column_definition()
7435        {
7436            // Support DESC for SQLite
7437            Ok(Some(ColumnOption::DialectSpecific(vec![
7438                Token::make_keyword("DESC"),
7439            ])))
7440        } else if self.parse_keywords(&[Keyword::ON, Keyword::UPDATE])
7441            && dialect_of!(self is MySqlDialect | GenericDialect)
7442        {
7443            let expr = self.parse_expr()?;
7444            Ok(Some(ColumnOption::OnUpdate(expr)))
7445        } else if self.parse_keyword(Keyword::GENERATED) {
7446            self.parse_optional_column_option_generated()
7447        } else if dialect_of!(self is BigQueryDialect | GenericDialect)
7448            && self.parse_keyword(Keyword::OPTIONS)
7449        {
7450            self.prev_token();
7451            Ok(Some(ColumnOption::Options(
7452                self.parse_options(Keyword::OPTIONS)?,
7453            )))
7454        } else if self.parse_keyword(Keyword::AS)
7455            && dialect_of!(self is MySqlDialect | SQLiteDialect | DuckDbDialect | GenericDialect)
7456        {
7457            self.parse_optional_column_option_as()
7458        } else if self.parse_keyword(Keyword::IDENTITY)
7459            && dialect_of!(self is MsSqlDialect | GenericDialect)
7460        {
7461            let parameters = if self.consume_token(&Token::LParen) {
7462                let seed = self.parse_number()?;
7463                self.expect_token(&Token::Comma)?;
7464                let increment = self.parse_number()?;
7465                self.expect_token(&Token::RParen)?;
7466
7467                Some(IdentityPropertyFormatKind::FunctionCall(
7468                    IdentityParameters { seed, increment },
7469                ))
7470            } else {
7471                None
7472            };
7473            Ok(Some(ColumnOption::Identity(
7474                IdentityPropertyKind::Identity(IdentityProperty {
7475                    parameters,
7476                    order: None,
7477                }),
7478            )))
7479        } else if dialect_of!(self is SQLiteDialect | GenericDialect)
7480            && self.parse_keywords(&[Keyword::ON, Keyword::CONFLICT])
7481        {
7482            // Support ON CONFLICT for SQLite
7483            Ok(Some(ColumnOption::OnConflict(
7484                self.expect_one_of_keywords(&[
7485                    Keyword::ROLLBACK,
7486                    Keyword::ABORT,
7487                    Keyword::FAIL,
7488                    Keyword::IGNORE,
7489                    Keyword::REPLACE,
7490                ])?,
7491            )))
7492        } else {
7493            Ok(None)
7494        }
7495    }
7496
7497    pub(crate) fn parse_tag(&mut self) -> Result<Tag, ParserError> {
7498        let name = self.parse_identifier()?;
7499        self.expect_token(&Token::Eq)?;
7500        let value = self.parse_literal_string()?;
7501
7502        Ok(Tag::new(name, value))
7503    }
7504
7505    fn parse_optional_column_option_generated(
7506        &mut self,
7507    ) -> Result<Option<ColumnOption>, ParserError> {
7508        if self.parse_keywords(&[Keyword::ALWAYS, Keyword::AS, Keyword::IDENTITY]) {
7509            let mut sequence_options = vec![];
7510            if self.expect_token(&Token::LParen).is_ok() {
7511                sequence_options = self.parse_create_sequence_options()?;
7512                self.expect_token(&Token::RParen)?;
7513            }
7514            Ok(Some(ColumnOption::Generated {
7515                generated_as: GeneratedAs::Always,
7516                sequence_options: Some(sequence_options),
7517                generation_expr: None,
7518                generation_expr_mode: None,
7519                generated_keyword: true,
7520            }))
7521        } else if self.parse_keywords(&[
7522            Keyword::BY,
7523            Keyword::DEFAULT,
7524            Keyword::AS,
7525            Keyword::IDENTITY,
7526        ]) {
7527            let mut sequence_options = vec![];
7528            if self.expect_token(&Token::LParen).is_ok() {
7529                sequence_options = self.parse_create_sequence_options()?;
7530                self.expect_token(&Token::RParen)?;
7531            }
7532            Ok(Some(ColumnOption::Generated {
7533                generated_as: GeneratedAs::ByDefault,
7534                sequence_options: Some(sequence_options),
7535                generation_expr: None,
7536                generation_expr_mode: None,
7537                generated_keyword: true,
7538            }))
7539        } else if self.parse_keywords(&[Keyword::ALWAYS, Keyword::AS]) {
7540            if self.expect_token(&Token::LParen).is_ok() {
7541                let expr = self.parse_expr()?;
7542                self.expect_token(&Token::RParen)?;
7543                let (gen_as, expr_mode) = if self.parse_keywords(&[Keyword::STORED]) {
7544                    Ok((
7545                        GeneratedAs::ExpStored,
7546                        Some(GeneratedExpressionMode::Stored),
7547                    ))
7548                } else if dialect_of!(self is PostgreSqlDialect) {
7549                    // Postgres' AS IDENTITY branches are above, this one needs STORED
7550                    self.expected("STORED", self.peek_token())
7551                } else if self.parse_keywords(&[Keyword::VIRTUAL]) {
7552                    Ok((GeneratedAs::Always, Some(GeneratedExpressionMode::Virtual)))
7553                } else {
7554                    Ok((GeneratedAs::Always, None))
7555                }?;
7556
7557                Ok(Some(ColumnOption::Generated {
7558                    generated_as: gen_as,
7559                    sequence_options: None,
7560                    generation_expr: Some(expr),
7561                    generation_expr_mode: expr_mode,
7562                    generated_keyword: true,
7563                }))
7564            } else {
7565                Ok(None)
7566            }
7567        } else {
7568            Ok(None)
7569        }
7570    }
7571
7572    fn parse_optional_column_option_as(&mut self) -> Result<Option<ColumnOption>, ParserError> {
7573        // Some DBs allow 'AS (expr)', shorthand for GENERATED ALWAYS AS
7574        self.expect_token(&Token::LParen)?;
7575        let expr = self.parse_expr()?;
7576        self.expect_token(&Token::RParen)?;
7577
7578        let (gen_as, expr_mode) = if self.parse_keywords(&[Keyword::STORED]) {
7579            (
7580                GeneratedAs::ExpStored,
7581                Some(GeneratedExpressionMode::Stored),
7582            )
7583        } else if self.parse_keywords(&[Keyword::VIRTUAL]) {
7584            (GeneratedAs::Always, Some(GeneratedExpressionMode::Virtual))
7585        } else {
7586            (GeneratedAs::Always, None)
7587        };
7588
7589        Ok(Some(ColumnOption::Generated {
7590            generated_as: gen_as,
7591            sequence_options: None,
7592            generation_expr: Some(expr),
7593            generation_expr_mode: expr_mode,
7594            generated_keyword: false,
7595        }))
7596    }
7597
7598    pub fn parse_optional_clustered_by(&mut self) -> Result<Option<ClusteredBy>, ParserError> {
7599        let clustered_by = if dialect_of!(self is HiveDialect|GenericDialect)
7600            && self.parse_keywords(&[Keyword::CLUSTERED, Keyword::BY])
7601        {
7602            let columns = self.parse_parenthesized_column_list(Mandatory, false)?;
7603
7604            let sorted_by = if self.parse_keywords(&[Keyword::SORTED, Keyword::BY]) {
7605                self.expect_token(&Token::LParen)?;
7606                let sorted_by_columns = self.parse_comma_separated(|p| p.parse_order_by_expr())?;
7607                self.expect_token(&Token::RParen)?;
7608                Some(sorted_by_columns)
7609            } else {
7610                None
7611            };
7612
7613            self.expect_keyword_is(Keyword::INTO)?;
7614            let num_buckets = self.parse_number_value()?.value;
7615            self.expect_keyword_is(Keyword::BUCKETS)?;
7616            Some(ClusteredBy {
7617                columns,
7618                sorted_by,
7619                num_buckets,
7620            })
7621        } else {
7622            None
7623        };
7624        Ok(clustered_by)
7625    }
7626
7627    pub fn parse_referential_action(&mut self) -> Result<ReferentialAction, ParserError> {
7628        if self.parse_keyword(Keyword::RESTRICT) {
7629            Ok(ReferentialAction::Restrict)
7630        } else if self.parse_keyword(Keyword::CASCADE) {
7631            Ok(ReferentialAction::Cascade)
7632        } else if self.parse_keywords(&[Keyword::SET, Keyword::NULL]) {
7633            Ok(ReferentialAction::SetNull)
7634        } else if self.parse_keywords(&[Keyword::NO, Keyword::ACTION]) {
7635            Ok(ReferentialAction::NoAction)
7636        } else if self.parse_keywords(&[Keyword::SET, Keyword::DEFAULT]) {
7637            Ok(ReferentialAction::SetDefault)
7638        } else {
7639            self.expected(
7640                "one of RESTRICT, CASCADE, SET NULL, NO ACTION or SET DEFAULT",
7641                self.peek_token(),
7642            )
7643        }
7644    }
7645
7646    pub fn parse_constraint_characteristics(
7647        &mut self,
7648    ) -> Result<Option<ConstraintCharacteristics>, ParserError> {
7649        let mut cc = ConstraintCharacteristics::default();
7650
7651        loop {
7652            if cc.deferrable.is_none() && self.parse_keywords(&[Keyword::NOT, Keyword::DEFERRABLE])
7653            {
7654                cc.deferrable = Some(false);
7655            } else if cc.deferrable.is_none() && self.parse_keyword(Keyword::DEFERRABLE) {
7656                cc.deferrable = Some(true);
7657            } else if cc.initially.is_none() && self.parse_keyword(Keyword::INITIALLY) {
7658                if self.parse_keyword(Keyword::DEFERRED) {
7659                    cc.initially = Some(DeferrableInitial::Deferred);
7660                } else if self.parse_keyword(Keyword::IMMEDIATE) {
7661                    cc.initially = Some(DeferrableInitial::Immediate);
7662                } else {
7663                    self.expected("one of DEFERRED or IMMEDIATE", self.peek_token())?;
7664                }
7665            } else if cc.enforced.is_none() && self.parse_keyword(Keyword::ENFORCED) {
7666                cc.enforced = Some(true);
7667            } else if cc.enforced.is_none()
7668                && self.parse_keywords(&[Keyword::NOT, Keyword::ENFORCED])
7669            {
7670                cc.enforced = Some(false);
7671            } else {
7672                break;
7673            }
7674        }
7675
7676        if cc.deferrable.is_some() || cc.initially.is_some() || cc.enforced.is_some() {
7677            Ok(Some(cc))
7678        } else {
7679            Ok(None)
7680        }
7681    }
7682
7683    pub fn parse_optional_table_constraint(
7684        &mut self,
7685    ) -> Result<Option<TableConstraint>, ParserError> {
7686        let name = if self.parse_keyword(Keyword::CONSTRAINT) {
7687            Some(self.parse_identifier()?)
7688        } else {
7689            None
7690        };
7691
7692        let next_token = self.next_token();
7693        match next_token.token {
7694            Token::Word(w) if w.keyword == Keyword::UNIQUE => {
7695                let index_type_display = self.parse_index_type_display();
7696                if !dialect_of!(self is GenericDialect | MySqlDialect)
7697                    && !index_type_display.is_none()
7698                {
7699                    return self
7700                        .expected("`index_name` or `(column_name [, ...])`", self.peek_token());
7701                }
7702
7703                let nulls_distinct = self.parse_optional_nulls_distinct()?;
7704
7705                // optional index name
7706                let index_name = self.parse_optional_indent()?;
7707                let index_type = self.parse_optional_using_then_index_type()?;
7708
7709                let columns = self.parse_parenthesized_column_list(Mandatory, false)?;
7710                let index_options = self.parse_index_options()?;
7711                let characteristics = self.parse_constraint_characteristics()?;
7712                Ok(Some(TableConstraint::Unique {
7713                    name,
7714                    index_name,
7715                    index_type_display,
7716                    index_type,
7717                    columns,
7718                    index_options,
7719                    characteristics,
7720                    nulls_distinct,
7721                }))
7722            }
7723            Token::Word(w) if w.keyword == Keyword::PRIMARY => {
7724                // after `PRIMARY` always stay `KEY`
7725                self.expect_keyword_is(Keyword::KEY)?;
7726
7727                // optional index name
7728                let index_name = self.parse_optional_indent()?;
7729                let index_type = self.parse_optional_using_then_index_type()?;
7730
7731                let columns = self.parse_parenthesized_column_list(Mandatory, false)?;
7732                let index_options = self.parse_index_options()?;
7733                let characteristics = self.parse_constraint_characteristics()?;
7734                Ok(Some(TableConstraint::PrimaryKey {
7735                    name,
7736                    index_name,
7737                    index_type,
7738                    columns,
7739                    index_options,
7740                    characteristics,
7741                }))
7742            }
7743            Token::Word(w) if w.keyword == Keyword::FOREIGN => {
7744                self.expect_keyword_is(Keyword::KEY)?;
7745                let columns = self.parse_parenthesized_column_list(Mandatory, false)?;
7746                self.expect_keyword_is(Keyword::REFERENCES)?;
7747                let foreign_table = self.parse_object_name(false)?;
7748                let referred_columns = self.parse_parenthesized_column_list(Optional, false)?;
7749                let mut on_delete = None;
7750                let mut on_update = None;
7751                loop {
7752                    if on_delete.is_none() && self.parse_keywords(&[Keyword::ON, Keyword::DELETE]) {
7753                        on_delete = Some(self.parse_referential_action()?);
7754                    } else if on_update.is_none()
7755                        && self.parse_keywords(&[Keyword::ON, Keyword::UPDATE])
7756                    {
7757                        on_update = Some(self.parse_referential_action()?);
7758                    } else {
7759                        break;
7760                    }
7761                }
7762
7763                let characteristics = self.parse_constraint_characteristics()?;
7764
7765                Ok(Some(TableConstraint::ForeignKey {
7766                    name,
7767                    columns,
7768                    foreign_table,
7769                    referred_columns,
7770                    on_delete,
7771                    on_update,
7772                    characteristics,
7773                }))
7774            }
7775            Token::Word(w) if w.keyword == Keyword::CHECK => {
7776                self.expect_token(&Token::LParen)?;
7777                let expr = Box::new(self.parse_expr()?);
7778                self.expect_token(&Token::RParen)?;
7779                Ok(Some(TableConstraint::Check { name, expr }))
7780            }
7781            Token::Word(w)
7782                if (w.keyword == Keyword::INDEX || w.keyword == Keyword::KEY)
7783                    && dialect_of!(self is GenericDialect | MySqlDialect)
7784                    && name.is_none() =>
7785            {
7786                let display_as_key = w.keyword == Keyword::KEY;
7787
7788                let name = match self.peek_token().token {
7789                    Token::Word(word) if word.keyword == Keyword::USING => None,
7790                    _ => self.parse_optional_indent()?,
7791                };
7792
7793                let index_type = self.parse_optional_using_then_index_type()?;
7794                let columns = self.parse_parenthesized_column_list(Mandatory, false)?;
7795
7796                Ok(Some(TableConstraint::Index {
7797                    display_as_key,
7798                    name,
7799                    index_type,
7800                    columns,
7801                }))
7802            }
7803            Token::Word(w)
7804                if (w.keyword == Keyword::FULLTEXT || w.keyword == Keyword::SPATIAL)
7805                    && dialect_of!(self is GenericDialect | MySqlDialect) =>
7806            {
7807                if let Some(name) = name {
7808                    return self.expected(
7809                        "FULLTEXT or SPATIAL option without constraint name",
7810                        TokenWithSpan {
7811                            token: Token::make_keyword(&name.to_string()),
7812                            span: next_token.span,
7813                        },
7814                    );
7815                }
7816
7817                let fulltext = w.keyword == Keyword::FULLTEXT;
7818
7819                let index_type_display = self.parse_index_type_display();
7820
7821                let opt_index_name = self.parse_optional_indent()?;
7822
7823                let columns = self.parse_parenthesized_column_list(Mandatory, false)?;
7824
7825                Ok(Some(TableConstraint::FulltextOrSpatial {
7826                    fulltext,
7827                    index_type_display,
7828                    opt_index_name,
7829                    columns,
7830                }))
7831            }
7832            _ => {
7833                if name.is_some() {
7834                    self.expected("PRIMARY, UNIQUE, FOREIGN, or CHECK", next_token)
7835                } else {
7836                    self.prev_token();
7837                    Ok(None)
7838                }
7839            }
7840        }
7841    }
7842
7843    fn parse_optional_nulls_distinct(&mut self) -> Result<NullsDistinctOption, ParserError> {
7844        Ok(if self.parse_keyword(Keyword::NULLS) {
7845            let not = self.parse_keyword(Keyword::NOT);
7846            self.expect_keyword_is(Keyword::DISTINCT)?;
7847            if not {
7848                NullsDistinctOption::NotDistinct
7849            } else {
7850                NullsDistinctOption::Distinct
7851            }
7852        } else {
7853            NullsDistinctOption::None
7854        })
7855    }
7856
7857    pub fn maybe_parse_options(
7858        &mut self,
7859        keyword: Keyword,
7860    ) -> Result<Option<Vec<SqlOption>>, ParserError> {
7861        if let Token::Word(word) = self.peek_token().token {
7862            if word.keyword == keyword {
7863                return Ok(Some(self.parse_options(keyword)?));
7864            }
7865        };
7866        Ok(None)
7867    }
7868
7869    pub fn parse_options(&mut self, keyword: Keyword) -> Result<Vec<SqlOption>, ParserError> {
7870        if self.parse_keyword(keyword) {
7871            self.expect_token(&Token::LParen)?;
7872            let options = self.parse_comma_separated0(Parser::parse_sql_option, Token::RParen)?;
7873            self.expect_token(&Token::RParen)?;
7874            Ok(options)
7875        } else {
7876            Ok(vec![])
7877        }
7878    }
7879
7880    pub fn parse_options_with_keywords(
7881        &mut self,
7882        keywords: &[Keyword],
7883    ) -> Result<Vec<SqlOption>, ParserError> {
7884        if self.parse_keywords(keywords) {
7885            self.expect_token(&Token::LParen)?;
7886            let options = self.parse_comma_separated(Parser::parse_sql_option)?;
7887            self.expect_token(&Token::RParen)?;
7888            Ok(options)
7889        } else {
7890            Ok(vec![])
7891        }
7892    }
7893
7894    pub fn parse_index_type(&mut self) -> Result<IndexType, ParserError> {
7895        Ok(if self.parse_keyword(Keyword::BTREE) {
7896            IndexType::BTree
7897        } else if self.parse_keyword(Keyword::HASH) {
7898            IndexType::Hash
7899        } else if self.parse_keyword(Keyword::GIN) {
7900            IndexType::GIN
7901        } else if self.parse_keyword(Keyword::GIST) {
7902            IndexType::GiST
7903        } else if self.parse_keyword(Keyword::SPGIST) {
7904            IndexType::SPGiST
7905        } else if self.parse_keyword(Keyword::BRIN) {
7906            IndexType::BRIN
7907        } else if self.parse_keyword(Keyword::BLOOM) {
7908            IndexType::Bloom
7909        } else {
7910            IndexType::Custom(self.parse_identifier()?)
7911        })
7912    }
7913
7914    /// Optionally parse the `USING` keyword, followed by an [IndexType]
7915    /// Example:
7916    /// ```sql
7917    //// USING BTREE (name, age DESC)
7918    /// ```
7919    pub fn parse_optional_using_then_index_type(
7920        &mut self,
7921    ) -> Result<Option<IndexType>, ParserError> {
7922        if self.parse_keyword(Keyword::USING) {
7923            Ok(Some(self.parse_index_type()?))
7924        } else {
7925            Ok(None)
7926        }
7927    }
7928
7929    /// Parse `[ident]`, mostly `ident` is name, like:
7930    /// `window_name`, `index_name`, ...
7931    pub fn parse_optional_indent(&mut self) -> Result<Option<Ident>, ParserError> {
7932        self.maybe_parse(|parser| parser.parse_identifier())
7933    }
7934
7935    #[must_use]
7936    pub fn parse_index_type_display(&mut self) -> KeyOrIndexDisplay {
7937        if self.parse_keyword(Keyword::KEY) {
7938            KeyOrIndexDisplay::Key
7939        } else if self.parse_keyword(Keyword::INDEX) {
7940            KeyOrIndexDisplay::Index
7941        } else {
7942            KeyOrIndexDisplay::None
7943        }
7944    }
7945
7946    pub fn parse_optional_index_option(&mut self) -> Result<Option<IndexOption>, ParserError> {
7947        if let Some(index_type) = self.parse_optional_using_then_index_type()? {
7948            Ok(Some(IndexOption::Using(index_type)))
7949        } else if self.parse_keyword(Keyword::COMMENT) {
7950            let s = self.parse_literal_string()?;
7951            Ok(Some(IndexOption::Comment(s)))
7952        } else {
7953            Ok(None)
7954        }
7955    }
7956
7957    pub fn parse_index_options(&mut self) -> Result<Vec<IndexOption>, ParserError> {
7958        let mut options = Vec::new();
7959
7960        loop {
7961            match self.parse_optional_index_option()? {
7962                Some(index_option) => options.push(index_option),
7963                None => return Ok(options),
7964            }
7965        }
7966    }
7967
7968    pub fn parse_sql_option(&mut self) -> Result<SqlOption, ParserError> {
7969        let is_mssql = dialect_of!(self is MsSqlDialect|GenericDialect);
7970
7971        match self.peek_token().token {
7972            Token::Word(w) if w.keyword == Keyword::HEAP && is_mssql => {
7973                Ok(SqlOption::Ident(self.parse_identifier()?))
7974            }
7975            Token::Word(w) if w.keyword == Keyword::PARTITION && is_mssql => {
7976                self.parse_option_partition()
7977            }
7978            Token::Word(w) if w.keyword == Keyword::CLUSTERED && is_mssql => {
7979                self.parse_option_clustered()
7980            }
7981            _ => {
7982                let name = self.parse_identifier()?;
7983                self.expect_token(&Token::Eq)?;
7984                let value = self.parse_expr()?;
7985
7986                Ok(SqlOption::KeyValue { key: name, value })
7987            }
7988        }
7989    }
7990
7991    pub fn parse_option_clustered(&mut self) -> Result<SqlOption, ParserError> {
7992        if self.parse_keywords(&[
7993            Keyword::CLUSTERED,
7994            Keyword::COLUMNSTORE,
7995            Keyword::INDEX,
7996            Keyword::ORDER,
7997        ]) {
7998            Ok(SqlOption::Clustered(
7999                TableOptionsClustered::ColumnstoreIndexOrder(
8000                    self.parse_parenthesized_column_list(IsOptional::Mandatory, false)?,
8001                ),
8002            ))
8003        } else if self.parse_keywords(&[Keyword::CLUSTERED, Keyword::COLUMNSTORE, Keyword::INDEX]) {
8004            Ok(SqlOption::Clustered(
8005                TableOptionsClustered::ColumnstoreIndex,
8006            ))
8007        } else if self.parse_keywords(&[Keyword::CLUSTERED, Keyword::INDEX]) {
8008            self.expect_token(&Token::LParen)?;
8009
8010            let columns = self.parse_comma_separated(|p| {
8011                let name = p.parse_identifier()?;
8012                let asc = p.parse_asc_desc();
8013
8014                Ok(ClusteredIndex { name, asc })
8015            })?;
8016
8017            self.expect_token(&Token::RParen)?;
8018
8019            Ok(SqlOption::Clustered(TableOptionsClustered::Index(columns)))
8020        } else {
8021            Err(ParserError::ParserError(
8022                "invalid CLUSTERED sequence".to_string(),
8023            ))
8024        }
8025    }
8026
8027    pub fn parse_option_partition(&mut self) -> Result<SqlOption, ParserError> {
8028        self.expect_keyword_is(Keyword::PARTITION)?;
8029        self.expect_token(&Token::LParen)?;
8030        let column_name = self.parse_identifier()?;
8031
8032        self.expect_keyword_is(Keyword::RANGE)?;
8033        let range_direction = if self.parse_keyword(Keyword::LEFT) {
8034            Some(PartitionRangeDirection::Left)
8035        } else if self.parse_keyword(Keyword::RIGHT) {
8036            Some(PartitionRangeDirection::Right)
8037        } else {
8038            None
8039        };
8040
8041        self.expect_keywords(&[Keyword::FOR, Keyword::VALUES])?;
8042        self.expect_token(&Token::LParen)?;
8043
8044        let for_values = self.parse_comma_separated(Parser::parse_expr)?;
8045
8046        self.expect_token(&Token::RParen)?;
8047        self.expect_token(&Token::RParen)?;
8048
8049        Ok(SqlOption::Partition {
8050            column_name,
8051            range_direction,
8052            for_values,
8053        })
8054    }
8055
8056    pub fn parse_partition(&mut self) -> Result<Partition, ParserError> {
8057        self.expect_token(&Token::LParen)?;
8058        let partitions = self.parse_comma_separated(Parser::parse_expr)?;
8059        self.expect_token(&Token::RParen)?;
8060        Ok(Partition::Partitions(partitions))
8061    }
8062
8063    pub fn parse_projection_select(&mut self) -> Result<ProjectionSelect, ParserError> {
8064        self.expect_token(&Token::LParen)?;
8065        self.expect_keyword_is(Keyword::SELECT)?;
8066        let projection = self.parse_projection()?;
8067        let group_by = self.parse_optional_group_by()?;
8068        let order_by = self.parse_optional_order_by()?;
8069        self.expect_token(&Token::RParen)?;
8070        Ok(ProjectionSelect {
8071            projection,
8072            group_by,
8073            order_by,
8074        })
8075    }
8076    pub fn parse_alter_table_add_projection(&mut self) -> Result<AlterTableOperation, ParserError> {
8077        let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
8078        let name = self.parse_identifier()?;
8079        let query = self.parse_projection_select()?;
8080        Ok(AlterTableOperation::AddProjection {
8081            if_not_exists,
8082            name,
8083            select: query,
8084        })
8085    }
8086
8087    pub fn parse_alter_table_operation(&mut self) -> Result<AlterTableOperation, ParserError> {
8088        let operation = if self.parse_keyword(Keyword::ADD) {
8089            if let Some(constraint) = self.parse_optional_table_constraint()? {
8090                AlterTableOperation::AddConstraint(constraint)
8091            } else if dialect_of!(self is ClickHouseDialect|GenericDialect)
8092                && self.parse_keyword(Keyword::PROJECTION)
8093            {
8094                return self.parse_alter_table_add_projection();
8095            } else {
8096                let if_not_exists =
8097                    self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
8098                let mut new_partitions = vec![];
8099                loop {
8100                    if self.parse_keyword(Keyword::PARTITION) {
8101                        new_partitions.push(self.parse_partition()?);
8102                    } else {
8103                        break;
8104                    }
8105                }
8106                if !new_partitions.is_empty() {
8107                    AlterTableOperation::AddPartitions {
8108                        if_not_exists,
8109                        new_partitions,
8110                    }
8111                } else {
8112                    let column_keyword = self.parse_keyword(Keyword::COLUMN);
8113
8114                    let if_not_exists = if dialect_of!(self is PostgreSqlDialect | BigQueryDialect | DuckDbDialect | GenericDialect)
8115                    {
8116                        self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS])
8117                            || if_not_exists
8118                    } else {
8119                        false
8120                    };
8121
8122                    let column_def = self.parse_column_def()?;
8123
8124                    let column_position = self.parse_column_position()?;
8125
8126                    AlterTableOperation::AddColumn {
8127                        column_keyword,
8128                        if_not_exists,
8129                        column_def,
8130                        column_position,
8131                    }
8132                }
8133            }
8134        } else if self.parse_keyword(Keyword::RENAME) {
8135            if dialect_of!(self is PostgreSqlDialect) && self.parse_keyword(Keyword::CONSTRAINT) {
8136                let old_name = self.parse_identifier()?;
8137                self.expect_keyword_is(Keyword::TO)?;
8138                let new_name = self.parse_identifier()?;
8139                AlterTableOperation::RenameConstraint { old_name, new_name }
8140            } else if self.parse_keyword(Keyword::TO) {
8141                let table_name = self.parse_object_name(false)?;
8142                AlterTableOperation::RenameTable { table_name }
8143            } else {
8144                let _ = self.parse_keyword(Keyword::COLUMN); // [ COLUMN ]
8145                let old_column_name = self.parse_identifier()?;
8146                self.expect_keyword_is(Keyword::TO)?;
8147                let new_column_name = self.parse_identifier()?;
8148                AlterTableOperation::RenameColumn {
8149                    old_column_name,
8150                    new_column_name,
8151                }
8152            }
8153        } else if self.parse_keyword(Keyword::DISABLE) {
8154            if self.parse_keywords(&[Keyword::ROW, Keyword::LEVEL, Keyword::SECURITY]) {
8155                AlterTableOperation::DisableRowLevelSecurity {}
8156            } else if self.parse_keyword(Keyword::RULE) {
8157                let name = self.parse_identifier()?;
8158                AlterTableOperation::DisableRule { name }
8159            } else if self.parse_keyword(Keyword::TRIGGER) {
8160                let name = self.parse_identifier()?;
8161                AlterTableOperation::DisableTrigger { name }
8162            } else {
8163                return self.expected(
8164                    "ROW LEVEL SECURITY, RULE, or TRIGGER after DISABLE",
8165                    self.peek_token(),
8166                );
8167            }
8168        } else if self.parse_keyword(Keyword::ENABLE) {
8169            if self.parse_keywords(&[Keyword::ALWAYS, Keyword::RULE]) {
8170                let name = self.parse_identifier()?;
8171                AlterTableOperation::EnableAlwaysRule { name }
8172            } else if self.parse_keywords(&[Keyword::ALWAYS, Keyword::TRIGGER]) {
8173                let name = self.parse_identifier()?;
8174                AlterTableOperation::EnableAlwaysTrigger { name }
8175            } else if self.parse_keywords(&[Keyword::ROW, Keyword::LEVEL, Keyword::SECURITY]) {
8176                AlterTableOperation::EnableRowLevelSecurity {}
8177            } else if self.parse_keywords(&[Keyword::REPLICA, Keyword::RULE]) {
8178                let name = self.parse_identifier()?;
8179                AlterTableOperation::EnableReplicaRule { name }
8180            } else if self.parse_keywords(&[Keyword::REPLICA, Keyword::TRIGGER]) {
8181                let name = self.parse_identifier()?;
8182                AlterTableOperation::EnableReplicaTrigger { name }
8183            } else if self.parse_keyword(Keyword::RULE) {
8184                let name = self.parse_identifier()?;
8185                AlterTableOperation::EnableRule { name }
8186            } else if self.parse_keyword(Keyword::TRIGGER) {
8187                let name = self.parse_identifier()?;
8188                AlterTableOperation::EnableTrigger { name }
8189            } else {
8190                return self.expected(
8191                    "ALWAYS, REPLICA, ROW LEVEL SECURITY, RULE, or TRIGGER after ENABLE",
8192                    self.peek_token(),
8193                );
8194            }
8195        } else if self.parse_keywords(&[Keyword::CLEAR, Keyword::PROJECTION])
8196            && dialect_of!(self is ClickHouseDialect|GenericDialect)
8197        {
8198            let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
8199            let name = self.parse_identifier()?;
8200            let partition = if self.parse_keywords(&[Keyword::IN, Keyword::PARTITION]) {
8201                Some(self.parse_identifier()?)
8202            } else {
8203                None
8204            };
8205            AlterTableOperation::ClearProjection {
8206                if_exists,
8207                name,
8208                partition,
8209            }
8210        } else if self.parse_keywords(&[Keyword::MATERIALIZE, Keyword::PROJECTION])
8211            && dialect_of!(self is ClickHouseDialect|GenericDialect)
8212        {
8213            let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
8214            let name = self.parse_identifier()?;
8215            let partition = if self.parse_keywords(&[Keyword::IN, Keyword::PARTITION]) {
8216                Some(self.parse_identifier()?)
8217            } else {
8218                None
8219            };
8220            AlterTableOperation::MaterializeProjection {
8221                if_exists,
8222                name,
8223                partition,
8224            }
8225        } else if self.parse_keyword(Keyword::DROP) {
8226            if self.parse_keywords(&[Keyword::IF, Keyword::EXISTS, Keyword::PARTITION]) {
8227                self.expect_token(&Token::LParen)?;
8228                let partitions = self.parse_comma_separated(Parser::parse_expr)?;
8229                self.expect_token(&Token::RParen)?;
8230                AlterTableOperation::DropPartitions {
8231                    partitions,
8232                    if_exists: true,
8233                }
8234            } else if self.parse_keyword(Keyword::PARTITION) {
8235                self.expect_token(&Token::LParen)?;
8236                let partitions = self.parse_comma_separated(Parser::parse_expr)?;
8237                self.expect_token(&Token::RParen)?;
8238                AlterTableOperation::DropPartitions {
8239                    partitions,
8240                    if_exists: false,
8241                }
8242            } else if self.parse_keyword(Keyword::CONSTRAINT) {
8243                let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
8244                let name = self.parse_identifier()?;
8245                let drop_behavior = self.parse_optional_drop_behavior();
8246                AlterTableOperation::DropConstraint {
8247                    if_exists,
8248                    name,
8249                    drop_behavior,
8250                }
8251            } else if self.parse_keywords(&[Keyword::PRIMARY, Keyword::KEY]) {
8252                AlterTableOperation::DropPrimaryKey
8253            } else if self.parse_keywords(&[Keyword::FOREIGN, Keyword::KEY]) {
8254                let name = self.parse_identifier()?;
8255                AlterTableOperation::DropForeignKey { name }
8256            } else if self.parse_keyword(Keyword::PROJECTION)
8257                && dialect_of!(self is ClickHouseDialect|GenericDialect)
8258            {
8259                let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
8260                let name = self.parse_identifier()?;
8261                AlterTableOperation::DropProjection { if_exists, name }
8262            } else if self.parse_keywords(&[Keyword::CLUSTERING, Keyword::KEY]) {
8263                AlterTableOperation::DropClusteringKey
8264            } else {
8265                let _ = self.parse_keyword(Keyword::COLUMN); // [ COLUMN ]
8266                let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
8267                let column_name = self.parse_identifier()?;
8268                let drop_behavior = self.parse_optional_drop_behavior();
8269                AlterTableOperation::DropColumn {
8270                    column_name,
8271                    if_exists,
8272                    drop_behavior,
8273                }
8274            }
8275        } else if self.parse_keyword(Keyword::PARTITION) {
8276            self.expect_token(&Token::LParen)?;
8277            let before = self.parse_comma_separated(Parser::parse_expr)?;
8278            self.expect_token(&Token::RParen)?;
8279            self.expect_keyword_is(Keyword::RENAME)?;
8280            self.expect_keywords(&[Keyword::TO, Keyword::PARTITION])?;
8281            self.expect_token(&Token::LParen)?;
8282            let renames = self.parse_comma_separated(Parser::parse_expr)?;
8283            self.expect_token(&Token::RParen)?;
8284            AlterTableOperation::RenamePartitions {
8285                old_partitions: before,
8286                new_partitions: renames,
8287            }
8288        } else if self.parse_keyword(Keyword::CHANGE) {
8289            let _ = self.parse_keyword(Keyword::COLUMN); // [ COLUMN ]
8290            let old_name = self.parse_identifier()?;
8291            let new_name = self.parse_identifier()?;
8292            let data_type = self.parse_data_type()?;
8293            let mut options = vec![];
8294            while let Some(option) = self.parse_optional_column_option()? {
8295                options.push(option);
8296            }
8297
8298            let column_position = self.parse_column_position()?;
8299
8300            AlterTableOperation::ChangeColumn {
8301                old_name,
8302                new_name,
8303                data_type,
8304                options,
8305                column_position,
8306            }
8307        } else if self.parse_keyword(Keyword::MODIFY) {
8308            let _ = self.parse_keyword(Keyword::COLUMN); // [ COLUMN ]
8309            let col_name = self.parse_identifier()?;
8310            let data_type = self.parse_data_type()?;
8311            let mut options = vec![];
8312            while let Some(option) = self.parse_optional_column_option()? {
8313                options.push(option);
8314            }
8315
8316            let column_position = self.parse_column_position()?;
8317
8318            AlterTableOperation::ModifyColumn {
8319                col_name,
8320                data_type,
8321                options,
8322                column_position,
8323            }
8324        } else if self.parse_keyword(Keyword::ALTER) {
8325            let _ = self.parse_keyword(Keyword::COLUMN); // [ COLUMN ]
8326            let column_name = self.parse_identifier()?;
8327            let is_postgresql = dialect_of!(self is PostgreSqlDialect);
8328
8329            let op: AlterColumnOperation = if self.parse_keywords(&[
8330                Keyword::SET,
8331                Keyword::NOT,
8332                Keyword::NULL,
8333            ]) {
8334                AlterColumnOperation::SetNotNull {}
8335            } else if self.parse_keywords(&[Keyword::DROP, Keyword::NOT, Keyword::NULL]) {
8336                AlterColumnOperation::DropNotNull {}
8337            } else if self.parse_keywords(&[Keyword::SET, Keyword::DEFAULT]) {
8338                AlterColumnOperation::SetDefault {
8339                    value: self.parse_expr()?,
8340                }
8341            } else if self.parse_keywords(&[Keyword::DROP, Keyword::DEFAULT]) {
8342                AlterColumnOperation::DropDefault {}
8343            } else if self.parse_keywords(&[Keyword::SET, Keyword::DATA, Keyword::TYPE])
8344                || (is_postgresql && self.parse_keyword(Keyword::TYPE))
8345            {
8346                let data_type = self.parse_data_type()?;
8347                let using = if is_postgresql && self.parse_keyword(Keyword::USING) {
8348                    Some(self.parse_expr()?)
8349                } else {
8350                    None
8351                };
8352                AlterColumnOperation::SetDataType { data_type, using }
8353            } else if self.parse_keywords(&[Keyword::ADD, Keyword::GENERATED]) {
8354                let generated_as = if self.parse_keyword(Keyword::ALWAYS) {
8355                    Some(GeneratedAs::Always)
8356                } else if self.parse_keywords(&[Keyword::BY, Keyword::DEFAULT]) {
8357                    Some(GeneratedAs::ByDefault)
8358                } else {
8359                    None
8360                };
8361
8362                self.expect_keywords(&[Keyword::AS, Keyword::IDENTITY])?;
8363
8364                let mut sequence_options: Option<Vec<SequenceOptions>> = None;
8365
8366                if self.peek_token().token == Token::LParen {
8367                    self.expect_token(&Token::LParen)?;
8368                    sequence_options = Some(self.parse_create_sequence_options()?);
8369                    self.expect_token(&Token::RParen)?;
8370                }
8371
8372                AlterColumnOperation::AddGenerated {
8373                    generated_as,
8374                    sequence_options,
8375                }
8376            } else {
8377                let message = if is_postgresql {
8378                    "SET/DROP NOT NULL, SET DEFAULT, SET DATA TYPE, or ADD GENERATED after ALTER COLUMN"
8379                } else {
8380                    "SET/DROP NOT NULL, SET DEFAULT, or SET DATA TYPE after ALTER COLUMN"
8381                };
8382
8383                return self.expected(message, self.peek_token());
8384            };
8385            AlterTableOperation::AlterColumn { column_name, op }
8386        } else if self.parse_keyword(Keyword::SWAP) {
8387            self.expect_keyword_is(Keyword::WITH)?;
8388            let table_name = self.parse_object_name(false)?;
8389            AlterTableOperation::SwapWith { table_name }
8390        } else if dialect_of!(self is PostgreSqlDialect | GenericDialect)
8391            && self.parse_keywords(&[Keyword::OWNER, Keyword::TO])
8392        {
8393            let new_owner = self.parse_owner()?;
8394            AlterTableOperation::OwnerTo { new_owner }
8395        } else if dialect_of!(self is ClickHouseDialect|GenericDialect)
8396            && self.parse_keyword(Keyword::ATTACH)
8397        {
8398            AlterTableOperation::AttachPartition {
8399                partition: self.parse_part_or_partition()?,
8400            }
8401        } else if dialect_of!(self is ClickHouseDialect|GenericDialect)
8402            && self.parse_keyword(Keyword::DETACH)
8403        {
8404            AlterTableOperation::DetachPartition {
8405                partition: self.parse_part_or_partition()?,
8406            }
8407        } else if dialect_of!(self is ClickHouseDialect|GenericDialect)
8408            && self.parse_keyword(Keyword::FREEZE)
8409        {
8410            let partition = self.parse_part_or_partition()?;
8411            let with_name = if self.parse_keyword(Keyword::WITH) {
8412                self.expect_keyword_is(Keyword::NAME)?;
8413                Some(self.parse_identifier()?)
8414            } else {
8415                None
8416            };
8417            AlterTableOperation::FreezePartition {
8418                partition,
8419                with_name,
8420            }
8421        } else if dialect_of!(self is ClickHouseDialect|GenericDialect)
8422            && self.parse_keyword(Keyword::UNFREEZE)
8423        {
8424            let partition = self.parse_part_or_partition()?;
8425            let with_name = if self.parse_keyword(Keyword::WITH) {
8426                self.expect_keyword_is(Keyword::NAME)?;
8427                Some(self.parse_identifier()?)
8428            } else {
8429                None
8430            };
8431            AlterTableOperation::UnfreezePartition {
8432                partition,
8433                with_name,
8434            }
8435        } else if self.parse_keywords(&[Keyword::CLUSTER, Keyword::BY]) {
8436            self.expect_token(&Token::LParen)?;
8437            let exprs = self.parse_comma_separated(|parser| parser.parse_expr())?;
8438            self.expect_token(&Token::RParen)?;
8439            AlterTableOperation::ClusterBy { exprs }
8440        } else if self.parse_keywords(&[Keyword::SUSPEND, Keyword::RECLUSTER]) {
8441            AlterTableOperation::SuspendRecluster
8442        } else if self.parse_keywords(&[Keyword::RESUME, Keyword::RECLUSTER]) {
8443            AlterTableOperation::ResumeRecluster
8444        } else if self.parse_keyword(Keyword::LOCK) {
8445            let equals = self.consume_token(&Token::Eq);
8446            let lock = match self.parse_one_of_keywords(&[
8447                Keyword::DEFAULT,
8448                Keyword::EXCLUSIVE,
8449                Keyword::NONE,
8450                Keyword::SHARED,
8451            ]) {
8452                Some(Keyword::DEFAULT) => AlterTableLock::Default,
8453                Some(Keyword::EXCLUSIVE) => AlterTableLock::Exclusive,
8454                Some(Keyword::NONE) => AlterTableLock::None,
8455                Some(Keyword::SHARED) => AlterTableLock::Shared,
8456                _ => self.expected(
8457                    "DEFAULT, EXCLUSIVE, NONE or SHARED after LOCK [=]",
8458                    self.peek_token(),
8459                )?,
8460            };
8461            AlterTableOperation::Lock { equals, lock }
8462        } else if self.parse_keyword(Keyword::ALGORITHM) {
8463            let equals = self.consume_token(&Token::Eq);
8464            let algorithm = match self.parse_one_of_keywords(&[
8465                Keyword::DEFAULT,
8466                Keyword::INSTANT,
8467                Keyword::INPLACE,
8468                Keyword::COPY,
8469            ]) {
8470                Some(Keyword::DEFAULT) => AlterTableAlgorithm::Default,
8471                Some(Keyword::INSTANT) => AlterTableAlgorithm::Instant,
8472                Some(Keyword::INPLACE) => AlterTableAlgorithm::Inplace,
8473                Some(Keyword::COPY) => AlterTableAlgorithm::Copy,
8474                _ => self.expected(
8475                    "DEFAULT, INSTANT, INPLACE, or COPY after ALGORITHM [=]",
8476                    self.peek_token(),
8477                )?,
8478            };
8479            AlterTableOperation::Algorithm { equals, algorithm }
8480        } else if self.parse_keyword(Keyword::AUTO_INCREMENT) {
8481            let equals = self.consume_token(&Token::Eq);
8482            let value = self.parse_number_value()?;
8483            AlterTableOperation::AutoIncrement { equals, value }
8484        } else {
8485            let options: Vec<SqlOption> =
8486                self.parse_options_with_keywords(&[Keyword::SET, Keyword::TBLPROPERTIES])?;
8487            if !options.is_empty() {
8488                AlterTableOperation::SetTblProperties {
8489                    table_properties: options,
8490                }
8491            } else {
8492                return self.expected(
8493                    "ADD, RENAME, PARTITION, SWAP, DROP, or SET TBLPROPERTIES after ALTER TABLE",
8494                    self.peek_token(),
8495                );
8496            }
8497        };
8498        Ok(operation)
8499    }
8500
8501    fn parse_part_or_partition(&mut self) -> Result<Partition, ParserError> {
8502        let keyword = self.expect_one_of_keywords(&[Keyword::PART, Keyword::PARTITION])?;
8503        match keyword {
8504            Keyword::PART => Ok(Partition::Part(self.parse_expr()?)),
8505            Keyword::PARTITION => Ok(Partition::Expr(self.parse_expr()?)),
8506            // unreachable because expect_one_of_keywords used above
8507            _ => unreachable!(),
8508        }
8509    }
8510
8511    pub fn parse_alter(&mut self) -> Result<Statement, ParserError> {
8512        let object_type = self.expect_one_of_keywords(&[
8513            Keyword::VIEW,
8514            Keyword::TYPE,
8515            Keyword::TABLE,
8516            Keyword::INDEX,
8517            Keyword::ROLE,
8518            Keyword::POLICY,
8519            Keyword::CONNECTOR,
8520        ])?;
8521        match object_type {
8522            Keyword::VIEW => self.parse_alter_view(),
8523            Keyword::TYPE => self.parse_alter_type(),
8524            Keyword::TABLE => {
8525                let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
8526                let only = self.parse_keyword(Keyword::ONLY); // [ ONLY ]
8527                let table_name = self.parse_object_name(false)?;
8528                let on_cluster = self.parse_optional_on_cluster()?;
8529                let operations = self.parse_comma_separated(Parser::parse_alter_table_operation)?;
8530
8531                let mut location = None;
8532                if self.parse_keyword(Keyword::LOCATION) {
8533                    location = Some(HiveSetLocation {
8534                        has_set: false,
8535                        location: self.parse_identifier()?,
8536                    });
8537                } else if self.parse_keywords(&[Keyword::SET, Keyword::LOCATION]) {
8538                    location = Some(HiveSetLocation {
8539                        has_set: true,
8540                        location: self.parse_identifier()?,
8541                    });
8542                }
8543
8544                Ok(Statement::AlterTable {
8545                    name: table_name,
8546                    if_exists,
8547                    only,
8548                    operations,
8549                    location,
8550                    on_cluster,
8551                })
8552            }
8553            Keyword::INDEX => {
8554                let index_name = self.parse_object_name(false)?;
8555                let operation = if self.parse_keyword(Keyword::RENAME) {
8556                    if self.parse_keyword(Keyword::TO) {
8557                        let index_name = self.parse_object_name(false)?;
8558                        AlterIndexOperation::RenameIndex { index_name }
8559                    } else {
8560                        return self.expected("TO after RENAME", self.peek_token());
8561                    }
8562                } else {
8563                    return self.expected("RENAME after ALTER INDEX", self.peek_token());
8564                };
8565
8566                Ok(Statement::AlterIndex {
8567                    name: index_name,
8568                    operation,
8569                })
8570            }
8571            Keyword::ROLE => self.parse_alter_role(),
8572            Keyword::POLICY => self.parse_alter_policy(),
8573            Keyword::CONNECTOR => self.parse_alter_connector(),
8574            // unreachable because expect_one_of_keywords used above
8575            _ => unreachable!(),
8576        }
8577    }
8578
8579    pub fn parse_alter_view(&mut self) -> Result<Statement, ParserError> {
8580        let name = self.parse_object_name(false)?;
8581        let columns = self.parse_parenthesized_column_list(Optional, false)?;
8582
8583        let with_options = self.parse_options(Keyword::WITH)?;
8584
8585        self.expect_keyword_is(Keyword::AS)?;
8586        let query = self.parse_query()?;
8587
8588        Ok(Statement::AlterView {
8589            name,
8590            columns,
8591            query,
8592            with_options,
8593        })
8594    }
8595
8596    /// Parse a [Statement::AlterType]
8597    pub fn parse_alter_type(&mut self) -> Result<Statement, ParserError> {
8598        let name = self.parse_object_name(false)?;
8599
8600        if self.parse_keywords(&[Keyword::RENAME, Keyword::TO]) {
8601            let new_name = self.parse_identifier()?;
8602            Ok(Statement::AlterType(AlterType {
8603                name,
8604                operation: AlterTypeOperation::Rename(AlterTypeRename { new_name }),
8605            }))
8606        } else if self.parse_keywords(&[Keyword::ADD, Keyword::VALUE]) {
8607            let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
8608            let new_enum_value = self.parse_identifier()?;
8609            let position = if self.parse_keyword(Keyword::BEFORE) {
8610                Some(AlterTypeAddValuePosition::Before(self.parse_identifier()?))
8611            } else if self.parse_keyword(Keyword::AFTER) {
8612                Some(AlterTypeAddValuePosition::After(self.parse_identifier()?))
8613            } else {
8614                None
8615            };
8616
8617            Ok(Statement::AlterType(AlterType {
8618                name,
8619                operation: AlterTypeOperation::AddValue(AlterTypeAddValue {
8620                    if_not_exists,
8621                    value: new_enum_value,
8622                    position,
8623                }),
8624            }))
8625        } else if self.parse_keywords(&[Keyword::RENAME, Keyword::VALUE]) {
8626            let existing_enum_value = self.parse_identifier()?;
8627            self.expect_keyword(Keyword::TO)?;
8628            let new_enum_value = self.parse_identifier()?;
8629
8630            Ok(Statement::AlterType(AlterType {
8631                name,
8632                operation: AlterTypeOperation::RenameValue(AlterTypeRenameValue {
8633                    from: existing_enum_value,
8634                    to: new_enum_value,
8635                }),
8636            }))
8637        } else {
8638            return self.expected_ref(
8639                "{RENAME TO | { RENAME | ADD } VALUE}",
8640                self.peek_token_ref(),
8641            );
8642        }
8643    }
8644
8645    /// Parse a `CALL procedure_name(arg1, arg2, ...)`
8646    /// or `CALL procedure_name` statement
8647    pub fn parse_call(&mut self) -> Result<Statement, ParserError> {
8648        let object_name = self.parse_object_name(false)?;
8649        if self.peek_token().token == Token::LParen {
8650            match self.parse_function(object_name)? {
8651                Expr::Function(f) => Ok(Statement::Call(f)),
8652                other => parser_err!(
8653                    format!("Expected a simple procedure call but found: {other}"),
8654                    self.peek_token().span.start
8655                ),
8656            }
8657        } else {
8658            Ok(Statement::Call(Function {
8659                name: object_name,
8660                uses_odbc_syntax: false,
8661                parameters: FunctionArguments::None,
8662                args: FunctionArguments::None,
8663                over: None,
8664                filter: None,
8665                null_treatment: None,
8666                within_group: vec![],
8667            }))
8668        }
8669    }
8670
8671    /// Parse a copy statement
8672    pub fn parse_copy(&mut self) -> Result<Statement, ParserError> {
8673        let source;
8674        if self.consume_token(&Token::LParen) {
8675            source = CopySource::Query(self.parse_query()?);
8676            self.expect_token(&Token::RParen)?;
8677        } else {
8678            let table_name = self.parse_object_name(false)?;
8679            let columns = self.parse_parenthesized_column_list(Optional, false)?;
8680            source = CopySource::Table {
8681                table_name,
8682                columns,
8683            };
8684        }
8685        let to = match self.parse_one_of_keywords(&[Keyword::FROM, Keyword::TO]) {
8686            Some(Keyword::FROM) => false,
8687            Some(Keyword::TO) => true,
8688            _ => self.expected("FROM or TO", self.peek_token())?,
8689        };
8690        if !to {
8691            // Use a separate if statement to prevent Rust compiler from complaining about
8692            // "if statement in this position is unstable: https://github.com/rust-lang/rust/issues/53667"
8693            if let CopySource::Query(_) = source {
8694                return Err(ParserError::ParserError(
8695                    "COPY ... FROM does not support query as a source".to_string(),
8696                ));
8697            }
8698        }
8699        let target = if self.parse_keyword(Keyword::STDIN) {
8700            CopyTarget::Stdin
8701        } else if self.parse_keyword(Keyword::STDOUT) {
8702            CopyTarget::Stdout
8703        } else if self.parse_keyword(Keyword::PROGRAM) {
8704            CopyTarget::Program {
8705                command: self.parse_literal_string()?,
8706            }
8707        } else {
8708            CopyTarget::File {
8709                filename: self.parse_literal_string()?,
8710            }
8711        };
8712        let _ = self.parse_keyword(Keyword::WITH); // [ WITH ]
8713        let mut options = vec![];
8714        if self.consume_token(&Token::LParen) {
8715            options = self.parse_comma_separated(Parser::parse_copy_option)?;
8716            self.expect_token(&Token::RParen)?;
8717        }
8718        let mut legacy_options = vec![];
8719        while let Some(opt) = self.maybe_parse(|parser| parser.parse_copy_legacy_option())? {
8720            legacy_options.push(opt);
8721        }
8722        let values = if let CopyTarget::Stdin = target {
8723            self.expect_token(&Token::SemiColon)?;
8724            self.parse_tsv()
8725        } else {
8726            vec![]
8727        };
8728        Ok(Statement::Copy {
8729            source,
8730            to,
8731            target,
8732            options,
8733            legacy_options,
8734            values,
8735        })
8736    }
8737
8738    pub fn parse_close(&mut self) -> Result<Statement, ParserError> {
8739        let cursor = if self.parse_keyword(Keyword::ALL) {
8740            CloseCursor::All
8741        } else {
8742            let name = self.parse_identifier()?;
8743
8744            CloseCursor::Specific { name }
8745        };
8746
8747        Ok(Statement::Close { cursor })
8748    }
8749
8750    fn parse_copy_option(&mut self) -> Result<CopyOption, ParserError> {
8751        let ret = match self.parse_one_of_keywords(&[
8752            Keyword::FORMAT,
8753            Keyword::FREEZE,
8754            Keyword::DELIMITER,
8755            Keyword::NULL,
8756            Keyword::HEADER,
8757            Keyword::QUOTE,
8758            Keyword::ESCAPE,
8759            Keyword::FORCE_QUOTE,
8760            Keyword::FORCE_NOT_NULL,
8761            Keyword::FORCE_NULL,
8762            Keyword::ENCODING,
8763        ]) {
8764            Some(Keyword::FORMAT) => CopyOption::Format(self.parse_identifier()?),
8765            Some(Keyword::FREEZE) => CopyOption::Freeze(!matches!(
8766                self.parse_one_of_keywords(&[Keyword::TRUE, Keyword::FALSE]),
8767                Some(Keyword::FALSE)
8768            )),
8769            Some(Keyword::DELIMITER) => CopyOption::Delimiter(self.parse_literal_char()?),
8770            Some(Keyword::NULL) => CopyOption::Null(self.parse_literal_string()?),
8771            Some(Keyword::HEADER) => CopyOption::Header(!matches!(
8772                self.parse_one_of_keywords(&[Keyword::TRUE, Keyword::FALSE]),
8773                Some(Keyword::FALSE)
8774            )),
8775            Some(Keyword::QUOTE) => CopyOption::Quote(self.parse_literal_char()?),
8776            Some(Keyword::ESCAPE) => CopyOption::Escape(self.parse_literal_char()?),
8777            Some(Keyword::FORCE_QUOTE) => {
8778                CopyOption::ForceQuote(self.parse_parenthesized_column_list(Mandatory, false)?)
8779            }
8780            Some(Keyword::FORCE_NOT_NULL) => {
8781                CopyOption::ForceNotNull(self.parse_parenthesized_column_list(Mandatory, false)?)
8782            }
8783            Some(Keyword::FORCE_NULL) => {
8784                CopyOption::ForceNull(self.parse_parenthesized_column_list(Mandatory, false)?)
8785            }
8786            Some(Keyword::ENCODING) => CopyOption::Encoding(self.parse_literal_string()?),
8787            _ => self.expected("option", self.peek_token())?,
8788        };
8789        Ok(ret)
8790    }
8791
8792    fn parse_copy_legacy_option(&mut self) -> Result<CopyLegacyOption, ParserError> {
8793        let ret = match self.parse_one_of_keywords(&[
8794            Keyword::BINARY,
8795            Keyword::DELIMITER,
8796            Keyword::NULL,
8797            Keyword::CSV,
8798        ]) {
8799            Some(Keyword::BINARY) => CopyLegacyOption::Binary,
8800            Some(Keyword::DELIMITER) => {
8801                let _ = self.parse_keyword(Keyword::AS); // [ AS ]
8802                CopyLegacyOption::Delimiter(self.parse_literal_char()?)
8803            }
8804            Some(Keyword::NULL) => {
8805                let _ = self.parse_keyword(Keyword::AS); // [ AS ]
8806                CopyLegacyOption::Null(self.parse_literal_string()?)
8807            }
8808            Some(Keyword::CSV) => CopyLegacyOption::Csv({
8809                let mut opts = vec![];
8810                while let Some(opt) =
8811                    self.maybe_parse(|parser| parser.parse_copy_legacy_csv_option())?
8812                {
8813                    opts.push(opt);
8814                }
8815                opts
8816            }),
8817            _ => self.expected("option", self.peek_token())?,
8818        };
8819        Ok(ret)
8820    }
8821
8822    fn parse_copy_legacy_csv_option(&mut self) -> Result<CopyLegacyCsvOption, ParserError> {
8823        let ret = match self.parse_one_of_keywords(&[
8824            Keyword::HEADER,
8825            Keyword::QUOTE,
8826            Keyword::ESCAPE,
8827            Keyword::FORCE,
8828        ]) {
8829            Some(Keyword::HEADER) => CopyLegacyCsvOption::Header,
8830            Some(Keyword::QUOTE) => {
8831                let _ = self.parse_keyword(Keyword::AS); // [ AS ]
8832                CopyLegacyCsvOption::Quote(self.parse_literal_char()?)
8833            }
8834            Some(Keyword::ESCAPE) => {
8835                let _ = self.parse_keyword(Keyword::AS); // [ AS ]
8836                CopyLegacyCsvOption::Escape(self.parse_literal_char()?)
8837            }
8838            Some(Keyword::FORCE) if self.parse_keywords(&[Keyword::NOT, Keyword::NULL]) => {
8839                CopyLegacyCsvOption::ForceNotNull(
8840                    self.parse_comma_separated(|p| p.parse_identifier())?,
8841                )
8842            }
8843            Some(Keyword::FORCE) if self.parse_keywords(&[Keyword::QUOTE]) => {
8844                CopyLegacyCsvOption::ForceQuote(
8845                    self.parse_comma_separated(|p| p.parse_identifier())?,
8846                )
8847            }
8848            _ => self.expected("csv option", self.peek_token())?,
8849        };
8850        Ok(ret)
8851    }
8852
8853    fn parse_literal_char(&mut self) -> Result<char, ParserError> {
8854        let s = self.parse_literal_string()?;
8855        if s.len() != 1 {
8856            let loc = self
8857                .tokens
8858                .get(self.index - 1)
8859                .map_or(Location { line: 0, column: 0 }, |t| t.span.start);
8860            return parser_err!(format!("Expect a char, found {s:?}"), loc);
8861        }
8862        Ok(s.chars().next().unwrap())
8863    }
8864
8865    /// Parse a tab separated values in
8866    /// COPY payload
8867    pub fn parse_tsv(&mut self) -> Vec<Option<String>> {
8868        self.parse_tab_value()
8869    }
8870
8871    pub fn parse_tab_value(&mut self) -> Vec<Option<String>> {
8872        let mut values = vec![];
8873        let mut content = String::from("");
8874        while let Some(t) = self.next_token_no_skip().map(|t| &t.token) {
8875            match t {
8876                Token::Whitespace(Whitespace::Tab) => {
8877                    values.push(Some(content.to_string()));
8878                    content.clear();
8879                }
8880                Token::Whitespace(Whitespace::Newline) => {
8881                    values.push(Some(content.to_string()));
8882                    content.clear();
8883                }
8884                Token::Backslash => {
8885                    if self.consume_token(&Token::Period) {
8886                        return values;
8887                    }
8888                    if let Token::Word(w) = self.next_token().token {
8889                        if w.value == "N" {
8890                            values.push(None);
8891                        }
8892                    }
8893                }
8894                _ => {
8895                    content.push_str(&t.to_string());
8896                }
8897            }
8898        }
8899        values
8900    }
8901
8902    /// Parse a literal value (numbers, strings, date/time, booleans)
8903    pub fn parse_value(&mut self) -> Result<ValueWithSpan, ParserError> {
8904        let next_token = self.next_token();
8905        let span = next_token.span;
8906        let ok_value = |value: Value| Ok(value.with_span(span));
8907        match next_token.token {
8908            Token::Word(w) => match w.keyword {
8909                Keyword::TRUE if self.dialect.supports_boolean_literals() => {
8910                    ok_value(Value::Boolean(true))
8911                }
8912                Keyword::FALSE if self.dialect.supports_boolean_literals() => {
8913                    ok_value(Value::Boolean(false))
8914                }
8915                Keyword::NULL => ok_value(Value::Null),
8916                Keyword::NoKeyword if w.quote_style.is_some() => match w.quote_style {
8917                    Some('"') => ok_value(Value::DoubleQuotedString(w.value)),
8918                    Some('\'') => ok_value(Value::SingleQuotedString(w.value)),
8919                    _ => self.expected(
8920                        "A value?",
8921                        TokenWithSpan {
8922                            token: Token::Word(w),
8923                            span,
8924                        },
8925                    )?,
8926                },
8927                _ => self.expected(
8928                    "a concrete value",
8929                    TokenWithSpan {
8930                        token: Token::Word(w),
8931                        span,
8932                    },
8933                ),
8934            },
8935            // The call to n.parse() returns a bigdecimal when the
8936            // bigdecimal feature is enabled, and is otherwise a no-op
8937            // (i.e., it returns the input string).
8938            Token::Number(n, l) => ok_value(Value::Number(Self::parse(n, span.start)?, l)),
8939            Token::SingleQuotedString(ref s) => ok_value(Value::SingleQuotedString(s.to_string())),
8940            Token::DoubleQuotedString(ref s) => ok_value(Value::DoubleQuotedString(s.to_string())),
8941            Token::TripleSingleQuotedString(ref s) => {
8942                ok_value(Value::TripleSingleQuotedString(s.to_string()))
8943            }
8944            Token::TripleDoubleQuotedString(ref s) => {
8945                ok_value(Value::TripleDoubleQuotedString(s.to_string()))
8946            }
8947            Token::DollarQuotedString(ref s) => ok_value(Value::DollarQuotedString(s.clone())),
8948            Token::SingleQuotedByteStringLiteral(ref s) => {
8949                ok_value(Value::SingleQuotedByteStringLiteral(s.clone()))
8950            }
8951            Token::DoubleQuotedByteStringLiteral(ref s) => {
8952                ok_value(Value::DoubleQuotedByteStringLiteral(s.clone()))
8953            }
8954            Token::TripleSingleQuotedByteStringLiteral(ref s) => {
8955                ok_value(Value::TripleSingleQuotedByteStringLiteral(s.clone()))
8956            }
8957            Token::TripleDoubleQuotedByteStringLiteral(ref s) => {
8958                ok_value(Value::TripleDoubleQuotedByteStringLiteral(s.clone()))
8959            }
8960            Token::SingleQuotedRawStringLiteral(ref s) => {
8961                ok_value(Value::SingleQuotedRawStringLiteral(s.clone()))
8962            }
8963            Token::DoubleQuotedRawStringLiteral(ref s) => {
8964                ok_value(Value::DoubleQuotedRawStringLiteral(s.clone()))
8965            }
8966            Token::TripleSingleQuotedRawStringLiteral(ref s) => {
8967                ok_value(Value::TripleSingleQuotedRawStringLiteral(s.clone()))
8968            }
8969            Token::TripleDoubleQuotedRawStringLiteral(ref s) => {
8970                ok_value(Value::TripleDoubleQuotedRawStringLiteral(s.clone()))
8971            }
8972            Token::NationalStringLiteral(ref s) => {
8973                ok_value(Value::NationalStringLiteral(s.to_string()))
8974            }
8975            Token::EscapedStringLiteral(ref s) => {
8976                ok_value(Value::EscapedStringLiteral(s.to_string()))
8977            }
8978            Token::UnicodeStringLiteral(ref s) => {
8979                ok_value(Value::UnicodeStringLiteral(s.to_string()))
8980            }
8981            Token::HexStringLiteral(ref s) => ok_value(Value::HexStringLiteral(s.to_string())),
8982            Token::Placeholder(ref s) => ok_value(Value::Placeholder(s.to_string())),
8983            tok @ Token::Colon | tok @ Token::AtSign => {
8984                // Not calling self.parse_identifier(false)? because only in placeholder we want to check numbers as idfentifies
8985                // This because snowflake allows numbers as placeholders
8986                let next_token = self.next_token();
8987                let ident = match next_token.token {
8988                    Token::Word(w) => Ok(w.into_ident(next_token.span)),
8989                    Token::Number(w, false) => Ok(Ident::new(w)),
8990                    _ => self.expected("placeholder", next_token),
8991                }?;
8992                let placeholder = tok.to_string() + &ident.value;
8993                ok_value(Value::Placeholder(placeholder))
8994            }
8995            unexpected => self.expected(
8996                "a value",
8997                TokenWithSpan {
8998                    token: unexpected,
8999                    span,
9000                },
9001            ),
9002        }
9003    }
9004
9005    /// Parse an unsigned numeric literal
9006    pub fn parse_number_value(&mut self) -> Result<ValueWithSpan, ParserError> {
9007        let value_wrapper = self.parse_value()?;
9008        match &value_wrapper.value {
9009            Value::Number(_, _) => Ok(value_wrapper),
9010            Value::Placeholder(_) => Ok(value_wrapper),
9011            _ => {
9012                self.prev_token();
9013                self.expected("literal number", self.peek_token())
9014            }
9015        }
9016    }
9017
9018    /// Parse a numeric literal as an expression. Returns a [`Expr::UnaryOp`] if the number is signed,
9019    /// otherwise returns a [`Expr::Value`]
9020    pub fn parse_number(&mut self) -> Result<Expr, ParserError> {
9021        let next_token = self.next_token();
9022        match next_token.token {
9023            Token::Plus => Ok(Expr::UnaryOp {
9024                op: UnaryOperator::Plus,
9025                expr: Box::new(Expr::Value(self.parse_number_value()?)),
9026            }),
9027            Token::Minus => Ok(Expr::UnaryOp {
9028                op: UnaryOperator::Minus,
9029                expr: Box::new(Expr::Value(self.parse_number_value()?)),
9030            }),
9031            _ => {
9032                self.prev_token();
9033                Ok(Expr::Value(self.parse_number_value()?))
9034            }
9035        }
9036    }
9037
9038    fn parse_introduced_string_expr(&mut self) -> Result<Expr, ParserError> {
9039        let next_token = self.next_token();
9040        let span = next_token.span;
9041        match next_token.token {
9042            Token::SingleQuotedString(ref s) => Ok(Expr::Value(
9043                Value::SingleQuotedString(s.to_string()).with_span(span),
9044            )),
9045            Token::DoubleQuotedString(ref s) => Ok(Expr::Value(
9046                Value::DoubleQuotedString(s.to_string()).with_span(span),
9047            )),
9048            Token::HexStringLiteral(ref s) => Ok(Expr::Value(
9049                Value::HexStringLiteral(s.to_string()).with_span(span),
9050            )),
9051            unexpected => self.expected(
9052                "a string value",
9053                TokenWithSpan {
9054                    token: unexpected,
9055                    span,
9056                },
9057            ),
9058        }
9059    }
9060
9061    /// Parse an unsigned literal integer/long
9062    pub fn parse_literal_uint(&mut self) -> Result<u64, ParserError> {
9063        let next_token = self.next_token();
9064        match next_token.token {
9065            Token::Number(s, _) => Self::parse::<u64>(s, next_token.span.start),
9066            _ => self.expected("literal int", next_token),
9067        }
9068    }
9069
9070    /// Parse the body of a `CREATE FUNCTION` specified as a string.
9071    /// e.g. `CREATE FUNCTION ... AS $$ body $$`.
9072    fn parse_create_function_body_string(&mut self) -> Result<Expr, ParserError> {
9073        let peek_token = self.peek_token();
9074        let span = peek_token.span;
9075        match peek_token.token {
9076            Token::DollarQuotedString(s) if dialect_of!(self is PostgreSqlDialect | GenericDialect) =>
9077            {
9078                self.next_token();
9079                Ok(Expr::Value(Value::DollarQuotedString(s).with_span(span)))
9080            }
9081            _ => Ok(Expr::Value(
9082                Value::SingleQuotedString(self.parse_literal_string()?).with_span(span),
9083            )),
9084        }
9085    }
9086
9087    /// Parse a literal string
9088    pub fn parse_literal_string(&mut self) -> Result<String, ParserError> {
9089        let next_token = self.next_token();
9090        match next_token.token {
9091            Token::Word(Word {
9092                value,
9093                keyword: Keyword::NoKeyword,
9094                ..
9095            }) => Ok(value),
9096            Token::SingleQuotedString(s) => Ok(s),
9097            Token::DoubleQuotedString(s) => Ok(s),
9098            Token::EscapedStringLiteral(s) if dialect_of!(self is PostgreSqlDialect | GenericDialect) => {
9099                Ok(s)
9100            }
9101            Token::UnicodeStringLiteral(s) => Ok(s),
9102            _ => self.expected("literal string", next_token),
9103        }
9104    }
9105
9106    /// Parse a literal unicode normalization clause
9107    pub fn parse_unicode_is_normalized(&mut self, expr: Expr) -> Result<Expr, ParserError> {
9108        let neg = self.parse_keyword(Keyword::NOT);
9109        let normalized_form = self.maybe_parse(|parser| {
9110            match parser.parse_one_of_keywords(&[
9111                Keyword::NFC,
9112                Keyword::NFD,
9113                Keyword::NFKC,
9114                Keyword::NFKD,
9115            ]) {
9116                Some(Keyword::NFC) => Ok(NormalizationForm::NFC),
9117                Some(Keyword::NFD) => Ok(NormalizationForm::NFD),
9118                Some(Keyword::NFKC) => Ok(NormalizationForm::NFKC),
9119                Some(Keyword::NFKD) => Ok(NormalizationForm::NFKD),
9120                _ => parser.expected("unicode normalization form", parser.peek_token()),
9121            }
9122        })?;
9123        if self.parse_keyword(Keyword::NORMALIZED) {
9124            return Ok(Expr::IsNormalized {
9125                expr: Box::new(expr),
9126                form: normalized_form,
9127                negated: neg,
9128            });
9129        }
9130        self.expected("unicode normalization form", self.peek_token())
9131    }
9132
9133    pub fn parse_enum_values(&mut self) -> Result<Vec<EnumMember>, ParserError> {
9134        self.expect_token(&Token::LParen)?;
9135        let values = self.parse_comma_separated(|parser| {
9136            let name = parser.parse_literal_string()?;
9137            let e = if parser.consume_token(&Token::Eq) {
9138                let value = parser.parse_number()?;
9139                EnumMember::NamedValue(name, value)
9140            } else {
9141                EnumMember::Name(name)
9142            };
9143            Ok(e)
9144        })?;
9145        self.expect_token(&Token::RParen)?;
9146
9147        Ok(values)
9148    }
9149
9150    /// Parse a SQL datatype (in the context of a CREATE TABLE statement for example)
9151    pub fn parse_data_type(&mut self) -> Result<DataType, ParserError> {
9152        let (ty, trailing_bracket) = self.parse_data_type_helper()?;
9153        if trailing_bracket.0 {
9154            return parser_err!(
9155                format!("unmatched > after parsing data type {ty}"),
9156                self.peek_token()
9157            );
9158        }
9159
9160        Ok(ty)
9161    }
9162
9163    fn parse_data_type_helper(
9164        &mut self,
9165    ) -> Result<(DataType, MatchedTrailingBracket), ParserError> {
9166        let dialect = self.dialect;
9167        self.advance_token();
9168        let next_token = self.get_current_token();
9169        let next_token_index = self.get_current_index();
9170
9171        let mut trailing_bracket: MatchedTrailingBracket = false.into();
9172        let mut data = match &next_token.token {
9173            Token::Word(w) => match w.keyword {
9174                Keyword::BOOLEAN => Ok(DataType::Boolean),
9175                Keyword::BOOL => Ok(DataType::Bool),
9176                Keyword::FLOAT => Ok(DataType::Float(self.parse_optional_precision()?)),
9177                Keyword::REAL => Ok(DataType::Real),
9178                Keyword::FLOAT4 => Ok(DataType::Float4),
9179                Keyword::FLOAT32 => Ok(DataType::Float32),
9180                Keyword::FLOAT64 => Ok(DataType::Float64),
9181                Keyword::FLOAT8 => Ok(DataType::Float8),
9182                Keyword::DOUBLE => {
9183                    if self.parse_keyword(Keyword::PRECISION) {
9184                        Ok(DataType::DoublePrecision)
9185                    } else {
9186                        Ok(DataType::Double(
9187                            self.parse_exact_number_optional_precision_scale()?,
9188                        ))
9189                    }
9190                }
9191                Keyword::TINYINT => {
9192                    let optional_precision = self.parse_optional_precision();
9193                    if self.parse_keyword(Keyword::UNSIGNED) {
9194                        Ok(DataType::TinyIntUnsigned(optional_precision?))
9195                    } else {
9196                        Ok(DataType::TinyInt(optional_precision?))
9197                    }
9198                }
9199                Keyword::INT2 => {
9200                    let optional_precision = self.parse_optional_precision();
9201                    if self.parse_keyword(Keyword::UNSIGNED) {
9202                        Ok(DataType::Int2Unsigned(optional_precision?))
9203                    } else {
9204                        Ok(DataType::Int2(optional_precision?))
9205                    }
9206                }
9207                Keyword::SMALLINT => {
9208                    let optional_precision = self.parse_optional_precision();
9209                    if self.parse_keyword(Keyword::UNSIGNED) {
9210                        Ok(DataType::SmallIntUnsigned(optional_precision?))
9211                    } else {
9212                        Ok(DataType::SmallInt(optional_precision?))
9213                    }
9214                }
9215                Keyword::MEDIUMINT => {
9216                    let optional_precision = self.parse_optional_precision();
9217                    if self.parse_keyword(Keyword::UNSIGNED) {
9218                        Ok(DataType::MediumIntUnsigned(optional_precision?))
9219                    } else {
9220                        Ok(DataType::MediumInt(optional_precision?))
9221                    }
9222                }
9223                Keyword::INT => {
9224                    let optional_precision = self.parse_optional_precision();
9225                    if self.parse_keyword(Keyword::UNSIGNED) {
9226                        Ok(DataType::IntUnsigned(optional_precision?))
9227                    } else {
9228                        Ok(DataType::Int(optional_precision?))
9229                    }
9230                }
9231                Keyword::INT4 => {
9232                    let optional_precision = self.parse_optional_precision();
9233                    if self.parse_keyword(Keyword::UNSIGNED) {
9234                        Ok(DataType::Int4Unsigned(optional_precision?))
9235                    } else {
9236                        Ok(DataType::Int4(optional_precision?))
9237                    }
9238                }
9239                Keyword::INT8 => {
9240                    let optional_precision = self.parse_optional_precision();
9241                    if self.parse_keyword(Keyword::UNSIGNED) {
9242                        Ok(DataType::Int8Unsigned(optional_precision?))
9243                    } else {
9244                        Ok(DataType::Int8(optional_precision?))
9245                    }
9246                }
9247                Keyword::INT16 => Ok(DataType::Int16),
9248                Keyword::INT32 => Ok(DataType::Int32),
9249                Keyword::INT64 => Ok(DataType::Int64),
9250                Keyword::INT128 => Ok(DataType::Int128),
9251                Keyword::INT256 => Ok(DataType::Int256),
9252                Keyword::INTEGER => {
9253                    let optional_precision = self.parse_optional_precision();
9254                    if self.parse_keyword(Keyword::UNSIGNED) {
9255                        Ok(DataType::IntegerUnsigned(optional_precision?))
9256                    } else {
9257                        Ok(DataType::Integer(optional_precision?))
9258                    }
9259                }
9260                Keyword::BIGINT => {
9261                    let optional_precision = self.parse_optional_precision();
9262                    if self.parse_keyword(Keyword::UNSIGNED) {
9263                        Ok(DataType::BigIntUnsigned(optional_precision?))
9264                    } else {
9265                        Ok(DataType::BigInt(optional_precision?))
9266                    }
9267                }
9268                Keyword::HUGEINT => Ok(DataType::HugeInt),
9269                Keyword::UBIGINT => Ok(DataType::UBigInt),
9270                Keyword::UHUGEINT => Ok(DataType::UHugeInt),
9271                Keyword::USMALLINT => Ok(DataType::USmallInt),
9272                Keyword::UTINYINT => Ok(DataType::UTinyInt),
9273                Keyword::UINT8 => Ok(DataType::UInt8),
9274                Keyword::UINT16 => Ok(DataType::UInt16),
9275                Keyword::UINT32 => Ok(DataType::UInt32),
9276                Keyword::UINT64 => Ok(DataType::UInt64),
9277                Keyword::UINT128 => Ok(DataType::UInt128),
9278                Keyword::UINT256 => Ok(DataType::UInt256),
9279                Keyword::VARCHAR => Ok(DataType::Varchar(self.parse_optional_character_length()?)),
9280                Keyword::NVARCHAR => {
9281                    Ok(DataType::Nvarchar(self.parse_optional_character_length()?))
9282                }
9283                Keyword::CHARACTER => {
9284                    if self.parse_keyword(Keyword::VARYING) {
9285                        Ok(DataType::CharacterVarying(
9286                            self.parse_optional_character_length()?,
9287                        ))
9288                    } else if self.parse_keywords(&[Keyword::LARGE, Keyword::OBJECT]) {
9289                        Ok(DataType::CharacterLargeObject(
9290                            self.parse_optional_precision()?,
9291                        ))
9292                    } else {
9293                        Ok(DataType::Character(self.parse_optional_character_length()?))
9294                    }
9295                }
9296                Keyword::CHAR => {
9297                    if self.parse_keyword(Keyword::VARYING) {
9298                        Ok(DataType::CharVarying(
9299                            self.parse_optional_character_length()?,
9300                        ))
9301                    } else if self.parse_keywords(&[Keyword::LARGE, Keyword::OBJECT]) {
9302                        Ok(DataType::CharLargeObject(self.parse_optional_precision()?))
9303                    } else {
9304                        Ok(DataType::Char(self.parse_optional_character_length()?))
9305                    }
9306                }
9307                Keyword::CLOB => Ok(DataType::Clob(self.parse_optional_precision()?)),
9308                Keyword::BINARY => Ok(DataType::Binary(self.parse_optional_precision()?)),
9309                Keyword::VARBINARY => Ok(DataType::Varbinary(self.parse_optional_binary_length()?)),
9310                Keyword::BLOB => Ok(DataType::Blob(self.parse_optional_precision()?)),
9311                Keyword::TINYBLOB => Ok(DataType::TinyBlob),
9312                Keyword::MEDIUMBLOB => Ok(DataType::MediumBlob),
9313                Keyword::LONGBLOB => Ok(DataType::LongBlob),
9314                Keyword::BYTES => Ok(DataType::Bytes(self.parse_optional_precision()?)),
9315                Keyword::BIT => {
9316                    if self.parse_keyword(Keyword::VARYING) {
9317                        Ok(DataType::BitVarying(self.parse_optional_precision()?))
9318                    } else {
9319                        Ok(DataType::Bit(self.parse_optional_precision()?))
9320                    }
9321                }
9322                Keyword::VARBIT => Ok(DataType::VarBit(self.parse_optional_precision()?)),
9323                Keyword::UUID => Ok(DataType::Uuid),
9324                Keyword::DATE => Ok(DataType::Date),
9325                Keyword::DATE32 => Ok(DataType::Date32),
9326                Keyword::DATETIME => Ok(DataType::Datetime(self.parse_optional_precision()?)),
9327                Keyword::DATETIME64 => {
9328                    self.prev_token();
9329                    let (precision, time_zone) = self.parse_datetime_64()?;
9330                    Ok(DataType::Datetime64(precision, time_zone))
9331                }
9332                Keyword::TIMESTAMP => {
9333                    let precision = self.parse_optional_precision()?;
9334                    let tz = if self.parse_keyword(Keyword::WITH) {
9335                        self.expect_keywords(&[Keyword::TIME, Keyword::ZONE])?;
9336                        TimezoneInfo::WithTimeZone
9337                    } else if self.parse_keyword(Keyword::WITHOUT) {
9338                        self.expect_keywords(&[Keyword::TIME, Keyword::ZONE])?;
9339                        TimezoneInfo::WithoutTimeZone
9340                    } else {
9341                        TimezoneInfo::None
9342                    };
9343                    Ok(DataType::Timestamp(precision, tz))
9344                }
9345                Keyword::TIMESTAMPTZ => Ok(DataType::Timestamp(
9346                    self.parse_optional_precision()?,
9347                    TimezoneInfo::Tz,
9348                )),
9349                Keyword::TIMESTAMP_NTZ => Ok(DataType::TimestampNtz),
9350                Keyword::TIME => {
9351                    let precision = self.parse_optional_precision()?;
9352                    let tz = if self.parse_keyword(Keyword::WITH) {
9353                        self.expect_keywords(&[Keyword::TIME, Keyword::ZONE])?;
9354                        TimezoneInfo::WithTimeZone
9355                    } else if self.parse_keyword(Keyword::WITHOUT) {
9356                        self.expect_keywords(&[Keyword::TIME, Keyword::ZONE])?;
9357                        TimezoneInfo::WithoutTimeZone
9358                    } else {
9359                        TimezoneInfo::None
9360                    };
9361                    Ok(DataType::Time(precision, tz))
9362                }
9363                Keyword::TIMETZ => Ok(DataType::Time(
9364                    self.parse_optional_precision()?,
9365                    TimezoneInfo::Tz,
9366                )),
9367                // Interval types can be followed by a complicated interval
9368                // qualifier that we don't currently support. See
9369                // parse_interval for a taste.
9370                Keyword::INTERVAL => Ok(DataType::Interval),
9371                Keyword::JSON => Ok(DataType::JSON),
9372                Keyword::JSONB => Ok(DataType::JSONB),
9373                Keyword::REGCLASS => Ok(DataType::Regclass),
9374                Keyword::STRING => Ok(DataType::String(self.parse_optional_precision()?)),
9375                Keyword::FIXEDSTRING => {
9376                    self.expect_token(&Token::LParen)?;
9377                    let character_length = self.parse_literal_uint()?;
9378                    self.expect_token(&Token::RParen)?;
9379                    Ok(DataType::FixedString(character_length))
9380                }
9381                Keyword::TEXT => Ok(DataType::Text),
9382                Keyword::TINYTEXT => Ok(DataType::TinyText),
9383                Keyword::MEDIUMTEXT => Ok(DataType::MediumText),
9384                Keyword::LONGTEXT => Ok(DataType::LongText),
9385                Keyword::BYTEA => Ok(DataType::Bytea),
9386                Keyword::NUMERIC => Ok(DataType::Numeric(
9387                    self.parse_exact_number_optional_precision_scale()?,
9388                )),
9389                Keyword::DECIMAL => Ok(DataType::Decimal(
9390                    self.parse_exact_number_optional_precision_scale()?,
9391                )),
9392                Keyword::DEC => Ok(DataType::Dec(
9393                    self.parse_exact_number_optional_precision_scale()?,
9394                )),
9395                Keyword::BIGNUMERIC => Ok(DataType::BigNumeric(
9396                    self.parse_exact_number_optional_precision_scale()?,
9397                )),
9398                Keyword::BIGDECIMAL => Ok(DataType::BigDecimal(
9399                    self.parse_exact_number_optional_precision_scale()?,
9400                )),
9401                Keyword::ENUM => Ok(DataType::Enum(self.parse_enum_values()?, None)),
9402                Keyword::ENUM8 => Ok(DataType::Enum(self.parse_enum_values()?, Some(8))),
9403                Keyword::ENUM16 => Ok(DataType::Enum(self.parse_enum_values()?, Some(16))),
9404                Keyword::SET => Ok(DataType::Set(self.parse_string_values()?)),
9405                Keyword::ARRAY => {
9406                    if dialect_of!(self is SnowflakeDialect) {
9407                        Ok(DataType::Array(ArrayElemTypeDef::None))
9408                    } else if dialect_of!(self is ClickHouseDialect) {
9409                        Ok(self.parse_sub_type(|internal_type| {
9410                            DataType::Array(ArrayElemTypeDef::Parenthesis(internal_type))
9411                        })?)
9412                    } else {
9413                        self.expect_token(&Token::Lt)?;
9414                        let (inside_type, _trailing_bracket) = self.parse_data_type_helper()?;
9415                        trailing_bracket = self.expect_closing_angle_bracket(_trailing_bracket)?;
9416                        Ok(DataType::Array(ArrayElemTypeDef::AngleBracket(Box::new(
9417                            inside_type,
9418                        ))))
9419                    }
9420                }
9421                Keyword::STRUCT if dialect_is!(dialect is DuckDbDialect) => {
9422                    self.prev_token();
9423                    let field_defs = self.parse_duckdb_struct_type_def()?;
9424                    Ok(DataType::Struct(field_defs, StructBracketKind::Parentheses))
9425                }
9426                Keyword::STRUCT if dialect_is!(dialect is BigQueryDialect | GenericDialect) => {
9427                    self.prev_token();
9428                    let (field_defs, _trailing_bracket) =
9429                        self.parse_struct_type_def(Self::parse_struct_field_def)?;
9430                    trailing_bracket = _trailing_bracket;
9431                    Ok(DataType::Struct(
9432                        field_defs,
9433                        StructBracketKind::AngleBrackets,
9434                    ))
9435                }
9436                Keyword::UNION if dialect_is!(dialect is DuckDbDialect | GenericDialect) => {
9437                    self.prev_token();
9438                    let fields = self.parse_union_type_def()?;
9439                    Ok(DataType::Union(fields))
9440                }
9441                Keyword::NULLABLE if dialect_is!(dialect is ClickHouseDialect | GenericDialect) => {
9442                    Ok(self.parse_sub_type(DataType::Nullable)?)
9443                }
9444                Keyword::LOWCARDINALITY if dialect_is!(dialect is ClickHouseDialect | GenericDialect) => {
9445                    Ok(self.parse_sub_type(DataType::LowCardinality)?)
9446                }
9447                Keyword::MAP if dialect_is!(dialect is ClickHouseDialect | GenericDialect) => {
9448                    self.prev_token();
9449                    let (key_data_type, value_data_type) = self.parse_click_house_map_def()?;
9450                    Ok(DataType::Map(
9451                        Box::new(key_data_type),
9452                        Box::new(value_data_type),
9453                    ))
9454                }
9455                Keyword::NESTED if dialect_is!(dialect is ClickHouseDialect | GenericDialect) => {
9456                    self.expect_token(&Token::LParen)?;
9457                    let field_defs = self.parse_comma_separated(Parser::parse_column_def)?;
9458                    self.expect_token(&Token::RParen)?;
9459                    Ok(DataType::Nested(field_defs))
9460                }
9461                Keyword::TUPLE if dialect_is!(dialect is ClickHouseDialect | GenericDialect) => {
9462                    self.prev_token();
9463                    let field_defs = self.parse_click_house_tuple_def()?;
9464                    Ok(DataType::Tuple(field_defs))
9465                }
9466                Keyword::TRIGGER => Ok(DataType::Trigger),
9467                Keyword::ANY if self.peek_keyword(Keyword::TYPE) => {
9468                    let _ = self.parse_keyword(Keyword::TYPE);
9469                    Ok(DataType::AnyType)
9470                }
9471                Keyword::TABLE => {
9472                    let columns = self.parse_returns_table_columns()?;
9473                    Ok(DataType::Table(columns))
9474                }
9475                Keyword::SIGNED => {
9476                    if self.parse_keyword(Keyword::INTEGER) {
9477                        Ok(DataType::SignedInteger)
9478                    } else {
9479                        Ok(DataType::Signed)
9480                    }
9481                }
9482                Keyword::UNSIGNED => {
9483                    if self.parse_keyword(Keyword::INTEGER) {
9484                        Ok(DataType::UnsignedInteger)
9485                    } else {
9486                        Ok(DataType::Unsigned)
9487                    }
9488                }
9489                _ => {
9490                    self.prev_token();
9491                    let type_name = self.parse_object_name(false)?;
9492                    if let Some(modifiers) = self.parse_optional_type_modifiers()? {
9493                        Ok(DataType::Custom(type_name, modifiers))
9494                    } else {
9495                        Ok(DataType::Custom(type_name, vec![]))
9496                    }
9497                }
9498            },
9499            _ => self.expected_at("a data type name", next_token_index),
9500        }?;
9501
9502        if self.dialect.supports_array_typedef_with_brackets() {
9503            while self.consume_token(&Token::LBracket) {
9504                // Parse optional array data type size
9505                let size = self.maybe_parse(|p| p.parse_literal_uint())?;
9506                self.expect_token(&Token::RBracket)?;
9507                data = DataType::Array(ArrayElemTypeDef::SquareBracket(Box::new(data), size))
9508            }
9509        }
9510        Ok((data, trailing_bracket))
9511    }
9512
9513    fn parse_returns_table_column(&mut self) -> Result<ColumnDef, ParserError> {
9514        let name = self.parse_identifier()?;
9515        let data_type = self.parse_data_type()?;
9516        Ok(ColumnDef {
9517            name,
9518            data_type,
9519            options: Vec::new(), // No constraints expected here
9520        })
9521    }
9522
9523    fn parse_returns_table_columns(&mut self) -> Result<Vec<ColumnDef>, ParserError> {
9524        self.expect_token(&Token::LParen)?;
9525        let columns = self.parse_comma_separated(Parser::parse_returns_table_column)?;
9526        self.expect_token(&Token::RParen)?;
9527        Ok(columns)
9528    }
9529
9530    pub fn parse_string_values(&mut self) -> Result<Vec<String>, ParserError> {
9531        self.expect_token(&Token::LParen)?;
9532        let mut values = Vec::new();
9533        loop {
9534            let next_token = self.next_token();
9535            match next_token.token {
9536                Token::SingleQuotedString(value) => values.push(value),
9537                _ => self.expected("a string", next_token)?,
9538            }
9539            let next_token = self.next_token();
9540            match next_token.token {
9541                Token::Comma => (),
9542                Token::RParen => break,
9543                _ => self.expected(", or }", next_token)?,
9544            }
9545        }
9546        Ok(values)
9547    }
9548
9549    /// Strictly parse `identifier AS identifier`
9550    pub fn parse_identifier_with_alias(&mut self) -> Result<IdentWithAlias, ParserError> {
9551        let ident = self.parse_identifier()?;
9552        self.expect_keyword_is(Keyword::AS)?;
9553        let alias = self.parse_identifier()?;
9554        Ok(IdentWithAlias { ident, alias })
9555    }
9556
9557    /// Optionally parses an alias for a select list item
9558    fn maybe_parse_select_item_alias(&mut self) -> Result<Option<Ident>, ParserError> {
9559        fn validator(explicit: bool, kw: &Keyword, parser: &mut Parser) -> bool {
9560            parser.dialect.is_select_item_alias(explicit, kw, parser)
9561        }
9562        self.parse_optional_alias_inner(None, validator)
9563    }
9564
9565    /// Optionally parses an alias for a table like in `... FROM generate_series(1, 10) AS t (col)`.
9566    /// In this case, the alias is allowed to optionally name the columns in the table, in
9567    /// addition to the table itself.
9568    pub fn maybe_parse_table_alias(&mut self) -> Result<Option<TableAlias>, ParserError> {
9569        fn validator(explicit: bool, kw: &Keyword, parser: &mut Parser) -> bool {
9570            parser.dialect.is_table_factor_alias(explicit, kw, parser)
9571        }
9572        match self.parse_optional_alias_inner(None, validator)? {
9573            Some(name) => {
9574                let columns = self.parse_table_alias_column_defs()?;
9575                Ok(Some(TableAlias { name, columns }))
9576            }
9577            None => Ok(None),
9578        }
9579    }
9580
9581    fn parse_table_index_hints(&mut self) -> Result<Vec<TableIndexHints>, ParserError> {
9582        let mut hints = vec![];
9583        while let Some(hint_type) =
9584            self.parse_one_of_keywords(&[Keyword::USE, Keyword::IGNORE, Keyword::FORCE])
9585        {
9586            let hint_type = match hint_type {
9587                Keyword::USE => TableIndexHintType::Use,
9588                Keyword::IGNORE => TableIndexHintType::Ignore,
9589                Keyword::FORCE => TableIndexHintType::Force,
9590                _ => {
9591                    return self.expected(
9592                        "expected to match USE/IGNORE/FORCE keyword",
9593                        self.peek_token(),
9594                    )
9595                }
9596            };
9597            let index_type = match self.parse_one_of_keywords(&[Keyword::INDEX, Keyword::KEY]) {
9598                Some(Keyword::INDEX) => TableIndexType::Index,
9599                Some(Keyword::KEY) => TableIndexType::Key,
9600                _ => {
9601                    return self.expected("expected to match INDEX/KEY keyword", self.peek_token())
9602                }
9603            };
9604            let for_clause = if self.parse_keyword(Keyword::FOR) {
9605                let clause = if self.parse_keyword(Keyword::JOIN) {
9606                    TableIndexHintForClause::Join
9607                } else if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
9608                    TableIndexHintForClause::OrderBy
9609                } else if self.parse_keywords(&[Keyword::GROUP, Keyword::BY]) {
9610                    TableIndexHintForClause::GroupBy
9611                } else {
9612                    return self.expected(
9613                        "expected to match FOR/ORDER BY/GROUP BY table hint in for clause",
9614                        self.peek_token(),
9615                    );
9616                };
9617                Some(clause)
9618            } else {
9619                None
9620            };
9621
9622            self.expect_token(&Token::LParen)?;
9623            let index_names = if self.peek_token().token != Token::RParen {
9624                self.parse_comma_separated(Parser::parse_identifier)?
9625            } else {
9626                vec![]
9627            };
9628            self.expect_token(&Token::RParen)?;
9629            hints.push(TableIndexHints {
9630                hint_type,
9631                index_type,
9632                for_clause,
9633                index_names,
9634            });
9635        }
9636        Ok(hints)
9637    }
9638
9639    /// Wrapper for parse_optional_alias_inner, left for backwards-compatibility
9640    /// but new flows should use the context-specific methods such as `maybe_parse_select_item_alias`
9641    /// and `maybe_parse_table_alias`.
9642    pub fn parse_optional_alias(
9643        &mut self,
9644        reserved_kwds: &[Keyword],
9645    ) -> Result<Option<Ident>, ParserError> {
9646        fn validator(_explicit: bool, _kw: &Keyword, _parser: &mut Parser) -> bool {
9647            false
9648        }
9649        self.parse_optional_alias_inner(Some(reserved_kwds), validator)
9650    }
9651
9652    /// Parses an optional alias after a SQL element such as a select list item
9653    /// or a table name.
9654    ///
9655    /// This method accepts an optional list of reserved keywords or a function
9656    /// to call to validate if a keyword should be parsed as an alias, to allow
9657    /// callers to customize the parsing logic based on their context.
9658    fn parse_optional_alias_inner<F>(
9659        &mut self,
9660        reserved_kwds: Option<&[Keyword]>,
9661        validator: F,
9662    ) -> Result<Option<Ident>, ParserError>
9663    where
9664        F: Fn(bool, &Keyword, &mut Parser) -> bool,
9665    {
9666        let after_as = self.parse_keyword(Keyword::AS);
9667
9668        let next_token = self.next_token();
9669        match next_token.token {
9670            // By default, if a word is located after the `AS` keyword we consider it an alias
9671            // as long as it's not reserved.
9672            Token::Word(w)
9673                if after_as || reserved_kwds.is_some_and(|x| !x.contains(&w.keyword)) =>
9674            {
9675                Ok(Some(w.into_ident(next_token.span)))
9676            }
9677            // This pattern allows for customizing the acceptance of words as aliases based on the caller's
9678            // context, such as to what SQL element this word is a potential alias of (select item alias, table name
9679            // alias, etc.) or dialect-specific logic that goes beyond a simple list of reserved keywords.
9680            Token::Word(w) if validator(after_as, &w.keyword, self) => {
9681                Ok(Some(w.into_ident(next_token.span)))
9682            }
9683            // For backwards-compatibility, we accept quoted strings as aliases regardless of the context.
9684            Token::SingleQuotedString(s) => Ok(Some(Ident::with_quote('\'', s))),
9685            Token::DoubleQuotedString(s) => Ok(Some(Ident::with_quote('\"', s))),
9686            _ => {
9687                if after_as {
9688                    return self.expected("an identifier after AS", next_token);
9689                }
9690                self.prev_token();
9691                Ok(None) // no alias found
9692            }
9693        }
9694    }
9695
9696    pub fn parse_optional_group_by(&mut self) -> Result<Option<GroupByExpr>, ParserError> {
9697        if self.parse_keywords(&[Keyword::GROUP, Keyword::BY]) {
9698            let expressions = if self.parse_keyword(Keyword::ALL) {
9699                None
9700            } else {
9701                Some(self.parse_comma_separated(Parser::parse_group_by_expr)?)
9702            };
9703
9704            let mut modifiers = vec![];
9705            if self.dialect.supports_group_by_with_modifier() {
9706                loop {
9707                    if !self.parse_keyword(Keyword::WITH) {
9708                        break;
9709                    }
9710                    let keyword = self.expect_one_of_keywords(&[
9711                        Keyword::ROLLUP,
9712                        Keyword::CUBE,
9713                        Keyword::TOTALS,
9714                    ])?;
9715                    modifiers.push(match keyword {
9716                        Keyword::ROLLUP => GroupByWithModifier::Rollup,
9717                        Keyword::CUBE => GroupByWithModifier::Cube,
9718                        Keyword::TOTALS => GroupByWithModifier::Totals,
9719                        _ => {
9720                            return parser_err!(
9721                                "BUG: expected to match GroupBy modifier keyword",
9722                                self.peek_token().span.start
9723                            )
9724                        }
9725                    });
9726                }
9727            }
9728            if self.parse_keywords(&[Keyword::GROUPING, Keyword::SETS]) {
9729                self.expect_token(&Token::LParen)?;
9730                let result = self.parse_comma_separated(|p| p.parse_tuple(true, true))?;
9731                self.expect_token(&Token::RParen)?;
9732                modifiers.push(GroupByWithModifier::GroupingSets(Expr::GroupingSets(
9733                    result,
9734                )));
9735            };
9736            let group_by = match expressions {
9737                None => GroupByExpr::All(modifiers),
9738                Some(exprs) => GroupByExpr::Expressions(exprs, modifiers),
9739            };
9740            Ok(Some(group_by))
9741        } else {
9742            Ok(None)
9743        }
9744    }
9745
9746    pub fn parse_optional_order_by(&mut self) -> Result<Option<OrderBy>, ParserError> {
9747        if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
9748            let order_by =
9749                if self.dialect.supports_order_by_all() && self.parse_keyword(Keyword::ALL) {
9750                    let order_by_options = self.parse_order_by_options()?;
9751                    OrderBy {
9752                        kind: OrderByKind::All(order_by_options),
9753                        interpolate: None,
9754                    }
9755                } else {
9756                    let exprs = self.parse_comma_separated(Parser::parse_order_by_expr)?;
9757                    let interpolate = if dialect_of!(self is ClickHouseDialect | GenericDialect) {
9758                        self.parse_interpolations()?
9759                    } else {
9760                        None
9761                    };
9762                    OrderBy {
9763                        kind: OrderByKind::Expressions(exprs),
9764                        interpolate,
9765                    }
9766                };
9767            Ok(Some(order_by))
9768        } else {
9769            Ok(None)
9770        }
9771    }
9772
9773    fn parse_optional_limit_clause(&mut self) -> Result<Option<LimitClause>, ParserError> {
9774        let mut offset = if self.parse_keyword(Keyword::OFFSET) {
9775            Some(self.parse_offset()?)
9776        } else {
9777            None
9778        };
9779
9780        let (limit, limit_by) = if self.parse_keyword(Keyword::LIMIT) {
9781            let expr = self.parse_limit()?;
9782
9783            if self.dialect.supports_limit_comma()
9784                && offset.is_none()
9785                && expr.is_some() // ALL not supported with comma
9786                && self.consume_token(&Token::Comma)
9787            {
9788                let offset = expr.ok_or_else(|| {
9789                    ParserError::ParserError(
9790                        "Missing offset for LIMIT <offset>, <limit>".to_string(),
9791                    )
9792                })?;
9793                return Ok(Some(LimitClause::OffsetCommaLimit {
9794                    offset,
9795                    limit: self.parse_expr()?,
9796                }));
9797            }
9798
9799            let limit_by = if dialect_of!(self is ClickHouseDialect | GenericDialect)
9800                && self.parse_keyword(Keyword::BY)
9801            {
9802                Some(self.parse_comma_separated(Parser::parse_expr)?)
9803            } else {
9804                None
9805            };
9806
9807            (Some(expr), limit_by)
9808        } else {
9809            (None, None)
9810        };
9811
9812        if offset.is_none() && limit.is_some() && self.parse_keyword(Keyword::OFFSET) {
9813            offset = Some(self.parse_offset()?);
9814        }
9815
9816        if offset.is_some() || (limit.is_some() && limit != Some(None)) || limit_by.is_some() {
9817            Ok(Some(LimitClause::LimitOffset {
9818                limit: limit.unwrap_or_default(),
9819                offset,
9820                limit_by: limit_by.unwrap_or_default(),
9821            }))
9822        } else {
9823            Ok(None)
9824        }
9825    }
9826
9827    /// Parse a table object for insertion
9828    /// e.g. `some_database.some_table` or `FUNCTION some_table_func(...)`
9829    pub fn parse_table_object(&mut self) -> Result<TableObject, ParserError> {
9830        if self.dialect.supports_insert_table_function() && self.parse_keyword(Keyword::FUNCTION) {
9831            let fn_name = self.parse_object_name(false)?;
9832            self.parse_function_call(fn_name)
9833                .map(TableObject::TableFunction)
9834        } else {
9835            self.parse_object_name(false).map(TableObject::TableName)
9836        }
9837    }
9838
9839    /// Parse a possibly qualified, possibly quoted identifier, optionally allowing for wildcards,
9840    /// e.g. *, *.*, `foo`.*, or "foo"."bar"
9841    fn parse_object_name_with_wildcards(
9842        &mut self,
9843        in_table_clause: bool,
9844        allow_wildcards: bool,
9845    ) -> Result<ObjectName, ParserError> {
9846        let mut idents = vec![];
9847
9848        if dialect_of!(self is BigQueryDialect) && in_table_clause {
9849            loop {
9850                let (ident, end_with_period) = self.parse_unquoted_hyphenated_identifier()?;
9851                idents.push(ident);
9852                if !self.consume_token(&Token::Period) && !end_with_period {
9853                    break;
9854                }
9855            }
9856        } else {
9857            loop {
9858                let ident = if allow_wildcards && self.peek_token().token == Token::Mul {
9859                    let span = self.next_token().span;
9860                    Ident {
9861                        value: Token::Mul.to_string(),
9862                        quote_style: None,
9863                        span,
9864                    }
9865                } else {
9866                    if self.dialect.supports_object_name_double_dot_notation()
9867                        && idents.len() == 1
9868                        && self.consume_token(&Token::Period)
9869                    {
9870                        // Empty string here means default schema
9871                        idents.push(Ident::new(""));
9872                    }
9873                    self.parse_identifier()?
9874                };
9875                idents.push(ident);
9876                if !self.consume_token(&Token::Period) {
9877                    break;
9878                }
9879            }
9880        }
9881        Ok(ObjectName::from(idents))
9882    }
9883
9884    /// Parse a possibly qualified, possibly quoted identifier, e.g.
9885    /// `foo` or `myschema."table"
9886    ///
9887    /// The `in_table_clause` parameter indicates whether the object name is a table in a FROM, JOIN,
9888    /// or similar table clause. Currently, this is used only to support unquoted hyphenated identifiers
9889    /// in this context on BigQuery.
9890    pub fn parse_object_name(&mut self, in_table_clause: bool) -> Result<ObjectName, ParserError> {
9891        let ObjectName(mut idents) =
9892            self.parse_object_name_with_wildcards(in_table_clause, false)?;
9893
9894        // BigQuery accepts any number of quoted identifiers of a table name.
9895        // https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical#quoted_identifiers
9896        if dialect_of!(self is BigQueryDialect)
9897            && idents.iter().any(|part| {
9898                part.as_ident()
9899                    .is_some_and(|ident| ident.value.contains('.'))
9900            })
9901        {
9902            idents = idents
9903                .into_iter()
9904                .flat_map(|part| match part.as_ident() {
9905                    Some(ident) => ident
9906                        .value
9907                        .split('.')
9908                        .map(|value| {
9909                            ObjectNamePart::Identifier(Ident {
9910                                value: value.into(),
9911                                quote_style: ident.quote_style,
9912                                span: ident.span,
9913                            })
9914                        })
9915                        .collect::<Vec<_>>(),
9916                    None => vec![part],
9917                })
9918                .collect()
9919        }
9920
9921        Ok(ObjectName(idents))
9922    }
9923
9924    /// Parse identifiers
9925    pub fn parse_identifiers(&mut self) -> Result<Vec<Ident>, ParserError> {
9926        let mut idents = vec![];
9927        loop {
9928            match &self.peek_token_ref().token {
9929                Token::Word(w) => {
9930                    idents.push(w.clone().into_ident(self.peek_token_ref().span));
9931                }
9932                Token::EOF | Token::Eq => break,
9933                _ => {}
9934            }
9935            self.advance_token();
9936        }
9937        Ok(idents)
9938    }
9939
9940    /// Parse identifiers of form ident1[.identN]*
9941    ///
9942    /// Similar in functionality to [parse_identifiers], with difference
9943    /// being this function is much more strict about parsing a valid multipart identifier, not
9944    /// allowing extraneous tokens to be parsed, otherwise it fails.
9945    ///
9946    /// For example:
9947    ///
9948    /// ```rust
9949    /// use sqlparser::ast::Ident;
9950    /// use sqlparser::dialect::GenericDialect;
9951    /// use sqlparser::parser::Parser;
9952    ///
9953    /// let dialect = GenericDialect {};
9954    /// let expected = vec![Ident::new("one"), Ident::new("two")];
9955    ///
9956    /// // expected usage
9957    /// let sql = "one.two";
9958    /// let mut parser = Parser::new(&dialect).try_with_sql(sql).unwrap();
9959    /// let actual = parser.parse_multipart_identifier().unwrap();
9960    /// assert_eq!(&actual, &expected);
9961    ///
9962    /// // parse_identifiers is more loose on what it allows, parsing successfully
9963    /// let sql = "one + two";
9964    /// let mut parser = Parser::new(&dialect).try_with_sql(sql).unwrap();
9965    /// let actual = parser.parse_identifiers().unwrap();
9966    /// assert_eq!(&actual, &expected);
9967    ///
9968    /// // expected to strictly fail due to + separator
9969    /// let sql = "one + two";
9970    /// let mut parser = Parser::new(&dialect).try_with_sql(sql).unwrap();
9971    /// let actual = parser.parse_multipart_identifier().unwrap_err();
9972    /// assert_eq!(
9973    ///     actual.to_string(),
9974    ///     "sql parser error: Unexpected token in identifier: +"
9975    /// );
9976    /// ```
9977    ///
9978    /// [parse_identifiers]: Parser::parse_identifiers
9979    pub fn parse_multipart_identifier(&mut self) -> Result<Vec<Ident>, ParserError> {
9980        let mut idents = vec![];
9981
9982        // expecting at least one word for identifier
9983        let next_token = self.next_token();
9984        match next_token.token {
9985            Token::Word(w) => idents.push(w.into_ident(next_token.span)),
9986            Token::EOF => {
9987                return Err(ParserError::ParserError(
9988                    "Empty input when parsing identifier".to_string(),
9989                ))?
9990            }
9991            token => {
9992                return Err(ParserError::ParserError(format!(
9993                    "Unexpected token in identifier: {token}"
9994                )))?
9995            }
9996        };
9997
9998        // parse optional next parts if exist
9999        loop {
10000            match self.next_token().token {
10001                // ensure that optional period is succeeded by another identifier
10002                Token::Period => {
10003                    let next_token = self.next_token();
10004                    match next_token.token {
10005                        Token::Word(w) => idents.push(w.into_ident(next_token.span)),
10006                        Token::EOF => {
10007                            return Err(ParserError::ParserError(
10008                                "Trailing period in identifier".to_string(),
10009                            ))?
10010                        }
10011                        token => {
10012                            return Err(ParserError::ParserError(format!(
10013                                "Unexpected token following period in identifier: {token}"
10014                            )))?
10015                        }
10016                    }
10017                }
10018                Token::EOF => break,
10019                token => {
10020                    return Err(ParserError::ParserError(format!(
10021                        "Unexpected token in identifier: {token}"
10022                    )))?
10023                }
10024            }
10025        }
10026
10027        Ok(idents)
10028    }
10029
10030    /// Parse a simple one-word identifier (possibly quoted, possibly a keyword)
10031    pub fn parse_identifier(&mut self) -> Result<Ident, ParserError> {
10032        let next_token = self.next_token();
10033        match next_token.token {
10034            Token::Word(w) => Ok(w.into_ident(next_token.span)),
10035            Token::SingleQuotedString(s) => Ok(Ident::with_quote('\'', s)),
10036            Token::DoubleQuotedString(s) => Ok(Ident::with_quote('\"', s)),
10037            _ => self.expected("identifier", next_token),
10038        }
10039    }
10040
10041    /// On BigQuery, hyphens are permitted in unquoted identifiers inside of a FROM or
10042    /// TABLE clause.
10043    ///
10044    /// The first segment must be an ordinary unquoted identifier, e.g. it must not start
10045    /// with a digit. Subsequent segments are either must either be valid identifiers or
10046    /// integers, e.g. foo-123 is allowed, but foo-123a is not.
10047    ///
10048    /// [BigQuery-lexical](https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical)
10049    ///
10050    /// Return a tuple of the identifier and a boolean indicating it ends with a period.
10051    fn parse_unquoted_hyphenated_identifier(&mut self) -> Result<(Ident, bool), ParserError> {
10052        match self.peek_token().token {
10053            Token::Word(w) => {
10054                let quote_style_is_none = w.quote_style.is_none();
10055                let mut requires_whitespace = false;
10056                let mut ident = w.into_ident(self.next_token().span);
10057                if quote_style_is_none {
10058                    while matches!(self.peek_token_no_skip().token, Token::Minus) {
10059                        self.next_token();
10060                        ident.value.push('-');
10061
10062                        let token = self
10063                            .next_token_no_skip()
10064                            .cloned()
10065                            .unwrap_or(TokenWithSpan::wrap(Token::EOF));
10066                        requires_whitespace = match token.token {
10067                            Token::Word(next_word) if next_word.quote_style.is_none() => {
10068                                ident.value.push_str(&next_word.value);
10069                                false
10070                            }
10071                            Token::Number(s, false) => {
10072                                // A number token can represent a decimal value ending with a period, e.g., `Number('123.')`.
10073                                // However, for an [ObjectName], it is part of a hyphenated identifier, e.g., `foo-123.bar`.
10074                                //
10075                                // If a number token is followed by a period, it is part of an [ObjectName].
10076                                // Return the identifier with `true` if the number token is followed by a period, indicating that
10077                                // parsing should continue for the next part of the hyphenated identifier.
10078                                if s.ends_with('.') {
10079                                    let Some(s) = s.split('.').next().filter(|s| {
10080                                        !s.is_empty() && s.chars().all(|c| c.is_ascii_digit())
10081                                    }) else {
10082                                        return self.expected(
10083                                            "continuation of hyphenated identifier",
10084                                            TokenWithSpan::new(Token::Number(s, false), token.span),
10085                                        );
10086                                    };
10087                                    ident.value.push_str(s);
10088                                    return Ok((ident, true));
10089                                } else {
10090                                    ident.value.push_str(&s);
10091                                }
10092                                // If next token is period, then it is part of an ObjectName and we don't expect whitespace
10093                                // after the number.
10094                                !matches!(self.peek_token().token, Token::Period)
10095                            }
10096                            _ => {
10097                                return self
10098                                    .expected("continuation of hyphenated identifier", token);
10099                            }
10100                        }
10101                    }
10102
10103                    // If the last segment was a number, we must check that it's followed by whitespace,
10104                    // otherwise foo-123a will be parsed as `foo-123` with the alias `a`.
10105                    if requires_whitespace {
10106                        let token = self.next_token();
10107                        if !matches!(token.token, Token::EOF | Token::Whitespace(_)) {
10108                            return self
10109                                .expected("whitespace following hyphenated identifier", token);
10110                        }
10111                    }
10112                }
10113                Ok((ident, false))
10114            }
10115            _ => Ok((self.parse_identifier()?, false)),
10116        }
10117    }
10118
10119    /// Parses a parenthesized, comma-separated list of column definitions within a view.
10120    fn parse_view_columns(&mut self) -> Result<Vec<ViewColumnDef>, ParserError> {
10121        if self.consume_token(&Token::LParen) {
10122            if self.peek_token().token == Token::RParen {
10123                self.next_token();
10124                Ok(vec![])
10125            } else {
10126                let cols = self.parse_comma_separated_with_trailing_commas(
10127                    Parser::parse_view_column,
10128                    self.dialect.supports_column_definition_trailing_commas(),
10129                    Self::is_reserved_for_column_alias,
10130                )?;
10131                self.expect_token(&Token::RParen)?;
10132                Ok(cols)
10133            }
10134        } else {
10135            Ok(vec![])
10136        }
10137    }
10138
10139    /// Parses a column definition within a view.
10140    fn parse_view_column(&mut self) -> Result<ViewColumnDef, ParserError> {
10141        let name = self.parse_identifier()?;
10142        let options = if (dialect_of!(self is BigQueryDialect | GenericDialect)
10143            && self.parse_keyword(Keyword::OPTIONS))
10144            || (dialect_of!(self is SnowflakeDialect | GenericDialect)
10145                && self.parse_keyword(Keyword::COMMENT))
10146        {
10147            self.prev_token();
10148            self.parse_optional_column_option()?
10149                .map(|option| vec![option])
10150        } else {
10151            None
10152        };
10153        let data_type = if dialect_of!(self is ClickHouseDialect) {
10154            Some(self.parse_data_type()?)
10155        } else {
10156            None
10157        };
10158        Ok(ViewColumnDef {
10159            name,
10160            data_type,
10161            options,
10162        })
10163    }
10164
10165    /// Parses a parenthesized comma-separated list of unqualified, possibly quoted identifiers.
10166    /// For example: `(col1, "col 2", ...)`
10167    pub fn parse_parenthesized_column_list(
10168        &mut self,
10169        optional: IsOptional,
10170        allow_empty: bool,
10171    ) -> Result<Vec<Ident>, ParserError> {
10172        self.parse_parenthesized_column_list_inner(optional, allow_empty, |p| p.parse_identifier())
10173    }
10174
10175    /// Parses a parenthesized comma-separated list of qualified, possibly quoted identifiers.
10176    /// For example: `(db1.sc1.tbl1.col1, db1.sc1.tbl1."col 2", ...)`
10177    pub fn parse_parenthesized_qualified_column_list(
10178        &mut self,
10179        optional: IsOptional,
10180        allow_empty: bool,
10181    ) -> Result<Vec<ObjectName>, ParserError> {
10182        self.parse_parenthesized_column_list_inner(optional, allow_empty, |p| {
10183            p.parse_object_name(true)
10184        })
10185    }
10186
10187    /// Parses a parenthesized comma-separated list of columns using
10188    /// the provided function to parse each element.
10189    fn parse_parenthesized_column_list_inner<F, T>(
10190        &mut self,
10191        optional: IsOptional,
10192        allow_empty: bool,
10193        mut f: F,
10194    ) -> Result<Vec<T>, ParserError>
10195    where
10196        F: FnMut(&mut Parser) -> Result<T, ParserError>,
10197    {
10198        if self.consume_token(&Token::LParen) {
10199            if allow_empty && self.peek_token().token == Token::RParen {
10200                self.next_token();
10201                Ok(vec![])
10202            } else {
10203                let cols = self.parse_comma_separated(|p| f(p))?;
10204                self.expect_token(&Token::RParen)?;
10205                Ok(cols)
10206            }
10207        } else if optional == Optional {
10208            Ok(vec![])
10209        } else {
10210            self.expected("a list of columns in parentheses", self.peek_token())
10211        }
10212    }
10213
10214    /// Parses a parenthesized comma-separated list of table alias column definitions.
10215    fn parse_table_alias_column_defs(&mut self) -> Result<Vec<TableAliasColumnDef>, ParserError> {
10216        if self.consume_token(&Token::LParen) {
10217            let cols = self.parse_comma_separated(|p| {
10218                let name = p.parse_identifier()?;
10219                let data_type = p.maybe_parse(|p| p.parse_data_type())?;
10220                Ok(TableAliasColumnDef { name, data_type })
10221            })?;
10222            self.expect_token(&Token::RParen)?;
10223            Ok(cols)
10224        } else {
10225            Ok(vec![])
10226        }
10227    }
10228
10229    pub fn parse_precision(&mut self) -> Result<u64, ParserError> {
10230        self.expect_token(&Token::LParen)?;
10231        let n = self.parse_literal_uint()?;
10232        self.expect_token(&Token::RParen)?;
10233        Ok(n)
10234    }
10235
10236    pub fn parse_optional_precision(&mut self) -> Result<Option<u64>, ParserError> {
10237        if self.consume_token(&Token::LParen) {
10238            let n = self.parse_literal_uint()?;
10239            self.expect_token(&Token::RParen)?;
10240            Ok(Some(n))
10241        } else {
10242            Ok(None)
10243        }
10244    }
10245
10246    /// Parse datetime64 [1]
10247    /// Syntax
10248    /// ```sql
10249    /// DateTime64(precision[, timezone])
10250    /// ```
10251    ///
10252    /// [1]: https://clickhouse.com/docs/en/sql-reference/data-types/datetime64
10253    pub fn parse_datetime_64(&mut self) -> Result<(u64, Option<String>), ParserError> {
10254        self.expect_keyword_is(Keyword::DATETIME64)?;
10255        self.expect_token(&Token::LParen)?;
10256        let precision = self.parse_literal_uint()?;
10257        let time_zone = if self.consume_token(&Token::Comma) {
10258            Some(self.parse_literal_string()?)
10259        } else {
10260            None
10261        };
10262        self.expect_token(&Token::RParen)?;
10263        Ok((precision, time_zone))
10264    }
10265
10266    pub fn parse_optional_character_length(
10267        &mut self,
10268    ) -> Result<Option<CharacterLength>, ParserError> {
10269        if self.consume_token(&Token::LParen) {
10270            let character_length = self.parse_character_length()?;
10271            self.expect_token(&Token::RParen)?;
10272            Ok(Some(character_length))
10273        } else {
10274            Ok(None)
10275        }
10276    }
10277
10278    pub fn parse_optional_binary_length(&mut self) -> Result<Option<BinaryLength>, ParserError> {
10279        if self.consume_token(&Token::LParen) {
10280            let binary_length = self.parse_binary_length()?;
10281            self.expect_token(&Token::RParen)?;
10282            Ok(Some(binary_length))
10283        } else {
10284            Ok(None)
10285        }
10286    }
10287
10288    pub fn parse_character_length(&mut self) -> Result<CharacterLength, ParserError> {
10289        if self.parse_keyword(Keyword::MAX) {
10290            return Ok(CharacterLength::Max);
10291        }
10292        let length = self.parse_literal_uint()?;
10293        let unit = if self.parse_keyword(Keyword::CHARACTERS) {
10294            Some(CharLengthUnits::Characters)
10295        } else if self.parse_keyword(Keyword::OCTETS) {
10296            Some(CharLengthUnits::Octets)
10297        } else {
10298            None
10299        };
10300        Ok(CharacterLength::IntegerLength { length, unit })
10301    }
10302
10303    pub fn parse_binary_length(&mut self) -> Result<BinaryLength, ParserError> {
10304        if self.parse_keyword(Keyword::MAX) {
10305            return Ok(BinaryLength::Max);
10306        }
10307        let length = self.parse_literal_uint()?;
10308        Ok(BinaryLength::IntegerLength { length })
10309    }
10310
10311    pub fn parse_optional_precision_scale(
10312        &mut self,
10313    ) -> Result<(Option<u64>, Option<u64>), ParserError> {
10314        if self.consume_token(&Token::LParen) {
10315            let n = self.parse_literal_uint()?;
10316            let scale = if self.consume_token(&Token::Comma) {
10317                Some(self.parse_literal_uint()?)
10318            } else {
10319                None
10320            };
10321            self.expect_token(&Token::RParen)?;
10322            Ok((Some(n), scale))
10323        } else {
10324            Ok((None, None))
10325        }
10326    }
10327
10328    pub fn parse_exact_number_optional_precision_scale(
10329        &mut self,
10330    ) -> Result<ExactNumberInfo, ParserError> {
10331        if self.consume_token(&Token::LParen) {
10332            let precision = self.parse_literal_uint()?;
10333            let scale = if self.consume_token(&Token::Comma) {
10334                Some(self.parse_literal_uint()?)
10335            } else {
10336                None
10337            };
10338
10339            self.expect_token(&Token::RParen)?;
10340
10341            match scale {
10342                None => Ok(ExactNumberInfo::Precision(precision)),
10343                Some(scale) => Ok(ExactNumberInfo::PrecisionAndScale(precision, scale)),
10344            }
10345        } else {
10346            Ok(ExactNumberInfo::None)
10347        }
10348    }
10349
10350    pub fn parse_optional_type_modifiers(&mut self) -> Result<Option<Vec<String>>, ParserError> {
10351        if self.consume_token(&Token::LParen) {
10352            let mut modifiers = Vec::new();
10353            loop {
10354                let next_token = self.next_token();
10355                match next_token.token {
10356                    Token::Word(w) => modifiers.push(w.to_string()),
10357                    Token::Number(n, _) => modifiers.push(n),
10358                    Token::SingleQuotedString(s) => modifiers.push(s),
10359
10360                    Token::Comma => {
10361                        continue;
10362                    }
10363                    Token::RParen => {
10364                        break;
10365                    }
10366                    _ => self.expected("type modifiers", next_token)?,
10367                }
10368            }
10369
10370            Ok(Some(modifiers))
10371        } else {
10372            Ok(None)
10373        }
10374    }
10375
10376    /// Parse a parenthesized sub data type
10377    fn parse_sub_type<F>(&mut self, parent_type: F) -> Result<DataType, ParserError>
10378    where
10379        F: FnOnce(Box<DataType>) -> DataType,
10380    {
10381        self.expect_token(&Token::LParen)?;
10382        let inside_type = self.parse_data_type()?;
10383        self.expect_token(&Token::RParen)?;
10384        Ok(parent_type(inside_type.into()))
10385    }
10386
10387    /// Parse a DELETE statement, returning a `Box`ed SetExpr
10388    ///
10389    /// This is used to reduce the size of the stack frames in debug builds
10390    fn parse_delete_setexpr_boxed(&mut self) -> Result<Box<SetExpr>, ParserError> {
10391        Ok(Box::new(SetExpr::Delete(self.parse_delete()?)))
10392    }
10393
10394    pub fn parse_delete(&mut self) -> Result<Statement, ParserError> {
10395        let (tables, with_from_keyword) = if !self.parse_keyword(Keyword::FROM) {
10396            // `FROM` keyword is optional in BigQuery SQL.
10397            // https://cloud.google.com/bigquery/docs/reference/standard-sql/dml-syntax#delete_statement
10398            if dialect_of!(self is BigQueryDialect | GenericDialect) {
10399                (vec![], false)
10400            } else {
10401                let tables = self.parse_comma_separated(|p| p.parse_object_name(false))?;
10402                self.expect_keyword_is(Keyword::FROM)?;
10403                (tables, true)
10404            }
10405        } else {
10406            (vec![], true)
10407        };
10408
10409        let from = self.parse_comma_separated(Parser::parse_table_and_joins)?;
10410        let using = if self.parse_keyword(Keyword::USING) {
10411            Some(self.parse_comma_separated(Parser::parse_table_and_joins)?)
10412        } else {
10413            None
10414        };
10415        let selection = if self.parse_keyword(Keyword::WHERE) {
10416            Some(self.parse_expr()?)
10417        } else {
10418            None
10419        };
10420        let returning = if self.parse_keyword(Keyword::RETURNING) {
10421            Some(self.parse_comma_separated(Parser::parse_select_item)?)
10422        } else {
10423            None
10424        };
10425        let order_by = if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
10426            self.parse_comma_separated(Parser::parse_order_by_expr)?
10427        } else {
10428            vec![]
10429        };
10430        let limit = if self.parse_keyword(Keyword::LIMIT) {
10431            self.parse_limit()?
10432        } else {
10433            None
10434        };
10435
10436        Ok(Statement::Delete(Delete {
10437            tables,
10438            from: if with_from_keyword {
10439                FromTable::WithFromKeyword(from)
10440            } else {
10441                FromTable::WithoutKeyword(from)
10442            },
10443            using,
10444            selection,
10445            returning,
10446            order_by,
10447            limit,
10448        }))
10449    }
10450
10451    // KILL [CONNECTION | QUERY | MUTATION] processlist_id
10452    pub fn parse_kill(&mut self) -> Result<Statement, ParserError> {
10453        let modifier_keyword =
10454            self.parse_one_of_keywords(&[Keyword::CONNECTION, Keyword::QUERY, Keyword::MUTATION]);
10455
10456        let id = self.parse_literal_uint()?;
10457
10458        let modifier = match modifier_keyword {
10459            Some(Keyword::CONNECTION) => Some(KillType::Connection),
10460            Some(Keyword::QUERY) => Some(KillType::Query),
10461            Some(Keyword::MUTATION) => {
10462                if dialect_of!(self is ClickHouseDialect | GenericDialect) {
10463                    Some(KillType::Mutation)
10464                } else {
10465                    self.expected(
10466                        "Unsupported type for KILL, allowed: CONNECTION | QUERY",
10467                        self.peek_token(),
10468                    )?
10469                }
10470            }
10471            _ => None,
10472        };
10473
10474        Ok(Statement::Kill { modifier, id })
10475    }
10476
10477    pub fn parse_explain(
10478        &mut self,
10479        describe_alias: DescribeAlias,
10480    ) -> Result<Statement, ParserError> {
10481        let mut analyze = false;
10482        let mut verbose = false;
10483        let mut query_plan = false;
10484        let mut estimate = false;
10485        let mut format = None;
10486        let mut options = None;
10487
10488        // Note: DuckDB is compatible with PostgreSQL syntax for this statement,
10489        // although not all features may be implemented.
10490        if describe_alias == DescribeAlias::Explain
10491            && self.dialect.supports_explain_with_utility_options()
10492            && self.peek_token().token == Token::LParen
10493        {
10494            options = Some(self.parse_utility_options()?)
10495        } else if self.parse_keywords(&[Keyword::QUERY, Keyword::PLAN]) {
10496            query_plan = true;
10497        } else if self.parse_keyword(Keyword::ESTIMATE) {
10498            estimate = true;
10499        } else {
10500            analyze = self.parse_keyword(Keyword::ANALYZE);
10501            verbose = self.parse_keyword(Keyword::VERBOSE);
10502            if self.parse_keyword(Keyword::FORMAT) {
10503                format = Some(self.parse_analyze_format()?);
10504            }
10505        }
10506
10507        match self.maybe_parse(|parser| parser.parse_statement())? {
10508            Some(Statement::Explain { .. }) | Some(Statement::ExplainTable { .. }) => Err(
10509                ParserError::ParserError("Explain must be root of the plan".to_string()),
10510            ),
10511            Some(statement) => Ok(Statement::Explain {
10512                describe_alias,
10513                analyze,
10514                verbose,
10515                query_plan,
10516                estimate,
10517                statement: Box::new(statement),
10518                format,
10519                options,
10520            }),
10521            _ => {
10522                let hive_format =
10523                    match self.parse_one_of_keywords(&[Keyword::EXTENDED, Keyword::FORMATTED]) {
10524                        Some(Keyword::EXTENDED) => Some(HiveDescribeFormat::Extended),
10525                        Some(Keyword::FORMATTED) => Some(HiveDescribeFormat::Formatted),
10526                        _ => None,
10527                    };
10528
10529                let has_table_keyword = if self.dialect.describe_requires_table_keyword() {
10530                    // only allow to use TABLE keyword for DESC|DESCRIBE statement
10531                    self.parse_keyword(Keyword::TABLE)
10532                } else {
10533                    false
10534                };
10535
10536                let table_name = self.parse_object_name(false)?;
10537                Ok(Statement::ExplainTable {
10538                    describe_alias,
10539                    hive_format,
10540                    has_table_keyword,
10541                    table_name,
10542                })
10543            }
10544        }
10545    }
10546
10547    /// Parse a query expression, i.e. a `SELECT` statement optionally
10548    /// preceded with some `WITH` CTE declarations and optionally followed
10549    /// by `ORDER BY`. Unlike some other parse_... methods, this one doesn't
10550    /// expect the initial keyword to be already consumed
10551    pub fn parse_query(&mut self) -> Result<Box<Query>, ParserError> {
10552        let _guard = self.recursion_counter.try_decrease()?;
10553        let with = if self.parse_keyword(Keyword::WITH) {
10554            let with_token = self.get_current_token();
10555            Some(With {
10556                with_token: with_token.clone().into(),
10557                recursive: self.parse_keyword(Keyword::RECURSIVE),
10558                cte_tables: self.parse_comma_separated(Parser::parse_cte)?,
10559            })
10560        } else {
10561            None
10562        };
10563        if self.parse_keyword(Keyword::INSERT) {
10564            Ok(Query {
10565                with,
10566                body: self.parse_insert_setexpr_boxed()?,
10567                order_by: None,
10568                limit_clause: None,
10569                fetch: None,
10570                locks: vec![],
10571                for_clause: None,
10572                settings: None,
10573                format_clause: None,
10574            }
10575            .into())
10576        } else if self.parse_keyword(Keyword::UPDATE) {
10577            Ok(Query {
10578                with,
10579                body: self.parse_update_setexpr_boxed()?,
10580                order_by: None,
10581                limit_clause: None,
10582                fetch: None,
10583                locks: vec![],
10584                for_clause: None,
10585                settings: None,
10586                format_clause: None,
10587            }
10588            .into())
10589        } else if self.parse_keyword(Keyword::DELETE) {
10590            Ok(Query {
10591                with,
10592                body: self.parse_delete_setexpr_boxed()?,
10593                limit_clause: None,
10594                order_by: None,
10595                fetch: None,
10596                locks: vec![],
10597                for_clause: None,
10598                settings: None,
10599                format_clause: None,
10600            }
10601            .into())
10602        } else {
10603            let body = self.parse_query_body(self.dialect.prec_unknown())?;
10604
10605            let order_by = self.parse_optional_order_by()?;
10606
10607            let limit_clause = self.parse_optional_limit_clause()?;
10608
10609            let settings = self.parse_settings()?;
10610
10611            let fetch = if self.parse_keyword(Keyword::FETCH) {
10612                Some(self.parse_fetch()?)
10613            } else {
10614                None
10615            };
10616
10617            let mut for_clause = None;
10618            let mut locks = Vec::new();
10619            while self.parse_keyword(Keyword::FOR) {
10620                if let Some(parsed_for_clause) = self.parse_for_clause()? {
10621                    for_clause = Some(parsed_for_clause);
10622                    break;
10623                } else {
10624                    locks.push(self.parse_lock()?);
10625                }
10626            }
10627            let format_clause = if dialect_of!(self is ClickHouseDialect | GenericDialect)
10628                && self.parse_keyword(Keyword::FORMAT)
10629            {
10630                if self.parse_keyword(Keyword::NULL) {
10631                    Some(FormatClause::Null)
10632                } else {
10633                    let ident = self.parse_identifier()?;
10634                    Some(FormatClause::Identifier(ident))
10635                }
10636            } else {
10637                None
10638            };
10639
10640            Ok(Query {
10641                with,
10642                body,
10643                order_by,
10644                limit_clause,
10645                fetch,
10646                locks,
10647                for_clause,
10648                settings,
10649                format_clause,
10650            }
10651            .into())
10652        }
10653    }
10654
10655    fn parse_settings(&mut self) -> Result<Option<Vec<Setting>>, ParserError> {
10656        let settings = if dialect_of!(self is ClickHouseDialect|GenericDialect)
10657            && self.parse_keyword(Keyword::SETTINGS)
10658        {
10659            let key_values = self.parse_comma_separated(|p| {
10660                let key = p.parse_identifier()?;
10661                p.expect_token(&Token::Eq)?;
10662                let value = p.parse_value()?.value;
10663                Ok(Setting { key, value })
10664            })?;
10665            Some(key_values)
10666        } else {
10667            None
10668        };
10669        Ok(settings)
10670    }
10671
10672    /// Parse a mssql `FOR [XML | JSON | BROWSE]` clause
10673    pub fn parse_for_clause(&mut self) -> Result<Option<ForClause>, ParserError> {
10674        if self.parse_keyword(Keyword::XML) {
10675            Ok(Some(self.parse_for_xml()?))
10676        } else if self.parse_keyword(Keyword::JSON) {
10677            Ok(Some(self.parse_for_json()?))
10678        } else if self.parse_keyword(Keyword::BROWSE) {
10679            Ok(Some(ForClause::Browse))
10680        } else {
10681            Ok(None)
10682        }
10683    }
10684
10685    /// Parse a mssql `FOR XML` clause
10686    pub fn parse_for_xml(&mut self) -> Result<ForClause, ParserError> {
10687        let for_xml = if self.parse_keyword(Keyword::RAW) {
10688            let mut element_name = None;
10689            if self.peek_token().token == Token::LParen {
10690                self.expect_token(&Token::LParen)?;
10691                element_name = Some(self.parse_literal_string()?);
10692                self.expect_token(&Token::RParen)?;
10693            }
10694            ForXml::Raw(element_name)
10695        } else if self.parse_keyword(Keyword::AUTO) {
10696            ForXml::Auto
10697        } else if self.parse_keyword(Keyword::EXPLICIT) {
10698            ForXml::Explicit
10699        } else if self.parse_keyword(Keyword::PATH) {
10700            let mut element_name = None;
10701            if self.peek_token().token == Token::LParen {
10702                self.expect_token(&Token::LParen)?;
10703                element_name = Some(self.parse_literal_string()?);
10704                self.expect_token(&Token::RParen)?;
10705            }
10706            ForXml::Path(element_name)
10707        } else {
10708            return Err(ParserError::ParserError(
10709                "Expected FOR XML [RAW | AUTO | EXPLICIT | PATH ]".to_string(),
10710            ));
10711        };
10712        let mut elements = false;
10713        let mut binary_base64 = false;
10714        let mut root = None;
10715        let mut r#type = false;
10716        while self.peek_token().token == Token::Comma {
10717            self.next_token();
10718            if self.parse_keyword(Keyword::ELEMENTS) {
10719                elements = true;
10720            } else if self.parse_keyword(Keyword::BINARY) {
10721                self.expect_keyword_is(Keyword::BASE64)?;
10722                binary_base64 = true;
10723            } else if self.parse_keyword(Keyword::ROOT) {
10724                self.expect_token(&Token::LParen)?;
10725                root = Some(self.parse_literal_string()?);
10726                self.expect_token(&Token::RParen)?;
10727            } else if self.parse_keyword(Keyword::TYPE) {
10728                r#type = true;
10729            }
10730        }
10731        Ok(ForClause::Xml {
10732            for_xml,
10733            elements,
10734            binary_base64,
10735            root,
10736            r#type,
10737        })
10738    }
10739
10740    /// Parse a mssql `FOR JSON` clause
10741    pub fn parse_for_json(&mut self) -> Result<ForClause, ParserError> {
10742        let for_json = if self.parse_keyword(Keyword::AUTO) {
10743            ForJson::Auto
10744        } else if self.parse_keyword(Keyword::PATH) {
10745            ForJson::Path
10746        } else {
10747            return Err(ParserError::ParserError(
10748                "Expected FOR JSON [AUTO | PATH ]".to_string(),
10749            ));
10750        };
10751        let mut root = None;
10752        let mut include_null_values = false;
10753        let mut without_array_wrapper = false;
10754        while self.peek_token().token == Token::Comma {
10755            self.next_token();
10756            if self.parse_keyword(Keyword::ROOT) {
10757                self.expect_token(&Token::LParen)?;
10758                root = Some(self.parse_literal_string()?);
10759                self.expect_token(&Token::RParen)?;
10760            } else if self.parse_keyword(Keyword::INCLUDE_NULL_VALUES) {
10761                include_null_values = true;
10762            } else if self.parse_keyword(Keyword::WITHOUT_ARRAY_WRAPPER) {
10763                without_array_wrapper = true;
10764            }
10765        }
10766        Ok(ForClause::Json {
10767            for_json,
10768            root,
10769            include_null_values,
10770            without_array_wrapper,
10771        })
10772    }
10773
10774    /// Parse a CTE (`alias [( col1, col2, ... )] AS (subquery)`)
10775    pub fn parse_cte(&mut self) -> Result<Cte, ParserError> {
10776        let name = self.parse_identifier()?;
10777
10778        let mut cte = if self.parse_keyword(Keyword::AS) {
10779            let mut is_materialized = None;
10780            if dialect_of!(self is PostgreSqlDialect) {
10781                if self.parse_keyword(Keyword::MATERIALIZED) {
10782                    is_materialized = Some(CteAsMaterialized::Materialized);
10783                } else if self.parse_keywords(&[Keyword::NOT, Keyword::MATERIALIZED]) {
10784                    is_materialized = Some(CteAsMaterialized::NotMaterialized);
10785                }
10786            }
10787            self.expect_token(&Token::LParen)?;
10788
10789            let query = self.parse_query()?;
10790            let closing_paren_token = self.expect_token(&Token::RParen)?;
10791
10792            let alias = TableAlias {
10793                name,
10794                columns: vec![],
10795            };
10796            Cte {
10797                alias,
10798                query,
10799                from: None,
10800                materialized: is_materialized,
10801                closing_paren_token: closing_paren_token.into(),
10802            }
10803        } else {
10804            let columns = self.parse_table_alias_column_defs()?;
10805            self.expect_keyword_is(Keyword::AS)?;
10806            let mut is_materialized = None;
10807            if dialect_of!(self is PostgreSqlDialect) {
10808                if self.parse_keyword(Keyword::MATERIALIZED) {
10809                    is_materialized = Some(CteAsMaterialized::Materialized);
10810                } else if self.parse_keywords(&[Keyword::NOT, Keyword::MATERIALIZED]) {
10811                    is_materialized = Some(CteAsMaterialized::NotMaterialized);
10812                }
10813            }
10814            self.expect_token(&Token::LParen)?;
10815
10816            let query = self.parse_query()?;
10817            let closing_paren_token = self.expect_token(&Token::RParen)?;
10818
10819            let alias = TableAlias { name, columns };
10820            Cte {
10821                alias,
10822                query,
10823                from: None,
10824                materialized: is_materialized,
10825                closing_paren_token: closing_paren_token.into(),
10826            }
10827        };
10828        if self.parse_keyword(Keyword::FROM) {
10829            cte.from = Some(self.parse_identifier()?);
10830        }
10831        Ok(cte)
10832    }
10833
10834    /// Parse a "query body", which is an expression with roughly the
10835    /// following grammar:
10836    /// ```sql
10837    ///   query_body ::= restricted_select | '(' subquery ')' | set_operation
10838    ///   restricted_select ::= 'SELECT' [expr_list] [ from ] [ where ] [ groupby_having ]
10839    ///   subquery ::= query_body [ order_by_limit ]
10840    ///   set_operation ::= query_body { 'UNION' | 'EXCEPT' | 'INTERSECT' } [ 'ALL' ] query_body
10841    /// ```
10842    pub fn parse_query_body(&mut self, precedence: u8) -> Result<Box<SetExpr>, ParserError> {
10843        // We parse the expression using a Pratt parser, as in `parse_expr()`.
10844        // Start by parsing a restricted SELECT or a `(subquery)`:
10845        let expr = if self.peek_keyword(Keyword::SELECT)
10846            || (self.peek_keyword(Keyword::FROM) && self.dialect.supports_from_first_select())
10847        {
10848            SetExpr::Select(self.parse_select().map(Box::new)?)
10849        } else if self.consume_token(&Token::LParen) {
10850            // CTEs are not allowed here, but the parser currently accepts them
10851            let subquery = self.parse_query()?;
10852            self.expect_token(&Token::RParen)?;
10853            SetExpr::Query(subquery)
10854        } else if self.parse_keyword(Keyword::VALUES) {
10855            let is_mysql = dialect_of!(self is MySqlDialect);
10856            SetExpr::Values(self.parse_values(is_mysql)?)
10857        } else if self.parse_keyword(Keyword::TABLE) {
10858            SetExpr::Table(Box::new(self.parse_as_table()?))
10859        } else {
10860            return self.expected(
10861                "SELECT, VALUES, or a subquery in the query body",
10862                self.peek_token(),
10863            );
10864        };
10865
10866        self.parse_remaining_set_exprs(expr, precedence)
10867    }
10868
10869    /// Parse any extra set expressions that may be present in a query body
10870    ///
10871    /// (this is its own function to reduce required stack size in debug builds)
10872    fn parse_remaining_set_exprs(
10873        &mut self,
10874        mut expr: SetExpr,
10875        precedence: u8,
10876    ) -> Result<Box<SetExpr>, ParserError> {
10877        loop {
10878            // The query can be optionally followed by a set operator:
10879            let op = self.parse_set_operator(&self.peek_token().token);
10880            let next_precedence = match op {
10881                // UNION and EXCEPT have the same binding power and evaluate left-to-right
10882                Some(SetOperator::Union) | Some(SetOperator::Except) | Some(SetOperator::Minus) => {
10883                    10
10884                }
10885                // INTERSECT has higher precedence than UNION/EXCEPT
10886                Some(SetOperator::Intersect) => 20,
10887                // Unexpected token or EOF => stop parsing the query body
10888                None => break,
10889            };
10890            if precedence >= next_precedence {
10891                break;
10892            }
10893            self.next_token(); // skip past the set operator
10894            let set_quantifier = self.parse_set_quantifier(&op);
10895            expr = SetExpr::SetOperation {
10896                left: Box::new(expr),
10897                op: op.unwrap(),
10898                set_quantifier,
10899                right: self.parse_query_body(next_precedence)?,
10900            };
10901        }
10902
10903        Ok(expr.into())
10904    }
10905
10906    pub fn parse_set_operator(&mut self, token: &Token) -> Option<SetOperator> {
10907        match token {
10908            Token::Word(w) if w.keyword == Keyword::UNION => Some(SetOperator::Union),
10909            Token::Word(w) if w.keyword == Keyword::EXCEPT => Some(SetOperator::Except),
10910            Token::Word(w) if w.keyword == Keyword::INTERSECT => Some(SetOperator::Intersect),
10911            Token::Word(w) if w.keyword == Keyword::MINUS => Some(SetOperator::Minus),
10912            _ => None,
10913        }
10914    }
10915
10916    pub fn parse_set_quantifier(&mut self, op: &Option<SetOperator>) -> SetQuantifier {
10917        match op {
10918            Some(
10919                SetOperator::Except
10920                | SetOperator::Intersect
10921                | SetOperator::Union
10922                | SetOperator::Minus,
10923            ) => {
10924                if self.parse_keywords(&[Keyword::DISTINCT, Keyword::BY, Keyword::NAME]) {
10925                    SetQuantifier::DistinctByName
10926                } else if self.parse_keywords(&[Keyword::BY, Keyword::NAME]) {
10927                    SetQuantifier::ByName
10928                } else if self.parse_keyword(Keyword::ALL) {
10929                    if self.parse_keywords(&[Keyword::BY, Keyword::NAME]) {
10930                        SetQuantifier::AllByName
10931                    } else {
10932                        SetQuantifier::All
10933                    }
10934                } else if self.parse_keyword(Keyword::DISTINCT) {
10935                    SetQuantifier::Distinct
10936                } else {
10937                    SetQuantifier::None
10938                }
10939            }
10940            _ => SetQuantifier::None,
10941        }
10942    }
10943
10944    /// Parse a restricted `SELECT` statement (no CTEs / `UNION` / `ORDER BY`)
10945    pub fn parse_select(&mut self) -> Result<Select, ParserError> {
10946        let mut from_first = None;
10947
10948        if self.dialect.supports_from_first_select() && self.peek_keyword(Keyword::FROM) {
10949            let from_token = self.expect_keyword(Keyword::FROM)?;
10950            let from = self.parse_table_with_joins()?;
10951            if !self.peek_keyword(Keyword::SELECT) {
10952                return Ok(Select {
10953                    select_token: AttachedToken(from_token),
10954                    distinct: None,
10955                    top: None,
10956                    top_before_distinct: false,
10957                    projection: vec![],
10958                    into: None,
10959                    from,
10960                    lateral_views: vec![],
10961                    prewhere: None,
10962                    selection: None,
10963                    group_by: GroupByExpr::Expressions(vec![], vec![]),
10964                    cluster_by: vec![],
10965                    distribute_by: vec![],
10966                    sort_by: vec![],
10967                    having: None,
10968                    named_window: vec![],
10969                    window_before_qualify: false,
10970                    qualify: None,
10971                    value_table_mode: None,
10972                    connect_by: None,
10973                    flavor: SelectFlavor::FromFirstNoSelect,
10974                });
10975            }
10976            from_first = Some(from);
10977        }
10978
10979        let select_token = self.expect_keyword(Keyword::SELECT)?;
10980        let value_table_mode =
10981            if dialect_of!(self is BigQueryDialect) && self.parse_keyword(Keyword::AS) {
10982                if self.parse_keyword(Keyword::VALUE) {
10983                    Some(ValueTableMode::AsValue)
10984                } else if self.parse_keyword(Keyword::STRUCT) {
10985                    Some(ValueTableMode::AsStruct)
10986                } else {
10987                    self.expected("VALUE or STRUCT", self.peek_token())?
10988                }
10989            } else {
10990                None
10991            };
10992
10993        let mut top_before_distinct = false;
10994        let mut top = None;
10995        if self.dialect.supports_top_before_distinct() && self.parse_keyword(Keyword::TOP) {
10996            top = Some(self.parse_top()?);
10997            top_before_distinct = true;
10998        }
10999        let distinct = self.parse_all_or_distinct()?;
11000        if !self.dialect.supports_top_before_distinct() && self.parse_keyword(Keyword::TOP) {
11001            top = Some(self.parse_top()?);
11002        }
11003
11004        let projection =
11005            if self.dialect.supports_empty_projections() && self.peek_keyword(Keyword::FROM) {
11006                vec![]
11007            } else {
11008                self.parse_projection()?
11009            };
11010
11011        let into = if self.parse_keyword(Keyword::INTO) {
11012            Some(self.parse_select_into()?)
11013        } else {
11014            None
11015        };
11016
11017        // Note that for keywords to be properly handled here, they need to be
11018        // added to `RESERVED_FOR_COLUMN_ALIAS` / `RESERVED_FOR_TABLE_ALIAS`,
11019        // otherwise they may be parsed as an alias as part of the `projection`
11020        // or `from`.
11021
11022        let (from, from_first) = if let Some(from) = from_first.take() {
11023            (from, true)
11024        } else if self.parse_keyword(Keyword::FROM) {
11025            (self.parse_table_with_joins()?, false)
11026        } else {
11027            (vec![], false)
11028        };
11029
11030        let mut lateral_views = vec![];
11031        loop {
11032            if self.parse_keywords(&[Keyword::LATERAL, Keyword::VIEW]) {
11033                let outer = self.parse_keyword(Keyword::OUTER);
11034                let lateral_view = self.parse_expr()?;
11035                let lateral_view_name = self.parse_object_name(false)?;
11036                let lateral_col_alias = self
11037                    .parse_comma_separated(|parser| {
11038                        parser.parse_optional_alias(&[
11039                            Keyword::WHERE,
11040                            Keyword::GROUP,
11041                            Keyword::CLUSTER,
11042                            Keyword::HAVING,
11043                            Keyword::LATERAL,
11044                        ]) // This couldn't possibly be a bad idea
11045                    })?
11046                    .into_iter()
11047                    .flatten()
11048                    .collect();
11049
11050                lateral_views.push(LateralView {
11051                    lateral_view,
11052                    lateral_view_name,
11053                    lateral_col_alias,
11054                    outer,
11055                });
11056            } else {
11057                break;
11058            }
11059        }
11060
11061        let prewhere = if dialect_of!(self is ClickHouseDialect|GenericDialect)
11062            && self.parse_keyword(Keyword::PREWHERE)
11063        {
11064            Some(self.parse_expr()?)
11065        } else {
11066            None
11067        };
11068
11069        let selection = if self.parse_keyword(Keyword::WHERE) {
11070            Some(self.parse_expr()?)
11071        } else {
11072            None
11073        };
11074
11075        let group_by = self
11076            .parse_optional_group_by()?
11077            .unwrap_or_else(|| GroupByExpr::Expressions(vec![], vec![]));
11078
11079        let cluster_by = if self.parse_keywords(&[Keyword::CLUSTER, Keyword::BY]) {
11080            self.parse_comma_separated(Parser::parse_expr)?
11081        } else {
11082            vec![]
11083        };
11084
11085        let distribute_by = if self.parse_keywords(&[Keyword::DISTRIBUTE, Keyword::BY]) {
11086            self.parse_comma_separated(Parser::parse_expr)?
11087        } else {
11088            vec![]
11089        };
11090
11091        let sort_by = if self.parse_keywords(&[Keyword::SORT, Keyword::BY]) {
11092            self.parse_comma_separated(Parser::parse_expr)?
11093        } else {
11094            vec![]
11095        };
11096
11097        let having = if self.parse_keyword(Keyword::HAVING) {
11098            Some(self.parse_expr()?)
11099        } else {
11100            None
11101        };
11102
11103        // Accept QUALIFY and WINDOW in any order and flag accordingly.
11104        let (named_windows, qualify, window_before_qualify) = if self.parse_keyword(Keyword::WINDOW)
11105        {
11106            let named_windows = self.parse_comma_separated(Parser::parse_named_window)?;
11107            if self.parse_keyword(Keyword::QUALIFY) {
11108                (named_windows, Some(self.parse_expr()?), true)
11109            } else {
11110                (named_windows, None, true)
11111            }
11112        } else if self.parse_keyword(Keyword::QUALIFY) {
11113            let qualify = Some(self.parse_expr()?);
11114            if self.parse_keyword(Keyword::WINDOW) {
11115                (
11116                    self.parse_comma_separated(Parser::parse_named_window)?,
11117                    qualify,
11118                    false,
11119                )
11120            } else {
11121                (Default::default(), qualify, false)
11122            }
11123        } else {
11124            Default::default()
11125        };
11126
11127        let connect_by = if self.dialect.supports_connect_by()
11128            && self
11129                .parse_one_of_keywords(&[Keyword::START, Keyword::CONNECT])
11130                .is_some()
11131        {
11132            self.prev_token();
11133            Some(self.parse_connect_by()?)
11134        } else {
11135            None
11136        };
11137
11138        Ok(Select {
11139            select_token: AttachedToken(select_token),
11140            distinct,
11141            top,
11142            top_before_distinct,
11143            projection,
11144            into,
11145            from,
11146            lateral_views,
11147            prewhere,
11148            selection,
11149            group_by,
11150            cluster_by,
11151            distribute_by,
11152            sort_by,
11153            having,
11154            named_window: named_windows,
11155            window_before_qualify,
11156            qualify,
11157            value_table_mode,
11158            connect_by,
11159            flavor: if from_first {
11160                SelectFlavor::FromFirst
11161            } else {
11162                SelectFlavor::Standard
11163            },
11164        })
11165    }
11166
11167    /// Invoke `f` after first setting the parser's `ParserState` to `state`.
11168    ///
11169    /// Upon return, restores the parser's state to what it started at.
11170    fn with_state<T, F>(&mut self, state: ParserState, mut f: F) -> Result<T, ParserError>
11171    where
11172        F: FnMut(&mut Parser) -> Result<T, ParserError>,
11173    {
11174        let current_state = self.state;
11175        self.state = state;
11176        let res = f(self);
11177        self.state = current_state;
11178        res
11179    }
11180
11181    pub fn parse_connect_by(&mut self) -> Result<ConnectBy, ParserError> {
11182        let (condition, relationships) = if self.parse_keywords(&[Keyword::CONNECT, Keyword::BY]) {
11183            let relationships = self.with_state(ParserState::ConnectBy, |parser| {
11184                parser.parse_comma_separated(Parser::parse_expr)
11185            })?;
11186            self.expect_keywords(&[Keyword::START, Keyword::WITH])?;
11187            let condition = self.parse_expr()?;
11188            (condition, relationships)
11189        } else {
11190            self.expect_keywords(&[Keyword::START, Keyword::WITH])?;
11191            let condition = self.parse_expr()?;
11192            self.expect_keywords(&[Keyword::CONNECT, Keyword::BY])?;
11193            let relationships = self.with_state(ParserState::ConnectBy, |parser| {
11194                parser.parse_comma_separated(Parser::parse_expr)
11195            })?;
11196            (condition, relationships)
11197        };
11198        Ok(ConnectBy {
11199            condition,
11200            relationships,
11201        })
11202    }
11203
11204    /// Parse `CREATE TABLE x AS TABLE y`
11205    pub fn parse_as_table(&mut self) -> Result<Table, ParserError> {
11206        let token1 = self.next_token();
11207        let token2 = self.next_token();
11208        let token3 = self.next_token();
11209
11210        let table_name;
11211        let schema_name;
11212        if token2 == Token::Period {
11213            match token1.token {
11214                Token::Word(w) => {
11215                    schema_name = w.value;
11216                }
11217                _ => {
11218                    return self.expected("Schema name", token1);
11219                }
11220            }
11221            match token3.token {
11222                Token::Word(w) => {
11223                    table_name = w.value;
11224                }
11225                _ => {
11226                    return self.expected("Table name", token3);
11227                }
11228            }
11229            Ok(Table {
11230                table_name: Some(table_name),
11231                schema_name: Some(schema_name),
11232            })
11233        } else {
11234            match token1.token {
11235                Token::Word(w) => {
11236                    table_name = w.value;
11237                }
11238                _ => {
11239                    return self.expected("Table name", token1);
11240                }
11241            }
11242            Ok(Table {
11243                table_name: Some(table_name),
11244                schema_name: None,
11245            })
11246        }
11247    }
11248
11249    /// Parse a `SET ROLE` statement. Expects SET to be consumed already.
11250    fn parse_set_role(
11251        &mut self,
11252        modifier: Option<ContextModifier>,
11253    ) -> Result<Statement, ParserError> {
11254        self.expect_keyword_is(Keyword::ROLE)?;
11255
11256        let role_name = if self.parse_keyword(Keyword::NONE) {
11257            None
11258        } else {
11259            Some(self.parse_identifier()?)
11260        };
11261        Ok(Statement::Set(Set::SetRole {
11262            context_modifier: modifier,
11263            role_name,
11264        }))
11265    }
11266
11267    fn parse_set_values(
11268        &mut self,
11269        parenthesized_assignment: bool,
11270    ) -> Result<Vec<Expr>, ParserError> {
11271        let mut values = vec![];
11272
11273        if parenthesized_assignment {
11274            self.expect_token(&Token::LParen)?;
11275        }
11276
11277        loop {
11278            let value = if let Some(expr) = self.try_parse_expr_sub_query()? {
11279                expr
11280            } else if let Ok(expr) = self.parse_expr() {
11281                expr
11282            } else {
11283                self.expected("variable value", self.peek_token())?
11284            };
11285
11286            values.push(value);
11287            if self.consume_token(&Token::Comma) {
11288                continue;
11289            }
11290
11291            if parenthesized_assignment {
11292                self.expect_token(&Token::RParen)?;
11293            }
11294            return Ok(values);
11295        }
11296    }
11297
11298    fn parse_context_modifier(&mut self) -> Option<ContextModifier> {
11299        let modifier =
11300            self.parse_one_of_keywords(&[Keyword::SESSION, Keyword::LOCAL, Keyword::GLOBAL])?;
11301
11302        Self::keyword_to_modifier(modifier)
11303    }
11304
11305    /// Parse a single SET statement assignment `var = expr`.
11306    fn parse_set_assignment(&mut self) -> Result<SetAssignment, ParserError> {
11307        let scope = self.parse_context_modifier();
11308
11309        let name = if self.dialect.supports_parenthesized_set_variables()
11310            && self.consume_token(&Token::LParen)
11311        {
11312            // Parenthesized assignments are handled in the `parse_set` function after
11313            // trying to parse list of assignments using this function.
11314            // If a dialect supports both, and we find a LParen, we early exit from this function.
11315            self.expected("Unparenthesized assignment", self.peek_token())?
11316        } else {
11317            self.parse_object_name(false)?
11318        };
11319
11320        if !(self.consume_token(&Token::Eq) || self.parse_keyword(Keyword::TO)) {
11321            return self.expected("assignment operator", self.peek_token());
11322        }
11323
11324        let value = self.parse_expr()?;
11325
11326        Ok(SetAssignment { scope, name, value })
11327    }
11328
11329    fn parse_set(&mut self) -> Result<Statement, ParserError> {
11330        let hivevar = self.parse_keyword(Keyword::HIVEVAR);
11331
11332        // Modifier is either HIVEVAR: or a ContextModifier (LOCAL, SESSION, etc), not both
11333        let scope = if !hivevar {
11334            self.parse_context_modifier()
11335        } else {
11336            None
11337        };
11338
11339        if hivevar {
11340            self.expect_token(&Token::Colon)?;
11341        }
11342
11343        if let Some(set_role_stmt) = self.maybe_parse(|parser| parser.parse_set_role(scope))? {
11344            return Ok(set_role_stmt);
11345        }
11346
11347        // Handle special cases first
11348        if self.parse_keywords(&[Keyword::TIME, Keyword::ZONE])
11349            || self.parse_keyword(Keyword::TIMEZONE)
11350        {
11351            if self.consume_token(&Token::Eq) || self.parse_keyword(Keyword::TO) {
11352                return Ok(Set::SingleAssignment {
11353                    scope,
11354                    hivevar,
11355                    variable: ObjectName::from(vec!["TIMEZONE".into()]),
11356                    values: self.parse_set_values(false)?,
11357                }
11358                .into());
11359            } else {
11360                // A shorthand alias for SET TIME ZONE that doesn't require
11361                // the assignment operator. It's originally PostgreSQL specific,
11362                // but we allow it for all the dialects
11363                return Ok(Set::SetTimeZone {
11364                    local: scope == Some(ContextModifier::Local),
11365                    value: self.parse_expr()?,
11366                }
11367                .into());
11368            }
11369        } else if self.dialect.supports_set_names() && self.parse_keyword(Keyword::NAMES) {
11370            if self.parse_keyword(Keyword::DEFAULT) {
11371                return Ok(Set::SetNamesDefault {}.into());
11372            }
11373            let charset_name = self.parse_identifier()?;
11374            let collation_name = if self.parse_one_of_keywords(&[Keyword::COLLATE]).is_some() {
11375                Some(self.parse_literal_string()?)
11376            } else {
11377                None
11378            };
11379
11380            return Ok(Set::SetNames {
11381                charset_name,
11382                collation_name,
11383            }
11384            .into());
11385        } else if self.parse_keyword(Keyword::CHARACTERISTICS) {
11386            self.expect_keywords(&[Keyword::AS, Keyword::TRANSACTION])?;
11387            return Ok(Set::SetTransaction {
11388                modes: self.parse_transaction_modes()?,
11389                snapshot: None,
11390                session: true,
11391            }
11392            .into());
11393        } else if self.parse_keyword(Keyword::TRANSACTION) {
11394            if self.parse_keyword(Keyword::SNAPSHOT) {
11395                let snapshot_id = self.parse_value()?.value;
11396                return Ok(Set::SetTransaction {
11397                    modes: vec![],
11398                    snapshot: Some(snapshot_id),
11399                    session: false,
11400                }
11401                .into());
11402            }
11403            return Ok(Set::SetTransaction {
11404                modes: self.parse_transaction_modes()?,
11405                snapshot: None,
11406                session: false,
11407            }
11408            .into());
11409        }
11410
11411        if self.dialect.supports_comma_separated_set_assignments() {
11412            if scope.is_some() {
11413                self.prev_token();
11414            }
11415
11416            if let Some(assignments) = self
11417                .maybe_parse(|parser| parser.parse_comma_separated(Parser::parse_set_assignment))?
11418            {
11419                return if assignments.len() > 1 {
11420                    Ok(Set::MultipleAssignments { assignments }.into())
11421                } else {
11422                    let SetAssignment { scope, name, value } =
11423                        assignments.into_iter().next().ok_or_else(|| {
11424                            ParserError::ParserError("Expected at least one assignment".to_string())
11425                        })?;
11426
11427                    Ok(Set::SingleAssignment {
11428                        scope,
11429                        hivevar,
11430                        variable: name,
11431                        values: vec![value],
11432                    }
11433                    .into())
11434                };
11435            }
11436        }
11437
11438        let variables = if self.dialect.supports_parenthesized_set_variables()
11439            && self.consume_token(&Token::LParen)
11440        {
11441            let vars = OneOrManyWithParens::Many(
11442                self.parse_comma_separated(|parser: &mut Parser<'a>| parser.parse_identifier())?
11443                    .into_iter()
11444                    .map(|ident| ObjectName::from(vec![ident]))
11445                    .collect(),
11446            );
11447            self.expect_token(&Token::RParen)?;
11448            vars
11449        } else {
11450            OneOrManyWithParens::One(self.parse_object_name(false)?)
11451        };
11452
11453        if self.consume_token(&Token::Eq) || self.parse_keyword(Keyword::TO) {
11454            let stmt = match variables {
11455                OneOrManyWithParens::One(var) => Set::SingleAssignment {
11456                    scope,
11457                    hivevar,
11458                    variable: var,
11459                    values: self.parse_set_values(false)?,
11460                },
11461                OneOrManyWithParens::Many(vars) => Set::ParenthesizedAssignments {
11462                    variables: vars,
11463                    values: self.parse_set_values(true)?,
11464                },
11465            };
11466
11467            return Ok(stmt.into());
11468        }
11469
11470        if self.dialect.supports_set_stmt_without_operator() {
11471            self.prev_token();
11472            return self.parse_set_session_params();
11473        };
11474
11475        self.expected("equals sign or TO", self.peek_token())
11476    }
11477
11478    pub fn parse_set_session_params(&mut self) -> Result<Statement, ParserError> {
11479        if self.parse_keyword(Keyword::STATISTICS) {
11480            let topic = match self.parse_one_of_keywords(&[
11481                Keyword::IO,
11482                Keyword::PROFILE,
11483                Keyword::TIME,
11484                Keyword::XML,
11485            ]) {
11486                Some(Keyword::IO) => SessionParamStatsTopic::IO,
11487                Some(Keyword::PROFILE) => SessionParamStatsTopic::Profile,
11488                Some(Keyword::TIME) => SessionParamStatsTopic::Time,
11489                Some(Keyword::XML) => SessionParamStatsTopic::Xml,
11490                _ => return self.expected("IO, PROFILE, TIME or XML", self.peek_token()),
11491            };
11492            let value = self.parse_session_param_value()?;
11493            Ok(
11494                Set::SetSessionParam(SetSessionParamKind::Statistics(SetSessionParamStatistics {
11495                    topic,
11496                    value,
11497                }))
11498                .into(),
11499            )
11500        } else if self.parse_keyword(Keyword::IDENTITY_INSERT) {
11501            let obj = self.parse_object_name(false)?;
11502            let value = self.parse_session_param_value()?;
11503            Ok(Set::SetSessionParam(SetSessionParamKind::IdentityInsert(
11504                SetSessionParamIdentityInsert { obj, value },
11505            ))
11506            .into())
11507        } else if self.parse_keyword(Keyword::OFFSETS) {
11508            let keywords = self.parse_comma_separated(|parser| {
11509                let next_token = parser.next_token();
11510                match &next_token.token {
11511                    Token::Word(w) => Ok(w.to_string()),
11512                    _ => parser.expected("SQL keyword", next_token),
11513                }
11514            })?;
11515            let value = self.parse_session_param_value()?;
11516            Ok(
11517                Set::SetSessionParam(SetSessionParamKind::Offsets(SetSessionParamOffsets {
11518                    keywords,
11519                    value,
11520                }))
11521                .into(),
11522            )
11523        } else {
11524            let names = self.parse_comma_separated(|parser| {
11525                let next_token = parser.next_token();
11526                match next_token.token {
11527                    Token::Word(w) => Ok(w.to_string()),
11528                    _ => parser.expected("Session param name", next_token),
11529                }
11530            })?;
11531            let value = self.parse_expr()?.to_string();
11532            Ok(
11533                Set::SetSessionParam(SetSessionParamKind::Generic(SetSessionParamGeneric {
11534                    names,
11535                    value,
11536                }))
11537                .into(),
11538            )
11539        }
11540    }
11541
11542    fn parse_session_param_value(&mut self) -> Result<SessionParamValue, ParserError> {
11543        if self.parse_keyword(Keyword::ON) {
11544            Ok(SessionParamValue::On)
11545        } else if self.parse_keyword(Keyword::OFF) {
11546            Ok(SessionParamValue::Off)
11547        } else {
11548            self.expected("ON or OFF", self.peek_token())
11549        }
11550    }
11551
11552    pub fn parse_show(&mut self) -> Result<Statement, ParserError> {
11553        let terse = self.parse_keyword(Keyword::TERSE);
11554        let extended = self.parse_keyword(Keyword::EXTENDED);
11555        let full = self.parse_keyword(Keyword::FULL);
11556        let session = self.parse_keyword(Keyword::SESSION);
11557        let global = self.parse_keyword(Keyword::GLOBAL);
11558        let external = self.parse_keyword(Keyword::EXTERNAL);
11559        if self
11560            .parse_one_of_keywords(&[Keyword::COLUMNS, Keyword::FIELDS])
11561            .is_some()
11562        {
11563            Ok(self.parse_show_columns(extended, full)?)
11564        } else if self.parse_keyword(Keyword::TABLES) {
11565            Ok(self.parse_show_tables(terse, extended, full, external)?)
11566        } else if self.parse_keywords(&[Keyword::MATERIALIZED, Keyword::VIEWS]) {
11567            Ok(self.parse_show_views(terse, true)?)
11568        } else if self.parse_keyword(Keyword::VIEWS) {
11569            Ok(self.parse_show_views(terse, false)?)
11570        } else if self.parse_keyword(Keyword::FUNCTIONS) {
11571            Ok(self.parse_show_functions()?)
11572        } else if extended || full {
11573            Err(ParserError::ParserError(
11574                "EXTENDED/FULL are not supported with this type of SHOW query".to_string(),
11575            ))
11576        } else if self.parse_one_of_keywords(&[Keyword::CREATE]).is_some() {
11577            Ok(self.parse_show_create()?)
11578        } else if self.parse_keyword(Keyword::COLLATION) {
11579            Ok(self.parse_show_collation()?)
11580        } else if self.parse_keyword(Keyword::VARIABLES)
11581            && dialect_of!(self is MySqlDialect | GenericDialect)
11582        {
11583            Ok(Statement::ShowVariables {
11584                filter: self.parse_show_statement_filter()?,
11585                session,
11586                global,
11587            })
11588        } else if self.parse_keyword(Keyword::STATUS)
11589            && dialect_of!(self is MySqlDialect | GenericDialect)
11590        {
11591            Ok(Statement::ShowStatus {
11592                filter: self.parse_show_statement_filter()?,
11593                session,
11594                global,
11595            })
11596        } else if self.parse_keyword(Keyword::DATABASES) {
11597            self.parse_show_databases(terse)
11598        } else if self.parse_keyword(Keyword::SCHEMAS) {
11599            self.parse_show_schemas(terse)
11600        } else {
11601            Ok(Statement::ShowVariable {
11602                variable: self.parse_identifiers()?,
11603            })
11604        }
11605    }
11606
11607    fn parse_show_databases(&mut self, terse: bool) -> Result<Statement, ParserError> {
11608        let history = self.parse_keyword(Keyword::HISTORY);
11609        let show_options = self.parse_show_stmt_options()?;
11610        Ok(Statement::ShowDatabases {
11611            terse,
11612            history,
11613            show_options,
11614        })
11615    }
11616
11617    fn parse_show_schemas(&mut self, terse: bool) -> Result<Statement, ParserError> {
11618        let history = self.parse_keyword(Keyword::HISTORY);
11619        let show_options = self.parse_show_stmt_options()?;
11620        Ok(Statement::ShowSchemas {
11621            terse,
11622            history,
11623            show_options,
11624        })
11625    }
11626
11627    pub fn parse_show_create(&mut self) -> Result<Statement, ParserError> {
11628        let obj_type = match self.expect_one_of_keywords(&[
11629            Keyword::TABLE,
11630            Keyword::TRIGGER,
11631            Keyword::FUNCTION,
11632            Keyword::PROCEDURE,
11633            Keyword::EVENT,
11634            Keyword::VIEW,
11635        ])? {
11636            Keyword::TABLE => Ok(ShowCreateObject::Table),
11637            Keyword::TRIGGER => Ok(ShowCreateObject::Trigger),
11638            Keyword::FUNCTION => Ok(ShowCreateObject::Function),
11639            Keyword::PROCEDURE => Ok(ShowCreateObject::Procedure),
11640            Keyword::EVENT => Ok(ShowCreateObject::Event),
11641            Keyword::VIEW => Ok(ShowCreateObject::View),
11642            keyword => Err(ParserError::ParserError(format!(
11643                "Unable to map keyword to ShowCreateObject: {keyword:?}"
11644            ))),
11645        }?;
11646
11647        let obj_name = self.parse_object_name(false)?;
11648
11649        Ok(Statement::ShowCreate { obj_type, obj_name })
11650    }
11651
11652    pub fn parse_show_columns(
11653        &mut self,
11654        extended: bool,
11655        full: bool,
11656    ) -> Result<Statement, ParserError> {
11657        let show_options = self.parse_show_stmt_options()?;
11658        Ok(Statement::ShowColumns {
11659            extended,
11660            full,
11661            show_options,
11662        })
11663    }
11664
11665    fn parse_show_tables(
11666        &mut self,
11667        terse: bool,
11668        extended: bool,
11669        full: bool,
11670        external: bool,
11671    ) -> Result<Statement, ParserError> {
11672        let history = !external && self.parse_keyword(Keyword::HISTORY);
11673        let show_options = self.parse_show_stmt_options()?;
11674        Ok(Statement::ShowTables {
11675            terse,
11676            history,
11677            extended,
11678            full,
11679            external,
11680            show_options,
11681        })
11682    }
11683
11684    fn parse_show_views(
11685        &mut self,
11686        terse: bool,
11687        materialized: bool,
11688    ) -> Result<Statement, ParserError> {
11689        let show_options = self.parse_show_stmt_options()?;
11690        Ok(Statement::ShowViews {
11691            materialized,
11692            terse,
11693            show_options,
11694        })
11695    }
11696
11697    pub fn parse_show_functions(&mut self) -> Result<Statement, ParserError> {
11698        let filter = self.parse_show_statement_filter()?;
11699        Ok(Statement::ShowFunctions { filter })
11700    }
11701
11702    pub fn parse_show_collation(&mut self) -> Result<Statement, ParserError> {
11703        let filter = self.parse_show_statement_filter()?;
11704        Ok(Statement::ShowCollation { filter })
11705    }
11706
11707    pub fn parse_show_statement_filter(
11708        &mut self,
11709    ) -> Result<Option<ShowStatementFilter>, ParserError> {
11710        if self.parse_keyword(Keyword::LIKE) {
11711            Ok(Some(ShowStatementFilter::Like(
11712                self.parse_literal_string()?,
11713            )))
11714        } else if self.parse_keyword(Keyword::ILIKE) {
11715            Ok(Some(ShowStatementFilter::ILike(
11716                self.parse_literal_string()?,
11717            )))
11718        } else if self.parse_keyword(Keyword::WHERE) {
11719            Ok(Some(ShowStatementFilter::Where(self.parse_expr()?)))
11720        } else {
11721            self.maybe_parse(|parser| -> Result<String, ParserError> {
11722                parser.parse_literal_string()
11723            })?
11724            .map_or(Ok(None), |filter| {
11725                Ok(Some(ShowStatementFilter::NoKeyword(filter)))
11726            })
11727        }
11728    }
11729
11730    pub fn parse_use(&mut self) -> Result<Statement, ParserError> {
11731        // Determine which keywords are recognized by the current dialect
11732        let parsed_keyword = if dialect_of!(self is HiveDialect) {
11733            // HiveDialect accepts USE DEFAULT; statement without any db specified
11734            if self.parse_keyword(Keyword::DEFAULT) {
11735                return Ok(Statement::Use(Use::Default));
11736            }
11737            None // HiveDialect doesn't expect any other specific keyword after `USE`
11738        } else if dialect_of!(self is DatabricksDialect) {
11739            self.parse_one_of_keywords(&[Keyword::CATALOG, Keyword::DATABASE, Keyword::SCHEMA])
11740        } else if dialect_of!(self is SnowflakeDialect) {
11741            self.parse_one_of_keywords(&[
11742                Keyword::DATABASE,
11743                Keyword::SCHEMA,
11744                Keyword::WAREHOUSE,
11745                Keyword::ROLE,
11746                Keyword::SECONDARY,
11747            ])
11748        } else {
11749            None // No specific keywords for other dialects, including GenericDialect
11750        };
11751
11752        let result = if matches!(parsed_keyword, Some(Keyword::SECONDARY)) {
11753            self.parse_secondary_roles()?
11754        } else {
11755            let obj_name = self.parse_object_name(false)?;
11756            match parsed_keyword {
11757                Some(Keyword::CATALOG) => Use::Catalog(obj_name),
11758                Some(Keyword::DATABASE) => Use::Database(obj_name),
11759                Some(Keyword::SCHEMA) => Use::Schema(obj_name),
11760                Some(Keyword::WAREHOUSE) => Use::Warehouse(obj_name),
11761                Some(Keyword::ROLE) => Use::Role(obj_name),
11762                _ => Use::Object(obj_name),
11763            }
11764        };
11765
11766        Ok(Statement::Use(result))
11767    }
11768
11769    fn parse_secondary_roles(&mut self) -> Result<Use, ParserError> {
11770        self.expect_one_of_keywords(&[Keyword::ROLES, Keyword::ROLE])?;
11771        if self.parse_keyword(Keyword::NONE) {
11772            Ok(Use::SecondaryRoles(SecondaryRoles::None))
11773        } else if self.parse_keyword(Keyword::ALL) {
11774            Ok(Use::SecondaryRoles(SecondaryRoles::All))
11775        } else {
11776            let roles = self.parse_comma_separated(|parser| parser.parse_identifier())?;
11777            Ok(Use::SecondaryRoles(SecondaryRoles::List(roles)))
11778        }
11779    }
11780
11781    pub fn parse_table_and_joins(&mut self) -> Result<TableWithJoins, ParserError> {
11782        let relation = self.parse_table_factor()?;
11783        // Note that for keywords to be properly handled here, they need to be
11784        // added to `RESERVED_FOR_TABLE_ALIAS`, otherwise they may be parsed as
11785        // a table alias.
11786        let joins = self.parse_joins()?;
11787        Ok(TableWithJoins { relation, joins })
11788    }
11789
11790    fn parse_joins(&mut self) -> Result<Vec<Join>, ParserError> {
11791        let mut joins = vec![];
11792        loop {
11793            let global = self.parse_keyword(Keyword::GLOBAL);
11794            let join = if self.parse_keyword(Keyword::CROSS) {
11795                let join_operator = if self.parse_keyword(Keyword::JOIN) {
11796                    JoinOperator::CrossJoin
11797                } else if self.parse_keyword(Keyword::APPLY) {
11798                    // MSSQL extension, similar to CROSS JOIN LATERAL
11799                    JoinOperator::CrossApply
11800                } else {
11801                    return self.expected("JOIN or APPLY after CROSS", self.peek_token());
11802                };
11803                Join {
11804                    relation: self.parse_table_factor()?,
11805                    global,
11806                    join_operator,
11807                }
11808            } else if self.parse_keyword(Keyword::OUTER) {
11809                // MSSQL extension, similar to LEFT JOIN LATERAL .. ON 1=1
11810                self.expect_keyword_is(Keyword::APPLY)?;
11811                Join {
11812                    relation: self.parse_table_factor()?,
11813                    global,
11814                    join_operator: JoinOperator::OuterApply,
11815                }
11816            } else if self.parse_keyword(Keyword::ASOF) {
11817                self.expect_keyword_is(Keyword::JOIN)?;
11818                let relation = self.parse_table_factor()?;
11819                self.expect_keyword_is(Keyword::MATCH_CONDITION)?;
11820                let match_condition = self.parse_parenthesized(Self::parse_expr)?;
11821                Join {
11822                    relation,
11823                    global,
11824                    join_operator: JoinOperator::AsOf {
11825                        match_condition,
11826                        constraint: self.parse_join_constraint(false)?,
11827                    },
11828                }
11829            } else {
11830                let natural = self.parse_keyword(Keyword::NATURAL);
11831                let peek_keyword = if let Token::Word(w) = self.peek_token().token {
11832                    w.keyword
11833                } else {
11834                    Keyword::NoKeyword
11835                };
11836
11837                let join_operator_type = match peek_keyword {
11838                    Keyword::INNER | Keyword::JOIN => {
11839                        let inner = self.parse_keyword(Keyword::INNER); // [ INNER ]
11840                        self.expect_keyword_is(Keyword::JOIN)?;
11841                        if inner {
11842                            JoinOperator::Inner
11843                        } else {
11844                            JoinOperator::Join
11845                        }
11846                    }
11847                    kw @ Keyword::LEFT | kw @ Keyword::RIGHT => {
11848                        let _ = self.next_token(); // consume LEFT/RIGHT
11849                        let is_left = kw == Keyword::LEFT;
11850                        let join_type = self.parse_one_of_keywords(&[
11851                            Keyword::OUTER,
11852                            Keyword::SEMI,
11853                            Keyword::ANTI,
11854                            Keyword::JOIN,
11855                        ]);
11856                        match join_type {
11857                            Some(Keyword::OUTER) => {
11858                                self.expect_keyword_is(Keyword::JOIN)?;
11859                                if is_left {
11860                                    JoinOperator::LeftOuter
11861                                } else {
11862                                    JoinOperator::RightOuter
11863                                }
11864                            }
11865                            Some(Keyword::SEMI) => {
11866                                self.expect_keyword_is(Keyword::JOIN)?;
11867                                if is_left {
11868                                    JoinOperator::LeftSemi
11869                                } else {
11870                                    JoinOperator::RightSemi
11871                                }
11872                            }
11873                            Some(Keyword::ANTI) => {
11874                                self.expect_keyword_is(Keyword::JOIN)?;
11875                                if is_left {
11876                                    JoinOperator::LeftAnti
11877                                } else {
11878                                    JoinOperator::RightAnti
11879                                }
11880                            }
11881                            Some(Keyword::JOIN) => {
11882                                if is_left {
11883                                    JoinOperator::Left
11884                                } else {
11885                                    JoinOperator::Right
11886                                }
11887                            }
11888                            _ => {
11889                                return Err(ParserError::ParserError(format!(
11890                                    "expected OUTER, SEMI, ANTI or JOIN after {kw:?}"
11891                                )))
11892                            }
11893                        }
11894                    }
11895                    Keyword::ANTI => {
11896                        let _ = self.next_token(); // consume ANTI
11897                        self.expect_keyword_is(Keyword::JOIN)?;
11898                        JoinOperator::Anti
11899                    }
11900                    Keyword::SEMI => {
11901                        let _ = self.next_token(); // consume SEMI
11902                        self.expect_keyword_is(Keyword::JOIN)?;
11903                        JoinOperator::Semi
11904                    }
11905                    Keyword::FULL => {
11906                        let _ = self.next_token(); // consume FULL
11907                        let _ = self.parse_keyword(Keyword::OUTER); // [ OUTER ]
11908                        self.expect_keyword_is(Keyword::JOIN)?;
11909                        JoinOperator::FullOuter
11910                    }
11911                    Keyword::OUTER => {
11912                        return self.expected("LEFT, RIGHT, or FULL", self.peek_token());
11913                    }
11914                    Keyword::STRAIGHT_JOIN => {
11915                        let _ = self.next_token(); // consume STRAIGHT_JOIN
11916                        JoinOperator::StraightJoin
11917                    }
11918                    _ if natural => {
11919                        return self.expected("a join type after NATURAL", self.peek_token());
11920                    }
11921                    _ => break,
11922                };
11923                let mut relation = self.parse_table_factor()?;
11924
11925                if self.peek_parens_less_nested_join() {
11926                    let joins = self.parse_joins()?;
11927                    relation = TableFactor::NestedJoin {
11928                        table_with_joins: Box::new(TableWithJoins { relation, joins }),
11929                        alias: None,
11930                    };
11931                }
11932
11933                let join_constraint = self.parse_join_constraint(natural)?;
11934                Join {
11935                    relation,
11936                    global,
11937                    join_operator: join_operator_type(join_constraint),
11938                }
11939            };
11940            joins.push(join);
11941        }
11942        Ok(joins)
11943    }
11944
11945    fn peek_parens_less_nested_join(&self) -> bool {
11946        matches!(
11947            self.peek_token_ref().token,
11948            Token::Word(Word {
11949                keyword: Keyword::JOIN
11950                    | Keyword::INNER
11951                    | Keyword::LEFT
11952                    | Keyword::RIGHT
11953                    | Keyword::FULL,
11954                ..
11955            })
11956        )
11957    }
11958
11959    /// A table name or a parenthesized subquery, followed by optional `[AS] alias`
11960    pub fn parse_table_factor(&mut self) -> Result<TableFactor, ParserError> {
11961        if self.parse_keyword(Keyword::LATERAL) {
11962            // LATERAL must always be followed by a subquery or table function.
11963            if self.consume_token(&Token::LParen) {
11964                self.parse_derived_table_factor(Lateral)
11965            } else {
11966                let name = self.parse_object_name(false)?;
11967                self.expect_token(&Token::LParen)?;
11968                let args = self.parse_optional_args()?;
11969                let alias = self.maybe_parse_table_alias()?;
11970                Ok(TableFactor::Function {
11971                    lateral: true,
11972                    name,
11973                    args,
11974                    alias,
11975                })
11976            }
11977        } else if self.parse_keyword(Keyword::TABLE) {
11978            // parse table function (SELECT * FROM TABLE (<expr>) [ AS <alias> ])
11979            self.expect_token(&Token::LParen)?;
11980            let expr = self.parse_expr()?;
11981            self.expect_token(&Token::RParen)?;
11982            let alias = self.maybe_parse_table_alias()?;
11983            Ok(TableFactor::TableFunction { expr, alias })
11984        } else if self.consume_token(&Token::LParen) {
11985            // A left paren introduces either a derived table (i.e., a subquery)
11986            // or a nested join. It's nearly impossible to determine ahead of
11987            // time which it is... so we just try to parse both.
11988            //
11989            // Here's an example that demonstrates the complexity:
11990            //                     /-------------------------------------------------------\
11991            //                     | /-----------------------------------\                 |
11992            //     SELECT * FROM ( ( ( (SELECT 1) UNION (SELECT 2) ) AS t1 NATURAL JOIN t2 ) )
11993            //                   ^ ^ ^ ^
11994            //                   | | | |
11995            //                   | | | |
11996            //                   | | | (4) belongs to a SetExpr::Query inside the subquery
11997            //                   | | (3) starts a derived table (subquery)
11998            //                   | (2) starts a nested join
11999            //                   (1) an additional set of parens around a nested join
12000            //
12001
12002            // If the recently consumed '(' starts a derived table, the call to
12003            // `parse_derived_table_factor` below will return success after parsing the
12004            // subquery, followed by the closing ')', and the alias of the derived table.
12005            // In the example above this is case (3).
12006            if let Some(mut table) =
12007                self.maybe_parse(|parser| parser.parse_derived_table_factor(NotLateral))?
12008            {
12009                while let Some(kw) = self.parse_one_of_keywords(&[Keyword::PIVOT, Keyword::UNPIVOT])
12010                {
12011                    table = match kw {
12012                        Keyword::PIVOT => self.parse_pivot_table_factor(table)?,
12013                        Keyword::UNPIVOT => self.parse_unpivot_table_factor(table)?,
12014                        _ => unreachable!(),
12015                    }
12016                }
12017                return Ok(table);
12018            }
12019
12020            // A parsing error from `parse_derived_table_factor` indicates that the '(' we've
12021            // recently consumed does not start a derived table (cases 1, 2, or 4).
12022            // `maybe_parse` will ignore such an error and rewind to be after the opening '('.
12023
12024            // Inside the parentheses we expect to find an (A) table factor
12025            // followed by some joins or (B) another level of nesting.
12026            let mut table_and_joins = self.parse_table_and_joins()?;
12027
12028            #[allow(clippy::if_same_then_else)]
12029            if !table_and_joins.joins.is_empty() {
12030                self.expect_token(&Token::RParen)?;
12031                let alias = self.maybe_parse_table_alias()?;
12032                Ok(TableFactor::NestedJoin {
12033                    table_with_joins: Box::new(table_and_joins),
12034                    alias,
12035                }) // (A)
12036            } else if let TableFactor::NestedJoin {
12037                table_with_joins: _,
12038                alias: _,
12039            } = &table_and_joins.relation
12040            {
12041                // (B): `table_and_joins` (what we found inside the parentheses)
12042                // is a nested join `(foo JOIN bar)`, not followed by other joins.
12043                self.expect_token(&Token::RParen)?;
12044                let alias = self.maybe_parse_table_alias()?;
12045                Ok(TableFactor::NestedJoin {
12046                    table_with_joins: Box::new(table_and_joins),
12047                    alias,
12048                })
12049            } else if dialect_of!(self is SnowflakeDialect | GenericDialect) {
12050                // Dialect-specific behavior: Snowflake diverges from the
12051                // standard and from most of the other implementations by
12052                // allowing extra parentheses not only around a join (B), but
12053                // around lone table names (e.g. `FROM (mytable [AS alias])`)
12054                // and around derived tables (e.g. `FROM ((SELECT ...)
12055                // [AS alias])`) as well.
12056                self.expect_token(&Token::RParen)?;
12057
12058                if let Some(outer_alias) = self.maybe_parse_table_alias()? {
12059                    // Snowflake also allows specifying an alias *after* parens
12060                    // e.g. `FROM (mytable) AS alias`
12061                    match &mut table_and_joins.relation {
12062                        TableFactor::Derived { alias, .. }
12063                        | TableFactor::Table { alias, .. }
12064                        | TableFactor::Function { alias, .. }
12065                        | TableFactor::UNNEST { alias, .. }
12066                        | TableFactor::JsonTable { alias, .. }
12067                        | TableFactor::XmlTable { alias, .. }
12068                        | TableFactor::OpenJsonTable { alias, .. }
12069                        | TableFactor::TableFunction { alias, .. }
12070                        | TableFactor::Pivot { alias, .. }
12071                        | TableFactor::Unpivot { alias, .. }
12072                        | TableFactor::MatchRecognize { alias, .. }
12073                        | TableFactor::NestedJoin { alias, .. } => {
12074                            // but not `FROM (mytable AS alias1) AS alias2`.
12075                            if let Some(inner_alias) = alias {
12076                                return Err(ParserError::ParserError(format!(
12077                                    "duplicate alias {inner_alias}"
12078                                )));
12079                            }
12080                            // Act as if the alias was specified normally next
12081                            // to the table name: `(mytable) AS alias` ->
12082                            // `(mytable AS alias)`
12083                            alias.replace(outer_alias);
12084                        }
12085                    };
12086                }
12087                // Do not store the extra set of parens in the AST
12088                Ok(table_and_joins.relation)
12089            } else {
12090                // The SQL spec prohibits derived tables and bare tables from
12091                // appearing alone in parentheses (e.g. `FROM (mytable)`)
12092                self.expected("joined table", self.peek_token())
12093            }
12094        } else if dialect_of!(self is SnowflakeDialect | DatabricksDialect | GenericDialect)
12095            && matches!(
12096                self.peek_tokens(),
12097                [
12098                    Token::Word(Word {
12099                        keyword: Keyword::VALUES,
12100                        ..
12101                    }),
12102                    Token::LParen
12103                ]
12104            )
12105        {
12106            self.expect_keyword_is(Keyword::VALUES)?;
12107
12108            // Snowflake and Databricks allow syntax like below:
12109            // SELECT * FROM VALUES (1, 'a'), (2, 'b') AS t (col1, col2)
12110            // where there are no parentheses around the VALUES clause.
12111            let values = SetExpr::Values(self.parse_values(false)?);
12112            let alias = self.maybe_parse_table_alias()?;
12113            Ok(TableFactor::Derived {
12114                lateral: false,
12115                subquery: Box::new(Query {
12116                    with: None,
12117                    body: Box::new(values),
12118                    order_by: None,
12119                    limit_clause: None,
12120                    fetch: None,
12121                    locks: vec![],
12122                    for_clause: None,
12123                    settings: None,
12124                    format_clause: None,
12125                }),
12126                alias,
12127            })
12128        } else if dialect_of!(self is BigQueryDialect | PostgreSqlDialect | GenericDialect)
12129            && self.parse_keyword(Keyword::UNNEST)
12130        {
12131            self.expect_token(&Token::LParen)?;
12132            let array_exprs = self.parse_comma_separated(Parser::parse_expr)?;
12133            self.expect_token(&Token::RParen)?;
12134
12135            let with_ordinality = self.parse_keywords(&[Keyword::WITH, Keyword::ORDINALITY]);
12136            let alias = match self.maybe_parse_table_alias() {
12137                Ok(Some(alias)) => Some(alias),
12138                Ok(None) => None,
12139                Err(e) => return Err(e),
12140            };
12141
12142            let with_offset = match self.expect_keywords(&[Keyword::WITH, Keyword::OFFSET]) {
12143                Ok(()) => true,
12144                Err(_) => false,
12145            };
12146
12147            let with_offset_alias = if with_offset {
12148                match self.parse_optional_alias(keywords::RESERVED_FOR_COLUMN_ALIAS) {
12149                    Ok(Some(alias)) => Some(alias),
12150                    Ok(None) => None,
12151                    Err(e) => return Err(e),
12152                }
12153            } else {
12154                None
12155            };
12156
12157            Ok(TableFactor::UNNEST {
12158                alias,
12159                array_exprs,
12160                with_offset,
12161                with_offset_alias,
12162                with_ordinality,
12163            })
12164        } else if self.parse_keyword_with_tokens(Keyword::JSON_TABLE, &[Token::LParen]) {
12165            let json_expr = self.parse_expr()?;
12166            self.expect_token(&Token::Comma)?;
12167            let json_path = self.parse_value()?.value;
12168            self.expect_keyword_is(Keyword::COLUMNS)?;
12169            self.expect_token(&Token::LParen)?;
12170            let columns = self.parse_comma_separated(Parser::parse_json_table_column_def)?;
12171            self.expect_token(&Token::RParen)?;
12172            self.expect_token(&Token::RParen)?;
12173            let alias = self.maybe_parse_table_alias()?;
12174            Ok(TableFactor::JsonTable {
12175                json_expr,
12176                json_path,
12177                columns,
12178                alias,
12179            })
12180        } else if self.parse_keyword_with_tokens(Keyword::OPENJSON, &[Token::LParen]) {
12181            self.prev_token();
12182            self.parse_open_json_table_factor()
12183        } else if self.parse_keyword_with_tokens(Keyword::XMLTABLE, &[Token::LParen]) {
12184            self.prev_token();
12185            self.parse_xml_table_factor()
12186        } else {
12187            let name = self.parse_object_name(true)?;
12188
12189            let json_path = match self.peek_token().token {
12190                Token::LBracket if self.dialect.supports_partiql() => Some(self.parse_json_path()?),
12191                _ => None,
12192            };
12193
12194            let partitions: Vec<Ident> = if dialect_of!(self is MySqlDialect | GenericDialect)
12195                && self.parse_keyword(Keyword::PARTITION)
12196            {
12197                self.parse_parenthesized_identifiers()?
12198            } else {
12199                vec![]
12200            };
12201
12202            // Parse potential version qualifier
12203            let version = self.maybe_parse_table_version()?;
12204
12205            // Postgres, MSSQL, ClickHouse: table-valued functions:
12206            let args = if self.consume_token(&Token::LParen) {
12207                Some(self.parse_table_function_args()?)
12208            } else {
12209                None
12210            };
12211
12212            let with_ordinality = self.parse_keywords(&[Keyword::WITH, Keyword::ORDINALITY]);
12213
12214            let mut sample = None;
12215            if self.dialect.supports_table_sample_before_alias() {
12216                if let Some(parsed_sample) = self.maybe_parse_table_sample()? {
12217                    sample = Some(TableSampleKind::BeforeTableAlias(parsed_sample));
12218                }
12219            }
12220
12221            let alias = self.maybe_parse_table_alias()?;
12222
12223            // MYSQL-specific table hints:
12224            let index_hints = if self.dialect.supports_table_hints() {
12225                self.maybe_parse(|p| p.parse_table_index_hints())?
12226                    .unwrap_or(vec![])
12227            } else {
12228                vec![]
12229            };
12230
12231            // MSSQL-specific table hints:
12232            let mut with_hints = vec![];
12233            if self.parse_keyword(Keyword::WITH) {
12234                if self.consume_token(&Token::LParen) {
12235                    with_hints = self.parse_comma_separated(Parser::parse_expr)?;
12236                    self.expect_token(&Token::RParen)?;
12237                } else {
12238                    // rewind, as WITH may belong to the next statement's CTE
12239                    self.prev_token();
12240                }
12241            };
12242
12243            if !self.dialect.supports_table_sample_before_alias() {
12244                if let Some(parsed_sample) = self.maybe_parse_table_sample()? {
12245                    sample = Some(TableSampleKind::AfterTableAlias(parsed_sample));
12246                }
12247            }
12248
12249            let mut table = TableFactor::Table {
12250                name,
12251                alias,
12252                args,
12253                with_hints,
12254                version,
12255                partitions,
12256                with_ordinality,
12257                json_path,
12258                sample,
12259                index_hints,
12260            };
12261
12262            while let Some(kw) = self.parse_one_of_keywords(&[Keyword::PIVOT, Keyword::UNPIVOT]) {
12263                table = match kw {
12264                    Keyword::PIVOT => self.parse_pivot_table_factor(table)?,
12265                    Keyword::UNPIVOT => self.parse_unpivot_table_factor(table)?,
12266                    _ => unreachable!(),
12267                }
12268            }
12269
12270            if self.dialect.supports_match_recognize()
12271                && self.parse_keyword(Keyword::MATCH_RECOGNIZE)
12272            {
12273                table = self.parse_match_recognize(table)?;
12274            }
12275
12276            Ok(table)
12277        }
12278    }
12279
12280    fn maybe_parse_table_sample(&mut self) -> Result<Option<Box<TableSample>>, ParserError> {
12281        let modifier = if self.parse_keyword(Keyword::TABLESAMPLE) {
12282            TableSampleModifier::TableSample
12283        } else if self.parse_keyword(Keyword::SAMPLE) {
12284            TableSampleModifier::Sample
12285        } else {
12286            return Ok(None);
12287        };
12288
12289        let name = match self.parse_one_of_keywords(&[
12290            Keyword::BERNOULLI,
12291            Keyword::ROW,
12292            Keyword::SYSTEM,
12293            Keyword::BLOCK,
12294        ]) {
12295            Some(Keyword::BERNOULLI) => Some(TableSampleMethod::Bernoulli),
12296            Some(Keyword::ROW) => Some(TableSampleMethod::Row),
12297            Some(Keyword::SYSTEM) => Some(TableSampleMethod::System),
12298            Some(Keyword::BLOCK) => Some(TableSampleMethod::Block),
12299            _ => None,
12300        };
12301
12302        let parenthesized = self.consume_token(&Token::LParen);
12303
12304        let (quantity, bucket) = if parenthesized && self.parse_keyword(Keyword::BUCKET) {
12305            let selected_bucket = self.parse_number_value()?.value;
12306            self.expect_keywords(&[Keyword::OUT, Keyword::OF])?;
12307            let total = self.parse_number_value()?.value;
12308            let on = if self.parse_keyword(Keyword::ON) {
12309                Some(self.parse_expr()?)
12310            } else {
12311                None
12312            };
12313            (
12314                None,
12315                Some(TableSampleBucket {
12316                    bucket: selected_bucket,
12317                    total,
12318                    on,
12319                }),
12320            )
12321        } else {
12322            let value = match self.maybe_parse(|p| p.parse_expr())? {
12323                Some(num) => num,
12324                None => {
12325                    let next_token = self.next_token();
12326                    if let Token::Word(w) = next_token.token {
12327                        Expr::Value(Value::Placeholder(w.value).with_span(next_token.span))
12328                    } else {
12329                        return parser_err!(
12330                            "Expecting number or byte length e.g. 100M",
12331                            self.peek_token().span.start
12332                        );
12333                    }
12334                }
12335            };
12336            let unit = if self.parse_keyword(Keyword::ROWS) {
12337                Some(TableSampleUnit::Rows)
12338            } else if self.parse_keyword(Keyword::PERCENT) {
12339                Some(TableSampleUnit::Percent)
12340            } else {
12341                None
12342            };
12343            (
12344                Some(TableSampleQuantity {
12345                    parenthesized,
12346                    value,
12347                    unit,
12348                }),
12349                None,
12350            )
12351        };
12352        if parenthesized {
12353            self.expect_token(&Token::RParen)?;
12354        }
12355
12356        let seed = if self.parse_keyword(Keyword::REPEATABLE) {
12357            Some(self.parse_table_sample_seed(TableSampleSeedModifier::Repeatable)?)
12358        } else if self.parse_keyword(Keyword::SEED) {
12359            Some(self.parse_table_sample_seed(TableSampleSeedModifier::Seed)?)
12360        } else {
12361            None
12362        };
12363
12364        let offset = if self.parse_keyword(Keyword::OFFSET) {
12365            Some(self.parse_expr()?)
12366        } else {
12367            None
12368        };
12369
12370        Ok(Some(Box::new(TableSample {
12371            modifier,
12372            name,
12373            quantity,
12374            seed,
12375            bucket,
12376            offset,
12377        })))
12378    }
12379
12380    fn parse_table_sample_seed(
12381        &mut self,
12382        modifier: TableSampleSeedModifier,
12383    ) -> Result<TableSampleSeed, ParserError> {
12384        self.expect_token(&Token::LParen)?;
12385        let value = self.parse_number_value()?.value;
12386        self.expect_token(&Token::RParen)?;
12387        Ok(TableSampleSeed { modifier, value })
12388    }
12389
12390    /// Parses `OPENJSON( jsonExpression [ , path ] )  [ <with_clause> ]` clause,
12391    /// assuming the `OPENJSON` keyword was already consumed.
12392    fn parse_open_json_table_factor(&mut self) -> Result<TableFactor, ParserError> {
12393        self.expect_token(&Token::LParen)?;
12394        let json_expr = self.parse_expr()?;
12395        let json_path = if self.consume_token(&Token::Comma) {
12396            Some(self.parse_value()?.value)
12397        } else {
12398            None
12399        };
12400        self.expect_token(&Token::RParen)?;
12401        let columns = if self.parse_keyword(Keyword::WITH) {
12402            self.expect_token(&Token::LParen)?;
12403            let columns = self.parse_comma_separated(Parser::parse_openjson_table_column_def)?;
12404            self.expect_token(&Token::RParen)?;
12405            columns
12406        } else {
12407            Vec::new()
12408        };
12409        let alias = self.maybe_parse_table_alias()?;
12410        Ok(TableFactor::OpenJsonTable {
12411            json_expr,
12412            json_path,
12413            columns,
12414            alias,
12415        })
12416    }
12417
12418    fn parse_xml_table_factor(&mut self) -> Result<TableFactor, ParserError> {
12419        self.expect_token(&Token::LParen)?;
12420        let namespaces = if self.parse_keyword(Keyword::XMLNAMESPACES) {
12421            self.expect_token(&Token::LParen)?;
12422            let namespaces = self.parse_comma_separated(Parser::parse_xml_namespace_definition)?;
12423            self.expect_token(&Token::RParen)?;
12424            self.expect_token(&Token::Comma)?;
12425            namespaces
12426        } else {
12427            vec![]
12428        };
12429        let row_expression = self.parse_expr()?;
12430        let passing = self.parse_xml_passing_clause()?;
12431        self.expect_keyword_is(Keyword::COLUMNS)?;
12432        let columns = self.parse_comma_separated(Parser::parse_xml_table_column)?;
12433        self.expect_token(&Token::RParen)?;
12434        let alias = self.maybe_parse_table_alias()?;
12435        Ok(TableFactor::XmlTable {
12436            namespaces,
12437            row_expression,
12438            passing,
12439            columns,
12440            alias,
12441        })
12442    }
12443
12444    fn parse_xml_namespace_definition(&mut self) -> Result<XmlNamespaceDefinition, ParserError> {
12445        let uri = self.parse_expr()?;
12446        self.expect_keyword_is(Keyword::AS)?;
12447        let name = self.parse_identifier()?;
12448        Ok(XmlNamespaceDefinition { uri, name })
12449    }
12450
12451    fn parse_xml_table_column(&mut self) -> Result<XmlTableColumn, ParserError> {
12452        let name = self.parse_identifier()?;
12453
12454        let option = if self.parse_keyword(Keyword::FOR) {
12455            self.expect_keyword(Keyword::ORDINALITY)?;
12456            XmlTableColumnOption::ForOrdinality
12457        } else {
12458            let r#type = self.parse_data_type()?;
12459            let mut path = None;
12460            let mut default = None;
12461
12462            if self.parse_keyword(Keyword::PATH) {
12463                path = Some(self.parse_expr()?);
12464            }
12465
12466            if self.parse_keyword(Keyword::DEFAULT) {
12467                default = Some(self.parse_expr()?);
12468            }
12469
12470            let not_null = self.parse_keywords(&[Keyword::NOT, Keyword::NULL]);
12471            if !not_null {
12472                // NULL is the default but can be specified explicitly
12473                let _ = self.parse_keyword(Keyword::NULL);
12474            }
12475
12476            XmlTableColumnOption::NamedInfo {
12477                r#type,
12478                path,
12479                default,
12480                nullable: !not_null,
12481            }
12482        };
12483        Ok(XmlTableColumn { name, option })
12484    }
12485
12486    fn parse_xml_passing_clause(&mut self) -> Result<XmlPassingClause, ParserError> {
12487        let mut arguments = vec![];
12488        if self.parse_keyword(Keyword::PASSING) {
12489            loop {
12490                let by_value =
12491                    self.parse_keyword(Keyword::BY) && self.expect_keyword(Keyword::VALUE).is_ok();
12492                let expr = self.parse_expr()?;
12493                let alias = if self.parse_keyword(Keyword::AS) {
12494                    Some(self.parse_identifier()?)
12495                } else {
12496                    None
12497                };
12498                arguments.push(XmlPassingArgument {
12499                    expr,
12500                    alias,
12501                    by_value,
12502                });
12503                if !self.consume_token(&Token::Comma) {
12504                    break;
12505                }
12506            }
12507        }
12508        Ok(XmlPassingClause { arguments })
12509    }
12510
12511    fn parse_match_recognize(&mut self, table: TableFactor) -> Result<TableFactor, ParserError> {
12512        self.expect_token(&Token::LParen)?;
12513
12514        let partition_by = if self.parse_keywords(&[Keyword::PARTITION, Keyword::BY]) {
12515            self.parse_comma_separated(Parser::parse_expr)?
12516        } else {
12517            vec![]
12518        };
12519
12520        let order_by = if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
12521            self.parse_comma_separated(Parser::parse_order_by_expr)?
12522        } else {
12523            vec![]
12524        };
12525
12526        let measures = if self.parse_keyword(Keyword::MEASURES) {
12527            self.parse_comma_separated(|p| {
12528                let expr = p.parse_expr()?;
12529                let _ = p.parse_keyword(Keyword::AS);
12530                let alias = p.parse_identifier()?;
12531                Ok(Measure { expr, alias })
12532            })?
12533        } else {
12534            vec![]
12535        };
12536
12537        let rows_per_match =
12538            if self.parse_keywords(&[Keyword::ONE, Keyword::ROW, Keyword::PER, Keyword::MATCH]) {
12539                Some(RowsPerMatch::OneRow)
12540            } else if self.parse_keywords(&[
12541                Keyword::ALL,
12542                Keyword::ROWS,
12543                Keyword::PER,
12544                Keyword::MATCH,
12545            ]) {
12546                Some(RowsPerMatch::AllRows(
12547                    if self.parse_keywords(&[Keyword::SHOW, Keyword::EMPTY, Keyword::MATCHES]) {
12548                        Some(EmptyMatchesMode::Show)
12549                    } else if self.parse_keywords(&[
12550                        Keyword::OMIT,
12551                        Keyword::EMPTY,
12552                        Keyword::MATCHES,
12553                    ]) {
12554                        Some(EmptyMatchesMode::Omit)
12555                    } else if self.parse_keywords(&[
12556                        Keyword::WITH,
12557                        Keyword::UNMATCHED,
12558                        Keyword::ROWS,
12559                    ]) {
12560                        Some(EmptyMatchesMode::WithUnmatched)
12561                    } else {
12562                        None
12563                    },
12564                ))
12565            } else {
12566                None
12567            };
12568
12569        let after_match_skip =
12570            if self.parse_keywords(&[Keyword::AFTER, Keyword::MATCH, Keyword::SKIP]) {
12571                if self.parse_keywords(&[Keyword::PAST, Keyword::LAST, Keyword::ROW]) {
12572                    Some(AfterMatchSkip::PastLastRow)
12573                } else if self.parse_keywords(&[Keyword::TO, Keyword::NEXT, Keyword::ROW]) {
12574                    Some(AfterMatchSkip::ToNextRow)
12575                } else if self.parse_keywords(&[Keyword::TO, Keyword::FIRST]) {
12576                    Some(AfterMatchSkip::ToFirst(self.parse_identifier()?))
12577                } else if self.parse_keywords(&[Keyword::TO, Keyword::LAST]) {
12578                    Some(AfterMatchSkip::ToLast(self.parse_identifier()?))
12579                } else {
12580                    let found = self.next_token();
12581                    return self.expected("after match skip option", found);
12582                }
12583            } else {
12584                None
12585            };
12586
12587        self.expect_keyword_is(Keyword::PATTERN)?;
12588        let pattern = self.parse_parenthesized(Self::parse_pattern)?;
12589
12590        self.expect_keyword_is(Keyword::DEFINE)?;
12591
12592        let symbols = self.parse_comma_separated(|p| {
12593            let symbol = p.parse_identifier()?;
12594            p.expect_keyword_is(Keyword::AS)?;
12595            let definition = p.parse_expr()?;
12596            Ok(SymbolDefinition { symbol, definition })
12597        })?;
12598
12599        self.expect_token(&Token::RParen)?;
12600
12601        let alias = self.maybe_parse_table_alias()?;
12602
12603        Ok(TableFactor::MatchRecognize {
12604            table: Box::new(table),
12605            partition_by,
12606            order_by,
12607            measures,
12608            rows_per_match,
12609            after_match_skip,
12610            pattern,
12611            symbols,
12612            alias,
12613        })
12614    }
12615
12616    fn parse_base_pattern(&mut self) -> Result<MatchRecognizePattern, ParserError> {
12617        match self.next_token().token {
12618            Token::Caret => Ok(MatchRecognizePattern::Symbol(MatchRecognizeSymbol::Start)),
12619            Token::Placeholder(s) if s == "$" => {
12620                Ok(MatchRecognizePattern::Symbol(MatchRecognizeSymbol::End))
12621            }
12622            Token::LBrace => {
12623                self.expect_token(&Token::Minus)?;
12624                let symbol = self.parse_identifier().map(MatchRecognizeSymbol::Named)?;
12625                self.expect_token(&Token::Minus)?;
12626                self.expect_token(&Token::RBrace)?;
12627                Ok(MatchRecognizePattern::Exclude(symbol))
12628            }
12629            Token::Word(Word {
12630                value,
12631                quote_style: None,
12632                ..
12633            }) if value == "PERMUTE" => {
12634                self.expect_token(&Token::LParen)?;
12635                let symbols = self.parse_comma_separated(|p| {
12636                    p.parse_identifier().map(MatchRecognizeSymbol::Named)
12637                })?;
12638                self.expect_token(&Token::RParen)?;
12639                Ok(MatchRecognizePattern::Permute(symbols))
12640            }
12641            Token::LParen => {
12642                let pattern = self.parse_pattern()?;
12643                self.expect_token(&Token::RParen)?;
12644                Ok(MatchRecognizePattern::Group(Box::new(pattern)))
12645            }
12646            _ => {
12647                self.prev_token();
12648                self.parse_identifier()
12649                    .map(MatchRecognizeSymbol::Named)
12650                    .map(MatchRecognizePattern::Symbol)
12651            }
12652        }
12653    }
12654
12655    fn parse_repetition_pattern(&mut self) -> Result<MatchRecognizePattern, ParserError> {
12656        let mut pattern = self.parse_base_pattern()?;
12657        loop {
12658            let token = self.next_token();
12659            let quantifier = match token.token {
12660                Token::Mul => RepetitionQuantifier::ZeroOrMore,
12661                Token::Plus => RepetitionQuantifier::OneOrMore,
12662                Token::Placeholder(s) if s == "?" => RepetitionQuantifier::AtMostOne,
12663                Token::LBrace => {
12664                    // quantifier is a range like {n} or {n,} or {,m} or {n,m}
12665                    let token = self.next_token();
12666                    match token.token {
12667                        Token::Comma => {
12668                            let next_token = self.next_token();
12669                            let Token::Number(n, _) = next_token.token else {
12670                                return self.expected("literal number", next_token);
12671                            };
12672                            self.expect_token(&Token::RBrace)?;
12673                            RepetitionQuantifier::AtMost(Self::parse(n, token.span.start)?)
12674                        }
12675                        Token::Number(n, _) if self.consume_token(&Token::Comma) => {
12676                            let next_token = self.next_token();
12677                            match next_token.token {
12678                                Token::Number(m, _) => {
12679                                    self.expect_token(&Token::RBrace)?;
12680                                    RepetitionQuantifier::Range(
12681                                        Self::parse(n, token.span.start)?,
12682                                        Self::parse(m, token.span.start)?,
12683                                    )
12684                                }
12685                                Token::RBrace => {
12686                                    RepetitionQuantifier::AtLeast(Self::parse(n, token.span.start)?)
12687                                }
12688                                _ => {
12689                                    return self.expected("} or upper bound", next_token);
12690                                }
12691                            }
12692                        }
12693                        Token::Number(n, _) => {
12694                            self.expect_token(&Token::RBrace)?;
12695                            RepetitionQuantifier::Exactly(Self::parse(n, token.span.start)?)
12696                        }
12697                        _ => return self.expected("quantifier range", token),
12698                    }
12699                }
12700                _ => {
12701                    self.prev_token();
12702                    break;
12703                }
12704            };
12705            pattern = MatchRecognizePattern::Repetition(Box::new(pattern), quantifier);
12706        }
12707        Ok(pattern)
12708    }
12709
12710    fn parse_concat_pattern(&mut self) -> Result<MatchRecognizePattern, ParserError> {
12711        let mut patterns = vec![self.parse_repetition_pattern()?];
12712        while !matches!(self.peek_token().token, Token::RParen | Token::Pipe) {
12713            patterns.push(self.parse_repetition_pattern()?);
12714        }
12715        match <[MatchRecognizePattern; 1]>::try_from(patterns) {
12716            Ok([pattern]) => Ok(pattern),
12717            Err(patterns) => Ok(MatchRecognizePattern::Concat(patterns)),
12718        }
12719    }
12720
12721    fn parse_pattern(&mut self) -> Result<MatchRecognizePattern, ParserError> {
12722        let pattern = self.parse_concat_pattern()?;
12723        if self.consume_token(&Token::Pipe) {
12724            match self.parse_pattern()? {
12725                // flatten nested alternations
12726                MatchRecognizePattern::Alternation(mut patterns) => {
12727                    patterns.insert(0, pattern);
12728                    Ok(MatchRecognizePattern::Alternation(patterns))
12729                }
12730                next => Ok(MatchRecognizePattern::Alternation(vec![pattern, next])),
12731            }
12732        } else {
12733            Ok(pattern)
12734        }
12735    }
12736
12737    /// Parses a the timestamp version specifier (i.e. query historical data)
12738    pub fn maybe_parse_table_version(&mut self) -> Result<Option<TableVersion>, ParserError> {
12739        if self.dialect.supports_timestamp_versioning() {
12740            if self.parse_keywords(&[Keyword::FOR, Keyword::SYSTEM_TIME, Keyword::AS, Keyword::OF])
12741            {
12742                let expr = self.parse_expr()?;
12743                return Ok(Some(TableVersion::ForSystemTimeAsOf(expr)));
12744            } else if self.peek_keyword(Keyword::AT) || self.peek_keyword(Keyword::BEFORE) {
12745                let func_name = self.parse_object_name(true)?;
12746                let func = self.parse_function(func_name)?;
12747                return Ok(Some(TableVersion::Function(func)));
12748            }
12749        }
12750        Ok(None)
12751    }
12752
12753    /// Parses MySQL's JSON_TABLE column definition.
12754    /// For example: `id INT EXISTS PATH '$' DEFAULT '0' ON EMPTY ERROR ON ERROR`
12755    pub fn parse_json_table_column_def(&mut self) -> Result<JsonTableColumn, ParserError> {
12756        if self.parse_keyword(Keyword::NESTED) {
12757            let _has_path_keyword = self.parse_keyword(Keyword::PATH);
12758            let path = self.parse_value()?.value;
12759            self.expect_keyword_is(Keyword::COLUMNS)?;
12760            let columns = self.parse_parenthesized(|p| {
12761                p.parse_comma_separated(Self::parse_json_table_column_def)
12762            })?;
12763            return Ok(JsonTableColumn::Nested(JsonTableNestedColumn {
12764                path,
12765                columns,
12766            }));
12767        }
12768        let name = self.parse_identifier()?;
12769        if self.parse_keyword(Keyword::FOR) {
12770            self.expect_keyword_is(Keyword::ORDINALITY)?;
12771            return Ok(JsonTableColumn::ForOrdinality(name));
12772        }
12773        let r#type = self.parse_data_type()?;
12774        let exists = self.parse_keyword(Keyword::EXISTS);
12775        self.expect_keyword_is(Keyword::PATH)?;
12776        let path = self.parse_value()?.value;
12777        let mut on_empty = None;
12778        let mut on_error = None;
12779        while let Some(error_handling) = self.parse_json_table_column_error_handling()? {
12780            if self.parse_keyword(Keyword::EMPTY) {
12781                on_empty = Some(error_handling);
12782            } else {
12783                self.expect_keyword_is(Keyword::ERROR)?;
12784                on_error = Some(error_handling);
12785            }
12786        }
12787        Ok(JsonTableColumn::Named(JsonTableNamedColumn {
12788            name,
12789            r#type,
12790            path,
12791            exists,
12792            on_empty,
12793            on_error,
12794        }))
12795    }
12796
12797    /// Parses MSSQL's `OPENJSON WITH` column definition.
12798    ///
12799    /// ```sql
12800    /// colName type [ column_path ] [ AS JSON ]
12801    /// ```
12802    ///
12803    /// Reference: <https://learn.microsoft.com/en-us/sql/t-sql/functions/openjson-transact-sql?view=sql-server-ver16#syntax>
12804    pub fn parse_openjson_table_column_def(&mut self) -> Result<OpenJsonTableColumn, ParserError> {
12805        let name = self.parse_identifier()?;
12806        let r#type = self.parse_data_type()?;
12807        let path = if let Token::SingleQuotedString(path) = self.peek_token().token {
12808            self.next_token();
12809            Some(path)
12810        } else {
12811            None
12812        };
12813        let as_json = self.parse_keyword(Keyword::AS);
12814        if as_json {
12815            self.expect_keyword_is(Keyword::JSON)?;
12816        }
12817        Ok(OpenJsonTableColumn {
12818            name,
12819            r#type,
12820            path,
12821            as_json,
12822        })
12823    }
12824
12825    fn parse_json_table_column_error_handling(
12826        &mut self,
12827    ) -> Result<Option<JsonTableColumnErrorHandling>, ParserError> {
12828        let res = if self.parse_keyword(Keyword::NULL) {
12829            JsonTableColumnErrorHandling::Null
12830        } else if self.parse_keyword(Keyword::ERROR) {
12831            JsonTableColumnErrorHandling::Error
12832        } else if self.parse_keyword(Keyword::DEFAULT) {
12833            JsonTableColumnErrorHandling::Default(self.parse_value()?.value)
12834        } else {
12835            return Ok(None);
12836        };
12837        self.expect_keyword_is(Keyword::ON)?;
12838        Ok(Some(res))
12839    }
12840
12841    pub fn parse_derived_table_factor(
12842        &mut self,
12843        lateral: IsLateral,
12844    ) -> Result<TableFactor, ParserError> {
12845        let subquery = self.parse_query()?;
12846        self.expect_token(&Token::RParen)?;
12847        let alias = self.maybe_parse_table_alias()?;
12848        Ok(TableFactor::Derived {
12849            lateral: match lateral {
12850                Lateral => true,
12851                NotLateral => false,
12852            },
12853            subquery,
12854            alias,
12855        })
12856    }
12857
12858    fn parse_aliased_function_call(&mut self) -> Result<ExprWithAlias, ParserError> {
12859        let function_name = match self.next_token().token {
12860            Token::Word(w) => Ok(w.value),
12861            _ => self.expected("a function identifier", self.peek_token()),
12862        }?;
12863        let expr = self.parse_function(ObjectName::from(vec![Ident::new(function_name)]))?;
12864        let alias = if self.parse_keyword(Keyword::AS) {
12865            Some(self.parse_identifier()?)
12866        } else {
12867            None
12868        };
12869
12870        Ok(ExprWithAlias { expr, alias })
12871    }
12872    /// Parses an expression with an optional alias
12873    ///
12874    /// Examples:
12875    ///
12876    /// ```sql
12877    /// SUM(price) AS total_price
12878    /// ```
12879    /// ```sql
12880    /// SUM(price)
12881    /// ```
12882    ///
12883    /// Example
12884    /// ```
12885    /// # use sqlparser::parser::{Parser, ParserError};
12886    /// # use sqlparser::dialect::GenericDialect;
12887    /// # fn main() ->Result<(), ParserError> {
12888    /// let sql = r#"SUM("a") as "b""#;
12889    /// let mut parser = Parser::new(&GenericDialect).try_with_sql(sql)?;
12890    /// let expr_with_alias = parser.parse_expr_with_alias()?;
12891    /// assert_eq!(Some("b".to_string()), expr_with_alias.alias.map(|x|x.value));
12892    /// # Ok(())
12893    /// # }
12894    pub fn parse_expr_with_alias(&mut self) -> Result<ExprWithAlias, ParserError> {
12895        let expr = self.parse_expr()?;
12896        let alias = if self.parse_keyword(Keyword::AS) {
12897            Some(self.parse_identifier()?)
12898        } else {
12899            None
12900        };
12901
12902        Ok(ExprWithAlias { expr, alias })
12903    }
12904
12905    pub fn parse_pivot_table_factor(
12906        &mut self,
12907        table: TableFactor,
12908    ) -> Result<TableFactor, ParserError> {
12909        self.expect_token(&Token::LParen)?;
12910        let aggregate_functions = self.parse_comma_separated(Self::parse_aliased_function_call)?;
12911        self.expect_keyword_is(Keyword::FOR)?;
12912        let value_column = self.parse_period_separated(|p| p.parse_identifier())?;
12913        self.expect_keyword_is(Keyword::IN)?;
12914
12915        self.expect_token(&Token::LParen)?;
12916        let value_source = if self.parse_keyword(Keyword::ANY) {
12917            let order_by = if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
12918                self.parse_comma_separated(Parser::parse_order_by_expr)?
12919            } else {
12920                vec![]
12921            };
12922            PivotValueSource::Any(order_by)
12923        } else if self.peek_sub_query() {
12924            PivotValueSource::Subquery(self.parse_query()?)
12925        } else {
12926            PivotValueSource::List(self.parse_comma_separated(Self::parse_expr_with_alias)?)
12927        };
12928        self.expect_token(&Token::RParen)?;
12929
12930        let default_on_null =
12931            if self.parse_keywords(&[Keyword::DEFAULT, Keyword::ON, Keyword::NULL]) {
12932                self.expect_token(&Token::LParen)?;
12933                let expr = self.parse_expr()?;
12934                self.expect_token(&Token::RParen)?;
12935                Some(expr)
12936            } else {
12937                None
12938            };
12939
12940        self.expect_token(&Token::RParen)?;
12941        let alias = self.maybe_parse_table_alias()?;
12942        Ok(TableFactor::Pivot {
12943            table: Box::new(table),
12944            aggregate_functions,
12945            value_column,
12946            value_source,
12947            default_on_null,
12948            alias,
12949        })
12950    }
12951
12952    pub fn parse_unpivot_table_factor(
12953        &mut self,
12954        table: TableFactor,
12955    ) -> Result<TableFactor, ParserError> {
12956        self.expect_token(&Token::LParen)?;
12957        let value = self.parse_identifier()?;
12958        self.expect_keyword_is(Keyword::FOR)?;
12959        let name = self.parse_identifier()?;
12960        self.expect_keyword_is(Keyword::IN)?;
12961        let columns = self.parse_parenthesized_column_list(Mandatory, false)?;
12962        self.expect_token(&Token::RParen)?;
12963        let alias = self.maybe_parse_table_alias()?;
12964        Ok(TableFactor::Unpivot {
12965            table: Box::new(table),
12966            value,
12967            name,
12968            columns,
12969            alias,
12970        })
12971    }
12972
12973    pub fn parse_join_constraint(&mut self, natural: bool) -> Result<JoinConstraint, ParserError> {
12974        if natural {
12975            Ok(JoinConstraint::Natural)
12976        } else if self.parse_keyword(Keyword::ON) {
12977            let constraint = self.parse_expr()?;
12978            Ok(JoinConstraint::On(constraint))
12979        } else if self.parse_keyword(Keyword::USING) {
12980            let columns = self.parse_parenthesized_qualified_column_list(Mandatory, false)?;
12981            Ok(JoinConstraint::Using(columns))
12982        } else {
12983            Ok(JoinConstraint::None)
12984            //self.expected("ON, or USING after JOIN", self.peek_token())
12985        }
12986    }
12987
12988    /// Parse a GRANT statement.
12989    pub fn parse_grant(&mut self) -> Result<Statement, ParserError> {
12990        let (privileges, objects) = self.parse_grant_revoke_privileges_objects()?;
12991
12992        self.expect_keyword_is(Keyword::TO)?;
12993        let grantees = self.parse_grantees()?;
12994
12995        let with_grant_option =
12996            self.parse_keywords(&[Keyword::WITH, Keyword::GRANT, Keyword::OPTION]);
12997
12998        let granted_by = self
12999            .parse_keywords(&[Keyword::GRANTED, Keyword::BY])
13000            .then(|| self.parse_identifier().unwrap());
13001
13002        Ok(Statement::Grant {
13003            privileges,
13004            objects,
13005            grantees,
13006            with_grant_option,
13007            granted_by,
13008        })
13009    }
13010
13011    fn parse_grantees(&mut self) -> Result<Vec<Grantee>, ParserError> {
13012        let mut values = vec![];
13013        let mut grantee_type = GranteesType::None;
13014        loop {
13015            grantee_type = if self.parse_keyword(Keyword::ROLE) {
13016                GranteesType::Role
13017            } else if self.parse_keyword(Keyword::USER) {
13018                GranteesType::User
13019            } else if self.parse_keyword(Keyword::SHARE) {
13020                GranteesType::Share
13021            } else if self.parse_keyword(Keyword::GROUP) {
13022                GranteesType::Group
13023            } else if self.parse_keyword(Keyword::PUBLIC) {
13024                GranteesType::Public
13025            } else if self.parse_keywords(&[Keyword::DATABASE, Keyword::ROLE]) {
13026                GranteesType::DatabaseRole
13027            } else if self.parse_keywords(&[Keyword::APPLICATION, Keyword::ROLE]) {
13028                GranteesType::ApplicationRole
13029            } else if self.parse_keyword(Keyword::APPLICATION) {
13030                GranteesType::Application
13031            } else {
13032                grantee_type // keep from previous iteraton, if not specified
13033            };
13034
13035            let grantee = if grantee_type == GranteesType::Public {
13036                Grantee {
13037                    grantee_type: grantee_type.clone(),
13038                    name: None,
13039                }
13040            } else {
13041                let mut name = self.parse_grantee_name()?;
13042                if self.consume_token(&Token::Colon) {
13043                    // Redshift supports namespace prefix for external users and groups:
13044                    // <Namespace>:<GroupName> or <Namespace>:<UserName>
13045                    // https://docs.aws.amazon.com/redshift/latest/mgmt/redshift-iam-access-control-native-idp.html
13046                    let ident = self.parse_identifier()?;
13047                    if let GranteeName::ObjectName(namespace) = name {
13048                        name = GranteeName::ObjectName(ObjectName::from(vec![Ident::new(
13049                            format!("{}:{}", namespace, ident),
13050                        )]));
13051                    };
13052                }
13053                Grantee {
13054                    grantee_type: grantee_type.clone(),
13055                    name: Some(name),
13056                }
13057            };
13058
13059            values.push(grantee);
13060
13061            if !self.consume_token(&Token::Comma) {
13062                break;
13063            }
13064        }
13065
13066        Ok(values)
13067    }
13068
13069    pub fn parse_grant_revoke_privileges_objects(
13070        &mut self,
13071    ) -> Result<(Privileges, Option<GrantObjects>), ParserError> {
13072        let privileges = if self.parse_keyword(Keyword::ALL) {
13073            Privileges::All {
13074                with_privileges_keyword: self.parse_keyword(Keyword::PRIVILEGES),
13075            }
13076        } else {
13077            let actions = self.parse_actions_list()?;
13078            Privileges::Actions(actions)
13079        };
13080
13081        let objects = if self.parse_keyword(Keyword::ON) {
13082            if self.parse_keywords(&[Keyword::ALL, Keyword::TABLES, Keyword::IN, Keyword::SCHEMA]) {
13083                Some(GrantObjects::AllTablesInSchema {
13084                    schemas: self.parse_comma_separated(|p| p.parse_object_name(false))?,
13085                })
13086            } else if self.parse_keywords(&[
13087                Keyword::ALL,
13088                Keyword::SEQUENCES,
13089                Keyword::IN,
13090                Keyword::SCHEMA,
13091            ]) {
13092                Some(GrantObjects::AllSequencesInSchema {
13093                    schemas: self.parse_comma_separated(|p| p.parse_object_name(false))?,
13094                })
13095            } else if self.parse_keywords(&[Keyword::RESOURCE, Keyword::MONITOR]) {
13096                Some(GrantObjects::ResourceMonitors(self.parse_comma_separated(
13097                    |p| p.parse_object_name_with_wildcards(false, true),
13098                )?))
13099            } else if self.parse_keywords(&[Keyword::COMPUTE, Keyword::POOL]) {
13100                Some(GrantObjects::ComputePools(self.parse_comma_separated(
13101                    |p| p.parse_object_name_with_wildcards(false, true),
13102                )?))
13103            } else if self.parse_keywords(&[Keyword::FAILOVER, Keyword::GROUP]) {
13104                Some(GrantObjects::FailoverGroup(self.parse_comma_separated(
13105                    |p| p.parse_object_name_with_wildcards(false, true),
13106                )?))
13107            } else if self.parse_keywords(&[Keyword::REPLICATION, Keyword::GROUP]) {
13108                Some(GrantObjects::ReplicationGroup(self.parse_comma_separated(
13109                    |p| p.parse_object_name_with_wildcards(false, true),
13110                )?))
13111            } else if self.parse_keywords(&[Keyword::EXTERNAL, Keyword::VOLUME]) {
13112                Some(GrantObjects::ExternalVolumes(self.parse_comma_separated(
13113                    |p| p.parse_object_name_with_wildcards(false, true),
13114                )?))
13115            } else {
13116                let object_type = self.parse_one_of_keywords(&[
13117                    Keyword::SEQUENCE,
13118                    Keyword::DATABASE,
13119                    Keyword::DATABASE,
13120                    Keyword::SCHEMA,
13121                    Keyword::TABLE,
13122                    Keyword::VIEW,
13123                    Keyword::WAREHOUSE,
13124                    Keyword::INTEGRATION,
13125                    Keyword::VIEW,
13126                    Keyword::WAREHOUSE,
13127                    Keyword::INTEGRATION,
13128                    Keyword::USER,
13129                    Keyword::CONNECTION,
13130                ]);
13131                let objects =
13132                    self.parse_comma_separated(|p| p.parse_object_name_with_wildcards(false, true));
13133                match object_type {
13134                    Some(Keyword::DATABASE) => Some(GrantObjects::Databases(objects?)),
13135                    Some(Keyword::SCHEMA) => Some(GrantObjects::Schemas(objects?)),
13136                    Some(Keyword::SEQUENCE) => Some(GrantObjects::Sequences(objects?)),
13137                    Some(Keyword::WAREHOUSE) => Some(GrantObjects::Warehouses(objects?)),
13138                    Some(Keyword::INTEGRATION) => Some(GrantObjects::Integrations(objects?)),
13139                    Some(Keyword::VIEW) => Some(GrantObjects::Views(objects?)),
13140                    Some(Keyword::USER) => Some(GrantObjects::Users(objects?)),
13141                    Some(Keyword::CONNECTION) => Some(GrantObjects::Connections(objects?)),
13142                    Some(Keyword::TABLE) | None => Some(GrantObjects::Tables(objects?)),
13143                    _ => unreachable!(),
13144                }
13145            }
13146        } else {
13147            None
13148        };
13149
13150        Ok((privileges, objects))
13151    }
13152
13153    pub fn parse_grant_permission(&mut self) -> Result<Action, ParserError> {
13154        fn parse_columns(parser: &mut Parser) -> Result<Option<Vec<Ident>>, ParserError> {
13155            let columns = parser.parse_parenthesized_column_list(Optional, false)?;
13156            if columns.is_empty() {
13157                Ok(None)
13158            } else {
13159                Ok(Some(columns))
13160            }
13161        }
13162
13163        // Multi-word privileges
13164        if self.parse_keywords(&[Keyword::IMPORTED, Keyword::PRIVILEGES]) {
13165            Ok(Action::ImportedPrivileges)
13166        } else if self.parse_keywords(&[Keyword::ADD, Keyword::SEARCH, Keyword::OPTIMIZATION]) {
13167            Ok(Action::AddSearchOptimization)
13168        } else if self.parse_keywords(&[Keyword::ATTACH, Keyword::LISTING]) {
13169            Ok(Action::AttachListing)
13170        } else if self.parse_keywords(&[Keyword::ATTACH, Keyword::POLICY]) {
13171            Ok(Action::AttachPolicy)
13172        } else if self.parse_keywords(&[Keyword::BIND, Keyword::SERVICE, Keyword::ENDPOINT]) {
13173            Ok(Action::BindServiceEndpoint)
13174        } else if self.parse_keywords(&[Keyword::DATABASE, Keyword::ROLE]) {
13175            let role = self.parse_object_name(false)?;
13176            Ok(Action::DatabaseRole { role })
13177        } else if self.parse_keywords(&[Keyword::EVOLVE, Keyword::SCHEMA]) {
13178            Ok(Action::EvolveSchema)
13179        } else if self.parse_keywords(&[Keyword::IMPORT, Keyword::SHARE]) {
13180            Ok(Action::ImportShare)
13181        } else if self.parse_keywords(&[Keyword::MANAGE, Keyword::VERSIONS]) {
13182            Ok(Action::ManageVersions)
13183        } else if self.parse_keywords(&[Keyword::MANAGE, Keyword::RELEASES]) {
13184            Ok(Action::ManageReleases)
13185        } else if self.parse_keywords(&[Keyword::OVERRIDE, Keyword::SHARE, Keyword::RESTRICTIONS]) {
13186            Ok(Action::OverrideShareRestrictions)
13187        } else if self.parse_keywords(&[
13188            Keyword::PURCHASE,
13189            Keyword::DATA,
13190            Keyword::EXCHANGE,
13191            Keyword::LISTING,
13192        ]) {
13193            Ok(Action::PurchaseDataExchangeListing)
13194        } else if self.parse_keywords(&[Keyword::RESOLVE, Keyword::ALL]) {
13195            Ok(Action::ResolveAll)
13196        } else if self.parse_keywords(&[Keyword::READ, Keyword::SESSION]) {
13197            Ok(Action::ReadSession)
13198
13199        // Single-word privileges
13200        } else if self.parse_keyword(Keyword::APPLY) {
13201            let apply_type = self.parse_action_apply_type()?;
13202            Ok(Action::Apply { apply_type })
13203        } else if self.parse_keyword(Keyword::APPLYBUDGET) {
13204            Ok(Action::ApplyBudget)
13205        } else if self.parse_keyword(Keyword::AUDIT) {
13206            Ok(Action::Audit)
13207        } else if self.parse_keyword(Keyword::CONNECT) {
13208            Ok(Action::Connect)
13209        } else if self.parse_keyword(Keyword::CREATE) {
13210            let obj_type = self.maybe_parse_action_create_object_type();
13211            Ok(Action::Create { obj_type })
13212        } else if self.parse_keyword(Keyword::DELETE) {
13213            Ok(Action::Delete)
13214        } else if self.parse_keyword(Keyword::EXECUTE) {
13215            let obj_type = self.maybe_parse_action_execute_obj_type();
13216            Ok(Action::Execute { obj_type })
13217        } else if self.parse_keyword(Keyword::FAILOVER) {
13218            Ok(Action::Failover)
13219        } else if self.parse_keyword(Keyword::INSERT) {
13220            Ok(Action::Insert {
13221                columns: parse_columns(self)?,
13222            })
13223        } else if self.parse_keyword(Keyword::MANAGE) {
13224            let manage_type = self.parse_action_manage_type()?;
13225            Ok(Action::Manage { manage_type })
13226        } else if self.parse_keyword(Keyword::MODIFY) {
13227            let modify_type = self.parse_action_modify_type();
13228            Ok(Action::Modify { modify_type })
13229        } else if self.parse_keyword(Keyword::MONITOR) {
13230            let monitor_type = self.parse_action_monitor_type();
13231            Ok(Action::Monitor { monitor_type })
13232        } else if self.parse_keyword(Keyword::OPERATE) {
13233            Ok(Action::Operate)
13234        } else if self.parse_keyword(Keyword::REFERENCES) {
13235            Ok(Action::References {
13236                columns: parse_columns(self)?,
13237            })
13238        } else if self.parse_keyword(Keyword::READ) {
13239            Ok(Action::Read)
13240        } else if self.parse_keyword(Keyword::REPLICATE) {
13241            Ok(Action::Replicate)
13242        } else if self.parse_keyword(Keyword::ROLE) {
13243            let role = self.parse_identifier()?;
13244            Ok(Action::Role { role })
13245        } else if self.parse_keyword(Keyword::SELECT) {
13246            Ok(Action::Select {
13247                columns: parse_columns(self)?,
13248            })
13249        } else if self.parse_keyword(Keyword::TEMPORARY) {
13250            Ok(Action::Temporary)
13251        } else if self.parse_keyword(Keyword::TRIGGER) {
13252            Ok(Action::Trigger)
13253        } else if self.parse_keyword(Keyword::TRUNCATE) {
13254            Ok(Action::Truncate)
13255        } else if self.parse_keyword(Keyword::UPDATE) {
13256            Ok(Action::Update {
13257                columns: parse_columns(self)?,
13258            })
13259        } else if self.parse_keyword(Keyword::USAGE) {
13260            Ok(Action::Usage)
13261        } else if self.parse_keyword(Keyword::OWNERSHIP) {
13262            Ok(Action::Ownership)
13263        } else {
13264            self.expected("a privilege keyword", self.peek_token())?
13265        }
13266    }
13267
13268    fn maybe_parse_action_create_object_type(&mut self) -> Option<ActionCreateObjectType> {
13269        // Multi-word object types
13270        if self.parse_keywords(&[Keyword::APPLICATION, Keyword::PACKAGE]) {
13271            Some(ActionCreateObjectType::ApplicationPackage)
13272        } else if self.parse_keywords(&[Keyword::COMPUTE, Keyword::POOL]) {
13273            Some(ActionCreateObjectType::ComputePool)
13274        } else if self.parse_keywords(&[Keyword::DATA, Keyword::EXCHANGE, Keyword::LISTING]) {
13275            Some(ActionCreateObjectType::DataExchangeListing)
13276        } else if self.parse_keywords(&[Keyword::EXTERNAL, Keyword::VOLUME]) {
13277            Some(ActionCreateObjectType::ExternalVolume)
13278        } else if self.parse_keywords(&[Keyword::FAILOVER, Keyword::GROUP]) {
13279            Some(ActionCreateObjectType::FailoverGroup)
13280        } else if self.parse_keywords(&[Keyword::NETWORK, Keyword::POLICY]) {
13281            Some(ActionCreateObjectType::NetworkPolicy)
13282        } else if self.parse_keywords(&[Keyword::ORGANIZATION, Keyword::LISTING]) {
13283            Some(ActionCreateObjectType::OrganiationListing)
13284        } else if self.parse_keywords(&[Keyword::REPLICATION, Keyword::GROUP]) {
13285            Some(ActionCreateObjectType::ReplicationGroup)
13286        }
13287        // Single-word object types
13288        else if self.parse_keyword(Keyword::ACCOUNT) {
13289            Some(ActionCreateObjectType::Account)
13290        } else if self.parse_keyword(Keyword::APPLICATION) {
13291            Some(ActionCreateObjectType::Application)
13292        } else if self.parse_keyword(Keyword::DATABASE) {
13293            Some(ActionCreateObjectType::Database)
13294        } else if self.parse_keyword(Keyword::INTEGRATION) {
13295            Some(ActionCreateObjectType::Integration)
13296        } else if self.parse_keyword(Keyword::ROLE) {
13297            Some(ActionCreateObjectType::Role)
13298        } else if self.parse_keyword(Keyword::SHARE) {
13299            Some(ActionCreateObjectType::Share)
13300        } else if self.parse_keyword(Keyword::USER) {
13301            Some(ActionCreateObjectType::User)
13302        } else if self.parse_keyword(Keyword::WAREHOUSE) {
13303            Some(ActionCreateObjectType::Warehouse)
13304        } else {
13305            None
13306        }
13307    }
13308
13309    fn parse_action_apply_type(&mut self) -> Result<ActionApplyType, ParserError> {
13310        if self.parse_keywords(&[Keyword::AGGREGATION, Keyword::POLICY]) {
13311            Ok(ActionApplyType::AggregationPolicy)
13312        } else if self.parse_keywords(&[Keyword::AUTHENTICATION, Keyword::POLICY]) {
13313            Ok(ActionApplyType::AuthenticationPolicy)
13314        } else if self.parse_keywords(&[Keyword::JOIN, Keyword::POLICY]) {
13315            Ok(ActionApplyType::JoinPolicy)
13316        } else if self.parse_keywords(&[Keyword::MASKING, Keyword::POLICY]) {
13317            Ok(ActionApplyType::MaskingPolicy)
13318        } else if self.parse_keywords(&[Keyword::PACKAGES, Keyword::POLICY]) {
13319            Ok(ActionApplyType::PackagesPolicy)
13320        } else if self.parse_keywords(&[Keyword::PASSWORD, Keyword::POLICY]) {
13321            Ok(ActionApplyType::PasswordPolicy)
13322        } else if self.parse_keywords(&[Keyword::PROJECTION, Keyword::POLICY]) {
13323            Ok(ActionApplyType::ProjectionPolicy)
13324        } else if self.parse_keywords(&[Keyword::ROW, Keyword::ACCESS, Keyword::POLICY]) {
13325            Ok(ActionApplyType::RowAccessPolicy)
13326        } else if self.parse_keywords(&[Keyword::SESSION, Keyword::POLICY]) {
13327            Ok(ActionApplyType::SessionPolicy)
13328        } else if self.parse_keyword(Keyword::TAG) {
13329            Ok(ActionApplyType::Tag)
13330        } else {
13331            self.expected("GRANT APPLY type", self.peek_token())
13332        }
13333    }
13334
13335    fn maybe_parse_action_execute_obj_type(&mut self) -> Option<ActionExecuteObjectType> {
13336        if self.parse_keywords(&[Keyword::DATA, Keyword::METRIC, Keyword::FUNCTION]) {
13337            Some(ActionExecuteObjectType::DataMetricFunction)
13338        } else if self.parse_keywords(&[Keyword::MANAGED, Keyword::ALERT]) {
13339            Some(ActionExecuteObjectType::ManagedAlert)
13340        } else if self.parse_keywords(&[Keyword::MANAGED, Keyword::TASK]) {
13341            Some(ActionExecuteObjectType::ManagedTask)
13342        } else if self.parse_keyword(Keyword::ALERT) {
13343            Some(ActionExecuteObjectType::Alert)
13344        } else if self.parse_keyword(Keyword::TASK) {
13345            Some(ActionExecuteObjectType::Task)
13346        } else {
13347            None
13348        }
13349    }
13350
13351    fn parse_action_manage_type(&mut self) -> Result<ActionManageType, ParserError> {
13352        if self.parse_keywords(&[Keyword::ACCOUNT, Keyword::SUPPORT, Keyword::CASES]) {
13353            Ok(ActionManageType::AccountSupportCases)
13354        } else if self.parse_keywords(&[Keyword::EVENT, Keyword::SHARING]) {
13355            Ok(ActionManageType::EventSharing)
13356        } else if self.parse_keywords(&[Keyword::LISTING, Keyword::AUTO, Keyword::FULFILLMENT]) {
13357            Ok(ActionManageType::ListingAutoFulfillment)
13358        } else if self.parse_keywords(&[Keyword::ORGANIZATION, Keyword::SUPPORT, Keyword::CASES]) {
13359            Ok(ActionManageType::OrganizationSupportCases)
13360        } else if self.parse_keywords(&[Keyword::USER, Keyword::SUPPORT, Keyword::CASES]) {
13361            Ok(ActionManageType::UserSupportCases)
13362        } else if self.parse_keyword(Keyword::GRANTS) {
13363            Ok(ActionManageType::Grants)
13364        } else if self.parse_keyword(Keyword::WAREHOUSES) {
13365            Ok(ActionManageType::Warehouses)
13366        } else {
13367            self.expected("GRANT MANAGE type", self.peek_token())
13368        }
13369    }
13370
13371    fn parse_action_modify_type(&mut self) -> Option<ActionModifyType> {
13372        if self.parse_keywords(&[Keyword::LOG, Keyword::LEVEL]) {
13373            Some(ActionModifyType::LogLevel)
13374        } else if self.parse_keywords(&[Keyword::TRACE, Keyword::LEVEL]) {
13375            Some(ActionModifyType::TraceLevel)
13376        } else if self.parse_keywords(&[Keyword::SESSION, Keyword::LOG, Keyword::LEVEL]) {
13377            Some(ActionModifyType::SessionLogLevel)
13378        } else if self.parse_keywords(&[Keyword::SESSION, Keyword::TRACE, Keyword::LEVEL]) {
13379            Some(ActionModifyType::SessionTraceLevel)
13380        } else {
13381            None
13382        }
13383    }
13384
13385    fn parse_action_monitor_type(&mut self) -> Option<ActionMonitorType> {
13386        if self.parse_keyword(Keyword::EXECUTION) {
13387            Some(ActionMonitorType::Execution)
13388        } else if self.parse_keyword(Keyword::SECURITY) {
13389            Some(ActionMonitorType::Security)
13390        } else if self.parse_keyword(Keyword::USAGE) {
13391            Some(ActionMonitorType::Usage)
13392        } else {
13393            None
13394        }
13395    }
13396
13397    pub fn parse_grantee_name(&mut self) -> Result<GranteeName, ParserError> {
13398        let mut name = self.parse_object_name(false)?;
13399        if self.dialect.supports_user_host_grantee()
13400            && name.0.len() == 1
13401            && name.0[0].as_ident().is_some()
13402            && self.consume_token(&Token::AtSign)
13403        {
13404            let user = name.0.pop().unwrap().as_ident().unwrap().clone();
13405            let host = self.parse_identifier()?;
13406            Ok(GranteeName::UserHost { user, host })
13407        } else {
13408            Ok(GranteeName::ObjectName(name))
13409        }
13410    }
13411
13412    /// Parse a REVOKE statement
13413    pub fn parse_revoke(&mut self) -> Result<Statement, ParserError> {
13414        let (privileges, objects) = self.parse_grant_revoke_privileges_objects()?;
13415
13416        self.expect_keyword_is(Keyword::FROM)?;
13417        let grantees = self.parse_grantees()?;
13418
13419        let granted_by = self
13420            .parse_keywords(&[Keyword::GRANTED, Keyword::BY])
13421            .then(|| self.parse_identifier().unwrap());
13422
13423        let cascade = self.parse_cascade_option();
13424
13425        Ok(Statement::Revoke {
13426            privileges,
13427            objects,
13428            grantees,
13429            granted_by,
13430            cascade,
13431        })
13432    }
13433
13434    /// Parse an REPLACE statement
13435    pub fn parse_replace(&mut self) -> Result<Statement, ParserError> {
13436        if !dialect_of!(self is MySqlDialect | GenericDialect) {
13437            return parser_err!(
13438                "Unsupported statement REPLACE",
13439                self.peek_token().span.start
13440            );
13441        }
13442
13443        let mut insert = self.parse_insert()?;
13444        if let Statement::Insert(Insert { replace_into, .. }) = &mut insert {
13445            *replace_into = true;
13446        }
13447
13448        Ok(insert)
13449    }
13450
13451    /// Parse an INSERT statement, returning a `Box`ed SetExpr
13452    ///
13453    /// This is used to reduce the size of the stack frames in debug builds
13454    fn parse_insert_setexpr_boxed(&mut self) -> Result<Box<SetExpr>, ParserError> {
13455        Ok(Box::new(SetExpr::Insert(self.parse_insert()?)))
13456    }
13457
13458    /// Parse an INSERT statement
13459    pub fn parse_insert(&mut self) -> Result<Statement, ParserError> {
13460        let or = self.parse_conflict_clause();
13461        let priority = if !dialect_of!(self is MySqlDialect | GenericDialect) {
13462            None
13463        } else if self.parse_keyword(Keyword::LOW_PRIORITY) {
13464            Some(MysqlInsertPriority::LowPriority)
13465        } else if self.parse_keyword(Keyword::DELAYED) {
13466            Some(MysqlInsertPriority::Delayed)
13467        } else if self.parse_keyword(Keyword::HIGH_PRIORITY) {
13468            Some(MysqlInsertPriority::HighPriority)
13469        } else {
13470            None
13471        };
13472
13473        let ignore = dialect_of!(self is MySqlDialect | GenericDialect)
13474            && self.parse_keyword(Keyword::IGNORE);
13475
13476        let replace_into = false;
13477
13478        let overwrite = self.parse_keyword(Keyword::OVERWRITE);
13479        let into = self.parse_keyword(Keyword::INTO);
13480
13481        let local = self.parse_keyword(Keyword::LOCAL);
13482
13483        if self.parse_keyword(Keyword::DIRECTORY) {
13484            let path = self.parse_literal_string()?;
13485            let file_format = if self.parse_keywords(&[Keyword::STORED, Keyword::AS]) {
13486                Some(self.parse_file_format()?)
13487            } else {
13488                None
13489            };
13490            let source = self.parse_query()?;
13491            Ok(Statement::Directory {
13492                local,
13493                path,
13494                overwrite,
13495                file_format,
13496                source,
13497            })
13498        } else {
13499            // Hive lets you put table here regardless
13500            let table = self.parse_keyword(Keyword::TABLE);
13501            let table_object = self.parse_table_object()?;
13502
13503            let table_alias =
13504                if dialect_of!(self is PostgreSqlDialect) && self.parse_keyword(Keyword::AS) {
13505                    Some(self.parse_identifier()?)
13506                } else {
13507                    None
13508                };
13509
13510            let is_mysql = dialect_of!(self is MySqlDialect);
13511
13512            let (columns, partitioned, after_columns, source, assignments) = if self
13513                .parse_keywords(&[Keyword::DEFAULT, Keyword::VALUES])
13514            {
13515                (vec![], None, vec![], None, vec![])
13516            } else {
13517                let (columns, partitioned, after_columns) = if !self.peek_subquery_start() {
13518                    let columns = self.parse_parenthesized_column_list(Optional, is_mysql)?;
13519
13520                    let partitioned = self.parse_insert_partition()?;
13521                    // Hive allows you to specify columns after partitions as well if you want.
13522                    let after_columns = if dialect_of!(self is HiveDialect) {
13523                        self.parse_parenthesized_column_list(Optional, false)?
13524                    } else {
13525                        vec![]
13526                    };
13527                    (columns, partitioned, after_columns)
13528                } else {
13529                    Default::default()
13530                };
13531
13532                let (source, assignments) = if self.peek_keyword(Keyword::FORMAT)
13533                    || self.peek_keyword(Keyword::SETTINGS)
13534                {
13535                    (None, vec![])
13536                } else if self.dialect.supports_insert_set() && self.parse_keyword(Keyword::SET) {
13537                    (None, self.parse_comma_separated(Parser::parse_assignment)?)
13538                } else {
13539                    (Some(self.parse_query()?), vec![])
13540                };
13541
13542                (columns, partitioned, after_columns, source, assignments)
13543            };
13544
13545            let (format_clause, settings) = if self.dialect.supports_insert_format() {
13546                // Settings always comes before `FORMAT` for ClickHouse:
13547                // <https://clickhouse.com/docs/en/sql-reference/statements/insert-into>
13548                let settings = self.parse_settings()?;
13549
13550                let format = if self.parse_keyword(Keyword::FORMAT) {
13551                    Some(self.parse_input_format_clause()?)
13552                } else {
13553                    None
13554                };
13555
13556                (format, settings)
13557            } else {
13558                Default::default()
13559            };
13560
13561            let insert_alias = if dialect_of!(self is MySqlDialect | GenericDialect)
13562                && self.parse_keyword(Keyword::AS)
13563            {
13564                let row_alias = self.parse_object_name(false)?;
13565                let col_aliases = Some(self.parse_parenthesized_column_list(Optional, false)?);
13566                Some(InsertAliases {
13567                    row_alias,
13568                    col_aliases,
13569                })
13570            } else {
13571                None
13572            };
13573
13574            let on = if self.parse_keyword(Keyword::ON) {
13575                if self.parse_keyword(Keyword::CONFLICT) {
13576                    let conflict_target =
13577                        if self.parse_keywords(&[Keyword::ON, Keyword::CONSTRAINT]) {
13578                            Some(ConflictTarget::OnConstraint(self.parse_object_name(false)?))
13579                        } else if self.peek_token() == Token::LParen {
13580                            Some(ConflictTarget::Columns(
13581                                self.parse_parenthesized_column_list(IsOptional::Mandatory, false)?,
13582                            ))
13583                        } else {
13584                            None
13585                        };
13586
13587                    self.expect_keyword_is(Keyword::DO)?;
13588                    let action = if self.parse_keyword(Keyword::NOTHING) {
13589                        OnConflictAction::DoNothing
13590                    } else {
13591                        self.expect_keyword_is(Keyword::UPDATE)?;
13592                        self.expect_keyword_is(Keyword::SET)?;
13593                        let assignments = self.parse_comma_separated(Parser::parse_assignment)?;
13594                        let selection = if self.parse_keyword(Keyword::WHERE) {
13595                            Some(self.parse_expr()?)
13596                        } else {
13597                            None
13598                        };
13599                        OnConflictAction::DoUpdate(DoUpdate {
13600                            assignments,
13601                            selection,
13602                        })
13603                    };
13604
13605                    Some(OnInsert::OnConflict(OnConflict {
13606                        conflict_target,
13607                        action,
13608                    }))
13609                } else {
13610                    self.expect_keyword_is(Keyword::DUPLICATE)?;
13611                    self.expect_keyword_is(Keyword::KEY)?;
13612                    self.expect_keyword_is(Keyword::UPDATE)?;
13613                    let l = self.parse_comma_separated(Parser::parse_assignment)?;
13614
13615                    Some(OnInsert::DuplicateKeyUpdate(l))
13616                }
13617            } else {
13618                None
13619            };
13620
13621            let returning = if self.parse_keyword(Keyword::RETURNING) {
13622                Some(self.parse_comma_separated(Parser::parse_select_item)?)
13623            } else {
13624                None
13625            };
13626
13627            Ok(Statement::Insert(Insert {
13628                or,
13629                table: table_object,
13630                table_alias,
13631                ignore,
13632                into,
13633                overwrite,
13634                partitioned,
13635                columns,
13636                after_columns,
13637                source,
13638                assignments,
13639                has_table_keyword: table,
13640                on,
13641                returning,
13642                replace_into,
13643                priority,
13644                insert_alias,
13645                settings,
13646                format_clause,
13647            }))
13648        }
13649    }
13650
13651    // Parses input format clause used for [ClickHouse].
13652    //
13653    // <https://clickhouse.com/docs/en/interfaces/formats>
13654    pub fn parse_input_format_clause(&mut self) -> Result<InputFormatClause, ParserError> {
13655        let ident = self.parse_identifier()?;
13656        let values = self
13657            .maybe_parse(|p| p.parse_comma_separated(|p| p.parse_expr()))?
13658            .unwrap_or_default();
13659
13660        Ok(InputFormatClause { ident, values })
13661    }
13662
13663    /// Returns true if the immediate tokens look like the
13664    /// beginning of a subquery. `(SELECT ...`
13665    fn peek_subquery_start(&mut self) -> bool {
13666        let [maybe_lparen, maybe_select] = self.peek_tokens();
13667        Token::LParen == maybe_lparen
13668            && matches!(maybe_select, Token::Word(w) if w.keyword == Keyword::SELECT)
13669    }
13670
13671    fn parse_conflict_clause(&mut self) -> Option<SqliteOnConflict> {
13672        if self.parse_keywords(&[Keyword::OR, Keyword::REPLACE]) {
13673            Some(SqliteOnConflict::Replace)
13674        } else if self.parse_keywords(&[Keyword::OR, Keyword::ROLLBACK]) {
13675            Some(SqliteOnConflict::Rollback)
13676        } else if self.parse_keywords(&[Keyword::OR, Keyword::ABORT]) {
13677            Some(SqliteOnConflict::Abort)
13678        } else if self.parse_keywords(&[Keyword::OR, Keyword::FAIL]) {
13679            Some(SqliteOnConflict::Fail)
13680        } else if self.parse_keywords(&[Keyword::OR, Keyword::IGNORE]) {
13681            Some(SqliteOnConflict::Ignore)
13682        } else if self.parse_keyword(Keyword::REPLACE) {
13683            Some(SqliteOnConflict::Replace)
13684        } else {
13685            None
13686        }
13687    }
13688
13689    pub fn parse_insert_partition(&mut self) -> Result<Option<Vec<Expr>>, ParserError> {
13690        if self.parse_keyword(Keyword::PARTITION) {
13691            self.expect_token(&Token::LParen)?;
13692            let partition_cols = Some(self.parse_comma_separated(Parser::parse_expr)?);
13693            self.expect_token(&Token::RParen)?;
13694            Ok(partition_cols)
13695        } else {
13696            Ok(None)
13697        }
13698    }
13699
13700    pub fn parse_load_data_table_format(
13701        &mut self,
13702    ) -> Result<Option<HiveLoadDataFormat>, ParserError> {
13703        if self.parse_keyword(Keyword::INPUTFORMAT) {
13704            let input_format = self.parse_expr()?;
13705            self.expect_keyword_is(Keyword::SERDE)?;
13706            let serde = self.parse_expr()?;
13707            Ok(Some(HiveLoadDataFormat {
13708                input_format,
13709                serde,
13710            }))
13711        } else {
13712            Ok(None)
13713        }
13714    }
13715
13716    /// Parse an UPDATE statement, returning a `Box`ed SetExpr
13717    ///
13718    /// This is used to reduce the size of the stack frames in debug builds
13719    fn parse_update_setexpr_boxed(&mut self) -> Result<Box<SetExpr>, ParserError> {
13720        Ok(Box::new(SetExpr::Update(self.parse_update()?)))
13721    }
13722
13723    pub fn parse_update(&mut self) -> Result<Statement, ParserError> {
13724        let or = self.parse_conflict_clause();
13725        let table = self.parse_table_and_joins()?;
13726        let from_before_set = if self.parse_keyword(Keyword::FROM) {
13727            Some(UpdateTableFromKind::BeforeSet(
13728                self.parse_table_with_joins()?,
13729            ))
13730        } else {
13731            None
13732        };
13733        self.expect_keyword(Keyword::SET)?;
13734        let assignments = self.parse_comma_separated(Parser::parse_assignment)?;
13735        let from = if from_before_set.is_none() && self.parse_keyword(Keyword::FROM) {
13736            Some(UpdateTableFromKind::AfterSet(
13737                self.parse_table_with_joins()?,
13738            ))
13739        } else {
13740            from_before_set
13741        };
13742        let selection = if self.parse_keyword(Keyword::WHERE) {
13743            Some(self.parse_expr()?)
13744        } else {
13745            None
13746        };
13747        let returning = if self.parse_keyword(Keyword::RETURNING) {
13748            Some(self.parse_comma_separated(Parser::parse_select_item)?)
13749        } else {
13750            None
13751        };
13752        Ok(Statement::Update {
13753            table,
13754            assignments,
13755            from,
13756            selection,
13757            returning,
13758            or,
13759        })
13760    }
13761
13762    /// Parse a `var = expr` assignment, used in an UPDATE statement
13763    pub fn parse_assignment(&mut self) -> Result<Assignment, ParserError> {
13764        let target = self.parse_assignment_target()?;
13765        self.expect_token(&Token::Eq)?;
13766        let value = self.parse_expr()?;
13767        Ok(Assignment { target, value })
13768    }
13769
13770    /// Parse the left-hand side of an assignment, used in an UPDATE statement
13771    pub fn parse_assignment_target(&mut self) -> Result<AssignmentTarget, ParserError> {
13772        if self.consume_token(&Token::LParen) {
13773            let columns = self.parse_comma_separated(|p| p.parse_object_name(false))?;
13774            self.expect_token(&Token::RParen)?;
13775            Ok(AssignmentTarget::Tuple(columns))
13776        } else {
13777            let column = self.parse_object_name(false)?;
13778            Ok(AssignmentTarget::ColumnName(column))
13779        }
13780    }
13781
13782    pub fn parse_function_args(&mut self) -> Result<FunctionArg, ParserError> {
13783        let arg = if self.dialect.supports_named_fn_args_with_expr_name() {
13784            self.maybe_parse(|p| {
13785                let name = p.parse_expr()?;
13786                let operator = p.parse_function_named_arg_operator()?;
13787                let arg = p.parse_wildcard_expr()?.into();
13788                Ok(FunctionArg::ExprNamed {
13789                    name,
13790                    arg,
13791                    operator,
13792                })
13793            })?
13794        } else {
13795            self.maybe_parse(|p| {
13796                let name = p.parse_identifier()?;
13797                let operator = p.parse_function_named_arg_operator()?;
13798                let arg = p.parse_wildcard_expr()?.into();
13799                Ok(FunctionArg::Named {
13800                    name,
13801                    arg,
13802                    operator,
13803                })
13804            })?
13805        };
13806        if let Some(arg) = arg {
13807            return Ok(arg);
13808        }
13809        Ok(FunctionArg::Unnamed(self.parse_wildcard_expr()?.into()))
13810    }
13811
13812    fn parse_function_named_arg_operator(&mut self) -> Result<FunctionArgOperator, ParserError> {
13813        if self.parse_keyword(Keyword::VALUE) {
13814            return Ok(FunctionArgOperator::Value);
13815        }
13816        let tok = self.next_token();
13817        match tok.token {
13818            Token::RArrow if self.dialect.supports_named_fn_args_with_rarrow_operator() => {
13819                Ok(FunctionArgOperator::RightArrow)
13820            }
13821            Token::Eq if self.dialect.supports_named_fn_args_with_eq_operator() => {
13822                Ok(FunctionArgOperator::Equals)
13823            }
13824            Token::Assignment
13825                if self
13826                    .dialect
13827                    .supports_named_fn_args_with_assignment_operator() =>
13828            {
13829                Ok(FunctionArgOperator::Assignment)
13830            }
13831            Token::Colon if self.dialect.supports_named_fn_args_with_colon_operator() => {
13832                Ok(FunctionArgOperator::Colon)
13833            }
13834            _ => {
13835                self.prev_token();
13836                self.expected("argument operator", tok)
13837            }
13838        }
13839    }
13840
13841    pub fn parse_optional_args(&mut self) -> Result<Vec<FunctionArg>, ParserError> {
13842        if self.consume_token(&Token::RParen) {
13843            Ok(vec![])
13844        } else {
13845            let args = self.parse_comma_separated(Parser::parse_function_args)?;
13846            self.expect_token(&Token::RParen)?;
13847            Ok(args)
13848        }
13849    }
13850
13851    fn parse_table_function_args(&mut self) -> Result<TableFunctionArgs, ParserError> {
13852        if self.consume_token(&Token::RParen) {
13853            return Ok(TableFunctionArgs {
13854                args: vec![],
13855                settings: None,
13856            });
13857        }
13858        let mut args = vec![];
13859        let settings = loop {
13860            if let Some(settings) = self.parse_settings()? {
13861                break Some(settings);
13862            }
13863            args.push(self.parse_function_args()?);
13864            if self.is_parse_comma_separated_end() {
13865                break None;
13866            }
13867        };
13868        self.expect_token(&Token::RParen)?;
13869        Ok(TableFunctionArgs { args, settings })
13870    }
13871
13872    /// Parses a potentially empty list of arguments to a window function
13873    /// (including the closing parenthesis).
13874    ///
13875    /// Examples:
13876    /// ```sql
13877    /// FIRST_VALUE(x ORDER BY 1,2,3);
13878    /// FIRST_VALUE(x IGNORE NULL);
13879    /// ```
13880    fn parse_function_argument_list(&mut self) -> Result<FunctionArgumentList, ParserError> {
13881        let mut clauses = vec![];
13882
13883        // For MSSQL empty argument list with json-null-clause case, e.g. `JSON_ARRAY(NULL ON NULL)`
13884        if let Some(null_clause) = self.parse_json_null_clause() {
13885            clauses.push(FunctionArgumentClause::JsonNullClause(null_clause));
13886        }
13887
13888        if self.consume_token(&Token::RParen) {
13889            return Ok(FunctionArgumentList {
13890                duplicate_treatment: None,
13891                args: vec![],
13892                clauses,
13893            });
13894        }
13895
13896        let duplicate_treatment = self.parse_duplicate_treatment()?;
13897        let args = self.parse_comma_separated(Parser::parse_function_args)?;
13898
13899        if self.dialect.supports_window_function_null_treatment_arg() {
13900            if let Some(null_treatment) = self.parse_null_treatment()? {
13901                clauses.push(FunctionArgumentClause::IgnoreOrRespectNulls(null_treatment));
13902            }
13903        }
13904
13905        if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
13906            clauses.push(FunctionArgumentClause::OrderBy(
13907                self.parse_comma_separated(Parser::parse_order_by_expr)?,
13908            ));
13909        }
13910
13911        if self.parse_keyword(Keyword::LIMIT) {
13912            clauses.push(FunctionArgumentClause::Limit(self.parse_expr()?));
13913        }
13914
13915        if dialect_of!(self is GenericDialect | BigQueryDialect)
13916            && self.parse_keyword(Keyword::HAVING)
13917        {
13918            let kind = match self.expect_one_of_keywords(&[Keyword::MIN, Keyword::MAX])? {
13919                Keyword::MIN => HavingBoundKind::Min,
13920                Keyword::MAX => HavingBoundKind::Max,
13921                _ => unreachable!(),
13922            };
13923            clauses.push(FunctionArgumentClause::Having(HavingBound(
13924                kind,
13925                self.parse_expr()?,
13926            )))
13927        }
13928
13929        if dialect_of!(self is GenericDialect | MySqlDialect)
13930            && self.parse_keyword(Keyword::SEPARATOR)
13931        {
13932            clauses.push(FunctionArgumentClause::Separator(self.parse_value()?.value));
13933        }
13934
13935        if let Some(on_overflow) = self.parse_listagg_on_overflow()? {
13936            clauses.push(FunctionArgumentClause::OnOverflow(on_overflow));
13937        }
13938
13939        if let Some(null_clause) = self.parse_json_null_clause() {
13940            clauses.push(FunctionArgumentClause::JsonNullClause(null_clause));
13941        }
13942
13943        self.expect_token(&Token::RParen)?;
13944        Ok(FunctionArgumentList {
13945            duplicate_treatment,
13946            args,
13947            clauses,
13948        })
13949    }
13950
13951    /// Parses MSSQL's json-null-clause
13952    fn parse_json_null_clause(&mut self) -> Option<JsonNullClause> {
13953        if self.parse_keywords(&[Keyword::ABSENT, Keyword::ON, Keyword::NULL]) {
13954            Some(JsonNullClause::AbsentOnNull)
13955        } else if self.parse_keywords(&[Keyword::NULL, Keyword::ON, Keyword::NULL]) {
13956            Some(JsonNullClause::NullOnNull)
13957        } else {
13958            None
13959        }
13960    }
13961
13962    fn parse_duplicate_treatment(&mut self) -> Result<Option<DuplicateTreatment>, ParserError> {
13963        let loc = self.peek_token().span.start;
13964        match (
13965            self.parse_keyword(Keyword::ALL),
13966            self.parse_keyword(Keyword::DISTINCT),
13967        ) {
13968            (true, false) => Ok(Some(DuplicateTreatment::All)),
13969            (false, true) => Ok(Some(DuplicateTreatment::Distinct)),
13970            (false, false) => Ok(None),
13971            (true, true) => parser_err!("Cannot specify both ALL and DISTINCT".to_string(), loc),
13972        }
13973    }
13974
13975    /// Parse a comma-delimited list of projections after SELECT
13976    pub fn parse_select_item(&mut self) -> Result<SelectItem, ParserError> {
13977        let prefix = self
13978            .parse_one_of_keywords(
13979                self.dialect
13980                    .get_reserved_keywords_for_select_item_operator(),
13981            )
13982            .map(|keyword| Ident::new(format!("{:?}", keyword)));
13983
13984        match self.parse_wildcard_expr()? {
13985            Expr::QualifiedWildcard(prefix, token) => Ok(SelectItem::QualifiedWildcard(
13986                SelectItemQualifiedWildcardKind::ObjectName(prefix),
13987                self.parse_wildcard_additional_options(token.0)?,
13988            )),
13989            Expr::Wildcard(token) => Ok(SelectItem::Wildcard(
13990                self.parse_wildcard_additional_options(token.0)?,
13991            )),
13992            Expr::Identifier(v) if v.value.to_lowercase() == "from" && v.quote_style.is_none() => {
13993                parser_err!(
13994                    format!("Expected an expression, found: {}", v),
13995                    self.peek_token().span.start
13996                )
13997            }
13998            Expr::BinaryOp {
13999                left,
14000                op: BinaryOperator::Eq,
14001                right,
14002            } if self.dialect.supports_eq_alias_assignment()
14003                && matches!(left.as_ref(), Expr::Identifier(_)) =>
14004            {
14005                let Expr::Identifier(alias) = *left else {
14006                    return parser_err!(
14007                        "BUG: expected identifier expression as alias",
14008                        self.peek_token().span.start
14009                    );
14010                };
14011                Ok(SelectItem::ExprWithAlias {
14012                    expr: *right,
14013                    alias,
14014                })
14015            }
14016            expr if self.dialect.supports_select_expr_star()
14017                && self.consume_tokens(&[Token::Period, Token::Mul]) =>
14018            {
14019                let wildcard_token = self.get_previous_token().clone();
14020                Ok(SelectItem::QualifiedWildcard(
14021                    SelectItemQualifiedWildcardKind::Expr(expr),
14022                    self.parse_wildcard_additional_options(wildcard_token)?,
14023                ))
14024            }
14025            expr => self
14026                .maybe_parse_select_item_alias()
14027                .map(|alias| match alias {
14028                    Some(alias) => SelectItem::ExprWithAlias {
14029                        expr: maybe_prefixed_expr(expr, prefix),
14030                        alias,
14031                    },
14032                    None => SelectItem::UnnamedExpr(maybe_prefixed_expr(expr, prefix)),
14033                }),
14034        }
14035    }
14036
14037    /// Parse an [`WildcardAdditionalOptions`] information for wildcard select items.
14038    ///
14039    /// If it is not possible to parse it, will return an option.
14040    pub fn parse_wildcard_additional_options(
14041        &mut self,
14042        wildcard_token: TokenWithSpan,
14043    ) -> Result<WildcardAdditionalOptions, ParserError> {
14044        let opt_ilike = if dialect_of!(self is GenericDialect | SnowflakeDialect) {
14045            self.parse_optional_select_item_ilike()?
14046        } else {
14047            None
14048        };
14049        let opt_exclude = if opt_ilike.is_none()
14050            && dialect_of!(self is GenericDialect | DuckDbDialect | SnowflakeDialect)
14051        {
14052            self.parse_optional_select_item_exclude()?
14053        } else {
14054            None
14055        };
14056        let opt_except = if self.dialect.supports_select_wildcard_except() {
14057            self.parse_optional_select_item_except()?
14058        } else {
14059            None
14060        };
14061        let opt_replace = if dialect_of!(self is GenericDialect | BigQueryDialect | ClickHouseDialect | DuckDbDialect | SnowflakeDialect)
14062        {
14063            self.parse_optional_select_item_replace()?
14064        } else {
14065            None
14066        };
14067        let opt_rename = if dialect_of!(self is GenericDialect | SnowflakeDialect) {
14068            self.parse_optional_select_item_rename()?
14069        } else {
14070            None
14071        };
14072
14073        Ok(WildcardAdditionalOptions {
14074            wildcard_token: wildcard_token.into(),
14075            opt_ilike,
14076            opt_exclude,
14077            opt_except,
14078            opt_rename,
14079            opt_replace,
14080        })
14081    }
14082
14083    /// Parse an [`Ilike`](IlikeSelectItem) information for wildcard select items.
14084    ///
14085    /// If it is not possible to parse it, will return an option.
14086    pub fn parse_optional_select_item_ilike(
14087        &mut self,
14088    ) -> Result<Option<IlikeSelectItem>, ParserError> {
14089        let opt_ilike = if self.parse_keyword(Keyword::ILIKE) {
14090            let next_token = self.next_token();
14091            let pattern = match next_token.token {
14092                Token::SingleQuotedString(s) => s,
14093                _ => return self.expected("ilike pattern", next_token),
14094            };
14095            Some(IlikeSelectItem { pattern })
14096        } else {
14097            None
14098        };
14099        Ok(opt_ilike)
14100    }
14101
14102    /// Parse an [`Exclude`](ExcludeSelectItem) information for wildcard select items.
14103    ///
14104    /// If it is not possible to parse it, will return an option.
14105    pub fn parse_optional_select_item_exclude(
14106        &mut self,
14107    ) -> Result<Option<ExcludeSelectItem>, ParserError> {
14108        let opt_exclude = if self.parse_keyword(Keyword::EXCLUDE) {
14109            if self.consume_token(&Token::LParen) {
14110                let columns = self.parse_comma_separated(|parser| parser.parse_identifier())?;
14111                self.expect_token(&Token::RParen)?;
14112                Some(ExcludeSelectItem::Multiple(columns))
14113            } else {
14114                let column = self.parse_identifier()?;
14115                Some(ExcludeSelectItem::Single(column))
14116            }
14117        } else {
14118            None
14119        };
14120
14121        Ok(opt_exclude)
14122    }
14123
14124    /// Parse an [`Except`](ExceptSelectItem) information for wildcard select items.
14125    ///
14126    /// If it is not possible to parse it, will return an option.
14127    pub fn parse_optional_select_item_except(
14128        &mut self,
14129    ) -> Result<Option<ExceptSelectItem>, ParserError> {
14130        let opt_except = if self.parse_keyword(Keyword::EXCEPT) {
14131            if self.peek_token().token == Token::LParen {
14132                let idents = self.parse_parenthesized_column_list(Mandatory, false)?;
14133                match &idents[..] {
14134                    [] => {
14135                        return self.expected(
14136                            "at least one column should be parsed by the expect clause",
14137                            self.peek_token(),
14138                        )?;
14139                    }
14140                    [first, idents @ ..] => Some(ExceptSelectItem {
14141                        first_element: first.clone(),
14142                        additional_elements: idents.to_vec(),
14143                    }),
14144                }
14145            } else {
14146                // Clickhouse allows EXCEPT column_name
14147                let ident = self.parse_identifier()?;
14148                Some(ExceptSelectItem {
14149                    first_element: ident,
14150                    additional_elements: vec![],
14151                })
14152            }
14153        } else {
14154            None
14155        };
14156
14157        Ok(opt_except)
14158    }
14159
14160    /// Parse a [`Rename`](RenameSelectItem) information for wildcard select items.
14161    pub fn parse_optional_select_item_rename(
14162        &mut self,
14163    ) -> Result<Option<RenameSelectItem>, ParserError> {
14164        let opt_rename = if self.parse_keyword(Keyword::RENAME) {
14165            if self.consume_token(&Token::LParen) {
14166                let idents =
14167                    self.parse_comma_separated(|parser| parser.parse_identifier_with_alias())?;
14168                self.expect_token(&Token::RParen)?;
14169                Some(RenameSelectItem::Multiple(idents))
14170            } else {
14171                let ident = self.parse_identifier_with_alias()?;
14172                Some(RenameSelectItem::Single(ident))
14173            }
14174        } else {
14175            None
14176        };
14177
14178        Ok(opt_rename)
14179    }
14180
14181    /// Parse a [`Replace`](ReplaceSelectItem) information for wildcard select items.
14182    pub fn parse_optional_select_item_replace(
14183        &mut self,
14184    ) -> Result<Option<ReplaceSelectItem>, ParserError> {
14185        let opt_replace = if self.parse_keyword(Keyword::REPLACE) {
14186            if self.consume_token(&Token::LParen) {
14187                let items = self.parse_comma_separated(|parser| {
14188                    Ok(Box::new(parser.parse_replace_elements()?))
14189                })?;
14190                self.expect_token(&Token::RParen)?;
14191                Some(ReplaceSelectItem { items })
14192            } else {
14193                let tok = self.next_token();
14194                return self.expected("( after REPLACE but", tok);
14195            }
14196        } else {
14197            None
14198        };
14199
14200        Ok(opt_replace)
14201    }
14202    pub fn parse_replace_elements(&mut self) -> Result<ReplaceSelectElement, ParserError> {
14203        let expr = self.parse_expr()?;
14204        let as_keyword = self.parse_keyword(Keyword::AS);
14205        let ident = self.parse_identifier()?;
14206        Ok(ReplaceSelectElement {
14207            expr,
14208            column_name: ident,
14209            as_keyword,
14210        })
14211    }
14212
14213    /// Parse ASC or DESC, returns an Option with true if ASC, false of DESC or `None` if none of
14214    /// them.
14215    pub fn parse_asc_desc(&mut self) -> Option<bool> {
14216        if self.parse_keyword(Keyword::ASC) {
14217            Some(true)
14218        } else if self.parse_keyword(Keyword::DESC) {
14219            Some(false)
14220        } else {
14221            None
14222        }
14223    }
14224
14225    /// Parse an [OrderByExpr] expression.
14226    pub fn parse_order_by_expr(&mut self) -> Result<OrderByExpr, ParserError> {
14227        self.parse_order_by_expr_inner(false)
14228            .map(|(order_by, _)| order_by)
14229    }
14230
14231    /// Parse an [IndexColumn].
14232    pub fn parse_create_index_expr(&mut self) -> Result<IndexColumn, ParserError> {
14233        self.parse_order_by_expr_inner(true)
14234            .map(|(column, operator_class)| IndexColumn {
14235                column,
14236                operator_class,
14237            })
14238    }
14239
14240    fn parse_order_by_expr_inner(
14241        &mut self,
14242        with_operator_class: bool,
14243    ) -> Result<(OrderByExpr, Option<Ident>), ParserError> {
14244        let expr = self.parse_expr()?;
14245
14246        let operator_class: Option<Ident> = if with_operator_class {
14247            // We check that if non of the following keywords are present, then we parse an
14248            // identifier as operator class.
14249            if self
14250                .peek_one_of_keywords(&[Keyword::ASC, Keyword::DESC, Keyword::NULLS, Keyword::WITH])
14251                .is_some()
14252            {
14253                None
14254            } else {
14255                self.maybe_parse(|parser| parser.parse_identifier())?
14256            }
14257        } else {
14258            None
14259        };
14260
14261        let options = self.parse_order_by_options()?;
14262
14263        let with_fill = if dialect_of!(self is ClickHouseDialect | GenericDialect)
14264            && self.parse_keywords(&[Keyword::WITH, Keyword::FILL])
14265        {
14266            Some(self.parse_with_fill()?)
14267        } else {
14268            None
14269        };
14270
14271        Ok((
14272            OrderByExpr {
14273                expr,
14274                options,
14275                with_fill,
14276            },
14277            operator_class,
14278        ))
14279    }
14280
14281    fn parse_order_by_options(&mut self) -> Result<OrderByOptions, ParserError> {
14282        let asc = self.parse_asc_desc();
14283
14284        let nulls_first = if self.parse_keywords(&[Keyword::NULLS, Keyword::FIRST]) {
14285            Some(true)
14286        } else if self.parse_keywords(&[Keyword::NULLS, Keyword::LAST]) {
14287            Some(false)
14288        } else {
14289            None
14290        };
14291
14292        Ok(OrderByOptions { asc, nulls_first })
14293    }
14294
14295    // Parse a WITH FILL clause (ClickHouse dialect)
14296    // that follow the WITH FILL keywords in a ORDER BY clause
14297    pub fn parse_with_fill(&mut self) -> Result<WithFill, ParserError> {
14298        let from = if self.parse_keyword(Keyword::FROM) {
14299            Some(self.parse_expr()?)
14300        } else {
14301            None
14302        };
14303
14304        let to = if self.parse_keyword(Keyword::TO) {
14305            Some(self.parse_expr()?)
14306        } else {
14307            None
14308        };
14309
14310        let step = if self.parse_keyword(Keyword::STEP) {
14311            Some(self.parse_expr()?)
14312        } else {
14313            None
14314        };
14315
14316        Ok(WithFill { from, to, step })
14317    }
14318
14319    // Parse a set of comma separated INTERPOLATE expressions (ClickHouse dialect)
14320    // that follow the INTERPOLATE keyword in an ORDER BY clause with the WITH FILL modifier
14321    pub fn parse_interpolations(&mut self) -> Result<Option<Interpolate>, ParserError> {
14322        if !self.parse_keyword(Keyword::INTERPOLATE) {
14323            return Ok(None);
14324        }
14325
14326        if self.consume_token(&Token::LParen) {
14327            let interpolations =
14328                self.parse_comma_separated0(|p| p.parse_interpolation(), Token::RParen)?;
14329            self.expect_token(&Token::RParen)?;
14330            // INTERPOLATE () and INTERPOLATE ( ... ) variants
14331            return Ok(Some(Interpolate {
14332                exprs: Some(interpolations),
14333            }));
14334        }
14335
14336        // INTERPOLATE
14337        Ok(Some(Interpolate { exprs: None }))
14338    }
14339
14340    // Parse a INTERPOLATE expression (ClickHouse dialect)
14341    pub fn parse_interpolation(&mut self) -> Result<InterpolateExpr, ParserError> {
14342        let column = self.parse_identifier()?;
14343        let expr = if self.parse_keyword(Keyword::AS) {
14344            Some(self.parse_expr()?)
14345        } else {
14346            None
14347        };
14348        Ok(InterpolateExpr { column, expr })
14349    }
14350
14351    /// Parse a TOP clause, MSSQL equivalent of LIMIT,
14352    /// that follows after `SELECT [DISTINCT]`.
14353    pub fn parse_top(&mut self) -> Result<Top, ParserError> {
14354        let quantity = if self.consume_token(&Token::LParen) {
14355            let quantity = self.parse_expr()?;
14356            self.expect_token(&Token::RParen)?;
14357            Some(TopQuantity::Expr(quantity))
14358        } else {
14359            let next_token = self.next_token();
14360            let quantity = match next_token.token {
14361                Token::Number(s, _) => Self::parse::<u64>(s, next_token.span.start)?,
14362                _ => self.expected("literal int", next_token)?,
14363            };
14364            Some(TopQuantity::Constant(quantity))
14365        };
14366
14367        let percent = self.parse_keyword(Keyword::PERCENT);
14368
14369        let with_ties = self.parse_keywords(&[Keyword::WITH, Keyword::TIES]);
14370
14371        Ok(Top {
14372            with_ties,
14373            percent,
14374            quantity,
14375        })
14376    }
14377
14378    /// Parse a LIMIT clause
14379    pub fn parse_limit(&mut self) -> Result<Option<Expr>, ParserError> {
14380        if self.parse_keyword(Keyword::ALL) {
14381            Ok(None)
14382        } else {
14383            Ok(Some(self.parse_expr()?))
14384        }
14385    }
14386
14387    /// Parse an OFFSET clause
14388    pub fn parse_offset(&mut self) -> Result<Offset, ParserError> {
14389        let value = self.parse_expr()?;
14390        let rows = if self.parse_keyword(Keyword::ROW) {
14391            OffsetRows::Row
14392        } else if self.parse_keyword(Keyword::ROWS) {
14393            OffsetRows::Rows
14394        } else {
14395            OffsetRows::None
14396        };
14397        Ok(Offset { value, rows })
14398    }
14399
14400    /// Parse a FETCH clause
14401    pub fn parse_fetch(&mut self) -> Result<Fetch, ParserError> {
14402        self.expect_one_of_keywords(&[Keyword::FIRST, Keyword::NEXT])?;
14403        let (quantity, percent) = if self
14404            .parse_one_of_keywords(&[Keyword::ROW, Keyword::ROWS])
14405            .is_some()
14406        {
14407            (None, false)
14408        } else {
14409            let quantity = Expr::Value(self.parse_value()?);
14410            let percent = self.parse_keyword(Keyword::PERCENT);
14411            self.expect_one_of_keywords(&[Keyword::ROW, Keyword::ROWS])?;
14412            (Some(quantity), percent)
14413        };
14414        let with_ties = if self.parse_keyword(Keyword::ONLY) {
14415            false
14416        } else if self.parse_keywords(&[Keyword::WITH, Keyword::TIES]) {
14417            true
14418        } else {
14419            return self.expected("one of ONLY or WITH TIES", self.peek_token());
14420        };
14421        Ok(Fetch {
14422            with_ties,
14423            percent,
14424            quantity,
14425        })
14426    }
14427
14428    /// Parse a FOR UPDATE/FOR SHARE clause
14429    pub fn parse_lock(&mut self) -> Result<LockClause, ParserError> {
14430        let lock_type = match self.expect_one_of_keywords(&[Keyword::UPDATE, Keyword::SHARE])? {
14431            Keyword::UPDATE => LockType::Update,
14432            Keyword::SHARE => LockType::Share,
14433            _ => unreachable!(),
14434        };
14435        let of = if self.parse_keyword(Keyword::OF) {
14436            Some(self.parse_object_name(false)?)
14437        } else {
14438            None
14439        };
14440        let nonblock = if self.parse_keyword(Keyword::NOWAIT) {
14441            Some(NonBlock::Nowait)
14442        } else if self.parse_keywords(&[Keyword::SKIP, Keyword::LOCKED]) {
14443            Some(NonBlock::SkipLocked)
14444        } else {
14445            None
14446        };
14447        Ok(LockClause {
14448            lock_type,
14449            of,
14450            nonblock,
14451        })
14452    }
14453
14454    pub fn parse_values(&mut self, allow_empty: bool) -> Result<Values, ParserError> {
14455        let mut explicit_row = false;
14456
14457        let rows = self.parse_comma_separated(|parser| {
14458            if parser.parse_keyword(Keyword::ROW) {
14459                explicit_row = true;
14460            }
14461
14462            parser.expect_token(&Token::LParen)?;
14463            if allow_empty && parser.peek_token().token == Token::RParen {
14464                parser.next_token();
14465                Ok(vec![])
14466            } else {
14467                let exprs = parser.parse_comma_separated(Parser::parse_expr)?;
14468                parser.expect_token(&Token::RParen)?;
14469                Ok(exprs)
14470            }
14471        })?;
14472        Ok(Values { explicit_row, rows })
14473    }
14474
14475    pub fn parse_start_transaction(&mut self) -> Result<Statement, ParserError> {
14476        self.expect_keyword_is(Keyword::TRANSACTION)?;
14477        Ok(Statement::StartTransaction {
14478            modes: self.parse_transaction_modes()?,
14479            begin: false,
14480            transaction: Some(BeginTransactionKind::Transaction),
14481            modifier: None,
14482            statements: vec![],
14483            exception_statements: None,
14484            has_end_keyword: false,
14485        })
14486    }
14487
14488    pub fn parse_begin(&mut self) -> Result<Statement, ParserError> {
14489        let modifier = if !self.dialect.supports_start_transaction_modifier() {
14490            None
14491        } else if self.parse_keyword(Keyword::DEFERRED) {
14492            Some(TransactionModifier::Deferred)
14493        } else if self.parse_keyword(Keyword::IMMEDIATE) {
14494            Some(TransactionModifier::Immediate)
14495        } else if self.parse_keyword(Keyword::EXCLUSIVE) {
14496            Some(TransactionModifier::Exclusive)
14497        } else if self.parse_keyword(Keyword::TRY) {
14498            Some(TransactionModifier::Try)
14499        } else if self.parse_keyword(Keyword::CATCH) {
14500            Some(TransactionModifier::Catch)
14501        } else {
14502            None
14503        };
14504        let transaction = match self.parse_one_of_keywords(&[Keyword::TRANSACTION, Keyword::WORK]) {
14505            Some(Keyword::TRANSACTION) => Some(BeginTransactionKind::Transaction),
14506            Some(Keyword::WORK) => Some(BeginTransactionKind::Work),
14507            _ => None,
14508        };
14509        Ok(Statement::StartTransaction {
14510            modes: self.parse_transaction_modes()?,
14511            begin: true,
14512            transaction,
14513            modifier,
14514            statements: vec![],
14515            exception_statements: None,
14516            has_end_keyword: false,
14517        })
14518    }
14519
14520    pub fn parse_end(&mut self) -> Result<Statement, ParserError> {
14521        let modifier = if !self.dialect.supports_end_transaction_modifier() {
14522            None
14523        } else if self.parse_keyword(Keyword::TRY) {
14524            Some(TransactionModifier::Try)
14525        } else if self.parse_keyword(Keyword::CATCH) {
14526            Some(TransactionModifier::Catch)
14527        } else {
14528            None
14529        };
14530        Ok(Statement::Commit {
14531            chain: self.parse_commit_rollback_chain()?,
14532            end: true,
14533            modifier,
14534        })
14535    }
14536
14537    pub fn parse_transaction_modes(&mut self) -> Result<Vec<TransactionMode>, ParserError> {
14538        let mut modes = vec![];
14539        let mut required = false;
14540        loop {
14541            let mode = if self.parse_keywords(&[Keyword::ISOLATION, Keyword::LEVEL]) {
14542                let iso_level = if self.parse_keywords(&[Keyword::READ, Keyword::UNCOMMITTED]) {
14543                    TransactionIsolationLevel::ReadUncommitted
14544                } else if self.parse_keywords(&[Keyword::READ, Keyword::COMMITTED]) {
14545                    TransactionIsolationLevel::ReadCommitted
14546                } else if self.parse_keywords(&[Keyword::REPEATABLE, Keyword::READ]) {
14547                    TransactionIsolationLevel::RepeatableRead
14548                } else if self.parse_keyword(Keyword::SERIALIZABLE) {
14549                    TransactionIsolationLevel::Serializable
14550                } else if self.parse_keyword(Keyword::SNAPSHOT) {
14551                    TransactionIsolationLevel::Snapshot
14552                } else {
14553                    self.expected("isolation level", self.peek_token())?
14554                };
14555                TransactionMode::IsolationLevel(iso_level)
14556            } else if self.parse_keywords(&[Keyword::READ, Keyword::ONLY]) {
14557                TransactionMode::AccessMode(TransactionAccessMode::ReadOnly)
14558            } else if self.parse_keywords(&[Keyword::READ, Keyword::WRITE]) {
14559                TransactionMode::AccessMode(TransactionAccessMode::ReadWrite)
14560            } else if required {
14561                self.expected("transaction mode", self.peek_token())?
14562            } else {
14563                break;
14564            };
14565            modes.push(mode);
14566            // ANSI requires a comma after each transaction mode, but
14567            // PostgreSQL, for historical reasons, does not. We follow
14568            // PostgreSQL in making the comma optional, since that is strictly
14569            // more general.
14570            required = self.consume_token(&Token::Comma);
14571        }
14572        Ok(modes)
14573    }
14574
14575    pub fn parse_commit(&mut self) -> Result<Statement, ParserError> {
14576        Ok(Statement::Commit {
14577            chain: self.parse_commit_rollback_chain()?,
14578            end: false,
14579            modifier: None,
14580        })
14581    }
14582
14583    pub fn parse_rollback(&mut self) -> Result<Statement, ParserError> {
14584        let chain = self.parse_commit_rollback_chain()?;
14585        let savepoint = self.parse_rollback_savepoint()?;
14586
14587        Ok(Statement::Rollback { chain, savepoint })
14588    }
14589
14590    pub fn parse_commit_rollback_chain(&mut self) -> Result<bool, ParserError> {
14591        let _ = self.parse_one_of_keywords(&[Keyword::TRANSACTION, Keyword::WORK]);
14592        if self.parse_keyword(Keyword::AND) {
14593            let chain = !self.parse_keyword(Keyword::NO);
14594            self.expect_keyword_is(Keyword::CHAIN)?;
14595            Ok(chain)
14596        } else {
14597            Ok(false)
14598        }
14599    }
14600
14601    pub fn parse_rollback_savepoint(&mut self) -> Result<Option<Ident>, ParserError> {
14602        if self.parse_keyword(Keyword::TO) {
14603            let _ = self.parse_keyword(Keyword::SAVEPOINT);
14604            let savepoint = self.parse_identifier()?;
14605
14606            Ok(Some(savepoint))
14607        } else {
14608            Ok(None)
14609        }
14610    }
14611
14612    /// Parse a 'RAISERROR' statement
14613    pub fn parse_raiserror(&mut self) -> Result<Statement, ParserError> {
14614        self.expect_token(&Token::LParen)?;
14615        let message = Box::new(self.parse_expr()?);
14616        self.expect_token(&Token::Comma)?;
14617        let severity = Box::new(self.parse_expr()?);
14618        self.expect_token(&Token::Comma)?;
14619        let state = Box::new(self.parse_expr()?);
14620        let arguments = if self.consume_token(&Token::Comma) {
14621            self.parse_comma_separated(Parser::parse_expr)?
14622        } else {
14623            vec![]
14624        };
14625        self.expect_token(&Token::RParen)?;
14626        let options = if self.parse_keyword(Keyword::WITH) {
14627            self.parse_comma_separated(Parser::parse_raiserror_option)?
14628        } else {
14629            vec![]
14630        };
14631        Ok(Statement::RaisError {
14632            message,
14633            severity,
14634            state,
14635            arguments,
14636            options,
14637        })
14638    }
14639
14640    pub fn parse_raiserror_option(&mut self) -> Result<RaisErrorOption, ParserError> {
14641        match self.expect_one_of_keywords(&[Keyword::LOG, Keyword::NOWAIT, Keyword::SETERROR])? {
14642            Keyword::LOG => Ok(RaisErrorOption::Log),
14643            Keyword::NOWAIT => Ok(RaisErrorOption::NoWait),
14644            Keyword::SETERROR => Ok(RaisErrorOption::SetError),
14645            _ => self.expected(
14646                "LOG, NOWAIT OR SETERROR raiserror option",
14647                self.peek_token(),
14648            ),
14649        }
14650    }
14651
14652    pub fn parse_deallocate(&mut self) -> Result<Statement, ParserError> {
14653        let prepare = self.parse_keyword(Keyword::PREPARE);
14654        let name = self.parse_identifier()?;
14655        Ok(Statement::Deallocate { name, prepare })
14656    }
14657
14658    pub fn parse_execute(&mut self) -> Result<Statement, ParserError> {
14659        let name = if self.dialect.supports_execute_immediate()
14660            && self.parse_keyword(Keyword::IMMEDIATE)
14661        {
14662            None
14663        } else {
14664            let name = self.parse_object_name(false)?;
14665            Some(name)
14666        };
14667
14668        let has_parentheses = self.consume_token(&Token::LParen);
14669
14670        let end_token = match (has_parentheses, self.peek_token().token) {
14671            (true, _) => Token::RParen,
14672            (false, Token::EOF) => Token::EOF,
14673            (false, Token::Word(w)) if w.keyword == Keyword::USING => Token::Word(w),
14674            (false, _) => Token::SemiColon,
14675        };
14676
14677        let parameters = self.parse_comma_separated0(Parser::parse_expr, end_token)?;
14678
14679        if has_parentheses {
14680            self.expect_token(&Token::RParen)?;
14681        }
14682
14683        let into = if self.parse_keyword(Keyword::INTO) {
14684            self.parse_comma_separated(Self::parse_identifier)?
14685        } else {
14686            vec![]
14687        };
14688
14689        let using = if self.parse_keyword(Keyword::USING) {
14690            self.parse_comma_separated(Self::parse_expr_with_alias)?
14691        } else {
14692            vec![]
14693        };
14694
14695        Ok(Statement::Execute {
14696            immediate: name.is_none(),
14697            name,
14698            parameters,
14699            has_parentheses,
14700            into,
14701            using,
14702        })
14703    }
14704
14705    pub fn parse_prepare(&mut self) -> Result<Statement, ParserError> {
14706        let name = self.parse_identifier()?;
14707
14708        let mut data_types = vec![];
14709        if self.consume_token(&Token::LParen) {
14710            data_types = self.parse_comma_separated(Parser::parse_data_type)?;
14711            self.expect_token(&Token::RParen)?;
14712        }
14713
14714        self.expect_keyword_is(Keyword::AS)?;
14715        let statement = Box::new(self.parse_statement()?);
14716        Ok(Statement::Prepare {
14717            name,
14718            data_types,
14719            statement,
14720        })
14721    }
14722
14723    pub fn parse_unload(&mut self) -> Result<Statement, ParserError> {
14724        self.expect_token(&Token::LParen)?;
14725        let query = self.parse_query()?;
14726        self.expect_token(&Token::RParen)?;
14727
14728        self.expect_keyword_is(Keyword::TO)?;
14729        let to = self.parse_identifier()?;
14730
14731        let with_options = self.parse_options(Keyword::WITH)?;
14732
14733        Ok(Statement::Unload {
14734            query,
14735            to,
14736            with: with_options,
14737        })
14738    }
14739
14740    pub fn parse_merge_clauses(&mut self) -> Result<Vec<MergeClause>, ParserError> {
14741        let mut clauses = vec![];
14742        loop {
14743            if !(self.parse_keyword(Keyword::WHEN)) {
14744                break;
14745            }
14746
14747            let mut clause_kind = MergeClauseKind::Matched;
14748            if self.parse_keyword(Keyword::NOT) {
14749                clause_kind = MergeClauseKind::NotMatched;
14750            }
14751            self.expect_keyword_is(Keyword::MATCHED)?;
14752
14753            if matches!(clause_kind, MergeClauseKind::NotMatched)
14754                && self.parse_keywords(&[Keyword::BY, Keyword::SOURCE])
14755            {
14756                clause_kind = MergeClauseKind::NotMatchedBySource;
14757            } else if matches!(clause_kind, MergeClauseKind::NotMatched)
14758                && self.parse_keywords(&[Keyword::BY, Keyword::TARGET])
14759            {
14760                clause_kind = MergeClauseKind::NotMatchedByTarget;
14761            }
14762
14763            let predicate = if self.parse_keyword(Keyword::AND) {
14764                Some(self.parse_expr()?)
14765            } else {
14766                None
14767            };
14768
14769            self.expect_keyword_is(Keyword::THEN)?;
14770
14771            let merge_clause = match self.parse_one_of_keywords(&[
14772                Keyword::UPDATE,
14773                Keyword::INSERT,
14774                Keyword::DELETE,
14775            ]) {
14776                Some(Keyword::UPDATE) => {
14777                    if matches!(
14778                        clause_kind,
14779                        MergeClauseKind::NotMatched | MergeClauseKind::NotMatchedByTarget
14780                    ) {
14781                        return Err(ParserError::ParserError(format!(
14782                            "UPDATE is not allowed in a {clause_kind} merge clause"
14783                        )));
14784                    }
14785                    self.expect_keyword_is(Keyword::SET)?;
14786                    MergeAction::Update {
14787                        assignments: self.parse_comma_separated(Parser::parse_assignment)?,
14788                    }
14789                }
14790                Some(Keyword::DELETE) => {
14791                    if matches!(
14792                        clause_kind,
14793                        MergeClauseKind::NotMatched | MergeClauseKind::NotMatchedByTarget
14794                    ) {
14795                        return Err(ParserError::ParserError(format!(
14796                            "DELETE is not allowed in a {clause_kind} merge clause"
14797                        )));
14798                    }
14799                    MergeAction::Delete
14800                }
14801                Some(Keyword::INSERT) => {
14802                    if !matches!(
14803                        clause_kind,
14804                        MergeClauseKind::NotMatched | MergeClauseKind::NotMatchedByTarget
14805                    ) {
14806                        return Err(ParserError::ParserError(format!(
14807                            "INSERT is not allowed in a {clause_kind} merge clause"
14808                        )));
14809                    }
14810                    let is_mysql = dialect_of!(self is MySqlDialect);
14811
14812                    let columns = self.parse_parenthesized_column_list(Optional, is_mysql)?;
14813                    let kind = if dialect_of!(self is BigQueryDialect | GenericDialect)
14814                        && self.parse_keyword(Keyword::ROW)
14815                    {
14816                        MergeInsertKind::Row
14817                    } else {
14818                        self.expect_keyword_is(Keyword::VALUES)?;
14819                        let values = self.parse_values(is_mysql)?;
14820                        MergeInsertKind::Values(values)
14821                    };
14822                    MergeAction::Insert(MergeInsertExpr { columns, kind })
14823                }
14824                _ => {
14825                    return Err(ParserError::ParserError(
14826                        "expected UPDATE, DELETE or INSERT in merge clause".to_string(),
14827                    ));
14828                }
14829            };
14830            clauses.push(MergeClause {
14831                clause_kind,
14832                predicate,
14833                action: merge_clause,
14834            });
14835        }
14836        Ok(clauses)
14837    }
14838
14839    fn parse_output(&mut self) -> Result<OutputClause, ParserError> {
14840        self.expect_keyword_is(Keyword::OUTPUT)?;
14841        let select_items = self.parse_projection()?;
14842        self.expect_keyword_is(Keyword::INTO)?;
14843        let into_table = self.parse_select_into()?;
14844
14845        Ok(OutputClause {
14846            select_items,
14847            into_table,
14848        })
14849    }
14850
14851    fn parse_select_into(&mut self) -> Result<SelectInto, ParserError> {
14852        let temporary = self
14853            .parse_one_of_keywords(&[Keyword::TEMP, Keyword::TEMPORARY])
14854            .is_some();
14855        let unlogged = self.parse_keyword(Keyword::UNLOGGED);
14856        let table = self.parse_keyword(Keyword::TABLE);
14857        let name = self.parse_object_name(false)?;
14858
14859        Ok(SelectInto {
14860            temporary,
14861            unlogged,
14862            table,
14863            name,
14864        })
14865    }
14866
14867    pub fn parse_merge(&mut self) -> Result<Statement, ParserError> {
14868        let into = self.parse_keyword(Keyword::INTO);
14869
14870        let table = self.parse_table_factor()?;
14871
14872        self.expect_keyword_is(Keyword::USING)?;
14873        let source = self.parse_table_factor()?;
14874        self.expect_keyword_is(Keyword::ON)?;
14875        let on = self.parse_expr()?;
14876        let clauses = self.parse_merge_clauses()?;
14877        let output = if self.peek_keyword(Keyword::OUTPUT) {
14878            Some(self.parse_output()?)
14879        } else {
14880            None
14881        };
14882
14883        Ok(Statement::Merge {
14884            into,
14885            table,
14886            source,
14887            on: Box::new(on),
14888            clauses,
14889            output,
14890        })
14891    }
14892
14893    fn parse_pragma_value(&mut self) -> Result<Value, ParserError> {
14894        match self.parse_value()?.value {
14895            v @ Value::SingleQuotedString(_) => Ok(v),
14896            v @ Value::DoubleQuotedString(_) => Ok(v),
14897            v @ Value::Number(_, _) => Ok(v),
14898            v @ Value::Placeholder(_) => Ok(v),
14899            _ => {
14900                self.prev_token();
14901                self.expected("number or string or ? placeholder", self.peek_token())
14902            }
14903        }
14904    }
14905
14906    // PRAGMA [schema-name '.'] pragma-name [('=' pragma-value) | '(' pragma-value ')']
14907    pub fn parse_pragma(&mut self) -> Result<Statement, ParserError> {
14908        let name = self.parse_object_name(false)?;
14909        if self.consume_token(&Token::LParen) {
14910            let value = self.parse_pragma_value()?;
14911            self.expect_token(&Token::RParen)?;
14912            Ok(Statement::Pragma {
14913                name,
14914                value: Some(value),
14915                is_eq: false,
14916            })
14917        } else if self.consume_token(&Token::Eq) {
14918            Ok(Statement::Pragma {
14919                name,
14920                value: Some(self.parse_pragma_value()?),
14921                is_eq: true,
14922            })
14923        } else {
14924            Ok(Statement::Pragma {
14925                name,
14926                value: None,
14927                is_eq: false,
14928            })
14929        }
14930    }
14931
14932    /// `INSTALL [extension_name]`
14933    pub fn parse_install(&mut self) -> Result<Statement, ParserError> {
14934        let extension_name = self.parse_identifier()?;
14935
14936        Ok(Statement::Install { extension_name })
14937    }
14938
14939    /// Parse a SQL LOAD statement
14940    pub fn parse_load(&mut self) -> Result<Statement, ParserError> {
14941        if self.dialect.supports_load_extension() {
14942            let extension_name = self.parse_identifier()?;
14943            Ok(Statement::Load { extension_name })
14944        } else if self.parse_keyword(Keyword::DATA) && self.dialect.supports_load_data() {
14945            let local = self.parse_one_of_keywords(&[Keyword::LOCAL]).is_some();
14946            self.expect_keyword_is(Keyword::INPATH)?;
14947            let inpath = self.parse_literal_string()?;
14948            let overwrite = self.parse_one_of_keywords(&[Keyword::OVERWRITE]).is_some();
14949            self.expect_keyword_is(Keyword::INTO)?;
14950            self.expect_keyword_is(Keyword::TABLE)?;
14951            let table_name = self.parse_object_name(false)?;
14952            let partitioned = self.parse_insert_partition()?;
14953            let table_format = self.parse_load_data_table_format()?;
14954            Ok(Statement::LoadData {
14955                local,
14956                inpath,
14957                overwrite,
14958                table_name,
14959                partitioned,
14960                table_format,
14961            })
14962        } else {
14963            self.expected(
14964                "`DATA` or an extension name after `LOAD`",
14965                self.peek_token(),
14966            )
14967        }
14968    }
14969
14970    /// ```sql
14971    /// OPTIMIZE TABLE [db.]name [ON CLUSTER cluster] [PARTITION partition | PARTITION ID 'partition_id'] [FINAL] [DEDUPLICATE [BY expression]]
14972    /// ```
14973    /// [ClickHouse](https://clickhouse.com/docs/en/sql-reference/statements/optimize)
14974    pub fn parse_optimize_table(&mut self) -> Result<Statement, ParserError> {
14975        self.expect_keyword_is(Keyword::TABLE)?;
14976        let name = self.parse_object_name(false)?;
14977        let on_cluster = self.parse_optional_on_cluster()?;
14978
14979        let partition = if self.parse_keyword(Keyword::PARTITION) {
14980            if self.parse_keyword(Keyword::ID) {
14981                Some(Partition::Identifier(self.parse_identifier()?))
14982            } else {
14983                Some(Partition::Expr(self.parse_expr()?))
14984            }
14985        } else {
14986            None
14987        };
14988
14989        let include_final = self.parse_keyword(Keyword::FINAL);
14990        let deduplicate = if self.parse_keyword(Keyword::DEDUPLICATE) {
14991            if self.parse_keyword(Keyword::BY) {
14992                Some(Deduplicate::ByExpression(self.parse_expr()?))
14993            } else {
14994                Some(Deduplicate::All)
14995            }
14996        } else {
14997            None
14998        };
14999
15000        Ok(Statement::OptimizeTable {
15001            name,
15002            on_cluster,
15003            partition,
15004            include_final,
15005            deduplicate,
15006        })
15007    }
15008
15009    /// ```sql
15010    /// CREATE [ { TEMPORARY | TEMP } ] SEQUENCE [ IF NOT EXISTS ] <sequence_name>
15011    /// ```
15012    ///
15013    /// See [Postgres docs](https://www.postgresql.org/docs/current/sql-createsequence.html) for more details.
15014    pub fn parse_create_sequence(&mut self, temporary: bool) -> Result<Statement, ParserError> {
15015        //[ IF NOT EXISTS ]
15016        let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
15017        //name
15018        let name = self.parse_object_name(false)?;
15019        //[ AS data_type ]
15020        let mut data_type: Option<DataType> = None;
15021        if self.parse_keywords(&[Keyword::AS]) {
15022            data_type = Some(self.parse_data_type()?)
15023        }
15024        let sequence_options = self.parse_create_sequence_options()?;
15025        // [ OWNED BY { table_name.column_name | NONE } ]
15026        let owned_by = if self.parse_keywords(&[Keyword::OWNED, Keyword::BY]) {
15027            if self.parse_keywords(&[Keyword::NONE]) {
15028                Some(ObjectName::from(vec![Ident::new("NONE")]))
15029            } else {
15030                Some(self.parse_object_name(false)?)
15031            }
15032        } else {
15033            None
15034        };
15035        Ok(Statement::CreateSequence {
15036            temporary,
15037            if_not_exists,
15038            name,
15039            data_type,
15040            sequence_options,
15041            owned_by,
15042        })
15043    }
15044
15045    fn parse_create_sequence_options(&mut self) -> Result<Vec<SequenceOptions>, ParserError> {
15046        let mut sequence_options = vec![];
15047        //[ INCREMENT [ BY ] increment ]
15048        if self.parse_keywords(&[Keyword::INCREMENT]) {
15049            if self.parse_keywords(&[Keyword::BY]) {
15050                sequence_options.push(SequenceOptions::IncrementBy(self.parse_number()?, true));
15051            } else {
15052                sequence_options.push(SequenceOptions::IncrementBy(self.parse_number()?, false));
15053            }
15054        }
15055        //[ MINVALUE minvalue | NO MINVALUE ]
15056        if self.parse_keyword(Keyword::MINVALUE) {
15057            sequence_options.push(SequenceOptions::MinValue(Some(self.parse_number()?)));
15058        } else if self.parse_keywords(&[Keyword::NO, Keyword::MINVALUE]) {
15059            sequence_options.push(SequenceOptions::MinValue(None));
15060        }
15061        //[ MAXVALUE maxvalue | NO MAXVALUE ]
15062        if self.parse_keywords(&[Keyword::MAXVALUE]) {
15063            sequence_options.push(SequenceOptions::MaxValue(Some(self.parse_number()?)));
15064        } else if self.parse_keywords(&[Keyword::NO, Keyword::MAXVALUE]) {
15065            sequence_options.push(SequenceOptions::MaxValue(None));
15066        }
15067
15068        //[ START [ WITH ] start ]
15069        if self.parse_keywords(&[Keyword::START]) {
15070            if self.parse_keywords(&[Keyword::WITH]) {
15071                sequence_options.push(SequenceOptions::StartWith(self.parse_number()?, true));
15072            } else {
15073                sequence_options.push(SequenceOptions::StartWith(self.parse_number()?, false));
15074            }
15075        }
15076        //[ CACHE cache ]
15077        if self.parse_keywords(&[Keyword::CACHE]) {
15078            sequence_options.push(SequenceOptions::Cache(self.parse_number()?));
15079        }
15080        // [ [ NO ] CYCLE ]
15081        if self.parse_keywords(&[Keyword::NO, Keyword::CYCLE]) {
15082            sequence_options.push(SequenceOptions::Cycle(true));
15083        } else if self.parse_keywords(&[Keyword::CYCLE]) {
15084            sequence_options.push(SequenceOptions::Cycle(false));
15085        }
15086
15087        Ok(sequence_options)
15088    }
15089
15090    /// The index of the first unprocessed token.
15091    pub fn index(&self) -> usize {
15092        self.index
15093    }
15094
15095    pub fn parse_named_window(&mut self) -> Result<NamedWindowDefinition, ParserError> {
15096        let ident = self.parse_identifier()?;
15097        self.expect_keyword_is(Keyword::AS)?;
15098
15099        let window_expr = if self.consume_token(&Token::LParen) {
15100            NamedWindowExpr::WindowSpec(self.parse_window_spec()?)
15101        } else if self.dialect.supports_window_clause_named_window_reference() {
15102            NamedWindowExpr::NamedWindow(self.parse_identifier()?)
15103        } else {
15104            return self.expected("(", self.peek_token());
15105        };
15106
15107        Ok(NamedWindowDefinition(ident, window_expr))
15108    }
15109
15110    pub fn parse_create_procedure(&mut self, or_alter: bool) -> Result<Statement, ParserError> {
15111        let name = self.parse_object_name(false)?;
15112        let params = self.parse_optional_procedure_parameters()?;
15113        self.expect_keyword_is(Keyword::AS)?;
15114        self.expect_keyword_is(Keyword::BEGIN)?;
15115        let statements = self.parse_statements()?;
15116        self.expect_keyword_is(Keyword::END)?;
15117        Ok(Statement::CreateProcedure {
15118            name,
15119            or_alter,
15120            params,
15121            body: statements,
15122        })
15123    }
15124
15125    pub fn parse_window_spec(&mut self) -> Result<WindowSpec, ParserError> {
15126        let window_name = match self.peek_token().token {
15127            Token::Word(word) if word.keyword == Keyword::NoKeyword => {
15128                self.parse_optional_indent()?
15129            }
15130            _ => None,
15131        };
15132
15133        let partition_by = if self.parse_keywords(&[Keyword::PARTITION, Keyword::BY]) {
15134            self.parse_comma_separated(Parser::parse_expr)?
15135        } else {
15136            vec![]
15137        };
15138        let order_by = if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
15139            self.parse_comma_separated(Parser::parse_order_by_expr)?
15140        } else {
15141            vec![]
15142        };
15143
15144        let window_frame = if !self.consume_token(&Token::RParen) {
15145            let window_frame = self.parse_window_frame()?;
15146            self.expect_token(&Token::RParen)?;
15147            Some(window_frame)
15148        } else {
15149            None
15150        };
15151        Ok(WindowSpec {
15152            window_name,
15153            partition_by,
15154            order_by,
15155            window_frame,
15156        })
15157    }
15158
15159    pub fn parse_create_type(&mut self) -> Result<Statement, ParserError> {
15160        let name = self.parse_object_name(false)?;
15161        self.expect_keyword_is(Keyword::AS)?;
15162
15163        if self.parse_keyword(Keyword::ENUM) {
15164            return self.parse_create_type_enum(name);
15165        }
15166
15167        let mut attributes = vec![];
15168        if !self.consume_token(&Token::LParen) || self.consume_token(&Token::RParen) {
15169            return Ok(Statement::CreateType {
15170                name,
15171                representation: UserDefinedTypeRepresentation::Composite { attributes },
15172            });
15173        }
15174
15175        loop {
15176            let attr_name = self.parse_identifier()?;
15177            let attr_data_type = self.parse_data_type()?;
15178            let attr_collation = if self.parse_keyword(Keyword::COLLATE) {
15179                Some(self.parse_object_name(false)?)
15180            } else {
15181                None
15182            };
15183            attributes.push(UserDefinedTypeCompositeAttributeDef {
15184                name: attr_name,
15185                data_type: attr_data_type,
15186                collation: attr_collation,
15187            });
15188            let comma = self.consume_token(&Token::Comma);
15189            if self.consume_token(&Token::RParen) {
15190                // allow a trailing comma
15191                break;
15192            } else if !comma {
15193                return self.expected("',' or ')' after attribute definition", self.peek_token());
15194            }
15195        }
15196
15197        Ok(Statement::CreateType {
15198            name,
15199            representation: UserDefinedTypeRepresentation::Composite { attributes },
15200        })
15201    }
15202
15203    /// Parse remainder of `CREATE TYPE AS ENUM` statement (see [Statement::CreateType] and [Self::parse_create_type])
15204    ///
15205    /// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-createtype.html)
15206    pub fn parse_create_type_enum(&mut self, name: ObjectName) -> Result<Statement, ParserError> {
15207        self.expect_token(&Token::LParen)?;
15208        let labels = self.parse_comma_separated0(|p| p.parse_identifier(), Token::RParen)?;
15209        self.expect_token(&Token::RParen)?;
15210
15211        Ok(Statement::CreateType {
15212            name,
15213            representation: UserDefinedTypeRepresentation::Enum { labels },
15214        })
15215    }
15216
15217    fn parse_parenthesized_identifiers(&mut self) -> Result<Vec<Ident>, ParserError> {
15218        self.expect_token(&Token::LParen)?;
15219        let partitions = self.parse_comma_separated(|p| p.parse_identifier())?;
15220        self.expect_token(&Token::RParen)?;
15221        Ok(partitions)
15222    }
15223
15224    fn parse_column_position(&mut self) -> Result<Option<MySQLColumnPosition>, ParserError> {
15225        if dialect_of!(self is MySqlDialect | GenericDialect) {
15226            if self.parse_keyword(Keyword::FIRST) {
15227                Ok(Some(MySQLColumnPosition::First))
15228            } else if self.parse_keyword(Keyword::AFTER) {
15229                let ident = self.parse_identifier()?;
15230                Ok(Some(MySQLColumnPosition::After(ident)))
15231            } else {
15232                Ok(None)
15233            }
15234        } else {
15235            Ok(None)
15236        }
15237    }
15238
15239    /// Parse [Statement::Print]
15240    fn parse_print(&mut self) -> Result<Statement, ParserError> {
15241        Ok(Statement::Print(PrintStatement {
15242            message: Box::new(self.parse_expr()?),
15243        }))
15244    }
15245
15246    /// Parse [Statement::Return]
15247    fn parse_return(&mut self) -> Result<Statement, ParserError> {
15248        match self.maybe_parse(|p| p.parse_expr())? {
15249            Some(expr) => Ok(Statement::Return(ReturnStatement {
15250                value: Some(ReturnStatementValue::Expr(expr)),
15251            })),
15252            None => Ok(Statement::Return(ReturnStatement { value: None })),
15253        }
15254    }
15255
15256    /// Consume the parser and return its underlying token buffer
15257    pub fn into_tokens(self) -> Vec<TokenWithSpan> {
15258        self.tokens
15259    }
15260
15261    /// Returns true if the next keyword indicates a sub query, i.e. SELECT or WITH
15262    fn peek_sub_query(&mut self) -> bool {
15263        if self
15264            .parse_one_of_keywords(&[Keyword::SELECT, Keyword::WITH])
15265            .is_some()
15266        {
15267            self.prev_token();
15268            return true;
15269        }
15270        false
15271    }
15272
15273    pub(crate) fn parse_show_stmt_options(&mut self) -> Result<ShowStatementOptions, ParserError> {
15274        let show_in;
15275        let mut filter_position = None;
15276        if self.dialect.supports_show_like_before_in() {
15277            if let Some(filter) = self.parse_show_statement_filter()? {
15278                filter_position = Some(ShowStatementFilterPosition::Infix(filter));
15279            }
15280            show_in = self.maybe_parse_show_stmt_in()?;
15281        } else {
15282            show_in = self.maybe_parse_show_stmt_in()?;
15283            if let Some(filter) = self.parse_show_statement_filter()? {
15284                filter_position = Some(ShowStatementFilterPosition::Suffix(filter));
15285            }
15286        }
15287        let starts_with = self.maybe_parse_show_stmt_starts_with()?;
15288        let limit = self.maybe_parse_show_stmt_limit()?;
15289        let from = self.maybe_parse_show_stmt_from()?;
15290        Ok(ShowStatementOptions {
15291            filter_position,
15292            show_in,
15293            starts_with,
15294            limit,
15295            limit_from: from,
15296        })
15297    }
15298
15299    fn maybe_parse_show_stmt_in(&mut self) -> Result<Option<ShowStatementIn>, ParserError> {
15300        let clause = match self.parse_one_of_keywords(&[Keyword::FROM, Keyword::IN]) {
15301            Some(Keyword::FROM) => ShowStatementInClause::FROM,
15302            Some(Keyword::IN) => ShowStatementInClause::IN,
15303            None => return Ok(None),
15304            _ => return self.expected("FROM or IN", self.peek_token()),
15305        };
15306
15307        let (parent_type, parent_name) = match self.parse_one_of_keywords(&[
15308            Keyword::ACCOUNT,
15309            Keyword::DATABASE,
15310            Keyword::SCHEMA,
15311            Keyword::TABLE,
15312            Keyword::VIEW,
15313        ]) {
15314            // If we see these next keywords it means we don't have a parent name
15315            Some(Keyword::DATABASE)
15316                if self.peek_keywords(&[Keyword::STARTS, Keyword::WITH])
15317                    | self.peek_keyword(Keyword::LIMIT) =>
15318            {
15319                (Some(ShowStatementInParentType::Database), None)
15320            }
15321            Some(Keyword::SCHEMA)
15322                if self.peek_keywords(&[Keyword::STARTS, Keyword::WITH])
15323                    | self.peek_keyword(Keyword::LIMIT) =>
15324            {
15325                (Some(ShowStatementInParentType::Schema), None)
15326            }
15327            Some(parent_kw) => {
15328                // The parent name here is still optional, for example:
15329                // SHOW TABLES IN ACCOUNT, so parsing the object name
15330                // may fail because the statement ends.
15331                let parent_name = self.maybe_parse(|p| p.parse_object_name(false))?;
15332                match parent_kw {
15333                    Keyword::ACCOUNT => (Some(ShowStatementInParentType::Account), parent_name),
15334                    Keyword::DATABASE => (Some(ShowStatementInParentType::Database), parent_name),
15335                    Keyword::SCHEMA => (Some(ShowStatementInParentType::Schema), parent_name),
15336                    Keyword::TABLE => (Some(ShowStatementInParentType::Table), parent_name),
15337                    Keyword::VIEW => (Some(ShowStatementInParentType::View), parent_name),
15338                    _ => {
15339                        return self.expected(
15340                            "one of ACCOUNT, DATABASE, SCHEMA, TABLE or VIEW",
15341                            self.peek_token(),
15342                        )
15343                    }
15344                }
15345            }
15346            None => {
15347                // Parsing MySQL style FROM tbl_name FROM db_name
15348                // which is equivalent to FROM tbl_name.db_name
15349                let mut parent_name = self.parse_object_name(false)?;
15350                if self
15351                    .parse_one_of_keywords(&[Keyword::FROM, Keyword::IN])
15352                    .is_some()
15353                {
15354                    parent_name
15355                        .0
15356                        .insert(0, ObjectNamePart::Identifier(self.parse_identifier()?));
15357                }
15358                (None, Some(parent_name))
15359            }
15360        };
15361
15362        Ok(Some(ShowStatementIn {
15363            clause,
15364            parent_type,
15365            parent_name,
15366        }))
15367    }
15368
15369    fn maybe_parse_show_stmt_starts_with(&mut self) -> Result<Option<Value>, ParserError> {
15370        if self.parse_keywords(&[Keyword::STARTS, Keyword::WITH]) {
15371            Ok(Some(self.parse_value()?.value))
15372        } else {
15373            Ok(None)
15374        }
15375    }
15376
15377    fn maybe_parse_show_stmt_limit(&mut self) -> Result<Option<Expr>, ParserError> {
15378        if self.parse_keyword(Keyword::LIMIT) {
15379            Ok(self.parse_limit()?)
15380        } else {
15381            Ok(None)
15382        }
15383    }
15384
15385    fn maybe_parse_show_stmt_from(&mut self) -> Result<Option<Value>, ParserError> {
15386        if self.parse_keyword(Keyword::FROM) {
15387            Ok(Some(self.parse_value()?.value))
15388        } else {
15389            Ok(None)
15390        }
15391    }
15392}
15393
15394fn maybe_prefixed_expr(expr: Expr, prefix: Option<Ident>) -> Expr {
15395    if let Some(prefix) = prefix {
15396        Expr::Prefixed {
15397            prefix,
15398            value: Box::new(expr),
15399        }
15400    } else {
15401        expr
15402    }
15403}
15404
15405impl Word {
15406    #[deprecated(since = "0.54.0", note = "please use `into_ident` instead")]
15407    pub fn to_ident(&self, span: Span) -> Ident {
15408        Ident {
15409            value: self.value.clone(),
15410            quote_style: self.quote_style,
15411            span,
15412        }
15413    }
15414
15415    /// Convert this word into an [`Ident`] identifier
15416    pub fn into_ident(self, span: Span) -> Ident {
15417        Ident {
15418            value: self.value,
15419            quote_style: self.quote_style,
15420            span,
15421        }
15422    }
15423}
15424
15425#[cfg(test)]
15426mod tests {
15427    use crate::test_utils::{all_dialects, TestedDialects};
15428
15429    use super::*;
15430
15431    #[test]
15432    fn test_prev_index() {
15433        let sql = "SELECT version";
15434        all_dialects().run_parser_method(sql, |parser| {
15435            assert_eq!(parser.peek_token(), Token::make_keyword("SELECT"));
15436            assert_eq!(parser.next_token(), Token::make_keyword("SELECT"));
15437            parser.prev_token();
15438            assert_eq!(parser.next_token(), Token::make_keyword("SELECT"));
15439            assert_eq!(parser.next_token(), Token::make_word("version", None));
15440            parser.prev_token();
15441            assert_eq!(parser.peek_token(), Token::make_word("version", None));
15442            assert_eq!(parser.next_token(), Token::make_word("version", None));
15443            assert_eq!(parser.peek_token(), Token::EOF);
15444            parser.prev_token();
15445            assert_eq!(parser.next_token(), Token::make_word("version", None));
15446            assert_eq!(parser.next_token(), Token::EOF);
15447            assert_eq!(parser.next_token(), Token::EOF);
15448            parser.prev_token();
15449        });
15450    }
15451
15452    #[test]
15453    fn test_peek_tokens() {
15454        all_dialects().run_parser_method("SELECT foo AS bar FROM baz", |parser| {
15455            assert!(matches!(
15456                parser.peek_tokens(),
15457                [Token::Word(Word {
15458                    keyword: Keyword::SELECT,
15459                    ..
15460                })]
15461            ));
15462
15463            assert!(matches!(
15464                parser.peek_tokens(),
15465                [
15466                    Token::Word(Word {
15467                        keyword: Keyword::SELECT,
15468                        ..
15469                    }),
15470                    Token::Word(_),
15471                    Token::Word(Word {
15472                        keyword: Keyword::AS,
15473                        ..
15474                    }),
15475                ]
15476            ));
15477
15478            for _ in 0..4 {
15479                parser.next_token();
15480            }
15481
15482            assert!(matches!(
15483                parser.peek_tokens(),
15484                [
15485                    Token::Word(Word {
15486                        keyword: Keyword::FROM,
15487                        ..
15488                    }),
15489                    Token::Word(_),
15490                    Token::EOF,
15491                    Token::EOF,
15492                ]
15493            ))
15494        })
15495    }
15496
15497    #[cfg(test)]
15498    mod test_parse_data_type {
15499        use crate::ast::{
15500            CharLengthUnits, CharacterLength, DataType, ExactNumberInfo, ObjectName, TimezoneInfo,
15501        };
15502        use crate::dialect::{AnsiDialect, GenericDialect};
15503        use crate::test_utils::TestedDialects;
15504
15505        macro_rules! test_parse_data_type {
15506            ($dialect:expr, $input:expr, $expected_type:expr $(,)?) => {{
15507                $dialect.run_parser_method(&*$input, |parser| {
15508                    let data_type = parser.parse_data_type().unwrap();
15509                    assert_eq!($expected_type, data_type);
15510                    assert_eq!($input.to_string(), data_type.to_string());
15511                });
15512            }};
15513        }
15514
15515        #[test]
15516        fn test_ansii_character_string_types() {
15517            // Character string types: <https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#character-string-type>
15518            let dialect =
15519                TestedDialects::new(vec![Box::new(GenericDialect {}), Box::new(AnsiDialect {})]);
15520
15521            test_parse_data_type!(dialect, "CHARACTER", DataType::Character(None));
15522
15523            test_parse_data_type!(
15524                dialect,
15525                "CHARACTER(20)",
15526                DataType::Character(Some(CharacterLength::IntegerLength {
15527                    length: 20,
15528                    unit: None
15529                }))
15530            );
15531
15532            test_parse_data_type!(
15533                dialect,
15534                "CHARACTER(20 CHARACTERS)",
15535                DataType::Character(Some(CharacterLength::IntegerLength {
15536                    length: 20,
15537                    unit: Some(CharLengthUnits::Characters)
15538                }))
15539            );
15540
15541            test_parse_data_type!(
15542                dialect,
15543                "CHARACTER(20 OCTETS)",
15544                DataType::Character(Some(CharacterLength::IntegerLength {
15545                    length: 20,
15546                    unit: Some(CharLengthUnits::Octets)
15547                }))
15548            );
15549
15550            test_parse_data_type!(dialect, "CHAR", DataType::Char(None));
15551
15552            test_parse_data_type!(
15553                dialect,
15554                "CHAR(20)",
15555                DataType::Char(Some(CharacterLength::IntegerLength {
15556                    length: 20,
15557                    unit: None
15558                }))
15559            );
15560
15561            test_parse_data_type!(
15562                dialect,
15563                "CHAR(20 CHARACTERS)",
15564                DataType::Char(Some(CharacterLength::IntegerLength {
15565                    length: 20,
15566                    unit: Some(CharLengthUnits::Characters)
15567                }))
15568            );
15569
15570            test_parse_data_type!(
15571                dialect,
15572                "CHAR(20 OCTETS)",
15573                DataType::Char(Some(CharacterLength::IntegerLength {
15574                    length: 20,
15575                    unit: Some(CharLengthUnits::Octets)
15576                }))
15577            );
15578
15579            test_parse_data_type!(
15580                dialect,
15581                "CHARACTER VARYING(20)",
15582                DataType::CharacterVarying(Some(CharacterLength::IntegerLength {
15583                    length: 20,
15584                    unit: None
15585                }))
15586            );
15587
15588            test_parse_data_type!(
15589                dialect,
15590                "CHARACTER VARYING(20 CHARACTERS)",
15591                DataType::CharacterVarying(Some(CharacterLength::IntegerLength {
15592                    length: 20,
15593                    unit: Some(CharLengthUnits::Characters)
15594                }))
15595            );
15596
15597            test_parse_data_type!(
15598                dialect,
15599                "CHARACTER VARYING(20 OCTETS)",
15600                DataType::CharacterVarying(Some(CharacterLength::IntegerLength {
15601                    length: 20,
15602                    unit: Some(CharLengthUnits::Octets)
15603                }))
15604            );
15605
15606            test_parse_data_type!(
15607                dialect,
15608                "CHAR VARYING(20)",
15609                DataType::CharVarying(Some(CharacterLength::IntegerLength {
15610                    length: 20,
15611                    unit: None
15612                }))
15613            );
15614
15615            test_parse_data_type!(
15616                dialect,
15617                "CHAR VARYING(20 CHARACTERS)",
15618                DataType::CharVarying(Some(CharacterLength::IntegerLength {
15619                    length: 20,
15620                    unit: Some(CharLengthUnits::Characters)
15621                }))
15622            );
15623
15624            test_parse_data_type!(
15625                dialect,
15626                "CHAR VARYING(20 OCTETS)",
15627                DataType::CharVarying(Some(CharacterLength::IntegerLength {
15628                    length: 20,
15629                    unit: Some(CharLengthUnits::Octets)
15630                }))
15631            );
15632
15633            test_parse_data_type!(
15634                dialect,
15635                "VARCHAR(20)",
15636                DataType::Varchar(Some(CharacterLength::IntegerLength {
15637                    length: 20,
15638                    unit: None
15639                }))
15640            );
15641        }
15642
15643        #[test]
15644        fn test_ansii_character_large_object_types() {
15645            // Character large object types: <https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#character-large-object-length>
15646            let dialect =
15647                TestedDialects::new(vec![Box::new(GenericDialect {}), Box::new(AnsiDialect {})]);
15648
15649            test_parse_data_type!(
15650                dialect,
15651                "CHARACTER LARGE OBJECT",
15652                DataType::CharacterLargeObject(None)
15653            );
15654            test_parse_data_type!(
15655                dialect,
15656                "CHARACTER LARGE OBJECT(20)",
15657                DataType::CharacterLargeObject(Some(20))
15658            );
15659
15660            test_parse_data_type!(
15661                dialect,
15662                "CHAR LARGE OBJECT",
15663                DataType::CharLargeObject(None)
15664            );
15665            test_parse_data_type!(
15666                dialect,
15667                "CHAR LARGE OBJECT(20)",
15668                DataType::CharLargeObject(Some(20))
15669            );
15670
15671            test_parse_data_type!(dialect, "CLOB", DataType::Clob(None));
15672            test_parse_data_type!(dialect, "CLOB(20)", DataType::Clob(Some(20)));
15673        }
15674
15675        #[test]
15676        fn test_parse_custom_types() {
15677            let dialect =
15678                TestedDialects::new(vec![Box::new(GenericDialect {}), Box::new(AnsiDialect {})]);
15679
15680            test_parse_data_type!(
15681                dialect,
15682                "GEOMETRY",
15683                DataType::Custom(ObjectName::from(vec!["GEOMETRY".into()]), vec![])
15684            );
15685
15686            test_parse_data_type!(
15687                dialect,
15688                "GEOMETRY(POINT)",
15689                DataType::Custom(
15690                    ObjectName::from(vec!["GEOMETRY".into()]),
15691                    vec!["POINT".to_string()]
15692                )
15693            );
15694
15695            test_parse_data_type!(
15696                dialect,
15697                "GEOMETRY(POINT, 4326)",
15698                DataType::Custom(
15699                    ObjectName::from(vec!["GEOMETRY".into()]),
15700                    vec!["POINT".to_string(), "4326".to_string()]
15701                )
15702            );
15703        }
15704
15705        #[test]
15706        fn test_ansii_exact_numeric_types() {
15707            // Exact numeric types: <https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#exact-numeric-type>
15708            let dialect =
15709                TestedDialects::new(vec![Box::new(GenericDialect {}), Box::new(AnsiDialect {})]);
15710
15711            test_parse_data_type!(dialect, "NUMERIC", DataType::Numeric(ExactNumberInfo::None));
15712
15713            test_parse_data_type!(
15714                dialect,
15715                "NUMERIC(2)",
15716                DataType::Numeric(ExactNumberInfo::Precision(2))
15717            );
15718
15719            test_parse_data_type!(
15720                dialect,
15721                "NUMERIC(2,10)",
15722                DataType::Numeric(ExactNumberInfo::PrecisionAndScale(2, 10))
15723            );
15724
15725            test_parse_data_type!(dialect, "DECIMAL", DataType::Decimal(ExactNumberInfo::None));
15726
15727            test_parse_data_type!(
15728                dialect,
15729                "DECIMAL(2)",
15730                DataType::Decimal(ExactNumberInfo::Precision(2))
15731            );
15732
15733            test_parse_data_type!(
15734                dialect,
15735                "DECIMAL(2,10)",
15736                DataType::Decimal(ExactNumberInfo::PrecisionAndScale(2, 10))
15737            );
15738
15739            test_parse_data_type!(dialect, "DEC", DataType::Dec(ExactNumberInfo::None));
15740
15741            test_parse_data_type!(
15742                dialect,
15743                "DEC(2)",
15744                DataType::Dec(ExactNumberInfo::Precision(2))
15745            );
15746
15747            test_parse_data_type!(
15748                dialect,
15749                "DEC(2,10)",
15750                DataType::Dec(ExactNumberInfo::PrecisionAndScale(2, 10))
15751            );
15752        }
15753
15754        #[test]
15755        fn test_ansii_date_type() {
15756            // Datetime types: <https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#datetime-type>
15757            let dialect =
15758                TestedDialects::new(vec![Box::new(GenericDialect {}), Box::new(AnsiDialect {})]);
15759
15760            test_parse_data_type!(dialect, "DATE", DataType::Date);
15761
15762            test_parse_data_type!(dialect, "TIME", DataType::Time(None, TimezoneInfo::None));
15763
15764            test_parse_data_type!(
15765                dialect,
15766                "TIME(6)",
15767                DataType::Time(Some(6), TimezoneInfo::None)
15768            );
15769
15770            test_parse_data_type!(
15771                dialect,
15772                "TIME WITH TIME ZONE",
15773                DataType::Time(None, TimezoneInfo::WithTimeZone)
15774            );
15775
15776            test_parse_data_type!(
15777                dialect,
15778                "TIME(6) WITH TIME ZONE",
15779                DataType::Time(Some(6), TimezoneInfo::WithTimeZone)
15780            );
15781
15782            test_parse_data_type!(
15783                dialect,
15784                "TIME WITHOUT TIME ZONE",
15785                DataType::Time(None, TimezoneInfo::WithoutTimeZone)
15786            );
15787
15788            test_parse_data_type!(
15789                dialect,
15790                "TIME(6) WITHOUT TIME ZONE",
15791                DataType::Time(Some(6), TimezoneInfo::WithoutTimeZone)
15792            );
15793
15794            test_parse_data_type!(
15795                dialect,
15796                "TIMESTAMP",
15797                DataType::Timestamp(None, TimezoneInfo::None)
15798            );
15799
15800            test_parse_data_type!(
15801                dialect,
15802                "TIMESTAMP(22)",
15803                DataType::Timestamp(Some(22), TimezoneInfo::None)
15804            );
15805
15806            test_parse_data_type!(
15807                dialect,
15808                "TIMESTAMP(22) WITH TIME ZONE",
15809                DataType::Timestamp(Some(22), TimezoneInfo::WithTimeZone)
15810            );
15811
15812            test_parse_data_type!(
15813                dialect,
15814                "TIMESTAMP(33) WITHOUT TIME ZONE",
15815                DataType::Timestamp(Some(33), TimezoneInfo::WithoutTimeZone)
15816            );
15817        }
15818    }
15819
15820    #[test]
15821    fn test_parse_schema_name() {
15822        // The expected name should be identical as the input name, that's why I don't receive both
15823        macro_rules! test_parse_schema_name {
15824            ($input:expr, $expected_name:expr $(,)?) => {{
15825                all_dialects().run_parser_method(&*$input, |parser| {
15826                    let schema_name = parser.parse_schema_name().unwrap();
15827                    // Validate that the structure is the same as expected
15828                    assert_eq!(schema_name, $expected_name);
15829                    // Validate that the input and the expected structure serialization are the same
15830                    assert_eq!(schema_name.to_string(), $input.to_string());
15831                });
15832            }};
15833        }
15834
15835        let dummy_name = ObjectName::from(vec![Ident::new("dummy_name")]);
15836        let dummy_authorization = Ident::new("dummy_authorization");
15837
15838        test_parse_schema_name!(
15839            format!("{dummy_name}"),
15840            SchemaName::Simple(dummy_name.clone())
15841        );
15842
15843        test_parse_schema_name!(
15844            format!("AUTHORIZATION {dummy_authorization}"),
15845            SchemaName::UnnamedAuthorization(dummy_authorization.clone()),
15846        );
15847        test_parse_schema_name!(
15848            format!("{dummy_name} AUTHORIZATION {dummy_authorization}"),
15849            SchemaName::NamedAuthorization(dummy_name.clone(), dummy_authorization.clone()),
15850        );
15851    }
15852
15853    #[test]
15854    fn mysql_parse_index_table_constraint() {
15855        macro_rules! test_parse_table_constraint {
15856            ($dialect:expr, $input:expr, $expected:expr $(,)?) => {{
15857                $dialect.run_parser_method(&*$input, |parser| {
15858                    let constraint = parser.parse_optional_table_constraint().unwrap().unwrap();
15859                    // Validate that the structure is the same as expected
15860                    assert_eq!(constraint, $expected);
15861                    // Validate that the input and the expected structure serialization are the same
15862                    assert_eq!(constraint.to_string(), $input.to_string());
15863                });
15864            }};
15865        }
15866
15867        let dialect =
15868            TestedDialects::new(vec![Box::new(GenericDialect {}), Box::new(MySqlDialect {})]);
15869
15870        test_parse_table_constraint!(
15871            dialect,
15872            "INDEX (c1)",
15873            TableConstraint::Index {
15874                display_as_key: false,
15875                name: None,
15876                index_type: None,
15877                columns: vec![Ident::new("c1")],
15878            }
15879        );
15880
15881        test_parse_table_constraint!(
15882            dialect,
15883            "KEY (c1)",
15884            TableConstraint::Index {
15885                display_as_key: true,
15886                name: None,
15887                index_type: None,
15888                columns: vec![Ident::new("c1")],
15889            }
15890        );
15891
15892        test_parse_table_constraint!(
15893            dialect,
15894            "INDEX 'index' (c1, c2)",
15895            TableConstraint::Index {
15896                display_as_key: false,
15897                name: Some(Ident::with_quote('\'', "index")),
15898                index_type: None,
15899                columns: vec![Ident::new("c1"), Ident::new("c2")],
15900            }
15901        );
15902
15903        test_parse_table_constraint!(
15904            dialect,
15905            "INDEX USING BTREE (c1)",
15906            TableConstraint::Index {
15907                display_as_key: false,
15908                name: None,
15909                index_type: Some(IndexType::BTree),
15910                columns: vec![Ident::new("c1")],
15911            }
15912        );
15913
15914        test_parse_table_constraint!(
15915            dialect,
15916            "INDEX USING HASH (c1)",
15917            TableConstraint::Index {
15918                display_as_key: false,
15919                name: None,
15920                index_type: Some(IndexType::Hash),
15921                columns: vec![Ident::new("c1")],
15922            }
15923        );
15924
15925        test_parse_table_constraint!(
15926            dialect,
15927            "INDEX idx_name USING BTREE (c1)",
15928            TableConstraint::Index {
15929                display_as_key: false,
15930                name: Some(Ident::new("idx_name")),
15931                index_type: Some(IndexType::BTree),
15932                columns: vec![Ident::new("c1")],
15933            }
15934        );
15935
15936        test_parse_table_constraint!(
15937            dialect,
15938            "INDEX idx_name USING HASH (c1)",
15939            TableConstraint::Index {
15940                display_as_key: false,
15941                name: Some(Ident::new("idx_name")),
15942                index_type: Some(IndexType::Hash),
15943                columns: vec![Ident::new("c1")],
15944            }
15945        );
15946    }
15947
15948    #[test]
15949    fn test_tokenizer_error_loc() {
15950        let sql = "foo '";
15951        let ast = Parser::parse_sql(&GenericDialect, sql);
15952        assert_eq!(
15953            ast,
15954            Err(ParserError::TokenizerError(
15955                "Unterminated string literal at Line: 1, Column: 5".to_string()
15956            ))
15957        );
15958    }
15959
15960    #[test]
15961    fn test_parser_error_loc() {
15962        let sql = "SELECT this is a syntax error";
15963        let ast = Parser::parse_sql(&GenericDialect, sql);
15964        assert_eq!(
15965            ast,
15966            Err(ParserError::ParserError(
15967                "Expected: [NOT] NULL | TRUE | FALSE | DISTINCT | [form] NORMALIZED FROM after IS, found: a at Line: 1, Column: 16"
15968                    .to_string()
15969            ))
15970        );
15971    }
15972
15973    #[test]
15974    fn test_nested_explain_error() {
15975        let sql = "EXPLAIN EXPLAIN SELECT 1";
15976        let ast = Parser::parse_sql(&GenericDialect, sql);
15977        assert_eq!(
15978            ast,
15979            Err(ParserError::ParserError(
15980                "Explain must be root of the plan".to_string()
15981            ))
15982        );
15983    }
15984
15985    #[test]
15986    fn test_parse_multipart_identifier_positive() {
15987        let dialect = TestedDialects::new(vec![Box::new(GenericDialect {})]);
15988
15989        // parse multipart with quotes
15990        let expected = vec![
15991            Ident {
15992                value: "CATALOG".to_string(),
15993                quote_style: None,
15994                span: Span::empty(),
15995            },
15996            Ident {
15997                value: "F(o)o. \"bar".to_string(),
15998                quote_style: Some('"'),
15999                span: Span::empty(),
16000            },
16001            Ident {
16002                value: "table".to_string(),
16003                quote_style: None,
16004                span: Span::empty(),
16005            },
16006        ];
16007        dialect.run_parser_method(r#"CATALOG."F(o)o. ""bar".table"#, |parser| {
16008            let actual = parser.parse_multipart_identifier().unwrap();
16009            assert_eq!(expected, actual);
16010        });
16011
16012        // allow whitespace between ident parts
16013        let expected = vec![
16014            Ident {
16015                value: "CATALOG".to_string(),
16016                quote_style: None,
16017                span: Span::empty(),
16018            },
16019            Ident {
16020                value: "table".to_string(),
16021                quote_style: None,
16022                span: Span::empty(),
16023            },
16024        ];
16025        dialect.run_parser_method("CATALOG . table", |parser| {
16026            let actual = parser.parse_multipart_identifier().unwrap();
16027            assert_eq!(expected, actual);
16028        });
16029    }
16030
16031    #[test]
16032    fn test_parse_multipart_identifier_negative() {
16033        macro_rules! test_parse_multipart_identifier_error {
16034            ($input:expr, $expected_err:expr $(,)?) => {{
16035                all_dialects().run_parser_method(&*$input, |parser| {
16036                    let actual_err = parser.parse_multipart_identifier().unwrap_err();
16037                    assert_eq!(actual_err.to_string(), $expected_err);
16038                });
16039            }};
16040        }
16041
16042        test_parse_multipart_identifier_error!(
16043            "",
16044            "sql parser error: Empty input when parsing identifier",
16045        );
16046
16047        test_parse_multipart_identifier_error!(
16048            "*schema.table",
16049            "sql parser error: Unexpected token in identifier: *",
16050        );
16051
16052        test_parse_multipart_identifier_error!(
16053            "schema.table*",
16054            "sql parser error: Unexpected token in identifier: *",
16055        );
16056
16057        test_parse_multipart_identifier_error!(
16058            "schema.table.",
16059            "sql parser error: Trailing period in identifier",
16060        );
16061
16062        test_parse_multipart_identifier_error!(
16063            "schema.*",
16064            "sql parser error: Unexpected token following period in identifier: *",
16065        );
16066    }
16067
16068    #[test]
16069    fn test_mysql_partition_selection() {
16070        let sql = "SELECT * FROM employees PARTITION (p0, p2)";
16071        let expected = vec!["p0", "p2"];
16072
16073        let ast: Vec<Statement> = Parser::parse_sql(&MySqlDialect {}, sql).unwrap();
16074        assert_eq!(ast.len(), 1);
16075        if let Statement::Query(v) = &ast[0] {
16076            if let SetExpr::Select(select) = &*v.body {
16077                assert_eq!(select.from.len(), 1);
16078                let from: &TableWithJoins = &select.from[0];
16079                let table_factor = &from.relation;
16080                if let TableFactor::Table { partitions, .. } = table_factor {
16081                    let actual: Vec<&str> = partitions
16082                        .iter()
16083                        .map(|ident| ident.value.as_str())
16084                        .collect();
16085                    assert_eq!(expected, actual);
16086                }
16087            }
16088        } else {
16089            panic!("fail to parse mysql partition selection");
16090        }
16091    }
16092
16093    #[test]
16094    fn test_replace_into_placeholders() {
16095        let sql = "REPLACE INTO t (a) VALUES (&a)";
16096
16097        assert!(Parser::parse_sql(&GenericDialect {}, sql).is_err());
16098    }
16099
16100    #[test]
16101    fn test_replace_into_set_placeholder() {
16102        let sql = "REPLACE INTO t SET ?";
16103
16104        assert!(Parser::parse_sql(&GenericDialect {}, sql).is_err());
16105    }
16106
16107    #[test]
16108    fn test_replace_incomplete() {
16109        let sql = r#"REPLACE"#;
16110
16111        assert!(Parser::parse_sql(&MySqlDialect {}, sql).is_err());
16112    }
16113}