Skip to main content

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::SELECT | Keyword::WITH | Keyword::VALUES | Keyword::FROM => {
532                    self.prev_token();
533                    self.parse_query().map(Statement::Query)
534                }
535                // DuckDB statement-form PIVOT/UNPIVOT (`PIVOT t ON … USING …`) are
536                // queries in their own right — route them through `parse_query`,
537                // which handles the body via `parse_query_body`.
538                Keyword::PIVOT | Keyword::UNPIVOT if dialect_of!(self is DuckDbDialect | GenericDialect) =>
539                {
540                    self.prev_token();
541                    self.parse_query().map(Statement::Query)
542                }
543                Keyword::TRUNCATE => self.parse_truncate(),
544                Keyword::ATTACH => {
545                    if dialect_of!(self is DuckDbDialect) {
546                        self.parse_attach_duckdb_database()
547                    } else {
548                        self.parse_attach_database()
549                    }
550                }
551                Keyword::DETACH if dialect_of!(self is DuckDbDialect | GenericDialect) => {
552                    self.parse_detach_duckdb_database()
553                }
554                Keyword::MSCK => self.parse_msck(),
555                Keyword::CREATE => self.parse_create(),
556                Keyword::CACHE => self.parse_cache_table(),
557                Keyword::DROP => self.parse_drop(),
558                Keyword::DISCARD => self.parse_discard(),
559                Keyword::DECLARE => self.parse_declare(),
560                Keyword::FETCH => self.parse_fetch_statement(),
561                Keyword::DELETE => self.parse_delete(),
562                Keyword::INSERT => self.parse_insert(),
563                Keyword::REPLACE => self.parse_replace(),
564                Keyword::UNCACHE => self.parse_uncache_table(),
565                Keyword::UPDATE => self.parse_update(),
566                Keyword::ALTER => self.parse_alter(),
567                Keyword::CALL => self.parse_call(),
568                Keyword::COPY => self.parse_copy(),
569                Keyword::CLOSE => self.parse_close(),
570                Keyword::SET => self.parse_set(),
571                Keyword::SHOW => self.parse_show(),
572                Keyword::USE => self.parse_use(),
573                Keyword::GRANT => self.parse_grant(),
574                Keyword::REVOKE => self.parse_revoke(),
575                Keyword::START => self.parse_start_transaction(),
576                // `BEGIN` is a nonstandard but common alias for the
577                // standard `START TRANSACTION` statement. It is supported
578                // by at least PostgreSQL and MySQL.
579                Keyword::BEGIN => self.parse_begin(),
580                // `END` is a nonstandard but common alias for the
581                // standard `COMMIT TRANSACTION` statement. It is supported
582                // by PostgreSQL.
583                Keyword::END => self.parse_end(),
584                Keyword::SAVEPOINT => self.parse_savepoint(),
585                Keyword::RELEASE => self.parse_release(),
586                Keyword::COMMIT => self.parse_commit(),
587                Keyword::RAISERROR => Ok(self.parse_raiserror()?),
588                Keyword::ROLLBACK => self.parse_rollback(),
589                Keyword::ASSERT => self.parse_assert(),
590                // `PREPARE`, `EXECUTE` and `DEALLOCATE` are Postgres-specific
591                // syntaxes. They are used for Postgres prepared statement.
592                Keyword::DEALLOCATE => self.parse_deallocate(),
593                Keyword::EXECUTE | Keyword::EXEC => self.parse_execute(),
594                Keyword::PREPARE => self.parse_prepare(),
595                Keyword::MERGE => self.parse_merge(),
596                // `LISTEN`, `UNLISTEN` and `NOTIFY` are Postgres-specific
597                // syntaxes. They are used for Postgres statement.
598                Keyword::LISTEN if self.dialect.supports_listen_notify() => self.parse_listen(),
599                Keyword::UNLISTEN if self.dialect.supports_listen_notify() => self.parse_unlisten(),
600                Keyword::NOTIFY if self.dialect.supports_listen_notify() => self.parse_notify(),
601                // `PRAGMA` is sqlite specific https://www.sqlite.org/pragma.html
602                Keyword::PRAGMA => self.parse_pragma(),
603                Keyword::UNLOAD => self.parse_unload(),
604                Keyword::RENAME => self.parse_rename(),
605                // `INSTALL` is duckdb specific https://duckdb.org/docs/extensions/overview
606                Keyword::INSTALL if dialect_of!(self is DuckDbDialect | GenericDialect) => {
607                    self.parse_install(false)
608                }
609                // `FORCE INSTALL` — DuckDB's re-download variant. The `FORCE`
610                // keyword already exists for MySQL index hints, so this only
611                // claims it when it is immediately followed by `INSTALL`.
612                Keyword::FORCE
613                    if dialect_of!(self is DuckDbDialect | GenericDialect)
614                        && self.peek_keyword(Keyword::INSTALL) =>
615                {
616                    self.expect_keyword_is(Keyword::INSTALL)?;
617                    self.parse_install(true)
618                }
619                Keyword::LOAD => self.parse_load(),
620                // `OPTIMIZE` is clickhouse specific https://clickhouse.tech/docs/en/sql-reference/statements/optimize/
621                Keyword::OPTIMIZE if dialect_of!(self is ClickHouseDialect | GenericDialect) => {
622                    self.parse_optimize_table()
623                }
624                // `COMMENT` is snowflake specific https://docs.snowflake.com/en/sql-reference/sql/comment
625                Keyword::COMMENT if self.dialect.supports_comment_on() => self.parse_comment(),
626                _ => self.expected("an SQL statement", next_token),
627            },
628            Token::LParen => {
629                self.prev_token();
630                self.parse_query().map(Statement::Query)
631            }
632            _ => self.expected("an SQL statement", next_token),
633        }
634    }
635
636    pub fn parse_comment(&mut self) -> Result<Statement, ParserError> {
637        let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
638
639        self.expect_keyword_is(Keyword::ON)?;
640        let token = self.next_token();
641
642        let (object_type, object_name) = match token.token {
643            Token::Word(w) if w.keyword == Keyword::COLUMN => {
644                (CommentObject::Column, self.parse_object_name(false)?)
645            }
646            Token::Word(w) if w.keyword == Keyword::TABLE => {
647                (CommentObject::Table, self.parse_object_name(false)?)
648            }
649            Token::Word(w) if w.keyword == Keyword::EXTENSION => {
650                (CommentObject::Extension, self.parse_object_name(false)?)
651            }
652            Token::Word(w) if w.keyword == Keyword::SCHEMA => {
653                (CommentObject::Schema, self.parse_object_name(false)?)
654            }
655            Token::Word(w) if w.keyword == Keyword::DATABASE => {
656                (CommentObject::Database, self.parse_object_name(false)?)
657            }
658            Token::Word(w) if w.keyword == Keyword::USER => {
659                (CommentObject::User, self.parse_object_name(false)?)
660            }
661            Token::Word(w) if w.keyword == Keyword::ROLE => {
662                (CommentObject::Role, self.parse_object_name(false)?)
663            }
664            _ => self.expected("comment object_type", token)?,
665        };
666
667        self.expect_keyword_is(Keyword::IS)?;
668        let comment = if self.parse_keyword(Keyword::NULL) {
669            None
670        } else {
671            Some(self.parse_literal_string()?)
672        };
673        Ok(Statement::Comment {
674            object_type,
675            object_name,
676            comment,
677            if_exists,
678        })
679    }
680
681    pub fn parse_flush(&mut self) -> Result<Statement, ParserError> {
682        let mut channel = None;
683        let mut tables: Vec<ObjectName> = vec![];
684        let mut read_lock = false;
685        let mut export = false;
686
687        if !dialect_of!(self is MySqlDialect | GenericDialect) {
688            return parser_err!("Unsupported statement FLUSH", self.peek_token().span.start);
689        }
690
691        let location = if self.parse_keyword(Keyword::NO_WRITE_TO_BINLOG) {
692            Some(FlushLocation::NoWriteToBinlog)
693        } else if self.parse_keyword(Keyword::LOCAL) {
694            Some(FlushLocation::Local)
695        } else {
696            None
697        };
698
699        let object_type = if self.parse_keywords(&[Keyword::BINARY, Keyword::LOGS]) {
700            FlushType::BinaryLogs
701        } else if self.parse_keywords(&[Keyword::ENGINE, Keyword::LOGS]) {
702            FlushType::EngineLogs
703        } else if self.parse_keywords(&[Keyword::ERROR, Keyword::LOGS]) {
704            FlushType::ErrorLogs
705        } else if self.parse_keywords(&[Keyword::GENERAL, Keyword::LOGS]) {
706            FlushType::GeneralLogs
707        } else if self.parse_keywords(&[Keyword::HOSTS]) {
708            FlushType::Hosts
709        } else if self.parse_keyword(Keyword::PRIVILEGES) {
710            FlushType::Privileges
711        } else if self.parse_keyword(Keyword::OPTIMIZER_COSTS) {
712            FlushType::OptimizerCosts
713        } else if self.parse_keywords(&[Keyword::RELAY, Keyword::LOGS]) {
714            if self.parse_keywords(&[Keyword::FOR, Keyword::CHANNEL]) {
715                channel = Some(self.parse_object_name(false).unwrap().to_string());
716            }
717            FlushType::RelayLogs
718        } else if self.parse_keywords(&[Keyword::SLOW, Keyword::LOGS]) {
719            FlushType::SlowLogs
720        } else if self.parse_keyword(Keyword::STATUS) {
721            FlushType::Status
722        } else if self.parse_keyword(Keyword::USER_RESOURCES) {
723            FlushType::UserResources
724        } else if self.parse_keywords(&[Keyword::LOGS]) {
725            FlushType::Logs
726        } else if self.parse_keywords(&[Keyword::TABLES]) {
727            loop {
728                let next_token = self.next_token();
729                match &next_token.token {
730                    Token::Word(w) => match w.keyword {
731                        Keyword::WITH => {
732                            read_lock = self.parse_keywords(&[Keyword::READ, Keyword::LOCK]);
733                        }
734                        Keyword::FOR => {
735                            export = self.parse_keyword(Keyword::EXPORT);
736                        }
737                        Keyword::NoKeyword => {
738                            self.prev_token();
739                            tables = self.parse_comma_separated(|p| p.parse_object_name(false))?;
740                        }
741                        _ => {}
742                    },
743                    _ => {
744                        break;
745                    }
746                }
747            }
748
749            FlushType::Tables
750        } else {
751            return self.expected(
752                "BINARY LOGS, ENGINE LOGS, ERROR LOGS, GENERAL LOGS, HOSTS, LOGS, PRIVILEGES, OPTIMIZER_COSTS,\
753                 RELAY LOGS [FOR CHANNEL channel], SLOW LOGS, STATUS, USER_RESOURCES",
754                self.peek_token(),
755            );
756        };
757
758        Ok(Statement::Flush {
759            object_type,
760            location,
761            channel,
762            read_lock,
763            export,
764            tables,
765        })
766    }
767
768    pub fn parse_msck(&mut self) -> Result<Statement, ParserError> {
769        let repair = self.parse_keyword(Keyword::REPAIR);
770        self.expect_keyword_is(Keyword::TABLE)?;
771        let table_name = self.parse_object_name(false)?;
772        let partition_action = self
773            .maybe_parse(|parser| {
774                let pa = match parser.parse_one_of_keywords(&[
775                    Keyword::ADD,
776                    Keyword::DROP,
777                    Keyword::SYNC,
778                ]) {
779                    Some(Keyword::ADD) => Some(AddDropSync::ADD),
780                    Some(Keyword::DROP) => Some(AddDropSync::DROP),
781                    Some(Keyword::SYNC) => Some(AddDropSync::SYNC),
782                    _ => None,
783                };
784                parser.expect_keyword_is(Keyword::PARTITIONS)?;
785                Ok(pa)
786            })?
787            .unwrap_or_default();
788        Ok(Statement::Msck {
789            repair,
790            table_name,
791            partition_action,
792        })
793    }
794
795    pub fn parse_truncate(&mut self) -> Result<Statement, ParserError> {
796        let table = self.parse_keyword(Keyword::TABLE);
797        let only = self.parse_keyword(Keyword::ONLY);
798
799        let table_names = self
800            .parse_comma_separated(|p| p.parse_object_name(false))?
801            .into_iter()
802            .map(|n| TruncateTableTarget { name: n })
803            .collect();
804
805        let mut partitions = None;
806        if self.parse_keyword(Keyword::PARTITION) {
807            self.expect_token(&Token::LParen)?;
808            partitions = Some(self.parse_comma_separated(Parser::parse_expr)?);
809            self.expect_token(&Token::RParen)?;
810        }
811
812        let mut identity = None;
813        let mut cascade = None;
814
815        if dialect_of!(self is PostgreSqlDialect | GenericDialect) {
816            identity = if self.parse_keywords(&[Keyword::RESTART, Keyword::IDENTITY]) {
817                Some(TruncateIdentityOption::Restart)
818            } else if self.parse_keywords(&[Keyword::CONTINUE, Keyword::IDENTITY]) {
819                Some(TruncateIdentityOption::Continue)
820            } else {
821                None
822            };
823
824            cascade = self.parse_cascade_option();
825        };
826
827        let on_cluster = self.parse_optional_on_cluster()?;
828
829        Ok(Statement::Truncate {
830            table_names,
831            partitions,
832            table,
833            only,
834            identity,
835            cascade,
836            on_cluster,
837        })
838    }
839
840    fn parse_cascade_option(&mut self) -> Option<CascadeOption> {
841        if self.parse_keyword(Keyword::CASCADE) {
842            Some(CascadeOption::Cascade)
843        } else if self.parse_keyword(Keyword::RESTRICT) {
844            Some(CascadeOption::Restrict)
845        } else {
846            None
847        }
848    }
849
850    pub fn parse_attach_duckdb_database_options(
851        &mut self,
852    ) -> Result<Vec<AttachDuckDBDatabaseOption>, ParserError> {
853        if !self.consume_token(&Token::LParen) {
854            return Ok(vec![]);
855        }
856
857        let mut options = vec![];
858        loop {
859            if self.parse_keyword(Keyword::READ_ONLY) {
860                let boolean = if self.parse_keyword(Keyword::TRUE) {
861                    Some(true)
862                } else if self.parse_keyword(Keyword::FALSE) {
863                    Some(false)
864                } else {
865                    None
866                };
867                options.push(AttachDuckDBDatabaseOption::ReadOnly(boolean));
868            } else if self.parse_keyword(Keyword::TYPE) {
869                let ident = self.parse_identifier()?;
870                options.push(AttachDuckDBDatabaseOption::Type(ident));
871            } else {
872                return self.expected("expected one of: ), READ_ONLY, TYPE", self.peek_token());
873            };
874
875            if self.consume_token(&Token::RParen) {
876                return Ok(options);
877            } else if self.consume_token(&Token::Comma) {
878                continue;
879            } else {
880                return self.expected("expected one of: ')', ','", self.peek_token());
881            }
882        }
883    }
884
885    pub fn parse_attach_duckdb_database(&mut self) -> Result<Statement, ParserError> {
886        let database = self.parse_keyword(Keyword::DATABASE);
887        let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
888        let database_path = self.parse_identifier()?;
889        let database_alias = if self.parse_keyword(Keyword::AS) {
890            Some(self.parse_identifier()?)
891        } else {
892            None
893        };
894
895        let attach_options = self.parse_attach_duckdb_database_options()?;
896        Ok(Statement::AttachDuckDBDatabase {
897            if_not_exists,
898            database,
899            database_path,
900            database_alias,
901            attach_options,
902        })
903    }
904
905    pub fn parse_detach_duckdb_database(&mut self) -> Result<Statement, ParserError> {
906        let database = self.parse_keyword(Keyword::DATABASE);
907        let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
908        let database_alias = self.parse_identifier()?;
909        Ok(Statement::DetachDuckDBDatabase {
910            if_exists,
911            database,
912            database_alias,
913        })
914    }
915
916    pub fn parse_attach_database(&mut self) -> Result<Statement, ParserError> {
917        let database = self.parse_keyword(Keyword::DATABASE);
918        let database_file_name = self.parse_expr()?;
919        self.expect_keyword_is(Keyword::AS)?;
920        let schema_name = self.parse_identifier()?;
921        Ok(Statement::AttachDatabase {
922            database,
923            schema_name,
924            database_file_name,
925        })
926    }
927
928    pub fn parse_analyze(&mut self) -> Result<Statement, ParserError> {
929        let has_table_keyword = self.parse_keyword(Keyword::TABLE);
930        let table_name = self.parse_object_name(false)?;
931        let mut for_columns = false;
932        let mut cache_metadata = false;
933        let mut noscan = false;
934        let mut partitions = None;
935        let mut compute_statistics = false;
936        let mut columns = vec![];
937        loop {
938            match self.parse_one_of_keywords(&[
939                Keyword::PARTITION,
940                Keyword::FOR,
941                Keyword::CACHE,
942                Keyword::NOSCAN,
943                Keyword::COMPUTE,
944            ]) {
945                Some(Keyword::PARTITION) => {
946                    self.expect_token(&Token::LParen)?;
947                    partitions = Some(self.parse_comma_separated(Parser::parse_expr)?);
948                    self.expect_token(&Token::RParen)?;
949                }
950                Some(Keyword::NOSCAN) => noscan = true,
951                Some(Keyword::FOR) => {
952                    self.expect_keyword_is(Keyword::COLUMNS)?;
953
954                    columns = self
955                        .maybe_parse(|parser| {
956                            parser.parse_comma_separated(|p| p.parse_identifier())
957                        })?
958                        .unwrap_or_default();
959                    for_columns = true
960                }
961                Some(Keyword::CACHE) => {
962                    self.expect_keyword_is(Keyword::METADATA)?;
963                    cache_metadata = true
964                }
965                Some(Keyword::COMPUTE) => {
966                    self.expect_keyword_is(Keyword::STATISTICS)?;
967                    compute_statistics = true
968                }
969                _ => break,
970            }
971        }
972
973        Ok(Statement::Analyze {
974            has_table_keyword,
975            table_name,
976            for_columns,
977            columns,
978            partitions,
979            cache_metadata,
980            noscan,
981            compute_statistics,
982        })
983    }
984
985    /// Parse a new expression including wildcard & qualified wildcard.
986    pub fn parse_wildcard_expr(&mut self) -> Result<Expr, ParserError> {
987        let index = self.index;
988
989        let next_token = self.next_token();
990        match next_token.token {
991            t @ (Token::Word(_) | Token::SingleQuotedString(_)) => {
992                if self.peek_token().token == Token::Period {
993                    let mut id_parts: Vec<Ident> = vec![match t {
994                        Token::Word(w) => w.into_ident(next_token.span),
995                        Token::SingleQuotedString(s) => Ident::with_quote('\'', s),
996                        _ => unreachable!(), // We matched above
997                    }];
998
999                    while self.consume_token(&Token::Period) {
1000                        let next_token = self.next_token();
1001                        match next_token.token {
1002                            Token::Word(w) => id_parts.push(w.into_ident(next_token.span)),
1003                            Token::SingleQuotedString(s) => {
1004                                // SQLite has single-quoted identifiers
1005                                id_parts.push(Ident::with_quote('\'', s))
1006                            }
1007                            Token::Mul => {
1008                                return Ok(Expr::QualifiedWildcard(
1009                                    ObjectName::from(id_parts),
1010                                    AttachedToken(next_token),
1011                                ));
1012                            }
1013                            _ => {
1014                                return self
1015                                    .expected("an identifier or a '*' after '.'", next_token);
1016                            }
1017                        }
1018                    }
1019                }
1020            }
1021            Token::Mul => {
1022                return Ok(Expr::Wildcard(AttachedToken(next_token)));
1023            }
1024            _ => (),
1025        };
1026
1027        self.index = index;
1028        self.parse_expr()
1029    }
1030
1031    /// Parse a new expression.
1032    pub fn parse_expr(&mut self) -> Result<Expr, ParserError> {
1033        self.parse_subexpr(self.dialect.prec_unknown())
1034    }
1035
1036    /// Parse tokens until the precedence changes.
1037    pub fn parse_subexpr(&mut self, precedence: u8) -> Result<Expr, ParserError> {
1038        let _guard = self.recursion_counter.try_decrease()?;
1039        debug!("parsing expr");
1040        let mut expr = self.parse_prefix()?;
1041
1042        expr = self.parse_compound_expr(expr, vec![])?;
1043
1044        debug!("prefix: {:?}", expr);
1045        loop {
1046            let next_precedence = self.get_next_precedence()?;
1047            debug!("next precedence: {:?}", next_precedence);
1048
1049            if precedence >= next_precedence {
1050                break;
1051            }
1052
1053            // The period operator is handled exclusively by the
1054            // compound field access parsing.
1055            if Token::Period == self.peek_token_ref().token {
1056                break;
1057            }
1058
1059            expr = self.parse_infix(expr, next_precedence)?;
1060        }
1061        Ok(expr)
1062    }
1063
1064    pub fn parse_assert(&mut self) -> Result<Statement, ParserError> {
1065        let condition = self.parse_expr()?;
1066        let message = if self.parse_keyword(Keyword::AS) {
1067            Some(self.parse_expr()?)
1068        } else {
1069            None
1070        };
1071
1072        Ok(Statement::Assert { condition, message })
1073    }
1074
1075    pub fn parse_savepoint(&mut self) -> Result<Statement, ParserError> {
1076        let name = self.parse_identifier()?;
1077        Ok(Statement::Savepoint { name })
1078    }
1079
1080    pub fn parse_release(&mut self) -> Result<Statement, ParserError> {
1081        let _ = self.parse_keyword(Keyword::SAVEPOINT);
1082        let name = self.parse_identifier()?;
1083
1084        Ok(Statement::ReleaseSavepoint { name })
1085    }
1086
1087    pub fn parse_listen(&mut self) -> Result<Statement, ParserError> {
1088        let channel = self.parse_identifier()?;
1089        Ok(Statement::LISTEN { channel })
1090    }
1091
1092    pub fn parse_unlisten(&mut self) -> Result<Statement, ParserError> {
1093        let channel = if self.consume_token(&Token::Mul) {
1094            Ident::new(Expr::Wildcard(AttachedToken::empty()).to_string())
1095        } else {
1096            match self.parse_identifier() {
1097                Ok(expr) => expr,
1098                _ => {
1099                    self.prev_token();
1100                    return self.expected("wildcard or identifier", self.peek_token());
1101                }
1102            }
1103        };
1104        Ok(Statement::UNLISTEN { channel })
1105    }
1106
1107    pub fn parse_notify(&mut self) -> Result<Statement, ParserError> {
1108        let channel = self.parse_identifier()?;
1109        let payload = if self.consume_token(&Token::Comma) {
1110            Some(self.parse_literal_string()?)
1111        } else {
1112            None
1113        };
1114        Ok(Statement::NOTIFY { channel, payload })
1115    }
1116
1117    /// Parses a `RENAME TABLE` statement. See [Statement::RenameTable]
1118    pub fn parse_rename(&mut self) -> Result<Statement, ParserError> {
1119        if self.peek_keyword(Keyword::TABLE) {
1120            self.expect_keyword(Keyword::TABLE)?;
1121            let rename_tables = self.parse_comma_separated(|parser| {
1122                let old_name = parser.parse_object_name(false)?;
1123                parser.expect_keyword(Keyword::TO)?;
1124                let new_name = parser.parse_object_name(false)?;
1125
1126                Ok(RenameTable { old_name, new_name })
1127            })?;
1128            Ok(Statement::RenameTable(rename_tables))
1129        } else {
1130            self.expected("KEYWORD `TABLE` after RENAME", self.peek_token())
1131        }
1132    }
1133
1134    /// Tries to parse an expression by matching the specified word to known keywords that have a special meaning in the dialect.
1135    /// Returns `None if no match is found.
1136    fn parse_expr_prefix_by_reserved_word(
1137        &mut self,
1138        w: &Word,
1139        w_span: Span,
1140    ) -> Result<Option<Expr>, ParserError> {
1141        match w.keyword {
1142            Keyword::TRUE | Keyword::FALSE if self.dialect.supports_boolean_literals() => {
1143                self.prev_token();
1144                Ok(Some(Expr::Value(self.parse_value()?)))
1145            }
1146            Keyword::NULL => {
1147                self.prev_token();
1148                Ok(Some(Expr::Value(self.parse_value()?)))
1149            }
1150            Keyword::CURRENT_CATALOG
1151            | Keyword::CURRENT_USER
1152            | Keyword::SESSION_USER
1153            | Keyword::USER
1154            if dialect_of!(self is PostgreSqlDialect | GenericDialect) =>
1155                {
1156                    Ok(Some(Expr::Function(Function {
1157                        name: ObjectName::from(vec![w.clone().into_ident(w_span)]),
1158                        uses_odbc_syntax: false,
1159                        parameters: FunctionArguments::None,
1160                        args: FunctionArguments::None,
1161                        null_treatment: None,
1162                        filter: None,
1163                        over: None,
1164                        within_group: vec![],
1165                    })))
1166                }
1167            Keyword::CURRENT_TIMESTAMP
1168            | Keyword::CURRENT_TIME
1169            | Keyword::CURRENT_DATE
1170            | Keyword::LOCALTIME
1171            | Keyword::LOCALTIMESTAMP => {
1172                Ok(Some(self.parse_time_functions(ObjectName::from(vec![w.clone().into_ident(w_span)]))?))
1173            }
1174            Keyword::CASE => Ok(Some(self.parse_case_expr()?)),
1175            Keyword::CONVERT => Ok(Some(self.parse_convert_expr(false)?)),
1176            Keyword::TRY_CONVERT if self.dialect.supports_try_convert() => Ok(Some(self.parse_convert_expr(true)?)),
1177            Keyword::CAST => Ok(Some(self.parse_cast_expr(CastKind::Cast)?)),
1178            Keyword::TRY_CAST => Ok(Some(self.parse_cast_expr(CastKind::TryCast)?)),
1179            Keyword::SAFE_CAST => Ok(Some(self.parse_cast_expr(CastKind::SafeCast)?)),
1180            Keyword::EXISTS
1181            // Support parsing Databricks has a function named `exists`.
1182            if !dialect_of!(self is DatabricksDialect)
1183                || matches!(
1184                        self.peek_nth_token_ref(1).token,
1185                        Token::Word(Word {
1186                            keyword: Keyword::SELECT | Keyword::WITH,
1187                            ..
1188                        })
1189                    ) =>
1190                {
1191                    Ok(Some(self.parse_exists_expr(false)?))
1192                }
1193            Keyword::EXTRACT => Ok(Some(self.parse_extract_expr()?)),
1194            Keyword::CEIL => Ok(Some(self.parse_ceil_floor_expr(true)?)),
1195            Keyword::FLOOR => Ok(Some(self.parse_ceil_floor_expr(false)?)),
1196            Keyword::POSITION if self.peek_token_ref().token == Token::LParen => {
1197                Ok(Some(self.parse_position_expr(w.clone().into_ident(w_span))?))
1198            }
1199            Keyword::SUBSTRING => Ok(Some(self.parse_substring_expr()?)),
1200            Keyword::OVERLAY => Ok(Some(self.parse_overlay_expr()?)),
1201            Keyword::TRIM => Ok(Some(self.parse_trim_expr()?)),
1202            Keyword::INTERVAL => Ok(Some(self.parse_interval()?)),
1203            // Treat ARRAY[1,2,3] as an array [1,2,3], otherwise try as subquery or a function call
1204            Keyword::ARRAY if *self.peek_token_ref() == Token::LBracket => {
1205                self.expect_token(&Token::LBracket)?;
1206                Ok(Some(self.parse_array_expr(true)?))
1207            }
1208            Keyword::ARRAY
1209            if self.peek_token() == Token::LParen
1210                && !dialect_of!(self is ClickHouseDialect | DatabricksDialect) =>
1211                {
1212                    self.expect_token(&Token::LParen)?;
1213                    let query = self.parse_query()?;
1214                    self.expect_token(&Token::RParen)?;
1215                    Ok(Some(Expr::Function(Function {
1216                        name: ObjectName::from(vec![w.clone().into_ident(w_span)]),
1217                        uses_odbc_syntax: false,
1218                        parameters: FunctionArguments::None,
1219                        args: FunctionArguments::Subquery(query),
1220                        filter: None,
1221                        null_treatment: None,
1222                        over: None,
1223                        within_group: vec![],
1224                    })))
1225                }
1226            Keyword::NOT => Ok(Some(self.parse_not()?)),
1227            Keyword::MATCH if self.dialect.supports_match_against() => {
1228                Ok(Some(self.parse_match_against()?))
1229            }
1230            Keyword::STRUCT if self.dialect.supports_struct_literal() => {
1231                let struct_expr = self.parse_struct_literal()?;
1232                Ok(Some(struct_expr))
1233            }
1234            Keyword::PRIOR if matches!(self.state, ParserState::ConnectBy) => {
1235                let expr = self.parse_subexpr(self.dialect.prec_value(Precedence::PlusMinus))?;
1236                Ok(Some(Expr::Prior(Box::new(expr))))
1237            }
1238            Keyword::MAP if *self.peek_token_ref() == Token::LBrace && self.dialect.support_map_literal_syntax() => {
1239                Ok(Some(self.parse_duckdb_map_literal()?))
1240            }
1241            // DuckDB's Python-style `lambda x: expr` / `lambda x, y: expr`. On a
1242            // parse failure below (e.g. `SELECT lambda FROM t` where `lambda` is a
1243            // column name) the caller falls back to treating the word as a plain
1244            // identifier — see the `try_parse`/`maybe_parse` dance in `parse_prefix`.
1245            Keyword::LAMBDA if self.dialect.supports_lambda_colon_syntax() => {
1246                Ok(Some(self.parse_lambda_colon_expr()?))
1247            }
1248            _ if self.dialect.supports_geometric_types() => match w.keyword {
1249                Keyword::CIRCLE => Ok(Some(self.parse_geometric_type(GeometricTypeKind::Circle)?)),
1250                Keyword::BOX => Ok(Some(self.parse_geometric_type(GeometricTypeKind::GeometricBox)?)),
1251                Keyword::PATH => Ok(Some(self.parse_geometric_type(GeometricTypeKind::GeometricPath)?)),
1252                Keyword::LINE => Ok(Some(self.parse_geometric_type(GeometricTypeKind::Line)?)),
1253                Keyword::LSEG => Ok(Some(self.parse_geometric_type(GeometricTypeKind::LineSegment)?)),
1254                Keyword::POINT => Ok(Some(self.parse_geometric_type(GeometricTypeKind::Point)?)),
1255                Keyword::POLYGON => Ok(Some(self.parse_geometric_type(GeometricTypeKind::Polygon)?)),
1256                _ => Ok(None),
1257            },
1258            _ => Ok(None),
1259        }
1260    }
1261
1262    /// Tries to parse an expression by a word that is not known to have a special meaning in the dialect.
1263    fn parse_expr_prefix_by_unreserved_word(
1264        &mut self,
1265        w: &Word,
1266        w_span: Span,
1267    ) -> Result<Expr, ParserError> {
1268        match self.peek_token().token {
1269            Token::LParen if !self.peek_outer_join_operator() => {
1270                let id_parts = vec![w.clone().into_ident(w_span)];
1271                self.parse_function(ObjectName::from(id_parts))
1272            }
1273            // string introducer https://dev.mysql.com/doc/refman/8.0/en/charset-introducer.html
1274            Token::SingleQuotedString(_)
1275            | Token::DoubleQuotedString(_)
1276            | Token::HexStringLiteral(_)
1277                if w.value.starts_with('_') =>
1278            {
1279                Ok(Expr::IntroducedString {
1280                    introducer: w.value.clone(),
1281                    value: self.parse_introduced_string_value()?,
1282                })
1283            }
1284            // string introducer https://dev.mysql.com/doc/refman/8.0/en/charset-introducer.html
1285            Token::SingleQuotedString(_)
1286            | Token::DoubleQuotedString(_)
1287            | Token::HexStringLiteral(_)
1288                if w.value.starts_with('_') =>
1289            {
1290                Ok(Expr::IntroducedString {
1291                    introducer: w.value.clone(),
1292                    value: self.parse_introduced_string_value()?,
1293                })
1294            }
1295            Token::Arrow if self.dialect.supports_lambda_functions() => {
1296                self.expect_token(&Token::Arrow)?;
1297                Ok(Expr::Lambda(LambdaFunction {
1298                    params: OneOrManyWithParens::One(w.clone().into_ident(w_span)),
1299                    body: Box::new(self.parse_expr()?),
1300                }))
1301            }
1302            _ => Ok(Expr::Identifier(w.clone().into_ident(w_span))),
1303        }
1304    }
1305
1306    /// Parse an expression prefix.
1307    pub fn parse_prefix(&mut self) -> Result<Expr, ParserError> {
1308        // allow the dialect to override prefix parsing
1309        if let Some(prefix) = self.dialect.parse_prefix(self) {
1310            return prefix;
1311        }
1312
1313        // PostgreSQL allows any string literal to be preceded by a type name, indicating that the
1314        // string literal represents a literal of that type. Some examples:
1315        //
1316        //      DATE '2020-05-20'
1317        //      TIMESTAMP WITH TIME ZONE '2020-05-20 7:43:54'
1318        //      BOOL 'true'
1319        //
1320        // The first two are standard SQL, while the latter is a PostgreSQL extension. Complicating
1321        // matters is the fact that INTERVAL string literals may optionally be followed by special
1322        // keywords, e.g.:
1323        //
1324        //      INTERVAL '7' DAY
1325        //
1326        // Note also that naively `SELECT date` looks like a syntax error because the `date` type
1327        // name is not followed by a string literal, but in fact in PostgreSQL it is a valid
1328        // expression that should parse as the column name "date".
1329        let loc = self.peek_token_ref().span.start;
1330        let opt_expr = self.maybe_parse(|parser| {
1331            match parser.parse_data_type()? {
1332                DataType::Interval => parser.parse_interval(),
1333                // PostgreSQL allows almost any identifier to be used as custom data type name,
1334                // and we support that in `parse_data_type()`. But unlike Postgres we don't
1335                // have a list of globally reserved keywords (since they vary across dialects),
1336                // so given `NOT 'a' LIKE 'b'`, we'd accept `NOT` as a possible custom data type
1337                // name, resulting in `NOT 'a'` being recognized as a `TypedString` instead of
1338                // an unary negation `NOT ('a' LIKE 'b')`. To solve this, we don't accept the
1339                // `type 'string'` syntax for the custom data types at all.
1340                DataType::Custom(..) => parser_err!("dummy", loc),
1341                data_type => Ok(Expr::TypedString {
1342                    data_type,
1343                    value: parser.parse_value()?.value,
1344                }),
1345            }
1346        })?;
1347
1348        if let Some(expr) = opt_expr {
1349            return Ok(expr);
1350        }
1351
1352        // Cache some dialect properties to avoid lifetime issues with the
1353        // next_token reference.
1354
1355        let dialect = self.dialect;
1356
1357        self.advance_token();
1358        let next_token_index = self.get_current_index();
1359        let next_token = self.get_current_token();
1360        let span = next_token.span;
1361        let expr = match &next_token.token {
1362            Token::Word(w) => {
1363                // The word we consumed may fall into one of two cases: it has a special meaning, or not.
1364                // For example, in Snowflake, the word `interval` may have two meanings depending on the context:
1365                // `SELECT CURRENT_DATE() + INTERVAL '1 DAY', MAX(interval) FROM tbl;`
1366                //                          ^^^^^^^^^^^^^^^^      ^^^^^^^^
1367                //                         interval expression   identifier
1368                //
1369                // We first try to parse the word and following tokens as a special expression, and if that fails,
1370                // we rollback and try to parse it as an identifier.
1371                let w = w.clone();
1372                match self.try_parse(|parser| parser.parse_expr_prefix_by_reserved_word(&w, span)) {
1373                    // This word indicated an expression prefix and parsing was successful
1374                    Ok(Some(expr)) => Ok(expr),
1375
1376                    // No expression prefix associated with this word
1377                    Ok(None) => Ok(self.parse_expr_prefix_by_unreserved_word(&w, span)?),
1378
1379                    // If parsing of the word as a special expression failed, we are facing two options:
1380                    // 1. The statement is malformed, e.g. `SELECT INTERVAL '1 DAI` (`DAI` instead of `DAY`)
1381                    // 2. The word is used as an identifier, e.g. `SELECT MAX(interval) FROM tbl`
1382                    // We first try to parse the word as an identifier and if that fails
1383                    // we rollback and return the parsing error we got from trying to parse a
1384                    // special expression (to maintain backwards compatibility of parsing errors).
1385                    Err(e) => {
1386                        if !self.dialect.is_reserved_for_identifier(w.keyword) {
1387                            if let Ok(Some(expr)) = self.maybe_parse(|parser| {
1388                                parser.parse_expr_prefix_by_unreserved_word(&w, span)
1389                            }) {
1390                                return Ok(expr);
1391                            }
1392                        }
1393                        return Err(e);
1394                    }
1395                }
1396            } // End of Token::Word
1397            // array `[1, 2, 3]`
1398            Token::LBracket => self.parse_array_expr(false),
1399            tok @ Token::Minus | tok @ Token::Plus => {
1400                let op = if *tok == Token::Plus {
1401                    UnaryOperator::Plus
1402                } else {
1403                    UnaryOperator::Minus
1404                };
1405                Ok(Expr::UnaryOp {
1406                    op,
1407                    expr: Box::new(
1408                        self.parse_subexpr(self.dialect.prec_value(Precedence::MulDivModOp))?,
1409                    ),
1410                })
1411            }
1412            Token::ExclamationMark if dialect.supports_bang_not_operator() => Ok(Expr::UnaryOp {
1413                op: UnaryOperator::BangNot,
1414                expr: Box::new(self.parse_subexpr(self.dialect.prec_value(Precedence::UnaryNot))?),
1415            }),
1416            tok @ Token::DoubleExclamationMark
1417            | tok @ Token::PGSquareRoot
1418            | tok @ Token::PGCubeRoot
1419            | tok @ Token::AtSign
1420            | tok @ Token::Tilde
1421                if dialect_is!(dialect is PostgreSqlDialect) =>
1422            {
1423                let op = match tok {
1424                    Token::DoubleExclamationMark => UnaryOperator::PGPrefixFactorial,
1425                    Token::PGSquareRoot => UnaryOperator::PGSquareRoot,
1426                    Token::PGCubeRoot => UnaryOperator::PGCubeRoot,
1427                    Token::AtSign => UnaryOperator::PGAbs,
1428                    Token::Tilde => UnaryOperator::PGBitwiseNot,
1429                    _ => unreachable!(),
1430                };
1431                Ok(Expr::UnaryOp {
1432                    op,
1433                    expr: Box::new(
1434                        self.parse_subexpr(self.dialect.prec_value(Precedence::PlusMinus))?,
1435                    ),
1436                })
1437            }
1438            tok @ Token::Sharp
1439            | tok @ Token::AtDashAt
1440            | tok @ Token::AtAt
1441            | tok @ Token::QuestionMarkDash
1442            | tok @ Token::QuestionPipe
1443                if self.dialect.supports_geometric_types() =>
1444            {
1445                let op = match tok {
1446                    Token::Sharp => UnaryOperator::Hash,
1447                    Token::AtDashAt => UnaryOperator::AtDashAt,
1448                    Token::AtAt => UnaryOperator::DoubleAt,
1449                    Token::QuestionMarkDash => UnaryOperator::QuestionDash,
1450                    Token::QuestionPipe => UnaryOperator::QuestionPipe,
1451                    _ => {
1452                        return Err(ParserError::ParserError(format!(
1453                            "Unexpected token in unary operator parsing: {:?}",
1454                            tok
1455                        )))
1456                    }
1457                };
1458                Ok(Expr::UnaryOp {
1459                    op,
1460                    expr: Box::new(
1461                        self.parse_subexpr(self.dialect.prec_value(Precedence::PlusMinus))?,
1462                    ),
1463                })
1464            }
1465            Token::EscapedStringLiteral(_) if dialect_is!(dialect is PostgreSqlDialect | GenericDialect) =>
1466            {
1467                self.prev_token();
1468                Ok(Expr::Value(self.parse_value()?))
1469            }
1470            Token::UnicodeStringLiteral(_) => {
1471                self.prev_token();
1472                Ok(Expr::Value(self.parse_value()?))
1473            }
1474            Token::Number(_, _)
1475            | Token::SingleQuotedString(_)
1476            | Token::DoubleQuotedString(_)
1477            | Token::TripleSingleQuotedString(_)
1478            | Token::TripleDoubleQuotedString(_)
1479            | Token::DollarQuotedString(_)
1480            | Token::SingleQuotedByteStringLiteral(_)
1481            | Token::DoubleQuotedByteStringLiteral(_)
1482            | Token::TripleSingleQuotedByteStringLiteral(_)
1483            | Token::TripleDoubleQuotedByteStringLiteral(_)
1484            | Token::SingleQuotedRawStringLiteral(_)
1485            | Token::DoubleQuotedRawStringLiteral(_)
1486            | Token::TripleSingleQuotedRawStringLiteral(_)
1487            | Token::TripleDoubleQuotedRawStringLiteral(_)
1488            | Token::NationalStringLiteral(_)
1489            | Token::HexStringLiteral(_) => {
1490                self.prev_token();
1491                Ok(Expr::Value(self.parse_value()?))
1492            }
1493            Token::LParen => {
1494                let expr = if let Some(expr) = self.try_parse_expr_sub_query()? {
1495                    expr
1496                } else if let Some(lambda) = self.try_parse_lambda()? {
1497                    return Ok(lambda);
1498                } else {
1499                    let exprs = self.parse_comma_separated(Parser::parse_expr)?;
1500                    match exprs.len() {
1501                        0 => unreachable!(), // parse_comma_separated ensures 1 or more
1502                        1 => Expr::Nested(Box::new(exprs.into_iter().next().unwrap())),
1503                        _ => Expr::Tuple(exprs),
1504                    }
1505                };
1506                self.expect_token(&Token::RParen)?;
1507                Ok(expr)
1508            }
1509            Token::Placeholder(_) | Token::Colon | Token::AtSign => {
1510                self.prev_token();
1511                Ok(Expr::Value(self.parse_value()?))
1512            }
1513            Token::LBrace => {
1514                self.prev_token();
1515                self.parse_lbrace_expr()
1516            }
1517            _ => self.expected_at("an expression", next_token_index),
1518        }?;
1519
1520        if self.parse_keyword(Keyword::COLLATE) {
1521            Ok(Expr::Collate {
1522                expr: Box::new(expr),
1523                collation: self.parse_object_name(false)?,
1524            })
1525        } else {
1526            Ok(expr)
1527        }
1528    }
1529
1530    fn parse_geometric_type(&mut self, kind: GeometricTypeKind) -> Result<Expr, ParserError> {
1531        let value: Value = self.parse_value()?.value;
1532        Ok(Expr::TypedString {
1533            data_type: DataType::GeometricType(kind),
1534            value,
1535        })
1536    }
1537
1538    /// Try to parse an [Expr::CompoundFieldAccess] like `a.b.c` or `a.b[1].c`.
1539    /// If all the fields are `Expr::Identifier`s, return an [Expr::CompoundIdentifier] instead.
1540    /// If only the root exists, return the root.
1541    /// Parses compound expressions which may be delimited by period
1542    /// or bracket notation.
1543    /// For example: `a.b.c`, `a.b[1]`.
1544    pub fn parse_compound_expr(
1545        &mut self,
1546        root: Expr,
1547        mut chain: Vec<AccessExpr>,
1548    ) -> Result<Expr, ParserError> {
1549        let mut ending_wildcard: Option<TokenWithSpan> = None;
1550        loop {
1551            if self.consume_token(&Token::Period) {
1552                let next_token = self.peek_token_ref();
1553                match &next_token.token {
1554                    Token::Mul => {
1555                        // Postgres explicitly allows funcnm(tablenm.*) and the
1556                        // function array_agg traverses this control flow
1557                        if dialect_of!(self is PostgreSqlDialect) {
1558                            ending_wildcard = Some(self.next_token());
1559                        } else {
1560                            // Put back the consumed `.` tokens before exiting.
1561                            // If this expression is being parsed in the
1562                            // context of a projection, then the `.*` could imply
1563                            // a wildcard expansion. For example:
1564                            // `SELECT STRUCT('foo').* FROM T`
1565                            self.prev_token(); // .
1566                        }
1567
1568                        break;
1569                    }
1570                    Token::SingleQuotedString(s) => {
1571                        let expr =
1572                            Expr::Identifier(Ident::with_quote_and_span('\'', next_token.span, s));
1573                        chain.push(AccessExpr::Dot(expr));
1574                        self.advance_token(); // The consumed string
1575                    }
1576                    // Fallback to parsing an arbitrary expression.
1577                    _ => match self.parse_subexpr(self.dialect.prec_value(Precedence::Period))? {
1578                        // If we get back a compound field access or identifier,
1579                        // we flatten the nested expression.
1580                        // For example if the current root is `foo`
1581                        // and we get back a compound identifier expression `bar.baz`
1582                        // The full expression should be `foo.bar.baz` (i.e.
1583                        // a root with an access chain with 2 entries) and not
1584                        // `foo.(bar.baz)` (i.e. a root with an access chain with
1585                        // 1 entry`).
1586                        Expr::CompoundFieldAccess { root, access_chain } => {
1587                            chain.push(AccessExpr::Dot(*root));
1588                            chain.extend(access_chain);
1589                        }
1590                        Expr::CompoundIdentifier(parts) => chain
1591                            .extend(parts.into_iter().map(Expr::Identifier).map(AccessExpr::Dot)),
1592                        expr => {
1593                            chain.push(AccessExpr::Dot(expr));
1594                        }
1595                    },
1596                }
1597            } else if !self.dialect.supports_partiql()
1598                && self.peek_token_ref().token == Token::LBracket
1599            {
1600                self.parse_multi_dim_subscript(&mut chain)?;
1601            } else {
1602                break;
1603            }
1604        }
1605
1606        let tok_index = self.get_current_index();
1607        if let Some(wildcard_token) = ending_wildcard {
1608            if !Self::is_all_ident(&root, &chain) {
1609                return self.expected("an identifier or a '*' after '.'", self.peek_token());
1610            };
1611            Ok(Expr::QualifiedWildcard(
1612                ObjectName::from(Self::exprs_to_idents(root, chain)?),
1613                AttachedToken(wildcard_token),
1614            ))
1615        } else if self.maybe_parse_outer_join_operator() {
1616            if !Self::is_all_ident(&root, &chain) {
1617                return self.expected_at("column identifier before (+)", tok_index);
1618            };
1619            let expr = if chain.is_empty() {
1620                root
1621            } else {
1622                Expr::CompoundIdentifier(Self::exprs_to_idents(root, chain)?)
1623            };
1624            Ok(Expr::OuterJoin(expr.into()))
1625        } else {
1626            Self::build_compound_expr(root, chain)
1627        }
1628    }
1629
1630    /// Combines a root expression and access chain to form
1631    /// a compound expression. Which may be a [Expr::CompoundFieldAccess]
1632    /// or other special cased expressions like [Expr::CompoundIdentifier],
1633    /// [Expr::OuterJoin].
1634    fn build_compound_expr(
1635        root: Expr,
1636        mut access_chain: Vec<AccessExpr>,
1637    ) -> Result<Expr, ParserError> {
1638        if access_chain.is_empty() {
1639            return Ok(root);
1640        }
1641
1642        if Self::is_all_ident(&root, &access_chain) {
1643            return Ok(Expr::CompoundIdentifier(Self::exprs_to_idents(
1644                root,
1645                access_chain,
1646            )?));
1647        }
1648
1649        // Flatten qualified function calls.
1650        // For example, the expression `a.b.c.foo(1,2,3)` should
1651        // represent a function called `a.b.c.foo`, rather than
1652        // a composite expression.
1653        if matches!(root, Expr::Identifier(_))
1654            && matches!(
1655                access_chain.last(),
1656                Some(AccessExpr::Dot(Expr::Function(_)))
1657            )
1658            && access_chain
1659                .iter()
1660                .rev()
1661                .skip(1) // All except the Function
1662                .all(|access| matches!(access, AccessExpr::Dot(Expr::Identifier(_))))
1663        {
1664            let Some(AccessExpr::Dot(Expr::Function(mut func))) = access_chain.pop() else {
1665                return parser_err!("expected function expression", root.span().start);
1666            };
1667
1668            let compound_func_name = [root]
1669                .into_iter()
1670                .chain(access_chain.into_iter().flat_map(|access| match access {
1671                    AccessExpr::Dot(expr) => Some(expr),
1672                    _ => None,
1673                }))
1674                .flat_map(|expr| match expr {
1675                    Expr::Identifier(ident) => Some(ident),
1676                    _ => None,
1677                })
1678                .map(ObjectNamePart::Identifier)
1679                .chain(func.name.0)
1680                .collect::<Vec<_>>();
1681            func.name = ObjectName(compound_func_name);
1682
1683            return Ok(Expr::Function(func));
1684        }
1685
1686        // Flatten qualified outer join expressions.
1687        // For example, the expression `T.foo(+)` should
1688        // represent an outer join on the column name `T.foo`
1689        // rather than a composite expression.
1690        if access_chain.len() == 1
1691            && matches!(
1692                access_chain.last(),
1693                Some(AccessExpr::Dot(Expr::OuterJoin(_)))
1694            )
1695        {
1696            let Some(AccessExpr::Dot(Expr::OuterJoin(inner_expr))) = access_chain.pop() else {
1697                return parser_err!("expected (+) expression", root.span().start);
1698            };
1699
1700            if !Self::is_all_ident(&root, &[]) {
1701                return parser_err!("column identifier before (+)", root.span().start);
1702            };
1703
1704            let token_start = root.span().start;
1705            let mut idents = Self::exprs_to_idents(root, vec![])?;
1706            match *inner_expr {
1707                Expr::CompoundIdentifier(suffix) => idents.extend(suffix),
1708                Expr::Identifier(suffix) => idents.push(suffix),
1709                _ => {
1710                    return parser_err!("column identifier before (+)", token_start);
1711                }
1712            }
1713
1714            return Ok(Expr::OuterJoin(Expr::CompoundIdentifier(idents).into()));
1715        }
1716
1717        Ok(Expr::CompoundFieldAccess {
1718            root: Box::new(root),
1719            access_chain,
1720        })
1721    }
1722
1723    /// Check if the root is an identifier and all fields are identifiers.
1724    fn is_all_ident(root: &Expr, fields: &[AccessExpr]) -> bool {
1725        if !matches!(root, Expr::Identifier(_)) {
1726            return false;
1727        }
1728        fields
1729            .iter()
1730            .all(|x| matches!(x, AccessExpr::Dot(Expr::Identifier(_))))
1731    }
1732
1733    /// Convert a root and a list of fields to a list of identifiers.
1734    fn exprs_to_idents(root: Expr, fields: Vec<AccessExpr>) -> Result<Vec<Ident>, ParserError> {
1735        let mut idents = vec![];
1736        if let Expr::Identifier(root) = root {
1737            idents.push(root);
1738            for x in fields {
1739                if let AccessExpr::Dot(Expr::Identifier(ident)) = x {
1740                    idents.push(ident);
1741                } else {
1742                    return parser_err!(
1743                        format!("Expected identifier, found: {}", x),
1744                        x.span().start
1745                    );
1746                }
1747            }
1748            Ok(idents)
1749        } else {
1750            parser_err!(
1751                format!("Expected identifier, found: {}", root),
1752                root.span().start
1753            )
1754        }
1755    }
1756
1757    /// Returns true if the next tokens indicate the outer join operator `(+)`.
1758    fn peek_outer_join_operator(&mut self) -> bool {
1759        if !self.dialect.supports_outer_join_operator() {
1760            return false;
1761        }
1762
1763        let [maybe_lparen, maybe_plus, maybe_rparen] = self.peek_tokens_ref();
1764        Token::LParen == maybe_lparen.token
1765            && Token::Plus == maybe_plus.token
1766            && Token::RParen == maybe_rparen.token
1767    }
1768
1769    /// If the next tokens indicates the outer join operator `(+)`, consume
1770    /// the tokens and return true.
1771    fn maybe_parse_outer_join_operator(&mut self) -> bool {
1772        self.dialect.supports_outer_join_operator()
1773            && self.consume_tokens(&[Token::LParen, Token::Plus, Token::RParen])
1774    }
1775
1776    pub fn parse_utility_options(&mut self) -> Result<Vec<UtilityOption>, ParserError> {
1777        self.expect_token(&Token::LParen)?;
1778        let options = self.parse_comma_separated(Self::parse_utility_option)?;
1779        self.expect_token(&Token::RParen)?;
1780
1781        Ok(options)
1782    }
1783
1784    fn parse_utility_option(&mut self) -> Result<UtilityOption, ParserError> {
1785        let name = self.parse_identifier()?;
1786
1787        let next_token = self.peek_token();
1788        if next_token == Token::Comma || next_token == Token::RParen {
1789            return Ok(UtilityOption { name, arg: None });
1790        }
1791        let arg = self.parse_expr()?;
1792
1793        Ok(UtilityOption {
1794            name,
1795            arg: Some(arg),
1796        })
1797    }
1798
1799    fn try_parse_expr_sub_query(&mut self) -> Result<Option<Expr>, ParserError> {
1800        if !self.peek_sub_query() {
1801            return Ok(None);
1802        }
1803
1804        Ok(Some(Expr::Subquery(self.parse_query()?)))
1805    }
1806
1807    fn try_parse_lambda(&mut self) -> Result<Option<Expr>, ParserError> {
1808        if !self.dialect.supports_lambda_functions() {
1809            return Ok(None);
1810        }
1811        self.maybe_parse(|p| {
1812            let params = p.parse_comma_separated(|p| p.parse_identifier())?;
1813            p.expect_token(&Token::RParen)?;
1814            p.expect_token(&Token::Arrow)?;
1815            let expr = p.parse_expr()?;
1816            Ok(Expr::Lambda(LambdaFunction {
1817                params: OneOrManyWithParens::Many(params),
1818                body: Box::new(expr),
1819            }))
1820        })
1821    }
1822
1823    /// Parses DuckDB's Python-style lambda syntax, with the leading `LAMBDA`
1824    /// keyword already consumed: `x: expr` (one param) or `x, y: expr` (several),
1825    /// neither form parenthesized. Produces the same `Expr::Lambda(LambdaFunction)`
1826    /// node the arrow forms do (`params: OneOrManyWithParens::One` for a single
1827    /// param, `::Many` for several) — there is no dedicated AST node for "was
1828    /// written with a colon", so `Display` always renders the arrow form and a
1829    /// colon-form parse canonicalizes to it on round-trip.
1830    fn parse_lambda_colon_expr(&mut self) -> Result<Expr, ParserError> {
1831        let mut params = self.parse_comma_separated(|p| p.parse_identifier())?;
1832        self.expect_token(&Token::Colon)?;
1833        let body = Box::new(self.parse_expr()?);
1834        let params = if params.len() == 1 {
1835            OneOrManyWithParens::One(params.remove(0))
1836        } else {
1837            OneOrManyWithParens::Many(params)
1838        };
1839        Ok(Expr::Lambda(LambdaFunction { params, body }))
1840    }
1841
1842    /// Tries to parse the body of an [ODBC function] call.
1843    /// i.e. without the enclosing braces
1844    ///
1845    /// ```sql
1846    /// fn myfunc(1,2,3)
1847    /// ```
1848    ///
1849    /// [ODBC function]: https://learn.microsoft.com/en-us/sql/odbc/reference/develop-app/scalar-function-calls?view=sql-server-2017
1850    fn maybe_parse_odbc_fn_body(&mut self) -> Result<Option<Expr>, ParserError> {
1851        self.maybe_parse(|p| {
1852            p.expect_keyword(Keyword::FN)?;
1853            let fn_name = p.parse_object_name(false)?;
1854            let mut fn_call = p.parse_function_call(fn_name)?;
1855            fn_call.uses_odbc_syntax = true;
1856            Ok(Expr::Function(fn_call))
1857        })
1858    }
1859
1860    pub fn parse_function(&mut self, name: ObjectName) -> Result<Expr, ParserError> {
1861        self.parse_function_call(name).map(Expr::Function)
1862    }
1863
1864    fn parse_function_call(&mut self, name: ObjectName) -> Result<Function, ParserError> {
1865        self.expect_token(&Token::LParen)?;
1866
1867        // Snowflake permits a subquery to be passed as an argument without
1868        // an enclosing set of parens if it's the only argument.
1869        if dialect_of!(self is SnowflakeDialect) && self.peek_sub_query() {
1870            let subquery = self.parse_query()?;
1871            self.expect_token(&Token::RParen)?;
1872            return Ok(Function {
1873                name,
1874                uses_odbc_syntax: false,
1875                parameters: FunctionArguments::None,
1876                args: FunctionArguments::Subquery(subquery),
1877                filter: None,
1878                null_treatment: None,
1879                over: None,
1880                within_group: vec![],
1881            });
1882        }
1883
1884        let mut args = self.parse_function_argument_list()?;
1885        let mut parameters = FunctionArguments::None;
1886        // ClickHouse aggregations support parametric functions like `HISTOGRAM(0.5, 0.6)(x, y)`
1887        // which (0.5, 0.6) is a parameter to the function.
1888        if dialect_of!(self is ClickHouseDialect | GenericDialect)
1889            && self.consume_token(&Token::LParen)
1890        {
1891            parameters = FunctionArguments::List(args);
1892            args = self.parse_function_argument_list()?;
1893        }
1894
1895        let within_group = if self.parse_keywords(&[Keyword::WITHIN, Keyword::GROUP]) {
1896            self.expect_token(&Token::LParen)?;
1897            self.expect_keywords(&[Keyword::ORDER, Keyword::BY])?;
1898            let order_by = self.parse_comma_separated(Parser::parse_order_by_expr)?;
1899            self.expect_token(&Token::RParen)?;
1900            order_by
1901        } else {
1902            vec![]
1903        };
1904
1905        let filter = if self.dialect.supports_filter_during_aggregation()
1906            && self.parse_keyword(Keyword::FILTER)
1907            && self.consume_token(&Token::LParen)
1908            && self.parse_keyword(Keyword::WHERE)
1909        {
1910            let filter = Some(Box::new(self.parse_expr()?));
1911            self.expect_token(&Token::RParen)?;
1912            filter
1913        } else {
1914            None
1915        };
1916
1917        // Syntax for null treatment shows up either in the args list
1918        // or after the function call, but not both.
1919        let null_treatment = if args
1920            .clauses
1921            .iter()
1922            .all(|clause| !matches!(clause, FunctionArgumentClause::IgnoreOrRespectNulls(_)))
1923        {
1924            self.parse_null_treatment()?
1925        } else {
1926            None
1927        };
1928
1929        let over = if self.parse_keyword(Keyword::OVER) {
1930            if self.consume_token(&Token::LParen) {
1931                let window_spec = self.parse_window_spec()?;
1932                Some(WindowType::WindowSpec(window_spec))
1933            } else {
1934                Some(WindowType::NamedWindow(self.parse_identifier()?))
1935            }
1936        } else {
1937            None
1938        };
1939
1940        Ok(Function {
1941            name,
1942            uses_odbc_syntax: false,
1943            parameters,
1944            args: FunctionArguments::List(args),
1945            null_treatment,
1946            filter,
1947            over,
1948            within_group,
1949        })
1950    }
1951
1952    /// Optionally parses a null treatment clause.
1953    fn parse_null_treatment(&mut self) -> Result<Option<NullTreatment>, ParserError> {
1954        match self.parse_one_of_keywords(&[Keyword::RESPECT, Keyword::IGNORE]) {
1955            Some(keyword) => {
1956                self.expect_keyword_is(Keyword::NULLS)?;
1957
1958                Ok(match keyword {
1959                    Keyword::RESPECT => Some(NullTreatment::RespectNulls),
1960                    Keyword::IGNORE => Some(NullTreatment::IgnoreNulls),
1961                    _ => None,
1962                })
1963            }
1964            None => Ok(None),
1965        }
1966    }
1967
1968    pub fn parse_time_functions(&mut self, name: ObjectName) -> Result<Expr, ParserError> {
1969        let args = if self.consume_token(&Token::LParen) {
1970            FunctionArguments::List(self.parse_function_argument_list()?)
1971        } else {
1972            FunctionArguments::None
1973        };
1974        Ok(Expr::Function(Function {
1975            name,
1976            uses_odbc_syntax: false,
1977            parameters: FunctionArguments::None,
1978            args,
1979            filter: None,
1980            over: None,
1981            null_treatment: None,
1982            within_group: vec![],
1983        }))
1984    }
1985
1986    pub fn parse_window_frame_units(&mut self) -> Result<WindowFrameUnits, ParserError> {
1987        let next_token = self.next_token();
1988        match &next_token.token {
1989            Token::Word(w) => match w.keyword {
1990                Keyword::ROWS => Ok(WindowFrameUnits::Rows),
1991                Keyword::RANGE => Ok(WindowFrameUnits::Range),
1992                Keyword::GROUPS => Ok(WindowFrameUnits::Groups),
1993                _ => self.expected("ROWS, RANGE, GROUPS", next_token)?,
1994            },
1995            _ => self.expected("ROWS, RANGE, GROUPS", next_token),
1996        }
1997    }
1998
1999    pub fn parse_window_frame(&mut self) -> Result<WindowFrame, ParserError> {
2000        let units = self.parse_window_frame_units()?;
2001        let (start_bound, end_bound) = if self.parse_keyword(Keyword::BETWEEN) {
2002            let start_bound = self.parse_window_frame_bound()?;
2003            self.expect_keyword_is(Keyword::AND)?;
2004            let end_bound = Some(self.parse_window_frame_bound()?);
2005            (start_bound, end_bound)
2006        } else {
2007            (self.parse_window_frame_bound()?, None)
2008        };
2009        Ok(WindowFrame {
2010            units,
2011            start_bound,
2012            end_bound,
2013        })
2014    }
2015
2016    /// Parse `CURRENT ROW` or `{ <positive number> | UNBOUNDED } { PRECEDING | FOLLOWING }`
2017    pub fn parse_window_frame_bound(&mut self) -> Result<WindowFrameBound, ParserError> {
2018        if self.parse_keywords(&[Keyword::CURRENT, Keyword::ROW]) {
2019            Ok(WindowFrameBound::CurrentRow)
2020        } else {
2021            let rows = if self.parse_keyword(Keyword::UNBOUNDED) {
2022                None
2023            } else {
2024                Some(Box::new(match self.peek_token().token {
2025                    Token::SingleQuotedString(_) => self.parse_interval()?,
2026                    _ => self.parse_expr()?,
2027                }))
2028            };
2029            if self.parse_keyword(Keyword::PRECEDING) {
2030                Ok(WindowFrameBound::Preceding(rows))
2031            } else if self.parse_keyword(Keyword::FOLLOWING) {
2032                Ok(WindowFrameBound::Following(rows))
2033            } else {
2034                self.expected("PRECEDING or FOLLOWING", self.peek_token())
2035            }
2036        }
2037    }
2038
2039    /// Parse a group by expr. Group by expr can be one of group sets, roll up, cube, or simple expr.
2040    fn parse_group_by_expr(&mut self) -> Result<Expr, ParserError> {
2041        if self.dialect.supports_group_by_expr() {
2042            if self.parse_keywords(&[Keyword::GROUPING, Keyword::SETS]) {
2043                self.expect_token(&Token::LParen)?;
2044                let result = self.parse_comma_separated(|p| p.parse_tuple(false, true))?;
2045                self.expect_token(&Token::RParen)?;
2046                Ok(Expr::GroupingSets(result))
2047            } else if self.parse_keyword(Keyword::CUBE) {
2048                self.expect_token(&Token::LParen)?;
2049                let result = self.parse_comma_separated(|p| p.parse_tuple(true, true))?;
2050                self.expect_token(&Token::RParen)?;
2051                Ok(Expr::Cube(result))
2052            } else if self.parse_keyword(Keyword::ROLLUP) {
2053                self.expect_token(&Token::LParen)?;
2054                let result = self.parse_comma_separated(|p| p.parse_tuple(true, true))?;
2055                self.expect_token(&Token::RParen)?;
2056                Ok(Expr::Rollup(result))
2057            } else if self.consume_tokens(&[Token::LParen, Token::RParen]) {
2058                // PostgreSQL allow to use empty tuple as a group by expression,
2059                // e.g. `GROUP BY (), name`. Please refer to GROUP BY Clause section in
2060                // [PostgreSQL](https://www.postgresql.org/docs/16/sql-select.html)
2061                Ok(Expr::Tuple(vec![]))
2062            } else {
2063                self.parse_expr()
2064            }
2065        } else {
2066            // TODO parse rollup for other dialects
2067            self.parse_expr()
2068        }
2069    }
2070
2071    /// Parse a tuple with `(` and `)`.
2072    /// If `lift_singleton` is true, then a singleton tuple is lifted to a tuple of length 1, otherwise it will fail.
2073    /// If `allow_empty` is true, then an empty tuple is allowed.
2074    fn parse_tuple(
2075        &mut self,
2076        lift_singleton: bool,
2077        allow_empty: bool,
2078    ) -> Result<Vec<Expr>, ParserError> {
2079        if lift_singleton {
2080            if self.consume_token(&Token::LParen) {
2081                let result = if allow_empty && self.consume_token(&Token::RParen) {
2082                    vec![]
2083                } else {
2084                    let result = self.parse_comma_separated(Parser::parse_expr)?;
2085                    self.expect_token(&Token::RParen)?;
2086                    result
2087                };
2088                Ok(result)
2089            } else {
2090                Ok(vec![self.parse_expr()?])
2091            }
2092        } else {
2093            self.expect_token(&Token::LParen)?;
2094            let result = if allow_empty && self.consume_token(&Token::RParen) {
2095                vec![]
2096            } else {
2097                let result = self.parse_comma_separated(Parser::parse_expr)?;
2098                self.expect_token(&Token::RParen)?;
2099                result
2100            };
2101            Ok(result)
2102        }
2103    }
2104
2105    pub fn parse_case_expr(&mut self) -> Result<Expr, ParserError> {
2106        let mut operand = None;
2107        if !self.parse_keyword(Keyword::WHEN) {
2108            operand = Some(Box::new(self.parse_expr()?));
2109            self.expect_keyword_is(Keyword::WHEN)?;
2110        }
2111        let mut conditions = vec![];
2112        loop {
2113            let condition = self.parse_expr()?;
2114            self.expect_keyword_is(Keyword::THEN)?;
2115            let result = self.parse_expr()?;
2116            conditions.push(CaseWhen { condition, result });
2117            if !self.parse_keyword(Keyword::WHEN) {
2118                break;
2119            }
2120        }
2121        let else_result = if self.parse_keyword(Keyword::ELSE) {
2122            Some(Box::new(self.parse_expr()?))
2123        } else {
2124            None
2125        };
2126        self.expect_keyword_is(Keyword::END)?;
2127        Ok(Expr::Case {
2128            operand,
2129            conditions,
2130            else_result,
2131        })
2132    }
2133
2134    pub fn parse_optional_cast_format(&mut self) -> Result<Option<CastFormat>, ParserError> {
2135        if self.parse_keyword(Keyword::FORMAT) {
2136            let value = self.parse_value()?.value;
2137            match self.parse_optional_time_zone()? {
2138                Some(tz) => Ok(Some(CastFormat::ValueAtTimeZone(value, tz))),
2139                None => Ok(Some(CastFormat::Value(value))),
2140            }
2141        } else {
2142            Ok(None)
2143        }
2144    }
2145
2146    pub fn parse_optional_time_zone(&mut self) -> Result<Option<Value>, ParserError> {
2147        if self.parse_keywords(&[Keyword::AT, Keyword::TIME, Keyword::ZONE]) {
2148            self.parse_value().map(|v| Some(v.value))
2149        } else {
2150            Ok(None)
2151        }
2152    }
2153
2154    /// mssql-like convert function
2155    fn parse_mssql_convert(&mut self, is_try: bool) -> Result<Expr, ParserError> {
2156        self.expect_token(&Token::LParen)?;
2157        let data_type = self.parse_data_type()?;
2158        self.expect_token(&Token::Comma)?;
2159        let expr = self.parse_expr()?;
2160        let styles = if self.consume_token(&Token::Comma) {
2161            self.parse_comma_separated(Parser::parse_expr)?
2162        } else {
2163            Default::default()
2164        };
2165        self.expect_token(&Token::RParen)?;
2166        Ok(Expr::Convert {
2167            is_try,
2168            expr: Box::new(expr),
2169            data_type: Some(data_type),
2170            charset: None,
2171            target_before_value: true,
2172            styles,
2173        })
2174    }
2175
2176    /// Parse a SQL CONVERT function:
2177    ///  - `CONVERT('héhé' USING utf8mb4)` (MySQL)
2178    ///  - `CONVERT('héhé', CHAR CHARACTER SET utf8mb4)` (MySQL)
2179    ///  - `CONVERT(DECIMAL(10, 5), 42)` (MSSQL) - the type comes first
2180    pub fn parse_convert_expr(&mut self, is_try: bool) -> Result<Expr, ParserError> {
2181        if self.dialect.convert_type_before_value() {
2182            return self.parse_mssql_convert(is_try);
2183        }
2184        self.expect_token(&Token::LParen)?;
2185        let expr = self.parse_expr()?;
2186        if self.parse_keyword(Keyword::USING) {
2187            let charset = self.parse_object_name(false)?;
2188            self.expect_token(&Token::RParen)?;
2189            return Ok(Expr::Convert {
2190                is_try,
2191                expr: Box::new(expr),
2192                data_type: None,
2193                charset: Some(charset),
2194                target_before_value: false,
2195                styles: vec![],
2196            });
2197        }
2198        self.expect_token(&Token::Comma)?;
2199        let data_type = self.parse_data_type()?;
2200        let charset = if self.parse_keywords(&[Keyword::CHARACTER, Keyword::SET]) {
2201            Some(self.parse_object_name(false)?)
2202        } else {
2203            None
2204        };
2205        self.expect_token(&Token::RParen)?;
2206        Ok(Expr::Convert {
2207            is_try,
2208            expr: Box::new(expr),
2209            data_type: Some(data_type),
2210            charset,
2211            target_before_value: false,
2212            styles: vec![],
2213        })
2214    }
2215
2216    /// Parse a SQL CAST function e.g. `CAST(expr AS FLOAT)`
2217    pub fn parse_cast_expr(&mut self, kind: CastKind) -> Result<Expr, ParserError> {
2218        self.expect_token(&Token::LParen)?;
2219        let expr = self.parse_expr()?;
2220        self.expect_keyword_is(Keyword::AS)?;
2221        let data_type = self.parse_data_type()?;
2222        let format = self.parse_optional_cast_format()?;
2223        self.expect_token(&Token::RParen)?;
2224        Ok(Expr::Cast {
2225            kind,
2226            expr: Box::new(expr),
2227            data_type,
2228            format,
2229        })
2230    }
2231
2232    /// Parse a SQL EXISTS expression e.g. `WHERE EXISTS(SELECT ...)`.
2233    pub fn parse_exists_expr(&mut self, negated: bool) -> Result<Expr, ParserError> {
2234        self.expect_token(&Token::LParen)?;
2235        let exists_node = Expr::Exists {
2236            negated,
2237            subquery: self.parse_query()?,
2238        };
2239        self.expect_token(&Token::RParen)?;
2240        Ok(exists_node)
2241    }
2242
2243    pub fn parse_extract_expr(&mut self) -> Result<Expr, ParserError> {
2244        self.expect_token(&Token::LParen)?;
2245        let field = self.parse_date_time_field()?;
2246
2247        let syntax = if self.parse_keyword(Keyword::FROM) {
2248            ExtractSyntax::From
2249        } else if self.consume_token(&Token::Comma)
2250            && dialect_of!(self is SnowflakeDialect | GenericDialect)
2251        {
2252            ExtractSyntax::Comma
2253        } else {
2254            return Err(ParserError::ParserError(
2255                "Expected 'FROM' or ','".to_string(),
2256            ));
2257        };
2258
2259        let expr = self.parse_expr()?;
2260        self.expect_token(&Token::RParen)?;
2261        Ok(Expr::Extract {
2262            field,
2263            expr: Box::new(expr),
2264            syntax,
2265        })
2266    }
2267
2268    pub fn parse_ceil_floor_expr(&mut self, is_ceil: bool) -> Result<Expr, ParserError> {
2269        self.expect_token(&Token::LParen)?;
2270        let expr = self.parse_expr()?;
2271        // Parse `CEIL/FLOOR(expr)`
2272        let field = if self.parse_keyword(Keyword::TO) {
2273            // Parse `CEIL/FLOOR(expr TO DateTimeField)`
2274            CeilFloorKind::DateTimeField(self.parse_date_time_field()?)
2275        } else if self.consume_token(&Token::Comma) {
2276            // Parse `CEIL/FLOOR(expr, scale)`
2277            match self.parse_value()?.value {
2278                Value::Number(n, s) => CeilFloorKind::Scale(Value::Number(n, s)),
2279                _ => {
2280                    return Err(ParserError::ParserError(
2281                        "Scale field can only be of number type".to_string(),
2282                    ))
2283                }
2284            }
2285        } else {
2286            CeilFloorKind::DateTimeField(DateTimeField::NoDateTime)
2287        };
2288        self.expect_token(&Token::RParen)?;
2289        if is_ceil {
2290            Ok(Expr::Ceil {
2291                expr: Box::new(expr),
2292                field,
2293            })
2294        } else {
2295            Ok(Expr::Floor {
2296                expr: Box::new(expr),
2297                field,
2298            })
2299        }
2300    }
2301
2302    pub fn parse_position_expr(&mut self, ident: Ident) -> Result<Expr, ParserError> {
2303        let between_prec = self.dialect.prec_value(Precedence::Between);
2304        let position_expr = self.maybe_parse(|p| {
2305            // PARSE SELECT POSITION('@' in field)
2306            p.expect_token(&Token::LParen)?;
2307
2308            // Parse the subexpr till the IN keyword
2309            let expr = p.parse_subexpr(between_prec)?;
2310            p.expect_keyword_is(Keyword::IN)?;
2311            let from = p.parse_expr()?;
2312            p.expect_token(&Token::RParen)?;
2313            Ok(Expr::Position {
2314                expr: Box::new(expr),
2315                r#in: Box::new(from),
2316            })
2317        })?;
2318        match position_expr {
2319            Some(expr) => Ok(expr),
2320            // Snowflake supports `position` as an ordinary function call
2321            // without the special `IN` syntax.
2322            None => self.parse_function(ObjectName::from(vec![ident])),
2323        }
2324    }
2325
2326    pub fn parse_substring_expr(&mut self) -> Result<Expr, ParserError> {
2327        // PARSE SUBSTRING (EXPR [FROM 1] [FOR 3])
2328        self.expect_token(&Token::LParen)?;
2329        let expr = self.parse_expr()?;
2330        let mut from_expr = None;
2331        let special = self.consume_token(&Token::Comma);
2332        if special || self.parse_keyword(Keyword::FROM) {
2333            from_expr = Some(self.parse_expr()?);
2334        }
2335
2336        let mut to_expr = None;
2337        if self.parse_keyword(Keyword::FOR) || self.consume_token(&Token::Comma) {
2338            to_expr = Some(self.parse_expr()?);
2339        }
2340        self.expect_token(&Token::RParen)?;
2341
2342        Ok(Expr::Substring {
2343            expr: Box::new(expr),
2344            substring_from: from_expr.map(Box::new),
2345            substring_for: to_expr.map(Box::new),
2346            special,
2347        })
2348    }
2349
2350    pub fn parse_overlay_expr(&mut self) -> Result<Expr, ParserError> {
2351        // PARSE OVERLAY (EXPR PLACING EXPR FROM 1 [FOR 3])
2352        self.expect_token(&Token::LParen)?;
2353        let expr = self.parse_expr()?;
2354        self.expect_keyword_is(Keyword::PLACING)?;
2355        let what_expr = self.parse_expr()?;
2356        self.expect_keyword_is(Keyword::FROM)?;
2357        let from_expr = self.parse_expr()?;
2358        let mut for_expr = None;
2359        if self.parse_keyword(Keyword::FOR) {
2360            for_expr = Some(self.parse_expr()?);
2361        }
2362        self.expect_token(&Token::RParen)?;
2363
2364        Ok(Expr::Overlay {
2365            expr: Box::new(expr),
2366            overlay_what: Box::new(what_expr),
2367            overlay_from: Box::new(from_expr),
2368            overlay_for: for_expr.map(Box::new),
2369        })
2370    }
2371
2372    /// ```sql
2373    /// TRIM ([WHERE] ['text' FROM] 'text')
2374    /// TRIM ('text')
2375    /// TRIM(<expr>, [, characters]) -- only Snowflake or BigQuery
2376    /// ```
2377    pub fn parse_trim_expr(&mut self) -> Result<Expr, ParserError> {
2378        self.expect_token(&Token::LParen)?;
2379        let mut trim_where = None;
2380        if let Token::Word(word) = self.peek_token().token {
2381            if [Keyword::BOTH, Keyword::LEADING, Keyword::TRAILING]
2382                .iter()
2383                .any(|d| word.keyword == *d)
2384            {
2385                trim_where = Some(self.parse_trim_where()?);
2386            }
2387        }
2388        let expr = self.parse_expr()?;
2389        if self.parse_keyword(Keyword::FROM) {
2390            let trim_what = Box::new(expr);
2391            let expr = self.parse_expr()?;
2392            self.expect_token(&Token::RParen)?;
2393            Ok(Expr::Trim {
2394                expr: Box::new(expr),
2395                trim_where,
2396                trim_what: Some(trim_what),
2397                trim_characters: None,
2398            })
2399        } else if self.consume_token(&Token::Comma)
2400            && dialect_of!(self is SnowflakeDialect | BigQueryDialect | GenericDialect)
2401        {
2402            let characters = self.parse_comma_separated(Parser::parse_expr)?;
2403            self.expect_token(&Token::RParen)?;
2404            Ok(Expr::Trim {
2405                expr: Box::new(expr),
2406                trim_where: None,
2407                trim_what: None,
2408                trim_characters: Some(characters),
2409            })
2410        } else {
2411            self.expect_token(&Token::RParen)?;
2412            Ok(Expr::Trim {
2413                expr: Box::new(expr),
2414                trim_where,
2415                trim_what: None,
2416                trim_characters: None,
2417            })
2418        }
2419    }
2420
2421    pub fn parse_trim_where(&mut self) -> Result<TrimWhereField, ParserError> {
2422        let next_token = self.next_token();
2423        match &next_token.token {
2424            Token::Word(w) => match w.keyword {
2425                Keyword::BOTH => Ok(TrimWhereField::Both),
2426                Keyword::LEADING => Ok(TrimWhereField::Leading),
2427                Keyword::TRAILING => Ok(TrimWhereField::Trailing),
2428                _ => self.expected("trim_where field", next_token)?,
2429            },
2430            _ => self.expected("trim_where field", next_token),
2431        }
2432    }
2433
2434    /// Parses an array expression `[ex1, ex2, ..]`
2435    /// if `named` is `true`, came from an expression like  `ARRAY[ex1, ex2]`
2436    pub fn parse_array_expr(&mut self, named: bool) -> Result<Expr, ParserError> {
2437        let exprs = self.parse_comma_separated0(Parser::parse_expr, Token::RBracket)?;
2438        self.expect_token(&Token::RBracket)?;
2439        Ok(Expr::Array(Array { elem: exprs, named }))
2440    }
2441
2442    pub fn parse_listagg_on_overflow(&mut self) -> Result<Option<ListAggOnOverflow>, ParserError> {
2443        if self.parse_keywords(&[Keyword::ON, Keyword::OVERFLOW]) {
2444            if self.parse_keyword(Keyword::ERROR) {
2445                Ok(Some(ListAggOnOverflow::Error))
2446            } else {
2447                self.expect_keyword_is(Keyword::TRUNCATE)?;
2448                let filler = match self.peek_token().token {
2449                    Token::Word(w)
2450                        if w.keyword == Keyword::WITH || w.keyword == Keyword::WITHOUT =>
2451                    {
2452                        None
2453                    }
2454                    Token::SingleQuotedString(_)
2455                    | Token::EscapedStringLiteral(_)
2456                    | Token::UnicodeStringLiteral(_)
2457                    | Token::NationalStringLiteral(_)
2458                    | Token::HexStringLiteral(_) => Some(Box::new(self.parse_expr()?)),
2459                    _ => self.expected(
2460                        "either filler, WITH, or WITHOUT in LISTAGG",
2461                        self.peek_token(),
2462                    )?,
2463                };
2464                let with_count = self.parse_keyword(Keyword::WITH);
2465                if !with_count && !self.parse_keyword(Keyword::WITHOUT) {
2466                    self.expected("either WITH or WITHOUT in LISTAGG", self.peek_token())?;
2467                }
2468                self.expect_keyword_is(Keyword::COUNT)?;
2469                Ok(Some(ListAggOnOverflow::Truncate { filler, with_count }))
2470            }
2471        } else {
2472            Ok(None)
2473        }
2474    }
2475
2476    // This function parses date/time fields for the EXTRACT function-like
2477    // operator, interval qualifiers, and the ceil/floor operations.
2478    // EXTRACT supports a wider set of date/time fields than interval qualifiers,
2479    // so this function may need to be split in two.
2480    pub fn parse_date_time_field(&mut self) -> Result<DateTimeField, ParserError> {
2481        let next_token = self.next_token();
2482        match &next_token.token {
2483            Token::Word(w) => match w.keyword {
2484                Keyword::YEAR => Ok(DateTimeField::Year),
2485                Keyword::YEARS => Ok(DateTimeField::Years),
2486                Keyword::MONTH => Ok(DateTimeField::Month),
2487                Keyword::MONTHS => Ok(DateTimeField::Months),
2488                Keyword::WEEK => {
2489                    let week_day = if dialect_of!(self is BigQueryDialect | GenericDialect)
2490                        && self.consume_token(&Token::LParen)
2491                    {
2492                        let week_day = self.parse_identifier()?;
2493                        self.expect_token(&Token::RParen)?;
2494                        Some(week_day)
2495                    } else {
2496                        None
2497                    };
2498                    Ok(DateTimeField::Week(week_day))
2499                }
2500                Keyword::WEEKS => Ok(DateTimeField::Weeks),
2501                Keyword::DAY => Ok(DateTimeField::Day),
2502                Keyword::DAYOFWEEK => Ok(DateTimeField::DayOfWeek),
2503                Keyword::DAYOFYEAR => Ok(DateTimeField::DayOfYear),
2504                Keyword::DAYS => Ok(DateTimeField::Days),
2505                Keyword::DATE => Ok(DateTimeField::Date),
2506                Keyword::DATETIME => Ok(DateTimeField::Datetime),
2507                Keyword::HOUR => Ok(DateTimeField::Hour),
2508                Keyword::HOURS => Ok(DateTimeField::Hours),
2509                Keyword::MINUTE => Ok(DateTimeField::Minute),
2510                Keyword::MINUTES => Ok(DateTimeField::Minutes),
2511                Keyword::SECOND => Ok(DateTimeField::Second),
2512                Keyword::SECONDS => Ok(DateTimeField::Seconds),
2513                Keyword::CENTURY => Ok(DateTimeField::Century),
2514                Keyword::DECADE => Ok(DateTimeField::Decade),
2515                Keyword::DOY => Ok(DateTimeField::Doy),
2516                Keyword::DOW => Ok(DateTimeField::Dow),
2517                Keyword::EPOCH => Ok(DateTimeField::Epoch),
2518                Keyword::ISODOW => Ok(DateTimeField::Isodow),
2519                Keyword::ISOYEAR => Ok(DateTimeField::Isoyear),
2520                Keyword::ISOWEEK => Ok(DateTimeField::IsoWeek),
2521                Keyword::JULIAN => Ok(DateTimeField::Julian),
2522                Keyword::MICROSECOND => Ok(DateTimeField::Microsecond),
2523                Keyword::MICROSECONDS => Ok(DateTimeField::Microseconds),
2524                Keyword::MILLENIUM => Ok(DateTimeField::Millenium),
2525                Keyword::MILLENNIUM => Ok(DateTimeField::Millennium),
2526                Keyword::MILLISECOND => Ok(DateTimeField::Millisecond),
2527                Keyword::MILLISECONDS => Ok(DateTimeField::Milliseconds),
2528                Keyword::NANOSECOND => Ok(DateTimeField::Nanosecond),
2529                Keyword::NANOSECONDS => Ok(DateTimeField::Nanoseconds),
2530                Keyword::QUARTER => Ok(DateTimeField::Quarter),
2531                Keyword::TIME => Ok(DateTimeField::Time),
2532                Keyword::TIMEZONE => Ok(DateTimeField::Timezone),
2533                Keyword::TIMEZONE_ABBR => Ok(DateTimeField::TimezoneAbbr),
2534                Keyword::TIMEZONE_HOUR => Ok(DateTimeField::TimezoneHour),
2535                Keyword::TIMEZONE_MINUTE => Ok(DateTimeField::TimezoneMinute),
2536                Keyword::TIMEZONE_REGION => Ok(DateTimeField::TimezoneRegion),
2537                _ if self.dialect.allow_extract_custom() => {
2538                    self.prev_token();
2539                    let custom = self.parse_identifier()?;
2540                    Ok(DateTimeField::Custom(custom))
2541                }
2542                _ => self.expected("date/time field", next_token),
2543            },
2544            Token::SingleQuotedString(_) if self.dialect.allow_extract_single_quotes() => {
2545                self.prev_token();
2546                let custom = self.parse_identifier()?;
2547                Ok(DateTimeField::Custom(custom))
2548            }
2549            _ => self.expected("date/time field", next_token),
2550        }
2551    }
2552
2553    pub fn parse_not(&mut self) -> Result<Expr, ParserError> {
2554        match self.peek_token().token {
2555            Token::Word(w) => match w.keyword {
2556                Keyword::EXISTS => {
2557                    let negated = true;
2558                    let _ = self.parse_keyword(Keyword::EXISTS);
2559                    self.parse_exists_expr(negated)
2560                }
2561                _ => Ok(Expr::UnaryOp {
2562                    op: UnaryOperator::Not,
2563                    expr: Box::new(
2564                        self.parse_subexpr(self.dialect.prec_value(Precedence::UnaryNot))?,
2565                    ),
2566                }),
2567            },
2568            _ => Ok(Expr::UnaryOp {
2569                op: UnaryOperator::Not,
2570                expr: Box::new(self.parse_subexpr(self.dialect.prec_value(Precedence::UnaryNot))?),
2571            }),
2572        }
2573    }
2574
2575    /// Parse expression types that start with a left brace '{'.
2576    /// Examples:
2577    /// ```sql
2578    /// -- Dictionary expr.
2579    /// {'key1': 'value1', 'key2': 'value2'}
2580    ///
2581    /// -- Function call using the ODBC syntax.
2582    /// { fn CONCAT('foo', 'bar') }
2583    /// ```
2584    fn parse_lbrace_expr(&mut self) -> Result<Expr, ParserError> {
2585        let token = self.expect_token(&Token::LBrace)?;
2586
2587        if let Some(fn_expr) = self.maybe_parse_odbc_fn_body()? {
2588            self.expect_token(&Token::RBrace)?;
2589            return Ok(fn_expr);
2590        }
2591
2592        if self.dialect.supports_dictionary_syntax() {
2593            self.prev_token(); // Put back the '{'
2594            return self.parse_duckdb_struct_literal();
2595        }
2596
2597        self.expected("an expression", token)
2598    }
2599
2600    /// Parses fulltext expressions [`sqlparser::ast::Expr::MatchAgainst`]
2601    ///
2602    /// # Errors
2603    /// This method will raise an error if the column list is empty or with invalid identifiers,
2604    /// the match expression is not a literal string, or if the search modifier is not valid.
2605    pub fn parse_match_against(&mut self) -> Result<Expr, ParserError> {
2606        let columns = self.parse_parenthesized_column_list(Mandatory, false)?;
2607
2608        self.expect_keyword_is(Keyword::AGAINST)?;
2609
2610        self.expect_token(&Token::LParen)?;
2611
2612        // MySQL is too permissive about the value, IMO we can't validate it perfectly on syntax level.
2613        let match_value = self.parse_value()?.value;
2614
2615        let in_natural_language_mode_keywords = &[
2616            Keyword::IN,
2617            Keyword::NATURAL,
2618            Keyword::LANGUAGE,
2619            Keyword::MODE,
2620        ];
2621
2622        let with_query_expansion_keywords = &[Keyword::WITH, Keyword::QUERY, Keyword::EXPANSION];
2623
2624        let in_boolean_mode_keywords = &[Keyword::IN, Keyword::BOOLEAN, Keyword::MODE];
2625
2626        let opt_search_modifier = if self.parse_keywords(in_natural_language_mode_keywords) {
2627            if self.parse_keywords(with_query_expansion_keywords) {
2628                Some(SearchModifier::InNaturalLanguageModeWithQueryExpansion)
2629            } else {
2630                Some(SearchModifier::InNaturalLanguageMode)
2631            }
2632        } else if self.parse_keywords(in_boolean_mode_keywords) {
2633            Some(SearchModifier::InBooleanMode)
2634        } else if self.parse_keywords(with_query_expansion_keywords) {
2635            Some(SearchModifier::WithQueryExpansion)
2636        } else {
2637            None
2638        };
2639
2640        self.expect_token(&Token::RParen)?;
2641
2642        Ok(Expr::MatchAgainst {
2643            columns,
2644            match_value,
2645            opt_search_modifier,
2646        })
2647    }
2648
2649    /// Parse an `INTERVAL` expression.
2650    ///
2651    /// Some syntactically valid intervals:
2652    ///
2653    /// ```sql
2654    ///   1. INTERVAL '1' DAY
2655    ///   2. INTERVAL '1-1' YEAR TO MONTH
2656    ///   3. INTERVAL '1' SECOND
2657    ///   4. INTERVAL '1:1:1.1' HOUR (5) TO SECOND (5)
2658    ///   5. INTERVAL '1.1' SECOND (2, 2)
2659    ///   6. INTERVAL '1:1' HOUR (5) TO MINUTE (5)
2660    ///   7. (MySql & BigQuery only): INTERVAL 1 DAY
2661    /// ```
2662    ///
2663    /// Note that we do not currently attempt to parse the quoted value.
2664    pub fn parse_interval(&mut self) -> Result<Expr, ParserError> {
2665        // The SQL standard allows an optional sign before the value string, but
2666        // it is not clear if any implementations support that syntax, so we
2667        // don't currently try to parse it. (The sign can instead be included
2668        // inside the value string.)
2669
2670        // to match the different flavours of INTERVAL syntax, we only allow expressions
2671        // if the dialect requires an interval qualifier,
2672        // see https://github.com/sqlparser-rs/sqlparser-rs/pull/1398 for more details
2673        let value = if self.dialect.require_interval_qualifier() {
2674            // parse a whole expression so `INTERVAL 1 + 1 DAY` is valid
2675            self.parse_expr()?
2676        } else {
2677            // parse a prefix expression so `INTERVAL 1 DAY` is valid, but `INTERVAL 1 + 1 DAY` is not
2678            // this also means that `INTERVAL '5 days' > INTERVAL '1 day'` treated properly
2679            self.parse_prefix()?
2680        };
2681
2682        // Following the string literal is a qualifier which indicates the units
2683        // of the duration specified in the string literal.
2684        //
2685        // Note that PostgreSQL allows omitting the qualifier, so we provide
2686        // this more general implementation.
2687        let leading_field = if self.next_token_is_temporal_unit() {
2688            Some(self.parse_date_time_field()?)
2689        } else if self.dialect.require_interval_qualifier() {
2690            return parser_err!(
2691                "INTERVAL requires a unit after the literal value",
2692                self.peek_token().span.start
2693            );
2694        } else {
2695            None
2696        };
2697
2698        let (leading_precision, last_field, fsec_precision) =
2699            if leading_field == Some(DateTimeField::Second) {
2700                // SQL mandates special syntax for `SECOND TO SECOND` literals.
2701                // Instead of
2702                //     `SECOND [(<leading precision>)] TO SECOND[(<fractional seconds precision>)]`
2703                // one must use the special format:
2704                //     `SECOND [( <leading precision> [ , <fractional seconds precision>] )]`
2705                let last_field = None;
2706                let (leading_precision, fsec_precision) = self.parse_optional_precision_scale()?;
2707                (leading_precision, last_field, fsec_precision)
2708            } else {
2709                let leading_precision = self.parse_optional_precision()?;
2710                if self.parse_keyword(Keyword::TO) {
2711                    let last_field = Some(self.parse_date_time_field()?);
2712                    let fsec_precision = if last_field == Some(DateTimeField::Second) {
2713                        self.parse_optional_precision()?
2714                    } else {
2715                        None
2716                    };
2717                    (leading_precision, last_field, fsec_precision)
2718                } else {
2719                    (leading_precision, None, None)
2720                }
2721            };
2722
2723        Ok(Expr::Interval(Interval {
2724            value: Box::new(value),
2725            leading_field,
2726            leading_precision,
2727            last_field,
2728            fractional_seconds_precision: fsec_precision,
2729        }))
2730    }
2731
2732    /// Peek at the next token and determine if it is a temporal unit
2733    /// like `second`.
2734    pub fn next_token_is_temporal_unit(&mut self) -> bool {
2735        if let Token::Word(word) = self.peek_token().token {
2736            matches!(
2737                word.keyword,
2738                Keyword::YEAR
2739                    | Keyword::YEARS
2740                    | Keyword::MONTH
2741                    | Keyword::MONTHS
2742                    | Keyword::WEEK
2743                    | Keyword::WEEKS
2744                    | Keyword::DAY
2745                    | Keyword::DAYS
2746                    | Keyword::HOUR
2747                    | Keyword::HOURS
2748                    | Keyword::MINUTE
2749                    | Keyword::MINUTES
2750                    | Keyword::SECOND
2751                    | Keyword::SECONDS
2752                    | Keyword::CENTURY
2753                    | Keyword::DECADE
2754                    | Keyword::DOW
2755                    | Keyword::DOY
2756                    | Keyword::EPOCH
2757                    | Keyword::ISODOW
2758                    | Keyword::ISOYEAR
2759                    | Keyword::JULIAN
2760                    | Keyword::MICROSECOND
2761                    | Keyword::MICROSECONDS
2762                    | Keyword::MILLENIUM
2763                    | Keyword::MILLENNIUM
2764                    | Keyword::MILLISECOND
2765                    | Keyword::MILLISECONDS
2766                    | Keyword::NANOSECOND
2767                    | Keyword::NANOSECONDS
2768                    | Keyword::QUARTER
2769                    | Keyword::TIMEZONE
2770                    | Keyword::TIMEZONE_HOUR
2771                    | Keyword::TIMEZONE_MINUTE
2772            )
2773        } else {
2774            false
2775        }
2776    }
2777
2778    /// Syntax
2779    /// ```sql
2780    /// -- typed
2781    /// STRUCT<[field_name] field_type, ...>( expr1 [, ... ])
2782    /// -- typeless
2783    /// STRUCT( expr1 [AS field_name] [, ... ])
2784    /// ```
2785    fn parse_struct_literal(&mut self) -> Result<Expr, ParserError> {
2786        // Parse the fields definition if exist `<[field_name] field_type, ...>`
2787        self.prev_token();
2788        let (fields, trailing_bracket) =
2789            self.parse_struct_type_def(Self::parse_struct_field_def)?;
2790        if trailing_bracket.0 {
2791            return parser_err!(
2792                "unmatched > in STRUCT literal",
2793                self.peek_token().span.start
2794            );
2795        }
2796
2797        // Parse the struct values `(expr1 [, ... ])`
2798        self.expect_token(&Token::LParen)?;
2799        let values = self
2800            .parse_comma_separated(|parser| parser.parse_struct_field_expr(!fields.is_empty()))?;
2801        self.expect_token(&Token::RParen)?;
2802
2803        Ok(Expr::Struct { values, fields })
2804    }
2805
2806    /// Parse an expression value for a struct literal
2807    /// Syntax
2808    /// ```sql
2809    /// expr [AS name]
2810    /// ```
2811    ///
2812    /// For biquery [1], Parameter typed_syntax is set to true if the expression
2813    /// is to be parsed as a field expression declared using typed
2814    /// struct syntax [2], and false if using typeless struct syntax [3].
2815    ///
2816    /// [1]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#constructing_a_struct
2817    /// [2]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#typed_struct_syntax
2818    /// [3]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#typeless_struct_syntax
2819    fn parse_struct_field_expr(&mut self, typed_syntax: bool) -> Result<Expr, ParserError> {
2820        let expr = self.parse_expr()?;
2821        if self.parse_keyword(Keyword::AS) {
2822            if typed_syntax {
2823                return parser_err!("Typed syntax does not allow AS", {
2824                    self.prev_token();
2825                    self.peek_token().span.start
2826                });
2827            }
2828            let field_name = self.parse_identifier()?;
2829            Ok(Expr::Named {
2830                expr: expr.into(),
2831                name: field_name,
2832            })
2833        } else {
2834            Ok(expr)
2835        }
2836    }
2837
2838    /// Parse a Struct type definition as a sequence of field-value pairs.
2839    /// The syntax of the Struct elem differs by dialect so it is customised
2840    /// by the `elem_parser` argument.
2841    ///
2842    /// Syntax
2843    /// ```sql
2844    /// Hive:
2845    /// STRUCT<field_name: field_type>
2846    ///
2847    /// BigQuery:
2848    /// STRUCT<[field_name] field_type>
2849    /// ```
2850    fn parse_struct_type_def<F>(
2851        &mut self,
2852        mut elem_parser: F,
2853    ) -> Result<(Vec<StructField>, MatchedTrailingBracket), ParserError>
2854    where
2855        F: FnMut(&mut Parser<'a>) -> Result<(StructField, MatchedTrailingBracket), ParserError>,
2856    {
2857        let start_token = self.peek_token();
2858        self.expect_keyword_is(Keyword::STRUCT)?;
2859
2860        // Nothing to do if we have no type information.
2861        if Token::Lt != self.peek_token() {
2862            return Ok((Default::default(), false.into()));
2863        }
2864        self.next_token();
2865
2866        let mut field_defs = vec![];
2867        let trailing_bracket = loop {
2868            let (def, trailing_bracket) = elem_parser(self)?;
2869            field_defs.push(def);
2870            if !self.consume_token(&Token::Comma) {
2871                break trailing_bracket;
2872            }
2873
2874            // Angle brackets are balanced so we only expect the trailing `>>` after
2875            // we've matched all field types for the current struct.
2876            // e.g. this is invalid syntax `STRUCT<STRUCT<INT>>>, INT>(NULL)`
2877            if trailing_bracket.0 {
2878                return parser_err!("unmatched > in STRUCT definition", start_token.span.start);
2879            }
2880        };
2881
2882        Ok((
2883            field_defs,
2884            self.expect_closing_angle_bracket(trailing_bracket)?,
2885        ))
2886    }
2887
2888    /// Duckdb Struct Data Type <https://duckdb.org/docs/sql/data_types/struct.html#retrieving-from-structs>
2889    fn parse_duckdb_struct_type_def(&mut self) -> Result<Vec<StructField>, ParserError> {
2890        self.expect_keyword_is(Keyword::STRUCT)?;
2891        self.expect_token(&Token::LParen)?;
2892        let struct_body = self.parse_comma_separated(|parser| {
2893            let field_name = parser.parse_identifier()?;
2894            let field_type = parser.parse_data_type()?;
2895
2896            Ok(StructField {
2897                field_name: Some(field_name),
2898                field_type,
2899            })
2900        });
2901        self.expect_token(&Token::RParen)?;
2902        struct_body
2903    }
2904
2905    /// Parse a field definition in a [struct] or [tuple].
2906    /// Syntax:
2907    ///
2908    /// ```sql
2909    /// [field_name] field_type
2910    /// ```
2911    ///
2912    /// [struct]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#declaring_a_struct_type
2913    /// [tuple]: https://clickhouse.com/docs/en/sql-reference/data-types/tuple
2914    fn parse_struct_field_def(
2915        &mut self,
2916    ) -> Result<(StructField, MatchedTrailingBracket), ParserError> {
2917        // Look beyond the next item to infer whether both field name
2918        // and type are specified.
2919        let is_anonymous_field = !matches!(
2920            (self.peek_nth_token(0).token, self.peek_nth_token(1).token),
2921            (Token::Word(_), Token::Word(_))
2922        );
2923
2924        let field_name = if is_anonymous_field {
2925            None
2926        } else {
2927            Some(self.parse_identifier()?)
2928        };
2929
2930        let (field_type, trailing_bracket) = self.parse_data_type_helper()?;
2931
2932        Ok((
2933            StructField {
2934                field_name,
2935                field_type,
2936            },
2937            trailing_bracket,
2938        ))
2939    }
2940
2941    /// DuckDB specific: Parse a Union type definition as a sequence of field-value pairs.
2942    ///
2943    /// Syntax:
2944    ///
2945    /// ```sql
2946    /// UNION(field_name field_type[,...])
2947    /// ```
2948    ///
2949    /// [1]: https://duckdb.org/docs/sql/data_types/union.html
2950    fn parse_union_type_def(&mut self) -> Result<Vec<UnionField>, ParserError> {
2951        self.expect_keyword_is(Keyword::UNION)?;
2952
2953        self.expect_token(&Token::LParen)?;
2954
2955        let fields = self.parse_comma_separated(|p| {
2956            Ok(UnionField {
2957                field_name: p.parse_identifier()?,
2958                field_type: p.parse_data_type()?,
2959            })
2960        })?;
2961
2962        self.expect_token(&Token::RParen)?;
2963
2964        Ok(fields)
2965    }
2966
2967    /// DuckDB specific: Parse a duckdb [dictionary]
2968    ///
2969    /// Syntax:
2970    ///
2971    /// ```sql
2972    /// {'field_name': expr1[, ... ]}
2973    /// ```
2974    ///
2975    /// [dictionary]: https://duckdb.org/docs/sql/data_types/struct#creating-structs
2976    fn parse_duckdb_struct_literal(&mut self) -> Result<Expr, ParserError> {
2977        self.expect_token(&Token::LBrace)?;
2978
2979        let fields =
2980            self.parse_comma_separated0(Self::parse_duckdb_dictionary_field, Token::RBrace)?;
2981
2982        self.expect_token(&Token::RBrace)?;
2983
2984        Ok(Expr::Dictionary(fields))
2985    }
2986
2987    /// Parse a field for a duckdb [dictionary]
2988    ///
2989    /// Syntax
2990    ///
2991    /// ```sql
2992    /// 'name': expr
2993    /// ```
2994    ///
2995    /// [dictionary]: https://duckdb.org/docs/sql/data_types/struct#creating-structs
2996    fn parse_duckdb_dictionary_field(&mut self) -> Result<DictionaryField, ParserError> {
2997        let key = self.parse_identifier()?;
2998
2999        self.expect_token(&Token::Colon)?;
3000
3001        let expr = self.parse_expr()?;
3002
3003        Ok(DictionaryField {
3004            key,
3005            value: Box::new(expr),
3006        })
3007    }
3008
3009    /// DuckDB specific: Parse a duckdb [map]
3010    ///
3011    /// Syntax:
3012    ///
3013    /// ```sql
3014    /// Map {key1: value1[, ... ]}
3015    /// ```
3016    ///
3017    /// [map]: https://duckdb.org/docs/sql/data_types/map.html#creating-maps
3018    fn parse_duckdb_map_literal(&mut self) -> Result<Expr, ParserError> {
3019        self.expect_token(&Token::LBrace)?;
3020        let fields = self.parse_comma_separated0(Self::parse_duckdb_map_field, Token::RBrace)?;
3021        self.expect_token(&Token::RBrace)?;
3022        Ok(Expr::Map(Map { entries: fields }))
3023    }
3024
3025    /// Parse a field for a duckdb [map]
3026    ///
3027    /// Syntax
3028    ///
3029    /// ```sql
3030    /// key: value
3031    /// ```
3032    ///
3033    /// [map]: https://duckdb.org/docs/sql/data_types/map.html#creating-maps
3034    fn parse_duckdb_map_field(&mut self) -> Result<MapEntry, ParserError> {
3035        let key = self.parse_expr()?;
3036
3037        self.expect_token(&Token::Colon)?;
3038
3039        let value = self.parse_expr()?;
3040
3041        Ok(MapEntry {
3042            key: Box::new(key),
3043            value: Box::new(value),
3044        })
3045    }
3046
3047    /// Parse clickhouse [map]
3048    ///
3049    /// Syntax
3050    ///
3051    /// ```sql
3052    /// Map(key_data_type, value_data_type)
3053    /// ```
3054    ///
3055    /// [map]: https://clickhouse.com/docs/en/sql-reference/data-types/map
3056    fn parse_click_house_map_def(&mut self) -> Result<(DataType, DataType), ParserError> {
3057        self.expect_keyword_is(Keyword::MAP)?;
3058        self.expect_token(&Token::LParen)?;
3059        let key_data_type = self.parse_data_type()?;
3060        self.expect_token(&Token::Comma)?;
3061        let value_data_type = self.parse_data_type()?;
3062        self.expect_token(&Token::RParen)?;
3063
3064        Ok((key_data_type, value_data_type))
3065    }
3066
3067    /// Parse clickhouse [tuple]
3068    ///
3069    /// Syntax
3070    ///
3071    /// ```sql
3072    /// Tuple([field_name] field_type, ...)
3073    /// ```
3074    ///
3075    /// [tuple]: https://clickhouse.com/docs/en/sql-reference/data-types/tuple
3076    fn parse_click_house_tuple_def(&mut self) -> Result<Vec<StructField>, ParserError> {
3077        self.expect_keyword_is(Keyword::TUPLE)?;
3078        self.expect_token(&Token::LParen)?;
3079        let mut field_defs = vec![];
3080        loop {
3081            let (def, _) = self.parse_struct_field_def()?;
3082            field_defs.push(def);
3083            if !self.consume_token(&Token::Comma) {
3084                break;
3085            }
3086        }
3087        self.expect_token(&Token::RParen)?;
3088
3089        Ok(field_defs)
3090    }
3091
3092    /// For nested types that use the angle bracket syntax, this matches either
3093    /// `>`, `>>` or nothing depending on which variant is expected (specified by the previously
3094    /// matched `trailing_bracket` argument). It returns whether there is a trailing
3095    /// left to be matched - (i.e. if '>>' was matched).
3096    fn expect_closing_angle_bracket(
3097        &mut self,
3098        trailing_bracket: MatchedTrailingBracket,
3099    ) -> Result<MatchedTrailingBracket, ParserError> {
3100        let trailing_bracket = if !trailing_bracket.0 {
3101            match self.peek_token().token {
3102                Token::Gt => {
3103                    self.next_token();
3104                    false.into()
3105                }
3106                Token::ShiftRight => {
3107                    self.next_token();
3108                    true.into()
3109                }
3110                _ => return self.expected(">", self.peek_token()),
3111            }
3112        } else {
3113            false.into()
3114        };
3115
3116        Ok(trailing_bracket)
3117    }
3118
3119    /// Parse an operator following an expression
3120    pub fn parse_infix(&mut self, expr: Expr, precedence: u8) -> Result<Expr, ParserError> {
3121        // allow the dialect to override infix parsing
3122        if let Some(infix) = self.dialect.parse_infix(self, &expr, precedence) {
3123            return infix;
3124        }
3125
3126        let dialect = self.dialect;
3127
3128        self.advance_token();
3129        let tok = self.get_current_token();
3130        let tok_index = self.get_current_index();
3131        let span = tok.span;
3132        let regular_binary_operator = match &tok.token {
3133            Token::Spaceship => Some(BinaryOperator::Spaceship),
3134            Token::DoubleEq => Some(BinaryOperator::Eq),
3135            Token::Eq => Some(BinaryOperator::Eq),
3136            Token::Neq => Some(BinaryOperator::NotEq),
3137            Token::Gt => Some(BinaryOperator::Gt),
3138            Token::GtEq => Some(BinaryOperator::GtEq),
3139            Token::Lt => Some(BinaryOperator::Lt),
3140            Token::LtEq => Some(BinaryOperator::LtEq),
3141            Token::Plus => Some(BinaryOperator::Plus),
3142            Token::Minus => Some(BinaryOperator::Minus),
3143            Token::Mul => Some(BinaryOperator::Multiply),
3144            Token::Mod => Some(BinaryOperator::Modulo),
3145            Token::StringConcat => Some(BinaryOperator::StringConcat),
3146            Token::Pipe => Some(BinaryOperator::BitwiseOr),
3147            Token::Caret => {
3148                // In PostgreSQL, ^ stands for the exponentiation operation,
3149                // and # stands for XOR. See https://www.postgresql.org/docs/current/functions-math.html
3150                if dialect_is!(dialect is PostgreSqlDialect) {
3151                    Some(BinaryOperator::PGExp)
3152                } else {
3153                    Some(BinaryOperator::BitwiseXor)
3154                }
3155            }
3156            Token::Ampersand => Some(BinaryOperator::BitwiseAnd),
3157            Token::Div => Some(BinaryOperator::Divide),
3158            Token::DuckIntDiv if dialect_is!(dialect is DuckDbDialect | GenericDialect) => {
3159                Some(BinaryOperator::DuckIntegerDivide)
3160            }
3161            Token::ShiftLeft if dialect_is!(dialect is PostgreSqlDialect | DuckDbDialect | GenericDialect | RedshiftSqlDialect) => {
3162                Some(BinaryOperator::PGBitwiseShiftLeft)
3163            }
3164            Token::ShiftRight if dialect_is!(dialect is PostgreSqlDialect | DuckDbDialect | GenericDialect | RedshiftSqlDialect) => {
3165                Some(BinaryOperator::PGBitwiseShiftRight)
3166            }
3167            Token::Sharp if dialect_is!(dialect is PostgreSqlDialect | RedshiftSqlDialect) => {
3168                Some(BinaryOperator::PGBitwiseXor)
3169            }
3170            Token::Overlap if dialect_is!(dialect is PostgreSqlDialect | RedshiftSqlDialect) => {
3171                Some(BinaryOperator::PGOverlap)
3172            }
3173            Token::Overlap if dialect_is!(dialect is PostgreSqlDialect | GenericDialect) => {
3174                Some(BinaryOperator::PGOverlap)
3175            }
3176            Token::CaretAt if dialect_is!(dialect is PostgreSqlDialect | GenericDialect) => {
3177                Some(BinaryOperator::PGStartsWith)
3178            }
3179            Token::Tilde => Some(BinaryOperator::PGRegexMatch),
3180            Token::TildeAsterisk => Some(BinaryOperator::PGRegexIMatch),
3181            Token::ExclamationMarkTilde => Some(BinaryOperator::PGRegexNotMatch),
3182            Token::ExclamationMarkTildeAsterisk => Some(BinaryOperator::PGRegexNotIMatch),
3183            Token::DoubleTilde => Some(BinaryOperator::PGLikeMatch),
3184            Token::DoubleTildeAsterisk => Some(BinaryOperator::PGILikeMatch),
3185            Token::ExclamationMarkDoubleTilde => Some(BinaryOperator::PGNotLikeMatch),
3186            Token::ExclamationMarkDoubleTildeAsterisk => Some(BinaryOperator::PGNotILikeMatch),
3187            Token::Arrow => Some(BinaryOperator::Arrow),
3188            Token::LongArrow => Some(BinaryOperator::LongArrow),
3189            Token::HashArrow => Some(BinaryOperator::HashArrow),
3190            Token::HashLongArrow => Some(BinaryOperator::HashLongArrow),
3191            Token::AtArrow => Some(BinaryOperator::AtArrow),
3192            Token::ArrowAt => Some(BinaryOperator::ArrowAt),
3193            Token::HashMinus => Some(BinaryOperator::HashMinus),
3194            Token::AtQuestion => Some(BinaryOperator::AtQuestion),
3195            Token::AtAt => Some(BinaryOperator::AtAt),
3196            Token::Question => Some(BinaryOperator::Question),
3197            Token::QuestionAnd => Some(BinaryOperator::QuestionAnd),
3198            Token::QuestionPipe => Some(BinaryOperator::QuestionPipe),
3199            Token::CustomBinaryOperator(s) => Some(BinaryOperator::Custom(s.clone())),
3200            Token::DoubleSharp if self.dialect.supports_geometric_types() => {
3201                Some(BinaryOperator::DoubleHash)
3202            }
3203
3204            Token::AmpersandLeftAngleBracket if self.dialect.supports_geometric_types() => {
3205                Some(BinaryOperator::AndLt)
3206            }
3207            Token::AmpersandRightAngleBracket if self.dialect.supports_geometric_types() => {
3208                Some(BinaryOperator::AndGt)
3209            }
3210            Token::QuestionMarkDash if self.dialect.supports_geometric_types() => {
3211                Some(BinaryOperator::QuestionDash)
3212            }
3213            Token::AmpersandLeftAngleBracketVerticalBar
3214                if self.dialect.supports_geometric_types() =>
3215            {
3216                Some(BinaryOperator::AndLtPipe)
3217            }
3218            Token::VerticalBarAmpersandRightAngleBracket
3219                if self.dialect.supports_geometric_types() =>
3220            {
3221                Some(BinaryOperator::PipeAndGt)
3222            }
3223            Token::TwoWayArrow if self.dialect.supports_geometric_types() => {
3224                Some(BinaryOperator::LtDashGt)
3225            }
3226            Token::LeftAngleBracketCaret if self.dialect.supports_geometric_types() => {
3227                Some(BinaryOperator::LtCaret)
3228            }
3229            Token::RightAngleBracketCaret if self.dialect.supports_geometric_types() => {
3230                Some(BinaryOperator::GtCaret)
3231            }
3232            Token::QuestionMarkSharp if self.dialect.supports_geometric_types() => {
3233                Some(BinaryOperator::QuestionHash)
3234            }
3235            Token::QuestionMarkDoubleVerticalBar if self.dialect.supports_geometric_types() => {
3236                Some(BinaryOperator::QuestionDoublePipe)
3237            }
3238            Token::QuestionMarkDashVerticalBar if self.dialect.supports_geometric_types() => {
3239                Some(BinaryOperator::QuestionDashPipe)
3240            }
3241            Token::TildeEqual if self.dialect.supports_geometric_types() => {
3242                Some(BinaryOperator::TildeEq)
3243            }
3244            Token::ShiftLeftVerticalBar if self.dialect.supports_geometric_types() => {
3245                Some(BinaryOperator::LtLtPipe)
3246            }
3247            Token::VerticalBarShiftRight if self.dialect.supports_geometric_types() => {
3248                Some(BinaryOperator::PipeGtGt)
3249            }
3250            Token::AtSign if self.dialect.supports_geometric_types() => Some(BinaryOperator::At),
3251
3252            Token::Word(w) => match w.keyword {
3253                Keyword::AND => Some(BinaryOperator::And),
3254                Keyword::OR => Some(BinaryOperator::Or),
3255                Keyword::XOR => Some(BinaryOperator::Xor),
3256                Keyword::OVERLAPS => Some(BinaryOperator::Overlaps),
3257                Keyword::OPERATOR if dialect_is!(dialect is PostgreSqlDialect | GenericDialect) => {
3258                    self.expect_token(&Token::LParen)?;
3259                    // there are special rules for operator names in
3260                    // postgres so we can not use 'parse_object'
3261                    // or similar.
3262                    // See https://www.postgresql.org/docs/current/sql-createoperator.html
3263                    let mut idents = vec![];
3264                    loop {
3265                        self.advance_token();
3266                        idents.push(self.get_current_token().to_string());
3267                        if !self.consume_token(&Token::Period) {
3268                            break;
3269                        }
3270                    }
3271                    self.expect_token(&Token::RParen)?;
3272                    Some(BinaryOperator::PGCustomBinaryOperator(idents))
3273                }
3274                _ => None,
3275            },
3276            _ => None,
3277        };
3278
3279        let tok = self.token_at(tok_index);
3280        if let Some(op) = regular_binary_operator {
3281            if let Some(keyword) =
3282                self.parse_one_of_keywords(&[Keyword::ANY, Keyword::ALL, Keyword::SOME])
3283            {
3284                self.expect_token(&Token::LParen)?;
3285                let right = if self.peek_sub_query() {
3286                    // We have a subquery ahead (SELECT\WITH ...) need to rewind and
3287                    // use the parenthesis for parsing the subquery as an expression.
3288                    self.prev_token(); // LParen
3289                    self.parse_subexpr(precedence)?
3290                } else {
3291                    // Non-subquery expression
3292                    let right = self.parse_subexpr(precedence)?;
3293                    self.expect_token(&Token::RParen)?;
3294                    right
3295                };
3296
3297                if !matches!(
3298                    op,
3299                    BinaryOperator::Gt
3300                        | BinaryOperator::Lt
3301                        | BinaryOperator::GtEq
3302                        | BinaryOperator::LtEq
3303                        | BinaryOperator::Eq
3304                        | BinaryOperator::NotEq
3305                ) {
3306                    return parser_err!(
3307                        format!(
3308                        "Expected one of [=, >, <, =>, =<, !=] as comparison operator, found: {op}"
3309                    ),
3310                        span.start
3311                    );
3312                };
3313
3314                Ok(match keyword {
3315                    Keyword::ALL => Expr::AllOp {
3316                        left: Box::new(expr),
3317                        compare_op: op,
3318                        right: Box::new(right),
3319                    },
3320                    Keyword::ANY | Keyword::SOME => Expr::AnyOp {
3321                        left: Box::new(expr),
3322                        compare_op: op,
3323                        right: Box::new(right),
3324                        is_some: keyword == Keyword::SOME,
3325                    },
3326                    _ => unreachable!(),
3327                })
3328            } else {
3329                Ok(Expr::BinaryOp {
3330                    left: Box::new(expr),
3331                    op,
3332                    right: Box::new(self.parse_subexpr(precedence)?),
3333                })
3334            }
3335        } else if let Token::Word(w) = &tok.token {
3336            match w.keyword {
3337                Keyword::IS => {
3338                    if self.parse_keyword(Keyword::NULL) {
3339                        Ok(Expr::IsNull(Box::new(expr)))
3340                    } else if self.parse_keywords(&[Keyword::NOT, Keyword::NULL]) {
3341                        Ok(Expr::IsNotNull(Box::new(expr)))
3342                    } else if self.parse_keywords(&[Keyword::TRUE]) {
3343                        Ok(Expr::IsTrue(Box::new(expr)))
3344                    } else if self.parse_keywords(&[Keyword::NOT, Keyword::TRUE]) {
3345                        Ok(Expr::IsNotTrue(Box::new(expr)))
3346                    } else if self.parse_keywords(&[Keyword::FALSE]) {
3347                        Ok(Expr::IsFalse(Box::new(expr)))
3348                    } else if self.parse_keywords(&[Keyword::NOT, Keyword::FALSE]) {
3349                        Ok(Expr::IsNotFalse(Box::new(expr)))
3350                    } else if self.parse_keywords(&[Keyword::UNKNOWN]) {
3351                        Ok(Expr::IsUnknown(Box::new(expr)))
3352                    } else if self.parse_keywords(&[Keyword::NOT, Keyword::UNKNOWN]) {
3353                        Ok(Expr::IsNotUnknown(Box::new(expr)))
3354                    } else if self.parse_keywords(&[Keyword::DISTINCT, Keyword::FROM]) {
3355                        let expr2 = self.parse_expr()?;
3356                        Ok(Expr::IsDistinctFrom(Box::new(expr), Box::new(expr2)))
3357                    } else if self.parse_keywords(&[Keyword::NOT, Keyword::DISTINCT, Keyword::FROM])
3358                    {
3359                        let expr2 = self.parse_expr()?;
3360                        Ok(Expr::IsNotDistinctFrom(Box::new(expr), Box::new(expr2)))
3361                    } else if let Ok(is_normalized) = self.parse_unicode_is_normalized(expr) {
3362                        Ok(is_normalized)
3363                    } else {
3364                        self.expected(
3365                            "[NOT] NULL | TRUE | FALSE | DISTINCT | [form] NORMALIZED FROM after IS",
3366                            self.peek_token(),
3367                        )
3368                    }
3369                }
3370                Keyword::AT => {
3371                    self.expect_keywords(&[Keyword::TIME, Keyword::ZONE])?;
3372                    Ok(Expr::AtTimeZone {
3373                        timestamp: Box::new(expr),
3374                        time_zone: Box::new(self.parse_subexpr(precedence)?),
3375                    })
3376                }
3377                Keyword::NOT
3378                | Keyword::IN
3379                | Keyword::BETWEEN
3380                | Keyword::LIKE
3381                | Keyword::ILIKE
3382                | Keyword::SIMILAR
3383                | Keyword::REGEXP
3384                | Keyword::RLIKE => {
3385                    self.prev_token();
3386                    let negated = self.parse_keyword(Keyword::NOT);
3387                    let regexp = self.parse_keyword(Keyword::REGEXP);
3388                    let rlike = self.parse_keyword(Keyword::RLIKE);
3389                    if regexp || rlike {
3390                        Ok(Expr::RLike {
3391                            negated,
3392                            expr: Box::new(expr),
3393                            pattern: Box::new(
3394                                self.parse_subexpr(self.dialect.prec_value(Precedence::Like))?,
3395                            ),
3396                            regexp,
3397                        })
3398                    } else if self.parse_keyword(Keyword::IN) {
3399                        self.parse_in(expr, negated)
3400                    } else if self.parse_keyword(Keyword::BETWEEN) {
3401                        self.parse_between(expr, negated)
3402                    } else if self.parse_keyword(Keyword::LIKE) {
3403                        Ok(Expr::Like {
3404                            negated,
3405                            any: self.parse_keyword(Keyword::ANY),
3406                            expr: Box::new(expr),
3407                            pattern: Box::new(
3408                                self.parse_subexpr(self.dialect.prec_value(Precedence::Like))?,
3409                            ),
3410                            escape_char: self.parse_escape_char()?,
3411                        })
3412                    } else if self.parse_keyword(Keyword::ILIKE) {
3413                        Ok(Expr::ILike {
3414                            negated,
3415                            any: self.parse_keyword(Keyword::ANY),
3416                            expr: Box::new(expr),
3417                            pattern: Box::new(
3418                                self.parse_subexpr(self.dialect.prec_value(Precedence::Like))?,
3419                            ),
3420                            escape_char: self.parse_escape_char()?,
3421                        })
3422                    } else if self.parse_keywords(&[Keyword::SIMILAR, Keyword::TO]) {
3423                        Ok(Expr::SimilarTo {
3424                            negated,
3425                            expr: Box::new(expr),
3426                            pattern: Box::new(
3427                                self.parse_subexpr(self.dialect.prec_value(Precedence::Like))?,
3428                            ),
3429                            escape_char: self.parse_escape_char()?,
3430                        })
3431                    } else {
3432                        self.expected("IN or BETWEEN after NOT", self.peek_token())
3433                    }
3434                }
3435                // Can only happen if `get_next_precedence` got out of sync with this function
3436                _ => parser_err!(
3437                    format!("No infix parser for token {:?}", tok.token),
3438                    tok.span.start
3439                ),
3440            }
3441        } else if Token::DoubleColon == *tok {
3442            Ok(Expr::Cast {
3443                kind: CastKind::DoubleColon,
3444                expr: Box::new(expr),
3445                data_type: self.parse_data_type()?,
3446                format: None,
3447            })
3448        } else if Token::ExclamationMark == *tok && self.dialect.supports_factorial_operator() {
3449            Ok(Expr::UnaryOp {
3450                op: UnaryOperator::PGPostfixFactorial,
3451                expr: Box::new(expr),
3452            })
3453        } else if Token::LBracket == *tok && self.dialect.supports_partiql()
3454            || (dialect_of!(self is SnowflakeDialect | GenericDialect) && Token::Colon == *tok)
3455        {
3456            self.prev_token();
3457            self.parse_json_access(expr)
3458        } else {
3459            // Can only happen if `get_next_precedence` got out of sync with this function
3460            parser_err!(
3461                format!("No infix parser for token {:?}", tok.token),
3462                tok.span.start
3463            )
3464        }
3465    }
3466
3467    /// Parse the `ESCAPE CHAR` portion of `LIKE`, `ILIKE`, and `SIMILAR TO`
3468    pub fn parse_escape_char(&mut self) -> Result<Option<String>, ParserError> {
3469        if self.parse_keyword(Keyword::ESCAPE) {
3470            Ok(Some(self.parse_literal_string()?))
3471        } else {
3472            Ok(None)
3473        }
3474    }
3475
3476    /// Parses an array subscript like
3477    /// * `[:]`
3478    /// * `[l]`
3479    /// * `[l:]`
3480    /// * `[:u]`
3481    /// * `[l:u]`
3482    /// * `[l:u:s]`
3483    ///
3484    /// Parser is right after `[`
3485    fn parse_subscript_inner(&mut self) -> Result<Subscript, ParserError> {
3486        // at either `<lower>:(rest)` or `:(rest)]`
3487        let lower_bound = if self.consume_token(&Token::Colon) {
3488            None
3489        } else {
3490            Some(self.parse_expr()?)
3491        };
3492
3493        // check for end
3494        if self.consume_token(&Token::RBracket) {
3495            if let Some(lower_bound) = lower_bound {
3496                return Ok(Subscript::Index { index: lower_bound });
3497            };
3498            return Ok(Subscript::Slice {
3499                lower_bound,
3500                upper_bound: None,
3501                stride: None,
3502            });
3503        }
3504
3505        // consume the `:`
3506        if lower_bound.is_some() {
3507            self.expect_token(&Token::Colon)?;
3508        }
3509
3510        // we are now at either `]`, `<upper>(rest)]`
3511        let upper_bound = if self.consume_token(&Token::RBracket) {
3512            return Ok(Subscript::Slice {
3513                lower_bound,
3514                upper_bound: None,
3515                stride: None,
3516            });
3517        } else {
3518            Some(self.parse_expr()?)
3519        };
3520
3521        // check for end
3522        if self.consume_token(&Token::RBracket) {
3523            return Ok(Subscript::Slice {
3524                lower_bound,
3525                upper_bound,
3526                stride: None,
3527            });
3528        }
3529
3530        // we are now at `:]` or `:stride]`
3531        self.expect_token(&Token::Colon)?;
3532        let stride = if self.consume_token(&Token::RBracket) {
3533            None
3534        } else {
3535            Some(self.parse_expr()?)
3536        };
3537
3538        if stride.is_some() {
3539            self.expect_token(&Token::RBracket)?;
3540        }
3541
3542        Ok(Subscript::Slice {
3543            lower_bound,
3544            upper_bound,
3545            stride,
3546        })
3547    }
3548
3549    /// Parse a multi-dimension array accessing like `[1:3][1][1]`
3550    pub fn parse_multi_dim_subscript(
3551        &mut self,
3552        chain: &mut Vec<AccessExpr>,
3553    ) -> Result<(), ParserError> {
3554        while self.consume_token(&Token::LBracket) {
3555            self.parse_subscript(chain)?;
3556        }
3557        Ok(())
3558    }
3559
3560    /// Parses an array subscript like `[1:3]`
3561    ///
3562    /// Parser is right after `[`
3563    fn parse_subscript(&mut self, chain: &mut Vec<AccessExpr>) -> Result<(), ParserError> {
3564        let subscript = self.parse_subscript_inner()?;
3565        chain.push(AccessExpr::Subscript(subscript));
3566        Ok(())
3567    }
3568
3569    fn parse_json_path_object_key(&mut self) -> Result<JsonPathElem, ParserError> {
3570        let token = self.next_token();
3571        match token.token {
3572            Token::Word(Word {
3573                value,
3574                // path segments in SF dot notation can be unquoted or double-quoted
3575                quote_style: quote_style @ (Some('"') | None),
3576                // some experimentation suggests that snowflake permits
3577                // any keyword here unquoted.
3578                keyword: _,
3579            }) => Ok(JsonPathElem::Dot {
3580                key: value,
3581                quoted: quote_style.is_some(),
3582            }),
3583
3584            // This token should never be generated on snowflake or generic
3585            // dialects, but we handle it just in case this is used on future
3586            // dialects.
3587            Token::DoubleQuotedString(key) => Ok(JsonPathElem::Dot { key, quoted: true }),
3588
3589            _ => self.expected("variant object key name", token),
3590        }
3591    }
3592
3593    fn parse_json_access(&mut self, expr: Expr) -> Result<Expr, ParserError> {
3594        let path = self.parse_json_path()?;
3595        Ok(Expr::JsonAccess {
3596            value: Box::new(expr),
3597            path,
3598        })
3599    }
3600
3601    fn parse_json_path(&mut self) -> Result<JsonPath, ParserError> {
3602        let mut path = Vec::new();
3603        loop {
3604            match self.next_token().token {
3605                Token::Colon if path.is_empty() => {
3606                    path.push(self.parse_json_path_object_key()?);
3607                }
3608                Token::Period if !path.is_empty() => {
3609                    path.push(self.parse_json_path_object_key()?);
3610                }
3611                Token::LBracket => {
3612                    let key = self.parse_expr()?;
3613                    self.expect_token(&Token::RBracket)?;
3614
3615                    path.push(JsonPathElem::Bracket { key });
3616                }
3617                _ => {
3618                    self.prev_token();
3619                    break;
3620                }
3621            };
3622        }
3623
3624        debug_assert!(!path.is_empty());
3625        Ok(JsonPath { path })
3626    }
3627
3628    /// Parses the parens following the `[ NOT ] IN` operator.
3629    pub fn parse_in(&mut self, expr: Expr, negated: bool) -> Result<Expr, ParserError> {
3630        // BigQuery allows `IN UNNEST(array_expression)`
3631        // https://cloud.google.com/bigquery/docs/reference/standard-sql/operators#in_operators
3632        if self.parse_keyword(Keyword::UNNEST) {
3633            self.expect_token(&Token::LParen)?;
3634            let array_expr = self.parse_expr()?;
3635            self.expect_token(&Token::RParen)?;
3636            return Ok(Expr::InUnnest {
3637                expr: Box::new(expr),
3638                array_expr: Box::new(array_expr),
3639                negated,
3640            });
3641        }
3642        self.expect_token(&Token::LParen)?;
3643        let in_op = if self.parse_keyword(Keyword::SELECT) || self.parse_keyword(Keyword::WITH) {
3644            self.prev_token();
3645            Expr::InSubquery {
3646                expr: Box::new(expr),
3647                subquery: self.parse_query()?,
3648                negated,
3649            }
3650        } else {
3651            Expr::InList {
3652                expr: Box::new(expr),
3653                list: if self.dialect.supports_in_empty_list() {
3654                    self.parse_comma_separated0(Parser::parse_expr, Token::RParen)?
3655                } else {
3656                    self.parse_comma_separated(Parser::parse_expr)?
3657                },
3658                negated,
3659            }
3660        };
3661        self.expect_token(&Token::RParen)?;
3662        Ok(in_op)
3663    }
3664
3665    /// Parses `BETWEEN <low> AND <high>`, assuming the `BETWEEN` keyword was already consumed.
3666    pub fn parse_between(&mut self, expr: Expr, negated: bool) -> Result<Expr, ParserError> {
3667        // Stop parsing subexpressions for <low> and <high> on tokens with
3668        // precedence lower than that of `BETWEEN`, such as `AND`, `IS`, etc.
3669        let low = self.parse_subexpr(self.dialect.prec_value(Precedence::Between))?;
3670        self.expect_keyword_is(Keyword::AND)?;
3671        let high = self.parse_subexpr(self.dialect.prec_value(Precedence::Between))?;
3672        Ok(Expr::Between {
3673            expr: Box::new(expr),
3674            negated,
3675            low: Box::new(low),
3676            high: Box::new(high),
3677        })
3678    }
3679
3680    /// Parse a postgresql casting style which is in the form of `expr::datatype`.
3681    pub fn parse_pg_cast(&mut self, expr: Expr) -> Result<Expr, ParserError> {
3682        Ok(Expr::Cast {
3683            kind: CastKind::DoubleColon,
3684            expr: Box::new(expr),
3685            data_type: self.parse_data_type()?,
3686            format: None,
3687        })
3688    }
3689
3690    /// Get the precedence of the next token
3691    pub fn get_next_precedence(&self) -> Result<u8, ParserError> {
3692        self.dialect.get_next_precedence_default(self)
3693    }
3694
3695    /// Return the token at the given location, or EOF if the index is beyond
3696    /// the length of the current set of tokens.
3697    pub fn token_at(&self, index: usize) -> &TokenWithSpan {
3698        self.tokens.get(index).unwrap_or(&EOF_TOKEN)
3699    }
3700
3701    /// Return the first non-whitespace token that has not yet been processed
3702    /// or Token::EOF
3703    ///
3704    /// See [`Self::peek_token_ref`] to avoid the copy.
3705    pub fn peek_token(&self) -> TokenWithSpan {
3706        self.peek_nth_token(0)
3707    }
3708
3709    /// Return a reference to the first non-whitespace token that has not yet
3710    /// been processed or Token::EOF
3711    pub fn peek_token_ref(&self) -> &TokenWithSpan {
3712        self.peek_nth_token_ref(0)
3713    }
3714
3715    /// Returns the `N` next non-whitespace tokens that have not yet been
3716    /// processed.
3717    ///
3718    /// Example:
3719    /// ```rust
3720    /// # use sqlparser::dialect::GenericDialect;
3721    /// # use sqlparser::parser::Parser;
3722    /// # use sqlparser::keywords::Keyword;
3723    /// # use sqlparser::tokenizer::{Token, Word};
3724    /// let dialect = GenericDialect {};
3725    /// let mut parser = Parser::new(&dialect).try_with_sql("ORDER BY foo, bar").unwrap();
3726    ///
3727    /// // Note that Rust infers the number of tokens to peek based on the
3728    /// // length of the slice pattern!
3729    /// assert!(matches!(
3730    ///     parser.peek_tokens(),
3731    ///     [
3732    ///         Token::Word(Word { keyword: Keyword::ORDER, .. }),
3733    ///         Token::Word(Word { keyword: Keyword::BY, .. }),
3734    ///     ]
3735    /// ));
3736    /// ```
3737    pub fn peek_tokens<const N: usize>(&self) -> [Token; N] {
3738        self.peek_tokens_with_location()
3739            .map(|with_loc| with_loc.token)
3740    }
3741
3742    /// Returns the `N` next non-whitespace tokens with locations that have not
3743    /// yet been processed.
3744    ///
3745    /// See [`Self::peek_token`] for an example.
3746    pub fn peek_tokens_with_location<const N: usize>(&self) -> [TokenWithSpan; N] {
3747        let mut index = self.index;
3748        core::array::from_fn(|_| loop {
3749            let token = self.tokens.get(index);
3750            index += 1;
3751            if let Some(TokenWithSpan {
3752                token: Token::Whitespace(_),
3753                span: _,
3754            }) = token
3755            {
3756                continue;
3757            }
3758            break token.cloned().unwrap_or(TokenWithSpan {
3759                token: Token::EOF,
3760                span: Span::empty(),
3761            });
3762        })
3763    }
3764
3765    /// Returns references to the `N` next non-whitespace tokens
3766    /// that have not yet been processed.
3767    ///
3768    /// See [`Self::peek_tokens`] for an example.
3769    pub fn peek_tokens_ref<const N: usize>(&self) -> [&TokenWithSpan; N] {
3770        let mut index = self.index;
3771        core::array::from_fn(|_| loop {
3772            let token = self.tokens.get(index);
3773            index += 1;
3774            if let Some(TokenWithSpan {
3775                token: Token::Whitespace(_),
3776                span: _,
3777            }) = token
3778            {
3779                continue;
3780            }
3781            break token.unwrap_or(&EOF_TOKEN);
3782        })
3783    }
3784
3785    /// Return nth non-whitespace token that has not yet been processed
3786    pub fn peek_nth_token(&self, n: usize) -> TokenWithSpan {
3787        self.peek_nth_token_ref(n).clone()
3788    }
3789
3790    /// Return nth non-whitespace token that has not yet been processed
3791    pub fn peek_nth_token_ref(&self, mut n: usize) -> &TokenWithSpan {
3792        let mut index = self.index;
3793        loop {
3794            index += 1;
3795            match self.tokens.get(index - 1) {
3796                Some(TokenWithSpan {
3797                    token: Token::Whitespace(_),
3798                    span: _,
3799                }) => continue,
3800                non_whitespace => {
3801                    if n == 0 {
3802                        return non_whitespace.unwrap_or(&EOF_TOKEN);
3803                    }
3804                    n -= 1;
3805                }
3806            }
3807        }
3808    }
3809
3810    /// Return the first token, possibly whitespace, that has not yet been processed
3811    /// (or None if reached end-of-file).
3812    pub fn peek_token_no_skip(&self) -> TokenWithSpan {
3813        self.peek_nth_token_no_skip(0)
3814    }
3815
3816    /// Return nth token, possibly whitespace, that has not yet been processed.
3817    pub fn peek_nth_token_no_skip(&self, n: usize) -> TokenWithSpan {
3818        self.tokens
3819            .get(self.index + n)
3820            .cloned()
3821            .unwrap_or(TokenWithSpan {
3822                token: Token::EOF,
3823                span: Span::empty(),
3824            })
3825    }
3826
3827    /// Return true if the next tokens exactly `expected`
3828    ///
3829    /// Does not advance the current token.
3830    fn peek_keywords(&mut self, expected: &[Keyword]) -> bool {
3831        let index = self.index;
3832        let matched = self.parse_keywords(expected);
3833        self.index = index;
3834        matched
3835    }
3836
3837    /// Advances to the next non-whitespace token and returns a copy.
3838    ///
3839    /// Please use [`Self::advance_token`] and [`Self::get_current_token`] to
3840    /// avoid the copy.
3841    pub fn next_token(&mut self) -> TokenWithSpan {
3842        self.advance_token();
3843        self.get_current_token().clone()
3844    }
3845
3846    /// Returns the index of the current token
3847    ///
3848    /// This can be used with APIs that expect an index, such as
3849    /// [`Self::token_at`]
3850    pub fn get_current_index(&self) -> usize {
3851        self.index.saturating_sub(1)
3852    }
3853
3854    /// Return the next unprocessed token, possibly whitespace.
3855    pub fn next_token_no_skip(&mut self) -> Option<&TokenWithSpan> {
3856        self.index += 1;
3857        self.tokens.get(self.index - 1)
3858    }
3859
3860    /// Advances the current token to the next non-whitespace token
3861    ///
3862    /// See [`Self::get_current_token`] to get the current token after advancing
3863    pub fn advance_token(&mut self) {
3864        loop {
3865            self.index += 1;
3866            match self.tokens.get(self.index - 1) {
3867                Some(TokenWithSpan {
3868                    token: Token::Whitespace(_),
3869                    span: _,
3870                }) => continue,
3871                _ => break,
3872            }
3873        }
3874    }
3875
3876    /// Returns a reference to the current token
3877    ///
3878    /// Does not advance the current token.
3879    pub fn get_current_token(&self) -> &TokenWithSpan {
3880        self.token_at(self.index.saturating_sub(1))
3881    }
3882
3883    /// Returns a reference to the previous token
3884    ///
3885    /// Does not advance the current token.
3886    pub fn get_previous_token(&self) -> &TokenWithSpan {
3887        self.token_at(self.index.saturating_sub(2))
3888    }
3889
3890    /// Returns a reference to the next token
3891    ///
3892    /// Does not advance the current token.
3893    pub fn get_next_token(&self) -> &TokenWithSpan {
3894        self.token_at(self.index)
3895    }
3896
3897    /// Seek back the last one non-whitespace token.
3898    ///
3899    /// Must be called after `next_token()`, otherwise might panic. OK to call
3900    /// after `next_token()` indicates an EOF.
3901    ///
3902    // TODO rename to backup_token and deprecate prev_token?
3903    pub fn prev_token(&mut self) {
3904        loop {
3905            assert!(self.index > 0);
3906            self.index -= 1;
3907            if let Some(TokenWithSpan {
3908                token: Token::Whitespace(_),
3909                span: _,
3910            }) = self.tokens.get(self.index)
3911            {
3912                continue;
3913            }
3914            return;
3915        }
3916    }
3917
3918    /// Report `found` was encountered instead of `expected`
3919    pub fn expected<T>(&self, expected: &str, found: TokenWithSpan) -> Result<T, ParserError> {
3920        parser_err!(
3921            format!("Expected: {expected}, found: {found}"),
3922            found.span.start
3923        )
3924    }
3925
3926    /// report `found` was encountered instead of `expected`
3927    pub fn expected_ref<T>(&self, expected: &str, found: &TokenWithSpan) -> Result<T, ParserError> {
3928        parser_err!(
3929            format!("Expected: {expected}, found: {found}"),
3930            found.span.start
3931        )
3932    }
3933
3934    /// Report that the token at `index` was found instead of `expected`.
3935    pub fn expected_at<T>(&self, expected: &str, index: usize) -> Result<T, ParserError> {
3936        let found = self.tokens.get(index).unwrap_or(&EOF_TOKEN);
3937        parser_err!(
3938            format!("Expected: {expected}, found: {found}"),
3939            found.span.start
3940        )
3941    }
3942
3943    /// If the current token is the `expected` keyword, consume it and returns
3944    /// true. Otherwise, no tokens are consumed and returns false.
3945    #[must_use]
3946    pub fn parse_keyword(&mut self, expected: Keyword) -> bool {
3947        if self.peek_keyword(expected) {
3948            self.advance_token();
3949            true
3950        } else {
3951            false
3952        }
3953    }
3954
3955    #[must_use]
3956    pub fn peek_keyword(&self, expected: Keyword) -> bool {
3957        matches!(&self.peek_token_ref().token, Token::Word(w) if expected == w.keyword)
3958    }
3959
3960    /// If the current token is the `expected` keyword followed by
3961    /// specified tokens, consume them and returns true.
3962    /// Otherwise, no tokens are consumed and returns false.
3963    ///
3964    /// Note that if the length of `tokens` is too long, this function will
3965    /// not be efficient as it does a loop on the tokens with `peek_nth_token`
3966    /// each time.
3967    pub fn parse_keyword_with_tokens(&mut self, expected: Keyword, tokens: &[Token]) -> bool {
3968        match &self.peek_token_ref().token {
3969            Token::Word(w) if expected == w.keyword => {
3970                for (idx, token) in tokens.iter().enumerate() {
3971                    if self.peek_nth_token_ref(idx + 1).token != *token {
3972                        return false;
3973                    }
3974                }
3975                // consume all tokens
3976                for _ in 0..(tokens.len() + 1) {
3977                    self.advance_token();
3978                }
3979                true
3980            }
3981            _ => false,
3982        }
3983    }
3984
3985    /// If the current and subsequent tokens exactly match the `keywords`
3986    /// sequence, consume them and returns true. Otherwise, no tokens are
3987    /// consumed and returns false
3988    #[must_use]
3989    pub fn parse_keywords(&mut self, keywords: &[Keyword]) -> bool {
3990        let index = self.index;
3991        for &keyword in keywords {
3992            if !self.parse_keyword(keyword) {
3993                // println!("parse_keywords aborting .. did not find {:?}", keyword);
3994                // reset index and return immediately
3995                self.index = index;
3996                return false;
3997            }
3998        }
3999        true
4000    }
4001
4002    /// If the current token is one of the given `keywords`, consume the token
4003    /// and return the keyword that matches. Otherwise, no tokens are consumed
4004    /// and returns [`None`].
4005    #[must_use]
4006    pub fn parse_one_of_keywords(&mut self, keywords: &[Keyword]) -> Option<Keyword> {
4007        match &self.peek_token_ref().token {
4008            Token::Word(w) => {
4009                keywords
4010                    .iter()
4011                    .find(|keyword| **keyword == w.keyword)
4012                    .map(|keyword| {
4013                        self.advance_token();
4014                        *keyword
4015                    })
4016            }
4017            _ => None,
4018        }
4019    }
4020
4021    /// If the current token is one of the expected keywords, consume the token
4022    /// and return the keyword that matches. Otherwise, return an error.
4023    pub fn expect_one_of_keywords(&mut self, keywords: &[Keyword]) -> Result<Keyword, ParserError> {
4024        if let Some(keyword) = self.parse_one_of_keywords(keywords) {
4025            Ok(keyword)
4026        } else {
4027            let keywords: Vec<String> = keywords.iter().map(|x| format!("{x:?}")).collect();
4028            self.expected_ref(
4029                &format!("one of {}", keywords.join(" or ")),
4030                self.peek_token_ref(),
4031            )
4032        }
4033    }
4034
4035    /// If the current token is the `expected` keyword, consume the token.
4036    /// Otherwise, return an error.
4037    ///
4038    // todo deprecate in favor of expected_keyword_is
4039    pub fn expect_keyword(&mut self, expected: Keyword) -> Result<TokenWithSpan, ParserError> {
4040        if self.parse_keyword(expected) {
4041            Ok(self.get_current_token().clone())
4042        } else {
4043            self.expected_ref(format!("{:?}", &expected).as_str(), self.peek_token_ref())
4044        }
4045    }
4046
4047    /// If the current token is the `expected` keyword, consume the token.
4048    /// Otherwise, return an error.
4049    ///
4050    /// This differs from expect_keyword only in that the matched keyword
4051    /// token is not returned.
4052    pub fn expect_keyword_is(&mut self, expected: Keyword) -> Result<(), ParserError> {
4053        if self.parse_keyword(expected) {
4054            Ok(())
4055        } else {
4056            self.expected_ref(format!("{:?}", &expected).as_str(), self.peek_token_ref())
4057        }
4058    }
4059
4060    /// If the current and subsequent tokens exactly match the `keywords`
4061    /// sequence, consume them and returns Ok. Otherwise, return an Error.
4062    pub fn expect_keywords(&mut self, expected: &[Keyword]) -> Result<(), ParserError> {
4063        for &kw in expected {
4064            self.expect_keyword_is(kw)?;
4065        }
4066        Ok(())
4067    }
4068
4069    /// Consume the next token if it matches the expected token, otherwise return false
4070    ///
4071    /// See [Self::advance_token] to consume the token unconditionally
4072    #[must_use]
4073    pub fn consume_token(&mut self, expected: &Token) -> bool {
4074        if self.peek_token_ref() == expected {
4075            self.advance_token();
4076            true
4077        } else {
4078            false
4079        }
4080    }
4081
4082    /// If the current and subsequent tokens exactly match the `tokens`
4083    /// sequence, consume them and returns true. Otherwise, no tokens are
4084    /// consumed and returns false
4085    #[must_use]
4086    pub fn consume_tokens(&mut self, tokens: &[Token]) -> bool {
4087        let index = self.index;
4088        for token in tokens {
4089            if !self.consume_token(token) {
4090                self.index = index;
4091                return false;
4092            }
4093        }
4094        true
4095    }
4096
4097    /// Bail out if the current token is not an expected keyword, or consume it if it is
4098    pub fn expect_token(&mut self, expected: &Token) -> Result<TokenWithSpan, ParserError> {
4099        if self.peek_token_ref() == expected {
4100            Ok(self.next_token())
4101        } else {
4102            self.expected_ref(&expected.to_string(), self.peek_token_ref())
4103        }
4104    }
4105
4106    fn parse<T: FromStr>(s: String, loc: Location) -> Result<T, ParserError>
4107    where
4108        <T as FromStr>::Err: Display,
4109    {
4110        s.parse::<T>().map_err(|e| {
4111            ParserError::ParserError(format!(
4112                "Could not parse '{s}' as {}: {e}{loc}",
4113                core::any::type_name::<T>()
4114            ))
4115        })
4116    }
4117
4118    /// Parse a comma-separated list of 1+ SelectItem
4119    pub fn parse_projection(&mut self) -> Result<Vec<SelectItem>, ParserError> {
4120        // BigQuery and Snowflake allow trailing commas, but only in project lists
4121        // e.g. `SELECT 1, 2, FROM t`
4122        // https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical#trailing_commas
4123        // https://docs.snowflake.com/en/release-notes/2024/8_11#select-supports-trailing-commas
4124
4125        let trailing_commas =
4126            self.options.trailing_commas | self.dialect.supports_projection_trailing_commas();
4127
4128        self.parse_comma_separated_with_trailing_commas(
4129            |p| p.parse_select_item(),
4130            trailing_commas,
4131            Self::is_reserved_for_column_alias,
4132        )
4133    }
4134
4135    pub fn parse_actions_list(&mut self) -> Result<Vec<Action>, ParserError> {
4136        let mut values = vec![];
4137        loop {
4138            values.push(self.parse_grant_permission()?);
4139            if !self.consume_token(&Token::Comma) {
4140                break;
4141            } else if self.options.trailing_commas {
4142                match self.peek_token().token {
4143                    Token::Word(kw) if kw.keyword == Keyword::ON => {
4144                        break;
4145                    }
4146                    Token::RParen
4147                    | Token::SemiColon
4148                    | Token::EOF
4149                    | Token::RBracket
4150                    | Token::RBrace => break,
4151                    _ => continue,
4152                }
4153            }
4154        }
4155        Ok(values)
4156    }
4157
4158    /// Parse a list of [TableWithJoins]
4159    fn parse_table_with_joins(&mut self) -> Result<Vec<TableWithJoins>, ParserError> {
4160        let trailing_commas = self.dialect.supports_from_trailing_commas();
4161
4162        self.parse_comma_separated_with_trailing_commas(
4163            Parser::parse_table_and_joins,
4164            trailing_commas,
4165            |kw, _parser| {
4166                self.dialect
4167                    .get_reserved_keywords_for_table_factor()
4168                    .contains(kw)
4169            },
4170        )
4171    }
4172
4173    /// Parse the comma of a comma-separated syntax element.
4174    /// `R` is a predicate that should return true if the next
4175    /// keyword is a reserved keyword.
4176    /// Allows for control over trailing commas
4177    ///
4178    /// Returns true if there is a next element
4179    fn is_parse_comma_separated_end_with_trailing_commas<R>(
4180        &mut self,
4181        trailing_commas: bool,
4182        is_reserved_keyword: &R,
4183    ) -> bool
4184    where
4185        R: Fn(&Keyword, &mut Parser) -> bool,
4186    {
4187        if !self.consume_token(&Token::Comma) {
4188            true
4189        } else if trailing_commas {
4190            let token = self.next_token().token;
4191            let is_end = match token {
4192                Token::Word(ref kw) if is_reserved_keyword(&kw.keyword, self) => true,
4193                Token::RParen | Token::SemiColon | Token::EOF | Token::RBracket | Token::RBrace => {
4194                    true
4195                }
4196                _ => false,
4197            };
4198            self.prev_token();
4199
4200            is_end
4201        } else {
4202            false
4203        }
4204    }
4205
4206    /// Parse the comma of a comma-separated syntax element.
4207    /// Returns true if there is a next element
4208    fn is_parse_comma_separated_end(&mut self) -> bool {
4209        self.is_parse_comma_separated_end_with_trailing_commas(
4210            self.options.trailing_commas,
4211            &Self::is_reserved_for_column_alias,
4212        )
4213    }
4214
4215    /// Parse a comma-separated list of 1+ items accepted by `F`
4216    pub fn parse_comma_separated<T, F>(&mut self, f: F) -> Result<Vec<T>, ParserError>
4217    where
4218        F: FnMut(&mut Parser<'a>) -> Result<T, ParserError>,
4219    {
4220        self.parse_comma_separated_with_trailing_commas(
4221            f,
4222            self.options.trailing_commas,
4223            Self::is_reserved_for_column_alias,
4224        )
4225    }
4226
4227    /// Parse a comma-separated list of 1+ items accepted by `F`.
4228    /// `R` is a predicate that should return true if the next
4229    /// keyword is a reserved keyword.
4230    /// Allows for control over trailing commas.
4231    fn parse_comma_separated_with_trailing_commas<T, F, R>(
4232        &mut self,
4233        mut f: F,
4234        trailing_commas: bool,
4235        is_reserved_keyword: R,
4236    ) -> Result<Vec<T>, ParserError>
4237    where
4238        F: FnMut(&mut Parser<'a>) -> Result<T, ParserError>,
4239        R: Fn(&Keyword, &mut Parser) -> bool,
4240    {
4241        let mut values = vec![];
4242        loop {
4243            values.push(f(self)?);
4244            if self.is_parse_comma_separated_end_with_trailing_commas(
4245                trailing_commas,
4246                &is_reserved_keyword,
4247            ) {
4248                break;
4249            }
4250        }
4251        Ok(values)
4252    }
4253
4254    /// Parse a period-separated list of 1+ items accepted by `F`
4255    fn parse_period_separated<T, F>(&mut self, mut f: F) -> Result<Vec<T>, ParserError>
4256    where
4257        F: FnMut(&mut Parser<'a>) -> Result<T, ParserError>,
4258    {
4259        let mut values = vec![];
4260        loop {
4261            values.push(f(self)?);
4262            if !self.consume_token(&Token::Period) {
4263                break;
4264            }
4265        }
4266        Ok(values)
4267    }
4268
4269    /// Parse a keyword-separated list of 1+ items accepted by `F`
4270    pub fn parse_keyword_separated<T, F>(
4271        &mut self,
4272        keyword: Keyword,
4273        mut f: F,
4274    ) -> Result<Vec<T>, ParserError>
4275    where
4276        F: FnMut(&mut Parser<'a>) -> Result<T, ParserError>,
4277    {
4278        let mut values = vec![];
4279        loop {
4280            values.push(f(self)?);
4281            if !self.parse_keyword(keyword) {
4282                break;
4283            }
4284        }
4285        Ok(values)
4286    }
4287
4288    pub fn parse_parenthesized<T, F>(&mut self, mut f: F) -> Result<T, ParserError>
4289    where
4290        F: FnMut(&mut Parser<'a>) -> Result<T, ParserError>,
4291    {
4292        self.expect_token(&Token::LParen)?;
4293        let res = f(self)?;
4294        self.expect_token(&Token::RParen)?;
4295        Ok(res)
4296    }
4297
4298    /// Parse a comma-separated list of 0+ items accepted by `F`
4299    /// * `end_token` - expected end token for the closure (e.g. [Token::RParen], [Token::RBrace] ...)
4300    pub fn parse_comma_separated0<T, F>(
4301        &mut self,
4302        f: F,
4303        end_token: Token,
4304    ) -> Result<Vec<T>, ParserError>
4305    where
4306        F: FnMut(&mut Parser<'a>) -> Result<T, ParserError>,
4307    {
4308        if self.peek_token().token == end_token {
4309            return Ok(vec![]);
4310        }
4311
4312        if self.options.trailing_commas && self.peek_tokens() == [Token::Comma, end_token] {
4313            let _ = self.consume_token(&Token::Comma);
4314            return Ok(vec![]);
4315        }
4316
4317        self.parse_comma_separated(f)
4318    }
4319
4320    /// Parses 0 or more statements, each followed by a semicolon.
4321    /// If the next token is any of `terminal_keywords` then no more
4322    /// statements will be parsed.
4323    pub(crate) fn parse_statement_list(
4324        &mut self,
4325        terminal_keywords: &[Keyword],
4326    ) -> Result<Vec<Statement>, ParserError> {
4327        let mut values = vec![];
4328        loop {
4329            if let Token::Word(w) = &self.peek_nth_token_ref(0).token {
4330                if w.quote_style.is_none() && terminal_keywords.contains(&w.keyword) {
4331                    break;
4332                }
4333            }
4334
4335            values.push(self.parse_statement()?);
4336            self.expect_token(&Token::SemiColon)?;
4337        }
4338        Ok(values)
4339    }
4340
4341    /// Default implementation of a predicate that returns true if
4342    /// the specified keyword is reserved for column alias.
4343    /// See [Dialect::is_column_alias]
4344    fn is_reserved_for_column_alias(kw: &Keyword, parser: &mut Parser) -> bool {
4345        !parser.dialect.is_column_alias(kw, parser)
4346    }
4347
4348    /// Run a parser method `f`, reverting back to the current position if unsuccessful.
4349    /// Returns `None` if `f` returns an error
4350    pub fn maybe_parse<T, F>(&mut self, f: F) -> Result<Option<T>, ParserError>
4351    where
4352        F: FnMut(&mut Parser) -> Result<T, ParserError>,
4353    {
4354        match self.try_parse(f) {
4355            Ok(t) => Ok(Some(t)),
4356            Err(ParserError::RecursionLimitExceeded) => Err(ParserError::RecursionLimitExceeded),
4357            _ => Ok(None),
4358        }
4359    }
4360
4361    /// Run a parser method `f`, reverting back to the current position if unsuccessful.
4362    pub fn try_parse<T, F>(&mut self, mut f: F) -> Result<T, ParserError>
4363    where
4364        F: FnMut(&mut Parser) -> Result<T, ParserError>,
4365    {
4366        let index = self.index;
4367        match f(self) {
4368            Ok(t) => Ok(t),
4369            Err(e) => {
4370                // Unwind stack if limit exceeded
4371                self.index = index;
4372                Err(e)
4373            }
4374        }
4375    }
4376
4377    /// Parse either `ALL`, `DISTINCT` or `DISTINCT ON (...)`. Returns [`None`] if `ALL` is parsed
4378    /// and results in a [`ParserError`] if both `ALL` and `DISTINCT` are found.
4379    pub fn parse_all_or_distinct(&mut self) -> Result<Option<Distinct>, ParserError> {
4380        let loc = self.peek_token().span.start;
4381        let all = self.parse_keyword(Keyword::ALL);
4382        let distinct = self.parse_keyword(Keyword::DISTINCT);
4383        if !distinct {
4384            return Ok(None);
4385        }
4386        if all {
4387            return parser_err!("Cannot specify both ALL and DISTINCT".to_string(), loc);
4388        }
4389        let on = self.parse_keyword(Keyword::ON);
4390        if !on {
4391            return Ok(Some(Distinct::Distinct));
4392        }
4393
4394        self.expect_token(&Token::LParen)?;
4395        let col_names = if self.consume_token(&Token::RParen) {
4396            self.prev_token();
4397            Vec::new()
4398        } else {
4399            self.parse_comma_separated(Parser::parse_expr)?
4400        };
4401        self.expect_token(&Token::RParen)?;
4402        Ok(Some(Distinct::On(col_names)))
4403    }
4404
4405    /// Parse a SQL CREATE statement
4406    pub fn parse_create(&mut self) -> Result<Statement, ParserError> {
4407        let or_replace = self.parse_keywords(&[Keyword::OR, Keyword::REPLACE]);
4408        let or_alter = self.parse_keywords(&[Keyword::OR, Keyword::ALTER]);
4409        let local = self.parse_one_of_keywords(&[Keyword::LOCAL]).is_some();
4410        let global = self.parse_one_of_keywords(&[Keyword::GLOBAL]).is_some();
4411        let transient = self.parse_one_of_keywords(&[Keyword::TRANSIENT]).is_some();
4412        let global: Option<bool> = if global {
4413            Some(true)
4414        } else if local {
4415            Some(false)
4416        } else {
4417            None
4418        };
4419        let temporary = self
4420            .parse_one_of_keywords(&[Keyword::TEMP, Keyword::TEMPORARY])
4421            .is_some();
4422        let persistent = dialect_of!(self is DuckDbDialect)
4423            && self.parse_one_of_keywords(&[Keyword::PERSISTENT]).is_some();
4424        let create_view_params = self.parse_create_view_params()?;
4425        if self.parse_keyword(Keyword::TABLE) {
4426            self.parse_create_table(or_replace, temporary, global, transient)
4427        } else if self.parse_keyword(Keyword::MATERIALIZED) || self.parse_keyword(Keyword::VIEW) {
4428            self.prev_token();
4429            self.parse_create_view(or_replace, temporary, create_view_params)
4430        } else if self.parse_keyword(Keyword::POLICY) {
4431            self.parse_create_policy()
4432        } else if self.parse_keyword(Keyword::EXTERNAL) {
4433            self.parse_create_external_table(or_replace)
4434        } else if self.parse_keyword(Keyword::FUNCTION) {
4435            self.parse_create_function(or_replace, temporary)
4436        } else if self.parse_keyword(Keyword::TRIGGER) {
4437            self.parse_create_trigger(or_replace, false)
4438        } else if self.parse_keywords(&[Keyword::CONSTRAINT, Keyword::TRIGGER]) {
4439            self.parse_create_trigger(or_replace, true)
4440        } else if self.parse_keyword(Keyword::MACRO) {
4441            self.parse_create_macro(or_replace, temporary)
4442        } else if self.parse_keyword(Keyword::SECRET) {
4443            self.parse_create_secret(or_replace, temporary, persistent)
4444        } else if or_replace {
4445            self.expected(
4446                "[EXTERNAL] TABLE or [MATERIALIZED] VIEW or FUNCTION after CREATE OR REPLACE",
4447                self.peek_token(),
4448            )
4449        } else if self.parse_keyword(Keyword::EXTENSION) {
4450            self.parse_create_extension()
4451        } else if self.parse_keyword(Keyword::INDEX) {
4452            self.parse_create_index(false)
4453        } else if self.parse_keywords(&[Keyword::UNIQUE, Keyword::INDEX]) {
4454            self.parse_create_index(true)
4455        } else if self.parse_keyword(Keyword::VIRTUAL) {
4456            self.parse_create_virtual_table()
4457        } else if self.parse_keyword(Keyword::SCHEMA) {
4458            self.parse_create_schema()
4459        } else if self.parse_keyword(Keyword::DATABASE) {
4460            self.parse_create_database()
4461        } else if self.parse_keyword(Keyword::ROLE) {
4462            self.parse_create_role()
4463        } else if self.parse_keyword(Keyword::SEQUENCE) {
4464            self.parse_create_sequence(temporary)
4465        } else if self.parse_keyword(Keyword::TYPE) {
4466            self.parse_create_type()
4467        } else if self.parse_keyword(Keyword::PROCEDURE) {
4468            self.parse_create_procedure(or_alter)
4469        } else if self.parse_keyword(Keyword::CONNECTOR) {
4470            self.parse_create_connector()
4471        } else {
4472            self.expected("an object type after CREATE", self.peek_token())
4473        }
4474    }
4475
4476    /// See [DuckDB Docs](https://duckdb.org/docs/sql/statements/create_secret.html) for more details.
4477    pub fn parse_create_secret(
4478        &mut self,
4479        or_replace: bool,
4480        temporary: bool,
4481        persistent: bool,
4482    ) -> Result<Statement, ParserError> {
4483        let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
4484
4485        let mut storage_specifier = None;
4486        let mut name = None;
4487        if self.peek_token() != Token::LParen {
4488            if self.parse_keyword(Keyword::IN) {
4489                storage_specifier = self.parse_identifier().ok()
4490            } else {
4491                name = self.parse_identifier().ok();
4492            }
4493
4494            // Storage specifier may follow the name
4495            if storage_specifier.is_none()
4496                && self.peek_token() != Token::LParen
4497                && self.parse_keyword(Keyword::IN)
4498            {
4499                storage_specifier = self.parse_identifier().ok();
4500            }
4501        }
4502
4503        self.expect_token(&Token::LParen)?;
4504        self.expect_keyword_is(Keyword::TYPE)?;
4505        let secret_type = self.parse_identifier()?;
4506
4507        let mut options = Vec::new();
4508        if self.consume_token(&Token::Comma) {
4509            options.append(&mut self.parse_comma_separated(|p| {
4510                let key = p.parse_identifier()?;
4511                let value = p.parse_identifier()?;
4512                Ok(SecretOption { key, value })
4513            })?);
4514        }
4515        self.expect_token(&Token::RParen)?;
4516
4517        let temp = match (temporary, persistent) {
4518            (true, false) => Some(true),
4519            (false, true) => Some(false),
4520            (false, false) => None,
4521            _ => self.expected("TEMPORARY or PERSISTENT", self.peek_token())?,
4522        };
4523
4524        Ok(Statement::CreateSecret {
4525            or_replace,
4526            temporary: temp,
4527            if_not_exists,
4528            name,
4529            storage_specifier,
4530            secret_type,
4531            options,
4532        })
4533    }
4534
4535    /// Parse a CACHE TABLE statement
4536    pub fn parse_cache_table(&mut self) -> Result<Statement, ParserError> {
4537        let (mut table_flag, mut options, mut has_as, mut query) = (None, vec![], false, None);
4538        if self.parse_keyword(Keyword::TABLE) {
4539            let table_name = self.parse_object_name(false)?;
4540            if self.peek_token().token != Token::EOF {
4541                if let Token::Word(word) = self.peek_token().token {
4542                    if word.keyword == Keyword::OPTIONS {
4543                        options = self.parse_options(Keyword::OPTIONS)?
4544                    }
4545                };
4546
4547                if self.peek_token().token != Token::EOF {
4548                    let (a, q) = self.parse_as_query()?;
4549                    has_as = a;
4550                    query = Some(q);
4551                }
4552
4553                Ok(Statement::Cache {
4554                    table_flag,
4555                    table_name,
4556                    has_as,
4557                    options,
4558                    query,
4559                })
4560            } else {
4561                Ok(Statement::Cache {
4562                    table_flag,
4563                    table_name,
4564                    has_as,
4565                    options,
4566                    query,
4567                })
4568            }
4569        } else {
4570            table_flag = Some(self.parse_object_name(false)?);
4571            if self.parse_keyword(Keyword::TABLE) {
4572                let table_name = self.parse_object_name(false)?;
4573                if self.peek_token() != Token::EOF {
4574                    if let Token::Word(word) = self.peek_token().token {
4575                        if word.keyword == Keyword::OPTIONS {
4576                            options = self.parse_options(Keyword::OPTIONS)?
4577                        }
4578                    };
4579
4580                    if self.peek_token() != Token::EOF {
4581                        let (a, q) = self.parse_as_query()?;
4582                        has_as = a;
4583                        query = Some(q);
4584                    }
4585
4586                    Ok(Statement::Cache {
4587                        table_flag,
4588                        table_name,
4589                        has_as,
4590                        options,
4591                        query,
4592                    })
4593                } else {
4594                    Ok(Statement::Cache {
4595                        table_flag,
4596                        table_name,
4597                        has_as,
4598                        options,
4599                        query,
4600                    })
4601                }
4602            } else {
4603                if self.peek_token() == Token::EOF {
4604                    self.prev_token();
4605                }
4606                self.expected("a `TABLE` keyword", self.peek_token())
4607            }
4608        }
4609    }
4610
4611    /// Parse 'AS' before as query,such as `WITH XXX AS SELECT XXX` oer `CACHE TABLE AS SELECT XXX`
4612    pub fn parse_as_query(&mut self) -> Result<(bool, Box<Query>), ParserError> {
4613        match self.peek_token().token {
4614            Token::Word(word) => match word.keyword {
4615                Keyword::AS => {
4616                    self.next_token();
4617                    Ok((true, self.parse_query()?))
4618                }
4619                _ => Ok((false, self.parse_query()?)),
4620            },
4621            _ => self.expected("a QUERY statement", self.peek_token()),
4622        }
4623    }
4624
4625    /// Parse a UNCACHE TABLE statement
4626    pub fn parse_uncache_table(&mut self) -> Result<Statement, ParserError> {
4627        self.expect_keyword_is(Keyword::TABLE)?;
4628        let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
4629        let table_name = self.parse_object_name(false)?;
4630        Ok(Statement::UNCache {
4631            table_name,
4632            if_exists,
4633        })
4634    }
4635
4636    /// SQLite-specific `CREATE VIRTUAL TABLE`
4637    pub fn parse_create_virtual_table(&mut self) -> Result<Statement, ParserError> {
4638        self.expect_keyword_is(Keyword::TABLE)?;
4639        let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
4640        let table_name = self.parse_object_name(false)?;
4641        self.expect_keyword_is(Keyword::USING)?;
4642        let module_name = self.parse_identifier()?;
4643        // SQLite docs note that module "arguments syntax is sufficiently
4644        // general that the arguments can be made to appear as column
4645        // definitions in a traditional CREATE TABLE statement", but
4646        // we don't implement that.
4647        let module_args = self.parse_parenthesized_column_list(Optional, false)?;
4648        Ok(Statement::CreateVirtualTable {
4649            name: table_name,
4650            if_not_exists,
4651            module_name,
4652            module_args,
4653        })
4654    }
4655
4656    pub fn parse_create_schema(&mut self) -> Result<Statement, ParserError> {
4657        let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
4658
4659        let schema_name = self.parse_schema_name()?;
4660
4661        Ok(Statement::CreateSchema {
4662            schema_name,
4663            if_not_exists,
4664        })
4665    }
4666
4667    fn parse_schema_name(&mut self) -> Result<SchemaName, ParserError> {
4668        if self.parse_keyword(Keyword::AUTHORIZATION) {
4669            Ok(SchemaName::UnnamedAuthorization(self.parse_identifier()?))
4670        } else {
4671            let name = self.parse_object_name(false)?;
4672
4673            if self.parse_keyword(Keyword::AUTHORIZATION) {
4674                Ok(SchemaName::NamedAuthorization(
4675                    name,
4676                    self.parse_identifier()?,
4677                ))
4678            } else {
4679                Ok(SchemaName::Simple(name))
4680            }
4681        }
4682    }
4683
4684    pub fn parse_create_database(&mut self) -> Result<Statement, ParserError> {
4685        let ine = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
4686        let db_name = self.parse_object_name(false)?;
4687        let mut location = None;
4688        let mut managed_location = None;
4689        loop {
4690            match self.parse_one_of_keywords(&[Keyword::LOCATION, Keyword::MANAGEDLOCATION]) {
4691                Some(Keyword::LOCATION) => location = Some(self.parse_literal_string()?),
4692                Some(Keyword::MANAGEDLOCATION) => {
4693                    managed_location = Some(self.parse_literal_string()?)
4694                }
4695                _ => break,
4696            }
4697        }
4698        Ok(Statement::CreateDatabase {
4699            db_name,
4700            if_not_exists: ine,
4701            location,
4702            managed_location,
4703        })
4704    }
4705
4706    pub fn parse_optional_create_function_using(
4707        &mut self,
4708    ) -> Result<Option<CreateFunctionUsing>, ParserError> {
4709        if !self.parse_keyword(Keyword::USING) {
4710            return Ok(None);
4711        };
4712        let keyword =
4713            self.expect_one_of_keywords(&[Keyword::JAR, Keyword::FILE, Keyword::ARCHIVE])?;
4714
4715        let uri = self.parse_literal_string()?;
4716
4717        match keyword {
4718            Keyword::JAR => Ok(Some(CreateFunctionUsing::Jar(uri))),
4719            Keyword::FILE => Ok(Some(CreateFunctionUsing::File(uri))),
4720            Keyword::ARCHIVE => Ok(Some(CreateFunctionUsing::Archive(uri))),
4721            _ => self.expected(
4722                "JAR, FILE or ARCHIVE, got {:?}",
4723                TokenWithSpan::wrap(Token::make_keyword(format!("{keyword:?}").as_str())),
4724            ),
4725        }
4726    }
4727
4728    pub fn parse_create_function(
4729        &mut self,
4730        or_replace: bool,
4731        temporary: bool,
4732    ) -> Result<Statement, ParserError> {
4733        if dialect_of!(self is HiveDialect) {
4734            self.parse_hive_create_function(or_replace, temporary)
4735        } else if dialect_of!(self is PostgreSqlDialect | GenericDialect) {
4736            self.parse_postgres_create_function(or_replace, temporary)
4737        } else if dialect_of!(self is DuckDbDialect) {
4738            self.parse_create_macro(or_replace, temporary)
4739        } else if dialect_of!(self is BigQueryDialect) {
4740            self.parse_bigquery_create_function(or_replace, temporary)
4741        } else {
4742            self.prev_token();
4743            self.expected("an object type after CREATE", self.peek_token())
4744        }
4745    }
4746
4747    /// Parse `CREATE FUNCTION` for [Postgres]
4748    ///
4749    /// [Postgres]: https://www.postgresql.org/docs/15/sql-createfunction.html
4750    fn parse_postgres_create_function(
4751        &mut self,
4752        or_replace: bool,
4753        temporary: bool,
4754    ) -> Result<Statement, ParserError> {
4755        let name = self.parse_object_name(false)?;
4756
4757        self.expect_token(&Token::LParen)?;
4758        let args = if Token::RParen != self.peek_token_ref().token {
4759            self.parse_comma_separated(Parser::parse_function_arg)?
4760        } else {
4761            vec![]
4762        };
4763        self.expect_token(&Token::RParen)?;
4764
4765        let return_type = if self.parse_keyword(Keyword::RETURNS) {
4766            Some(self.parse_data_type()?)
4767        } else {
4768            None
4769        };
4770
4771        #[derive(Default)]
4772        struct Body {
4773            language: Option<Ident>,
4774            behavior: Option<FunctionBehavior>,
4775            function_body: Option<CreateFunctionBody>,
4776            called_on_null: Option<FunctionCalledOnNull>,
4777            parallel: Option<FunctionParallel>,
4778        }
4779        let mut body = Body::default();
4780        loop {
4781            fn ensure_not_set<T>(field: &Option<T>, name: &str) -> Result<(), ParserError> {
4782                if field.is_some() {
4783                    return Err(ParserError::ParserError(format!(
4784                        "{name} specified more than once",
4785                    )));
4786                }
4787                Ok(())
4788            }
4789            if self.parse_keyword(Keyword::AS) {
4790                ensure_not_set(&body.function_body, "AS")?;
4791                body.function_body = Some(CreateFunctionBody::AsBeforeOptions(
4792                    self.parse_create_function_body_string()?,
4793                ));
4794            } else if self.parse_keyword(Keyword::LANGUAGE) {
4795                ensure_not_set(&body.language, "LANGUAGE")?;
4796                body.language = Some(self.parse_identifier()?);
4797            } else if self.parse_keyword(Keyword::IMMUTABLE) {
4798                ensure_not_set(&body.behavior, "IMMUTABLE | STABLE | VOLATILE")?;
4799                body.behavior = Some(FunctionBehavior::Immutable);
4800            } else if self.parse_keyword(Keyword::STABLE) {
4801                ensure_not_set(&body.behavior, "IMMUTABLE | STABLE | VOLATILE")?;
4802                body.behavior = Some(FunctionBehavior::Stable);
4803            } else if self.parse_keyword(Keyword::VOLATILE) {
4804                ensure_not_set(&body.behavior, "IMMUTABLE | STABLE | VOLATILE")?;
4805                body.behavior = Some(FunctionBehavior::Volatile);
4806            } else if self.parse_keywords(&[
4807                Keyword::CALLED,
4808                Keyword::ON,
4809                Keyword::NULL,
4810                Keyword::INPUT,
4811            ]) {
4812                ensure_not_set(
4813                    &body.called_on_null,
4814                    "CALLED ON NULL INPUT | RETURNS NULL ON NULL INPUT | STRICT",
4815                )?;
4816                body.called_on_null = Some(FunctionCalledOnNull::CalledOnNullInput);
4817            } else if self.parse_keywords(&[
4818                Keyword::RETURNS,
4819                Keyword::NULL,
4820                Keyword::ON,
4821                Keyword::NULL,
4822                Keyword::INPUT,
4823            ]) {
4824                ensure_not_set(
4825                    &body.called_on_null,
4826                    "CALLED ON NULL INPUT | RETURNS NULL ON NULL INPUT | STRICT",
4827                )?;
4828                body.called_on_null = Some(FunctionCalledOnNull::ReturnsNullOnNullInput);
4829            } else if self.parse_keyword(Keyword::STRICT) {
4830                ensure_not_set(
4831                    &body.called_on_null,
4832                    "CALLED ON NULL INPUT | RETURNS NULL ON NULL INPUT | STRICT",
4833                )?;
4834                body.called_on_null = Some(FunctionCalledOnNull::Strict);
4835            } else if self.parse_keyword(Keyword::PARALLEL) {
4836                ensure_not_set(&body.parallel, "PARALLEL { UNSAFE | RESTRICTED | SAFE }")?;
4837                if self.parse_keyword(Keyword::UNSAFE) {
4838                    body.parallel = Some(FunctionParallel::Unsafe);
4839                } else if self.parse_keyword(Keyword::RESTRICTED) {
4840                    body.parallel = Some(FunctionParallel::Restricted);
4841                } else if self.parse_keyword(Keyword::SAFE) {
4842                    body.parallel = Some(FunctionParallel::Safe);
4843                } else {
4844                    return self.expected("one of UNSAFE | RESTRICTED | SAFE", self.peek_token());
4845                }
4846            } else if self.parse_keyword(Keyword::RETURN) {
4847                ensure_not_set(&body.function_body, "RETURN")?;
4848                body.function_body = Some(CreateFunctionBody::Return(self.parse_expr()?));
4849            } else {
4850                break;
4851            }
4852        }
4853
4854        Ok(Statement::CreateFunction(CreateFunction {
4855            or_replace,
4856            temporary,
4857            name,
4858            args: Some(args),
4859            return_type,
4860            behavior: body.behavior,
4861            called_on_null: body.called_on_null,
4862            parallel: body.parallel,
4863            language: body.language,
4864            function_body: body.function_body,
4865            if_not_exists: false,
4866            using: None,
4867            determinism_specifier: None,
4868            options: None,
4869            remote_connection: None,
4870        }))
4871    }
4872
4873    /// Parse `CREATE FUNCTION` for [Hive]
4874    ///
4875    /// [Hive]: https://cwiki.apache.org/confluence/display/hive/languagemanual+ddl#LanguageManualDDL-Create/Drop/ReloadFunction
4876    fn parse_hive_create_function(
4877        &mut self,
4878        or_replace: bool,
4879        temporary: bool,
4880    ) -> Result<Statement, ParserError> {
4881        let name = self.parse_object_name(false)?;
4882        self.expect_keyword_is(Keyword::AS)?;
4883
4884        let as_ = self.parse_create_function_body_string()?;
4885        let using = self.parse_optional_create_function_using()?;
4886
4887        Ok(Statement::CreateFunction(CreateFunction {
4888            or_replace,
4889            temporary,
4890            name,
4891            function_body: Some(CreateFunctionBody::AsBeforeOptions(as_)),
4892            using,
4893            if_not_exists: false,
4894            args: None,
4895            return_type: None,
4896            behavior: None,
4897            called_on_null: None,
4898            parallel: None,
4899            language: None,
4900            determinism_specifier: None,
4901            options: None,
4902            remote_connection: None,
4903        }))
4904    }
4905
4906    /// Parse `CREATE FUNCTION` for [BigQuery]
4907    ///
4908    /// [BigQuery]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_function_statement
4909    fn parse_bigquery_create_function(
4910        &mut self,
4911        or_replace: bool,
4912        temporary: bool,
4913    ) -> Result<Statement, ParserError> {
4914        let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
4915        let name = self.parse_object_name(false)?;
4916
4917        let parse_function_param =
4918            |parser: &mut Parser| -> Result<OperateFunctionArg, ParserError> {
4919                let name = parser.parse_identifier()?;
4920                let data_type = parser.parse_data_type()?;
4921                Ok(OperateFunctionArg {
4922                    mode: None,
4923                    name: Some(name),
4924                    data_type,
4925                    default_expr: None,
4926                })
4927            };
4928        self.expect_token(&Token::LParen)?;
4929        let args = self.parse_comma_separated0(parse_function_param, Token::RParen)?;
4930        self.expect_token(&Token::RParen)?;
4931
4932        let return_type = if self.parse_keyword(Keyword::RETURNS) {
4933            Some(self.parse_data_type()?)
4934        } else {
4935            None
4936        };
4937
4938        let determinism_specifier = if self.parse_keyword(Keyword::DETERMINISTIC) {
4939            Some(FunctionDeterminismSpecifier::Deterministic)
4940        } else if self.parse_keywords(&[Keyword::NOT, Keyword::DETERMINISTIC]) {
4941            Some(FunctionDeterminismSpecifier::NotDeterministic)
4942        } else {
4943            None
4944        };
4945
4946        let language = if self.parse_keyword(Keyword::LANGUAGE) {
4947            Some(self.parse_identifier()?)
4948        } else {
4949            None
4950        };
4951
4952        let remote_connection =
4953            if self.parse_keywords(&[Keyword::REMOTE, Keyword::WITH, Keyword::CONNECTION]) {
4954                Some(self.parse_object_name(false)?)
4955            } else {
4956                None
4957            };
4958
4959        // `OPTIONS` may come before of after the function body but
4960        // may be specified at most once.
4961        let mut options = self.maybe_parse_options(Keyword::OPTIONS)?;
4962
4963        let function_body = if remote_connection.is_none() {
4964            self.expect_keyword_is(Keyword::AS)?;
4965            let expr = self.parse_expr()?;
4966            if options.is_none() {
4967                options = self.maybe_parse_options(Keyword::OPTIONS)?;
4968                Some(CreateFunctionBody::AsBeforeOptions(expr))
4969            } else {
4970                Some(CreateFunctionBody::AsAfterOptions(expr))
4971            }
4972        } else {
4973            None
4974        };
4975
4976        Ok(Statement::CreateFunction(CreateFunction {
4977            or_replace,
4978            temporary,
4979            if_not_exists,
4980            name,
4981            args: Some(args),
4982            return_type,
4983            function_body,
4984            language,
4985            determinism_specifier,
4986            options,
4987            remote_connection,
4988            using: None,
4989            behavior: None,
4990            called_on_null: None,
4991            parallel: None,
4992        }))
4993    }
4994
4995    fn parse_function_arg(&mut self) -> Result<OperateFunctionArg, ParserError> {
4996        let mode = if self.parse_keyword(Keyword::IN) {
4997            Some(ArgMode::In)
4998        } else if self.parse_keyword(Keyword::OUT) {
4999            Some(ArgMode::Out)
5000        } else if self.parse_keyword(Keyword::INOUT) {
5001            Some(ArgMode::InOut)
5002        } else {
5003            None
5004        };
5005
5006        // parse: [ argname ] argtype
5007        let mut name = None;
5008        let mut data_type = self.parse_data_type()?;
5009        if let DataType::Custom(n, _) = &data_type {
5010            // the first token is actually a name
5011            match n.0[0].clone() {
5012                ObjectNamePart::Identifier(ident) => name = Some(ident),
5013            }
5014            data_type = self.parse_data_type()?;
5015        }
5016
5017        let default_expr = if self.parse_keyword(Keyword::DEFAULT) || self.consume_token(&Token::Eq)
5018        {
5019            Some(self.parse_expr()?)
5020        } else {
5021            None
5022        };
5023        Ok(OperateFunctionArg {
5024            mode,
5025            name,
5026            data_type,
5027            default_expr,
5028        })
5029    }
5030
5031    /// Parse statements of the DropTrigger type such as:
5032    ///
5033    /// ```sql
5034    /// DROP TRIGGER [ IF EXISTS ] name ON table_name [ CASCADE | RESTRICT ]
5035    /// ```
5036    pub fn parse_drop_trigger(&mut self) -> Result<Statement, ParserError> {
5037        if !dialect_of!(self is PostgreSqlDialect | GenericDialect | MySqlDialect) {
5038            self.prev_token();
5039            return self.expected("an object type after DROP", self.peek_token());
5040        }
5041        let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
5042        let trigger_name = self.parse_object_name(false)?;
5043        let table_name = if self.parse_keyword(Keyword::ON) {
5044            Some(self.parse_object_name(false)?)
5045        } else {
5046            None
5047        };
5048        let option = self
5049            .parse_one_of_keywords(&[Keyword::CASCADE, Keyword::RESTRICT])
5050            .map(|keyword| match keyword {
5051                Keyword::CASCADE => ReferentialAction::Cascade,
5052                Keyword::RESTRICT => ReferentialAction::Restrict,
5053                _ => unreachable!(),
5054            });
5055        Ok(Statement::DropTrigger {
5056            if_exists,
5057            trigger_name,
5058            table_name,
5059            option,
5060        })
5061    }
5062
5063    pub fn parse_create_trigger(
5064        &mut self,
5065        or_replace: bool,
5066        is_constraint: bool,
5067    ) -> Result<Statement, ParserError> {
5068        if !dialect_of!(self is PostgreSqlDialect | GenericDialect | MySqlDialect) {
5069            self.prev_token();
5070            return self.expected("an object type after CREATE", self.peek_token());
5071        }
5072
5073        let name = self.parse_object_name(false)?;
5074        let period = self.parse_trigger_period()?;
5075
5076        let events = self.parse_keyword_separated(Keyword::OR, Parser::parse_trigger_event)?;
5077        self.expect_keyword_is(Keyword::ON)?;
5078        let table_name = self.parse_object_name(false)?;
5079
5080        let referenced_table_name = if self.parse_keyword(Keyword::FROM) {
5081            self.parse_object_name(true).ok()
5082        } else {
5083            None
5084        };
5085
5086        let characteristics = self.parse_constraint_characteristics()?;
5087
5088        let mut referencing = vec![];
5089        if self.parse_keyword(Keyword::REFERENCING) {
5090            while let Some(refer) = self.parse_trigger_referencing()? {
5091                referencing.push(refer);
5092            }
5093        }
5094
5095        self.expect_keyword_is(Keyword::FOR)?;
5096        let include_each = self.parse_keyword(Keyword::EACH);
5097        let trigger_object =
5098            match self.expect_one_of_keywords(&[Keyword::ROW, Keyword::STATEMENT])? {
5099                Keyword::ROW => TriggerObject::Row,
5100                Keyword::STATEMENT => TriggerObject::Statement,
5101                _ => unreachable!(),
5102            };
5103
5104        let condition = self
5105            .parse_keyword(Keyword::WHEN)
5106            .then(|| self.parse_expr())
5107            .transpose()?;
5108
5109        self.expect_keyword_is(Keyword::EXECUTE)?;
5110
5111        let exec_body = self.parse_trigger_exec_body()?;
5112
5113        Ok(Statement::CreateTrigger {
5114            or_replace,
5115            is_constraint,
5116            name,
5117            period,
5118            events,
5119            table_name,
5120            referenced_table_name,
5121            referencing,
5122            trigger_object,
5123            include_each,
5124            condition,
5125            exec_body,
5126            characteristics,
5127        })
5128    }
5129
5130    pub fn parse_trigger_period(&mut self) -> Result<TriggerPeriod, ParserError> {
5131        Ok(
5132            match self.expect_one_of_keywords(&[
5133                Keyword::BEFORE,
5134                Keyword::AFTER,
5135                Keyword::INSTEAD,
5136            ])? {
5137                Keyword::BEFORE => TriggerPeriod::Before,
5138                Keyword::AFTER => TriggerPeriod::After,
5139                Keyword::INSTEAD => self
5140                    .expect_keyword_is(Keyword::OF)
5141                    .map(|_| TriggerPeriod::InsteadOf)?,
5142                _ => unreachable!(),
5143            },
5144        )
5145    }
5146
5147    pub fn parse_trigger_event(&mut self) -> Result<TriggerEvent, ParserError> {
5148        Ok(
5149            match self.expect_one_of_keywords(&[
5150                Keyword::INSERT,
5151                Keyword::UPDATE,
5152                Keyword::DELETE,
5153                Keyword::TRUNCATE,
5154            ])? {
5155                Keyword::INSERT => TriggerEvent::Insert,
5156                Keyword::UPDATE => {
5157                    if self.parse_keyword(Keyword::OF) {
5158                        let cols = self.parse_comma_separated(Parser::parse_identifier)?;
5159                        TriggerEvent::Update(cols)
5160                    } else {
5161                        TriggerEvent::Update(vec![])
5162                    }
5163                }
5164                Keyword::DELETE => TriggerEvent::Delete,
5165                Keyword::TRUNCATE => TriggerEvent::Truncate,
5166                _ => unreachable!(),
5167            },
5168        )
5169    }
5170
5171    pub fn parse_trigger_referencing(&mut self) -> Result<Option<TriggerReferencing>, ParserError> {
5172        let refer_type = match self.parse_one_of_keywords(&[Keyword::OLD, Keyword::NEW]) {
5173            Some(Keyword::OLD) if self.parse_keyword(Keyword::TABLE) => {
5174                TriggerReferencingType::OldTable
5175            }
5176            Some(Keyword::NEW) if self.parse_keyword(Keyword::TABLE) => {
5177                TriggerReferencingType::NewTable
5178            }
5179            _ => {
5180                return Ok(None);
5181            }
5182        };
5183
5184        let is_as = self.parse_keyword(Keyword::AS);
5185        let transition_relation_name = self.parse_object_name(false)?;
5186        Ok(Some(TriggerReferencing {
5187            refer_type,
5188            is_as,
5189            transition_relation_name,
5190        }))
5191    }
5192
5193    pub fn parse_trigger_exec_body(&mut self) -> Result<TriggerExecBody, ParserError> {
5194        Ok(TriggerExecBody {
5195            exec_type: match self
5196                .expect_one_of_keywords(&[Keyword::FUNCTION, Keyword::PROCEDURE])?
5197            {
5198                Keyword::FUNCTION => TriggerExecBodyType::Function,
5199                Keyword::PROCEDURE => TriggerExecBodyType::Procedure,
5200                _ => unreachable!(),
5201            },
5202            func_desc: self.parse_function_desc()?,
5203        })
5204    }
5205
5206    pub fn parse_create_macro(
5207        &mut self,
5208        or_replace: bool,
5209        temporary: bool,
5210    ) -> Result<Statement, ParserError> {
5211        if dialect_of!(self is DuckDbDialect |  GenericDialect) {
5212            let name = self.parse_object_name(false)?;
5213            self.expect_token(&Token::LParen)?;
5214            let args = if self.consume_token(&Token::RParen) {
5215                self.prev_token();
5216                None
5217            } else {
5218                Some(self.parse_comma_separated(Parser::parse_macro_arg)?)
5219            };
5220
5221            self.expect_token(&Token::RParen)?;
5222            self.expect_keyword_is(Keyword::AS)?;
5223
5224            Ok(Statement::CreateMacro {
5225                or_replace,
5226                temporary,
5227                name,
5228                args,
5229                definition: if self.parse_keyword(Keyword::TABLE) {
5230                    MacroDefinition::Table(self.parse_query()?)
5231                } else {
5232                    MacroDefinition::Expr(self.parse_expr()?)
5233                },
5234            })
5235        } else {
5236            self.prev_token();
5237            self.expected("an object type after CREATE", self.peek_token())
5238        }
5239    }
5240
5241    fn parse_macro_arg(&mut self) -> Result<MacroArg, ParserError> {
5242        let name = self.parse_identifier()?;
5243
5244        let default_expr =
5245            if self.consume_token(&Token::Assignment) || self.consume_token(&Token::RArrow) {
5246                Some(self.parse_expr()?)
5247            } else {
5248                None
5249            };
5250        Ok(MacroArg { name, default_expr })
5251    }
5252
5253    pub fn parse_create_external_table(
5254        &mut self,
5255        or_replace: bool,
5256    ) -> Result<Statement, ParserError> {
5257        self.expect_keyword_is(Keyword::TABLE)?;
5258        let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
5259        let table_name = self.parse_object_name(false)?;
5260        let (columns, constraints) = self.parse_columns()?;
5261
5262        let hive_distribution = self.parse_hive_distribution()?;
5263        let hive_formats = self.parse_hive_formats()?;
5264
5265        let file_format = if let Some(ff) = &hive_formats.storage {
5266            match ff {
5267                HiveIOFormat::FileFormat { format } => Some(*format),
5268                _ => None,
5269            }
5270        } else {
5271            None
5272        };
5273        let location = hive_formats.location.clone();
5274        let table_properties = self.parse_options(Keyword::TBLPROPERTIES)?;
5275        Ok(CreateTableBuilder::new(table_name)
5276            .columns(columns)
5277            .constraints(constraints)
5278            .hive_distribution(hive_distribution)
5279            .hive_formats(Some(hive_formats))
5280            .table_properties(table_properties)
5281            .or_replace(or_replace)
5282            .if_not_exists(if_not_exists)
5283            .external(true)
5284            .file_format(file_format)
5285            .location(location)
5286            .build())
5287    }
5288
5289    pub fn parse_file_format(&mut self) -> Result<FileFormat, ParserError> {
5290        let next_token = self.next_token();
5291        match &next_token.token {
5292            Token::Word(w) => match w.keyword {
5293                Keyword::AVRO => Ok(FileFormat::AVRO),
5294                Keyword::JSONFILE => Ok(FileFormat::JSONFILE),
5295                Keyword::ORC => Ok(FileFormat::ORC),
5296                Keyword::PARQUET => Ok(FileFormat::PARQUET),
5297                Keyword::RCFILE => Ok(FileFormat::RCFILE),
5298                Keyword::SEQUENCEFILE => Ok(FileFormat::SEQUENCEFILE),
5299                Keyword::TEXTFILE => Ok(FileFormat::TEXTFILE),
5300                _ => self.expected("fileformat", next_token),
5301            },
5302            _ => self.expected("fileformat", next_token),
5303        }
5304    }
5305
5306    pub fn parse_analyze_format(&mut self) -> Result<AnalyzeFormat, ParserError> {
5307        let next_token = self.next_token();
5308        match &next_token.token {
5309            Token::Word(w) => match w.keyword {
5310                Keyword::TEXT => Ok(AnalyzeFormat::TEXT),
5311                Keyword::GRAPHVIZ => Ok(AnalyzeFormat::GRAPHVIZ),
5312                Keyword::JSON => Ok(AnalyzeFormat::JSON),
5313                _ => self.expected("fileformat", next_token),
5314            },
5315            _ => self.expected("fileformat", next_token),
5316        }
5317    }
5318
5319    pub fn parse_create_view(
5320        &mut self,
5321        or_replace: bool,
5322        temporary: bool,
5323        create_view_params: Option<CreateViewParams>,
5324    ) -> Result<Statement, ParserError> {
5325        let materialized = self.parse_keyword(Keyword::MATERIALIZED);
5326        self.expect_keyword_is(Keyword::VIEW)?;
5327        let if_not_exists = dialect_of!(self is BigQueryDialect|SQLiteDialect|GenericDialect)
5328            && self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
5329        // Many dialects support `OR ALTER` right after `CREATE`, but we don't (yet).
5330        // ANSI SQL and Postgres support RECURSIVE here, but we don't support it either.
5331        let allow_unquoted_hyphen = dialect_of!(self is BigQueryDialect);
5332        let name = self.parse_object_name(allow_unquoted_hyphen)?;
5333        let columns = self.parse_view_columns()?;
5334        let mut options = CreateTableOptions::None;
5335        let with_options = self.parse_options(Keyword::WITH)?;
5336        if !with_options.is_empty() {
5337            options = CreateTableOptions::With(with_options);
5338        }
5339
5340        let cluster_by = if self.parse_keyword(Keyword::CLUSTER) {
5341            self.expect_keyword_is(Keyword::BY)?;
5342            self.parse_parenthesized_column_list(Optional, false)?
5343        } else {
5344            vec![]
5345        };
5346
5347        if dialect_of!(self is BigQueryDialect | GenericDialect) {
5348            if let Some(opts) = self.maybe_parse_options(Keyword::OPTIONS)? {
5349                if !opts.is_empty() {
5350                    options = CreateTableOptions::Options(opts);
5351                }
5352            };
5353        }
5354
5355        let to = if dialect_of!(self is ClickHouseDialect | GenericDialect)
5356            && self.parse_keyword(Keyword::TO)
5357        {
5358            Some(self.parse_object_name(false)?)
5359        } else {
5360            None
5361        };
5362
5363        let comment = if dialect_of!(self is SnowflakeDialect | GenericDialect)
5364            && self.parse_keyword(Keyword::COMMENT)
5365        {
5366            self.expect_token(&Token::Eq)?;
5367            let next_token = self.next_token();
5368            match next_token.token {
5369                Token::SingleQuotedString(str) => Some(str),
5370                _ => self.expected("string literal", next_token)?,
5371            }
5372        } else {
5373            None
5374        };
5375
5376        self.expect_keyword_is(Keyword::AS)?;
5377        let query = self.parse_query()?;
5378        // Optional `WITH [ CASCADED | LOCAL ] CHECK OPTION` is widely supported here.
5379
5380        let with_no_schema_binding = dialect_of!(self is RedshiftSqlDialect | GenericDialect)
5381            && self.parse_keywords(&[
5382                Keyword::WITH,
5383                Keyword::NO,
5384                Keyword::SCHEMA,
5385                Keyword::BINDING,
5386            ]);
5387
5388        Ok(Statement::CreateView {
5389            name,
5390            columns,
5391            query,
5392            materialized,
5393            or_replace,
5394            options,
5395            cluster_by,
5396            comment,
5397            with_no_schema_binding,
5398            if_not_exists,
5399            temporary,
5400            to,
5401            params: create_view_params,
5402        })
5403    }
5404
5405    /// Parse optional parameters for the `CREATE VIEW` statement supported by [MySQL].
5406    ///
5407    /// [MySQL]: https://dev.mysql.com/doc/refman/9.1/en/create-view.html
5408    fn parse_create_view_params(&mut self) -> Result<Option<CreateViewParams>, ParserError> {
5409        let algorithm = if self.parse_keyword(Keyword::ALGORITHM) {
5410            self.expect_token(&Token::Eq)?;
5411            Some(
5412                match self.expect_one_of_keywords(&[
5413                    Keyword::UNDEFINED,
5414                    Keyword::MERGE,
5415                    Keyword::TEMPTABLE,
5416                ])? {
5417                    Keyword::UNDEFINED => CreateViewAlgorithm::Undefined,
5418                    Keyword::MERGE => CreateViewAlgorithm::Merge,
5419                    Keyword::TEMPTABLE => CreateViewAlgorithm::TempTable,
5420                    _ => {
5421                        self.prev_token();
5422                        let found = self.next_token();
5423                        return self
5424                            .expected("UNDEFINED or MERGE or TEMPTABLE after ALGORITHM =", found);
5425                    }
5426                },
5427            )
5428        } else {
5429            None
5430        };
5431        let definer = if self.parse_keyword(Keyword::DEFINER) {
5432            self.expect_token(&Token::Eq)?;
5433            Some(self.parse_grantee_name()?)
5434        } else {
5435            None
5436        };
5437        let security = if self.parse_keywords(&[Keyword::SQL, Keyword::SECURITY]) {
5438            Some(
5439                match self.expect_one_of_keywords(&[Keyword::DEFINER, Keyword::INVOKER])? {
5440                    Keyword::DEFINER => CreateViewSecurity::Definer,
5441                    Keyword::INVOKER => CreateViewSecurity::Invoker,
5442                    _ => {
5443                        self.prev_token();
5444                        let found = self.next_token();
5445                        return self.expected("DEFINER or INVOKER after SQL SECURITY", found);
5446                    }
5447                },
5448            )
5449        } else {
5450            None
5451        };
5452        if algorithm.is_some() || definer.is_some() || security.is_some() {
5453            Ok(Some(CreateViewParams {
5454                algorithm,
5455                definer,
5456                security,
5457            }))
5458        } else {
5459            Ok(None)
5460        }
5461    }
5462
5463    pub fn parse_create_role(&mut self) -> Result<Statement, ParserError> {
5464        let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
5465        let names = self.parse_comma_separated(|p| p.parse_object_name(false))?;
5466
5467        let _ = self.parse_keyword(Keyword::WITH); // [ WITH ]
5468
5469        let optional_keywords = if dialect_of!(self is MsSqlDialect) {
5470            vec![Keyword::AUTHORIZATION]
5471        } else if dialect_of!(self is PostgreSqlDialect) {
5472            vec![
5473                Keyword::LOGIN,
5474                Keyword::NOLOGIN,
5475                Keyword::INHERIT,
5476                Keyword::NOINHERIT,
5477                Keyword::BYPASSRLS,
5478                Keyword::NOBYPASSRLS,
5479                Keyword::PASSWORD,
5480                Keyword::CREATEDB,
5481                Keyword::NOCREATEDB,
5482                Keyword::CREATEROLE,
5483                Keyword::NOCREATEROLE,
5484                Keyword::SUPERUSER,
5485                Keyword::NOSUPERUSER,
5486                Keyword::REPLICATION,
5487                Keyword::NOREPLICATION,
5488                Keyword::CONNECTION,
5489                Keyword::VALID,
5490                Keyword::IN,
5491                Keyword::ROLE,
5492                Keyword::ADMIN,
5493                Keyword::USER,
5494            ]
5495        } else {
5496            vec![]
5497        };
5498
5499        // MSSQL
5500        let mut authorization_owner = None;
5501        // Postgres
5502        let mut login = None;
5503        let mut inherit = None;
5504        let mut bypassrls = None;
5505        let mut password = None;
5506        let mut create_db = None;
5507        let mut create_role = None;
5508        let mut superuser = None;
5509        let mut replication = None;
5510        let mut connection_limit = None;
5511        let mut valid_until = None;
5512        let mut in_role = vec![];
5513        let mut in_group = vec![];
5514        let mut role = vec![];
5515        let mut user = vec![];
5516        let mut admin = vec![];
5517
5518        while let Some(keyword) = self.parse_one_of_keywords(&optional_keywords) {
5519            let loc = self
5520                .tokens
5521                .get(self.index - 1)
5522                .map_or(Location { line: 0, column: 0 }, |t| t.span.start);
5523            match keyword {
5524                Keyword::AUTHORIZATION => {
5525                    if authorization_owner.is_some() {
5526                        parser_err!("Found multiple AUTHORIZATION", loc)
5527                    } else {
5528                        authorization_owner = Some(self.parse_object_name(false)?);
5529                        Ok(())
5530                    }
5531                }
5532                Keyword::LOGIN | Keyword::NOLOGIN => {
5533                    if login.is_some() {
5534                        parser_err!("Found multiple LOGIN or NOLOGIN", loc)
5535                    } else {
5536                        login = Some(keyword == Keyword::LOGIN);
5537                        Ok(())
5538                    }
5539                }
5540                Keyword::INHERIT | Keyword::NOINHERIT => {
5541                    if inherit.is_some() {
5542                        parser_err!("Found multiple INHERIT or NOINHERIT", loc)
5543                    } else {
5544                        inherit = Some(keyword == Keyword::INHERIT);
5545                        Ok(())
5546                    }
5547                }
5548                Keyword::BYPASSRLS | Keyword::NOBYPASSRLS => {
5549                    if bypassrls.is_some() {
5550                        parser_err!("Found multiple BYPASSRLS or NOBYPASSRLS", loc)
5551                    } else {
5552                        bypassrls = Some(keyword == Keyword::BYPASSRLS);
5553                        Ok(())
5554                    }
5555                }
5556                Keyword::CREATEDB | Keyword::NOCREATEDB => {
5557                    if create_db.is_some() {
5558                        parser_err!("Found multiple CREATEDB or NOCREATEDB", loc)
5559                    } else {
5560                        create_db = Some(keyword == Keyword::CREATEDB);
5561                        Ok(())
5562                    }
5563                }
5564                Keyword::CREATEROLE | Keyword::NOCREATEROLE => {
5565                    if create_role.is_some() {
5566                        parser_err!("Found multiple CREATEROLE or NOCREATEROLE", loc)
5567                    } else {
5568                        create_role = Some(keyword == Keyword::CREATEROLE);
5569                        Ok(())
5570                    }
5571                }
5572                Keyword::SUPERUSER | Keyword::NOSUPERUSER => {
5573                    if superuser.is_some() {
5574                        parser_err!("Found multiple SUPERUSER or NOSUPERUSER", loc)
5575                    } else {
5576                        superuser = Some(keyword == Keyword::SUPERUSER);
5577                        Ok(())
5578                    }
5579                }
5580                Keyword::REPLICATION | Keyword::NOREPLICATION => {
5581                    if replication.is_some() {
5582                        parser_err!("Found multiple REPLICATION or NOREPLICATION", loc)
5583                    } else {
5584                        replication = Some(keyword == Keyword::REPLICATION);
5585                        Ok(())
5586                    }
5587                }
5588                Keyword::PASSWORD => {
5589                    if password.is_some() {
5590                        parser_err!("Found multiple PASSWORD", loc)
5591                    } else {
5592                        password = if self.parse_keyword(Keyword::NULL) {
5593                            Some(Password::NullPassword)
5594                        } else {
5595                            Some(Password::Password(Expr::Value(self.parse_value()?)))
5596                        };
5597                        Ok(())
5598                    }
5599                }
5600                Keyword::CONNECTION => {
5601                    self.expect_keyword_is(Keyword::LIMIT)?;
5602                    if connection_limit.is_some() {
5603                        parser_err!("Found multiple CONNECTION LIMIT", loc)
5604                    } else {
5605                        connection_limit = Some(Expr::Value(self.parse_number_value()?));
5606                        Ok(())
5607                    }
5608                }
5609                Keyword::VALID => {
5610                    self.expect_keyword_is(Keyword::UNTIL)?;
5611                    if valid_until.is_some() {
5612                        parser_err!("Found multiple VALID UNTIL", loc)
5613                    } else {
5614                        valid_until = Some(Expr::Value(self.parse_value()?));
5615                        Ok(())
5616                    }
5617                }
5618                Keyword::IN => {
5619                    if self.parse_keyword(Keyword::ROLE) {
5620                        if !in_role.is_empty() {
5621                            parser_err!("Found multiple IN ROLE", loc)
5622                        } else {
5623                            in_role = self.parse_comma_separated(|p| p.parse_identifier())?;
5624                            Ok(())
5625                        }
5626                    } else if self.parse_keyword(Keyword::GROUP) {
5627                        if !in_group.is_empty() {
5628                            parser_err!("Found multiple IN GROUP", loc)
5629                        } else {
5630                            in_group = self.parse_comma_separated(|p| p.parse_identifier())?;
5631                            Ok(())
5632                        }
5633                    } else {
5634                        self.expected("ROLE or GROUP after IN", self.peek_token())
5635                    }
5636                }
5637                Keyword::ROLE => {
5638                    if !role.is_empty() {
5639                        parser_err!("Found multiple ROLE", loc)
5640                    } else {
5641                        role = self.parse_comma_separated(|p| p.parse_identifier())?;
5642                        Ok(())
5643                    }
5644                }
5645                Keyword::USER => {
5646                    if !user.is_empty() {
5647                        parser_err!("Found multiple USER", loc)
5648                    } else {
5649                        user = self.parse_comma_separated(|p| p.parse_identifier())?;
5650                        Ok(())
5651                    }
5652                }
5653                Keyword::ADMIN => {
5654                    if !admin.is_empty() {
5655                        parser_err!("Found multiple ADMIN", loc)
5656                    } else {
5657                        admin = self.parse_comma_separated(|p| p.parse_identifier())?;
5658                        Ok(())
5659                    }
5660                }
5661                _ => break,
5662            }?
5663        }
5664
5665        Ok(Statement::CreateRole {
5666            names,
5667            if_not_exists,
5668            login,
5669            inherit,
5670            bypassrls,
5671            password,
5672            create_db,
5673            create_role,
5674            replication,
5675            superuser,
5676            connection_limit,
5677            valid_until,
5678            in_role,
5679            in_group,
5680            role,
5681            user,
5682            admin,
5683            authorization_owner,
5684        })
5685    }
5686
5687    pub fn parse_owner(&mut self) -> Result<Owner, ParserError> {
5688        let owner = match self.parse_one_of_keywords(&[Keyword::CURRENT_USER, Keyword::CURRENT_ROLE, Keyword::SESSION_USER]) {
5689            Some(Keyword::CURRENT_USER) => Owner::CurrentUser,
5690            Some(Keyword::CURRENT_ROLE) => Owner::CurrentRole,
5691            Some(Keyword::SESSION_USER) => Owner::SessionUser,
5692            Some(_) => unreachable!(),
5693            None => {
5694                match self.parse_identifier() {
5695                    Ok(ident) => Owner::Ident(ident),
5696                    Err(e) => {
5697                        return Err(ParserError::ParserError(format!("Expected: CURRENT_USER, CURRENT_ROLE, SESSION_USER or identifier after OWNER TO. {e}")))
5698                    }
5699                }
5700            }
5701        };
5702        Ok(owner)
5703    }
5704
5705    /// ```sql
5706    ///     CREATE POLICY name ON table_name [ AS { PERMISSIVE | RESTRICTIVE } ]
5707    ///     [ FOR { ALL | SELECT | INSERT | UPDATE | DELETE } ]
5708    ///     [ TO { role_name | PUBLIC | CURRENT_USER | CURRENT_ROLE | SESSION_USER } [, ...] ]
5709    ///     [ USING ( using_expression ) ]
5710    ///     [ WITH CHECK ( with_check_expression ) ]
5711    /// ```
5712    ///
5713    /// [PostgreSQL Documentation](https://www.postgresql.org/docs/current/sql-createpolicy.html)
5714    pub fn parse_create_policy(&mut self) -> Result<Statement, ParserError> {
5715        let name = self.parse_identifier()?;
5716        self.expect_keyword_is(Keyword::ON)?;
5717        let table_name = self.parse_object_name(false)?;
5718
5719        let policy_type = if self.parse_keyword(Keyword::AS) {
5720            let keyword =
5721                self.expect_one_of_keywords(&[Keyword::PERMISSIVE, Keyword::RESTRICTIVE])?;
5722            Some(match keyword {
5723                Keyword::PERMISSIVE => CreatePolicyType::Permissive,
5724                Keyword::RESTRICTIVE => CreatePolicyType::Restrictive,
5725                _ => unreachable!(),
5726            })
5727        } else {
5728            None
5729        };
5730
5731        let command = if self.parse_keyword(Keyword::FOR) {
5732            let keyword = self.expect_one_of_keywords(&[
5733                Keyword::ALL,
5734                Keyword::SELECT,
5735                Keyword::INSERT,
5736                Keyword::UPDATE,
5737                Keyword::DELETE,
5738            ])?;
5739            Some(match keyword {
5740                Keyword::ALL => CreatePolicyCommand::All,
5741                Keyword::SELECT => CreatePolicyCommand::Select,
5742                Keyword::INSERT => CreatePolicyCommand::Insert,
5743                Keyword::UPDATE => CreatePolicyCommand::Update,
5744                Keyword::DELETE => CreatePolicyCommand::Delete,
5745                _ => unreachable!(),
5746            })
5747        } else {
5748            None
5749        };
5750
5751        let to = if self.parse_keyword(Keyword::TO) {
5752            Some(self.parse_comma_separated(|p| p.parse_owner())?)
5753        } else {
5754            None
5755        };
5756
5757        let using = if self.parse_keyword(Keyword::USING) {
5758            self.expect_token(&Token::LParen)?;
5759            let expr = self.parse_expr()?;
5760            self.expect_token(&Token::RParen)?;
5761            Some(expr)
5762        } else {
5763            None
5764        };
5765
5766        let with_check = if self.parse_keywords(&[Keyword::WITH, Keyword::CHECK]) {
5767            self.expect_token(&Token::LParen)?;
5768            let expr = self.parse_expr()?;
5769            self.expect_token(&Token::RParen)?;
5770            Some(expr)
5771        } else {
5772            None
5773        };
5774
5775        Ok(CreatePolicy {
5776            name,
5777            table_name,
5778            policy_type,
5779            command,
5780            to,
5781            using,
5782            with_check,
5783        })
5784    }
5785
5786    /// ```sql
5787    /// CREATE CONNECTOR [IF NOT EXISTS] connector_name
5788    /// [TYPE datasource_type]
5789    /// [URL datasource_url]
5790    /// [COMMENT connector_comment]
5791    /// [WITH DCPROPERTIES(property_name=property_value, ...)]
5792    /// ```
5793    ///
5794    /// [Hive Documentation](https://cwiki.apache.org/confluence/pages/viewpage.action?pageId=27362034#LanguageManualDDL-CreateDataConnectorCreateConnector)
5795    pub fn parse_create_connector(&mut self) -> Result<Statement, ParserError> {
5796        let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
5797        let name = self.parse_identifier()?;
5798
5799        let connector_type = if self.parse_keyword(Keyword::TYPE) {
5800            Some(self.parse_literal_string()?)
5801        } else {
5802            None
5803        };
5804
5805        let url = if self.parse_keyword(Keyword::URL) {
5806            Some(self.parse_literal_string()?)
5807        } else {
5808            None
5809        };
5810
5811        let comment = self.parse_optional_inline_comment()?;
5812
5813        let with_dcproperties =
5814            match self.parse_options_with_keywords(&[Keyword::WITH, Keyword::DCPROPERTIES])? {
5815                properties if !properties.is_empty() => Some(properties),
5816                _ => None,
5817            };
5818
5819        Ok(Statement::CreateConnector(CreateConnector {
5820            name,
5821            if_not_exists,
5822            connector_type,
5823            url,
5824            comment,
5825            with_dcproperties,
5826        }))
5827    }
5828
5829    pub fn parse_drop(&mut self) -> Result<Statement, ParserError> {
5830        // MySQL dialect supports `TEMPORARY`
5831        let temporary = dialect_of!(self is MySqlDialect | GenericDialect | DuckDbDialect)
5832            && self.parse_keyword(Keyword::TEMPORARY);
5833        let persistent = dialect_of!(self is DuckDbDialect)
5834            && self.parse_one_of_keywords(&[Keyword::PERSISTENT]).is_some();
5835
5836        let object_type = if self.parse_keyword(Keyword::TABLE) {
5837            ObjectType::Table
5838        } else if self.parse_keyword(Keyword::VIEW) {
5839            ObjectType::View
5840        } else if self.parse_keyword(Keyword::INDEX) {
5841            ObjectType::Index
5842        } else if self.parse_keyword(Keyword::ROLE) {
5843            ObjectType::Role
5844        } else if self.parse_keyword(Keyword::SCHEMA) {
5845            ObjectType::Schema
5846        } else if self.parse_keyword(Keyword::DATABASE) {
5847            ObjectType::Database
5848        } else if self.parse_keyword(Keyword::SEQUENCE) {
5849            ObjectType::Sequence
5850        } else if self.parse_keyword(Keyword::STAGE) {
5851            ObjectType::Stage
5852        } else if self.parse_keyword(Keyword::TYPE) {
5853            ObjectType::Type
5854        } else if self.parse_keyword(Keyword::FUNCTION) {
5855            return self.parse_drop_function();
5856        } else if self.parse_keyword(Keyword::POLICY) {
5857            return self.parse_drop_policy();
5858        } else if self.parse_keyword(Keyword::CONNECTOR) {
5859            return self.parse_drop_connector();
5860        } else if self.parse_keyword(Keyword::PROCEDURE) {
5861            return self.parse_drop_procedure();
5862        } else if self.parse_keyword(Keyword::SECRET) {
5863            return self.parse_drop_secret(temporary, persistent);
5864        } else if self.parse_keyword(Keyword::TRIGGER) {
5865            return self.parse_drop_trigger();
5866        } else if self.parse_keyword(Keyword::EXTENSION) {
5867            return self.parse_drop_extension();
5868        } else {
5869            return self.expected(
5870                "CONNECTOR, DATABASE, EXTENSION, FUNCTION, INDEX, POLICY, PROCEDURE, ROLE, SCHEMA, SECRET, SEQUENCE, STAGE, TABLE, TRIGGER, TYPE, or VIEW after DROP",
5871                self.peek_token(),
5872            );
5873        };
5874        // Many dialects support the non-standard `IF EXISTS` clause and allow
5875        // specifying multiple objects to delete in a single statement
5876        let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
5877        let names = self.parse_comma_separated(|p| p.parse_object_name(false))?;
5878
5879        let loc = self.peek_token().span.start;
5880        let cascade = self.parse_keyword(Keyword::CASCADE);
5881        let restrict = self.parse_keyword(Keyword::RESTRICT);
5882        let purge = self.parse_keyword(Keyword::PURGE);
5883        if cascade && restrict {
5884            return parser_err!("Cannot specify both CASCADE and RESTRICT in DROP", loc);
5885        }
5886        if object_type == ObjectType::Role && (cascade || restrict || purge) {
5887            return parser_err!(
5888                "Cannot specify CASCADE, RESTRICT, or PURGE in DROP ROLE",
5889                loc
5890            );
5891        }
5892        Ok(Statement::Drop {
5893            object_type,
5894            if_exists,
5895            names,
5896            cascade,
5897            restrict,
5898            purge,
5899            temporary,
5900        })
5901    }
5902
5903    fn parse_optional_drop_behavior(&mut self) -> Option<DropBehavior> {
5904        match self.parse_one_of_keywords(&[Keyword::CASCADE, Keyword::RESTRICT]) {
5905            Some(Keyword::CASCADE) => Some(DropBehavior::Cascade),
5906            Some(Keyword::RESTRICT) => Some(DropBehavior::Restrict),
5907            _ => None,
5908        }
5909    }
5910
5911    /// ```sql
5912    /// DROP FUNCTION [ IF EXISTS ] name [ ( [ [ argmode ] [ argname ] argtype [, ...] ] ) ] [, ...]
5913    /// [ CASCADE | RESTRICT ]
5914    /// ```
5915    fn parse_drop_function(&mut self) -> Result<Statement, ParserError> {
5916        let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
5917        let func_desc = self.parse_comma_separated(Parser::parse_function_desc)?;
5918        let drop_behavior = self.parse_optional_drop_behavior();
5919        Ok(Statement::DropFunction {
5920            if_exists,
5921            func_desc,
5922            drop_behavior,
5923        })
5924    }
5925
5926    /// ```sql
5927    /// DROP POLICY [ IF EXISTS ] name ON table_name [ CASCADE | RESTRICT ]
5928    /// ```
5929    ///
5930    /// [PostgreSQL Documentation](https://www.postgresql.org/docs/current/sql-droppolicy.html)
5931    fn parse_drop_policy(&mut self) -> Result<Statement, ParserError> {
5932        let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
5933        let name = self.parse_identifier()?;
5934        self.expect_keyword_is(Keyword::ON)?;
5935        let table_name = self.parse_object_name(false)?;
5936        let drop_behavior = self.parse_optional_drop_behavior();
5937        Ok(Statement::DropPolicy {
5938            if_exists,
5939            name,
5940            table_name,
5941            drop_behavior,
5942        })
5943    }
5944    /// ```sql
5945    /// DROP CONNECTOR [IF EXISTS] name
5946    /// ```
5947    ///
5948    /// See [Hive](https://cwiki.apache.org/confluence/pages/viewpage.action?pageId=27362034#LanguageManualDDL-DropConnector)
5949    fn parse_drop_connector(&mut self) -> Result<Statement, ParserError> {
5950        let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
5951        let name = self.parse_identifier()?;
5952        Ok(Statement::DropConnector { if_exists, name })
5953    }
5954
5955    /// ```sql
5956    /// DROP PROCEDURE [ IF EXISTS ] name [ ( [ [ argmode ] [ argname ] argtype [, ...] ] ) ] [, ...]
5957    /// [ CASCADE | RESTRICT ]
5958    /// ```
5959    fn parse_drop_procedure(&mut self) -> Result<Statement, ParserError> {
5960        let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
5961        let proc_desc = self.parse_comma_separated(Parser::parse_function_desc)?;
5962        let drop_behavior = self.parse_optional_drop_behavior();
5963        Ok(Statement::DropProcedure {
5964            if_exists,
5965            proc_desc,
5966            drop_behavior,
5967        })
5968    }
5969
5970    fn parse_function_desc(&mut self) -> Result<FunctionDesc, ParserError> {
5971        let name = self.parse_object_name(false)?;
5972
5973        let args = if self.consume_token(&Token::LParen) {
5974            if self.consume_token(&Token::RParen) {
5975                None
5976            } else {
5977                let args = self.parse_comma_separated(Parser::parse_function_arg)?;
5978                self.expect_token(&Token::RParen)?;
5979                Some(args)
5980            }
5981        } else {
5982            None
5983        };
5984
5985        Ok(FunctionDesc { name, args })
5986    }
5987
5988    /// See [DuckDB Docs](https://duckdb.org/docs/sql/statements/create_secret.html) for more details.
5989    fn parse_drop_secret(
5990        &mut self,
5991        temporary: bool,
5992        persistent: bool,
5993    ) -> Result<Statement, ParserError> {
5994        let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
5995        let name = self.parse_identifier()?;
5996        let storage_specifier = if self.parse_keyword(Keyword::FROM) {
5997            self.parse_identifier().ok()
5998        } else {
5999            None
6000        };
6001        let temp = match (temporary, persistent) {
6002            (true, false) => Some(true),
6003            (false, true) => Some(false),
6004            (false, false) => None,
6005            _ => self.expected("TEMPORARY or PERSISTENT", self.peek_token())?,
6006        };
6007
6008        Ok(Statement::DropSecret {
6009            if_exists,
6010            temporary: temp,
6011            name,
6012            storage_specifier,
6013        })
6014    }
6015
6016    /// Parse a `DECLARE` statement.
6017    ///
6018    /// ```sql
6019    /// DECLARE name [ BINARY ] [ ASENSITIVE | INSENSITIVE ] [ [ NO ] SCROLL ]
6020    ///     CURSOR [ { WITH | WITHOUT } HOLD ] FOR query
6021    /// ```
6022    ///
6023    /// The syntax can vary significantly between warehouses. See the grammar
6024    /// on the warehouse specific function in such cases.
6025    pub fn parse_declare(&mut self) -> Result<Statement, ParserError> {
6026        if dialect_of!(self is BigQueryDialect) {
6027            return self.parse_big_query_declare();
6028        }
6029        if dialect_of!(self is SnowflakeDialect) {
6030            return self.parse_snowflake_declare();
6031        }
6032        if dialect_of!(self is MsSqlDialect) {
6033            return self.parse_mssql_declare();
6034        }
6035
6036        let name = self.parse_identifier()?;
6037
6038        let binary = Some(self.parse_keyword(Keyword::BINARY));
6039        let sensitive = if self.parse_keyword(Keyword::INSENSITIVE) {
6040            Some(true)
6041        } else if self.parse_keyword(Keyword::ASENSITIVE) {
6042            Some(false)
6043        } else {
6044            None
6045        };
6046        let scroll = if self.parse_keyword(Keyword::SCROLL) {
6047            Some(true)
6048        } else if self.parse_keywords(&[Keyword::NO, Keyword::SCROLL]) {
6049            Some(false)
6050        } else {
6051            None
6052        };
6053
6054        self.expect_keyword_is(Keyword::CURSOR)?;
6055        let declare_type = Some(DeclareType::Cursor);
6056
6057        let hold = match self.parse_one_of_keywords(&[Keyword::WITH, Keyword::WITHOUT]) {
6058            Some(keyword) => {
6059                self.expect_keyword_is(Keyword::HOLD)?;
6060
6061                match keyword {
6062                    Keyword::WITH => Some(true),
6063                    Keyword::WITHOUT => Some(false),
6064                    _ => unreachable!(),
6065                }
6066            }
6067            None => None,
6068        };
6069
6070        self.expect_keyword_is(Keyword::FOR)?;
6071
6072        let query = Some(self.parse_query()?);
6073
6074        Ok(Statement::Declare {
6075            stmts: vec![Declare {
6076                names: vec![name],
6077                data_type: None,
6078                assignment: None,
6079                declare_type,
6080                binary,
6081                sensitive,
6082                scroll,
6083                hold,
6084                for_query: query,
6085            }],
6086        })
6087    }
6088
6089    /// Parse a [BigQuery] `DECLARE` statement.
6090    ///
6091    /// Syntax:
6092    /// ```text
6093    /// DECLARE variable_name[, ...] [{ <variable_type> | <DEFAULT expression> }];
6094    /// ```
6095    /// [BigQuery]: https://cloud.google.com/bigquery/docs/reference/standard-sql/procedural-language#declare
6096    pub fn parse_big_query_declare(&mut self) -> Result<Statement, ParserError> {
6097        let names = self.parse_comma_separated(Parser::parse_identifier)?;
6098
6099        let data_type = match self.peek_token().token {
6100            Token::Word(w) if w.keyword == Keyword::DEFAULT => None,
6101            _ => Some(self.parse_data_type()?),
6102        };
6103
6104        let expr = if data_type.is_some() {
6105            if self.parse_keyword(Keyword::DEFAULT) {
6106                Some(self.parse_expr()?)
6107            } else {
6108                None
6109            }
6110        } else {
6111            // If no variable type - default expression must be specified, per BQ docs.
6112            // i.e `DECLARE foo;` is invalid.
6113            self.expect_keyword_is(Keyword::DEFAULT)?;
6114            Some(self.parse_expr()?)
6115        };
6116
6117        Ok(Statement::Declare {
6118            stmts: vec![Declare {
6119                names,
6120                data_type,
6121                assignment: expr.map(|expr| DeclareAssignment::Default(Box::new(expr))),
6122                declare_type: None,
6123                binary: None,
6124                sensitive: None,
6125                scroll: None,
6126                hold: None,
6127                for_query: None,
6128            }],
6129        })
6130    }
6131
6132    /// Parse a [Snowflake] `DECLARE` statement.
6133    ///
6134    /// Syntax:
6135    /// ```text
6136    /// DECLARE
6137    ///   [{ <variable_declaration>
6138    ///      | <cursor_declaration>
6139    ///      | <resultset_declaration>
6140    ///      | <exception_declaration> }; ... ]
6141    ///
6142    /// <variable_declaration>
6143    /// <variable_name> [<type>] [ { DEFAULT | := } <expression>]
6144    ///
6145    /// <cursor_declaration>
6146    /// <cursor_name> CURSOR FOR <query>
6147    ///
6148    /// <resultset_declaration>
6149    /// <resultset_name> RESULTSET [ { DEFAULT | := } ( <query> ) ] ;
6150    ///
6151    /// <exception_declaration>
6152    /// <exception_name> EXCEPTION [ ( <exception_number> , '<exception_message>' ) ] ;
6153    /// ```
6154    ///
6155    /// [Snowflake]: https://docs.snowflake.com/en/sql-reference/snowflake-scripting/declare
6156    pub fn parse_snowflake_declare(&mut self) -> Result<Statement, ParserError> {
6157        let mut stmts = vec![];
6158        loop {
6159            let name = self.parse_identifier()?;
6160            let (declare_type, for_query, assigned_expr, data_type) =
6161                if self.parse_keyword(Keyword::CURSOR) {
6162                    self.expect_keyword_is(Keyword::FOR)?;
6163                    match self.peek_token().token {
6164                        Token::Word(w) if w.keyword == Keyword::SELECT => (
6165                            Some(DeclareType::Cursor),
6166                            Some(self.parse_query()?),
6167                            None,
6168                            None,
6169                        ),
6170                        _ => (
6171                            Some(DeclareType::Cursor),
6172                            None,
6173                            Some(DeclareAssignment::For(Box::new(self.parse_expr()?))),
6174                            None,
6175                        ),
6176                    }
6177                } else if self.parse_keyword(Keyword::RESULTSET) {
6178                    let assigned_expr = if self.peek_token().token != Token::SemiColon {
6179                        self.parse_snowflake_variable_declaration_expression()?
6180                    } else {
6181                        // Nothing more to do. The statement has no further parameters.
6182                        None
6183                    };
6184
6185                    (Some(DeclareType::ResultSet), None, assigned_expr, None)
6186                } else if self.parse_keyword(Keyword::EXCEPTION) {
6187                    let assigned_expr = if self.peek_token().token == Token::LParen {
6188                        Some(DeclareAssignment::Expr(Box::new(self.parse_expr()?)))
6189                    } else {
6190                        // Nothing more to do. The statement has no further parameters.
6191                        None
6192                    };
6193
6194                    (Some(DeclareType::Exception), None, assigned_expr, None)
6195                } else {
6196                    // Without an explicit keyword, the only valid option is variable declaration.
6197                    let (assigned_expr, data_type) = if let Some(assigned_expr) =
6198                        self.parse_snowflake_variable_declaration_expression()?
6199                    {
6200                        (Some(assigned_expr), None)
6201                    } else if let Token::Word(_) = self.peek_token().token {
6202                        let data_type = self.parse_data_type()?;
6203                        (
6204                            self.parse_snowflake_variable_declaration_expression()?,
6205                            Some(data_type),
6206                        )
6207                    } else {
6208                        (None, None)
6209                    };
6210                    (None, None, assigned_expr, data_type)
6211                };
6212            let stmt = Declare {
6213                names: vec![name],
6214                data_type,
6215                assignment: assigned_expr,
6216                declare_type,
6217                binary: None,
6218                sensitive: None,
6219                scroll: None,
6220                hold: None,
6221                for_query,
6222            };
6223
6224            stmts.push(stmt);
6225            if self.consume_token(&Token::SemiColon) {
6226                match self.peek_token().token {
6227                    Token::Word(w)
6228                        if ALL_KEYWORDS
6229                            .binary_search(&w.value.to_uppercase().as_str())
6230                            .is_err() =>
6231                    {
6232                        // Not a keyword - start of a new declaration.
6233                        continue;
6234                    }
6235                    _ => {
6236                        // Put back the semicolon, this is the end of the DECLARE statement.
6237                        self.prev_token();
6238                    }
6239                }
6240            }
6241
6242            break;
6243        }
6244
6245        Ok(Statement::Declare { stmts })
6246    }
6247
6248    /// Parse a [MsSql] `DECLARE` statement.
6249    ///
6250    /// Syntax:
6251    /// ```text
6252    /// DECLARE
6253    // {
6254    //   { @local_variable [AS] data_type [ = value ] }
6255    //   | { @cursor_variable_name CURSOR }
6256    // } [ ,...n ]
6257    /// ```
6258    /// [MsSql]: https://learn.microsoft.com/en-us/sql/t-sql/language-elements/declare-local-variable-transact-sql?view=sql-server-ver16
6259    pub fn parse_mssql_declare(&mut self) -> Result<Statement, ParserError> {
6260        let stmts = self.parse_comma_separated(Parser::parse_mssql_declare_stmt)?;
6261
6262        Ok(Statement::Declare { stmts })
6263    }
6264
6265    /// Parse the body of a [MsSql] `DECLARE`statement.
6266    ///
6267    /// Syntax:
6268    /// ```text
6269    // {
6270    //   { @local_variable [AS] data_type [ = value ] }
6271    //   | { @cursor_variable_name CURSOR }
6272    // } [ ,...n ]
6273    /// ```
6274    /// [MsSql]: https://learn.microsoft.com/en-us/sql/t-sql/language-elements/declare-local-variable-transact-sql?view=sql-server-ver16
6275    pub fn parse_mssql_declare_stmt(&mut self) -> Result<Declare, ParserError> {
6276        let name = {
6277            let ident = self.parse_identifier()?;
6278            if !ident.value.starts_with('@') {
6279                Err(ParserError::TokenizerError(
6280                    "Invalid MsSql variable declaration.".to_string(),
6281                ))
6282            } else {
6283                Ok(ident)
6284            }
6285        }?;
6286
6287        let (declare_type, data_type) = match self.peek_token().token {
6288            Token::Word(w) => match w.keyword {
6289                Keyword::CURSOR => {
6290                    self.next_token();
6291                    (Some(DeclareType::Cursor), None)
6292                }
6293                Keyword::AS => {
6294                    self.next_token();
6295                    (None, Some(self.parse_data_type()?))
6296                }
6297                _ => (None, Some(self.parse_data_type()?)),
6298            },
6299            _ => (None, Some(self.parse_data_type()?)),
6300        };
6301
6302        let assignment = self.parse_mssql_variable_declaration_expression()?;
6303
6304        Ok(Declare {
6305            names: vec![name],
6306            data_type,
6307            assignment,
6308            declare_type,
6309            binary: None,
6310            sensitive: None,
6311            scroll: None,
6312            hold: None,
6313            for_query: None,
6314        })
6315    }
6316
6317    /// Parses the assigned expression in a variable declaration.
6318    ///
6319    /// Syntax:
6320    /// ```text
6321    /// [ { DEFAULT | := } <expression>]
6322    /// ```
6323    /// <https://docs.snowflake.com/en/sql-reference/snowflake-scripting/declare#variable-declaration-syntax>
6324    pub fn parse_snowflake_variable_declaration_expression(
6325        &mut self,
6326    ) -> Result<Option<DeclareAssignment>, ParserError> {
6327        Ok(match self.peek_token().token {
6328            Token::Word(w) if w.keyword == Keyword::DEFAULT => {
6329                self.next_token(); // Skip `DEFAULT`
6330                Some(DeclareAssignment::Default(Box::new(self.parse_expr()?)))
6331            }
6332            Token::Assignment => {
6333                self.next_token(); // Skip `:=`
6334                Some(DeclareAssignment::DuckAssignment(Box::new(
6335                    self.parse_expr()?,
6336                )))
6337            }
6338            _ => None,
6339        })
6340    }
6341
6342    /// Parses the assigned expression in a variable declaration.
6343    ///
6344    /// Syntax:
6345    /// ```text
6346    /// [ = <expression>]
6347    /// ```
6348    pub fn parse_mssql_variable_declaration_expression(
6349        &mut self,
6350    ) -> Result<Option<DeclareAssignment>, ParserError> {
6351        Ok(match self.peek_token().token {
6352            Token::Eq => {
6353                self.next_token(); // Skip `=`
6354                Some(DeclareAssignment::MsSqlAssignment(Box::new(
6355                    self.parse_expr()?,
6356                )))
6357            }
6358            _ => None,
6359        })
6360    }
6361
6362    // FETCH [ direction { FROM | IN } ] cursor INTO target;
6363    pub fn parse_fetch_statement(&mut self) -> Result<Statement, ParserError> {
6364        let direction = if self.parse_keyword(Keyword::NEXT) {
6365            FetchDirection::Next
6366        } else if self.parse_keyword(Keyword::PRIOR) {
6367            FetchDirection::Prior
6368        } else if self.parse_keyword(Keyword::FIRST) {
6369            FetchDirection::First
6370        } else if self.parse_keyword(Keyword::LAST) {
6371            FetchDirection::Last
6372        } else if self.parse_keyword(Keyword::ABSOLUTE) {
6373            FetchDirection::Absolute {
6374                limit: self.parse_number_value()?.value,
6375            }
6376        } else if self.parse_keyword(Keyword::RELATIVE) {
6377            FetchDirection::Relative {
6378                limit: self.parse_number_value()?.value,
6379            }
6380        } else if self.parse_keyword(Keyword::FORWARD) {
6381            if self.parse_keyword(Keyword::ALL) {
6382                FetchDirection::ForwardAll
6383            } else {
6384                FetchDirection::Forward {
6385                    // TODO: Support optional
6386                    limit: Some(self.parse_number_value()?.value),
6387                }
6388            }
6389        } else if self.parse_keyword(Keyword::BACKWARD) {
6390            if self.parse_keyword(Keyword::ALL) {
6391                FetchDirection::BackwardAll
6392            } else {
6393                FetchDirection::Backward {
6394                    // TODO: Support optional
6395                    limit: Some(self.parse_number_value()?.value),
6396                }
6397            }
6398        } else if self.parse_keyword(Keyword::ALL) {
6399            FetchDirection::All
6400        } else {
6401            FetchDirection::Count {
6402                limit: self.parse_number_value()?.value,
6403            }
6404        };
6405
6406        self.expect_one_of_keywords(&[Keyword::FROM, Keyword::IN])?;
6407
6408        let name = self.parse_identifier()?;
6409
6410        let into = if self.parse_keyword(Keyword::INTO) {
6411            Some(self.parse_object_name(false)?)
6412        } else {
6413            None
6414        };
6415
6416        Ok(Statement::Fetch {
6417            name,
6418            direction,
6419            into,
6420        })
6421    }
6422
6423    pub fn parse_discard(&mut self) -> Result<Statement, ParserError> {
6424        let object_type = if self.parse_keyword(Keyword::ALL) {
6425            DiscardObject::ALL
6426        } else if self.parse_keyword(Keyword::PLANS) {
6427            DiscardObject::PLANS
6428        } else if self.parse_keyword(Keyword::SEQUENCES) {
6429            DiscardObject::SEQUENCES
6430        } else if self.parse_keyword(Keyword::TEMP) || self.parse_keyword(Keyword::TEMPORARY) {
6431            DiscardObject::TEMP
6432        } else {
6433            return self.expected(
6434                "ALL, PLANS, SEQUENCES, TEMP or TEMPORARY after DISCARD",
6435                self.peek_token(),
6436            );
6437        };
6438        Ok(Statement::Discard { object_type })
6439    }
6440
6441    pub fn parse_create_index(&mut self, unique: bool) -> Result<Statement, ParserError> {
6442        let concurrently = self.parse_keyword(Keyword::CONCURRENTLY);
6443        let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
6444        let index_name = if if_not_exists || !self.parse_keyword(Keyword::ON) {
6445            let index_name = self.parse_object_name(false)?;
6446            self.expect_keyword_is(Keyword::ON)?;
6447            Some(index_name)
6448        } else {
6449            None
6450        };
6451        let table_name = self.parse_object_name(false)?;
6452        let using = if self.parse_keyword(Keyword::USING) {
6453            Some(self.parse_identifier()?)
6454        } else {
6455            None
6456        };
6457        self.expect_token(&Token::LParen)?;
6458        let columns = self.parse_comma_separated(Parser::parse_order_by_expr)?;
6459        self.expect_token(&Token::RParen)?;
6460
6461        let include = if self.parse_keyword(Keyword::INCLUDE) {
6462            self.expect_token(&Token::LParen)?;
6463            let columns = self.parse_comma_separated(|p| p.parse_identifier())?;
6464            self.expect_token(&Token::RParen)?;
6465            columns
6466        } else {
6467            vec![]
6468        };
6469
6470        let nulls_distinct = if self.parse_keyword(Keyword::NULLS) {
6471            let not = self.parse_keyword(Keyword::NOT);
6472            self.expect_keyword_is(Keyword::DISTINCT)?;
6473            Some(!not)
6474        } else {
6475            None
6476        };
6477
6478        let with = if self.dialect.supports_create_index_with_clause()
6479            && self.parse_keyword(Keyword::WITH)
6480        {
6481            self.expect_token(&Token::LParen)?;
6482            let with_params = self.parse_comma_separated(Parser::parse_expr)?;
6483            self.expect_token(&Token::RParen)?;
6484            with_params
6485        } else {
6486            Vec::new()
6487        };
6488
6489        let predicate = if self.parse_keyword(Keyword::WHERE) {
6490            Some(self.parse_expr()?)
6491        } else {
6492            None
6493        };
6494
6495        Ok(Statement::CreateIndex(CreateIndex {
6496            name: index_name,
6497            table_name,
6498            using,
6499            columns,
6500            unique,
6501            concurrently,
6502            if_not_exists,
6503            include,
6504            nulls_distinct,
6505            with,
6506            predicate,
6507        }))
6508    }
6509
6510    pub fn parse_create_extension(&mut self) -> Result<Statement, ParserError> {
6511        let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
6512        let name = self.parse_identifier()?;
6513
6514        let (schema, version, cascade) = if self.parse_keyword(Keyword::WITH) {
6515            let schema = if self.parse_keyword(Keyword::SCHEMA) {
6516                Some(self.parse_identifier()?)
6517            } else {
6518                None
6519            };
6520
6521            let version = if self.parse_keyword(Keyword::VERSION) {
6522                Some(self.parse_identifier()?)
6523            } else {
6524                None
6525            };
6526
6527            let cascade = self.parse_keyword(Keyword::CASCADE);
6528
6529            (schema, version, cascade)
6530        } else {
6531            (None, None, false)
6532        };
6533
6534        Ok(Statement::CreateExtension {
6535            name,
6536            if_not_exists,
6537            schema,
6538            version,
6539            cascade,
6540        })
6541    }
6542
6543    /// Parse a PostgreSQL-specific [Statement::DropExtension] statement.
6544    pub fn parse_drop_extension(&mut self) -> Result<Statement, ParserError> {
6545        let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
6546        let names = self.parse_comma_separated(|p| p.parse_identifier())?;
6547        let cascade_or_restrict =
6548            self.parse_one_of_keywords(&[Keyword::CASCADE, Keyword::RESTRICT]);
6549        Ok(Statement::DropExtension {
6550            names,
6551            if_exists,
6552            cascade_or_restrict: cascade_or_restrict
6553                .map(|k| match k {
6554                    Keyword::CASCADE => Ok(ReferentialAction::Cascade),
6555                    Keyword::RESTRICT => Ok(ReferentialAction::Restrict),
6556                    _ => self.expected("CASCADE or RESTRICT", self.peek_token()),
6557                })
6558                .transpose()?,
6559        })
6560    }
6561
6562    //TODO: Implement parsing for Skewed
6563    pub fn parse_hive_distribution(&mut self) -> Result<HiveDistributionStyle, ParserError> {
6564        if self.parse_keywords(&[Keyword::PARTITIONED, Keyword::BY]) {
6565            self.expect_token(&Token::LParen)?;
6566            let columns = self.parse_comma_separated(Parser::parse_column_def)?;
6567            self.expect_token(&Token::RParen)?;
6568            Ok(HiveDistributionStyle::PARTITIONED { columns })
6569        } else {
6570            Ok(HiveDistributionStyle::NONE)
6571        }
6572    }
6573
6574    pub fn parse_hive_formats(&mut self) -> Result<HiveFormat, ParserError> {
6575        let mut hive_format = HiveFormat::default();
6576        loop {
6577            match self.parse_one_of_keywords(&[
6578                Keyword::ROW,
6579                Keyword::STORED,
6580                Keyword::LOCATION,
6581                Keyword::WITH,
6582            ]) {
6583                Some(Keyword::ROW) => {
6584                    hive_format.row_format = Some(self.parse_row_format()?);
6585                }
6586                Some(Keyword::STORED) => {
6587                    self.expect_keyword_is(Keyword::AS)?;
6588                    if self.parse_keyword(Keyword::INPUTFORMAT) {
6589                        let input_format = self.parse_expr()?;
6590                        self.expect_keyword_is(Keyword::OUTPUTFORMAT)?;
6591                        let output_format = self.parse_expr()?;
6592                        hive_format.storage = Some(HiveIOFormat::IOF {
6593                            input_format,
6594                            output_format,
6595                        });
6596                    } else {
6597                        let format = self.parse_file_format()?;
6598                        hive_format.storage = Some(HiveIOFormat::FileFormat { format });
6599                    }
6600                }
6601                Some(Keyword::LOCATION) => {
6602                    hive_format.location = Some(self.parse_literal_string()?);
6603                }
6604                Some(Keyword::WITH) => {
6605                    self.prev_token();
6606                    let properties = self
6607                        .parse_options_with_keywords(&[Keyword::WITH, Keyword::SERDEPROPERTIES])?;
6608                    if !properties.is_empty() {
6609                        hive_format.serde_properties = Some(properties);
6610                    } else {
6611                        break;
6612                    }
6613                }
6614                None => break,
6615                _ => break,
6616            }
6617        }
6618
6619        Ok(hive_format)
6620    }
6621
6622    pub fn parse_row_format(&mut self) -> Result<HiveRowFormat, ParserError> {
6623        self.expect_keyword_is(Keyword::FORMAT)?;
6624        match self.parse_one_of_keywords(&[Keyword::SERDE, Keyword::DELIMITED]) {
6625            Some(Keyword::SERDE) => {
6626                let class = self.parse_literal_string()?;
6627                Ok(HiveRowFormat::SERDE { class })
6628            }
6629            _ => {
6630                let mut row_delimiters = vec![];
6631
6632                loop {
6633                    match self.parse_one_of_keywords(&[
6634                        Keyword::FIELDS,
6635                        Keyword::COLLECTION,
6636                        Keyword::MAP,
6637                        Keyword::LINES,
6638                        Keyword::NULL,
6639                    ]) {
6640                        Some(Keyword::FIELDS) => {
6641                            if self.parse_keywords(&[Keyword::TERMINATED, Keyword::BY]) {
6642                                row_delimiters.push(HiveRowDelimiter {
6643                                    delimiter: HiveDelimiter::FieldsTerminatedBy,
6644                                    char: self.parse_identifier()?,
6645                                });
6646
6647                                if self.parse_keywords(&[Keyword::ESCAPED, Keyword::BY]) {
6648                                    row_delimiters.push(HiveRowDelimiter {
6649                                        delimiter: HiveDelimiter::FieldsEscapedBy,
6650                                        char: self.parse_identifier()?,
6651                                    });
6652                                }
6653                            } else {
6654                                break;
6655                            }
6656                        }
6657                        Some(Keyword::COLLECTION) => {
6658                            if self.parse_keywords(&[
6659                                Keyword::ITEMS,
6660                                Keyword::TERMINATED,
6661                                Keyword::BY,
6662                            ]) {
6663                                row_delimiters.push(HiveRowDelimiter {
6664                                    delimiter: HiveDelimiter::CollectionItemsTerminatedBy,
6665                                    char: self.parse_identifier()?,
6666                                });
6667                            } else {
6668                                break;
6669                            }
6670                        }
6671                        Some(Keyword::MAP) => {
6672                            if self.parse_keywords(&[
6673                                Keyword::KEYS,
6674                                Keyword::TERMINATED,
6675                                Keyword::BY,
6676                            ]) {
6677                                row_delimiters.push(HiveRowDelimiter {
6678                                    delimiter: HiveDelimiter::MapKeysTerminatedBy,
6679                                    char: self.parse_identifier()?,
6680                                });
6681                            } else {
6682                                break;
6683                            }
6684                        }
6685                        Some(Keyword::LINES) => {
6686                            if self.parse_keywords(&[Keyword::TERMINATED, Keyword::BY]) {
6687                                row_delimiters.push(HiveRowDelimiter {
6688                                    delimiter: HiveDelimiter::LinesTerminatedBy,
6689                                    char: self.parse_identifier()?,
6690                                });
6691                            } else {
6692                                break;
6693                            }
6694                        }
6695                        Some(Keyword::NULL) => {
6696                            if self.parse_keywords(&[Keyword::DEFINED, Keyword::AS]) {
6697                                row_delimiters.push(HiveRowDelimiter {
6698                                    delimiter: HiveDelimiter::NullDefinedAs,
6699                                    char: self.parse_identifier()?,
6700                                });
6701                            } else {
6702                                break;
6703                            }
6704                        }
6705                        _ => {
6706                            break;
6707                        }
6708                    }
6709                }
6710
6711                Ok(HiveRowFormat::DELIMITED {
6712                    delimiters: row_delimiters,
6713                })
6714            }
6715        }
6716    }
6717
6718    fn parse_optional_on_cluster(&mut self) -> Result<Option<Ident>, ParserError> {
6719        if self.parse_keywords(&[Keyword::ON, Keyword::CLUSTER]) {
6720            Ok(Some(self.parse_identifier()?))
6721        } else {
6722            Ok(None)
6723        }
6724    }
6725
6726    pub fn parse_create_table(
6727        &mut self,
6728        or_replace: bool,
6729        temporary: bool,
6730        global: Option<bool>,
6731        transient: bool,
6732    ) -> Result<Statement, ParserError> {
6733        let allow_unquoted_hyphen = dialect_of!(self is BigQueryDialect);
6734        let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
6735        let table_name = self.parse_object_name(allow_unquoted_hyphen)?;
6736
6737        // Clickhouse has `ON CLUSTER 'cluster'` syntax for DDLs
6738        let on_cluster = self.parse_optional_on_cluster()?;
6739
6740        let like = if self.parse_keyword(Keyword::LIKE) || self.parse_keyword(Keyword::ILIKE) {
6741            self.parse_object_name(allow_unquoted_hyphen).ok()
6742        } else {
6743            None
6744        };
6745
6746        let clone = if self.parse_keyword(Keyword::CLONE) {
6747            self.parse_object_name(allow_unquoted_hyphen).ok()
6748        } else {
6749            None
6750        };
6751
6752        // parse optional column list (schema)
6753        let (columns, constraints) = self.parse_columns()?;
6754        let mut comment = if dialect_of!(self is HiveDialect)
6755            && self.parse_keyword(Keyword::COMMENT)
6756        {
6757            let next_token = self.next_token();
6758            match next_token.token {
6759                Token::SingleQuotedString(str) => Some(CommentDef::AfterColumnDefsWithoutEq(str)),
6760                _ => self.expected("comment", next_token)?,
6761            }
6762        } else {
6763            None
6764        };
6765
6766        // SQLite supports `WITHOUT ROWID` at the end of `CREATE TABLE`
6767        let without_rowid = self.parse_keywords(&[Keyword::WITHOUT, Keyword::ROWID]);
6768
6769        let hive_distribution = self.parse_hive_distribution()?;
6770        let clustered_by = self.parse_optional_clustered_by()?;
6771        let hive_formats = self.parse_hive_formats()?;
6772        // PostgreSQL supports `WITH ( options )`, before `AS`
6773        let with_options = self.parse_options(Keyword::WITH)?;
6774        let table_properties = self.parse_options(Keyword::TBLPROPERTIES)?;
6775
6776        let engine = if self.parse_keyword(Keyword::ENGINE) {
6777            self.expect_token(&Token::Eq)?;
6778            let next_token = self.next_token();
6779            match next_token.token {
6780                Token::Word(w) => {
6781                    let name = w.value;
6782                    let parameters = if self.peek_token() == Token::LParen {
6783                        Some(self.parse_parenthesized_identifiers()?)
6784                    } else {
6785                        None
6786                    };
6787                    Some(TableEngine { name, parameters })
6788                }
6789                _ => self.expected("identifier", next_token)?,
6790            }
6791        } else {
6792            None
6793        };
6794
6795        let auto_increment_offset = if self.parse_keyword(Keyword::AUTO_INCREMENT) {
6796            let _ = self.consume_token(&Token::Eq);
6797            let next_token = self.next_token();
6798            match next_token.token {
6799                Token::Number(s, _) => Some(Self::parse::<u32>(s, next_token.span.start)?),
6800                _ => self.expected("literal int", next_token)?,
6801            }
6802        } else {
6803            None
6804        };
6805
6806        // ClickHouse supports `PRIMARY KEY`, before `ORDER BY`
6807        // https://clickhouse.com/docs/en/sql-reference/statements/create/table#primary-key
6808        let primary_key = if dialect_of!(self is ClickHouseDialect | GenericDialect)
6809            && self.parse_keywords(&[Keyword::PRIMARY, Keyword::KEY])
6810        {
6811            Some(Box::new(self.parse_expr()?))
6812        } else {
6813            None
6814        };
6815
6816        let order_by = if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
6817            if self.consume_token(&Token::LParen) {
6818                let columns = if self.peek_token() != Token::RParen {
6819                    self.parse_comma_separated(|p| p.parse_expr())?
6820                } else {
6821                    vec![]
6822                };
6823                self.expect_token(&Token::RParen)?;
6824                Some(OneOrManyWithParens::Many(columns))
6825            } else {
6826                Some(OneOrManyWithParens::One(self.parse_expr()?))
6827            }
6828        } else {
6829            None
6830        };
6831
6832        let create_table_config = self.parse_optional_create_table_config()?;
6833
6834        let default_charset = if self.parse_keywords(&[Keyword::DEFAULT, Keyword::CHARSET]) {
6835            self.expect_token(&Token::Eq)?;
6836            let next_token = self.next_token();
6837            match next_token.token {
6838                Token::Word(w) => Some(w.value),
6839                _ => self.expected("identifier", next_token)?,
6840            }
6841        } else {
6842            None
6843        };
6844
6845        let collation = if self.parse_keywords(&[Keyword::COLLATE]) {
6846            self.expect_token(&Token::Eq)?;
6847            let next_token = self.next_token();
6848            match next_token.token {
6849                Token::Word(w) => Some(w.value),
6850                _ => self.expected("identifier", next_token)?,
6851            }
6852        } else {
6853            None
6854        };
6855
6856        let on_commit = if self.parse_keywords(&[Keyword::ON, Keyword::COMMIT]) {
6857            Some(self.parse_create_table_on_commit()?)
6858        } else {
6859            None
6860        };
6861
6862        let strict = self.parse_keyword(Keyword::STRICT);
6863
6864        // Excludes Hive dialect here since it has been handled after table column definitions.
6865        if !dialect_of!(self is HiveDialect) && self.parse_keyword(Keyword::COMMENT) {
6866            // rewind the COMMENT keyword
6867            self.prev_token();
6868            comment = self.parse_optional_inline_comment()?
6869        };
6870
6871        // Parse optional `AS ( query )`
6872        let query = if self.parse_keyword(Keyword::AS) {
6873            Some(self.parse_query()?)
6874        } else if self.dialect.supports_create_table_select() && self.parse_keyword(Keyword::SELECT)
6875        {
6876            // rewind the SELECT keyword
6877            self.prev_token();
6878            Some(self.parse_query()?)
6879        } else {
6880            None
6881        };
6882
6883        Ok(CreateTableBuilder::new(table_name)
6884            .temporary(temporary)
6885            .columns(columns)
6886            .constraints(constraints)
6887            .with_options(with_options)
6888            .table_properties(table_properties)
6889            .or_replace(or_replace)
6890            .if_not_exists(if_not_exists)
6891            .transient(transient)
6892            .hive_distribution(hive_distribution)
6893            .hive_formats(Some(hive_formats))
6894            .global(global)
6895            .query(query)
6896            .without_rowid(without_rowid)
6897            .like(like)
6898            .clone_clause(clone)
6899            .engine(engine)
6900            .comment(comment)
6901            .auto_increment_offset(auto_increment_offset)
6902            .order_by(order_by)
6903            .default_charset(default_charset)
6904            .collation(collation)
6905            .on_commit(on_commit)
6906            .on_cluster(on_cluster)
6907            .clustered_by(clustered_by)
6908            .partition_by(create_table_config.partition_by)
6909            .cluster_by(create_table_config.cluster_by)
6910            .options(create_table_config.options)
6911            .primary_key(primary_key)
6912            .strict(strict)
6913            .build())
6914    }
6915
6916    pub(crate) fn parse_create_table_on_commit(&mut self) -> Result<OnCommit, ParserError> {
6917        if self.parse_keywords(&[Keyword::DELETE, Keyword::ROWS]) {
6918            Ok(OnCommit::DeleteRows)
6919        } else if self.parse_keywords(&[Keyword::PRESERVE, Keyword::ROWS]) {
6920            Ok(OnCommit::PreserveRows)
6921        } else if self.parse_keywords(&[Keyword::DROP]) {
6922            Ok(OnCommit::Drop)
6923        } else {
6924            parser_err!(
6925                "Expecting DELETE ROWS, PRESERVE ROWS or DROP",
6926                self.peek_token()
6927            )
6928        }
6929    }
6930
6931    /// Parse configuration like partitioning, clustering information during the table creation.
6932    ///
6933    /// [BigQuery](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#syntax_2)
6934    /// [PostgreSQL](https://www.postgresql.org/docs/current/ddl-partitioning.html)
6935    fn parse_optional_create_table_config(
6936        &mut self,
6937    ) -> Result<CreateTableConfiguration, ParserError> {
6938        let partition_by = if dialect_of!(self is BigQueryDialect | PostgreSqlDialect | GenericDialect)
6939            && self.parse_keywords(&[Keyword::PARTITION, Keyword::BY])
6940        {
6941            Some(Box::new(self.parse_expr()?))
6942        } else {
6943            None
6944        };
6945
6946        let mut cluster_by = None;
6947        let mut options = None;
6948        if dialect_of!(self is BigQueryDialect | GenericDialect) {
6949            if self.parse_keywords(&[Keyword::CLUSTER, Keyword::BY]) {
6950                cluster_by = Some(WrappedCollection::NoWrapping(
6951                    self.parse_comma_separated(|p| p.parse_identifier())?,
6952                ));
6953            };
6954
6955            if let Token::Word(word) = self.peek_token().token {
6956                if word.keyword == Keyword::OPTIONS {
6957                    options = Some(self.parse_options(Keyword::OPTIONS)?);
6958                }
6959            };
6960        }
6961
6962        Ok(CreateTableConfiguration {
6963            partition_by,
6964            cluster_by,
6965            options,
6966        })
6967    }
6968
6969    pub fn parse_optional_inline_comment(&mut self) -> Result<Option<CommentDef>, ParserError> {
6970        let comment = if self.parse_keyword(Keyword::COMMENT) {
6971            let has_eq = self.consume_token(&Token::Eq);
6972            let next_token = self.next_token();
6973            match next_token.token {
6974                Token::SingleQuotedString(str) => Some(if has_eq {
6975                    CommentDef::WithEq(str)
6976                } else {
6977                    CommentDef::WithoutEq(str)
6978                }),
6979                _ => self.expected("comment", next_token)?,
6980            }
6981        } else {
6982            None
6983        };
6984        Ok(comment)
6985    }
6986
6987    pub fn parse_optional_procedure_parameters(
6988        &mut self,
6989    ) -> Result<Option<Vec<ProcedureParam>>, ParserError> {
6990        let mut params = vec![];
6991        if !self.consume_token(&Token::LParen) || self.consume_token(&Token::RParen) {
6992            return Ok(Some(params));
6993        }
6994        loop {
6995            if let Token::Word(_) = self.peek_token().token {
6996                params.push(self.parse_procedure_param()?)
6997            }
6998            let comma = self.consume_token(&Token::Comma);
6999            if self.consume_token(&Token::RParen) {
7000                // allow a trailing comma, even though it's not in standard
7001                break;
7002            } else if !comma {
7003                return self.expected("',' or ')' after parameter definition", self.peek_token());
7004            }
7005        }
7006        Ok(Some(params))
7007    }
7008
7009    pub fn parse_columns(&mut self) -> Result<(Vec<ColumnDef>, Vec<TableConstraint>), ParserError> {
7010        let mut columns = vec![];
7011        let mut constraints = vec![];
7012        if !self.consume_token(&Token::LParen) || self.consume_token(&Token::RParen) {
7013            return Ok((columns, constraints));
7014        }
7015
7016        loop {
7017            if let Some(constraint) = self.parse_optional_table_constraint()? {
7018                constraints.push(constraint);
7019            } else if let Token::Word(_) = self.peek_token().token {
7020                columns.push(self.parse_column_def()?);
7021            } else {
7022                return self.expected("column name or constraint definition", self.peek_token());
7023            }
7024
7025            let comma = self.consume_token(&Token::Comma);
7026            let rparen = self.peek_token().token == Token::RParen;
7027
7028            if !comma && !rparen {
7029                return self.expected("',' or ')' after column definition", self.peek_token());
7030            };
7031
7032            if rparen
7033                && (!comma
7034                    || self.dialect.supports_column_definition_trailing_commas()
7035                    || self.options.trailing_commas)
7036            {
7037                let _ = self.consume_token(&Token::RParen);
7038                break;
7039            }
7040        }
7041
7042        Ok((columns, constraints))
7043    }
7044
7045    pub fn parse_procedure_param(&mut self) -> Result<ProcedureParam, ParserError> {
7046        let name = self.parse_identifier()?;
7047        let data_type = self.parse_data_type()?;
7048        Ok(ProcedureParam { name, data_type })
7049    }
7050
7051    pub fn parse_column_def(&mut self) -> Result<ColumnDef, ParserError> {
7052        let name = self.parse_identifier()?;
7053        let data_type = if self.is_column_type_sqlite_unspecified() {
7054            DataType::Unspecified
7055        } else {
7056            self.parse_data_type()?
7057        };
7058        let mut options = vec![];
7059        loop {
7060            if self.parse_keyword(Keyword::CONSTRAINT) {
7061                let name = Some(self.parse_identifier()?);
7062                if let Some(option) = self.parse_optional_column_option()? {
7063                    options.push(ColumnOptionDef { name, option });
7064                } else {
7065                    return self.expected(
7066                        "constraint details after CONSTRAINT <name>",
7067                        self.peek_token(),
7068                    );
7069                }
7070            } else if let Some(option) = self.parse_optional_column_option()? {
7071                options.push(ColumnOptionDef { name: None, option });
7072            } else {
7073                break;
7074            };
7075        }
7076        Ok(ColumnDef {
7077            name,
7078            data_type,
7079            options,
7080        })
7081    }
7082
7083    fn is_column_type_sqlite_unspecified(&mut self) -> bool {
7084        if dialect_of!(self is SQLiteDialect) {
7085            match self.peek_token().token {
7086                Token::Word(word) => matches!(
7087                    word.keyword,
7088                    Keyword::CONSTRAINT
7089                        | Keyword::PRIMARY
7090                        | Keyword::NOT
7091                        | Keyword::UNIQUE
7092                        | Keyword::CHECK
7093                        | Keyword::DEFAULT
7094                        | Keyword::COLLATE
7095                        | Keyword::REFERENCES
7096                        | Keyword::GENERATED
7097                        | Keyword::AS
7098                ),
7099                _ => true, // e.g. comma immediately after column name
7100            }
7101        } else {
7102            false
7103        }
7104    }
7105
7106    pub fn parse_optional_column_option(&mut self) -> Result<Option<ColumnOption>, ParserError> {
7107        if let Some(option) = self.dialect.parse_column_option(self)? {
7108            return option;
7109        }
7110
7111        if self.parse_keywords(&[Keyword::CHARACTER, Keyword::SET]) {
7112            Ok(Some(ColumnOption::CharacterSet(
7113                self.parse_object_name(false)?,
7114            )))
7115        } else if self.parse_keywords(&[Keyword::COLLATE]) {
7116            Ok(Some(ColumnOption::Collation(
7117                self.parse_object_name(false)?,
7118            )))
7119        } else if self.parse_keywords(&[Keyword::NOT, Keyword::NULL]) {
7120            Ok(Some(ColumnOption::NotNull))
7121        } else if self.parse_keywords(&[Keyword::COMMENT]) {
7122            let next_token = self.next_token();
7123            match next_token.token {
7124                Token::SingleQuotedString(value, ..) => Ok(Some(ColumnOption::Comment(value))),
7125                _ => self.expected("string", next_token),
7126            }
7127        } else if self.parse_keyword(Keyword::NULL) {
7128            Ok(Some(ColumnOption::Null))
7129        } else if self.parse_keyword(Keyword::DEFAULT) {
7130            Ok(Some(ColumnOption::Default(self.parse_expr()?)))
7131        } else if dialect_of!(self is ClickHouseDialect| GenericDialect)
7132            && self.parse_keyword(Keyword::MATERIALIZED)
7133        {
7134            Ok(Some(ColumnOption::Materialized(self.parse_expr()?)))
7135        } else if dialect_of!(self is ClickHouseDialect| GenericDialect)
7136            && self.parse_keyword(Keyword::ALIAS)
7137        {
7138            Ok(Some(ColumnOption::Alias(self.parse_expr()?)))
7139        } else if dialect_of!(self is ClickHouseDialect| GenericDialect)
7140            && self.parse_keyword(Keyword::EPHEMERAL)
7141        {
7142            // The expression is optional for the EPHEMERAL syntax, so we need to check
7143            // if the column definition has remaining tokens before parsing the expression.
7144            if matches!(self.peek_token().token, Token::Comma | Token::RParen) {
7145                Ok(Some(ColumnOption::Ephemeral(None)))
7146            } else {
7147                Ok(Some(ColumnOption::Ephemeral(Some(self.parse_expr()?))))
7148            }
7149        } else if self.parse_keywords(&[Keyword::PRIMARY, Keyword::KEY]) {
7150            let characteristics = self.parse_constraint_characteristics()?;
7151            Ok(Some(ColumnOption::Unique {
7152                is_primary: true,
7153                characteristics,
7154            }))
7155        } else if self.parse_keyword(Keyword::UNIQUE) {
7156            let characteristics = self.parse_constraint_characteristics()?;
7157            Ok(Some(ColumnOption::Unique {
7158                is_primary: false,
7159                characteristics,
7160            }))
7161        } else if self.parse_keyword(Keyword::REFERENCES) {
7162            let foreign_table = self.parse_object_name(false)?;
7163            // PostgreSQL allows omitting the column list and
7164            // uses the primary key column of the foreign table by default
7165            let referred_columns = self.parse_parenthesized_column_list(Optional, false)?;
7166            let mut on_delete = None;
7167            let mut on_update = None;
7168            loop {
7169                if on_delete.is_none() && self.parse_keywords(&[Keyword::ON, Keyword::DELETE]) {
7170                    on_delete = Some(self.parse_referential_action()?);
7171                } else if on_update.is_none()
7172                    && self.parse_keywords(&[Keyword::ON, Keyword::UPDATE])
7173                {
7174                    on_update = Some(self.parse_referential_action()?);
7175                } else {
7176                    break;
7177                }
7178            }
7179            let characteristics = self.parse_constraint_characteristics()?;
7180
7181            Ok(Some(ColumnOption::ForeignKey {
7182                foreign_table,
7183                referred_columns,
7184                on_delete,
7185                on_update,
7186                characteristics,
7187            }))
7188        } else if self.parse_keyword(Keyword::CHECK) {
7189            self.expect_token(&Token::LParen)?;
7190            let expr = self.parse_expr()?;
7191            self.expect_token(&Token::RParen)?;
7192            Ok(Some(ColumnOption::Check(expr)))
7193        } else if self.parse_keyword(Keyword::AUTO_INCREMENT)
7194            && dialect_of!(self is MySqlDialect | GenericDialect)
7195        {
7196            // Support AUTO_INCREMENT for MySQL
7197            Ok(Some(ColumnOption::DialectSpecific(vec![
7198                Token::make_keyword("AUTO_INCREMENT"),
7199            ])))
7200        } else if self.parse_keyword(Keyword::AUTOINCREMENT)
7201            && dialect_of!(self is SQLiteDialect |  GenericDialect)
7202        {
7203            // Support AUTOINCREMENT for SQLite
7204            Ok(Some(ColumnOption::DialectSpecific(vec![
7205                Token::make_keyword("AUTOINCREMENT"),
7206            ])))
7207        } else if self.parse_keyword(Keyword::ASC)
7208            && self.dialect.supports_asc_desc_in_column_definition()
7209        {
7210            // Support ASC for SQLite
7211            Ok(Some(ColumnOption::DialectSpecific(vec![
7212                Token::make_keyword("ASC"),
7213            ])))
7214        } else if self.parse_keyword(Keyword::DESC)
7215            && self.dialect.supports_asc_desc_in_column_definition()
7216        {
7217            // Support DESC for SQLite
7218            Ok(Some(ColumnOption::DialectSpecific(vec![
7219                Token::make_keyword("DESC"),
7220            ])))
7221        } else if self.parse_keywords(&[Keyword::ON, Keyword::UPDATE])
7222            && dialect_of!(self is MySqlDialect | GenericDialect)
7223        {
7224            let expr = self.parse_expr()?;
7225            Ok(Some(ColumnOption::OnUpdate(expr)))
7226        } else if self.parse_keyword(Keyword::GENERATED) {
7227            self.parse_optional_column_option_generated()
7228        } else if dialect_of!(self is BigQueryDialect | GenericDialect)
7229            && self.parse_keyword(Keyword::OPTIONS)
7230        {
7231            self.prev_token();
7232            Ok(Some(ColumnOption::Options(
7233                self.parse_options(Keyword::OPTIONS)?,
7234            )))
7235        } else if self.parse_keyword(Keyword::AS)
7236            && dialect_of!(self is MySqlDialect | SQLiteDialect | DuckDbDialect | GenericDialect)
7237        {
7238            self.parse_optional_column_option_as()
7239        } else if self.parse_keyword(Keyword::IDENTITY)
7240            && dialect_of!(self is MsSqlDialect | GenericDialect)
7241        {
7242            let parameters = if self.consume_token(&Token::LParen) {
7243                let seed = self.parse_number()?;
7244                self.expect_token(&Token::Comma)?;
7245                let increment = self.parse_number()?;
7246                self.expect_token(&Token::RParen)?;
7247
7248                Some(IdentityPropertyFormatKind::FunctionCall(
7249                    IdentityParameters { seed, increment },
7250                ))
7251            } else {
7252                None
7253            };
7254            Ok(Some(ColumnOption::Identity(
7255                IdentityPropertyKind::Identity(IdentityProperty {
7256                    parameters,
7257                    order: None,
7258                }),
7259            )))
7260        } else if dialect_of!(self is SQLiteDialect | GenericDialect)
7261            && self.parse_keywords(&[Keyword::ON, Keyword::CONFLICT])
7262        {
7263            // Support ON CONFLICT for SQLite
7264            Ok(Some(ColumnOption::OnConflict(
7265                self.expect_one_of_keywords(&[
7266                    Keyword::ROLLBACK,
7267                    Keyword::ABORT,
7268                    Keyword::FAIL,
7269                    Keyword::IGNORE,
7270                    Keyword::REPLACE,
7271                ])?,
7272            )))
7273        } else {
7274            Ok(None)
7275        }
7276    }
7277
7278    pub(crate) fn parse_tag(&mut self) -> Result<Tag, ParserError> {
7279        let name = self.parse_identifier()?;
7280        self.expect_token(&Token::Eq)?;
7281        let value = self.parse_literal_string()?;
7282
7283        Ok(Tag::new(name, value))
7284    }
7285
7286    fn parse_optional_column_option_generated(
7287        &mut self,
7288    ) -> Result<Option<ColumnOption>, ParserError> {
7289        if self.parse_keywords(&[Keyword::ALWAYS, Keyword::AS, Keyword::IDENTITY]) {
7290            let mut sequence_options = vec![];
7291            if self.expect_token(&Token::LParen).is_ok() {
7292                sequence_options = self.parse_create_sequence_options()?;
7293                self.expect_token(&Token::RParen)?;
7294            }
7295            Ok(Some(ColumnOption::Generated {
7296                generated_as: GeneratedAs::Always,
7297                sequence_options: Some(sequence_options),
7298                generation_expr: None,
7299                generation_expr_mode: None,
7300                generated_keyword: true,
7301            }))
7302        } else if self.parse_keywords(&[
7303            Keyword::BY,
7304            Keyword::DEFAULT,
7305            Keyword::AS,
7306            Keyword::IDENTITY,
7307        ]) {
7308            let mut sequence_options = vec![];
7309            if self.expect_token(&Token::LParen).is_ok() {
7310                sequence_options = self.parse_create_sequence_options()?;
7311                self.expect_token(&Token::RParen)?;
7312            }
7313            Ok(Some(ColumnOption::Generated {
7314                generated_as: GeneratedAs::ByDefault,
7315                sequence_options: Some(sequence_options),
7316                generation_expr: None,
7317                generation_expr_mode: None,
7318                generated_keyword: true,
7319            }))
7320        } else if self.parse_keywords(&[Keyword::ALWAYS, Keyword::AS]) {
7321            if self.expect_token(&Token::LParen).is_ok() {
7322                let expr = self.parse_expr()?;
7323                self.expect_token(&Token::RParen)?;
7324                let (gen_as, expr_mode) = if self.parse_keywords(&[Keyword::STORED]) {
7325                    Ok((
7326                        GeneratedAs::ExpStored,
7327                        Some(GeneratedExpressionMode::Stored),
7328                    ))
7329                } else if dialect_of!(self is PostgreSqlDialect) {
7330                    // Postgres' AS IDENTITY branches are above, this one needs STORED
7331                    self.expected("STORED", self.peek_token())
7332                } else if self.parse_keywords(&[Keyword::VIRTUAL]) {
7333                    Ok((GeneratedAs::Always, Some(GeneratedExpressionMode::Virtual)))
7334                } else {
7335                    Ok((GeneratedAs::Always, None))
7336                }?;
7337
7338                Ok(Some(ColumnOption::Generated {
7339                    generated_as: gen_as,
7340                    sequence_options: None,
7341                    generation_expr: Some(expr),
7342                    generation_expr_mode: expr_mode,
7343                    generated_keyword: true,
7344                }))
7345            } else {
7346                Ok(None)
7347            }
7348        } else {
7349            Ok(None)
7350        }
7351    }
7352
7353    fn parse_optional_column_option_as(&mut self) -> Result<Option<ColumnOption>, ParserError> {
7354        // Some DBs allow 'AS (expr)', shorthand for GENERATED ALWAYS AS
7355        self.expect_token(&Token::LParen)?;
7356        let expr = self.parse_expr()?;
7357        self.expect_token(&Token::RParen)?;
7358
7359        let (gen_as, expr_mode) = if self.parse_keywords(&[Keyword::STORED]) {
7360            (
7361                GeneratedAs::ExpStored,
7362                Some(GeneratedExpressionMode::Stored),
7363            )
7364        } else if self.parse_keywords(&[Keyword::VIRTUAL]) {
7365            (GeneratedAs::Always, Some(GeneratedExpressionMode::Virtual))
7366        } else {
7367            (GeneratedAs::Always, None)
7368        };
7369
7370        Ok(Some(ColumnOption::Generated {
7371            generated_as: gen_as,
7372            sequence_options: None,
7373            generation_expr: Some(expr),
7374            generation_expr_mode: expr_mode,
7375            generated_keyword: false,
7376        }))
7377    }
7378
7379    pub fn parse_optional_clustered_by(&mut self) -> Result<Option<ClusteredBy>, ParserError> {
7380        let clustered_by = if dialect_of!(self is HiveDialect|GenericDialect)
7381            && self.parse_keywords(&[Keyword::CLUSTERED, Keyword::BY])
7382        {
7383            let columns = self.parse_parenthesized_column_list(Mandatory, false)?;
7384
7385            let sorted_by = if self.parse_keywords(&[Keyword::SORTED, Keyword::BY]) {
7386                self.expect_token(&Token::LParen)?;
7387                let sorted_by_columns = self.parse_comma_separated(|p| p.parse_order_by_expr())?;
7388                self.expect_token(&Token::RParen)?;
7389                Some(sorted_by_columns)
7390            } else {
7391                None
7392            };
7393
7394            self.expect_keyword_is(Keyword::INTO)?;
7395            let num_buckets = self.parse_number_value()?.value;
7396            self.expect_keyword_is(Keyword::BUCKETS)?;
7397            Some(ClusteredBy {
7398                columns,
7399                sorted_by,
7400                num_buckets,
7401            })
7402        } else {
7403            None
7404        };
7405        Ok(clustered_by)
7406    }
7407
7408    pub fn parse_referential_action(&mut self) -> Result<ReferentialAction, ParserError> {
7409        if self.parse_keyword(Keyword::RESTRICT) {
7410            Ok(ReferentialAction::Restrict)
7411        } else if self.parse_keyword(Keyword::CASCADE) {
7412            Ok(ReferentialAction::Cascade)
7413        } else if self.parse_keywords(&[Keyword::SET, Keyword::NULL]) {
7414            Ok(ReferentialAction::SetNull)
7415        } else if self.parse_keywords(&[Keyword::NO, Keyword::ACTION]) {
7416            Ok(ReferentialAction::NoAction)
7417        } else if self.parse_keywords(&[Keyword::SET, Keyword::DEFAULT]) {
7418            Ok(ReferentialAction::SetDefault)
7419        } else {
7420            self.expected(
7421                "one of RESTRICT, CASCADE, SET NULL, NO ACTION or SET DEFAULT",
7422                self.peek_token(),
7423            )
7424        }
7425    }
7426
7427    pub fn parse_constraint_characteristics(
7428        &mut self,
7429    ) -> Result<Option<ConstraintCharacteristics>, ParserError> {
7430        let mut cc = ConstraintCharacteristics::default();
7431
7432        loop {
7433            if cc.deferrable.is_none() && self.parse_keywords(&[Keyword::NOT, Keyword::DEFERRABLE])
7434            {
7435                cc.deferrable = Some(false);
7436            } else if cc.deferrable.is_none() && self.parse_keyword(Keyword::DEFERRABLE) {
7437                cc.deferrable = Some(true);
7438            } else if cc.initially.is_none() && self.parse_keyword(Keyword::INITIALLY) {
7439                if self.parse_keyword(Keyword::DEFERRED) {
7440                    cc.initially = Some(DeferrableInitial::Deferred);
7441                } else if self.parse_keyword(Keyword::IMMEDIATE) {
7442                    cc.initially = Some(DeferrableInitial::Immediate);
7443                } else {
7444                    self.expected("one of DEFERRED or IMMEDIATE", self.peek_token())?;
7445                }
7446            } else if cc.enforced.is_none() && self.parse_keyword(Keyword::ENFORCED) {
7447                cc.enforced = Some(true);
7448            } else if cc.enforced.is_none()
7449                && self.parse_keywords(&[Keyword::NOT, Keyword::ENFORCED])
7450            {
7451                cc.enforced = Some(false);
7452            } else {
7453                break;
7454            }
7455        }
7456
7457        if cc.deferrable.is_some() || cc.initially.is_some() || cc.enforced.is_some() {
7458            Ok(Some(cc))
7459        } else {
7460            Ok(None)
7461        }
7462    }
7463
7464    pub fn parse_optional_table_constraint(
7465        &mut self,
7466    ) -> Result<Option<TableConstraint>, ParserError> {
7467        let name = if self.parse_keyword(Keyword::CONSTRAINT) {
7468            Some(self.parse_identifier()?)
7469        } else {
7470            None
7471        };
7472
7473        let next_token = self.next_token();
7474        match next_token.token {
7475            Token::Word(w) if w.keyword == Keyword::UNIQUE => {
7476                let index_type_display = self.parse_index_type_display();
7477                if !dialect_of!(self is GenericDialect | MySqlDialect)
7478                    && !index_type_display.is_none()
7479                {
7480                    return self
7481                        .expected("`index_name` or `(column_name [, ...])`", self.peek_token());
7482                }
7483
7484                let nulls_distinct = self.parse_optional_nulls_distinct()?;
7485
7486                // optional index name
7487                let index_name = self.parse_optional_indent()?;
7488                let index_type = self.parse_optional_using_then_index_type()?;
7489
7490                let columns = self.parse_parenthesized_column_list(Mandatory, false)?;
7491                let index_options = self.parse_index_options()?;
7492                let characteristics = self.parse_constraint_characteristics()?;
7493                Ok(Some(TableConstraint::Unique {
7494                    name,
7495                    index_name,
7496                    index_type_display,
7497                    index_type,
7498                    columns,
7499                    index_options,
7500                    characteristics,
7501                    nulls_distinct,
7502                }))
7503            }
7504            Token::Word(w) if w.keyword == Keyword::PRIMARY => {
7505                // after `PRIMARY` always stay `KEY`
7506                self.expect_keyword_is(Keyword::KEY)?;
7507
7508                // optional index name
7509                let index_name = self.parse_optional_indent()?;
7510                let index_type = self.parse_optional_using_then_index_type()?;
7511
7512                let columns = self.parse_parenthesized_column_list(Mandatory, false)?;
7513                let index_options = self.parse_index_options()?;
7514                let characteristics = self.parse_constraint_characteristics()?;
7515                Ok(Some(TableConstraint::PrimaryKey {
7516                    name,
7517                    index_name,
7518                    index_type,
7519                    columns,
7520                    index_options,
7521                    characteristics,
7522                }))
7523            }
7524            Token::Word(w) if w.keyword == Keyword::FOREIGN => {
7525                self.expect_keyword_is(Keyword::KEY)?;
7526                let columns = self.parse_parenthesized_column_list(Mandatory, false)?;
7527                self.expect_keyword_is(Keyword::REFERENCES)?;
7528                let foreign_table = self.parse_object_name(false)?;
7529                let referred_columns = self.parse_parenthesized_column_list(Optional, false)?;
7530                let mut on_delete = None;
7531                let mut on_update = None;
7532                loop {
7533                    if on_delete.is_none() && self.parse_keywords(&[Keyword::ON, Keyword::DELETE]) {
7534                        on_delete = Some(self.parse_referential_action()?);
7535                    } else if on_update.is_none()
7536                        && self.parse_keywords(&[Keyword::ON, Keyword::UPDATE])
7537                    {
7538                        on_update = Some(self.parse_referential_action()?);
7539                    } else {
7540                        break;
7541                    }
7542                }
7543
7544                let characteristics = self.parse_constraint_characteristics()?;
7545
7546                Ok(Some(TableConstraint::ForeignKey {
7547                    name,
7548                    columns,
7549                    foreign_table,
7550                    referred_columns,
7551                    on_delete,
7552                    on_update,
7553                    characteristics,
7554                }))
7555            }
7556            Token::Word(w) if w.keyword == Keyword::CHECK => {
7557                self.expect_token(&Token::LParen)?;
7558                let expr = Box::new(self.parse_expr()?);
7559                self.expect_token(&Token::RParen)?;
7560                Ok(Some(TableConstraint::Check { name, expr }))
7561            }
7562            Token::Word(w)
7563                if (w.keyword == Keyword::INDEX || w.keyword == Keyword::KEY)
7564                    && dialect_of!(self is GenericDialect | MySqlDialect)
7565                    && name.is_none() =>
7566            {
7567                let display_as_key = w.keyword == Keyword::KEY;
7568
7569                let name = match self.peek_token().token {
7570                    Token::Word(word) if word.keyword == Keyword::USING => None,
7571                    _ => self.parse_optional_indent()?,
7572                };
7573
7574                let index_type = self.parse_optional_using_then_index_type()?;
7575                let columns = self.parse_parenthesized_column_list(Mandatory, false)?;
7576
7577                Ok(Some(TableConstraint::Index {
7578                    display_as_key,
7579                    name,
7580                    index_type,
7581                    columns,
7582                }))
7583            }
7584            Token::Word(w)
7585                if (w.keyword == Keyword::FULLTEXT || w.keyword == Keyword::SPATIAL)
7586                    && dialect_of!(self is GenericDialect | MySqlDialect) =>
7587            {
7588                if let Some(name) = name {
7589                    return self.expected(
7590                        "FULLTEXT or SPATIAL option without constraint name",
7591                        TokenWithSpan {
7592                            token: Token::make_keyword(&name.to_string()),
7593                            span: next_token.span,
7594                        },
7595                    );
7596                }
7597
7598                let fulltext = w.keyword == Keyword::FULLTEXT;
7599
7600                let index_type_display = self.parse_index_type_display();
7601
7602                let opt_index_name = self.parse_optional_indent()?;
7603
7604                let columns = self.parse_parenthesized_column_list(Mandatory, false)?;
7605
7606                Ok(Some(TableConstraint::FulltextOrSpatial {
7607                    fulltext,
7608                    index_type_display,
7609                    opt_index_name,
7610                    columns,
7611                }))
7612            }
7613            _ => {
7614                if name.is_some() {
7615                    self.expected("PRIMARY, UNIQUE, FOREIGN, or CHECK", next_token)
7616                } else {
7617                    self.prev_token();
7618                    Ok(None)
7619                }
7620            }
7621        }
7622    }
7623
7624    fn parse_optional_nulls_distinct(&mut self) -> Result<NullsDistinctOption, ParserError> {
7625        Ok(if self.parse_keyword(Keyword::NULLS) {
7626            let not = self.parse_keyword(Keyword::NOT);
7627            self.expect_keyword_is(Keyword::DISTINCT)?;
7628            if not {
7629                NullsDistinctOption::NotDistinct
7630            } else {
7631                NullsDistinctOption::Distinct
7632            }
7633        } else {
7634            NullsDistinctOption::None
7635        })
7636    }
7637
7638    pub fn maybe_parse_options(
7639        &mut self,
7640        keyword: Keyword,
7641    ) -> Result<Option<Vec<SqlOption>>, ParserError> {
7642        if let Token::Word(word) = self.peek_token().token {
7643            if word.keyword == keyword {
7644                return Ok(Some(self.parse_options(keyword)?));
7645            }
7646        };
7647        Ok(None)
7648    }
7649
7650    pub fn parse_options(&mut self, keyword: Keyword) -> Result<Vec<SqlOption>, ParserError> {
7651        if self.parse_keyword(keyword) {
7652            self.expect_token(&Token::LParen)?;
7653            let options = self.parse_comma_separated0(Parser::parse_sql_option, Token::RParen)?;
7654            self.expect_token(&Token::RParen)?;
7655            Ok(options)
7656        } else {
7657            Ok(vec![])
7658        }
7659    }
7660
7661    pub fn parse_options_with_keywords(
7662        &mut self,
7663        keywords: &[Keyword],
7664    ) -> Result<Vec<SqlOption>, ParserError> {
7665        if self.parse_keywords(keywords) {
7666            self.expect_token(&Token::LParen)?;
7667            let options = self.parse_comma_separated(Parser::parse_sql_option)?;
7668            self.expect_token(&Token::RParen)?;
7669            Ok(options)
7670        } else {
7671            Ok(vec![])
7672        }
7673    }
7674
7675    pub fn parse_index_type(&mut self) -> Result<IndexType, ParserError> {
7676        if self.parse_keyword(Keyword::BTREE) {
7677            Ok(IndexType::BTree)
7678        } else if self.parse_keyword(Keyword::HASH) {
7679            Ok(IndexType::Hash)
7680        } else {
7681            self.expected("index type {BTREE | HASH}", self.peek_token())
7682        }
7683    }
7684
7685    /// Parse [USING {BTREE | HASH}]
7686    pub fn parse_optional_using_then_index_type(
7687        &mut self,
7688    ) -> Result<Option<IndexType>, ParserError> {
7689        if self.parse_keyword(Keyword::USING) {
7690            Ok(Some(self.parse_index_type()?))
7691        } else {
7692            Ok(None)
7693        }
7694    }
7695
7696    /// Parse `[ident]`, mostly `ident` is name, like:
7697    /// `window_name`, `index_name`, ...
7698    pub fn parse_optional_indent(&mut self) -> Result<Option<Ident>, ParserError> {
7699        self.maybe_parse(|parser| parser.parse_identifier())
7700    }
7701
7702    #[must_use]
7703    pub fn parse_index_type_display(&mut self) -> KeyOrIndexDisplay {
7704        if self.parse_keyword(Keyword::KEY) {
7705            KeyOrIndexDisplay::Key
7706        } else if self.parse_keyword(Keyword::INDEX) {
7707            KeyOrIndexDisplay::Index
7708        } else {
7709            KeyOrIndexDisplay::None
7710        }
7711    }
7712
7713    pub fn parse_optional_index_option(&mut self) -> Result<Option<IndexOption>, ParserError> {
7714        if let Some(index_type) = self.parse_optional_using_then_index_type()? {
7715            Ok(Some(IndexOption::Using(index_type)))
7716        } else if self.parse_keyword(Keyword::COMMENT) {
7717            let s = self.parse_literal_string()?;
7718            Ok(Some(IndexOption::Comment(s)))
7719        } else {
7720            Ok(None)
7721        }
7722    }
7723
7724    pub fn parse_index_options(&mut self) -> Result<Vec<IndexOption>, ParserError> {
7725        let mut options = Vec::new();
7726
7727        loop {
7728            match self.parse_optional_index_option()? {
7729                Some(index_option) => options.push(index_option),
7730                None => return Ok(options),
7731            }
7732        }
7733    }
7734
7735    pub fn parse_sql_option(&mut self) -> Result<SqlOption, ParserError> {
7736        let is_mssql = dialect_of!(self is MsSqlDialect|GenericDialect);
7737
7738        match self.peek_token().token {
7739            Token::Word(w) if w.keyword == Keyword::HEAP && is_mssql => {
7740                Ok(SqlOption::Ident(self.parse_identifier()?))
7741            }
7742            Token::Word(w) if w.keyword == Keyword::PARTITION && is_mssql => {
7743                self.parse_option_partition()
7744            }
7745            Token::Word(w) if w.keyword == Keyword::CLUSTERED && is_mssql => {
7746                self.parse_option_clustered()
7747            }
7748            _ => {
7749                let name = self.parse_identifier()?;
7750                self.expect_token(&Token::Eq)?;
7751                let value = self.parse_expr()?;
7752
7753                Ok(SqlOption::KeyValue { key: name, value })
7754            }
7755        }
7756    }
7757
7758    pub fn parse_option_clustered(&mut self) -> Result<SqlOption, ParserError> {
7759        if self.parse_keywords(&[
7760            Keyword::CLUSTERED,
7761            Keyword::COLUMNSTORE,
7762            Keyword::INDEX,
7763            Keyword::ORDER,
7764        ]) {
7765            Ok(SqlOption::Clustered(
7766                TableOptionsClustered::ColumnstoreIndexOrder(
7767                    self.parse_parenthesized_column_list(IsOptional::Mandatory, false)?,
7768                ),
7769            ))
7770        } else if self.parse_keywords(&[Keyword::CLUSTERED, Keyword::COLUMNSTORE, Keyword::INDEX]) {
7771            Ok(SqlOption::Clustered(
7772                TableOptionsClustered::ColumnstoreIndex,
7773            ))
7774        } else if self.parse_keywords(&[Keyword::CLUSTERED, Keyword::INDEX]) {
7775            self.expect_token(&Token::LParen)?;
7776
7777            let columns = self.parse_comma_separated(|p| {
7778                let name = p.parse_identifier()?;
7779                let asc = p.parse_asc_desc();
7780
7781                Ok(ClusteredIndex { name, asc })
7782            })?;
7783
7784            self.expect_token(&Token::RParen)?;
7785
7786            Ok(SqlOption::Clustered(TableOptionsClustered::Index(columns)))
7787        } else {
7788            Err(ParserError::ParserError(
7789                "invalid CLUSTERED sequence".to_string(),
7790            ))
7791        }
7792    }
7793
7794    pub fn parse_option_partition(&mut self) -> Result<SqlOption, ParserError> {
7795        self.expect_keyword_is(Keyword::PARTITION)?;
7796        self.expect_token(&Token::LParen)?;
7797        let column_name = self.parse_identifier()?;
7798
7799        self.expect_keyword_is(Keyword::RANGE)?;
7800        let range_direction = if self.parse_keyword(Keyword::LEFT) {
7801            Some(PartitionRangeDirection::Left)
7802        } else if self.parse_keyword(Keyword::RIGHT) {
7803            Some(PartitionRangeDirection::Right)
7804        } else {
7805            None
7806        };
7807
7808        self.expect_keywords(&[Keyword::FOR, Keyword::VALUES])?;
7809        self.expect_token(&Token::LParen)?;
7810
7811        let for_values = self.parse_comma_separated(Parser::parse_expr)?;
7812
7813        self.expect_token(&Token::RParen)?;
7814        self.expect_token(&Token::RParen)?;
7815
7816        Ok(SqlOption::Partition {
7817            column_name,
7818            range_direction,
7819            for_values,
7820        })
7821    }
7822
7823    pub fn parse_partition(&mut self) -> Result<Partition, ParserError> {
7824        self.expect_token(&Token::LParen)?;
7825        let partitions = self.parse_comma_separated(Parser::parse_expr)?;
7826        self.expect_token(&Token::RParen)?;
7827        Ok(Partition::Partitions(partitions))
7828    }
7829
7830    pub fn parse_projection_select(&mut self) -> Result<ProjectionSelect, ParserError> {
7831        self.expect_token(&Token::LParen)?;
7832        self.expect_keyword_is(Keyword::SELECT)?;
7833        let projection = self.parse_projection()?;
7834        let group_by = self.parse_optional_group_by()?;
7835        let order_by = self.parse_optional_order_by()?;
7836        self.expect_token(&Token::RParen)?;
7837        Ok(ProjectionSelect {
7838            projection,
7839            group_by,
7840            order_by,
7841        })
7842    }
7843    pub fn parse_alter_table_add_projection(&mut self) -> Result<AlterTableOperation, ParserError> {
7844        let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
7845        let name = self.parse_identifier()?;
7846        let query = self.parse_projection_select()?;
7847        Ok(AlterTableOperation::AddProjection {
7848            if_not_exists,
7849            name,
7850            select: query,
7851        })
7852    }
7853
7854    pub fn parse_alter_table_operation(&mut self) -> Result<AlterTableOperation, ParserError> {
7855        let operation = if self.parse_keyword(Keyword::ADD) {
7856            if let Some(constraint) = self.parse_optional_table_constraint()? {
7857                AlterTableOperation::AddConstraint(constraint)
7858            } else if dialect_of!(self is ClickHouseDialect|GenericDialect)
7859                && self.parse_keyword(Keyword::PROJECTION)
7860            {
7861                return self.parse_alter_table_add_projection();
7862            } else {
7863                let if_not_exists =
7864                    self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
7865                let mut new_partitions = vec![];
7866                loop {
7867                    if self.parse_keyword(Keyword::PARTITION) {
7868                        new_partitions.push(self.parse_partition()?);
7869                    } else {
7870                        break;
7871                    }
7872                }
7873                if !new_partitions.is_empty() {
7874                    AlterTableOperation::AddPartitions {
7875                        if_not_exists,
7876                        new_partitions,
7877                    }
7878                } else {
7879                    let column_keyword = self.parse_keyword(Keyword::COLUMN);
7880
7881                    let if_not_exists = if dialect_of!(self is PostgreSqlDialect | BigQueryDialect | DuckDbDialect | GenericDialect)
7882                    {
7883                        self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS])
7884                            || if_not_exists
7885                    } else {
7886                        false
7887                    };
7888
7889                    let column_def = self.parse_column_def()?;
7890
7891                    let column_position = self.parse_column_position()?;
7892
7893                    AlterTableOperation::AddColumn {
7894                        column_keyword,
7895                        if_not_exists,
7896                        column_def,
7897                        column_position,
7898                    }
7899                }
7900            }
7901        } else if self.parse_keyword(Keyword::RENAME) {
7902            if dialect_of!(self is PostgreSqlDialect) && self.parse_keyword(Keyword::CONSTRAINT) {
7903                let old_name = self.parse_identifier()?;
7904                self.expect_keyword_is(Keyword::TO)?;
7905                let new_name = self.parse_identifier()?;
7906                AlterTableOperation::RenameConstraint { old_name, new_name }
7907            } else if self.parse_keyword(Keyword::TO) {
7908                let table_name = self.parse_object_name(false)?;
7909                AlterTableOperation::RenameTable { table_name }
7910            } else {
7911                let _ = self.parse_keyword(Keyword::COLUMN); // [ COLUMN ]
7912                let old_column_name = self.parse_identifier()?;
7913                self.expect_keyword_is(Keyword::TO)?;
7914                let new_column_name = self.parse_identifier()?;
7915                AlterTableOperation::RenameColumn {
7916                    old_column_name,
7917                    new_column_name,
7918                }
7919            }
7920        } else if self.parse_keyword(Keyword::DISABLE) {
7921            if self.parse_keywords(&[Keyword::ROW, Keyword::LEVEL, Keyword::SECURITY]) {
7922                AlterTableOperation::DisableRowLevelSecurity {}
7923            } else if self.parse_keyword(Keyword::RULE) {
7924                let name = self.parse_identifier()?;
7925                AlterTableOperation::DisableRule { name }
7926            } else if self.parse_keyword(Keyword::TRIGGER) {
7927                let name = self.parse_identifier()?;
7928                AlterTableOperation::DisableTrigger { name }
7929            } else {
7930                return self.expected(
7931                    "ROW LEVEL SECURITY, RULE, or TRIGGER after DISABLE",
7932                    self.peek_token(),
7933                );
7934            }
7935        } else if self.parse_keyword(Keyword::ENABLE) {
7936            if self.parse_keywords(&[Keyword::ALWAYS, Keyword::RULE]) {
7937                let name = self.parse_identifier()?;
7938                AlterTableOperation::EnableAlwaysRule { name }
7939            } else if self.parse_keywords(&[Keyword::ALWAYS, Keyword::TRIGGER]) {
7940                let name = self.parse_identifier()?;
7941                AlterTableOperation::EnableAlwaysTrigger { name }
7942            } else if self.parse_keywords(&[Keyword::ROW, Keyword::LEVEL, Keyword::SECURITY]) {
7943                AlterTableOperation::EnableRowLevelSecurity {}
7944            } else if self.parse_keywords(&[Keyword::REPLICA, Keyword::RULE]) {
7945                let name = self.parse_identifier()?;
7946                AlterTableOperation::EnableReplicaRule { name }
7947            } else if self.parse_keywords(&[Keyword::REPLICA, Keyword::TRIGGER]) {
7948                let name = self.parse_identifier()?;
7949                AlterTableOperation::EnableReplicaTrigger { name }
7950            } else if self.parse_keyword(Keyword::RULE) {
7951                let name = self.parse_identifier()?;
7952                AlterTableOperation::EnableRule { name }
7953            } else if self.parse_keyword(Keyword::TRIGGER) {
7954                let name = self.parse_identifier()?;
7955                AlterTableOperation::EnableTrigger { name }
7956            } else {
7957                return self.expected(
7958                    "ALWAYS, REPLICA, ROW LEVEL SECURITY, RULE, or TRIGGER after ENABLE",
7959                    self.peek_token(),
7960                );
7961            }
7962        } else if self.parse_keywords(&[Keyword::CLEAR, Keyword::PROJECTION])
7963            && dialect_of!(self is ClickHouseDialect|GenericDialect)
7964        {
7965            let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
7966            let name = self.parse_identifier()?;
7967            let partition = if self.parse_keywords(&[Keyword::IN, Keyword::PARTITION]) {
7968                Some(self.parse_identifier()?)
7969            } else {
7970                None
7971            };
7972            AlterTableOperation::ClearProjection {
7973                if_exists,
7974                name,
7975                partition,
7976            }
7977        } else if self.parse_keywords(&[Keyword::MATERIALIZE, Keyword::PROJECTION])
7978            && dialect_of!(self is ClickHouseDialect|GenericDialect)
7979        {
7980            let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
7981            let name = self.parse_identifier()?;
7982            let partition = if self.parse_keywords(&[Keyword::IN, Keyword::PARTITION]) {
7983                Some(self.parse_identifier()?)
7984            } else {
7985                None
7986            };
7987            AlterTableOperation::MaterializeProjection {
7988                if_exists,
7989                name,
7990                partition,
7991            }
7992        } else if self.parse_keyword(Keyword::DROP) {
7993            if self.parse_keywords(&[Keyword::IF, Keyword::EXISTS, Keyword::PARTITION]) {
7994                self.expect_token(&Token::LParen)?;
7995                let partitions = self.parse_comma_separated(Parser::parse_expr)?;
7996                self.expect_token(&Token::RParen)?;
7997                AlterTableOperation::DropPartitions {
7998                    partitions,
7999                    if_exists: true,
8000                }
8001            } else if self.parse_keyword(Keyword::PARTITION) {
8002                self.expect_token(&Token::LParen)?;
8003                let partitions = self.parse_comma_separated(Parser::parse_expr)?;
8004                self.expect_token(&Token::RParen)?;
8005                AlterTableOperation::DropPartitions {
8006                    partitions,
8007                    if_exists: false,
8008                }
8009            } else if self.parse_keyword(Keyword::CONSTRAINT) {
8010                let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
8011                let name = self.parse_identifier()?;
8012                let drop_behavior = self.parse_optional_drop_behavior();
8013                AlterTableOperation::DropConstraint {
8014                    if_exists,
8015                    name,
8016                    drop_behavior,
8017                }
8018            } else if self.parse_keywords(&[Keyword::PRIMARY, Keyword::KEY])
8019                && dialect_of!(self is MySqlDialect | GenericDialect)
8020            {
8021                AlterTableOperation::DropPrimaryKey
8022            } else if self.parse_keyword(Keyword::PROJECTION)
8023                && dialect_of!(self is ClickHouseDialect|GenericDialect)
8024            {
8025                let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
8026                let name = self.parse_identifier()?;
8027                AlterTableOperation::DropProjection { if_exists, name }
8028            } else if self.parse_keywords(&[Keyword::CLUSTERING, Keyword::KEY]) {
8029                AlterTableOperation::DropClusteringKey
8030            } else {
8031                let _ = self.parse_keyword(Keyword::COLUMN); // [ COLUMN ]
8032                let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
8033                let column_name = self.parse_identifier()?;
8034                let drop_behavior = self.parse_optional_drop_behavior();
8035                AlterTableOperation::DropColumn {
8036                    column_name,
8037                    if_exists,
8038                    drop_behavior,
8039                }
8040            }
8041        } else if self.parse_keyword(Keyword::PARTITION) {
8042            self.expect_token(&Token::LParen)?;
8043            let before = self.parse_comma_separated(Parser::parse_expr)?;
8044            self.expect_token(&Token::RParen)?;
8045            self.expect_keyword_is(Keyword::RENAME)?;
8046            self.expect_keywords(&[Keyword::TO, Keyword::PARTITION])?;
8047            self.expect_token(&Token::LParen)?;
8048            let renames = self.parse_comma_separated(Parser::parse_expr)?;
8049            self.expect_token(&Token::RParen)?;
8050            AlterTableOperation::RenamePartitions {
8051                old_partitions: before,
8052                new_partitions: renames,
8053            }
8054        } else if self.parse_keyword(Keyword::CHANGE) {
8055            let _ = self.parse_keyword(Keyword::COLUMN); // [ COLUMN ]
8056            let old_name = self.parse_identifier()?;
8057            let new_name = self.parse_identifier()?;
8058            let data_type = self.parse_data_type()?;
8059            let mut options = vec![];
8060            while let Some(option) = self.parse_optional_column_option()? {
8061                options.push(option);
8062            }
8063
8064            let column_position = self.parse_column_position()?;
8065
8066            AlterTableOperation::ChangeColumn {
8067                old_name,
8068                new_name,
8069                data_type,
8070                options,
8071                column_position,
8072            }
8073        } else if self.parse_keyword(Keyword::MODIFY) {
8074            let _ = self.parse_keyword(Keyword::COLUMN); // [ COLUMN ]
8075            let col_name = self.parse_identifier()?;
8076            let data_type = self.parse_data_type()?;
8077            let mut options = vec![];
8078            while let Some(option) = self.parse_optional_column_option()? {
8079                options.push(option);
8080            }
8081
8082            let column_position = self.parse_column_position()?;
8083
8084            AlterTableOperation::ModifyColumn {
8085                col_name,
8086                data_type,
8087                options,
8088                column_position,
8089            }
8090        } else if self.parse_keyword(Keyword::ALTER) {
8091            let _ = self.parse_keyword(Keyword::COLUMN); // [ COLUMN ]
8092            let column_name = self.parse_identifier()?;
8093            let is_postgresql = dialect_of!(self is PostgreSqlDialect);
8094
8095            let op: AlterColumnOperation = if self.parse_keywords(&[
8096                Keyword::SET,
8097                Keyword::NOT,
8098                Keyword::NULL,
8099            ]) {
8100                AlterColumnOperation::SetNotNull {}
8101            } else if self.parse_keywords(&[Keyword::DROP, Keyword::NOT, Keyword::NULL]) {
8102                AlterColumnOperation::DropNotNull {}
8103            } else if self.parse_keywords(&[Keyword::SET, Keyword::DEFAULT]) {
8104                AlterColumnOperation::SetDefault {
8105                    value: self.parse_expr()?,
8106                }
8107            } else if self.parse_keywords(&[Keyword::DROP, Keyword::DEFAULT]) {
8108                AlterColumnOperation::DropDefault {}
8109            } else if self.parse_keywords(&[Keyword::SET, Keyword::DATA, Keyword::TYPE])
8110                || (is_postgresql && self.parse_keyword(Keyword::TYPE))
8111            {
8112                let data_type = self.parse_data_type()?;
8113                let using = if is_postgresql && self.parse_keyword(Keyword::USING) {
8114                    Some(self.parse_expr()?)
8115                } else {
8116                    None
8117                };
8118                AlterColumnOperation::SetDataType { data_type, using }
8119            } else if self.parse_keywords(&[Keyword::ADD, Keyword::GENERATED]) {
8120                let generated_as = if self.parse_keyword(Keyword::ALWAYS) {
8121                    Some(GeneratedAs::Always)
8122                } else if self.parse_keywords(&[Keyword::BY, Keyword::DEFAULT]) {
8123                    Some(GeneratedAs::ByDefault)
8124                } else {
8125                    None
8126                };
8127
8128                self.expect_keywords(&[Keyword::AS, Keyword::IDENTITY])?;
8129
8130                let mut sequence_options: Option<Vec<SequenceOptions>> = None;
8131
8132                if self.peek_token().token == Token::LParen {
8133                    self.expect_token(&Token::LParen)?;
8134                    sequence_options = Some(self.parse_create_sequence_options()?);
8135                    self.expect_token(&Token::RParen)?;
8136                }
8137
8138                AlterColumnOperation::AddGenerated {
8139                    generated_as,
8140                    sequence_options,
8141                }
8142            } else {
8143                let message = if is_postgresql {
8144                    "SET/DROP NOT NULL, SET DEFAULT, SET DATA TYPE, or ADD GENERATED after ALTER COLUMN"
8145                } else {
8146                    "SET/DROP NOT NULL, SET DEFAULT, or SET DATA TYPE after ALTER COLUMN"
8147                };
8148
8149                return self.expected(message, self.peek_token());
8150            };
8151            AlterTableOperation::AlterColumn { column_name, op }
8152        } else if self.parse_keyword(Keyword::SWAP) {
8153            self.expect_keyword_is(Keyword::WITH)?;
8154            let table_name = self.parse_object_name(false)?;
8155            AlterTableOperation::SwapWith { table_name }
8156        } else if dialect_of!(self is PostgreSqlDialect | GenericDialect)
8157            && self.parse_keywords(&[Keyword::OWNER, Keyword::TO])
8158        {
8159            let new_owner = self.parse_owner()?;
8160            AlterTableOperation::OwnerTo { new_owner }
8161        } else if dialect_of!(self is ClickHouseDialect|GenericDialect)
8162            && self.parse_keyword(Keyword::ATTACH)
8163        {
8164            AlterTableOperation::AttachPartition {
8165                partition: self.parse_part_or_partition()?,
8166            }
8167        } else if dialect_of!(self is ClickHouseDialect|GenericDialect)
8168            && self.parse_keyword(Keyword::DETACH)
8169        {
8170            AlterTableOperation::DetachPartition {
8171                partition: self.parse_part_or_partition()?,
8172            }
8173        } else if dialect_of!(self is ClickHouseDialect|GenericDialect)
8174            && self.parse_keyword(Keyword::FREEZE)
8175        {
8176            let partition = self.parse_part_or_partition()?;
8177            let with_name = if self.parse_keyword(Keyword::WITH) {
8178                self.expect_keyword_is(Keyword::NAME)?;
8179                Some(self.parse_identifier()?)
8180            } else {
8181                None
8182            };
8183            AlterTableOperation::FreezePartition {
8184                partition,
8185                with_name,
8186            }
8187        } else if dialect_of!(self is ClickHouseDialect|GenericDialect)
8188            && self.parse_keyword(Keyword::UNFREEZE)
8189        {
8190            let partition = self.parse_part_or_partition()?;
8191            let with_name = if self.parse_keyword(Keyword::WITH) {
8192                self.expect_keyword_is(Keyword::NAME)?;
8193                Some(self.parse_identifier()?)
8194            } else {
8195                None
8196            };
8197            AlterTableOperation::UnfreezePartition {
8198                partition,
8199                with_name,
8200            }
8201        } else if self.parse_keywords(&[Keyword::CLUSTER, Keyword::BY]) {
8202            self.expect_token(&Token::LParen)?;
8203            let exprs = self.parse_comma_separated(|parser| parser.parse_expr())?;
8204            self.expect_token(&Token::RParen)?;
8205            AlterTableOperation::ClusterBy { exprs }
8206        } else if self.parse_keywords(&[Keyword::SUSPEND, Keyword::RECLUSTER]) {
8207            AlterTableOperation::SuspendRecluster
8208        } else if self.parse_keywords(&[Keyword::RESUME, Keyword::RECLUSTER]) {
8209            AlterTableOperation::ResumeRecluster
8210        } else if self.parse_keyword(Keyword::ALGORITHM) {
8211            let equals = self.consume_token(&Token::Eq);
8212            let algorithm = match self.parse_one_of_keywords(&[
8213                Keyword::DEFAULT,
8214                Keyword::INSTANT,
8215                Keyword::INPLACE,
8216                Keyword::COPY,
8217            ]) {
8218                Some(Keyword::DEFAULT) => AlterTableAlgorithm::Default,
8219                Some(Keyword::INSTANT) => AlterTableAlgorithm::Instant,
8220                Some(Keyword::INPLACE) => AlterTableAlgorithm::Inplace,
8221                Some(Keyword::COPY) => AlterTableAlgorithm::Copy,
8222                _ => self.expected(
8223                    "DEFAULT, INSTANT, INPLACE, or COPY after ALGORITHM [=]",
8224                    self.peek_token(),
8225                )?,
8226            };
8227            AlterTableOperation::Algorithm { equals, algorithm }
8228        } else if self.parse_keyword(Keyword::AUTO_INCREMENT) {
8229            let equals = self.consume_token(&Token::Eq);
8230            let value = self.parse_number_value()?;
8231            AlterTableOperation::AutoIncrement { equals, value }
8232        } else {
8233            let options: Vec<SqlOption> =
8234                self.parse_options_with_keywords(&[Keyword::SET, Keyword::TBLPROPERTIES])?;
8235            if !options.is_empty() {
8236                AlterTableOperation::SetTblProperties {
8237                    table_properties: options,
8238                }
8239            } else {
8240                return self.expected(
8241                    "ADD, RENAME, PARTITION, SWAP, DROP, or SET TBLPROPERTIES after ALTER TABLE",
8242                    self.peek_token(),
8243                );
8244            }
8245        };
8246        Ok(operation)
8247    }
8248
8249    fn parse_part_or_partition(&mut self) -> Result<Partition, ParserError> {
8250        let keyword = self.expect_one_of_keywords(&[Keyword::PART, Keyword::PARTITION])?;
8251        match keyword {
8252            Keyword::PART => Ok(Partition::Part(self.parse_expr()?)),
8253            Keyword::PARTITION => Ok(Partition::Expr(self.parse_expr()?)),
8254            // unreachable because expect_one_of_keywords used above
8255            _ => unreachable!(),
8256        }
8257    }
8258
8259    pub fn parse_alter(&mut self) -> Result<Statement, ParserError> {
8260        let object_type = self.expect_one_of_keywords(&[
8261            Keyword::VIEW,
8262            Keyword::TYPE,
8263            Keyword::TABLE,
8264            Keyword::INDEX,
8265            Keyword::ROLE,
8266            Keyword::POLICY,
8267            Keyword::CONNECTOR,
8268        ])?;
8269        match object_type {
8270            Keyword::VIEW => self.parse_alter_view(),
8271            Keyword::TYPE => self.parse_alter_type(),
8272            Keyword::TABLE => {
8273                let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
8274                let only = self.parse_keyword(Keyword::ONLY); // [ ONLY ]
8275                let table_name = self.parse_object_name(false)?;
8276                let on_cluster = self.parse_optional_on_cluster()?;
8277                let operations = self.parse_comma_separated(Parser::parse_alter_table_operation)?;
8278
8279                let mut location = None;
8280                if self.parse_keyword(Keyword::LOCATION) {
8281                    location = Some(HiveSetLocation {
8282                        has_set: false,
8283                        location: self.parse_identifier()?,
8284                    });
8285                } else if self.parse_keywords(&[Keyword::SET, Keyword::LOCATION]) {
8286                    location = Some(HiveSetLocation {
8287                        has_set: true,
8288                        location: self.parse_identifier()?,
8289                    });
8290                }
8291
8292                Ok(Statement::AlterTable {
8293                    name: table_name,
8294                    if_exists,
8295                    only,
8296                    operations,
8297                    location,
8298                    on_cluster,
8299                })
8300            }
8301            Keyword::INDEX => {
8302                let index_name = self.parse_object_name(false)?;
8303                let operation = if self.parse_keyword(Keyword::RENAME) {
8304                    if self.parse_keyword(Keyword::TO) {
8305                        let index_name = self.parse_object_name(false)?;
8306                        AlterIndexOperation::RenameIndex { index_name }
8307                    } else {
8308                        return self.expected("TO after RENAME", self.peek_token());
8309                    }
8310                } else {
8311                    return self.expected("RENAME after ALTER INDEX", self.peek_token());
8312                };
8313
8314                Ok(Statement::AlterIndex {
8315                    name: index_name,
8316                    operation,
8317                })
8318            }
8319            Keyword::ROLE => self.parse_alter_role(),
8320            Keyword::POLICY => self.parse_alter_policy(),
8321            Keyword::CONNECTOR => self.parse_alter_connector(),
8322            // unreachable because expect_one_of_keywords used above
8323            _ => unreachable!(),
8324        }
8325    }
8326
8327    pub fn parse_alter_view(&mut self) -> Result<Statement, ParserError> {
8328        let name = self.parse_object_name(false)?;
8329        let columns = self.parse_parenthesized_column_list(Optional, false)?;
8330
8331        let with_options = self.parse_options(Keyword::WITH)?;
8332
8333        self.expect_keyword_is(Keyword::AS)?;
8334        let query = self.parse_query()?;
8335
8336        Ok(Statement::AlterView {
8337            name,
8338            columns,
8339            query,
8340            with_options,
8341        })
8342    }
8343
8344    /// Parse a [Statement::AlterType]
8345    pub fn parse_alter_type(&mut self) -> Result<Statement, ParserError> {
8346        let name = self.parse_object_name(false)?;
8347
8348        if self.parse_keywords(&[Keyword::RENAME, Keyword::TO]) {
8349            let new_name = self.parse_identifier()?;
8350            Ok(Statement::AlterType(AlterType {
8351                name,
8352                operation: AlterTypeOperation::Rename(AlterTypeRename { new_name }),
8353            }))
8354        } else if self.parse_keywords(&[Keyword::ADD, Keyword::VALUE]) {
8355            let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
8356            let new_enum_value = self.parse_identifier()?;
8357            let position = if self.parse_keyword(Keyword::BEFORE) {
8358                Some(AlterTypeAddValuePosition::Before(self.parse_identifier()?))
8359            } else if self.parse_keyword(Keyword::AFTER) {
8360                Some(AlterTypeAddValuePosition::After(self.parse_identifier()?))
8361            } else {
8362                None
8363            };
8364
8365            Ok(Statement::AlterType(AlterType {
8366                name,
8367                operation: AlterTypeOperation::AddValue(AlterTypeAddValue {
8368                    if_not_exists,
8369                    value: new_enum_value,
8370                    position,
8371                }),
8372            }))
8373        } else if self.parse_keywords(&[Keyword::RENAME, Keyword::VALUE]) {
8374            let existing_enum_value = self.parse_identifier()?;
8375            self.expect_keyword(Keyword::TO)?;
8376            let new_enum_value = self.parse_identifier()?;
8377
8378            Ok(Statement::AlterType(AlterType {
8379                name,
8380                operation: AlterTypeOperation::RenameValue(AlterTypeRenameValue {
8381                    from: existing_enum_value,
8382                    to: new_enum_value,
8383                }),
8384            }))
8385        } else {
8386            return self.expected_ref(
8387                "{RENAME TO | { RENAME | ADD } VALUE}",
8388                self.peek_token_ref(),
8389            );
8390        }
8391    }
8392
8393    /// Parse a `CALL procedure_name(arg1, arg2, ...)`
8394    /// or `CALL procedure_name` statement
8395    pub fn parse_call(&mut self) -> Result<Statement, ParserError> {
8396        let object_name = self.parse_object_name(false)?;
8397        if self.peek_token().token == Token::LParen {
8398            match self.parse_function(object_name)? {
8399                Expr::Function(f) => Ok(Statement::Call(f)),
8400                other => parser_err!(
8401                    format!("Expected a simple procedure call but found: {other}"),
8402                    self.peek_token().span.start
8403                ),
8404            }
8405        } else {
8406            Ok(Statement::Call(Function {
8407                name: object_name,
8408                uses_odbc_syntax: false,
8409                parameters: FunctionArguments::None,
8410                args: FunctionArguments::None,
8411                over: None,
8412                filter: None,
8413                null_treatment: None,
8414                within_group: vec![],
8415            }))
8416        }
8417    }
8418
8419    /// Parse a copy statement
8420    pub fn parse_copy(&mut self) -> Result<Statement, ParserError> {
8421        let source;
8422        if self.consume_token(&Token::LParen) {
8423            source = CopySource::Query(self.parse_query()?);
8424            self.expect_token(&Token::RParen)?;
8425        } else {
8426            let table_name = self.parse_object_name(false)?;
8427            let columns = self.parse_parenthesized_column_list(Optional, false)?;
8428            source = CopySource::Table {
8429                table_name,
8430                columns,
8431            };
8432        }
8433        let to = match self.parse_one_of_keywords(&[Keyword::FROM, Keyword::TO]) {
8434            Some(Keyword::FROM) => false,
8435            Some(Keyword::TO) => true,
8436            _ => self.expected("FROM or TO", self.peek_token())?,
8437        };
8438        if !to {
8439            // Use a separate if statement to prevent Rust compiler from complaining about
8440            // "if statement in this position is unstable: https://github.com/rust-lang/rust/issues/53667"
8441            if let CopySource::Query(_) = source {
8442                return Err(ParserError::ParserError(
8443                    "COPY ... FROM does not support query as a source".to_string(),
8444                ));
8445            }
8446        }
8447        let target = if self.parse_keyword(Keyword::STDIN) {
8448            CopyTarget::Stdin
8449        } else if self.parse_keyword(Keyword::STDOUT) {
8450            CopyTarget::Stdout
8451        } else if self.parse_keyword(Keyword::PROGRAM) {
8452            CopyTarget::Program {
8453                command: self.parse_literal_string()?,
8454            }
8455        } else {
8456            CopyTarget::File {
8457                filename: self.parse_literal_string()?,
8458            }
8459        };
8460        let _ = self.parse_keyword(Keyword::WITH); // [ WITH ]
8461        let mut options = vec![];
8462        if self.consume_token(&Token::LParen) {
8463            options = self.parse_comma_separated(Parser::parse_copy_option)?;
8464            self.expect_token(&Token::RParen)?;
8465        }
8466        let mut legacy_options = vec![];
8467        while let Some(opt) = self.maybe_parse(|parser| parser.parse_copy_legacy_option())? {
8468            legacy_options.push(opt);
8469        }
8470        let values = if let CopyTarget::Stdin = target {
8471            self.expect_token(&Token::SemiColon)?;
8472            self.parse_tsv()
8473        } else {
8474            vec![]
8475        };
8476        Ok(Statement::Copy {
8477            source,
8478            to,
8479            target,
8480            options,
8481            legacy_options,
8482            values,
8483        })
8484    }
8485
8486    pub fn parse_close(&mut self) -> Result<Statement, ParserError> {
8487        let cursor = if self.parse_keyword(Keyword::ALL) {
8488            CloseCursor::All
8489        } else {
8490            let name = self.parse_identifier()?;
8491
8492            CloseCursor::Specific { name }
8493        };
8494
8495        Ok(Statement::Close { cursor })
8496    }
8497
8498    fn parse_copy_option(&mut self) -> Result<CopyOption, ParserError> {
8499        let ret = match self.parse_one_of_keywords(&[
8500            Keyword::FORMAT,
8501            Keyword::FREEZE,
8502            Keyword::DELIMITER,
8503            Keyword::NULL,
8504            Keyword::HEADER,
8505            Keyword::QUOTE,
8506            Keyword::ESCAPE,
8507            Keyword::FORCE_QUOTE,
8508            Keyword::FORCE_NOT_NULL,
8509            Keyword::FORCE_NULL,
8510            Keyword::ENCODING,
8511        ]) {
8512            Some(Keyword::FORMAT) => CopyOption::Format(self.parse_identifier()?),
8513            Some(Keyword::FREEZE) => CopyOption::Freeze(!matches!(
8514                self.parse_one_of_keywords(&[Keyword::TRUE, Keyword::FALSE]),
8515                Some(Keyword::FALSE)
8516            )),
8517            Some(Keyword::DELIMITER) => CopyOption::Delimiter(self.parse_copy_option_char()?),
8518            Some(Keyword::NULL) => CopyOption::Null(self.parse_literal_string()?),
8519            Some(Keyword::HEADER) => CopyOption::Header(!matches!(
8520                self.parse_one_of_keywords(&[Keyword::TRUE, Keyword::FALSE]),
8521                Some(Keyword::FALSE)
8522            )),
8523            Some(Keyword::QUOTE) => CopyOption::Quote(self.parse_copy_option_char()?),
8524            Some(Keyword::ESCAPE) => CopyOption::Escape(self.parse_copy_option_char()?),
8525            Some(Keyword::FORCE_QUOTE) => {
8526                CopyOption::ForceQuote(self.parse_parenthesized_column_list(Mandatory, false)?)
8527            }
8528            Some(Keyword::FORCE_NOT_NULL) => {
8529                CopyOption::ForceNotNull(self.parse_parenthesized_column_list(Mandatory, false)?)
8530            }
8531            Some(Keyword::FORCE_NULL) => {
8532                CopyOption::ForceNull(self.parse_parenthesized_column_list(Mandatory, false)?)
8533            }
8534            Some(Keyword::ENCODING) => CopyOption::Encoding(self.parse_literal_string()?),
8535            _ => {
8536                // DuckDB's COPY accepts an open-ended set of `name [value]` options
8537                // (COMPRESSION, PARTITION_BY, ROW_GROUP_SIZE, OVERWRITE_OR_IGNORE, …)
8538                // beyond the fixed PostgreSQL keywords above. Capture them generically
8539                // so multi-option COPY parses instead of degrading to an opaque step.
8540                if dialect_of!(self is DuckDbDialect | GenericDialect) {
8541                    let name = self.parse_identifier()?;
8542                    let value = if matches!(self.peek_token().token, Token::Comma | Token::RParen) {
8543                        None
8544                    } else {
8545                        Some(self.parse_expr()?)
8546                    };
8547                    CopyOption::DuckDbOption { name, value }
8548                } else {
8549                    self.expected("option", self.peek_token())?
8550                }
8551            }
8552        };
8553        Ok(ret)
8554    }
8555
8556    fn parse_copy_legacy_option(&mut self) -> Result<CopyLegacyOption, ParserError> {
8557        let ret = match self.parse_one_of_keywords(&[
8558            Keyword::BINARY,
8559            Keyword::DELIMITER,
8560            Keyword::NULL,
8561            Keyword::CSV,
8562        ]) {
8563            Some(Keyword::BINARY) => CopyLegacyOption::Binary,
8564            Some(Keyword::DELIMITER) => {
8565                let _ = self.parse_keyword(Keyword::AS); // [ AS ]
8566                CopyLegacyOption::Delimiter(self.parse_literal_char()?)
8567            }
8568            Some(Keyword::NULL) => {
8569                let _ = self.parse_keyword(Keyword::AS); // [ AS ]
8570                CopyLegacyOption::Null(self.parse_literal_string()?)
8571            }
8572            Some(Keyword::CSV) => CopyLegacyOption::Csv({
8573                let mut opts = vec![];
8574                while let Some(opt) =
8575                    self.maybe_parse(|parser| parser.parse_copy_legacy_csv_option())?
8576                {
8577                    opts.push(opt);
8578                }
8579                opts
8580            }),
8581            _ => self.expected("option", self.peek_token())?,
8582        };
8583        Ok(ret)
8584    }
8585
8586    fn parse_copy_legacy_csv_option(&mut self) -> Result<CopyLegacyCsvOption, ParserError> {
8587        let ret = match self.parse_one_of_keywords(&[
8588            Keyword::HEADER,
8589            Keyword::QUOTE,
8590            Keyword::ESCAPE,
8591            Keyword::FORCE,
8592        ]) {
8593            Some(Keyword::HEADER) => CopyLegacyCsvOption::Header,
8594            Some(Keyword::QUOTE) => {
8595                let _ = self.parse_keyword(Keyword::AS); // [ AS ]
8596                CopyLegacyCsvOption::Quote(self.parse_literal_char()?)
8597            }
8598            Some(Keyword::ESCAPE) => {
8599                let _ = self.parse_keyword(Keyword::AS); // [ AS ]
8600                CopyLegacyCsvOption::Escape(self.parse_literal_char()?)
8601            }
8602            Some(Keyword::FORCE) if self.parse_keywords(&[Keyword::NOT, Keyword::NULL]) => {
8603                CopyLegacyCsvOption::ForceNotNull(
8604                    self.parse_comma_separated(|p| p.parse_identifier())?,
8605                )
8606            }
8607            Some(Keyword::FORCE) if self.parse_keywords(&[Keyword::QUOTE]) => {
8608                CopyLegacyCsvOption::ForceQuote(
8609                    self.parse_comma_separated(|p| p.parse_identifier())?,
8610                )
8611            }
8612            _ => self.expected("csv option", self.peek_token())?,
8613        };
8614        Ok(ret)
8615    }
8616
8617    fn parse_literal_char(&mut self) -> Result<char, ParserError> {
8618        let s = self.parse_literal_string()?;
8619        if s.len() != 1 {
8620            let loc = self
8621                .tokens
8622                .get(self.index - 1)
8623                .map_or(Location { line: 0, column: 0 }, |t| t.span.start);
8624            return parser_err!(format!("Expect a char, found {s:?}"), loc);
8625        }
8626        Ok(s.chars().next().unwrap())
8627    }
8628
8629    /// The `DELIMITER`/`QUOTE`/`ESCAPE` value inside a `COPY … (option …)`
8630    /// list: exactly one character on every dialect, matching
8631    /// [`Self::parse_literal_char`] — except on DuckDB/Generic, which also
8632    /// accept the empty string (`QUOTE ''`) to disable that option, returned
8633    /// here as `None`. Every other dialect (PostgreSQL among them) keeps
8634    /// refusing the empty string exactly as [`Self::parse_literal_char`]
8635    /// always has.
8636    fn parse_copy_option_char(&mut self) -> Result<Option<char>, ParserError> {
8637        let s = self.parse_literal_string()?;
8638        if s.is_empty() && dialect_of!(self is DuckDbDialect | GenericDialect) {
8639            return Ok(None);
8640        }
8641        if s.len() != 1 {
8642            let loc = self
8643                .tokens
8644                .get(self.index - 1)
8645                .map_or(Location { line: 0, column: 0 }, |t| t.span.start);
8646            return parser_err!(format!("Expect a char, found {s:?}"), loc);
8647        }
8648        Ok(Some(s.chars().next().unwrap()))
8649    }
8650
8651    /// Parse a tab separated values in
8652    /// COPY payload
8653    pub fn parse_tsv(&mut self) -> Vec<Option<String>> {
8654        self.parse_tab_value()
8655    }
8656
8657    pub fn parse_tab_value(&mut self) -> Vec<Option<String>> {
8658        let mut values = vec![];
8659        let mut content = String::from("");
8660        while let Some(t) = self.next_token_no_skip().map(|t| &t.token) {
8661            match t {
8662                Token::Whitespace(Whitespace::Tab) => {
8663                    values.push(Some(content.to_string()));
8664                    content.clear();
8665                }
8666                Token::Whitespace(Whitespace::Newline) => {
8667                    values.push(Some(content.to_string()));
8668                    content.clear();
8669                }
8670                Token::Backslash => {
8671                    if self.consume_token(&Token::Period) {
8672                        return values;
8673                    }
8674                    if let Token::Word(w) = self.next_token().token {
8675                        if w.value == "N" {
8676                            values.push(None);
8677                        }
8678                    }
8679                }
8680                _ => {
8681                    content.push_str(&t.to_string());
8682                }
8683            }
8684        }
8685        values
8686    }
8687
8688    /// Parse a literal value (numbers, strings, date/time, booleans)
8689    pub fn parse_value(&mut self) -> Result<ValueWithSpan, ParserError> {
8690        let next_token = self.next_token();
8691        let span = next_token.span;
8692        let ok_value = |value: Value| Ok(value.with_span(span));
8693        match next_token.token {
8694            Token::Word(w) => match w.keyword {
8695                Keyword::TRUE if self.dialect.supports_boolean_literals() => {
8696                    ok_value(Value::Boolean(true))
8697                }
8698                Keyword::FALSE if self.dialect.supports_boolean_literals() => {
8699                    ok_value(Value::Boolean(false))
8700                }
8701                Keyword::NULL => ok_value(Value::Null),
8702                Keyword::NoKeyword if w.quote_style.is_some() => match w.quote_style {
8703                    Some('"') => ok_value(Value::DoubleQuotedString(w.value)),
8704                    Some('\'') => ok_value(Value::SingleQuotedString(w.value)),
8705                    _ => self.expected(
8706                        "A value?",
8707                        TokenWithSpan {
8708                            token: Token::Word(w),
8709                            span,
8710                        },
8711                    )?,
8712                },
8713                _ => self.expected(
8714                    "a concrete value",
8715                    TokenWithSpan {
8716                        token: Token::Word(w),
8717                        span,
8718                    },
8719                ),
8720            },
8721            // The call to n.parse() returns a bigdecimal when the
8722            // bigdecimal feature is enabled, and is otherwise a no-op
8723            // (i.e., it returns the input string).
8724            Token::Number(n, l) => ok_value(Value::Number(Self::parse(n, span.start)?, l)),
8725            Token::SingleQuotedString(ref s) => ok_value(Value::SingleQuotedString(s.to_string())),
8726            Token::DoubleQuotedString(ref s) => ok_value(Value::DoubleQuotedString(s.to_string())),
8727            Token::TripleSingleQuotedString(ref s) => {
8728                ok_value(Value::TripleSingleQuotedString(s.to_string()))
8729            }
8730            Token::TripleDoubleQuotedString(ref s) => {
8731                ok_value(Value::TripleDoubleQuotedString(s.to_string()))
8732            }
8733            Token::DollarQuotedString(ref s) => ok_value(Value::DollarQuotedString(s.clone())),
8734            Token::SingleQuotedByteStringLiteral(ref s) => {
8735                ok_value(Value::SingleQuotedByteStringLiteral(s.clone()))
8736            }
8737            Token::DoubleQuotedByteStringLiteral(ref s) => {
8738                ok_value(Value::DoubleQuotedByteStringLiteral(s.clone()))
8739            }
8740            Token::TripleSingleQuotedByteStringLiteral(ref s) => {
8741                ok_value(Value::TripleSingleQuotedByteStringLiteral(s.clone()))
8742            }
8743            Token::TripleDoubleQuotedByteStringLiteral(ref s) => {
8744                ok_value(Value::TripleDoubleQuotedByteStringLiteral(s.clone()))
8745            }
8746            Token::SingleQuotedRawStringLiteral(ref s) => {
8747                ok_value(Value::SingleQuotedRawStringLiteral(s.clone()))
8748            }
8749            Token::DoubleQuotedRawStringLiteral(ref s) => {
8750                ok_value(Value::DoubleQuotedRawStringLiteral(s.clone()))
8751            }
8752            Token::TripleSingleQuotedRawStringLiteral(ref s) => {
8753                ok_value(Value::TripleSingleQuotedRawStringLiteral(s.clone()))
8754            }
8755            Token::TripleDoubleQuotedRawStringLiteral(ref s) => {
8756                ok_value(Value::TripleDoubleQuotedRawStringLiteral(s.clone()))
8757            }
8758            Token::NationalStringLiteral(ref s) => {
8759                ok_value(Value::NationalStringLiteral(s.to_string()))
8760            }
8761            Token::EscapedStringLiteral(ref s) => {
8762                ok_value(Value::EscapedStringLiteral(s.to_string()))
8763            }
8764            Token::UnicodeStringLiteral(ref s) => {
8765                ok_value(Value::UnicodeStringLiteral(s.to_string()))
8766            }
8767            Token::HexStringLiteral(ref s) => ok_value(Value::HexStringLiteral(s.to_string())),
8768            Token::Placeholder(ref s) => ok_value(Value::Placeholder(s.to_string())),
8769            tok @ Token::Colon | tok @ Token::AtSign => {
8770                // Not calling self.parse_identifier(false)? because only in placeholder we want to check numbers as idfentifies
8771                // This because snowflake allows numbers as placeholders
8772                let next_token = self.next_token();
8773                let ident = match next_token.token {
8774                    Token::Word(w) => Ok(w.into_ident(next_token.span)),
8775                    Token::Number(w, false) => Ok(Ident::new(w)),
8776                    _ => self.expected("placeholder", next_token),
8777                }?;
8778                let placeholder = tok.to_string() + &ident.value;
8779                ok_value(Value::Placeholder(placeholder))
8780            }
8781            unexpected => self.expected(
8782                "a value",
8783                TokenWithSpan {
8784                    token: unexpected,
8785                    span,
8786                },
8787            ),
8788        }
8789    }
8790
8791    /// Parse an unsigned numeric literal
8792    pub fn parse_number_value(&mut self) -> Result<ValueWithSpan, ParserError> {
8793        let value_wrapper = self.parse_value()?;
8794        match &value_wrapper.value {
8795            Value::Number(_, _) => Ok(value_wrapper),
8796            Value::Placeholder(_) => Ok(value_wrapper),
8797            _ => {
8798                self.prev_token();
8799                self.expected("literal number", self.peek_token())
8800            }
8801        }
8802    }
8803
8804    /// Parse a numeric literal as an expression. Returns a [`Expr::UnaryOp`] if the number is signed,
8805    /// otherwise returns a [`Expr::Value`]
8806    pub fn parse_number(&mut self) -> Result<Expr, ParserError> {
8807        let next_token = self.next_token();
8808        match next_token.token {
8809            Token::Plus => Ok(Expr::UnaryOp {
8810                op: UnaryOperator::Plus,
8811                expr: Box::new(Expr::Value(self.parse_number_value()?)),
8812            }),
8813            Token::Minus => Ok(Expr::UnaryOp {
8814                op: UnaryOperator::Minus,
8815                expr: Box::new(Expr::Value(self.parse_number_value()?)),
8816            }),
8817            _ => {
8818                self.prev_token();
8819                Ok(Expr::Value(self.parse_number_value()?))
8820            }
8821        }
8822    }
8823
8824    fn parse_introduced_string_value(&mut self) -> Result<Value, ParserError> {
8825        let next_token = self.next_token();
8826        let span = next_token.span;
8827        match next_token.token {
8828            Token::SingleQuotedString(ref s) => Ok(Value::SingleQuotedString(s.to_string())),
8829            Token::DoubleQuotedString(ref s) => Ok(Value::DoubleQuotedString(s.to_string())),
8830            Token::HexStringLiteral(ref s) => Ok(Value::HexStringLiteral(s.to_string())),
8831            unexpected => self.expected(
8832                "a string value",
8833                TokenWithSpan {
8834                    token: unexpected,
8835                    span,
8836                },
8837            ),
8838        }
8839    }
8840
8841    /// Parse an unsigned literal integer/long
8842    pub fn parse_literal_uint(&mut self) -> Result<u64, ParserError> {
8843        let next_token = self.next_token();
8844        match next_token.token {
8845            Token::Number(s, _) => Self::parse::<u64>(s, next_token.span.start),
8846            _ => self.expected("literal int", next_token),
8847        }
8848    }
8849
8850    /// Parse the body of a `CREATE FUNCTION` specified as a string.
8851    /// e.g. `CREATE FUNCTION ... AS $$ body $$`.
8852    fn parse_create_function_body_string(&mut self) -> Result<Expr, ParserError> {
8853        let peek_token = self.peek_token();
8854        let span = peek_token.span;
8855        match peek_token.token {
8856            Token::DollarQuotedString(s) if dialect_of!(self is PostgreSqlDialect | GenericDialect) =>
8857            {
8858                self.next_token();
8859                Ok(Expr::Value(Value::DollarQuotedString(s).with_span(span)))
8860            }
8861            _ => Ok(Expr::Value(
8862                Value::SingleQuotedString(self.parse_literal_string()?).with_span(span),
8863            )),
8864        }
8865    }
8866
8867    /// Parse a literal string
8868    pub fn parse_literal_string(&mut self) -> Result<String, ParserError> {
8869        let next_token = self.next_token();
8870        match next_token.token {
8871            Token::Word(Word {
8872                value,
8873                keyword: Keyword::NoKeyword,
8874                ..
8875            }) => Ok(value),
8876            Token::SingleQuotedString(s) => Ok(s),
8877            Token::DoubleQuotedString(s) => Ok(s),
8878            Token::EscapedStringLiteral(s) if dialect_of!(self is PostgreSqlDialect | GenericDialect) => {
8879                Ok(s)
8880            }
8881            Token::UnicodeStringLiteral(s) => Ok(s),
8882            _ => self.expected("literal string", next_token),
8883        }
8884    }
8885
8886    /// Parse a literal unicode normalization clause
8887    pub fn parse_unicode_is_normalized(&mut self, expr: Expr) -> Result<Expr, ParserError> {
8888        let neg = self.parse_keyword(Keyword::NOT);
8889        let normalized_form = self.maybe_parse(|parser| {
8890            match parser.parse_one_of_keywords(&[
8891                Keyword::NFC,
8892                Keyword::NFD,
8893                Keyword::NFKC,
8894                Keyword::NFKD,
8895            ]) {
8896                Some(Keyword::NFC) => Ok(NormalizationForm::NFC),
8897                Some(Keyword::NFD) => Ok(NormalizationForm::NFD),
8898                Some(Keyword::NFKC) => Ok(NormalizationForm::NFKC),
8899                Some(Keyword::NFKD) => Ok(NormalizationForm::NFKD),
8900                _ => parser.expected("unicode normalization form", parser.peek_token()),
8901            }
8902        })?;
8903        if self.parse_keyword(Keyword::NORMALIZED) {
8904            return Ok(Expr::IsNormalized {
8905                expr: Box::new(expr),
8906                form: normalized_form,
8907                negated: neg,
8908            });
8909        }
8910        self.expected("unicode normalization form", self.peek_token())
8911    }
8912
8913    pub fn parse_enum_values(&mut self) -> Result<Vec<EnumMember>, ParserError> {
8914        self.expect_token(&Token::LParen)?;
8915        let values = self.parse_comma_separated(|parser| {
8916            let name = parser.parse_literal_string()?;
8917            let e = if parser.consume_token(&Token::Eq) {
8918                let value = parser.parse_number()?;
8919                EnumMember::NamedValue(name, value)
8920            } else {
8921                EnumMember::Name(name)
8922            };
8923            Ok(e)
8924        })?;
8925        self.expect_token(&Token::RParen)?;
8926
8927        Ok(values)
8928    }
8929
8930    /// Parse a SQL datatype (in the context of a CREATE TABLE statement for example)
8931    pub fn parse_data_type(&mut self) -> Result<DataType, ParserError> {
8932        let (ty, trailing_bracket) = self.parse_data_type_helper()?;
8933        if trailing_bracket.0 {
8934            return parser_err!(
8935                format!("unmatched > after parsing data type {ty}"),
8936                self.peek_token()
8937            );
8938        }
8939
8940        Ok(ty)
8941    }
8942
8943    fn parse_data_type_helper(
8944        &mut self,
8945    ) -> Result<(DataType, MatchedTrailingBracket), ParserError> {
8946        let dialect = self.dialect;
8947        self.advance_token();
8948        let next_token = self.get_current_token();
8949        let next_token_index = self.get_current_index();
8950
8951        let mut trailing_bracket: MatchedTrailingBracket = false.into();
8952        let mut data = match &next_token.token {
8953            Token::Word(w) => match w.keyword {
8954                Keyword::BOOLEAN => Ok(DataType::Boolean),
8955                Keyword::BOOL => Ok(DataType::Bool),
8956                Keyword::FLOAT => Ok(DataType::Float(self.parse_optional_precision()?)),
8957                Keyword::REAL => Ok(DataType::Real),
8958                Keyword::FLOAT4 => Ok(DataType::Float4),
8959                Keyword::FLOAT32 => Ok(DataType::Float32),
8960                Keyword::FLOAT64 => Ok(DataType::Float64),
8961                Keyword::FLOAT8 => Ok(DataType::Float8),
8962                Keyword::DOUBLE => {
8963                    if self.parse_keyword(Keyword::PRECISION) {
8964                        Ok(DataType::DoublePrecision)
8965                    } else {
8966                        Ok(DataType::Double(
8967                            self.parse_exact_number_optional_precision_scale()?,
8968                        ))
8969                    }
8970                }
8971                Keyword::TINYINT => {
8972                    let optional_precision = self.parse_optional_precision();
8973                    if self.parse_keyword(Keyword::UNSIGNED) {
8974                        Ok(DataType::TinyIntUnsigned(optional_precision?))
8975                    } else {
8976                        Ok(DataType::TinyInt(optional_precision?))
8977                    }
8978                }
8979                Keyword::INT2 => {
8980                    let optional_precision = self.parse_optional_precision();
8981                    if self.parse_keyword(Keyword::UNSIGNED) {
8982                        Ok(DataType::Int2Unsigned(optional_precision?))
8983                    } else {
8984                        Ok(DataType::Int2(optional_precision?))
8985                    }
8986                }
8987                Keyword::SMALLINT => {
8988                    let optional_precision = self.parse_optional_precision();
8989                    if self.parse_keyword(Keyword::UNSIGNED) {
8990                        Ok(DataType::SmallIntUnsigned(optional_precision?))
8991                    } else {
8992                        Ok(DataType::SmallInt(optional_precision?))
8993                    }
8994                }
8995                Keyword::MEDIUMINT => {
8996                    let optional_precision = self.parse_optional_precision();
8997                    if self.parse_keyword(Keyword::UNSIGNED) {
8998                        Ok(DataType::MediumIntUnsigned(optional_precision?))
8999                    } else {
9000                        Ok(DataType::MediumInt(optional_precision?))
9001                    }
9002                }
9003                Keyword::INT => {
9004                    let optional_precision = self.parse_optional_precision();
9005                    if self.parse_keyword(Keyword::UNSIGNED) {
9006                        Ok(DataType::IntUnsigned(optional_precision?))
9007                    } else {
9008                        Ok(DataType::Int(optional_precision?))
9009                    }
9010                }
9011                Keyword::INT4 => {
9012                    let optional_precision = self.parse_optional_precision();
9013                    if self.parse_keyword(Keyword::UNSIGNED) {
9014                        Ok(DataType::Int4Unsigned(optional_precision?))
9015                    } else {
9016                        Ok(DataType::Int4(optional_precision?))
9017                    }
9018                }
9019                Keyword::INT8 => {
9020                    let optional_precision = self.parse_optional_precision();
9021                    if self.parse_keyword(Keyword::UNSIGNED) {
9022                        Ok(DataType::Int8Unsigned(optional_precision?))
9023                    } else {
9024                        Ok(DataType::Int8(optional_precision?))
9025                    }
9026                }
9027                Keyword::INT16 => Ok(DataType::Int16),
9028                Keyword::INT32 => Ok(DataType::Int32),
9029                Keyword::INT64 => Ok(DataType::Int64),
9030                Keyword::INT128 => Ok(DataType::Int128),
9031                Keyword::INT256 => Ok(DataType::Int256),
9032                Keyword::INTEGER => {
9033                    let optional_precision = self.parse_optional_precision();
9034                    if self.parse_keyword(Keyword::UNSIGNED) {
9035                        Ok(DataType::IntegerUnsigned(optional_precision?))
9036                    } else {
9037                        Ok(DataType::Integer(optional_precision?))
9038                    }
9039                }
9040                Keyword::BIGINT => {
9041                    let optional_precision = self.parse_optional_precision();
9042                    if self.parse_keyword(Keyword::UNSIGNED) {
9043                        Ok(DataType::BigIntUnsigned(optional_precision?))
9044                    } else {
9045                        Ok(DataType::BigInt(optional_precision?))
9046                    }
9047                }
9048                Keyword::UINT8 => Ok(DataType::UInt8),
9049                Keyword::UINT16 => Ok(DataType::UInt16),
9050                Keyword::UINT32 => Ok(DataType::UInt32),
9051                Keyword::UINT64 => Ok(DataType::UInt64),
9052                Keyword::UINT128 => Ok(DataType::UInt128),
9053                Keyword::UINT256 => Ok(DataType::UInt256),
9054                Keyword::VARCHAR => Ok(DataType::Varchar(self.parse_optional_character_length()?)),
9055                Keyword::NVARCHAR => {
9056                    Ok(DataType::Nvarchar(self.parse_optional_character_length()?))
9057                }
9058                Keyword::CHARACTER => {
9059                    if self.parse_keyword(Keyword::VARYING) {
9060                        Ok(DataType::CharacterVarying(
9061                            self.parse_optional_character_length()?,
9062                        ))
9063                    } else if self.parse_keywords(&[Keyword::LARGE, Keyword::OBJECT]) {
9064                        Ok(DataType::CharacterLargeObject(
9065                            self.parse_optional_precision()?,
9066                        ))
9067                    } else {
9068                        Ok(DataType::Character(self.parse_optional_character_length()?))
9069                    }
9070                }
9071                Keyword::CHAR => {
9072                    if self.parse_keyword(Keyword::VARYING) {
9073                        Ok(DataType::CharVarying(
9074                            self.parse_optional_character_length()?,
9075                        ))
9076                    } else if self.parse_keywords(&[Keyword::LARGE, Keyword::OBJECT]) {
9077                        Ok(DataType::CharLargeObject(self.parse_optional_precision()?))
9078                    } else {
9079                        Ok(DataType::Char(self.parse_optional_character_length()?))
9080                    }
9081                }
9082                Keyword::CLOB => Ok(DataType::Clob(self.parse_optional_precision()?)),
9083                Keyword::BINARY => Ok(DataType::Binary(self.parse_optional_precision()?)),
9084                Keyword::VARBINARY => Ok(DataType::Varbinary(self.parse_optional_binary_length()?)),
9085                Keyword::BLOB => Ok(DataType::Blob(self.parse_optional_precision()?)),
9086                Keyword::TINYBLOB => Ok(DataType::TinyBlob),
9087                Keyword::MEDIUMBLOB => Ok(DataType::MediumBlob),
9088                Keyword::LONGBLOB => Ok(DataType::LongBlob),
9089                Keyword::BYTES => Ok(DataType::Bytes(self.parse_optional_precision()?)),
9090                Keyword::BIT => {
9091                    if self.parse_keyword(Keyword::VARYING) {
9092                        Ok(DataType::BitVarying(self.parse_optional_precision()?))
9093                    } else {
9094                        Ok(DataType::Bit(self.parse_optional_precision()?))
9095                    }
9096                }
9097                Keyword::VARBIT => Ok(DataType::VarBit(self.parse_optional_precision()?)),
9098                Keyword::UUID => Ok(DataType::Uuid),
9099                Keyword::DATE => Ok(DataType::Date),
9100                Keyword::DATE32 => Ok(DataType::Date32),
9101                Keyword::DATETIME => Ok(DataType::Datetime(self.parse_optional_precision()?)),
9102                Keyword::DATETIME64 => {
9103                    self.prev_token();
9104                    let (precision, time_zone) = self.parse_datetime_64()?;
9105                    Ok(DataType::Datetime64(precision, time_zone))
9106                }
9107                Keyword::TIMESTAMP => {
9108                    let precision = self.parse_optional_precision()?;
9109                    let tz = if self.parse_keyword(Keyword::WITH) {
9110                        self.expect_keywords(&[Keyword::TIME, Keyword::ZONE])?;
9111                        TimezoneInfo::WithTimeZone
9112                    } else if self.parse_keyword(Keyword::WITHOUT) {
9113                        self.expect_keywords(&[Keyword::TIME, Keyword::ZONE])?;
9114                        TimezoneInfo::WithoutTimeZone
9115                    } else {
9116                        TimezoneInfo::None
9117                    };
9118                    Ok(DataType::Timestamp(precision, tz))
9119                }
9120                Keyword::TIMESTAMPTZ => Ok(DataType::Timestamp(
9121                    self.parse_optional_precision()?,
9122                    TimezoneInfo::Tz,
9123                )),
9124                Keyword::TIME => {
9125                    let precision = self.parse_optional_precision()?;
9126                    let tz = if self.parse_keyword(Keyword::WITH) {
9127                        self.expect_keywords(&[Keyword::TIME, Keyword::ZONE])?;
9128                        TimezoneInfo::WithTimeZone
9129                    } else if self.parse_keyword(Keyword::WITHOUT) {
9130                        self.expect_keywords(&[Keyword::TIME, Keyword::ZONE])?;
9131                        TimezoneInfo::WithoutTimeZone
9132                    } else {
9133                        TimezoneInfo::None
9134                    };
9135                    Ok(DataType::Time(precision, tz))
9136                }
9137                Keyword::TIMETZ => Ok(DataType::Time(
9138                    self.parse_optional_precision()?,
9139                    TimezoneInfo::Tz,
9140                )),
9141                // Interval types can be followed by a complicated interval
9142                // qualifier that we don't currently support. See
9143                // parse_interval for a taste.
9144                Keyword::INTERVAL => Ok(DataType::Interval),
9145                Keyword::JSON => Ok(DataType::JSON),
9146                Keyword::JSONB => Ok(DataType::JSONB),
9147                Keyword::REGCLASS => Ok(DataType::Regclass),
9148                Keyword::STRING => Ok(DataType::String(self.parse_optional_precision()?)),
9149                Keyword::FIXEDSTRING => {
9150                    self.expect_token(&Token::LParen)?;
9151                    let character_length = self.parse_literal_uint()?;
9152                    self.expect_token(&Token::RParen)?;
9153                    Ok(DataType::FixedString(character_length))
9154                }
9155                Keyword::TEXT => Ok(DataType::Text),
9156                Keyword::TINYTEXT => Ok(DataType::TinyText),
9157                Keyword::MEDIUMTEXT => Ok(DataType::MediumText),
9158                Keyword::LONGTEXT => Ok(DataType::LongText),
9159                Keyword::BYTEA => Ok(DataType::Bytea),
9160                Keyword::NUMERIC => Ok(DataType::Numeric(
9161                    self.parse_exact_number_optional_precision_scale()?,
9162                )),
9163                Keyword::DECIMAL => Ok(DataType::Decimal(
9164                    self.parse_exact_number_optional_precision_scale()?,
9165                )),
9166                Keyword::DEC => Ok(DataType::Dec(
9167                    self.parse_exact_number_optional_precision_scale()?,
9168                )),
9169                Keyword::BIGNUMERIC => Ok(DataType::BigNumeric(
9170                    self.parse_exact_number_optional_precision_scale()?,
9171                )),
9172                Keyword::BIGDECIMAL => Ok(DataType::BigDecimal(
9173                    self.parse_exact_number_optional_precision_scale()?,
9174                )),
9175                Keyword::ENUM => Ok(DataType::Enum(self.parse_enum_values()?, None)),
9176                Keyword::ENUM8 => Ok(DataType::Enum(self.parse_enum_values()?, Some(8))),
9177                Keyword::ENUM16 => Ok(DataType::Enum(self.parse_enum_values()?, Some(16))),
9178                Keyword::SET => Ok(DataType::Set(self.parse_string_values()?)),
9179                Keyword::ARRAY => {
9180                    if dialect_of!(self is SnowflakeDialect) {
9181                        Ok(DataType::Array(ArrayElemTypeDef::None))
9182                    } else if dialect_of!(self is ClickHouseDialect) {
9183                        Ok(self.parse_sub_type(|internal_type| {
9184                            DataType::Array(ArrayElemTypeDef::Parenthesis(internal_type))
9185                        })?)
9186                    } else {
9187                        self.expect_token(&Token::Lt)?;
9188                        let (inside_type, _trailing_bracket) = self.parse_data_type_helper()?;
9189                        trailing_bracket = self.expect_closing_angle_bracket(_trailing_bracket)?;
9190                        Ok(DataType::Array(ArrayElemTypeDef::AngleBracket(Box::new(
9191                            inside_type,
9192                        ))))
9193                    }
9194                }
9195                Keyword::STRUCT if dialect_is!(dialect is DuckDbDialect) => {
9196                    self.prev_token();
9197                    let field_defs = self.parse_duckdb_struct_type_def()?;
9198                    Ok(DataType::Struct(field_defs, StructBracketKind::Parentheses))
9199                }
9200                Keyword::STRUCT if dialect_is!(dialect is BigQueryDialect | GenericDialect) => {
9201                    self.prev_token();
9202                    let (field_defs, _trailing_bracket) =
9203                        self.parse_struct_type_def(Self::parse_struct_field_def)?;
9204                    trailing_bracket = _trailing_bracket;
9205                    Ok(DataType::Struct(
9206                        field_defs,
9207                        StructBracketKind::AngleBrackets,
9208                    ))
9209                }
9210                Keyword::UNION if dialect_is!(dialect is DuckDbDialect | GenericDialect) => {
9211                    self.prev_token();
9212                    let fields = self.parse_union_type_def()?;
9213                    Ok(DataType::Union(fields))
9214                }
9215                Keyword::NULLABLE if dialect_is!(dialect is ClickHouseDialect | GenericDialect) => {
9216                    Ok(self.parse_sub_type(DataType::Nullable)?)
9217                }
9218                Keyword::LOWCARDINALITY if dialect_is!(dialect is ClickHouseDialect | GenericDialect) => {
9219                    Ok(self.parse_sub_type(DataType::LowCardinality)?)
9220                }
9221                Keyword::MAP if dialect_is!(dialect is ClickHouseDialect | GenericDialect) => {
9222                    self.prev_token();
9223                    let (key_data_type, value_data_type) = self.parse_click_house_map_def()?;
9224                    Ok(DataType::Map(
9225                        Box::new(key_data_type),
9226                        Box::new(value_data_type),
9227                    ))
9228                }
9229                Keyword::NESTED if dialect_is!(dialect is ClickHouseDialect | GenericDialect) => {
9230                    self.expect_token(&Token::LParen)?;
9231                    let field_defs = self.parse_comma_separated(Parser::parse_column_def)?;
9232                    self.expect_token(&Token::RParen)?;
9233                    Ok(DataType::Nested(field_defs))
9234                }
9235                Keyword::TUPLE if dialect_is!(dialect is ClickHouseDialect | GenericDialect) => {
9236                    self.prev_token();
9237                    let field_defs = self.parse_click_house_tuple_def()?;
9238                    Ok(DataType::Tuple(field_defs))
9239                }
9240                Keyword::TRIGGER => Ok(DataType::Trigger),
9241                Keyword::ANY if self.peek_keyword(Keyword::TYPE) => {
9242                    let _ = self.parse_keyword(Keyword::TYPE);
9243                    Ok(DataType::AnyType)
9244                }
9245                Keyword::TABLE => {
9246                    let columns = self.parse_returns_table_columns()?;
9247                    Ok(DataType::Table(columns))
9248                }
9249                Keyword::SIGNED => {
9250                    if self.parse_keyword(Keyword::INTEGER) {
9251                        Ok(DataType::SignedInteger)
9252                    } else {
9253                        Ok(DataType::Signed)
9254                    }
9255                }
9256                Keyword::UNSIGNED => {
9257                    if self.parse_keyword(Keyword::INTEGER) {
9258                        Ok(DataType::UnsignedInteger)
9259                    } else {
9260                        Ok(DataType::Unsigned)
9261                    }
9262                }
9263                _ => {
9264                    self.prev_token();
9265                    let type_name = self.parse_object_name(false)?;
9266                    if let Some(modifiers) = self.parse_optional_type_modifiers()? {
9267                        Ok(DataType::Custom(type_name, modifiers))
9268                    } else {
9269                        Ok(DataType::Custom(type_name, vec![]))
9270                    }
9271                }
9272            },
9273            _ => self.expected_at("a data type name", next_token_index),
9274        }?;
9275
9276        if self.dialect.supports_array_typedef_with_brackets() {
9277            while self.consume_token(&Token::LBracket) {
9278                // Parse optional array data type size
9279                let size = self.maybe_parse(|p| p.parse_literal_uint())?;
9280                self.expect_token(&Token::RBracket)?;
9281                data = DataType::Array(ArrayElemTypeDef::SquareBracket(Box::new(data), size))
9282            }
9283        }
9284        Ok((data, trailing_bracket))
9285    }
9286
9287    fn parse_returns_table_column(&mut self) -> Result<ColumnDef, ParserError> {
9288        let name = self.parse_identifier()?;
9289        let data_type = self.parse_data_type()?;
9290        Ok(ColumnDef {
9291            name,
9292            data_type,
9293            options: Vec::new(), // No constraints expected here
9294        })
9295    }
9296
9297    fn parse_returns_table_columns(&mut self) -> Result<Vec<ColumnDef>, ParserError> {
9298        self.expect_token(&Token::LParen)?;
9299        let columns = self.parse_comma_separated(Parser::parse_returns_table_column)?;
9300        self.expect_token(&Token::RParen)?;
9301        Ok(columns)
9302    }
9303
9304    pub fn parse_string_values(&mut self) -> Result<Vec<String>, ParserError> {
9305        self.expect_token(&Token::LParen)?;
9306        let mut values = Vec::new();
9307        loop {
9308            let next_token = self.next_token();
9309            match next_token.token {
9310                Token::SingleQuotedString(value) => values.push(value),
9311                _ => self.expected("a string", next_token)?,
9312            }
9313            let next_token = self.next_token();
9314            match next_token.token {
9315                Token::Comma => (),
9316                Token::RParen => break,
9317                _ => self.expected(", or }", next_token)?,
9318            }
9319        }
9320        Ok(values)
9321    }
9322
9323    /// Strictly parse `identifier AS identifier`
9324    pub fn parse_identifier_with_alias(&mut self) -> Result<IdentWithAlias, ParserError> {
9325        let ident = self.parse_identifier()?;
9326        self.expect_keyword_is(Keyword::AS)?;
9327        let alias = self.parse_identifier()?;
9328        Ok(IdentWithAlias { ident, alias })
9329    }
9330
9331    /// Optionally parses an alias for a select list item
9332    fn maybe_parse_select_item_alias(&mut self) -> Result<Option<Ident>, ParserError> {
9333        fn validator(explicit: bool, kw: &Keyword, parser: &mut Parser) -> bool {
9334            parser.dialect.is_select_item_alias(explicit, kw, parser)
9335        }
9336        self.parse_optional_alias_inner(None, validator)
9337    }
9338
9339    /// Optionally parses an alias for a table like in `... FROM generate_series(1, 10) AS t (col)`.
9340    /// In this case, the alias is allowed to optionally name the columns in the table, in
9341    /// addition to the table itself.
9342    pub fn maybe_parse_table_alias(&mut self) -> Result<Option<TableAlias>, ParserError> {
9343        fn validator(explicit: bool, kw: &Keyword, parser: &mut Parser) -> bool {
9344            parser.dialect.is_table_factor_alias(explicit, kw, parser)
9345        }
9346        match self.parse_optional_alias_inner(None, validator)? {
9347            Some(name) => {
9348                let columns = self.parse_table_alias_column_defs()?;
9349                Ok(Some(TableAlias { name, columns }))
9350            }
9351            None => Ok(None),
9352        }
9353    }
9354
9355    fn parse_table_index_hints(&mut self) -> Result<Vec<TableIndexHints>, ParserError> {
9356        let mut hints = vec![];
9357        while let Some(hint_type) =
9358            self.parse_one_of_keywords(&[Keyword::USE, Keyword::IGNORE, Keyword::FORCE])
9359        {
9360            let hint_type = match hint_type {
9361                Keyword::USE => TableIndexHintType::Use,
9362                Keyword::IGNORE => TableIndexHintType::Ignore,
9363                Keyword::FORCE => TableIndexHintType::Force,
9364                _ => {
9365                    return self.expected(
9366                        "expected to match USE/IGNORE/FORCE keyword",
9367                        self.peek_token(),
9368                    )
9369                }
9370            };
9371            let index_type = match self.parse_one_of_keywords(&[Keyword::INDEX, Keyword::KEY]) {
9372                Some(Keyword::INDEX) => TableIndexType::Index,
9373                Some(Keyword::KEY) => TableIndexType::Key,
9374                _ => {
9375                    return self.expected("expected to match INDEX/KEY keyword", self.peek_token())
9376                }
9377            };
9378            let for_clause = if self.parse_keyword(Keyword::FOR) {
9379                let clause = if self.parse_keyword(Keyword::JOIN) {
9380                    TableIndexHintForClause::Join
9381                } else if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
9382                    TableIndexHintForClause::OrderBy
9383                } else if self.parse_keywords(&[Keyword::GROUP, Keyword::BY]) {
9384                    TableIndexHintForClause::GroupBy
9385                } else {
9386                    return self.expected(
9387                        "expected to match FOR/ORDER BY/GROUP BY table hint in for clause",
9388                        self.peek_token(),
9389                    );
9390                };
9391                Some(clause)
9392            } else {
9393                None
9394            };
9395
9396            self.expect_token(&Token::LParen)?;
9397            let index_names = if self.peek_token().token != Token::RParen {
9398                self.parse_comma_separated(Parser::parse_identifier)?
9399            } else {
9400                vec![]
9401            };
9402            self.expect_token(&Token::RParen)?;
9403            hints.push(TableIndexHints {
9404                hint_type,
9405                index_type,
9406                for_clause,
9407                index_names,
9408            });
9409        }
9410        Ok(hints)
9411    }
9412
9413    /// Wrapper for parse_optional_alias_inner, left for backwards-compatibility
9414    /// but new flows should use the context-specific methods such as `maybe_parse_select_item_alias`
9415    /// and `maybe_parse_table_alias`.
9416    pub fn parse_optional_alias(
9417        &mut self,
9418        reserved_kwds: &[Keyword],
9419    ) -> Result<Option<Ident>, ParserError> {
9420        fn validator(_explicit: bool, _kw: &Keyword, _parser: &mut Parser) -> bool {
9421            false
9422        }
9423        self.parse_optional_alias_inner(Some(reserved_kwds), validator)
9424    }
9425
9426    /// Parses an optional alias after a SQL element such as a select list item
9427    /// or a table name.
9428    ///
9429    /// This method accepts an optional list of reserved keywords or a function
9430    /// to call to validate if a keyword should be parsed as an alias, to allow
9431    /// callers to customize the parsing logic based on their context.
9432    fn parse_optional_alias_inner<F>(
9433        &mut self,
9434        reserved_kwds: Option<&[Keyword]>,
9435        validator: F,
9436    ) -> Result<Option<Ident>, ParserError>
9437    where
9438        F: Fn(bool, &Keyword, &mut Parser) -> bool,
9439    {
9440        let after_as = self.parse_keyword(Keyword::AS);
9441
9442        let next_token = self.next_token();
9443        match next_token.token {
9444            // By default, if a word is located after the `AS` keyword we consider it an alias
9445            // as long as it's not reserved.
9446            Token::Word(w)
9447                if after_as || reserved_kwds.is_some_and(|x| !x.contains(&w.keyword)) =>
9448            {
9449                Ok(Some(w.into_ident(next_token.span)))
9450            }
9451            // This pattern allows for customizing the acceptance of words as aliases based on the caller's
9452            // context, such as to what SQL element this word is a potential alias of (select item alias, table name
9453            // alias, etc.) or dialect-specific logic that goes beyond a simple list of reserved keywords.
9454            Token::Word(w) if validator(after_as, &w.keyword, self) => {
9455                Ok(Some(w.into_ident(next_token.span)))
9456            }
9457            // For backwards-compatibility, we accept quoted strings as aliases regardless of the context.
9458            Token::SingleQuotedString(s) => Ok(Some(Ident::with_quote('\'', s))),
9459            Token::DoubleQuotedString(s) => Ok(Some(Ident::with_quote('\"', s))),
9460            _ => {
9461                if after_as {
9462                    return self.expected("an identifier after AS", next_token);
9463                }
9464                self.prev_token();
9465                Ok(None) // no alias found
9466            }
9467        }
9468    }
9469
9470    pub fn parse_optional_group_by(&mut self) -> Result<Option<GroupByExpr>, ParserError> {
9471        if self.parse_keywords(&[Keyword::GROUP, Keyword::BY]) {
9472            let expressions = if self.parse_keyword(Keyword::ALL) {
9473                None
9474            } else {
9475                Some(self.parse_comma_separated(Parser::parse_group_by_expr)?)
9476            };
9477
9478            let mut modifiers = vec![];
9479            if self.dialect.supports_group_by_with_modifier() {
9480                loop {
9481                    if !self.parse_keyword(Keyword::WITH) {
9482                        break;
9483                    }
9484                    let keyword = self.expect_one_of_keywords(&[
9485                        Keyword::ROLLUP,
9486                        Keyword::CUBE,
9487                        Keyword::TOTALS,
9488                    ])?;
9489                    modifiers.push(match keyword {
9490                        Keyword::ROLLUP => GroupByWithModifier::Rollup,
9491                        Keyword::CUBE => GroupByWithModifier::Cube,
9492                        Keyword::TOTALS => GroupByWithModifier::Totals,
9493                        _ => {
9494                            return parser_err!(
9495                                "BUG: expected to match GroupBy modifier keyword",
9496                                self.peek_token().span.start
9497                            )
9498                        }
9499                    });
9500                }
9501            }
9502            if self.parse_keywords(&[Keyword::GROUPING, Keyword::SETS]) {
9503                self.expect_token(&Token::LParen)?;
9504                let result = self.parse_comma_separated(|p| p.parse_tuple(true, true))?;
9505                self.expect_token(&Token::RParen)?;
9506                modifiers.push(GroupByWithModifier::GroupingSets(Expr::GroupingSets(
9507                    result,
9508                )));
9509            };
9510            let group_by = match expressions {
9511                None => GroupByExpr::All(modifiers),
9512                Some(exprs) => GroupByExpr::Expressions(exprs, modifiers),
9513            };
9514            Ok(Some(group_by))
9515        } else {
9516            Ok(None)
9517        }
9518    }
9519
9520    pub fn parse_optional_order_by(&mut self) -> Result<Option<OrderBy>, ParserError> {
9521        if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
9522            let order_by =
9523                if self.dialect.supports_order_by_all() && self.parse_keyword(Keyword::ALL) {
9524                    let order_by_options = self.parse_order_by_options()?;
9525                    OrderBy {
9526                        kind: OrderByKind::All(order_by_options),
9527                        interpolate: None,
9528                    }
9529                } else {
9530                    let exprs = self.parse_comma_separated(Parser::parse_order_by_expr)?;
9531                    let interpolate = if dialect_of!(self is ClickHouseDialect | GenericDialect) {
9532                        self.parse_interpolations()?
9533                    } else {
9534                        None
9535                    };
9536                    OrderBy {
9537                        kind: OrderByKind::Expressions(exprs),
9538                        interpolate,
9539                    }
9540                };
9541            Ok(Some(order_by))
9542        } else {
9543            Ok(None)
9544        }
9545    }
9546
9547    /// Parse a table object for insertion
9548    /// e.g. `some_database.some_table` or `FUNCTION some_table_func(...)`
9549    pub fn parse_table_object(&mut self) -> Result<TableObject, ParserError> {
9550        if self.dialect.supports_insert_table_function() && self.parse_keyword(Keyword::FUNCTION) {
9551            let fn_name = self.parse_object_name(false)?;
9552            self.parse_function_call(fn_name)
9553                .map(TableObject::TableFunction)
9554        } else {
9555            self.parse_object_name(false).map(TableObject::TableName)
9556        }
9557    }
9558
9559    /// Parse a possibly qualified, possibly quoted identifier, optionally allowing for wildcards,
9560    /// e.g. *, *.*, `foo`.*, or "foo"."bar"
9561    fn parse_object_name_with_wildcards(
9562        &mut self,
9563        in_table_clause: bool,
9564        allow_wildcards: bool,
9565    ) -> Result<ObjectName, ParserError> {
9566        let mut idents = vec![];
9567
9568        if dialect_of!(self is BigQueryDialect) && in_table_clause {
9569            loop {
9570                let (ident, end_with_period) = self.parse_unquoted_hyphenated_identifier()?;
9571                idents.push(ident);
9572                if !self.consume_token(&Token::Period) && !end_with_period {
9573                    break;
9574                }
9575            }
9576        } else {
9577            loop {
9578                let ident = if allow_wildcards && self.peek_token().token == Token::Mul {
9579                    let span = self.next_token().span;
9580                    Ident {
9581                        value: Token::Mul.to_string(),
9582                        quote_style: None,
9583                        span,
9584                    }
9585                } else {
9586                    if self.dialect.supports_object_name_double_dot_notation()
9587                        && idents.len() == 1
9588                        && self.consume_token(&Token::Period)
9589                    {
9590                        // Empty string here means default schema
9591                        idents.push(Ident::new(""));
9592                    }
9593                    self.parse_identifier()?
9594                };
9595                idents.push(ident);
9596                if !self.consume_token(&Token::Period) {
9597                    break;
9598                }
9599            }
9600        }
9601        Ok(ObjectName::from(idents))
9602    }
9603
9604    /// Parse a possibly qualified, possibly quoted identifier, e.g.
9605    /// `foo` or `myschema."table"
9606    ///
9607    /// The `in_table_clause` parameter indicates whether the object name is a table in a FROM, JOIN,
9608    /// or similar table clause. Currently, this is used only to support unquoted hyphenated identifiers
9609    /// in this context on BigQuery.
9610    pub fn parse_object_name(&mut self, in_table_clause: bool) -> Result<ObjectName, ParserError> {
9611        let ObjectName(mut idents) =
9612            self.parse_object_name_with_wildcards(in_table_clause, false)?;
9613
9614        // BigQuery accepts any number of quoted identifiers of a table name.
9615        // https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical#quoted_identifiers
9616        if dialect_of!(self is BigQueryDialect)
9617            && idents.iter().any(|part| {
9618                part.as_ident()
9619                    .is_some_and(|ident| ident.value.contains('.'))
9620            })
9621        {
9622            idents = idents
9623                .into_iter()
9624                .flat_map(|part| match part.as_ident() {
9625                    Some(ident) => ident
9626                        .value
9627                        .split('.')
9628                        .map(|value| {
9629                            ObjectNamePart::Identifier(Ident {
9630                                value: value.into(),
9631                                quote_style: ident.quote_style,
9632                                span: ident.span,
9633                            })
9634                        })
9635                        .collect::<Vec<_>>(),
9636                    None => vec![part],
9637                })
9638                .collect()
9639        }
9640
9641        Ok(ObjectName(idents))
9642    }
9643
9644    /// Parse identifiers
9645    pub fn parse_identifiers(&mut self) -> Result<Vec<Ident>, ParserError> {
9646        let mut idents = vec![];
9647        loop {
9648            match &self.peek_token_ref().token {
9649                Token::Word(w) => {
9650                    idents.push(w.clone().into_ident(self.peek_token_ref().span));
9651                }
9652                Token::EOF | Token::Eq => break,
9653                _ => {}
9654            }
9655            self.advance_token();
9656        }
9657        Ok(idents)
9658    }
9659
9660    /// Parse identifiers of form ident1[.identN]*
9661    ///
9662    /// Similar in functionality to [parse_identifiers], with difference
9663    /// being this function is much more strict about parsing a valid multipart identifier, not
9664    /// allowing extraneous tokens to be parsed, otherwise it fails.
9665    ///
9666    /// For example:
9667    ///
9668    /// ```rust
9669    /// use sqlparser::ast::Ident;
9670    /// use sqlparser::dialect::GenericDialect;
9671    /// use sqlparser::parser::Parser;
9672    ///
9673    /// let dialect = GenericDialect {};
9674    /// let expected = vec![Ident::new("one"), Ident::new("two")];
9675    ///
9676    /// // expected usage
9677    /// let sql = "one.two";
9678    /// let mut parser = Parser::new(&dialect).try_with_sql(sql).unwrap();
9679    /// let actual = parser.parse_multipart_identifier().unwrap();
9680    /// assert_eq!(&actual, &expected);
9681    ///
9682    /// // parse_identifiers is more loose on what it allows, parsing successfully
9683    /// let sql = "one + two";
9684    /// let mut parser = Parser::new(&dialect).try_with_sql(sql).unwrap();
9685    /// let actual = parser.parse_identifiers().unwrap();
9686    /// assert_eq!(&actual, &expected);
9687    ///
9688    /// // expected to strictly fail due to + separator
9689    /// let sql = "one + two";
9690    /// let mut parser = Parser::new(&dialect).try_with_sql(sql).unwrap();
9691    /// let actual = parser.parse_multipart_identifier().unwrap_err();
9692    /// assert_eq!(
9693    ///     actual.to_string(),
9694    ///     "sql parser error: Unexpected token in identifier: +"
9695    /// );
9696    /// ```
9697    ///
9698    /// [parse_identifiers]: Parser::parse_identifiers
9699    pub fn parse_multipart_identifier(&mut self) -> Result<Vec<Ident>, ParserError> {
9700        let mut idents = vec![];
9701
9702        // expecting at least one word for identifier
9703        let next_token = self.next_token();
9704        match next_token.token {
9705            Token::Word(w) => idents.push(w.into_ident(next_token.span)),
9706            Token::EOF => {
9707                return Err(ParserError::ParserError(
9708                    "Empty input when parsing identifier".to_string(),
9709                ))?
9710            }
9711            token => {
9712                return Err(ParserError::ParserError(format!(
9713                    "Unexpected token in identifier: {token}"
9714                )))?
9715            }
9716        };
9717
9718        // parse optional next parts if exist
9719        loop {
9720            match self.next_token().token {
9721                // ensure that optional period is succeeded by another identifier
9722                Token::Period => {
9723                    let next_token = self.next_token();
9724                    match next_token.token {
9725                        Token::Word(w) => idents.push(w.into_ident(next_token.span)),
9726                        Token::EOF => {
9727                            return Err(ParserError::ParserError(
9728                                "Trailing period in identifier".to_string(),
9729                            ))?
9730                        }
9731                        token => {
9732                            return Err(ParserError::ParserError(format!(
9733                                "Unexpected token following period in identifier: {token}"
9734                            )))?
9735                        }
9736                    }
9737                }
9738                Token::EOF => break,
9739                token => {
9740                    return Err(ParserError::ParserError(format!(
9741                        "Unexpected token in identifier: {token}"
9742                    )))?
9743                }
9744            }
9745        }
9746
9747        Ok(idents)
9748    }
9749
9750    /// Parse a simple one-word identifier (possibly quoted, possibly a keyword)
9751    pub fn parse_identifier(&mut self) -> Result<Ident, ParserError> {
9752        let next_token = self.next_token();
9753        match next_token.token {
9754            Token::Word(w) => Ok(w.into_ident(next_token.span)),
9755            Token::SingleQuotedString(s) => Ok(Ident::with_quote('\'', s)),
9756            Token::DoubleQuotedString(s) => Ok(Ident::with_quote('\"', s)),
9757            _ => self.expected("identifier", next_token),
9758        }
9759    }
9760
9761    /// On BigQuery, hyphens are permitted in unquoted identifiers inside of a FROM or
9762    /// TABLE clause.
9763    ///
9764    /// The first segment must be an ordinary unquoted identifier, e.g. it must not start
9765    /// with a digit. Subsequent segments are either must either be valid identifiers or
9766    /// integers, e.g. foo-123 is allowed, but foo-123a is not.
9767    ///
9768    /// [BigQuery-lexical](https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical)
9769    ///
9770    /// Return a tuple of the identifier and a boolean indicating it ends with a period.
9771    fn parse_unquoted_hyphenated_identifier(&mut self) -> Result<(Ident, bool), ParserError> {
9772        match self.peek_token().token {
9773            Token::Word(w) => {
9774                let quote_style_is_none = w.quote_style.is_none();
9775                let mut requires_whitespace = false;
9776                let mut ident = w.into_ident(self.next_token().span);
9777                if quote_style_is_none {
9778                    while matches!(self.peek_token_no_skip().token, Token::Minus) {
9779                        self.next_token();
9780                        ident.value.push('-');
9781
9782                        let token = self
9783                            .next_token_no_skip()
9784                            .cloned()
9785                            .unwrap_or(TokenWithSpan::wrap(Token::EOF));
9786                        requires_whitespace = match token.token {
9787                            Token::Word(next_word) if next_word.quote_style.is_none() => {
9788                                ident.value.push_str(&next_word.value);
9789                                false
9790                            }
9791                            Token::Number(s, false) => {
9792                                // A number token can represent a decimal value ending with a period, e.g., `Number('123.')`.
9793                                // However, for an [ObjectName], it is part of a hyphenated identifier, e.g., `foo-123.bar`.
9794                                //
9795                                // If a number token is followed by a period, it is part of an [ObjectName].
9796                                // Return the identifier with `true` if the number token is followed by a period, indicating that
9797                                // parsing should continue for the next part of the hyphenated identifier.
9798                                if s.ends_with('.') {
9799                                    let Some(s) = s.split('.').next().filter(|s| {
9800                                        !s.is_empty() && s.chars().all(|c| c.is_ascii_digit())
9801                                    }) else {
9802                                        return self.expected(
9803                                            "continuation of hyphenated identifier",
9804                                            TokenWithSpan::new(Token::Number(s, false), token.span),
9805                                        );
9806                                    };
9807                                    ident.value.push_str(s);
9808                                    return Ok((ident, true));
9809                                } else {
9810                                    ident.value.push_str(&s);
9811                                }
9812                                // If next token is period, then it is part of an ObjectName and we don't expect whitespace
9813                                // after the number.
9814                                !matches!(self.peek_token().token, Token::Period)
9815                            }
9816                            _ => {
9817                                return self
9818                                    .expected("continuation of hyphenated identifier", token);
9819                            }
9820                        }
9821                    }
9822
9823                    // If the last segment was a number, we must check that it's followed by whitespace,
9824                    // otherwise foo-123a will be parsed as `foo-123` with the alias `a`.
9825                    if requires_whitespace {
9826                        let token = self.next_token();
9827                        if !matches!(token.token, Token::EOF | Token::Whitespace(_)) {
9828                            return self
9829                                .expected("whitespace following hyphenated identifier", token);
9830                        }
9831                    }
9832                }
9833                Ok((ident, false))
9834            }
9835            _ => Ok((self.parse_identifier()?, false)),
9836        }
9837    }
9838
9839    /// Parses a parenthesized, comma-separated list of column definitions within a view.
9840    fn parse_view_columns(&mut self) -> Result<Vec<ViewColumnDef>, ParserError> {
9841        if self.consume_token(&Token::LParen) {
9842            if self.peek_token().token == Token::RParen {
9843                self.next_token();
9844                Ok(vec![])
9845            } else {
9846                let cols = self.parse_comma_separated_with_trailing_commas(
9847                    Parser::parse_view_column,
9848                    self.dialect.supports_column_definition_trailing_commas(),
9849                    Self::is_reserved_for_column_alias,
9850                )?;
9851                self.expect_token(&Token::RParen)?;
9852                Ok(cols)
9853            }
9854        } else {
9855            Ok(vec![])
9856        }
9857    }
9858
9859    /// Parses a column definition within a view.
9860    fn parse_view_column(&mut self) -> Result<ViewColumnDef, ParserError> {
9861        let name = self.parse_identifier()?;
9862        let options = if (dialect_of!(self is BigQueryDialect | GenericDialect)
9863            && self.parse_keyword(Keyword::OPTIONS))
9864            || (dialect_of!(self is SnowflakeDialect | GenericDialect)
9865                && self.parse_keyword(Keyword::COMMENT))
9866        {
9867            self.prev_token();
9868            self.parse_optional_column_option()?
9869                .map(|option| vec![option])
9870        } else {
9871            None
9872        };
9873        let data_type = if dialect_of!(self is ClickHouseDialect) {
9874            Some(self.parse_data_type()?)
9875        } else {
9876            None
9877        };
9878        Ok(ViewColumnDef {
9879            name,
9880            data_type,
9881            options,
9882        })
9883    }
9884
9885    /// Parses a parenthesized comma-separated list of unqualified, possibly quoted identifiers.
9886    /// For example: `(col1, "col 2", ...)`
9887    pub fn parse_parenthesized_column_list(
9888        &mut self,
9889        optional: IsOptional,
9890        allow_empty: bool,
9891    ) -> Result<Vec<Ident>, ParserError> {
9892        self.parse_parenthesized_column_list_inner(optional, allow_empty, |p| p.parse_identifier())
9893    }
9894
9895    /// Parses a parenthesized comma-separated list of qualified, possibly quoted identifiers.
9896    /// For example: `(db1.sc1.tbl1.col1, db1.sc1.tbl1."col 2", ...)`
9897    pub fn parse_parenthesized_qualified_column_list(
9898        &mut self,
9899        optional: IsOptional,
9900        allow_empty: bool,
9901    ) -> Result<Vec<ObjectName>, ParserError> {
9902        self.parse_parenthesized_column_list_inner(optional, allow_empty, |p| {
9903            p.parse_object_name(true)
9904        })
9905    }
9906
9907    /// Parses a parenthesized comma-separated list of columns using
9908    /// the provided function to parse each element.
9909    fn parse_parenthesized_column_list_inner<F, T>(
9910        &mut self,
9911        optional: IsOptional,
9912        allow_empty: bool,
9913        mut f: F,
9914    ) -> Result<Vec<T>, ParserError>
9915    where
9916        F: FnMut(&mut Parser) -> Result<T, ParserError>,
9917    {
9918        if self.consume_token(&Token::LParen) {
9919            if allow_empty && self.peek_token().token == Token::RParen {
9920                self.next_token();
9921                Ok(vec![])
9922            } else {
9923                let cols = self.parse_comma_separated(|p| f(p))?;
9924                self.expect_token(&Token::RParen)?;
9925                Ok(cols)
9926            }
9927        } else if optional == Optional {
9928            Ok(vec![])
9929        } else {
9930            self.expected("a list of columns in parentheses", self.peek_token())
9931        }
9932    }
9933
9934    /// Parses a parenthesized comma-separated list of table alias column definitions.
9935    fn parse_table_alias_column_defs(&mut self) -> Result<Vec<TableAliasColumnDef>, ParserError> {
9936        if self.consume_token(&Token::LParen) {
9937            let cols = self.parse_comma_separated(|p| {
9938                let name = p.parse_identifier()?;
9939                let data_type = p.maybe_parse(|p| p.parse_data_type())?;
9940                Ok(TableAliasColumnDef { name, data_type })
9941            })?;
9942            self.expect_token(&Token::RParen)?;
9943            Ok(cols)
9944        } else {
9945            Ok(vec![])
9946        }
9947    }
9948
9949    pub fn parse_precision(&mut self) -> Result<u64, ParserError> {
9950        self.expect_token(&Token::LParen)?;
9951        let n = self.parse_literal_uint()?;
9952        self.expect_token(&Token::RParen)?;
9953        Ok(n)
9954    }
9955
9956    pub fn parse_optional_precision(&mut self) -> Result<Option<u64>, ParserError> {
9957        if self.consume_token(&Token::LParen) {
9958            let n = self.parse_literal_uint()?;
9959            self.expect_token(&Token::RParen)?;
9960            Ok(Some(n))
9961        } else {
9962            Ok(None)
9963        }
9964    }
9965
9966    /// Parse datetime64 [1]
9967    /// Syntax
9968    /// ```sql
9969    /// DateTime64(precision[, timezone])
9970    /// ```
9971    ///
9972    /// [1]: https://clickhouse.com/docs/en/sql-reference/data-types/datetime64
9973    pub fn parse_datetime_64(&mut self) -> Result<(u64, Option<String>), ParserError> {
9974        self.expect_keyword_is(Keyword::DATETIME64)?;
9975        self.expect_token(&Token::LParen)?;
9976        let precision = self.parse_literal_uint()?;
9977        let time_zone = if self.consume_token(&Token::Comma) {
9978            Some(self.parse_literal_string()?)
9979        } else {
9980            None
9981        };
9982        self.expect_token(&Token::RParen)?;
9983        Ok((precision, time_zone))
9984    }
9985
9986    pub fn parse_optional_character_length(
9987        &mut self,
9988    ) -> Result<Option<CharacterLength>, ParserError> {
9989        if self.consume_token(&Token::LParen) {
9990            let character_length = self.parse_character_length()?;
9991            self.expect_token(&Token::RParen)?;
9992            Ok(Some(character_length))
9993        } else {
9994            Ok(None)
9995        }
9996    }
9997
9998    pub fn parse_optional_binary_length(&mut self) -> Result<Option<BinaryLength>, ParserError> {
9999        if self.consume_token(&Token::LParen) {
10000            let binary_length = self.parse_binary_length()?;
10001            self.expect_token(&Token::RParen)?;
10002            Ok(Some(binary_length))
10003        } else {
10004            Ok(None)
10005        }
10006    }
10007
10008    pub fn parse_character_length(&mut self) -> Result<CharacterLength, ParserError> {
10009        if self.parse_keyword(Keyword::MAX) {
10010            return Ok(CharacterLength::Max);
10011        }
10012        let length = self.parse_literal_uint()?;
10013        let unit = if self.parse_keyword(Keyword::CHARACTERS) {
10014            Some(CharLengthUnits::Characters)
10015        } else if self.parse_keyword(Keyword::OCTETS) {
10016            Some(CharLengthUnits::Octets)
10017        } else {
10018            None
10019        };
10020        Ok(CharacterLength::IntegerLength { length, unit })
10021    }
10022
10023    pub fn parse_binary_length(&mut self) -> Result<BinaryLength, ParserError> {
10024        if self.parse_keyword(Keyword::MAX) {
10025            return Ok(BinaryLength::Max);
10026        }
10027        let length = self.parse_literal_uint()?;
10028        Ok(BinaryLength::IntegerLength { length })
10029    }
10030
10031    pub fn parse_optional_precision_scale(
10032        &mut self,
10033    ) -> Result<(Option<u64>, Option<u64>), ParserError> {
10034        if self.consume_token(&Token::LParen) {
10035            let n = self.parse_literal_uint()?;
10036            let scale = if self.consume_token(&Token::Comma) {
10037                Some(self.parse_literal_uint()?)
10038            } else {
10039                None
10040            };
10041            self.expect_token(&Token::RParen)?;
10042            Ok((Some(n), scale))
10043        } else {
10044            Ok((None, None))
10045        }
10046    }
10047
10048    pub fn parse_exact_number_optional_precision_scale(
10049        &mut self,
10050    ) -> Result<ExactNumberInfo, ParserError> {
10051        if self.consume_token(&Token::LParen) {
10052            let precision = self.parse_literal_uint()?;
10053            let scale = if self.consume_token(&Token::Comma) {
10054                Some(self.parse_literal_uint()?)
10055            } else {
10056                None
10057            };
10058
10059            self.expect_token(&Token::RParen)?;
10060
10061            match scale {
10062                None => Ok(ExactNumberInfo::Precision(precision)),
10063                Some(scale) => Ok(ExactNumberInfo::PrecisionAndScale(precision, scale)),
10064            }
10065        } else {
10066            Ok(ExactNumberInfo::None)
10067        }
10068    }
10069
10070    pub fn parse_optional_type_modifiers(&mut self) -> Result<Option<Vec<String>>, ParserError> {
10071        if self.consume_token(&Token::LParen) {
10072            let mut modifiers = Vec::new();
10073            loop {
10074                let next_token = self.next_token();
10075                match next_token.token {
10076                    Token::Word(w) => modifiers.push(w.to_string()),
10077                    Token::Number(n, _) => modifiers.push(n),
10078                    Token::SingleQuotedString(s) => modifiers.push(s),
10079
10080                    Token::Comma => {
10081                        continue;
10082                    }
10083                    Token::RParen => {
10084                        break;
10085                    }
10086                    _ => self.expected("type modifiers", next_token)?,
10087                }
10088            }
10089
10090            Ok(Some(modifiers))
10091        } else {
10092            Ok(None)
10093        }
10094    }
10095
10096    /// Parse a parenthesized sub data type
10097    fn parse_sub_type<F>(&mut self, parent_type: F) -> Result<DataType, ParserError>
10098    where
10099        F: FnOnce(Box<DataType>) -> DataType,
10100    {
10101        self.expect_token(&Token::LParen)?;
10102        let inside_type = self.parse_data_type()?;
10103        self.expect_token(&Token::RParen)?;
10104        Ok(parent_type(inside_type.into()))
10105    }
10106
10107    pub fn parse_delete(&mut self) -> Result<Statement, ParserError> {
10108        let (tables, with_from_keyword) = if !self.parse_keyword(Keyword::FROM) {
10109            // `FROM` keyword is optional in BigQuery SQL.
10110            // https://cloud.google.com/bigquery/docs/reference/standard-sql/dml-syntax#delete_statement
10111            if dialect_of!(self is BigQueryDialect | GenericDialect) {
10112                (vec![], false)
10113            } else {
10114                let tables = self.parse_comma_separated(|p| p.parse_object_name(false))?;
10115                self.expect_keyword_is(Keyword::FROM)?;
10116                (tables, true)
10117            }
10118        } else {
10119            (vec![], true)
10120        };
10121
10122        let from = self.parse_comma_separated(Parser::parse_table_and_joins)?;
10123        let using = if self.parse_keyword(Keyword::USING) {
10124            Some(self.parse_comma_separated(Parser::parse_table_and_joins)?)
10125        } else {
10126            None
10127        };
10128        let selection = if self.parse_keyword(Keyword::WHERE) {
10129            Some(self.parse_expr()?)
10130        } else {
10131            None
10132        };
10133        let returning = if self.parse_keyword(Keyword::RETURNING) {
10134            Some(self.parse_comma_separated(Parser::parse_select_item)?)
10135        } else {
10136            None
10137        };
10138        let order_by = if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
10139            self.parse_comma_separated(Parser::parse_order_by_expr)?
10140        } else {
10141            vec![]
10142        };
10143        let limit = if self.parse_keyword(Keyword::LIMIT) {
10144            self.parse_limit()?
10145        } else {
10146            None
10147        };
10148
10149        Ok(Statement::Delete(Delete {
10150            tables,
10151            from: if with_from_keyword {
10152                FromTable::WithFromKeyword(from)
10153            } else {
10154                FromTable::WithoutKeyword(from)
10155            },
10156            using,
10157            selection,
10158            returning,
10159            order_by,
10160            limit,
10161        }))
10162    }
10163
10164    // KILL [CONNECTION | QUERY | MUTATION] processlist_id
10165    pub fn parse_kill(&mut self) -> Result<Statement, ParserError> {
10166        let modifier_keyword =
10167            self.parse_one_of_keywords(&[Keyword::CONNECTION, Keyword::QUERY, Keyword::MUTATION]);
10168
10169        let id = self.parse_literal_uint()?;
10170
10171        let modifier = match modifier_keyword {
10172            Some(Keyword::CONNECTION) => Some(KillType::Connection),
10173            Some(Keyword::QUERY) => Some(KillType::Query),
10174            Some(Keyword::MUTATION) => {
10175                if dialect_of!(self is ClickHouseDialect | GenericDialect) {
10176                    Some(KillType::Mutation)
10177                } else {
10178                    self.expected(
10179                        "Unsupported type for KILL, allowed: CONNECTION | QUERY",
10180                        self.peek_token(),
10181                    )?
10182                }
10183            }
10184            _ => None,
10185        };
10186
10187        Ok(Statement::Kill { modifier, id })
10188    }
10189
10190    pub fn parse_explain(
10191        &mut self,
10192        describe_alias: DescribeAlias,
10193    ) -> Result<Statement, ParserError> {
10194        let mut analyze = false;
10195        let mut verbose = false;
10196        let mut query_plan = false;
10197        let mut estimate = false;
10198        let mut format = None;
10199        let mut options = None;
10200
10201        // Note: DuckDB is compatible with PostgreSQL syntax for this statement,
10202        // although not all features may be implemented.
10203        if describe_alias == DescribeAlias::Explain
10204            && self.dialect.supports_explain_with_utility_options()
10205            && self.peek_token().token == Token::LParen
10206        {
10207            options = Some(self.parse_utility_options()?)
10208        } else if self.parse_keywords(&[Keyword::QUERY, Keyword::PLAN]) {
10209            query_plan = true;
10210        } else if self.parse_keyword(Keyword::ESTIMATE) {
10211            estimate = true;
10212        } else {
10213            analyze = self.parse_keyword(Keyword::ANALYZE);
10214            verbose = self.parse_keyword(Keyword::VERBOSE);
10215            if self.parse_keyword(Keyword::FORMAT) {
10216                format = Some(self.parse_analyze_format()?);
10217            }
10218        }
10219
10220        match self.maybe_parse(|parser| parser.parse_statement())? {
10221            Some(Statement::Explain { .. }) | Some(Statement::ExplainTable { .. }) => Err(
10222                ParserError::ParserError("Explain must be root of the plan".to_string()),
10223            ),
10224            Some(statement) => Ok(Statement::Explain {
10225                describe_alias,
10226                analyze,
10227                verbose,
10228                query_plan,
10229                estimate,
10230                statement: Box::new(statement),
10231                format,
10232                options,
10233            }),
10234            _ => {
10235                let hive_format =
10236                    match self.parse_one_of_keywords(&[Keyword::EXTENDED, Keyword::FORMATTED]) {
10237                        Some(Keyword::EXTENDED) => Some(HiveDescribeFormat::Extended),
10238                        Some(Keyword::FORMATTED) => Some(HiveDescribeFormat::Formatted),
10239                        _ => None,
10240                    };
10241
10242                let has_table_keyword = if self.dialect.describe_requires_table_keyword() {
10243                    // only allow to use TABLE keyword for DESC|DESCRIBE statement
10244                    self.parse_keyword(Keyword::TABLE)
10245                } else {
10246                    false
10247                };
10248
10249                let table_name = self.parse_object_name(false)?;
10250                Ok(Statement::ExplainTable {
10251                    describe_alias,
10252                    hive_format,
10253                    has_table_keyword,
10254                    table_name,
10255                })
10256            }
10257        }
10258    }
10259
10260    /// Parse a query expression, i.e. a `SELECT` statement optionally
10261    /// preceded with some `WITH` CTE declarations and optionally followed
10262    /// by `ORDER BY`. Unlike some other parse_... methods, this one doesn't
10263    /// expect the initial keyword to be already consumed
10264    pub fn parse_query(&mut self) -> Result<Box<Query>, ParserError> {
10265        let _guard = self.recursion_counter.try_decrease()?;
10266        let with = if self.parse_keyword(Keyword::WITH) {
10267            let with_token = self.get_current_token();
10268            Some(With {
10269                with_token: with_token.clone().into(),
10270                recursive: self.parse_keyword(Keyword::RECURSIVE),
10271                cte_tables: self.parse_comma_separated(Parser::parse_cte)?,
10272            })
10273        } else {
10274            None
10275        };
10276        if self.parse_keyword(Keyword::INSERT) {
10277            Ok(Query {
10278                with,
10279                body: self.parse_insert_setexpr_boxed()?,
10280                limit: None,
10281                limit_by: vec![],
10282                order_by: None,
10283                offset: None,
10284                fetch: None,
10285                locks: vec![],
10286                for_clause: None,
10287                settings: None,
10288                format_clause: None,
10289            }
10290            .into())
10291        } else if self.parse_keyword(Keyword::UPDATE) {
10292            Ok(Query {
10293                with,
10294                body: self.parse_update_setexpr_boxed()?,
10295                limit: None,
10296                limit_by: vec![],
10297                order_by: None,
10298                offset: None,
10299                fetch: None,
10300                locks: vec![],
10301                for_clause: None,
10302                settings: None,
10303                format_clause: None,
10304            }
10305            .into())
10306        } else {
10307            let body = self.parse_query_body(self.dialect.prec_unknown())?;
10308
10309            let order_by = self.parse_optional_order_by()?;
10310
10311            let mut limit = None;
10312            let mut offset = None;
10313
10314            for _x in 0..2 {
10315                if limit.is_none() && self.parse_keyword(Keyword::LIMIT) {
10316                    limit = self.parse_limit()?
10317                }
10318
10319                if offset.is_none() && self.parse_keyword(Keyword::OFFSET) {
10320                    offset = Some(self.parse_offset()?)
10321                }
10322
10323                if self.dialect.supports_limit_comma()
10324                    && limit.is_some()
10325                    && offset.is_none()
10326                    && self.consume_token(&Token::Comma)
10327                {
10328                    // MySQL style LIMIT x,y => LIMIT y OFFSET x.
10329                    // Check <https://dev.mysql.com/doc/refman/8.0/en/select.html> for more details.
10330                    offset = Some(Offset {
10331                        value: limit.unwrap(),
10332                        rows: OffsetRows::None,
10333                    });
10334                    limit = Some(self.parse_expr()?);
10335                }
10336            }
10337
10338            let limit_by = if dialect_of!(self is ClickHouseDialect | GenericDialect)
10339                && self.parse_keyword(Keyword::BY)
10340            {
10341                self.parse_comma_separated(Parser::parse_expr)?
10342            } else {
10343                vec![]
10344            };
10345
10346            let settings = self.parse_settings()?;
10347
10348            let fetch = if self.parse_keyword(Keyword::FETCH) {
10349                Some(self.parse_fetch()?)
10350            } else {
10351                None
10352            };
10353
10354            let mut for_clause = None;
10355            let mut locks = Vec::new();
10356            while self.parse_keyword(Keyword::FOR) {
10357                if let Some(parsed_for_clause) = self.parse_for_clause()? {
10358                    for_clause = Some(parsed_for_clause);
10359                    break;
10360                } else {
10361                    locks.push(self.parse_lock()?);
10362                }
10363            }
10364            let format_clause = if dialect_of!(self is ClickHouseDialect | GenericDialect)
10365                && self.parse_keyword(Keyword::FORMAT)
10366            {
10367                if self.parse_keyword(Keyword::NULL) {
10368                    Some(FormatClause::Null)
10369                } else {
10370                    let ident = self.parse_identifier()?;
10371                    Some(FormatClause::Identifier(ident))
10372                }
10373            } else {
10374                None
10375            };
10376
10377            Ok(Query {
10378                with,
10379                body,
10380                order_by,
10381                limit,
10382                limit_by,
10383                offset,
10384                fetch,
10385                locks,
10386                for_clause,
10387                settings,
10388                format_clause,
10389            }
10390            .into())
10391        }
10392    }
10393
10394    fn parse_settings(&mut self) -> Result<Option<Vec<Setting>>, ParserError> {
10395        let settings = if dialect_of!(self is ClickHouseDialect|GenericDialect)
10396            && self.parse_keyword(Keyword::SETTINGS)
10397        {
10398            let key_values = self.parse_comma_separated(|p| {
10399                let key = p.parse_identifier()?;
10400                p.expect_token(&Token::Eq)?;
10401                let value = p.parse_value()?.value;
10402                Ok(Setting { key, value })
10403            })?;
10404            Some(key_values)
10405        } else {
10406            None
10407        };
10408        Ok(settings)
10409    }
10410
10411    /// Parse a mssql `FOR [XML | JSON | BROWSE]` clause
10412    pub fn parse_for_clause(&mut self) -> Result<Option<ForClause>, ParserError> {
10413        if self.parse_keyword(Keyword::XML) {
10414            Ok(Some(self.parse_for_xml()?))
10415        } else if self.parse_keyword(Keyword::JSON) {
10416            Ok(Some(self.parse_for_json()?))
10417        } else if self.parse_keyword(Keyword::BROWSE) {
10418            Ok(Some(ForClause::Browse))
10419        } else {
10420            Ok(None)
10421        }
10422    }
10423
10424    /// Parse a mssql `FOR XML` clause
10425    pub fn parse_for_xml(&mut self) -> Result<ForClause, ParserError> {
10426        let for_xml = if self.parse_keyword(Keyword::RAW) {
10427            let mut element_name = None;
10428            if self.peek_token().token == Token::LParen {
10429                self.expect_token(&Token::LParen)?;
10430                element_name = Some(self.parse_literal_string()?);
10431                self.expect_token(&Token::RParen)?;
10432            }
10433            ForXml::Raw(element_name)
10434        } else if self.parse_keyword(Keyword::AUTO) {
10435            ForXml::Auto
10436        } else if self.parse_keyword(Keyword::EXPLICIT) {
10437            ForXml::Explicit
10438        } else if self.parse_keyword(Keyword::PATH) {
10439            let mut element_name = None;
10440            if self.peek_token().token == Token::LParen {
10441                self.expect_token(&Token::LParen)?;
10442                element_name = Some(self.parse_literal_string()?);
10443                self.expect_token(&Token::RParen)?;
10444            }
10445            ForXml::Path(element_name)
10446        } else {
10447            return Err(ParserError::ParserError(
10448                "Expected FOR XML [RAW | AUTO | EXPLICIT | PATH ]".to_string(),
10449            ));
10450        };
10451        let mut elements = false;
10452        let mut binary_base64 = false;
10453        let mut root = None;
10454        let mut r#type = false;
10455        while self.peek_token().token == Token::Comma {
10456            self.next_token();
10457            if self.parse_keyword(Keyword::ELEMENTS) {
10458                elements = true;
10459            } else if self.parse_keyword(Keyword::BINARY) {
10460                self.expect_keyword_is(Keyword::BASE64)?;
10461                binary_base64 = true;
10462            } else if self.parse_keyword(Keyword::ROOT) {
10463                self.expect_token(&Token::LParen)?;
10464                root = Some(self.parse_literal_string()?);
10465                self.expect_token(&Token::RParen)?;
10466            } else if self.parse_keyword(Keyword::TYPE) {
10467                r#type = true;
10468            }
10469        }
10470        Ok(ForClause::Xml {
10471            for_xml,
10472            elements,
10473            binary_base64,
10474            root,
10475            r#type,
10476        })
10477    }
10478
10479    /// Parse a mssql `FOR JSON` clause
10480    pub fn parse_for_json(&mut self) -> Result<ForClause, ParserError> {
10481        let for_json = if self.parse_keyword(Keyword::AUTO) {
10482            ForJson::Auto
10483        } else if self.parse_keyword(Keyword::PATH) {
10484            ForJson::Path
10485        } else {
10486            return Err(ParserError::ParserError(
10487                "Expected FOR JSON [AUTO | PATH ]".to_string(),
10488            ));
10489        };
10490        let mut root = None;
10491        let mut include_null_values = false;
10492        let mut without_array_wrapper = false;
10493        while self.peek_token().token == Token::Comma {
10494            self.next_token();
10495            if self.parse_keyword(Keyword::ROOT) {
10496                self.expect_token(&Token::LParen)?;
10497                root = Some(self.parse_literal_string()?);
10498                self.expect_token(&Token::RParen)?;
10499            } else if self.parse_keyword(Keyword::INCLUDE_NULL_VALUES) {
10500                include_null_values = true;
10501            } else if self.parse_keyword(Keyword::WITHOUT_ARRAY_WRAPPER) {
10502                without_array_wrapper = true;
10503            }
10504        }
10505        Ok(ForClause::Json {
10506            for_json,
10507            root,
10508            include_null_values,
10509            without_array_wrapper,
10510        })
10511    }
10512
10513    /// Parse a CTE (`alias [( col1, col2, ... )] AS (subquery)`)
10514    pub fn parse_cte(&mut self) -> Result<Cte, ParserError> {
10515        let name = self.parse_identifier()?;
10516
10517        let mut cte = if self.parse_keyword(Keyword::AS) {
10518            let mut is_materialized = None;
10519            if dialect_of!(self is PostgreSqlDialect) {
10520                if self.parse_keyword(Keyword::MATERIALIZED) {
10521                    is_materialized = Some(CteAsMaterialized::Materialized);
10522                } else if self.parse_keywords(&[Keyword::NOT, Keyword::MATERIALIZED]) {
10523                    is_materialized = Some(CteAsMaterialized::NotMaterialized);
10524                }
10525            }
10526            self.expect_token(&Token::LParen)?;
10527
10528            let query = self.parse_query()?;
10529            let closing_paren_token = self.expect_token(&Token::RParen)?;
10530
10531            let alias = TableAlias {
10532                name,
10533                columns: vec![],
10534            };
10535            Cte {
10536                alias,
10537                query,
10538                from: None,
10539                materialized: is_materialized,
10540                closing_paren_token: closing_paren_token.into(),
10541            }
10542        } else {
10543            let columns = self.parse_table_alias_column_defs()?;
10544            self.expect_keyword_is(Keyword::AS)?;
10545            let mut is_materialized = None;
10546            if dialect_of!(self is PostgreSqlDialect) {
10547                if self.parse_keyword(Keyword::MATERIALIZED) {
10548                    is_materialized = Some(CteAsMaterialized::Materialized);
10549                } else if self.parse_keywords(&[Keyword::NOT, Keyword::MATERIALIZED]) {
10550                    is_materialized = Some(CteAsMaterialized::NotMaterialized);
10551                }
10552            }
10553            self.expect_token(&Token::LParen)?;
10554
10555            let query = self.parse_query()?;
10556            let closing_paren_token = self.expect_token(&Token::RParen)?;
10557
10558            let alias = TableAlias { name, columns };
10559            Cte {
10560                alias,
10561                query,
10562                from: None,
10563                materialized: is_materialized,
10564                closing_paren_token: closing_paren_token.into(),
10565            }
10566        };
10567        if self.parse_keyword(Keyword::FROM) {
10568            cte.from = Some(self.parse_identifier()?);
10569        }
10570        Ok(cte)
10571    }
10572
10573    /// Parse a "query body", which is an expression with roughly the
10574    /// following grammar:
10575    /// ```sql
10576    ///   query_body ::= restricted_select | '(' subquery ')' | set_operation
10577    ///   restricted_select ::= 'SELECT' [expr_list] [ from ] [ where ] [ groupby_having ]
10578    ///   subquery ::= query_body [ order_by_limit ]
10579    ///   set_operation ::= query_body { 'UNION' | 'EXCEPT' | 'INTERSECT' } [ 'ALL' ] query_body
10580    /// ```
10581    pub fn parse_query_body(&mut self, precedence: u8) -> Result<Box<SetExpr>, ParserError> {
10582        // We parse the expression using a Pratt parser, as in `parse_expr()`.
10583        // Start by parsing a restricted SELECT or a `(subquery)`:
10584        let expr = if self.peek_keyword(Keyword::SELECT)
10585            || (self.peek_keyword(Keyword::FROM) && self.dialect.supports_from_first_select())
10586        {
10587            SetExpr::Select(self.parse_select().map(Box::new)?)
10588        } else if self.consume_token(&Token::LParen) {
10589            // CTEs are not allowed here, but the parser currently accepts them
10590            let subquery = self.parse_query()?;
10591            self.expect_token(&Token::RParen)?;
10592            SetExpr::Query(subquery)
10593        } else if self.parse_keyword(Keyword::VALUES) {
10594            let is_mysql = dialect_of!(self is MySqlDialect);
10595            SetExpr::Values(self.parse_values(is_mysql)?)
10596        } else if self.parse_keyword(Keyword::TABLE) {
10597            SetExpr::Table(Box::new(self.parse_as_table()?))
10598        } else if self.peek_keyword(Keyword::PIVOT)
10599            && dialect_of!(self is DuckDbDialect | GenericDialect)
10600        {
10601            // DuckDB statement-form PIVOT as a query body (top-level, CTAS body,
10602            // or subquery). See `parse_pivot_statement`.
10603            SetExpr::Pivot(Box::new(self.parse_pivot_statement()?))
10604        } else if self.peek_keyword(Keyword::UNPIVOT)
10605            && dialect_of!(self is DuckDbDialect | GenericDialect)
10606        {
10607            SetExpr::Unpivot(Box::new(self.parse_unpivot_statement()?))
10608        } else {
10609            return self.expected(
10610                "SELECT, VALUES, or a subquery in the query body",
10611                self.peek_token(),
10612            );
10613        };
10614
10615        self.parse_remaining_set_exprs(expr, precedence)
10616    }
10617
10618    /// Parse any extra set expressions that may be present in a query body
10619    ///
10620    /// (this is its own function to reduce required stack size in debug builds)
10621    fn parse_remaining_set_exprs(
10622        &mut self,
10623        mut expr: SetExpr,
10624        precedence: u8,
10625    ) -> Result<Box<SetExpr>, ParserError> {
10626        loop {
10627            // The query can be optionally followed by a set operator:
10628            let op = self.parse_set_operator(&self.peek_token().token);
10629            let next_precedence = match op {
10630                // UNION and EXCEPT have the same binding power and evaluate left-to-right
10631                Some(SetOperator::Union) | Some(SetOperator::Except) | Some(SetOperator::Minus) => {
10632                    10
10633                }
10634                // INTERSECT has higher precedence than UNION/EXCEPT
10635                Some(SetOperator::Intersect) => 20,
10636                // Unexpected token or EOF => stop parsing the query body
10637                None => break,
10638            };
10639            if precedence >= next_precedence {
10640                break;
10641            }
10642            self.next_token(); // skip past the set operator
10643            let set_quantifier = self.parse_set_quantifier(&op);
10644            expr = SetExpr::SetOperation {
10645                left: Box::new(expr),
10646                op: op.unwrap(),
10647                set_quantifier,
10648                right: self.parse_query_body(next_precedence)?,
10649            };
10650        }
10651
10652        Ok(expr.into())
10653    }
10654
10655    pub fn parse_set_operator(&mut self, token: &Token) -> Option<SetOperator> {
10656        match token {
10657            Token::Word(w) if w.keyword == Keyword::UNION => Some(SetOperator::Union),
10658            Token::Word(w) if w.keyword == Keyword::EXCEPT => Some(SetOperator::Except),
10659            Token::Word(w) if w.keyword == Keyword::INTERSECT => Some(SetOperator::Intersect),
10660            Token::Word(w) if w.keyword == Keyword::MINUS => Some(SetOperator::Minus),
10661            _ => None,
10662        }
10663    }
10664
10665    pub fn parse_set_quantifier(&mut self, op: &Option<SetOperator>) -> SetQuantifier {
10666        match op {
10667            Some(
10668                SetOperator::Except
10669                | SetOperator::Intersect
10670                | SetOperator::Union
10671                | SetOperator::Minus,
10672            ) => {
10673                if self.parse_keywords(&[Keyword::DISTINCT, Keyword::BY, Keyword::NAME]) {
10674                    SetQuantifier::DistinctByName
10675                } else if self.parse_keywords(&[Keyword::BY, Keyword::NAME]) {
10676                    SetQuantifier::ByName
10677                } else if self.parse_keyword(Keyword::ALL) {
10678                    if self.parse_keywords(&[Keyword::BY, Keyword::NAME]) {
10679                        SetQuantifier::AllByName
10680                    } else {
10681                        SetQuantifier::All
10682                    }
10683                } else if self.parse_keyword(Keyword::DISTINCT) {
10684                    SetQuantifier::Distinct
10685                } else {
10686                    SetQuantifier::None
10687                }
10688            }
10689            _ => SetQuantifier::None,
10690        }
10691    }
10692
10693    /// Parse a restricted `SELECT` statement (no CTEs / `UNION` / `ORDER BY`)
10694    pub fn parse_select(&mut self) -> Result<Select, ParserError> {
10695        let mut from_first = None;
10696
10697        if self.dialect.supports_from_first_select() && self.peek_keyword(Keyword::FROM) {
10698            let from_token = self.expect_keyword(Keyword::FROM)?;
10699            let from = self.parse_table_with_joins()?;
10700            if !self.peek_keyword(Keyword::SELECT) {
10701                return Ok(Select {
10702                    select_token: AttachedToken(from_token),
10703                    distinct: None,
10704                    top: None,
10705                    top_before_distinct: false,
10706                    projection: vec![],
10707                    into: None,
10708                    from,
10709                    lateral_views: vec![],
10710                    prewhere: None,
10711                    selection: None,
10712                    group_by: GroupByExpr::Expressions(vec![], vec![]),
10713                    cluster_by: vec![],
10714                    distribute_by: vec![],
10715                    sort_by: vec![],
10716                    having: None,
10717                    named_window: vec![],
10718                    window_before_qualify: false,
10719                    qualify: None,
10720                    value_table_mode: None,
10721                    connect_by: None,
10722                    flavor: SelectFlavor::FromFirstNoSelect,
10723                });
10724            }
10725            from_first = Some(from);
10726        }
10727
10728        let select_token = self.expect_keyword(Keyword::SELECT)?;
10729        let value_table_mode =
10730            if dialect_of!(self is BigQueryDialect) && self.parse_keyword(Keyword::AS) {
10731                if self.parse_keyword(Keyword::VALUE) {
10732                    Some(ValueTableMode::AsValue)
10733                } else if self.parse_keyword(Keyword::STRUCT) {
10734                    Some(ValueTableMode::AsStruct)
10735                } else {
10736                    self.expected("VALUE or STRUCT", self.peek_token())?
10737                }
10738            } else {
10739                None
10740            };
10741
10742        let mut top_before_distinct = false;
10743        let mut top = None;
10744        if self.dialect.supports_top_before_distinct() && self.parse_keyword(Keyword::TOP) {
10745            top = Some(self.parse_top()?);
10746            top_before_distinct = true;
10747        }
10748        let distinct = self.parse_all_or_distinct()?;
10749        if !self.dialect.supports_top_before_distinct() && self.parse_keyword(Keyword::TOP) {
10750            top = Some(self.parse_top()?);
10751        }
10752
10753        let projection =
10754            if self.dialect.supports_empty_projections() && self.peek_keyword(Keyword::FROM) {
10755                vec![]
10756            } else {
10757                self.parse_projection()?
10758            };
10759
10760        let into = if self.parse_keyword(Keyword::INTO) {
10761            let temporary = self
10762                .parse_one_of_keywords(&[Keyword::TEMP, Keyword::TEMPORARY])
10763                .is_some();
10764            let unlogged = self.parse_keyword(Keyword::UNLOGGED);
10765            let table = self.parse_keyword(Keyword::TABLE);
10766            let name = self.parse_object_name(false)?;
10767            Some(SelectInto {
10768                temporary,
10769                unlogged,
10770                table,
10771                name,
10772            })
10773        } else {
10774            None
10775        };
10776
10777        // Note that for keywords to be properly handled here, they need to be
10778        // added to `RESERVED_FOR_COLUMN_ALIAS` / `RESERVED_FOR_TABLE_ALIAS`,
10779        // otherwise they may be parsed as an alias as part of the `projection`
10780        // or `from`.
10781
10782        let (from, from_first) = if let Some(from) = from_first.take() {
10783            (from, true)
10784        } else if self.parse_keyword(Keyword::FROM) {
10785            (self.parse_table_with_joins()?, false)
10786        } else {
10787            (vec![], false)
10788        };
10789
10790        let mut lateral_views = vec![];
10791        loop {
10792            if self.parse_keywords(&[Keyword::LATERAL, Keyword::VIEW]) {
10793                let outer = self.parse_keyword(Keyword::OUTER);
10794                let lateral_view = self.parse_expr()?;
10795                let lateral_view_name = self.parse_object_name(false)?;
10796                let lateral_col_alias = self
10797                    .parse_comma_separated(|parser| {
10798                        parser.parse_optional_alias(&[
10799                            Keyword::WHERE,
10800                            Keyword::GROUP,
10801                            Keyword::CLUSTER,
10802                            Keyword::HAVING,
10803                            Keyword::LATERAL,
10804                        ]) // This couldn't possibly be a bad idea
10805                    })?
10806                    .into_iter()
10807                    .flatten()
10808                    .collect();
10809
10810                lateral_views.push(LateralView {
10811                    lateral_view,
10812                    lateral_view_name,
10813                    lateral_col_alias,
10814                    outer,
10815                });
10816            } else {
10817                break;
10818            }
10819        }
10820
10821        let prewhere = if dialect_of!(self is ClickHouseDialect|GenericDialect)
10822            && self.parse_keyword(Keyword::PREWHERE)
10823        {
10824            Some(self.parse_expr()?)
10825        } else {
10826            None
10827        };
10828
10829        let selection = if self.parse_keyword(Keyword::WHERE) {
10830            Some(self.parse_expr()?)
10831        } else {
10832            None
10833        };
10834
10835        let group_by = self
10836            .parse_optional_group_by()?
10837            .unwrap_or_else(|| GroupByExpr::Expressions(vec![], vec![]));
10838
10839        let cluster_by = if self.parse_keywords(&[Keyword::CLUSTER, Keyword::BY]) {
10840            self.parse_comma_separated(Parser::parse_expr)?
10841        } else {
10842            vec![]
10843        };
10844
10845        let distribute_by = if self.parse_keywords(&[Keyword::DISTRIBUTE, Keyword::BY]) {
10846            self.parse_comma_separated(Parser::parse_expr)?
10847        } else {
10848            vec![]
10849        };
10850
10851        let sort_by = if self.parse_keywords(&[Keyword::SORT, Keyword::BY]) {
10852            self.parse_comma_separated(Parser::parse_expr)?
10853        } else {
10854            vec![]
10855        };
10856
10857        let having = if self.parse_keyword(Keyword::HAVING) {
10858            Some(self.parse_expr()?)
10859        } else {
10860            None
10861        };
10862
10863        // Accept QUALIFY and WINDOW in any order and flag accordingly.
10864        let (named_windows, qualify, window_before_qualify) = if self.parse_keyword(Keyword::WINDOW)
10865        {
10866            let named_windows = self.parse_comma_separated(Parser::parse_named_window)?;
10867            if self.parse_keyword(Keyword::QUALIFY) {
10868                (named_windows, Some(self.parse_expr()?), true)
10869            } else {
10870                (named_windows, None, true)
10871            }
10872        } else if self.parse_keyword(Keyword::QUALIFY) {
10873            let qualify = Some(self.parse_expr()?);
10874            if self.parse_keyword(Keyword::WINDOW) {
10875                (
10876                    self.parse_comma_separated(Parser::parse_named_window)?,
10877                    qualify,
10878                    false,
10879                )
10880            } else {
10881                (Default::default(), qualify, false)
10882            }
10883        } else {
10884            Default::default()
10885        };
10886
10887        let connect_by = if self.dialect.supports_connect_by()
10888            && self
10889                .parse_one_of_keywords(&[Keyword::START, Keyword::CONNECT])
10890                .is_some()
10891        {
10892            self.prev_token();
10893            Some(self.parse_connect_by()?)
10894        } else {
10895            None
10896        };
10897
10898        Ok(Select {
10899            select_token: AttachedToken(select_token),
10900            distinct,
10901            top,
10902            top_before_distinct,
10903            projection,
10904            into,
10905            from,
10906            lateral_views,
10907            prewhere,
10908            selection,
10909            group_by,
10910            cluster_by,
10911            distribute_by,
10912            sort_by,
10913            having,
10914            named_window: named_windows,
10915            window_before_qualify,
10916            qualify,
10917            value_table_mode,
10918            connect_by,
10919            flavor: if from_first {
10920                SelectFlavor::FromFirst
10921            } else {
10922                SelectFlavor::Standard
10923            },
10924        })
10925    }
10926
10927    /// Invoke `f` after first setting the parser's `ParserState` to `state`.
10928    ///
10929    /// Upon return, restores the parser's state to what it started at.
10930    fn with_state<T, F>(&mut self, state: ParserState, mut f: F) -> Result<T, ParserError>
10931    where
10932        F: FnMut(&mut Parser) -> Result<T, ParserError>,
10933    {
10934        let current_state = self.state;
10935        self.state = state;
10936        let res = f(self);
10937        self.state = current_state;
10938        res
10939    }
10940
10941    pub fn parse_connect_by(&mut self) -> Result<ConnectBy, ParserError> {
10942        let (condition, relationships) = if self.parse_keywords(&[Keyword::CONNECT, Keyword::BY]) {
10943            let relationships = self.with_state(ParserState::ConnectBy, |parser| {
10944                parser.parse_comma_separated(Parser::parse_expr)
10945            })?;
10946            self.expect_keywords(&[Keyword::START, Keyword::WITH])?;
10947            let condition = self.parse_expr()?;
10948            (condition, relationships)
10949        } else {
10950            self.expect_keywords(&[Keyword::START, Keyword::WITH])?;
10951            let condition = self.parse_expr()?;
10952            self.expect_keywords(&[Keyword::CONNECT, Keyword::BY])?;
10953            let relationships = self.with_state(ParserState::ConnectBy, |parser| {
10954                parser.parse_comma_separated(Parser::parse_expr)
10955            })?;
10956            (condition, relationships)
10957        };
10958        Ok(ConnectBy {
10959            condition,
10960            relationships,
10961        })
10962    }
10963
10964    /// Parse `CREATE TABLE x AS TABLE y`
10965    pub fn parse_as_table(&mut self) -> Result<Table, ParserError> {
10966        let token1 = self.next_token();
10967        let token2 = self.next_token();
10968        let token3 = self.next_token();
10969
10970        let table_name;
10971        let schema_name;
10972        if token2 == Token::Period {
10973            match token1.token {
10974                Token::Word(w) => {
10975                    schema_name = w.value;
10976                }
10977                _ => {
10978                    return self.expected("Schema name", token1);
10979                }
10980            }
10981            match token3.token {
10982                Token::Word(w) => {
10983                    table_name = w.value;
10984                }
10985                _ => {
10986                    return self.expected("Table name", token3);
10987                }
10988            }
10989            Ok(Table {
10990                table_name: Some(table_name),
10991                schema_name: Some(schema_name),
10992            })
10993        } else {
10994            match token1.token {
10995                Token::Word(w) => {
10996                    table_name = w.value;
10997                }
10998                _ => {
10999                    return self.expected("Table name", token1);
11000                }
11001            }
11002            Ok(Table {
11003                table_name: Some(table_name),
11004                schema_name: None,
11005            })
11006        }
11007    }
11008
11009    /// Parse a `SET ROLE` statement. Expects SET to be consumed already.
11010    fn parse_set_role(&mut self, modifier: Option<Keyword>) -> Result<Statement, ParserError> {
11011        self.expect_keyword_is(Keyword::ROLE)?;
11012        let context_modifier = match modifier {
11013            Some(Keyword::LOCAL) => ContextModifier::Local,
11014            Some(Keyword::SESSION) => ContextModifier::Session,
11015            _ => ContextModifier::None,
11016        };
11017
11018        let role_name = if self.parse_keyword(Keyword::NONE) {
11019            None
11020        } else {
11021            Some(self.parse_identifier()?)
11022        };
11023        Ok(Statement::SetRole {
11024            context_modifier,
11025            role_name,
11026        })
11027    }
11028
11029    pub fn parse_set(&mut self) -> Result<Statement, ParserError> {
11030        let modifier =
11031            self.parse_one_of_keywords(&[Keyword::SESSION, Keyword::LOCAL, Keyword::HIVEVAR]);
11032        if let Some(Keyword::HIVEVAR) = modifier {
11033            self.expect_token(&Token::Colon)?;
11034        } else if let Some(set_role_stmt) =
11035            self.maybe_parse(|parser| parser.parse_set_role(modifier))?
11036        {
11037            return Ok(set_role_stmt);
11038        }
11039
11040        // DuckDB's `SET VARIABLE <name> = <expr>` sets a session-scoped named
11041        // value read back with `getvariable(name)`, distinct from the
11042        // `SET <config_option> = <value>` form below that configures the
11043        // engine itself. `VARIABLE` is a non-reserved keyword (see
11044        // `keywords.rs`), so on every other dialect this is unclaimed and
11045        // `variables` below parses it as the plain object name it always
11046        // has — `SET VARIABLE cutoff = 1` on PostgreSQL still fails the same
11047        // way it did before this addition, at the next token instead of `=`
11048        // or `TO` ("Expected: equals sign or TO").
11049        if dialect_of!(self is DuckDbDialect | GenericDialect)
11050            && self.parse_keyword(Keyword::VARIABLE)
11051        {
11052            let name = self.parse_identifier()?;
11053            self.expect_token(&Token::Eq)?;
11054            let value = self.parse_expr()?;
11055            return Ok(Statement::SetVariable {
11056                local: modifier == Some(Keyword::LOCAL),
11057                hivevar: Some(Keyword::HIVEVAR) == modifier,
11058                variables: OneOrManyWithParens::One(ObjectName::from(vec![name])),
11059                value: vec![value],
11060                is_variable: true,
11061            });
11062        }
11063
11064        let variables = if self.parse_keywords(&[Keyword::TIME, Keyword::ZONE]) {
11065            OneOrManyWithParens::One(ObjectName::from(vec!["TIMEZONE".into()]))
11066        } else if self.dialect.supports_parenthesized_set_variables()
11067            && self.consume_token(&Token::LParen)
11068        {
11069            let variables = OneOrManyWithParens::Many(
11070                self.parse_comma_separated(|parser: &mut Parser<'a>| parser.parse_identifier())?
11071                    .into_iter()
11072                    .map(|ident| ObjectName::from(vec![ident]))
11073                    .collect(),
11074            );
11075            self.expect_token(&Token::RParen)?;
11076            variables
11077        } else {
11078            OneOrManyWithParens::One(self.parse_object_name(false)?)
11079        };
11080
11081        if matches!(&variables, OneOrManyWithParens::One(variable) if variable.to_string().eq_ignore_ascii_case("NAMES")
11082            && dialect_of!(self is MySqlDialect | GenericDialect))
11083        {
11084            if self.parse_keyword(Keyword::DEFAULT) {
11085                return Ok(Statement::SetNamesDefault {});
11086            }
11087
11088            let charset_name = self.parse_literal_string()?;
11089            let collation_name = if self.parse_one_of_keywords(&[Keyword::COLLATE]).is_some() {
11090                Some(self.parse_literal_string()?)
11091            } else {
11092                None
11093            };
11094
11095            return Ok(Statement::SetNames {
11096                charset_name,
11097                collation_name,
11098            });
11099        }
11100
11101        let parenthesized_assignment = matches!(&variables, OneOrManyWithParens::Many(_));
11102
11103        if self.consume_token(&Token::Eq) || self.parse_keyword(Keyword::TO) {
11104            if parenthesized_assignment {
11105                self.expect_token(&Token::LParen)?;
11106            }
11107
11108            let mut values = vec![];
11109            loop {
11110                let value = if let Some(expr) = self.try_parse_expr_sub_query()? {
11111                    expr
11112                } else if let Ok(expr) = self.parse_expr() {
11113                    expr
11114                } else {
11115                    self.expected("variable value", self.peek_token())?
11116                };
11117
11118                values.push(value);
11119                if self.consume_token(&Token::Comma) {
11120                    continue;
11121                }
11122
11123                if parenthesized_assignment {
11124                    self.expect_token(&Token::RParen)?;
11125                }
11126                return Ok(Statement::SetVariable {
11127                    local: modifier == Some(Keyword::LOCAL),
11128                    hivevar: Some(Keyword::HIVEVAR) == modifier,
11129                    variables,
11130                    value: values,
11131                    is_variable: false,
11132                });
11133            }
11134        }
11135
11136        let OneOrManyWithParens::One(variable) = variables else {
11137            return self.expected("set variable", self.peek_token());
11138        };
11139
11140        if variable.to_string().eq_ignore_ascii_case("TIMEZONE") {
11141            // for some db (e.g. postgresql), SET TIME ZONE <value> is an alias for SET TIMEZONE [TO|=] <value>
11142            match self.parse_expr() {
11143                Ok(expr) => Ok(Statement::SetTimeZone {
11144                    local: modifier == Some(Keyword::LOCAL),
11145                    value: expr,
11146                }),
11147                _ => self.expected("timezone value", self.peek_token())?,
11148            }
11149        } else if variable.to_string() == "CHARACTERISTICS" {
11150            self.expect_keywords(&[Keyword::AS, Keyword::TRANSACTION])?;
11151            Ok(Statement::SetTransaction {
11152                modes: self.parse_transaction_modes()?,
11153                snapshot: None,
11154                session: true,
11155            })
11156        } else if variable.to_string() == "TRANSACTION" && modifier.is_none() {
11157            if self.parse_keyword(Keyword::SNAPSHOT) {
11158                let snapshot_id = self.parse_value()?.value;
11159                return Ok(Statement::SetTransaction {
11160                    modes: vec![],
11161                    snapshot: Some(snapshot_id),
11162                    session: false,
11163                });
11164            }
11165            Ok(Statement::SetTransaction {
11166                modes: self.parse_transaction_modes()?,
11167                snapshot: None,
11168                session: false,
11169            })
11170        } else if self.dialect.supports_set_stmt_without_operator() {
11171            self.prev_token();
11172            self.parse_set_session_params()
11173        } else {
11174            self.expected("equals sign or TO", self.peek_token())
11175        }
11176    }
11177
11178    pub fn parse_set_session_params(&mut self) -> Result<Statement, ParserError> {
11179        if self.parse_keyword(Keyword::STATISTICS) {
11180            let topic = match self.parse_one_of_keywords(&[
11181                Keyword::IO,
11182                Keyword::PROFILE,
11183                Keyword::TIME,
11184                Keyword::XML,
11185            ]) {
11186                Some(Keyword::IO) => SessionParamStatsTopic::IO,
11187                Some(Keyword::PROFILE) => SessionParamStatsTopic::Profile,
11188                Some(Keyword::TIME) => SessionParamStatsTopic::Time,
11189                Some(Keyword::XML) => SessionParamStatsTopic::Xml,
11190                _ => return self.expected("IO, PROFILE, TIME or XML", self.peek_token()),
11191            };
11192            let value = self.parse_session_param_value()?;
11193            Ok(Statement::SetSessionParam(SetSessionParamKind::Statistics(
11194                SetSessionParamStatistics { topic, value },
11195            )))
11196        } else if self.parse_keyword(Keyword::IDENTITY_INSERT) {
11197            let obj = self.parse_object_name(false)?;
11198            let value = self.parse_session_param_value()?;
11199            Ok(Statement::SetSessionParam(
11200                SetSessionParamKind::IdentityInsert(SetSessionParamIdentityInsert { obj, value }),
11201            ))
11202        } else if self.parse_keyword(Keyword::OFFSETS) {
11203            let keywords = self.parse_comma_separated(|parser| {
11204                let next_token = parser.next_token();
11205                match &next_token.token {
11206                    Token::Word(w) => Ok(w.to_string()),
11207                    _ => parser.expected("SQL keyword", next_token),
11208                }
11209            })?;
11210            let value = self.parse_session_param_value()?;
11211            Ok(Statement::SetSessionParam(SetSessionParamKind::Offsets(
11212                SetSessionParamOffsets { keywords, value },
11213            )))
11214        } else {
11215            let names = self.parse_comma_separated(|parser| {
11216                let next_token = parser.next_token();
11217                match next_token.token {
11218                    Token::Word(w) => Ok(w.to_string()),
11219                    _ => parser.expected("Session param name", next_token),
11220                }
11221            })?;
11222            let value = self.parse_expr()?.to_string();
11223            Ok(Statement::SetSessionParam(SetSessionParamKind::Generic(
11224                SetSessionParamGeneric { names, value },
11225            )))
11226        }
11227    }
11228
11229    fn parse_session_param_value(&mut self) -> Result<SessionParamValue, ParserError> {
11230        if self.parse_keyword(Keyword::ON) {
11231            Ok(SessionParamValue::On)
11232        } else if self.parse_keyword(Keyword::OFF) {
11233            Ok(SessionParamValue::Off)
11234        } else {
11235            self.expected("ON or OFF", self.peek_token())
11236        }
11237    }
11238
11239    pub fn parse_show(&mut self) -> Result<Statement, ParserError> {
11240        let terse = self.parse_keyword(Keyword::TERSE);
11241        let extended = self.parse_keyword(Keyword::EXTENDED);
11242        let full = self.parse_keyword(Keyword::FULL);
11243        let session = self.parse_keyword(Keyword::SESSION);
11244        let global = self.parse_keyword(Keyword::GLOBAL);
11245        let external = self.parse_keyword(Keyword::EXTERNAL);
11246        if self
11247            .parse_one_of_keywords(&[Keyword::COLUMNS, Keyword::FIELDS])
11248            .is_some()
11249        {
11250            Ok(self.parse_show_columns(extended, full)?)
11251        } else if self.parse_keyword(Keyword::TABLES) {
11252            Ok(self.parse_show_tables(terse, extended, full, external)?)
11253        } else if self.parse_keywords(&[Keyword::MATERIALIZED, Keyword::VIEWS]) {
11254            Ok(self.parse_show_views(terse, true)?)
11255        } else if self.parse_keyword(Keyword::VIEWS) {
11256            Ok(self.parse_show_views(terse, false)?)
11257        } else if self.parse_keyword(Keyword::FUNCTIONS) {
11258            Ok(self.parse_show_functions()?)
11259        } else if extended || full {
11260            Err(ParserError::ParserError(
11261                "EXTENDED/FULL are not supported with this type of SHOW query".to_string(),
11262            ))
11263        } else if self.parse_one_of_keywords(&[Keyword::CREATE]).is_some() {
11264            Ok(self.parse_show_create()?)
11265        } else if self.parse_keyword(Keyword::COLLATION) {
11266            Ok(self.parse_show_collation()?)
11267        } else if self.parse_keyword(Keyword::VARIABLES)
11268            && dialect_of!(self is MySqlDialect | GenericDialect)
11269        {
11270            Ok(Statement::ShowVariables {
11271                filter: self.parse_show_statement_filter()?,
11272                session,
11273                global,
11274            })
11275        } else if self.parse_keyword(Keyword::STATUS)
11276            && dialect_of!(self is MySqlDialect | GenericDialect)
11277        {
11278            Ok(Statement::ShowStatus {
11279                filter: self.parse_show_statement_filter()?,
11280                session,
11281                global,
11282            })
11283        } else if self.parse_keyword(Keyword::DATABASES) {
11284            self.parse_show_databases(terse)
11285        } else if self.parse_keyword(Keyword::SCHEMAS) {
11286            self.parse_show_schemas(terse)
11287        } else {
11288            Ok(Statement::ShowVariable {
11289                variable: self.parse_identifiers()?,
11290            })
11291        }
11292    }
11293
11294    fn parse_show_databases(&mut self, terse: bool) -> Result<Statement, ParserError> {
11295        let history = self.parse_keyword(Keyword::HISTORY);
11296        let show_options = self.parse_show_stmt_options()?;
11297        Ok(Statement::ShowDatabases {
11298            terse,
11299            history,
11300            show_options,
11301        })
11302    }
11303
11304    fn parse_show_schemas(&mut self, terse: bool) -> Result<Statement, ParserError> {
11305        let history = self.parse_keyword(Keyword::HISTORY);
11306        let show_options = self.parse_show_stmt_options()?;
11307        Ok(Statement::ShowSchemas {
11308            terse,
11309            history,
11310            show_options,
11311        })
11312    }
11313
11314    pub fn parse_show_create(&mut self) -> Result<Statement, ParserError> {
11315        let obj_type = match self.expect_one_of_keywords(&[
11316            Keyword::TABLE,
11317            Keyword::TRIGGER,
11318            Keyword::FUNCTION,
11319            Keyword::PROCEDURE,
11320            Keyword::EVENT,
11321            Keyword::VIEW,
11322        ])? {
11323            Keyword::TABLE => Ok(ShowCreateObject::Table),
11324            Keyword::TRIGGER => Ok(ShowCreateObject::Trigger),
11325            Keyword::FUNCTION => Ok(ShowCreateObject::Function),
11326            Keyword::PROCEDURE => Ok(ShowCreateObject::Procedure),
11327            Keyword::EVENT => Ok(ShowCreateObject::Event),
11328            Keyword::VIEW => Ok(ShowCreateObject::View),
11329            keyword => Err(ParserError::ParserError(format!(
11330                "Unable to map keyword to ShowCreateObject: {keyword:?}"
11331            ))),
11332        }?;
11333
11334        let obj_name = self.parse_object_name(false)?;
11335
11336        Ok(Statement::ShowCreate { obj_type, obj_name })
11337    }
11338
11339    pub fn parse_show_columns(
11340        &mut self,
11341        extended: bool,
11342        full: bool,
11343    ) -> Result<Statement, ParserError> {
11344        let show_options = self.parse_show_stmt_options()?;
11345        Ok(Statement::ShowColumns {
11346            extended,
11347            full,
11348            show_options,
11349        })
11350    }
11351
11352    fn parse_show_tables(
11353        &mut self,
11354        terse: bool,
11355        extended: bool,
11356        full: bool,
11357        external: bool,
11358    ) -> Result<Statement, ParserError> {
11359        let history = !external && self.parse_keyword(Keyword::HISTORY);
11360        let show_options = self.parse_show_stmt_options()?;
11361        Ok(Statement::ShowTables {
11362            terse,
11363            history,
11364            extended,
11365            full,
11366            external,
11367            show_options,
11368        })
11369    }
11370
11371    fn parse_show_views(
11372        &mut self,
11373        terse: bool,
11374        materialized: bool,
11375    ) -> Result<Statement, ParserError> {
11376        let show_options = self.parse_show_stmt_options()?;
11377        Ok(Statement::ShowViews {
11378            materialized,
11379            terse,
11380            show_options,
11381        })
11382    }
11383
11384    pub fn parse_show_functions(&mut self) -> Result<Statement, ParserError> {
11385        let filter = self.parse_show_statement_filter()?;
11386        Ok(Statement::ShowFunctions { filter })
11387    }
11388
11389    pub fn parse_show_collation(&mut self) -> Result<Statement, ParserError> {
11390        let filter = self.parse_show_statement_filter()?;
11391        Ok(Statement::ShowCollation { filter })
11392    }
11393
11394    pub fn parse_show_statement_filter(
11395        &mut self,
11396    ) -> Result<Option<ShowStatementFilter>, ParserError> {
11397        if self.parse_keyword(Keyword::LIKE) {
11398            Ok(Some(ShowStatementFilter::Like(
11399                self.parse_literal_string()?,
11400            )))
11401        } else if self.parse_keyword(Keyword::ILIKE) {
11402            Ok(Some(ShowStatementFilter::ILike(
11403                self.parse_literal_string()?,
11404            )))
11405        } else if self.parse_keyword(Keyword::WHERE) {
11406            Ok(Some(ShowStatementFilter::Where(self.parse_expr()?)))
11407        } else {
11408            self.maybe_parse(|parser| -> Result<String, ParserError> {
11409                parser.parse_literal_string()
11410            })?
11411            .map_or(Ok(None), |filter| {
11412                Ok(Some(ShowStatementFilter::NoKeyword(filter)))
11413            })
11414        }
11415    }
11416
11417    pub fn parse_use(&mut self) -> Result<Statement, ParserError> {
11418        // Determine which keywords are recognized by the current dialect
11419        let parsed_keyword = if dialect_of!(self is HiveDialect) {
11420            // HiveDialect accepts USE DEFAULT; statement without any db specified
11421            if self.parse_keyword(Keyword::DEFAULT) {
11422                return Ok(Statement::Use(Use::Default));
11423            }
11424            None // HiveDialect doesn't expect any other specific keyword after `USE`
11425        } else if dialect_of!(self is DatabricksDialect) {
11426            self.parse_one_of_keywords(&[Keyword::CATALOG, Keyword::DATABASE, Keyword::SCHEMA])
11427        } else if dialect_of!(self is SnowflakeDialect) {
11428            self.parse_one_of_keywords(&[
11429                Keyword::DATABASE,
11430                Keyword::SCHEMA,
11431                Keyword::WAREHOUSE,
11432                Keyword::ROLE,
11433                Keyword::SECONDARY,
11434            ])
11435        } else {
11436            None // No specific keywords for other dialects, including GenericDialect
11437        };
11438
11439        let result = if matches!(parsed_keyword, Some(Keyword::SECONDARY)) {
11440            self.parse_secondary_roles()?
11441        } else {
11442            let obj_name = self.parse_object_name(false)?;
11443            match parsed_keyword {
11444                Some(Keyword::CATALOG) => Use::Catalog(obj_name),
11445                Some(Keyword::DATABASE) => Use::Database(obj_name),
11446                Some(Keyword::SCHEMA) => Use::Schema(obj_name),
11447                Some(Keyword::WAREHOUSE) => Use::Warehouse(obj_name),
11448                Some(Keyword::ROLE) => Use::Role(obj_name),
11449                _ => Use::Object(obj_name),
11450            }
11451        };
11452
11453        Ok(Statement::Use(result))
11454    }
11455
11456    fn parse_secondary_roles(&mut self) -> Result<Use, ParserError> {
11457        self.expect_one_of_keywords(&[Keyword::ROLES, Keyword::ROLE])?;
11458        if self.parse_keyword(Keyword::NONE) {
11459            Ok(Use::SecondaryRoles(SecondaryRoles::None))
11460        } else if self.parse_keyword(Keyword::ALL) {
11461            Ok(Use::SecondaryRoles(SecondaryRoles::All))
11462        } else {
11463            let roles = self.parse_comma_separated(|parser| parser.parse_identifier())?;
11464            Ok(Use::SecondaryRoles(SecondaryRoles::List(roles)))
11465        }
11466    }
11467
11468    pub fn parse_table_and_joins(&mut self) -> Result<TableWithJoins, ParserError> {
11469        let relation = self.parse_table_factor()?;
11470        // Note that for keywords to be properly handled here, they need to be
11471        // added to `RESERVED_FOR_TABLE_ALIAS`, otherwise they may be parsed as
11472        // a table alias.
11473        let mut joins = vec![];
11474        loop {
11475            let global = self.parse_keyword(Keyword::GLOBAL);
11476            let join = if self.parse_keyword(Keyword::CROSS) {
11477                let join_operator = if self.parse_keyword(Keyword::JOIN) {
11478                    JoinOperator::CrossJoin
11479                } else if self.parse_keyword(Keyword::APPLY) {
11480                    // MSSQL extension, similar to CROSS JOIN LATERAL
11481                    JoinOperator::CrossApply
11482                } else {
11483                    return self.expected("JOIN or APPLY after CROSS", self.peek_token());
11484                };
11485                Join {
11486                    relation: self.parse_table_factor()?,
11487                    global,
11488                    join_operator,
11489                }
11490            } else if self.parse_keyword(Keyword::OUTER) {
11491                // MSSQL extension, similar to LEFT JOIN LATERAL .. ON 1=1
11492                self.expect_keyword_is(Keyword::APPLY)?;
11493                Join {
11494                    relation: self.parse_table_factor()?,
11495                    global,
11496                    join_operator: JoinOperator::OuterApply,
11497                }
11498            } else if self.parse_keyword(Keyword::ASOF) {
11499                self.expect_keyword_is(Keyword::JOIN)?;
11500                let relation = self.parse_table_factor()?;
11501                self.expect_keyword_is(Keyword::MATCH_CONDITION)?;
11502                let match_condition = self.parse_parenthesized(Self::parse_expr)?;
11503                Join {
11504                    relation,
11505                    global,
11506                    join_operator: JoinOperator::AsOf {
11507                        match_condition,
11508                        constraint: self.parse_join_constraint(false)?,
11509                    },
11510                }
11511            } else {
11512                let natural = self.parse_keyword(Keyword::NATURAL);
11513                let peek_keyword = if let Token::Word(w) = self.peek_token().token {
11514                    w.keyword
11515                } else {
11516                    Keyword::NoKeyword
11517                };
11518
11519                let join_operator_type = match peek_keyword {
11520                    Keyword::INNER | Keyword::JOIN => {
11521                        let inner = self.parse_keyword(Keyword::INNER); // [ INNER ]
11522                        self.expect_keyword_is(Keyword::JOIN)?;
11523                        if inner {
11524                            JoinOperator::Inner
11525                        } else {
11526                            JoinOperator::Join
11527                        }
11528                    }
11529                    kw @ Keyword::LEFT | kw @ Keyword::RIGHT => {
11530                        let _ = self.next_token(); // consume LEFT/RIGHT
11531                        let is_left = kw == Keyword::LEFT;
11532                        let join_type = self.parse_one_of_keywords(&[
11533                            Keyword::OUTER,
11534                            Keyword::SEMI,
11535                            Keyword::ANTI,
11536                            Keyword::JOIN,
11537                        ]);
11538                        match join_type {
11539                            Some(Keyword::OUTER) => {
11540                                self.expect_keyword_is(Keyword::JOIN)?;
11541                                if is_left {
11542                                    JoinOperator::LeftOuter
11543                                } else {
11544                                    JoinOperator::RightOuter
11545                                }
11546                            }
11547                            Some(Keyword::SEMI) => {
11548                                self.expect_keyword_is(Keyword::JOIN)?;
11549                                if is_left {
11550                                    JoinOperator::LeftSemi
11551                                } else {
11552                                    JoinOperator::RightSemi
11553                                }
11554                            }
11555                            Some(Keyword::ANTI) => {
11556                                self.expect_keyword_is(Keyword::JOIN)?;
11557                                if is_left {
11558                                    JoinOperator::LeftAnti
11559                                } else {
11560                                    JoinOperator::RightAnti
11561                                }
11562                            }
11563                            Some(Keyword::JOIN) => {
11564                                if is_left {
11565                                    JoinOperator::Left
11566                                } else {
11567                                    JoinOperator::Right
11568                                }
11569                            }
11570                            _ => {
11571                                return Err(ParserError::ParserError(format!(
11572                                    "expected OUTER, SEMI, ANTI or JOIN after {kw:?}"
11573                                )))
11574                            }
11575                        }
11576                    }
11577                    Keyword::ANTI => {
11578                        let _ = self.next_token(); // consume ANTI
11579                        self.expect_keyword_is(Keyword::JOIN)?;
11580                        JoinOperator::Anti
11581                    }
11582                    Keyword::SEMI => {
11583                        let _ = self.next_token(); // consume SEMI
11584                        self.expect_keyword_is(Keyword::JOIN)?;
11585                        JoinOperator::Semi
11586                    }
11587                    Keyword::FULL => {
11588                        let _ = self.next_token(); // consume FULL
11589                        let _ = self.parse_keyword(Keyword::OUTER); // [ OUTER ]
11590                        self.expect_keyword_is(Keyword::JOIN)?;
11591                        JoinOperator::FullOuter
11592                    }
11593                    Keyword::OUTER => {
11594                        return self.expected("LEFT, RIGHT, or FULL", self.peek_token());
11595                    }
11596                    _ if natural => {
11597                        return self.expected("a join type after NATURAL", self.peek_token());
11598                    }
11599                    _ => break,
11600                };
11601                let relation = self.parse_table_factor()?;
11602                let join_constraint = self.parse_join_constraint(natural)?;
11603                Join {
11604                    relation,
11605                    global,
11606                    join_operator: join_operator_type(join_constraint),
11607                }
11608            };
11609            joins.push(join);
11610        }
11611        Ok(TableWithJoins { relation, joins })
11612    }
11613
11614    /// A table name or a parenthesized subquery, followed by optional `[AS] alias`
11615    pub fn parse_table_factor(&mut self) -> Result<TableFactor, ParserError> {
11616        if self.parse_keyword(Keyword::LATERAL) {
11617            // LATERAL must always be followed by a subquery or table function.
11618            if self.consume_token(&Token::LParen) {
11619                self.parse_derived_table_factor(Lateral)
11620            } else {
11621                let name = self.parse_object_name(false)?;
11622                self.expect_token(&Token::LParen)?;
11623                let args = self.parse_optional_args()?;
11624                let alias = self.maybe_parse_table_alias()?;
11625                Ok(TableFactor::Function {
11626                    lateral: true,
11627                    name,
11628                    args,
11629                    alias,
11630                })
11631            }
11632        } else if self.parse_keyword(Keyword::TABLE) {
11633            // parse table function (SELECT * FROM TABLE (<expr>) [ AS <alias> ])
11634            self.expect_token(&Token::LParen)?;
11635            let expr = self.parse_expr()?;
11636            self.expect_token(&Token::RParen)?;
11637            let alias = self.maybe_parse_table_alias()?;
11638            Ok(TableFactor::TableFunction { expr, alias })
11639        } else if self.consume_token(&Token::LParen) {
11640            // A left paren introduces either a derived table (i.e., a subquery)
11641            // or a nested join. It's nearly impossible to determine ahead of
11642            // time which it is... so we just try to parse both.
11643            //
11644            // Here's an example that demonstrates the complexity:
11645            //                     /-------------------------------------------------------\
11646            //                     | /-----------------------------------\                 |
11647            //     SELECT * FROM ( ( ( (SELECT 1) UNION (SELECT 2) ) AS t1 NATURAL JOIN t2 ) )
11648            //                   ^ ^ ^ ^
11649            //                   | | | |
11650            //                   | | | |
11651            //                   | | | (4) belongs to a SetExpr::Query inside the subquery
11652            //                   | | (3) starts a derived table (subquery)
11653            //                   | (2) starts a nested join
11654            //                   (1) an additional set of parens around a nested join
11655            //
11656
11657            // If the recently consumed '(' starts a derived table, the call to
11658            // `parse_derived_table_factor` below will return success after parsing the
11659            // subquery, followed by the closing ')', and the alias of the derived table.
11660            // In the example above this is case (3).
11661            if let Some(mut table) =
11662                self.maybe_parse(|parser| parser.parse_derived_table_factor(NotLateral))?
11663            {
11664                while let Some(kw) = self.parse_one_of_keywords(&[Keyword::PIVOT, Keyword::UNPIVOT])
11665                {
11666                    table = match kw {
11667                        Keyword::PIVOT => self.parse_pivot_table_factor(table)?,
11668                        Keyword::UNPIVOT => self.parse_unpivot_table_factor(table)?,
11669                        _ => unreachable!(),
11670                    }
11671                }
11672                return Ok(table);
11673            }
11674
11675            // A parsing error from `parse_derived_table_factor` indicates that the '(' we've
11676            // recently consumed does not start a derived table (cases 1, 2, or 4).
11677            // `maybe_parse` will ignore such an error and rewind to be after the opening '('.
11678
11679            // Inside the parentheses we expect to find an (A) table factor
11680            // followed by some joins or (B) another level of nesting.
11681            let mut table_and_joins = self.parse_table_and_joins()?;
11682
11683            #[allow(clippy::if_same_then_else)]
11684            if !table_and_joins.joins.is_empty() {
11685                self.expect_token(&Token::RParen)?;
11686                let alias = self.maybe_parse_table_alias()?;
11687                Ok(TableFactor::NestedJoin {
11688                    table_with_joins: Box::new(table_and_joins),
11689                    alias,
11690                }) // (A)
11691            } else if let TableFactor::NestedJoin {
11692                table_with_joins: _,
11693                alias: _,
11694            } = &table_and_joins.relation
11695            {
11696                // (B): `table_and_joins` (what we found inside the parentheses)
11697                // is a nested join `(foo JOIN bar)`, not followed by other joins.
11698                self.expect_token(&Token::RParen)?;
11699                let alias = self.maybe_parse_table_alias()?;
11700                Ok(TableFactor::NestedJoin {
11701                    table_with_joins: Box::new(table_and_joins),
11702                    alias,
11703                })
11704            } else if dialect_of!(self is SnowflakeDialect | GenericDialect) {
11705                // Dialect-specific behavior: Snowflake diverges from the
11706                // standard and from most of the other implementations by
11707                // allowing extra parentheses not only around a join (B), but
11708                // around lone table names (e.g. `FROM (mytable [AS alias])`)
11709                // and around derived tables (e.g. `FROM ((SELECT ...)
11710                // [AS alias])`) as well.
11711                self.expect_token(&Token::RParen)?;
11712
11713                if let Some(outer_alias) = self.maybe_parse_table_alias()? {
11714                    // Snowflake also allows specifying an alias *after* parens
11715                    // e.g. `FROM (mytable) AS alias`
11716                    match &mut table_and_joins.relation {
11717                        TableFactor::Derived { alias, .. }
11718                        | TableFactor::Table { alias, .. }
11719                        | TableFactor::Function { alias, .. }
11720                        | TableFactor::UNNEST { alias, .. }
11721                        | TableFactor::JsonTable { alias, .. }
11722                        | TableFactor::OpenJsonTable { alias, .. }
11723                        | TableFactor::TableFunction { alias, .. }
11724                        | TableFactor::Pivot { alias, .. }
11725                        | TableFactor::Unpivot { alias, .. }
11726                        | TableFactor::MatchRecognize { alias, .. }
11727                        | TableFactor::NestedJoin { alias, .. } => {
11728                            // but not `FROM (mytable AS alias1) AS alias2`.
11729                            if let Some(inner_alias) = alias {
11730                                return Err(ParserError::ParserError(format!(
11731                                    "duplicate alias {inner_alias}"
11732                                )));
11733                            }
11734                            // Act as if the alias was specified normally next
11735                            // to the table name: `(mytable) AS alias` ->
11736                            // `(mytable AS alias)`
11737                            alias.replace(outer_alias);
11738                        }
11739                    };
11740                }
11741                // Do not store the extra set of parens in the AST
11742                Ok(table_and_joins.relation)
11743            } else {
11744                // The SQL spec prohibits derived tables and bare tables from
11745                // appearing alone in parentheses (e.g. `FROM (mytable)`)
11746                self.expected("joined table", self.peek_token())
11747            }
11748        } else if dialect_of!(self is SnowflakeDialect | DatabricksDialect | GenericDialect)
11749            && matches!(
11750                self.peek_tokens(),
11751                [
11752                    Token::Word(Word {
11753                        keyword: Keyword::VALUES,
11754                        ..
11755                    }),
11756                    Token::LParen
11757                ]
11758            )
11759        {
11760            self.expect_keyword_is(Keyword::VALUES)?;
11761
11762            // Snowflake and Databricks allow syntax like below:
11763            // SELECT * FROM VALUES (1, 'a'), (2, 'b') AS t (col1, col2)
11764            // where there are no parentheses around the VALUES clause.
11765            let values = SetExpr::Values(self.parse_values(false)?);
11766            let alias = self.maybe_parse_table_alias()?;
11767            Ok(TableFactor::Derived {
11768                lateral: false,
11769                subquery: Box::new(Query {
11770                    with: None,
11771                    body: Box::new(values),
11772                    order_by: None,
11773                    limit: None,
11774                    limit_by: vec![],
11775                    offset: None,
11776                    fetch: None,
11777                    locks: vec![],
11778                    for_clause: None,
11779                    settings: None,
11780                    format_clause: None,
11781                }),
11782                alias,
11783            })
11784        } else if dialect_of!(self is BigQueryDialect | PostgreSqlDialect | GenericDialect)
11785            && self.parse_keyword(Keyword::UNNEST)
11786        {
11787            self.expect_token(&Token::LParen)?;
11788            let array_exprs = self.parse_comma_separated(Parser::parse_expr)?;
11789            self.expect_token(&Token::RParen)?;
11790
11791            let with_ordinality = self.parse_keywords(&[Keyword::WITH, Keyword::ORDINALITY]);
11792            let alias = match self.maybe_parse_table_alias() {
11793                Ok(Some(alias)) => Some(alias),
11794                Ok(None) => None,
11795                Err(e) => return Err(e),
11796            };
11797
11798            let with_offset = match self.expect_keywords(&[Keyword::WITH, Keyword::OFFSET]) {
11799                Ok(()) => true,
11800                Err(_) => false,
11801            };
11802
11803            let with_offset_alias = if with_offset {
11804                match self.parse_optional_alias(keywords::RESERVED_FOR_COLUMN_ALIAS) {
11805                    Ok(Some(alias)) => Some(alias),
11806                    Ok(None) => None,
11807                    Err(e) => return Err(e),
11808                }
11809            } else {
11810                None
11811            };
11812
11813            Ok(TableFactor::UNNEST {
11814                alias,
11815                array_exprs,
11816                with_offset,
11817                with_offset_alias,
11818                with_ordinality,
11819            })
11820        } else if self.parse_keyword_with_tokens(Keyword::JSON_TABLE, &[Token::LParen]) {
11821            let json_expr = self.parse_expr()?;
11822            self.expect_token(&Token::Comma)?;
11823            let json_path = self.parse_value()?.value;
11824            self.expect_keyword_is(Keyword::COLUMNS)?;
11825            self.expect_token(&Token::LParen)?;
11826            let columns = self.parse_comma_separated(Parser::parse_json_table_column_def)?;
11827            self.expect_token(&Token::RParen)?;
11828            self.expect_token(&Token::RParen)?;
11829            let alias = self.maybe_parse_table_alias()?;
11830            Ok(TableFactor::JsonTable {
11831                json_expr,
11832                json_path,
11833                columns,
11834                alias,
11835            })
11836        } else if self.parse_keyword_with_tokens(Keyword::OPENJSON, &[Token::LParen]) {
11837            self.prev_token();
11838            self.parse_open_json_table_factor()
11839        } else {
11840            let name = self.parse_object_name(true)?;
11841
11842            let json_path = match self.peek_token().token {
11843                Token::LBracket if self.dialect.supports_partiql() => Some(self.parse_json_path()?),
11844                _ => None,
11845            };
11846
11847            let partitions: Vec<Ident> = if dialect_of!(self is MySqlDialect | GenericDialect)
11848                && self.parse_keyword(Keyword::PARTITION)
11849            {
11850                self.parse_parenthesized_identifiers()?
11851            } else {
11852                vec![]
11853            };
11854
11855            // Parse potential version qualifier
11856            let version = self.maybe_parse_table_version()?;
11857
11858            // Postgres, MSSQL, ClickHouse: table-valued functions:
11859            let args = if self.consume_token(&Token::LParen) {
11860                Some(self.parse_table_function_args()?)
11861            } else {
11862                None
11863            };
11864
11865            let with_ordinality = self.parse_keywords(&[Keyword::WITH, Keyword::ORDINALITY]);
11866
11867            let mut sample = None;
11868            if self.dialect.supports_table_sample_before_alias() {
11869                if let Some(parsed_sample) = self.maybe_parse_table_sample()? {
11870                    sample = Some(TableSampleKind::BeforeTableAlias(parsed_sample));
11871                }
11872            }
11873
11874            let alias = self.maybe_parse_table_alias()?;
11875
11876            // MYSQL-specific table hints:
11877            let index_hints = if self.dialect.supports_table_hints() {
11878                self.maybe_parse(|p| p.parse_table_index_hints())?
11879                    .unwrap_or(vec![])
11880            } else {
11881                vec![]
11882            };
11883
11884            // MSSQL-specific table hints:
11885            let mut with_hints = vec![];
11886            if self.parse_keyword(Keyword::WITH) {
11887                if self.consume_token(&Token::LParen) {
11888                    with_hints = self.parse_comma_separated(Parser::parse_expr)?;
11889                    self.expect_token(&Token::RParen)?;
11890                } else {
11891                    // rewind, as WITH may belong to the next statement's CTE
11892                    self.prev_token();
11893                }
11894            };
11895
11896            if !self.dialect.supports_table_sample_before_alias() {
11897                if let Some(parsed_sample) = self.maybe_parse_table_sample()? {
11898                    sample = Some(TableSampleKind::AfterTableAlias(parsed_sample));
11899                }
11900            }
11901
11902            let mut table = TableFactor::Table {
11903                name,
11904                alias,
11905                args,
11906                with_hints,
11907                version,
11908                partitions,
11909                with_ordinality,
11910                json_path,
11911                sample,
11912                index_hints,
11913            };
11914
11915            while let Some(kw) = self.parse_one_of_keywords(&[Keyword::PIVOT, Keyword::UNPIVOT]) {
11916                table = match kw {
11917                    Keyword::PIVOT => self.parse_pivot_table_factor(table)?,
11918                    Keyword::UNPIVOT => self.parse_unpivot_table_factor(table)?,
11919                    _ => unreachable!(),
11920                }
11921            }
11922
11923            if self.dialect.supports_match_recognize()
11924                && self.parse_keyword(Keyword::MATCH_RECOGNIZE)
11925            {
11926                table = self.parse_match_recognize(table)?;
11927            }
11928
11929            Ok(table)
11930        }
11931    }
11932
11933    fn maybe_parse_table_sample(&mut self) -> Result<Option<Box<TableSample>>, ParserError> {
11934        let modifier = if self.parse_keyword(Keyword::TABLESAMPLE) {
11935            TableSampleModifier::TableSample
11936        } else if self.parse_keyword(Keyword::SAMPLE) {
11937            TableSampleModifier::Sample
11938        } else {
11939            return Ok(None);
11940        };
11941
11942        let name = match self.parse_one_of_keywords(&[
11943            Keyword::BERNOULLI,
11944            Keyword::ROW,
11945            Keyword::SYSTEM,
11946            Keyword::BLOCK,
11947        ]) {
11948            Some(Keyword::BERNOULLI) => Some(TableSampleMethod::Bernoulli),
11949            Some(Keyword::ROW) => Some(TableSampleMethod::Row),
11950            Some(Keyword::SYSTEM) => Some(TableSampleMethod::System),
11951            Some(Keyword::BLOCK) => Some(TableSampleMethod::Block),
11952            _ => None,
11953        };
11954
11955        let parenthesized = self.consume_token(&Token::LParen);
11956
11957        let (quantity, bucket) = if parenthesized && self.parse_keyword(Keyword::BUCKET) {
11958            let selected_bucket = self.parse_number_value()?.value;
11959            self.expect_keywords(&[Keyword::OUT, Keyword::OF])?;
11960            let total = self.parse_number_value()?.value;
11961            let on = if self.parse_keyword(Keyword::ON) {
11962                Some(self.parse_expr()?)
11963            } else {
11964                None
11965            };
11966            (
11967                None,
11968                Some(TableSampleBucket {
11969                    bucket: selected_bucket,
11970                    total,
11971                    on,
11972                }),
11973            )
11974        } else {
11975            let value = match self.maybe_parse(|p| p.parse_expr())? {
11976                Some(num) => num,
11977                None => {
11978                    let next_token = self.next_token();
11979                    if let Token::Word(w) = next_token.token {
11980                        Expr::Value(Value::Placeholder(w.value).with_span(next_token.span))
11981                    } else {
11982                        return parser_err!(
11983                            "Expecting number or byte length e.g. 100M",
11984                            self.peek_token().span.start
11985                        );
11986                    }
11987                }
11988            };
11989            let unit = if self.parse_keyword(Keyword::ROWS) {
11990                Some(TableSampleUnit::Rows)
11991            } else if self.parse_keyword(Keyword::PERCENT) {
11992                Some(TableSampleUnit::Percent)
11993            } else {
11994                None
11995            };
11996            (
11997                Some(TableSampleQuantity {
11998                    parenthesized,
11999                    value,
12000                    unit,
12001                }),
12002                None,
12003            )
12004        };
12005        if parenthesized {
12006            self.expect_token(&Token::RParen)?;
12007        }
12008
12009        let seed = if self.parse_keyword(Keyword::REPEATABLE) {
12010            Some(self.parse_table_sample_seed(TableSampleSeedModifier::Repeatable)?)
12011        } else if self.parse_keyword(Keyword::SEED) {
12012            Some(self.parse_table_sample_seed(TableSampleSeedModifier::Seed)?)
12013        } else {
12014            None
12015        };
12016
12017        let offset = if self.parse_keyword(Keyword::OFFSET) {
12018            Some(self.parse_expr()?)
12019        } else {
12020            None
12021        };
12022
12023        Ok(Some(Box::new(TableSample {
12024            modifier,
12025            name,
12026            quantity,
12027            seed,
12028            bucket,
12029            offset,
12030        })))
12031    }
12032
12033    fn parse_table_sample_seed(
12034        &mut self,
12035        modifier: TableSampleSeedModifier,
12036    ) -> Result<TableSampleSeed, ParserError> {
12037        self.expect_token(&Token::LParen)?;
12038        let value = self.parse_number_value()?.value;
12039        self.expect_token(&Token::RParen)?;
12040        Ok(TableSampleSeed { modifier, value })
12041    }
12042
12043    /// Parses `OPENJSON( jsonExpression [ , path ] )  [ <with_clause> ]` clause,
12044    /// assuming the `OPENJSON` keyword was already consumed.
12045    fn parse_open_json_table_factor(&mut self) -> Result<TableFactor, ParserError> {
12046        self.expect_token(&Token::LParen)?;
12047        let json_expr = self.parse_expr()?;
12048        let json_path = if self.consume_token(&Token::Comma) {
12049            Some(self.parse_value()?.value)
12050        } else {
12051            None
12052        };
12053        self.expect_token(&Token::RParen)?;
12054        let columns = if self.parse_keyword(Keyword::WITH) {
12055            self.expect_token(&Token::LParen)?;
12056            let columns = self.parse_comma_separated(Parser::parse_openjson_table_column_def)?;
12057            self.expect_token(&Token::RParen)?;
12058            columns
12059        } else {
12060            Vec::new()
12061        };
12062        let alias = self.maybe_parse_table_alias()?;
12063        Ok(TableFactor::OpenJsonTable {
12064            json_expr,
12065            json_path,
12066            columns,
12067            alias,
12068        })
12069    }
12070
12071    fn parse_match_recognize(&mut self, table: TableFactor) -> Result<TableFactor, ParserError> {
12072        self.expect_token(&Token::LParen)?;
12073
12074        let partition_by = if self.parse_keywords(&[Keyword::PARTITION, Keyword::BY]) {
12075            self.parse_comma_separated(Parser::parse_expr)?
12076        } else {
12077            vec![]
12078        };
12079
12080        let order_by = if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
12081            self.parse_comma_separated(Parser::parse_order_by_expr)?
12082        } else {
12083            vec![]
12084        };
12085
12086        let measures = if self.parse_keyword(Keyword::MEASURES) {
12087            self.parse_comma_separated(|p| {
12088                let expr = p.parse_expr()?;
12089                let _ = p.parse_keyword(Keyword::AS);
12090                let alias = p.parse_identifier()?;
12091                Ok(Measure { expr, alias })
12092            })?
12093        } else {
12094            vec![]
12095        };
12096
12097        let rows_per_match =
12098            if self.parse_keywords(&[Keyword::ONE, Keyword::ROW, Keyword::PER, Keyword::MATCH]) {
12099                Some(RowsPerMatch::OneRow)
12100            } else if self.parse_keywords(&[
12101                Keyword::ALL,
12102                Keyword::ROWS,
12103                Keyword::PER,
12104                Keyword::MATCH,
12105            ]) {
12106                Some(RowsPerMatch::AllRows(
12107                    if self.parse_keywords(&[Keyword::SHOW, Keyword::EMPTY, Keyword::MATCHES]) {
12108                        Some(EmptyMatchesMode::Show)
12109                    } else if self.parse_keywords(&[
12110                        Keyword::OMIT,
12111                        Keyword::EMPTY,
12112                        Keyword::MATCHES,
12113                    ]) {
12114                        Some(EmptyMatchesMode::Omit)
12115                    } else if self.parse_keywords(&[
12116                        Keyword::WITH,
12117                        Keyword::UNMATCHED,
12118                        Keyword::ROWS,
12119                    ]) {
12120                        Some(EmptyMatchesMode::WithUnmatched)
12121                    } else {
12122                        None
12123                    },
12124                ))
12125            } else {
12126                None
12127            };
12128
12129        let after_match_skip =
12130            if self.parse_keywords(&[Keyword::AFTER, Keyword::MATCH, Keyword::SKIP]) {
12131                if self.parse_keywords(&[Keyword::PAST, Keyword::LAST, Keyword::ROW]) {
12132                    Some(AfterMatchSkip::PastLastRow)
12133                } else if self.parse_keywords(&[Keyword::TO, Keyword::NEXT, Keyword::ROW]) {
12134                    Some(AfterMatchSkip::ToNextRow)
12135                } else if self.parse_keywords(&[Keyword::TO, Keyword::FIRST]) {
12136                    Some(AfterMatchSkip::ToFirst(self.parse_identifier()?))
12137                } else if self.parse_keywords(&[Keyword::TO, Keyword::LAST]) {
12138                    Some(AfterMatchSkip::ToLast(self.parse_identifier()?))
12139                } else {
12140                    let found = self.next_token();
12141                    return self.expected("after match skip option", found);
12142                }
12143            } else {
12144                None
12145            };
12146
12147        self.expect_keyword_is(Keyword::PATTERN)?;
12148        let pattern = self.parse_parenthesized(Self::parse_pattern)?;
12149
12150        self.expect_keyword_is(Keyword::DEFINE)?;
12151
12152        let symbols = self.parse_comma_separated(|p| {
12153            let symbol = p.parse_identifier()?;
12154            p.expect_keyword_is(Keyword::AS)?;
12155            let definition = p.parse_expr()?;
12156            Ok(SymbolDefinition { symbol, definition })
12157        })?;
12158
12159        self.expect_token(&Token::RParen)?;
12160
12161        let alias = self.maybe_parse_table_alias()?;
12162
12163        Ok(TableFactor::MatchRecognize {
12164            table: Box::new(table),
12165            partition_by,
12166            order_by,
12167            measures,
12168            rows_per_match,
12169            after_match_skip,
12170            pattern,
12171            symbols,
12172            alias,
12173        })
12174    }
12175
12176    fn parse_base_pattern(&mut self) -> Result<MatchRecognizePattern, ParserError> {
12177        match self.next_token().token {
12178            Token::Caret => Ok(MatchRecognizePattern::Symbol(MatchRecognizeSymbol::Start)),
12179            Token::Placeholder(s) if s == "$" => {
12180                Ok(MatchRecognizePattern::Symbol(MatchRecognizeSymbol::End))
12181            }
12182            Token::LBrace => {
12183                self.expect_token(&Token::Minus)?;
12184                let symbol = self.parse_identifier().map(MatchRecognizeSymbol::Named)?;
12185                self.expect_token(&Token::Minus)?;
12186                self.expect_token(&Token::RBrace)?;
12187                Ok(MatchRecognizePattern::Exclude(symbol))
12188            }
12189            Token::Word(Word {
12190                value,
12191                quote_style: None,
12192                ..
12193            }) if value == "PERMUTE" => {
12194                self.expect_token(&Token::LParen)?;
12195                let symbols = self.parse_comma_separated(|p| {
12196                    p.parse_identifier().map(MatchRecognizeSymbol::Named)
12197                })?;
12198                self.expect_token(&Token::RParen)?;
12199                Ok(MatchRecognizePattern::Permute(symbols))
12200            }
12201            Token::LParen => {
12202                let pattern = self.parse_pattern()?;
12203                self.expect_token(&Token::RParen)?;
12204                Ok(MatchRecognizePattern::Group(Box::new(pattern)))
12205            }
12206            _ => {
12207                self.prev_token();
12208                self.parse_identifier()
12209                    .map(MatchRecognizeSymbol::Named)
12210                    .map(MatchRecognizePattern::Symbol)
12211            }
12212        }
12213    }
12214
12215    fn parse_repetition_pattern(&mut self) -> Result<MatchRecognizePattern, ParserError> {
12216        let mut pattern = self.parse_base_pattern()?;
12217        loop {
12218            let token = self.next_token();
12219            let quantifier = match token.token {
12220                Token::Mul => RepetitionQuantifier::ZeroOrMore,
12221                Token::Plus => RepetitionQuantifier::OneOrMore,
12222                Token::Placeholder(s) if s == "?" => RepetitionQuantifier::AtMostOne,
12223                Token::LBrace => {
12224                    // quantifier is a range like {n} or {n,} or {,m} or {n,m}
12225                    let token = self.next_token();
12226                    match token.token {
12227                        Token::Comma => {
12228                            let next_token = self.next_token();
12229                            let Token::Number(n, _) = next_token.token else {
12230                                return self.expected("literal number", next_token);
12231                            };
12232                            self.expect_token(&Token::RBrace)?;
12233                            RepetitionQuantifier::AtMost(Self::parse(n, token.span.start)?)
12234                        }
12235                        Token::Number(n, _) if self.consume_token(&Token::Comma) => {
12236                            let next_token = self.next_token();
12237                            match next_token.token {
12238                                Token::Number(m, _) => {
12239                                    self.expect_token(&Token::RBrace)?;
12240                                    RepetitionQuantifier::Range(
12241                                        Self::parse(n, token.span.start)?,
12242                                        Self::parse(m, token.span.start)?,
12243                                    )
12244                                }
12245                                Token::RBrace => {
12246                                    RepetitionQuantifier::AtLeast(Self::parse(n, token.span.start)?)
12247                                }
12248                                _ => {
12249                                    return self.expected("} or upper bound", next_token);
12250                                }
12251                            }
12252                        }
12253                        Token::Number(n, _) => {
12254                            self.expect_token(&Token::RBrace)?;
12255                            RepetitionQuantifier::Exactly(Self::parse(n, token.span.start)?)
12256                        }
12257                        _ => return self.expected("quantifier range", token),
12258                    }
12259                }
12260                _ => {
12261                    self.prev_token();
12262                    break;
12263                }
12264            };
12265            pattern = MatchRecognizePattern::Repetition(Box::new(pattern), quantifier);
12266        }
12267        Ok(pattern)
12268    }
12269
12270    fn parse_concat_pattern(&mut self) -> Result<MatchRecognizePattern, ParserError> {
12271        let mut patterns = vec![self.parse_repetition_pattern()?];
12272        while !matches!(self.peek_token().token, Token::RParen | Token::Pipe) {
12273            patterns.push(self.parse_repetition_pattern()?);
12274        }
12275        match <[MatchRecognizePattern; 1]>::try_from(patterns) {
12276            Ok([pattern]) => Ok(pattern),
12277            Err(patterns) => Ok(MatchRecognizePattern::Concat(patterns)),
12278        }
12279    }
12280
12281    fn parse_pattern(&mut self) -> Result<MatchRecognizePattern, ParserError> {
12282        let pattern = self.parse_concat_pattern()?;
12283        if self.consume_token(&Token::Pipe) {
12284            match self.parse_pattern()? {
12285                // flatten nested alternations
12286                MatchRecognizePattern::Alternation(mut patterns) => {
12287                    patterns.insert(0, pattern);
12288                    Ok(MatchRecognizePattern::Alternation(patterns))
12289                }
12290                next => Ok(MatchRecognizePattern::Alternation(vec![pattern, next])),
12291            }
12292        } else {
12293            Ok(pattern)
12294        }
12295    }
12296
12297    /// Parses a the timestamp version specifier (i.e. query historical data)
12298    pub fn maybe_parse_table_version(&mut self) -> Result<Option<TableVersion>, ParserError> {
12299        if self.dialect.supports_timestamp_versioning() {
12300            if self.parse_keywords(&[Keyword::FOR, Keyword::SYSTEM_TIME, Keyword::AS, Keyword::OF])
12301            {
12302                let expr = self.parse_expr()?;
12303                return Ok(Some(TableVersion::ForSystemTimeAsOf(expr)));
12304            } else if self.peek_keyword(Keyword::AT) || self.peek_keyword(Keyword::BEFORE) {
12305                let func_name = self.parse_object_name(true)?;
12306                let func = self.parse_function(func_name)?;
12307                return Ok(Some(TableVersion::Function(func)));
12308            }
12309        }
12310        Ok(None)
12311    }
12312
12313    /// Parses MySQL's JSON_TABLE column definition.
12314    /// For example: `id INT EXISTS PATH '$' DEFAULT '0' ON EMPTY ERROR ON ERROR`
12315    pub fn parse_json_table_column_def(&mut self) -> Result<JsonTableColumn, ParserError> {
12316        if self.parse_keyword(Keyword::NESTED) {
12317            let _has_path_keyword = self.parse_keyword(Keyword::PATH);
12318            let path = self.parse_value()?.value;
12319            self.expect_keyword_is(Keyword::COLUMNS)?;
12320            let columns = self.parse_parenthesized(|p| {
12321                p.parse_comma_separated(Self::parse_json_table_column_def)
12322            })?;
12323            return Ok(JsonTableColumn::Nested(JsonTableNestedColumn {
12324                path,
12325                columns,
12326            }));
12327        }
12328        let name = self.parse_identifier()?;
12329        if self.parse_keyword(Keyword::FOR) {
12330            self.expect_keyword_is(Keyword::ORDINALITY)?;
12331            return Ok(JsonTableColumn::ForOrdinality(name));
12332        }
12333        let r#type = self.parse_data_type()?;
12334        let exists = self.parse_keyword(Keyword::EXISTS);
12335        self.expect_keyword_is(Keyword::PATH)?;
12336        let path = self.parse_value()?.value;
12337        let mut on_empty = None;
12338        let mut on_error = None;
12339        while let Some(error_handling) = self.parse_json_table_column_error_handling()? {
12340            if self.parse_keyword(Keyword::EMPTY) {
12341                on_empty = Some(error_handling);
12342            } else {
12343                self.expect_keyword_is(Keyword::ERROR)?;
12344                on_error = Some(error_handling);
12345            }
12346        }
12347        Ok(JsonTableColumn::Named(JsonTableNamedColumn {
12348            name,
12349            r#type,
12350            path,
12351            exists,
12352            on_empty,
12353            on_error,
12354        }))
12355    }
12356
12357    /// Parses MSSQL's `OPENJSON WITH` column definition.
12358    ///
12359    /// ```sql
12360    /// colName type [ column_path ] [ AS JSON ]
12361    /// ```
12362    ///
12363    /// Reference: <https://learn.microsoft.com/en-us/sql/t-sql/functions/openjson-transact-sql?view=sql-server-ver16#syntax>
12364    pub fn parse_openjson_table_column_def(&mut self) -> Result<OpenJsonTableColumn, ParserError> {
12365        let name = self.parse_identifier()?;
12366        let r#type = self.parse_data_type()?;
12367        let path = if let Token::SingleQuotedString(path) = self.peek_token().token {
12368            self.next_token();
12369            Some(path)
12370        } else {
12371            None
12372        };
12373        let as_json = self.parse_keyword(Keyword::AS);
12374        if as_json {
12375            self.expect_keyword_is(Keyword::JSON)?;
12376        }
12377        Ok(OpenJsonTableColumn {
12378            name,
12379            r#type,
12380            path,
12381            as_json,
12382        })
12383    }
12384
12385    fn parse_json_table_column_error_handling(
12386        &mut self,
12387    ) -> Result<Option<JsonTableColumnErrorHandling>, ParserError> {
12388        let res = if self.parse_keyword(Keyword::NULL) {
12389            JsonTableColumnErrorHandling::Null
12390        } else if self.parse_keyword(Keyword::ERROR) {
12391            JsonTableColumnErrorHandling::Error
12392        } else if self.parse_keyword(Keyword::DEFAULT) {
12393            JsonTableColumnErrorHandling::Default(self.parse_value()?.value)
12394        } else {
12395            return Ok(None);
12396        };
12397        self.expect_keyword_is(Keyword::ON)?;
12398        Ok(Some(res))
12399    }
12400
12401    pub fn parse_derived_table_factor(
12402        &mut self,
12403        lateral: IsLateral,
12404    ) -> Result<TableFactor, ParserError> {
12405        let subquery = self.parse_query()?;
12406        self.expect_token(&Token::RParen)?;
12407        let alias = self.maybe_parse_table_alias()?;
12408        Ok(TableFactor::Derived {
12409            lateral: match lateral {
12410                Lateral => true,
12411                NotLateral => false,
12412            },
12413            subquery,
12414            alias,
12415        })
12416    }
12417
12418    fn parse_aliased_function_call(&mut self) -> Result<ExprWithAlias, ParserError> {
12419        let function_name = match self.next_token().token {
12420            Token::Word(w) => Ok(w.value),
12421            _ => self.expected("a function identifier", self.peek_token()),
12422        }?;
12423        let expr = self.parse_function(ObjectName::from(vec![Ident::new(function_name)]))?;
12424        let alias = if self.parse_keyword(Keyword::AS) {
12425            Some(self.parse_identifier()?)
12426        } else {
12427            None
12428        };
12429
12430        Ok(ExprWithAlias { expr, alias })
12431    }
12432    /// Parses an expression with an optional alias
12433    ///
12434    /// Examples:
12435    ///
12436    /// ```sql
12437    /// SUM(price) AS total_price
12438    /// ```
12439    /// ```sql
12440    /// SUM(price)
12441    /// ```
12442    ///
12443    /// Example
12444    /// ```
12445    /// # use sqlparser::parser::{Parser, ParserError};
12446    /// # use sqlparser::dialect::GenericDialect;
12447    /// # fn main() ->Result<(), ParserError> {
12448    /// let sql = r#"SUM("a") as "b""#;
12449    /// let mut parser = Parser::new(&GenericDialect).try_with_sql(sql)?;
12450    /// let expr_with_alias = parser.parse_expr_with_alias()?;
12451    /// assert_eq!(Some("b".to_string()), expr_with_alias.alias.map(|x|x.value));
12452    /// # Ok(())
12453    /// # }
12454    pub fn parse_expr_with_alias(&mut self) -> Result<ExprWithAlias, ParserError> {
12455        let expr = self.parse_expr()?;
12456        let alias = if self.parse_keyword(Keyword::AS) {
12457            Some(self.parse_identifier()?)
12458        } else {
12459            None
12460        };
12461
12462        Ok(ExprWithAlias { expr, alias })
12463    }
12464
12465    /// Parse DuckDB's statement-form `PIVOT`:
12466    /// `PIVOT <source> ON <expr>[, …] [USING <aggregate>[, …]] [GROUP BY <expr>[, …]]`.
12467    ///
12468    /// The `PIVOT` keyword is at the cursor. Distinct from
12469    /// [`Self::parse_pivot_table_factor`], the SQL-standard `FROM t PIVOT (…)` form:
12470    /// this variant is a query in its own right (top-level, CTAS body, subquery) and
12471    /// auto-detects the pivot values, so there is no explicit `IN (…)` list.
12472    pub fn parse_pivot_statement(&mut self) -> Result<PivotStatement, ParserError> {
12473        self.expect_keyword_is(Keyword::PIVOT)?;
12474        // `ON` is reserved for a table alias, so `parse_table_factor` stops before it.
12475        let source = Box::new(self.parse_table_factor()?);
12476        self.expect_keyword_is(Keyword::ON)?;
12477        let on = self.parse_comma_separated(Parser::parse_expr)?;
12478        let using = if self.parse_keyword(Keyword::USING) {
12479            self.parse_comma_separated(Parser::parse_expr_with_alias)?
12480        } else {
12481            vec![]
12482        };
12483        let group_by = if self.parse_keywords(&[Keyword::GROUP, Keyword::BY]) {
12484            self.parse_comma_separated(Parser::parse_expr)?
12485        } else {
12486            vec![]
12487        };
12488        Ok(PivotStatement {
12489            source,
12490            on,
12491            using,
12492            group_by,
12493        })
12494    }
12495
12496    /// Parse DuckDB's statement-form `UNPIVOT`:
12497    /// `UNPIVOT <source> ON <expr>[, …] [INTO NAME <ident> VALUE <ident>[, …]]`.
12498    ///
12499    /// The `UNPIVOT` keyword is at the cursor. The counterpart to
12500    /// [`Self::parse_pivot_statement`]; distinct from
12501    /// [`Self::parse_unpivot_table_factor`].
12502    pub fn parse_unpivot_statement(&mut self) -> Result<UnpivotStatement, ParserError> {
12503        self.expect_keyword_is(Keyword::UNPIVOT)?;
12504        let source = Box::new(self.parse_table_factor()?);
12505        self.expect_keyword_is(Keyword::ON)?;
12506        let on = self.parse_comma_separated(Parser::parse_expr)?;
12507        let into = if self.parse_keyword(Keyword::INTO) {
12508            self.expect_keyword_is(Keyword::NAME)?;
12509            let name = self.parse_identifier()?;
12510            self.expect_keyword_is(Keyword::VALUE)?;
12511            let value = self.parse_comma_separated(Parser::parse_identifier)?;
12512            Some(UnpivotInto { name, value })
12513        } else {
12514            None
12515        };
12516        Ok(UnpivotStatement { source, on, into })
12517    }
12518
12519    pub fn parse_pivot_table_factor(
12520        &mut self,
12521        table: TableFactor,
12522    ) -> Result<TableFactor, ParserError> {
12523        self.expect_token(&Token::LParen)?;
12524        let aggregate_functions = self.parse_comma_separated(Self::parse_aliased_function_call)?;
12525        self.expect_keyword_is(Keyword::FOR)?;
12526        let value_column = self.parse_period_separated(|p| p.parse_identifier())?;
12527        self.expect_keyword_is(Keyword::IN)?;
12528
12529        self.expect_token(&Token::LParen)?;
12530        let value_source = if self.parse_keyword(Keyword::ANY) {
12531            let order_by = if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
12532                self.parse_comma_separated(Parser::parse_order_by_expr)?
12533            } else {
12534                vec![]
12535            };
12536            PivotValueSource::Any(order_by)
12537        } else if self.peek_sub_query() {
12538            PivotValueSource::Subquery(self.parse_query()?)
12539        } else {
12540            PivotValueSource::List(self.parse_comma_separated(Self::parse_expr_with_alias)?)
12541        };
12542        self.expect_token(&Token::RParen)?;
12543
12544        let default_on_null =
12545            if self.parse_keywords(&[Keyword::DEFAULT, Keyword::ON, Keyword::NULL]) {
12546                self.expect_token(&Token::LParen)?;
12547                let expr = self.parse_expr()?;
12548                self.expect_token(&Token::RParen)?;
12549                Some(expr)
12550            } else {
12551                None
12552            };
12553
12554        self.expect_token(&Token::RParen)?;
12555        let alias = self.maybe_parse_table_alias()?;
12556        Ok(TableFactor::Pivot {
12557            table: Box::new(table),
12558            aggregate_functions,
12559            value_column,
12560            value_source,
12561            default_on_null,
12562            alias,
12563        })
12564    }
12565
12566    pub fn parse_unpivot_table_factor(
12567        &mut self,
12568        table: TableFactor,
12569    ) -> Result<TableFactor, ParserError> {
12570        self.expect_token(&Token::LParen)?;
12571        let value = self.parse_identifier()?;
12572        self.expect_keyword_is(Keyword::FOR)?;
12573        let name = self.parse_identifier()?;
12574        self.expect_keyword_is(Keyword::IN)?;
12575        let columns = self.parse_parenthesized_column_list(Mandatory, false)?;
12576        self.expect_token(&Token::RParen)?;
12577        let alias = self.maybe_parse_table_alias()?;
12578        Ok(TableFactor::Unpivot {
12579            table: Box::new(table),
12580            value,
12581            name,
12582            columns,
12583            alias,
12584        })
12585    }
12586
12587    pub fn parse_join_constraint(&mut self, natural: bool) -> Result<JoinConstraint, ParserError> {
12588        if natural {
12589            Ok(JoinConstraint::Natural)
12590        } else if self.parse_keyword(Keyword::ON) {
12591            let constraint = self.parse_expr()?;
12592            Ok(JoinConstraint::On(constraint))
12593        } else if self.parse_keyword(Keyword::USING) {
12594            let columns = self.parse_parenthesized_qualified_column_list(Mandatory, false)?;
12595            Ok(JoinConstraint::Using(columns))
12596        } else {
12597            Ok(JoinConstraint::None)
12598            //self.expected("ON, or USING after JOIN", self.peek_token())
12599        }
12600    }
12601
12602    /// Parse a GRANT statement.
12603    pub fn parse_grant(&mut self) -> Result<Statement, ParserError> {
12604        let (privileges, objects) = self.parse_grant_revoke_privileges_objects()?;
12605
12606        self.expect_keyword_is(Keyword::TO)?;
12607        let grantees = self.parse_grantees()?;
12608
12609        let with_grant_option =
12610            self.parse_keywords(&[Keyword::WITH, Keyword::GRANT, Keyword::OPTION]);
12611
12612        let granted_by = self
12613            .parse_keywords(&[Keyword::GRANTED, Keyword::BY])
12614            .then(|| self.parse_identifier().unwrap());
12615
12616        Ok(Statement::Grant {
12617            privileges,
12618            objects,
12619            grantees,
12620            with_grant_option,
12621            granted_by,
12622        })
12623    }
12624
12625    fn parse_grantees(&mut self) -> Result<Vec<Grantee>, ParserError> {
12626        let mut values = vec![];
12627        let mut grantee_type = GranteesType::None;
12628        loop {
12629            grantee_type = if self.parse_keyword(Keyword::ROLE) {
12630                GranteesType::Role
12631            } else if self.parse_keyword(Keyword::USER) {
12632                GranteesType::User
12633            } else if self.parse_keyword(Keyword::SHARE) {
12634                GranteesType::Share
12635            } else if self.parse_keyword(Keyword::GROUP) {
12636                GranteesType::Group
12637            } else if self.parse_keyword(Keyword::PUBLIC) {
12638                GranteesType::Public
12639            } else if self.parse_keywords(&[Keyword::DATABASE, Keyword::ROLE]) {
12640                GranteesType::DatabaseRole
12641            } else if self.parse_keywords(&[Keyword::APPLICATION, Keyword::ROLE]) {
12642                GranteesType::ApplicationRole
12643            } else if self.parse_keyword(Keyword::APPLICATION) {
12644                GranteesType::Application
12645            } else {
12646                grantee_type // keep from previous iteraton, if not specified
12647            };
12648
12649            let grantee = if grantee_type == GranteesType::Public {
12650                Grantee {
12651                    grantee_type: grantee_type.clone(),
12652                    name: None,
12653                }
12654            } else {
12655                let mut name = self.parse_grantee_name()?;
12656                if self.consume_token(&Token::Colon) {
12657                    // Redshift supports namespace prefix for external users and groups:
12658                    // <Namespace>:<GroupName> or <Namespace>:<UserName>
12659                    // https://docs.aws.amazon.com/redshift/latest/mgmt/redshift-iam-access-control-native-idp.html
12660                    let ident = self.parse_identifier()?;
12661                    if let GranteeName::ObjectName(namespace) = name {
12662                        name = GranteeName::ObjectName(ObjectName::from(vec![Ident::new(
12663                            format!("{}:{}", namespace, ident),
12664                        )]));
12665                    };
12666                }
12667                Grantee {
12668                    grantee_type: grantee_type.clone(),
12669                    name: Some(name),
12670                }
12671            };
12672
12673            values.push(grantee);
12674
12675            if !self.consume_token(&Token::Comma) {
12676                break;
12677            }
12678        }
12679
12680        Ok(values)
12681    }
12682
12683    pub fn parse_grant_revoke_privileges_objects(
12684        &mut self,
12685    ) -> Result<(Privileges, Option<GrantObjects>), ParserError> {
12686        let privileges = if self.parse_keyword(Keyword::ALL) {
12687            Privileges::All {
12688                with_privileges_keyword: self.parse_keyword(Keyword::PRIVILEGES),
12689            }
12690        } else {
12691            let actions = self.parse_actions_list()?;
12692            Privileges::Actions(actions)
12693        };
12694
12695        let objects = if self.parse_keyword(Keyword::ON) {
12696            if self.parse_keywords(&[Keyword::ALL, Keyword::TABLES, Keyword::IN, Keyword::SCHEMA]) {
12697                Some(GrantObjects::AllTablesInSchema {
12698                    schemas: self.parse_comma_separated(|p| p.parse_object_name(false))?,
12699                })
12700            } else if self.parse_keywords(&[
12701                Keyword::ALL,
12702                Keyword::SEQUENCES,
12703                Keyword::IN,
12704                Keyword::SCHEMA,
12705            ]) {
12706                Some(GrantObjects::AllSequencesInSchema {
12707                    schemas: self.parse_comma_separated(|p| p.parse_object_name(false))?,
12708                })
12709            } else {
12710                let object_type = self.parse_one_of_keywords(&[
12711                    Keyword::SEQUENCE,
12712                    Keyword::DATABASE,
12713                    Keyword::DATABASE,
12714                    Keyword::SCHEMA,
12715                    Keyword::TABLE,
12716                    Keyword::VIEW,
12717                    Keyword::WAREHOUSE,
12718                    Keyword::INTEGRATION,
12719                    Keyword::VIEW,
12720                    Keyword::WAREHOUSE,
12721                    Keyword::INTEGRATION,
12722                ]);
12723                let objects =
12724                    self.parse_comma_separated(|p| p.parse_object_name_with_wildcards(false, true));
12725                match object_type {
12726                    Some(Keyword::DATABASE) => Some(GrantObjects::Databases(objects?)),
12727                    Some(Keyword::SCHEMA) => Some(GrantObjects::Schemas(objects?)),
12728                    Some(Keyword::SEQUENCE) => Some(GrantObjects::Sequences(objects?)),
12729                    Some(Keyword::WAREHOUSE) => Some(GrantObjects::Warehouses(objects?)),
12730                    Some(Keyword::INTEGRATION) => Some(GrantObjects::Integrations(objects?)),
12731                    Some(Keyword::VIEW) => Some(GrantObjects::Views(objects?)),
12732                    Some(Keyword::TABLE) | None => Some(GrantObjects::Tables(objects?)),
12733                    _ => unreachable!(),
12734                }
12735            }
12736        } else {
12737            None
12738        };
12739
12740        Ok((privileges, objects))
12741    }
12742
12743    pub fn parse_grant_permission(&mut self) -> Result<Action, ParserError> {
12744        fn parse_columns(parser: &mut Parser) -> Result<Option<Vec<Ident>>, ParserError> {
12745            let columns = parser.parse_parenthesized_column_list(Optional, false)?;
12746            if columns.is_empty() {
12747                Ok(None)
12748            } else {
12749                Ok(Some(columns))
12750            }
12751        }
12752
12753        // Multi-word privileges
12754        if self.parse_keywords(&[Keyword::IMPORTED, Keyword::PRIVILEGES]) {
12755            Ok(Action::ImportedPrivileges)
12756        } else if self.parse_keywords(&[Keyword::ADD, Keyword::SEARCH, Keyword::OPTIMIZATION]) {
12757            Ok(Action::AddSearchOptimization)
12758        } else if self.parse_keywords(&[Keyword::ATTACH, Keyword::LISTING]) {
12759            Ok(Action::AttachListing)
12760        } else if self.parse_keywords(&[Keyword::ATTACH, Keyword::POLICY]) {
12761            Ok(Action::AttachPolicy)
12762        } else if self.parse_keywords(&[Keyword::BIND, Keyword::SERVICE, Keyword::ENDPOINT]) {
12763            Ok(Action::BindServiceEndpoint)
12764        } else if self.parse_keywords(&[Keyword::DATABASE, Keyword::ROLE]) {
12765            let role = self.parse_object_name(false)?;
12766            Ok(Action::DatabaseRole { role })
12767        } else if self.parse_keywords(&[Keyword::EVOLVE, Keyword::SCHEMA]) {
12768            Ok(Action::EvolveSchema)
12769        } else if self.parse_keywords(&[Keyword::IMPORT, Keyword::SHARE]) {
12770            Ok(Action::ImportShare)
12771        } else if self.parse_keywords(&[Keyword::MANAGE, Keyword::VERSIONS]) {
12772            Ok(Action::ManageVersions)
12773        } else if self.parse_keywords(&[Keyword::MANAGE, Keyword::RELEASES]) {
12774            Ok(Action::ManageReleases)
12775        } else if self.parse_keywords(&[Keyword::OVERRIDE, Keyword::SHARE, Keyword::RESTRICTIONS]) {
12776            Ok(Action::OverrideShareRestrictions)
12777        } else if self.parse_keywords(&[
12778            Keyword::PURCHASE,
12779            Keyword::DATA,
12780            Keyword::EXCHANGE,
12781            Keyword::LISTING,
12782        ]) {
12783            Ok(Action::PurchaseDataExchangeListing)
12784        } else if self.parse_keywords(&[Keyword::RESOLVE, Keyword::ALL]) {
12785            Ok(Action::ResolveAll)
12786        } else if self.parse_keywords(&[Keyword::READ, Keyword::SESSION]) {
12787            Ok(Action::ReadSession)
12788
12789        // Single-word privileges
12790        } else if self.parse_keyword(Keyword::APPLY) {
12791            let apply_type = self.parse_action_apply_type()?;
12792            Ok(Action::Apply { apply_type })
12793        } else if self.parse_keyword(Keyword::APPLYBUDGET) {
12794            Ok(Action::ApplyBudget)
12795        } else if self.parse_keyword(Keyword::AUDIT) {
12796            Ok(Action::Audit)
12797        } else if self.parse_keyword(Keyword::CONNECT) {
12798            Ok(Action::Connect)
12799        } else if self.parse_keyword(Keyword::CREATE) {
12800            let obj_type = self.maybe_parse_action_create_object_type();
12801            Ok(Action::Create { obj_type })
12802        } else if self.parse_keyword(Keyword::DELETE) {
12803            Ok(Action::Delete)
12804        } else if self.parse_keyword(Keyword::EXECUTE) {
12805            let obj_type = self.maybe_parse_action_execute_obj_type();
12806            Ok(Action::Execute { obj_type })
12807        } else if self.parse_keyword(Keyword::FAILOVER) {
12808            Ok(Action::Failover)
12809        } else if self.parse_keyword(Keyword::INSERT) {
12810            Ok(Action::Insert {
12811                columns: parse_columns(self)?,
12812            })
12813        } else if self.parse_keyword(Keyword::MANAGE) {
12814            let manage_type = self.parse_action_manage_type()?;
12815            Ok(Action::Manage { manage_type })
12816        } else if self.parse_keyword(Keyword::MODIFY) {
12817            let modify_type = self.parse_action_modify_type()?;
12818            Ok(Action::Modify { modify_type })
12819        } else if self.parse_keyword(Keyword::MONITOR) {
12820            let monitor_type = self.parse_action_monitor_type()?;
12821            Ok(Action::Monitor { monitor_type })
12822        } else if self.parse_keyword(Keyword::OPERATE) {
12823            Ok(Action::Operate)
12824        } else if self.parse_keyword(Keyword::REFERENCES) {
12825            Ok(Action::References {
12826                columns: parse_columns(self)?,
12827            })
12828        } else if self.parse_keyword(Keyword::READ) {
12829            Ok(Action::Read)
12830        } else if self.parse_keyword(Keyword::REPLICATE) {
12831            Ok(Action::Replicate)
12832        } else if self.parse_keyword(Keyword::ROLE) {
12833            let role = self.parse_identifier()?;
12834            Ok(Action::Role { role })
12835        } else if self.parse_keyword(Keyword::SELECT) {
12836            Ok(Action::Select {
12837                columns: parse_columns(self)?,
12838            })
12839        } else if self.parse_keyword(Keyword::TEMPORARY) {
12840            Ok(Action::Temporary)
12841        } else if self.parse_keyword(Keyword::TRIGGER) {
12842            Ok(Action::Trigger)
12843        } else if self.parse_keyword(Keyword::TRUNCATE) {
12844            Ok(Action::Truncate)
12845        } else if self.parse_keyword(Keyword::UPDATE) {
12846            Ok(Action::Update {
12847                columns: parse_columns(self)?,
12848            })
12849        } else if self.parse_keyword(Keyword::USAGE) {
12850            Ok(Action::Usage)
12851        } else if self.parse_keyword(Keyword::OWNERSHIP) {
12852            Ok(Action::Ownership)
12853        } else {
12854            self.expected("a privilege keyword", self.peek_token())?
12855        }
12856    }
12857
12858    fn maybe_parse_action_create_object_type(&mut self) -> Option<ActionCreateObjectType> {
12859        // Multi-word object types
12860        if self.parse_keywords(&[Keyword::APPLICATION, Keyword::PACKAGE]) {
12861            Some(ActionCreateObjectType::ApplicationPackage)
12862        } else if self.parse_keywords(&[Keyword::COMPUTE, Keyword::POOL]) {
12863            Some(ActionCreateObjectType::ComputePool)
12864        } else if self.parse_keywords(&[Keyword::DATA, Keyword::EXCHANGE, Keyword::LISTING]) {
12865            Some(ActionCreateObjectType::DataExchangeListing)
12866        } else if self.parse_keywords(&[Keyword::EXTERNAL, Keyword::VOLUME]) {
12867            Some(ActionCreateObjectType::ExternalVolume)
12868        } else if self.parse_keywords(&[Keyword::FAILOVER, Keyword::GROUP]) {
12869            Some(ActionCreateObjectType::FailoverGroup)
12870        } else if self.parse_keywords(&[Keyword::NETWORK, Keyword::POLICY]) {
12871            Some(ActionCreateObjectType::NetworkPolicy)
12872        } else if self.parse_keywords(&[Keyword::ORGANIZATION, Keyword::LISTING]) {
12873            Some(ActionCreateObjectType::OrganiationListing)
12874        } else if self.parse_keywords(&[Keyword::REPLICATION, Keyword::GROUP]) {
12875            Some(ActionCreateObjectType::ReplicationGroup)
12876        }
12877        // Single-word object types
12878        else if self.parse_keyword(Keyword::ACCOUNT) {
12879            Some(ActionCreateObjectType::Account)
12880        } else if self.parse_keyword(Keyword::APPLICATION) {
12881            Some(ActionCreateObjectType::Application)
12882        } else if self.parse_keyword(Keyword::DATABASE) {
12883            Some(ActionCreateObjectType::Database)
12884        } else if self.parse_keyword(Keyword::INTEGRATION) {
12885            Some(ActionCreateObjectType::Integration)
12886        } else if self.parse_keyword(Keyword::ROLE) {
12887            Some(ActionCreateObjectType::Role)
12888        } else if self.parse_keyword(Keyword::SHARE) {
12889            Some(ActionCreateObjectType::Share)
12890        } else if self.parse_keyword(Keyword::USER) {
12891            Some(ActionCreateObjectType::User)
12892        } else if self.parse_keyword(Keyword::WAREHOUSE) {
12893            Some(ActionCreateObjectType::Warehouse)
12894        } else {
12895            None
12896        }
12897    }
12898
12899    fn parse_action_apply_type(&mut self) -> Result<ActionApplyType, ParserError> {
12900        if self.parse_keywords(&[Keyword::AGGREGATION, Keyword::POLICY]) {
12901            Ok(ActionApplyType::AggregationPolicy)
12902        } else if self.parse_keywords(&[Keyword::AUTHENTICATION, Keyword::POLICY]) {
12903            Ok(ActionApplyType::AuthenticationPolicy)
12904        } else if self.parse_keywords(&[Keyword::JOIN, Keyword::POLICY]) {
12905            Ok(ActionApplyType::JoinPolicy)
12906        } else if self.parse_keywords(&[Keyword::MASKING, Keyword::POLICY]) {
12907            Ok(ActionApplyType::MaskingPolicy)
12908        } else if self.parse_keywords(&[Keyword::PACKAGES, Keyword::POLICY]) {
12909            Ok(ActionApplyType::PackagesPolicy)
12910        } else if self.parse_keywords(&[Keyword::PASSWORD, Keyword::POLICY]) {
12911            Ok(ActionApplyType::PasswordPolicy)
12912        } else if self.parse_keywords(&[Keyword::PROJECTION, Keyword::POLICY]) {
12913            Ok(ActionApplyType::ProjectionPolicy)
12914        } else if self.parse_keywords(&[Keyword::ROW, Keyword::ACCESS, Keyword::POLICY]) {
12915            Ok(ActionApplyType::RowAccessPolicy)
12916        } else if self.parse_keywords(&[Keyword::SESSION, Keyword::POLICY]) {
12917            Ok(ActionApplyType::SessionPolicy)
12918        } else if self.parse_keyword(Keyword::TAG) {
12919            Ok(ActionApplyType::Tag)
12920        } else {
12921            self.expected("GRANT APPLY type", self.peek_token())
12922        }
12923    }
12924
12925    fn maybe_parse_action_execute_obj_type(&mut self) -> Option<ActionExecuteObjectType> {
12926        if self.parse_keywords(&[Keyword::DATA, Keyword::METRIC, Keyword::FUNCTION]) {
12927            Some(ActionExecuteObjectType::DataMetricFunction)
12928        } else if self.parse_keywords(&[Keyword::MANAGED, Keyword::ALERT]) {
12929            Some(ActionExecuteObjectType::ManagedAlert)
12930        } else if self.parse_keywords(&[Keyword::MANAGED, Keyword::TASK]) {
12931            Some(ActionExecuteObjectType::ManagedTask)
12932        } else if self.parse_keyword(Keyword::ALERT) {
12933            Some(ActionExecuteObjectType::Alert)
12934        } else if self.parse_keyword(Keyword::TASK) {
12935            Some(ActionExecuteObjectType::Task)
12936        } else {
12937            None
12938        }
12939    }
12940
12941    fn parse_action_manage_type(&mut self) -> Result<ActionManageType, ParserError> {
12942        if self.parse_keywords(&[Keyword::ACCOUNT, Keyword::SUPPORT, Keyword::CASES]) {
12943            Ok(ActionManageType::AccountSupportCases)
12944        } else if self.parse_keywords(&[Keyword::EVENT, Keyword::SHARING]) {
12945            Ok(ActionManageType::EventSharing)
12946        } else if self.parse_keywords(&[Keyword::LISTING, Keyword::AUTO, Keyword::FULFILLMENT]) {
12947            Ok(ActionManageType::ListingAutoFulfillment)
12948        } else if self.parse_keywords(&[Keyword::ORGANIZATION, Keyword::SUPPORT, Keyword::CASES]) {
12949            Ok(ActionManageType::OrganizationSupportCases)
12950        } else if self.parse_keywords(&[Keyword::USER, Keyword::SUPPORT, Keyword::CASES]) {
12951            Ok(ActionManageType::UserSupportCases)
12952        } else if self.parse_keyword(Keyword::GRANTS) {
12953            Ok(ActionManageType::Grants)
12954        } else if self.parse_keyword(Keyword::WAREHOUSES) {
12955            Ok(ActionManageType::Warehouses)
12956        } else {
12957            self.expected("GRANT MANAGE type", self.peek_token())
12958        }
12959    }
12960
12961    fn parse_action_modify_type(&mut self) -> Result<ActionModifyType, ParserError> {
12962        if self.parse_keywords(&[Keyword::LOG, Keyword::LEVEL]) {
12963            Ok(ActionModifyType::LogLevel)
12964        } else if self.parse_keywords(&[Keyword::TRACE, Keyword::LEVEL]) {
12965            Ok(ActionModifyType::TraceLevel)
12966        } else if self.parse_keywords(&[Keyword::SESSION, Keyword::LOG, Keyword::LEVEL]) {
12967            Ok(ActionModifyType::SessionLogLevel)
12968        } else if self.parse_keywords(&[Keyword::SESSION, Keyword::TRACE, Keyword::LEVEL]) {
12969            Ok(ActionModifyType::SessionTraceLevel)
12970        } else {
12971            self.expected("GRANT MODIFY type", self.peek_token())
12972        }
12973    }
12974
12975    fn parse_action_monitor_type(&mut self) -> Result<ActionMonitorType, ParserError> {
12976        if self.parse_keyword(Keyword::EXECUTION) {
12977            Ok(ActionMonitorType::Execution)
12978        } else if self.parse_keyword(Keyword::SECURITY) {
12979            Ok(ActionMonitorType::Security)
12980        } else if self.parse_keyword(Keyword::USAGE) {
12981            Ok(ActionMonitorType::Usage)
12982        } else {
12983            self.expected("GRANT MONITOR type", self.peek_token())
12984        }
12985    }
12986
12987    pub fn parse_grantee_name(&mut self) -> Result<GranteeName, ParserError> {
12988        let mut name = self.parse_object_name(false)?;
12989        if self.dialect.supports_user_host_grantee()
12990            && name.0.len() == 1
12991            && name.0[0].as_ident().is_some()
12992            && self.consume_token(&Token::AtSign)
12993        {
12994            let user = name.0.pop().unwrap().as_ident().unwrap().clone();
12995            let host = self.parse_identifier()?;
12996            Ok(GranteeName::UserHost { user, host })
12997        } else {
12998            Ok(GranteeName::ObjectName(name))
12999        }
13000    }
13001
13002    /// Parse a REVOKE statement
13003    pub fn parse_revoke(&mut self) -> Result<Statement, ParserError> {
13004        let (privileges, objects) = self.parse_grant_revoke_privileges_objects()?;
13005
13006        self.expect_keyword_is(Keyword::FROM)?;
13007        let grantees = self.parse_grantees()?;
13008
13009        let granted_by = self
13010            .parse_keywords(&[Keyword::GRANTED, Keyword::BY])
13011            .then(|| self.parse_identifier().unwrap());
13012
13013        let cascade = self.parse_cascade_option();
13014
13015        Ok(Statement::Revoke {
13016            privileges,
13017            objects,
13018            grantees,
13019            granted_by,
13020            cascade,
13021        })
13022    }
13023
13024    /// Parse an REPLACE statement
13025    pub fn parse_replace(&mut self) -> Result<Statement, ParserError> {
13026        if !dialect_of!(self is MySqlDialect | GenericDialect) {
13027            return parser_err!(
13028                "Unsupported statement REPLACE",
13029                self.peek_token().span.start
13030            );
13031        }
13032
13033        let mut insert = self.parse_insert()?;
13034        if let Statement::Insert(Insert { replace_into, .. }) = &mut insert {
13035            *replace_into = true;
13036        }
13037
13038        Ok(insert)
13039    }
13040
13041    /// Parse an INSERT statement, returning a `Box`ed SetExpr
13042    ///
13043    /// This is used to reduce the size of the stack frames in debug builds
13044    fn parse_insert_setexpr_boxed(&mut self) -> Result<Box<SetExpr>, ParserError> {
13045        Ok(Box::new(SetExpr::Insert(self.parse_insert()?)))
13046    }
13047
13048    /// Parse an INSERT statement
13049    pub fn parse_insert(&mut self) -> Result<Statement, ParserError> {
13050        let or = self.parse_conflict_clause();
13051        let priority = if !dialect_of!(self is MySqlDialect | GenericDialect) {
13052            None
13053        } else if self.parse_keyword(Keyword::LOW_PRIORITY) {
13054            Some(MysqlInsertPriority::LowPriority)
13055        } else if self.parse_keyword(Keyword::DELAYED) {
13056            Some(MysqlInsertPriority::Delayed)
13057        } else if self.parse_keyword(Keyword::HIGH_PRIORITY) {
13058            Some(MysqlInsertPriority::HighPriority)
13059        } else {
13060            None
13061        };
13062
13063        let ignore = dialect_of!(self is MySqlDialect | GenericDialect)
13064            && self.parse_keyword(Keyword::IGNORE);
13065
13066        let replace_into = false;
13067
13068        let overwrite = self.parse_keyword(Keyword::OVERWRITE);
13069        let into = self.parse_keyword(Keyword::INTO);
13070
13071        let local = self.parse_keyword(Keyword::LOCAL);
13072
13073        if self.parse_keyword(Keyword::DIRECTORY) {
13074            let path = self.parse_literal_string()?;
13075            let file_format = if self.parse_keywords(&[Keyword::STORED, Keyword::AS]) {
13076                Some(self.parse_file_format()?)
13077            } else {
13078                None
13079            };
13080            let source = self.parse_query()?;
13081            Ok(Statement::Directory {
13082                local,
13083                path,
13084                overwrite,
13085                file_format,
13086                source,
13087            })
13088        } else {
13089            // Hive lets you put table here regardless
13090            let table = self.parse_keyword(Keyword::TABLE);
13091            let table_object = self.parse_table_object()?;
13092
13093            let table_alias =
13094                if dialect_of!(self is PostgreSqlDialect) && self.parse_keyword(Keyword::AS) {
13095                    Some(self.parse_identifier()?)
13096                } else {
13097                    None
13098                };
13099
13100            let is_mysql = dialect_of!(self is MySqlDialect);
13101
13102            let (columns, partitioned, after_columns, source, assignments) = if self
13103                .parse_keywords(&[Keyword::DEFAULT, Keyword::VALUES])
13104            {
13105                (vec![], None, vec![], None, vec![])
13106            } else {
13107                let (columns, partitioned, after_columns) = if !self.peek_subquery_start() {
13108                    let columns = self.parse_parenthesized_column_list(Optional, is_mysql)?;
13109
13110                    let partitioned = self.parse_insert_partition()?;
13111                    // Hive allows you to specify columns after partitions as well if you want.
13112                    let after_columns = if dialect_of!(self is HiveDialect) {
13113                        self.parse_parenthesized_column_list(Optional, false)?
13114                    } else {
13115                        vec![]
13116                    };
13117                    (columns, partitioned, after_columns)
13118                } else {
13119                    Default::default()
13120                };
13121
13122                let (source, assignments) = if self.peek_keyword(Keyword::FORMAT)
13123                    || self.peek_keyword(Keyword::SETTINGS)
13124                {
13125                    (None, vec![])
13126                } else if self.dialect.supports_insert_set() && self.parse_keyword(Keyword::SET) {
13127                    (None, self.parse_comma_separated(Parser::parse_assignment)?)
13128                } else {
13129                    (Some(self.parse_query()?), vec![])
13130                };
13131
13132                (columns, partitioned, after_columns, source, assignments)
13133            };
13134
13135            let (format_clause, settings) = if self.dialect.supports_insert_format() {
13136                // Settings always comes before `FORMAT` for ClickHouse:
13137                // <https://clickhouse.com/docs/en/sql-reference/statements/insert-into>
13138                let settings = self.parse_settings()?;
13139
13140                let format = if self.parse_keyword(Keyword::FORMAT) {
13141                    Some(self.parse_input_format_clause()?)
13142                } else {
13143                    None
13144                };
13145
13146                (format, settings)
13147            } else {
13148                Default::default()
13149            };
13150
13151            let insert_alias = if dialect_of!(self is MySqlDialect | GenericDialect)
13152                && self.parse_keyword(Keyword::AS)
13153            {
13154                let row_alias = self.parse_object_name(false)?;
13155                let col_aliases = Some(self.parse_parenthesized_column_list(Optional, false)?);
13156                Some(InsertAliases {
13157                    row_alias,
13158                    col_aliases,
13159                })
13160            } else {
13161                None
13162            };
13163
13164            let on = if self.parse_keyword(Keyword::ON) {
13165                if self.parse_keyword(Keyword::CONFLICT) {
13166                    let conflict_target =
13167                        if self.parse_keywords(&[Keyword::ON, Keyword::CONSTRAINT]) {
13168                            Some(ConflictTarget::OnConstraint(self.parse_object_name(false)?))
13169                        } else if self.peek_token() == Token::LParen {
13170                            Some(ConflictTarget::Columns(
13171                                self.parse_parenthesized_column_list(IsOptional::Mandatory, false)?,
13172                            ))
13173                        } else {
13174                            None
13175                        };
13176
13177                    self.expect_keyword_is(Keyword::DO)?;
13178                    let action = if self.parse_keyword(Keyword::NOTHING) {
13179                        OnConflictAction::DoNothing
13180                    } else {
13181                        self.expect_keyword_is(Keyword::UPDATE)?;
13182                        self.expect_keyword_is(Keyword::SET)?;
13183                        let assignments = self.parse_comma_separated(Parser::parse_assignment)?;
13184                        let selection = if self.parse_keyword(Keyword::WHERE) {
13185                            Some(self.parse_expr()?)
13186                        } else {
13187                            None
13188                        };
13189                        OnConflictAction::DoUpdate(DoUpdate {
13190                            assignments,
13191                            selection,
13192                        })
13193                    };
13194
13195                    Some(OnInsert::OnConflict(OnConflict {
13196                        conflict_target,
13197                        action,
13198                    }))
13199                } else {
13200                    self.expect_keyword_is(Keyword::DUPLICATE)?;
13201                    self.expect_keyword_is(Keyword::KEY)?;
13202                    self.expect_keyword_is(Keyword::UPDATE)?;
13203                    let l = self.parse_comma_separated(Parser::parse_assignment)?;
13204
13205                    Some(OnInsert::DuplicateKeyUpdate(l))
13206                }
13207            } else {
13208                None
13209            };
13210
13211            let returning = if self.parse_keyword(Keyword::RETURNING) {
13212                Some(self.parse_comma_separated(Parser::parse_select_item)?)
13213            } else {
13214                None
13215            };
13216
13217            Ok(Statement::Insert(Insert {
13218                or,
13219                table: table_object,
13220                table_alias,
13221                ignore,
13222                into,
13223                overwrite,
13224                partitioned,
13225                columns,
13226                after_columns,
13227                source,
13228                assignments,
13229                has_table_keyword: table,
13230                on,
13231                returning,
13232                replace_into,
13233                priority,
13234                insert_alias,
13235                settings,
13236                format_clause,
13237            }))
13238        }
13239    }
13240
13241    // Parses input format clause used for [ClickHouse].
13242    //
13243    // <https://clickhouse.com/docs/en/interfaces/formats>
13244    pub fn parse_input_format_clause(&mut self) -> Result<InputFormatClause, ParserError> {
13245        let ident = self.parse_identifier()?;
13246        let values = self
13247            .maybe_parse(|p| p.parse_comma_separated(|p| p.parse_expr()))?
13248            .unwrap_or_default();
13249
13250        Ok(InputFormatClause { ident, values })
13251    }
13252
13253    /// Returns true if the immediate tokens look like the
13254    /// beginning of a subquery. `(SELECT ...`
13255    fn peek_subquery_start(&mut self) -> bool {
13256        let [maybe_lparen, maybe_select] = self.peek_tokens();
13257        Token::LParen == maybe_lparen
13258            && matches!(maybe_select, Token::Word(w) if w.keyword == Keyword::SELECT)
13259    }
13260
13261    fn parse_conflict_clause(&mut self) -> Option<SqliteOnConflict> {
13262        if self.parse_keywords(&[Keyword::OR, Keyword::REPLACE]) {
13263            Some(SqliteOnConflict::Replace)
13264        } else if self.parse_keywords(&[Keyword::OR, Keyword::ROLLBACK]) {
13265            Some(SqliteOnConflict::Rollback)
13266        } else if self.parse_keywords(&[Keyword::OR, Keyword::ABORT]) {
13267            Some(SqliteOnConflict::Abort)
13268        } else if self.parse_keywords(&[Keyword::OR, Keyword::FAIL]) {
13269            Some(SqliteOnConflict::Fail)
13270        } else if self.parse_keywords(&[Keyword::OR, Keyword::IGNORE]) {
13271            Some(SqliteOnConflict::Ignore)
13272        } else if self.parse_keyword(Keyword::REPLACE) {
13273            Some(SqliteOnConflict::Replace)
13274        } else {
13275            None
13276        }
13277    }
13278
13279    pub fn parse_insert_partition(&mut self) -> Result<Option<Vec<Expr>>, ParserError> {
13280        if self.parse_keyword(Keyword::PARTITION) {
13281            self.expect_token(&Token::LParen)?;
13282            let partition_cols = Some(self.parse_comma_separated(Parser::parse_expr)?);
13283            self.expect_token(&Token::RParen)?;
13284            Ok(partition_cols)
13285        } else {
13286            Ok(None)
13287        }
13288    }
13289
13290    pub fn parse_load_data_table_format(
13291        &mut self,
13292    ) -> Result<Option<HiveLoadDataFormat>, ParserError> {
13293        if self.parse_keyword(Keyword::INPUTFORMAT) {
13294            let input_format = self.parse_expr()?;
13295            self.expect_keyword_is(Keyword::SERDE)?;
13296            let serde = self.parse_expr()?;
13297            Ok(Some(HiveLoadDataFormat {
13298                input_format,
13299                serde,
13300            }))
13301        } else {
13302            Ok(None)
13303        }
13304    }
13305
13306    /// Parse an UPDATE statement, returning a `Box`ed SetExpr
13307    ///
13308    /// This is used to reduce the size of the stack frames in debug builds
13309    fn parse_update_setexpr_boxed(&mut self) -> Result<Box<SetExpr>, ParserError> {
13310        Ok(Box::new(SetExpr::Update(self.parse_update()?)))
13311    }
13312
13313    pub fn parse_update(&mut self) -> Result<Statement, ParserError> {
13314        let or = self.parse_conflict_clause();
13315        let table = self.parse_table_and_joins()?;
13316        let from_before_set = if self.parse_keyword(Keyword::FROM) {
13317            Some(UpdateTableFromKind::BeforeSet(
13318                self.parse_table_with_joins()?,
13319            ))
13320        } else {
13321            None
13322        };
13323        self.expect_keyword(Keyword::SET)?;
13324        let assignments = self.parse_comma_separated(Parser::parse_assignment)?;
13325        let from = if from_before_set.is_none() && self.parse_keyword(Keyword::FROM) {
13326            Some(UpdateTableFromKind::AfterSet(
13327                self.parse_table_with_joins()?,
13328            ))
13329        } else {
13330            from_before_set
13331        };
13332        let selection = if self.parse_keyword(Keyword::WHERE) {
13333            Some(self.parse_expr()?)
13334        } else {
13335            None
13336        };
13337        let returning = if self.parse_keyword(Keyword::RETURNING) {
13338            Some(self.parse_comma_separated(Parser::parse_select_item)?)
13339        } else {
13340            None
13341        };
13342        Ok(Statement::Update {
13343            table,
13344            assignments,
13345            from,
13346            selection,
13347            returning,
13348            or,
13349        })
13350    }
13351
13352    /// Parse a `var = expr` assignment, used in an UPDATE statement
13353    pub fn parse_assignment(&mut self) -> Result<Assignment, ParserError> {
13354        let target = self.parse_assignment_target()?;
13355        self.expect_token(&Token::Eq)?;
13356        let value = self.parse_expr()?;
13357        Ok(Assignment { target, value })
13358    }
13359
13360    /// Parse the left-hand side of an assignment, used in an UPDATE statement
13361    pub fn parse_assignment_target(&mut self) -> Result<AssignmentTarget, ParserError> {
13362        if self.consume_token(&Token::LParen) {
13363            let columns = self.parse_comma_separated(|p| p.parse_object_name(false))?;
13364            self.expect_token(&Token::RParen)?;
13365            Ok(AssignmentTarget::Tuple(columns))
13366        } else {
13367            let column = self.parse_object_name(false)?;
13368            Ok(AssignmentTarget::ColumnName(column))
13369        }
13370    }
13371
13372    pub fn parse_function_args(&mut self) -> Result<FunctionArg, ParserError> {
13373        let arg = if self.dialect.supports_named_fn_args_with_expr_name() {
13374            self.maybe_parse(|p| {
13375                let name = p.parse_expr()?;
13376                let operator = p.parse_function_named_arg_operator()?;
13377                let arg = p.parse_wildcard_expr()?.into();
13378                Ok(FunctionArg::ExprNamed {
13379                    name,
13380                    arg,
13381                    operator,
13382                })
13383            })?
13384        } else {
13385            self.maybe_parse(|p| {
13386                let name = p.parse_identifier()?;
13387                let operator = p.parse_function_named_arg_operator()?;
13388                let arg = p.parse_wildcard_expr()?.into();
13389                Ok(FunctionArg::Named {
13390                    name,
13391                    arg,
13392                    operator,
13393                })
13394            })?
13395        };
13396        if let Some(arg) = arg {
13397            return Ok(arg);
13398        }
13399        Ok(FunctionArg::Unnamed(self.parse_wildcard_expr()?.into()))
13400    }
13401
13402    fn parse_function_named_arg_operator(&mut self) -> Result<FunctionArgOperator, ParserError> {
13403        if self.parse_keyword(Keyword::VALUE) {
13404            return Ok(FunctionArgOperator::Value);
13405        }
13406        let tok = self.next_token();
13407        match tok.token {
13408            Token::RArrow if self.dialect.supports_named_fn_args_with_rarrow_operator() => {
13409                Ok(FunctionArgOperator::RightArrow)
13410            }
13411            Token::Eq if self.dialect.supports_named_fn_args_with_eq_operator() => {
13412                Ok(FunctionArgOperator::Equals)
13413            }
13414            Token::Assignment
13415                if self
13416                    .dialect
13417                    .supports_named_fn_args_with_assignment_operator() =>
13418            {
13419                Ok(FunctionArgOperator::Assignment)
13420            }
13421            Token::Colon if self.dialect.supports_named_fn_args_with_colon_operator() => {
13422                Ok(FunctionArgOperator::Colon)
13423            }
13424            _ => {
13425                self.prev_token();
13426                self.expected("argument operator", tok)
13427            }
13428        }
13429    }
13430
13431    pub fn parse_optional_args(&mut self) -> Result<Vec<FunctionArg>, ParserError> {
13432        if self.consume_token(&Token::RParen) {
13433            Ok(vec![])
13434        } else {
13435            let args = self.parse_comma_separated(Parser::parse_function_args)?;
13436            self.expect_token(&Token::RParen)?;
13437            Ok(args)
13438        }
13439    }
13440
13441    fn parse_table_function_args(&mut self) -> Result<TableFunctionArgs, ParserError> {
13442        if self.consume_token(&Token::RParen) {
13443            return Ok(TableFunctionArgs {
13444                args: vec![],
13445                settings: None,
13446            });
13447        }
13448        let mut args = vec![];
13449        let settings = loop {
13450            if let Some(settings) = self.parse_settings()? {
13451                break Some(settings);
13452            }
13453            args.push(self.parse_function_args()?);
13454            if self.is_parse_comma_separated_end() {
13455                break None;
13456            }
13457        };
13458        self.expect_token(&Token::RParen)?;
13459        Ok(TableFunctionArgs { args, settings })
13460    }
13461
13462    /// Parses a potentially empty list of arguments to a window function
13463    /// (including the closing parenthesis).
13464    ///
13465    /// Examples:
13466    /// ```sql
13467    /// FIRST_VALUE(x ORDER BY 1,2,3);
13468    /// FIRST_VALUE(x IGNORE NULL);
13469    /// ```
13470    fn parse_function_argument_list(&mut self) -> Result<FunctionArgumentList, ParserError> {
13471        let mut clauses = vec![];
13472
13473        // For MSSQL empty argument list with json-null-clause case, e.g. `JSON_ARRAY(NULL ON NULL)`
13474        if let Some(null_clause) = self.parse_json_null_clause() {
13475            clauses.push(FunctionArgumentClause::JsonNullClause(null_clause));
13476        }
13477
13478        if self.consume_token(&Token::RParen) {
13479            return Ok(FunctionArgumentList {
13480                duplicate_treatment: None,
13481                args: vec![],
13482                clauses,
13483            });
13484        }
13485
13486        let duplicate_treatment = self.parse_duplicate_treatment()?;
13487        let args = self.parse_comma_separated(Parser::parse_function_args)?;
13488
13489        if self.dialect.supports_window_function_null_treatment_arg() {
13490            if let Some(null_treatment) = self.parse_null_treatment()? {
13491                clauses.push(FunctionArgumentClause::IgnoreOrRespectNulls(null_treatment));
13492            }
13493        }
13494
13495        if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
13496            clauses.push(FunctionArgumentClause::OrderBy(
13497                self.parse_comma_separated(Parser::parse_order_by_expr)?,
13498            ));
13499        }
13500
13501        if self.parse_keyword(Keyword::LIMIT) {
13502            clauses.push(FunctionArgumentClause::Limit(self.parse_expr()?));
13503        }
13504
13505        if dialect_of!(self is GenericDialect | BigQueryDialect)
13506            && self.parse_keyword(Keyword::HAVING)
13507        {
13508            let kind = match self.expect_one_of_keywords(&[Keyword::MIN, Keyword::MAX])? {
13509                Keyword::MIN => HavingBoundKind::Min,
13510                Keyword::MAX => HavingBoundKind::Max,
13511                _ => unreachable!(),
13512            };
13513            clauses.push(FunctionArgumentClause::Having(HavingBound(
13514                kind,
13515                self.parse_expr()?,
13516            )))
13517        }
13518
13519        if dialect_of!(self is GenericDialect | MySqlDialect)
13520            && self.parse_keyword(Keyword::SEPARATOR)
13521        {
13522            clauses.push(FunctionArgumentClause::Separator(self.parse_value()?.value));
13523        }
13524
13525        if let Some(on_overflow) = self.parse_listagg_on_overflow()? {
13526            clauses.push(FunctionArgumentClause::OnOverflow(on_overflow));
13527        }
13528
13529        if let Some(null_clause) = self.parse_json_null_clause() {
13530            clauses.push(FunctionArgumentClause::JsonNullClause(null_clause));
13531        }
13532
13533        self.expect_token(&Token::RParen)?;
13534        Ok(FunctionArgumentList {
13535            duplicate_treatment,
13536            args,
13537            clauses,
13538        })
13539    }
13540
13541    /// Parses MSSQL's json-null-clause
13542    fn parse_json_null_clause(&mut self) -> Option<JsonNullClause> {
13543        if self.parse_keywords(&[Keyword::ABSENT, Keyword::ON, Keyword::NULL]) {
13544            Some(JsonNullClause::AbsentOnNull)
13545        } else if self.parse_keywords(&[Keyword::NULL, Keyword::ON, Keyword::NULL]) {
13546            Some(JsonNullClause::NullOnNull)
13547        } else {
13548            None
13549        }
13550    }
13551
13552    fn parse_duplicate_treatment(&mut self) -> Result<Option<DuplicateTreatment>, ParserError> {
13553        let loc = self.peek_token().span.start;
13554        match (
13555            self.parse_keyword(Keyword::ALL),
13556            self.parse_keyword(Keyword::DISTINCT),
13557        ) {
13558            (true, false) => Ok(Some(DuplicateTreatment::All)),
13559            (false, true) => Ok(Some(DuplicateTreatment::Distinct)),
13560            (false, false) => Ok(None),
13561            (true, true) => parser_err!("Cannot specify both ALL and DISTINCT".to_string(), loc),
13562        }
13563    }
13564
13565    /// Parse a comma-delimited list of projections after SELECT
13566    pub fn parse_select_item(&mut self) -> Result<SelectItem, ParserError> {
13567        match self.parse_wildcard_expr()? {
13568            Expr::QualifiedWildcard(prefix, token) => Ok(SelectItem::QualifiedWildcard(
13569                SelectItemQualifiedWildcardKind::ObjectName(prefix),
13570                self.parse_wildcard_additional_options(token.0)?,
13571            )),
13572            Expr::Wildcard(token) => Ok(SelectItem::Wildcard(
13573                self.parse_wildcard_additional_options(token.0)?,
13574            )),
13575            Expr::Identifier(v) if v.value.to_lowercase() == "from" && v.quote_style.is_none() => {
13576                parser_err!(
13577                    format!("Expected an expression, found: {}", v),
13578                    self.peek_token().span.start
13579                )
13580            }
13581            Expr::BinaryOp {
13582                left,
13583                op: BinaryOperator::Eq,
13584                right,
13585            } if self.dialect.supports_eq_alias_assignment()
13586                && matches!(left.as_ref(), Expr::Identifier(_)) =>
13587            {
13588                let Expr::Identifier(alias) = *left else {
13589                    return parser_err!(
13590                        "BUG: expected identifier expression as alias",
13591                        self.peek_token().span.start
13592                    );
13593                };
13594                Ok(SelectItem::ExprWithAlias {
13595                    expr: *right,
13596                    alias,
13597                })
13598            }
13599            expr if self.dialect.supports_select_expr_star()
13600                && self.consume_tokens(&[Token::Period, Token::Mul]) =>
13601            {
13602                let wildcard_token = self.get_previous_token().clone();
13603                Ok(SelectItem::QualifiedWildcard(
13604                    SelectItemQualifiedWildcardKind::Expr(expr),
13605                    self.parse_wildcard_additional_options(wildcard_token)?,
13606                ))
13607            }
13608            expr => self
13609                .maybe_parse_select_item_alias()
13610                .map(|alias| match alias {
13611                    Some(alias) => SelectItem::ExprWithAlias { expr, alias },
13612                    None => SelectItem::UnnamedExpr(expr),
13613                }),
13614        }
13615    }
13616
13617    /// Parse an [`WildcardAdditionalOptions`] information for wildcard select items.
13618    ///
13619    /// If it is not possible to parse it, will return an option.
13620    pub fn parse_wildcard_additional_options(
13621        &mut self,
13622        wildcard_token: TokenWithSpan,
13623    ) -> Result<WildcardAdditionalOptions, ParserError> {
13624        let opt_ilike = if dialect_of!(self is GenericDialect | SnowflakeDialect) {
13625            self.parse_optional_select_item_ilike()?
13626        } else {
13627            None
13628        };
13629        let opt_exclude = if opt_ilike.is_none()
13630            && dialect_of!(self is GenericDialect | DuckDbDialect | SnowflakeDialect)
13631        {
13632            self.parse_optional_select_item_exclude()?
13633        } else {
13634            None
13635        };
13636        let opt_except = if self.dialect.supports_select_wildcard_except() {
13637            self.parse_optional_select_item_except()?
13638        } else {
13639            None
13640        };
13641        let opt_replace = if dialect_of!(self is GenericDialect | BigQueryDialect | ClickHouseDialect | DuckDbDialect | SnowflakeDialect)
13642        {
13643            self.parse_optional_select_item_replace()?
13644        } else {
13645            None
13646        };
13647        let opt_rename = if dialect_of!(self is GenericDialect | SnowflakeDialect) {
13648            self.parse_optional_select_item_rename()?
13649        } else {
13650            None
13651        };
13652
13653        Ok(WildcardAdditionalOptions {
13654            wildcard_token: wildcard_token.into(),
13655            opt_ilike,
13656            opt_exclude,
13657            opt_except,
13658            opt_rename,
13659            opt_replace,
13660        })
13661    }
13662
13663    /// Parse an [`Ilike`](IlikeSelectItem) information for wildcard select items.
13664    ///
13665    /// If it is not possible to parse it, will return an option.
13666    pub fn parse_optional_select_item_ilike(
13667        &mut self,
13668    ) -> Result<Option<IlikeSelectItem>, ParserError> {
13669        let opt_ilike = if self.parse_keyword(Keyword::ILIKE) {
13670            let next_token = self.next_token();
13671            let pattern = match next_token.token {
13672                Token::SingleQuotedString(s) => s,
13673                _ => return self.expected("ilike pattern", next_token),
13674            };
13675            Some(IlikeSelectItem { pattern })
13676        } else {
13677            None
13678        };
13679        Ok(opt_ilike)
13680    }
13681
13682    /// Parse an [`Exclude`](ExcludeSelectItem) information for wildcard select items.
13683    ///
13684    /// If it is not possible to parse it, will return an option.
13685    pub fn parse_optional_select_item_exclude(
13686        &mut self,
13687    ) -> Result<Option<ExcludeSelectItem>, ParserError> {
13688        let opt_exclude = if self.parse_keyword(Keyword::EXCLUDE) {
13689            if self.consume_token(&Token::LParen) {
13690                let columns = self.parse_comma_separated(|parser| parser.parse_identifier())?;
13691                self.expect_token(&Token::RParen)?;
13692                Some(ExcludeSelectItem::Multiple(columns))
13693            } else {
13694                let column = self.parse_identifier()?;
13695                Some(ExcludeSelectItem::Single(column))
13696            }
13697        } else {
13698            None
13699        };
13700
13701        Ok(opt_exclude)
13702    }
13703
13704    /// Parse an [`Except`](ExceptSelectItem) information for wildcard select items.
13705    ///
13706    /// If it is not possible to parse it, will return an option.
13707    pub fn parse_optional_select_item_except(
13708        &mut self,
13709    ) -> Result<Option<ExceptSelectItem>, ParserError> {
13710        let opt_except = if self.parse_keyword(Keyword::EXCEPT) {
13711            if self.peek_token().token == Token::LParen {
13712                let idents = self.parse_parenthesized_column_list(Mandatory, false)?;
13713                match &idents[..] {
13714                    [] => {
13715                        return self.expected(
13716                            "at least one column should be parsed by the expect clause",
13717                            self.peek_token(),
13718                        )?;
13719                    }
13720                    [first, idents @ ..] => Some(ExceptSelectItem {
13721                        first_element: first.clone(),
13722                        additional_elements: idents.to_vec(),
13723                    }),
13724                }
13725            } else {
13726                // Clickhouse allows EXCEPT column_name
13727                let ident = self.parse_identifier()?;
13728                Some(ExceptSelectItem {
13729                    first_element: ident,
13730                    additional_elements: vec![],
13731                })
13732            }
13733        } else {
13734            None
13735        };
13736
13737        Ok(opt_except)
13738    }
13739
13740    /// Parse a [`Rename`](RenameSelectItem) information for wildcard select items.
13741    pub fn parse_optional_select_item_rename(
13742        &mut self,
13743    ) -> Result<Option<RenameSelectItem>, ParserError> {
13744        let opt_rename = if self.parse_keyword(Keyword::RENAME) {
13745            if self.consume_token(&Token::LParen) {
13746                let idents =
13747                    self.parse_comma_separated(|parser| parser.parse_identifier_with_alias())?;
13748                self.expect_token(&Token::RParen)?;
13749                Some(RenameSelectItem::Multiple(idents))
13750            } else {
13751                let ident = self.parse_identifier_with_alias()?;
13752                Some(RenameSelectItem::Single(ident))
13753            }
13754        } else {
13755            None
13756        };
13757
13758        Ok(opt_rename)
13759    }
13760
13761    /// Parse a [`Replace`](ReplaceSelectItem) information for wildcard select items.
13762    pub fn parse_optional_select_item_replace(
13763        &mut self,
13764    ) -> Result<Option<ReplaceSelectItem>, ParserError> {
13765        let opt_replace = if self.parse_keyword(Keyword::REPLACE) {
13766            if self.consume_token(&Token::LParen) {
13767                let items = self.parse_comma_separated(|parser| {
13768                    Ok(Box::new(parser.parse_replace_elements()?))
13769                })?;
13770                self.expect_token(&Token::RParen)?;
13771                Some(ReplaceSelectItem { items })
13772            } else {
13773                let tok = self.next_token();
13774                return self.expected("( after REPLACE but", tok);
13775            }
13776        } else {
13777            None
13778        };
13779
13780        Ok(opt_replace)
13781    }
13782    pub fn parse_replace_elements(&mut self) -> Result<ReplaceSelectElement, ParserError> {
13783        let expr = self.parse_expr()?;
13784        let as_keyword = self.parse_keyword(Keyword::AS);
13785        let ident = self.parse_identifier()?;
13786        Ok(ReplaceSelectElement {
13787            expr,
13788            column_name: ident,
13789            as_keyword,
13790        })
13791    }
13792
13793    /// Parse ASC or DESC, returns an Option with true if ASC, false of DESC or `None` if none of
13794    /// them.
13795    pub fn parse_asc_desc(&mut self) -> Option<bool> {
13796        if self.parse_keyword(Keyword::ASC) {
13797            Some(true)
13798        } else if self.parse_keyword(Keyword::DESC) {
13799            Some(false)
13800        } else {
13801            None
13802        }
13803    }
13804
13805    /// Parse an expression, optionally followed by ASC or DESC (used in ORDER BY)
13806    pub fn parse_order_by_expr(&mut self) -> Result<OrderByExpr, ParserError> {
13807        let expr = self.parse_expr()?;
13808
13809        let options = self.parse_order_by_options()?;
13810
13811        let with_fill = if dialect_of!(self is ClickHouseDialect | GenericDialect)
13812            && self.parse_keywords(&[Keyword::WITH, Keyword::FILL])
13813        {
13814            Some(self.parse_with_fill()?)
13815        } else {
13816            None
13817        };
13818
13819        Ok(OrderByExpr {
13820            expr,
13821            options,
13822            with_fill,
13823        })
13824    }
13825
13826    fn parse_order_by_options(&mut self) -> Result<OrderByOptions, ParserError> {
13827        let asc = self.parse_asc_desc();
13828
13829        let nulls_first = if self.parse_keywords(&[Keyword::NULLS, Keyword::FIRST]) {
13830            Some(true)
13831        } else if self.parse_keywords(&[Keyword::NULLS, Keyword::LAST]) {
13832            Some(false)
13833        } else {
13834            None
13835        };
13836
13837        Ok(OrderByOptions { asc, nulls_first })
13838    }
13839
13840    // Parse a WITH FILL clause (ClickHouse dialect)
13841    // that follow the WITH FILL keywords in a ORDER BY clause
13842    pub fn parse_with_fill(&mut self) -> Result<WithFill, ParserError> {
13843        let from = if self.parse_keyword(Keyword::FROM) {
13844            Some(self.parse_expr()?)
13845        } else {
13846            None
13847        };
13848
13849        let to = if self.parse_keyword(Keyword::TO) {
13850            Some(self.parse_expr()?)
13851        } else {
13852            None
13853        };
13854
13855        let step = if self.parse_keyword(Keyword::STEP) {
13856            Some(self.parse_expr()?)
13857        } else {
13858            None
13859        };
13860
13861        Ok(WithFill { from, to, step })
13862    }
13863
13864    // Parse a set of comma separated INTERPOLATE expressions (ClickHouse dialect)
13865    // that follow the INTERPOLATE keyword in an ORDER BY clause with the WITH FILL modifier
13866    pub fn parse_interpolations(&mut self) -> Result<Option<Interpolate>, ParserError> {
13867        if !self.parse_keyword(Keyword::INTERPOLATE) {
13868            return Ok(None);
13869        }
13870
13871        if self.consume_token(&Token::LParen) {
13872            let interpolations =
13873                self.parse_comma_separated0(|p| p.parse_interpolation(), Token::RParen)?;
13874            self.expect_token(&Token::RParen)?;
13875            // INTERPOLATE () and INTERPOLATE ( ... ) variants
13876            return Ok(Some(Interpolate {
13877                exprs: Some(interpolations),
13878            }));
13879        }
13880
13881        // INTERPOLATE
13882        Ok(Some(Interpolate { exprs: None }))
13883    }
13884
13885    // Parse a INTERPOLATE expression (ClickHouse dialect)
13886    pub fn parse_interpolation(&mut self) -> Result<InterpolateExpr, ParserError> {
13887        let column = self.parse_identifier()?;
13888        let expr = if self.parse_keyword(Keyword::AS) {
13889            Some(self.parse_expr()?)
13890        } else {
13891            None
13892        };
13893        Ok(InterpolateExpr { column, expr })
13894    }
13895
13896    /// Parse a TOP clause, MSSQL equivalent of LIMIT,
13897    /// that follows after `SELECT [DISTINCT]`.
13898    pub fn parse_top(&mut self) -> Result<Top, ParserError> {
13899        let quantity = if self.consume_token(&Token::LParen) {
13900            let quantity = self.parse_expr()?;
13901            self.expect_token(&Token::RParen)?;
13902            Some(TopQuantity::Expr(quantity))
13903        } else {
13904            let next_token = self.next_token();
13905            let quantity = match next_token.token {
13906                Token::Number(s, _) => Self::parse::<u64>(s, next_token.span.start)?,
13907                _ => self.expected("literal int", next_token)?,
13908            };
13909            Some(TopQuantity::Constant(quantity))
13910        };
13911
13912        let percent = self.parse_keyword(Keyword::PERCENT);
13913
13914        let with_ties = self.parse_keywords(&[Keyword::WITH, Keyword::TIES]);
13915
13916        Ok(Top {
13917            with_ties,
13918            percent,
13919            quantity,
13920        })
13921    }
13922
13923    /// Parse a LIMIT clause
13924    pub fn parse_limit(&mut self) -> Result<Option<Expr>, ParserError> {
13925        if self.parse_keyword(Keyword::ALL) {
13926            Ok(None)
13927        } else {
13928            Ok(Some(self.parse_expr()?))
13929        }
13930    }
13931
13932    /// Parse an OFFSET clause
13933    pub fn parse_offset(&mut self) -> Result<Offset, ParserError> {
13934        let value = self.parse_expr()?;
13935        let rows = if self.parse_keyword(Keyword::ROW) {
13936            OffsetRows::Row
13937        } else if self.parse_keyword(Keyword::ROWS) {
13938            OffsetRows::Rows
13939        } else {
13940            OffsetRows::None
13941        };
13942        Ok(Offset { value, rows })
13943    }
13944
13945    /// Parse a FETCH clause
13946    pub fn parse_fetch(&mut self) -> Result<Fetch, ParserError> {
13947        self.expect_one_of_keywords(&[Keyword::FIRST, Keyword::NEXT])?;
13948        let (quantity, percent) = if self
13949            .parse_one_of_keywords(&[Keyword::ROW, Keyword::ROWS])
13950            .is_some()
13951        {
13952            (None, false)
13953        } else {
13954            let quantity = Expr::Value(self.parse_value()?);
13955            let percent = self.parse_keyword(Keyword::PERCENT);
13956            self.expect_one_of_keywords(&[Keyword::ROW, Keyword::ROWS])?;
13957            (Some(quantity), percent)
13958        };
13959        let with_ties = if self.parse_keyword(Keyword::ONLY) {
13960            false
13961        } else if self.parse_keywords(&[Keyword::WITH, Keyword::TIES]) {
13962            true
13963        } else {
13964            return self.expected("one of ONLY or WITH TIES", self.peek_token());
13965        };
13966        Ok(Fetch {
13967            with_ties,
13968            percent,
13969            quantity,
13970        })
13971    }
13972
13973    /// Parse a FOR UPDATE/FOR SHARE clause
13974    pub fn parse_lock(&mut self) -> Result<LockClause, ParserError> {
13975        let lock_type = match self.expect_one_of_keywords(&[Keyword::UPDATE, Keyword::SHARE])? {
13976            Keyword::UPDATE => LockType::Update,
13977            Keyword::SHARE => LockType::Share,
13978            _ => unreachable!(),
13979        };
13980        let of = if self.parse_keyword(Keyword::OF) {
13981            Some(self.parse_object_name(false)?)
13982        } else {
13983            None
13984        };
13985        let nonblock = if self.parse_keyword(Keyword::NOWAIT) {
13986            Some(NonBlock::Nowait)
13987        } else if self.parse_keywords(&[Keyword::SKIP, Keyword::LOCKED]) {
13988            Some(NonBlock::SkipLocked)
13989        } else {
13990            None
13991        };
13992        Ok(LockClause {
13993            lock_type,
13994            of,
13995            nonblock,
13996        })
13997    }
13998
13999    pub fn parse_values(&mut self, allow_empty: bool) -> Result<Values, ParserError> {
14000        let mut explicit_row = false;
14001
14002        let rows = self.parse_comma_separated(|parser| {
14003            if parser.parse_keyword(Keyword::ROW) {
14004                explicit_row = true;
14005            }
14006
14007            parser.expect_token(&Token::LParen)?;
14008            if allow_empty && parser.peek_token().token == Token::RParen {
14009                parser.next_token();
14010                Ok(vec![])
14011            } else {
14012                let exprs = parser.parse_comma_separated(Parser::parse_expr)?;
14013                parser.expect_token(&Token::RParen)?;
14014                Ok(exprs)
14015            }
14016        })?;
14017        Ok(Values { explicit_row, rows })
14018    }
14019
14020    pub fn parse_start_transaction(&mut self) -> Result<Statement, ParserError> {
14021        self.expect_keyword_is(Keyword::TRANSACTION)?;
14022        Ok(Statement::StartTransaction {
14023            modes: self.parse_transaction_modes()?,
14024            begin: false,
14025            transaction: Some(BeginTransactionKind::Transaction),
14026            modifier: None,
14027            statements: vec![],
14028            exception_statements: None,
14029            has_end_keyword: false,
14030        })
14031    }
14032
14033    pub fn parse_begin(&mut self) -> Result<Statement, ParserError> {
14034        let modifier = if !self.dialect.supports_start_transaction_modifier() {
14035            None
14036        } else if self.parse_keyword(Keyword::DEFERRED) {
14037            Some(TransactionModifier::Deferred)
14038        } else if self.parse_keyword(Keyword::IMMEDIATE) {
14039            Some(TransactionModifier::Immediate)
14040        } else if self.parse_keyword(Keyword::EXCLUSIVE) {
14041            Some(TransactionModifier::Exclusive)
14042        } else if self.parse_keyword(Keyword::TRY) {
14043            Some(TransactionModifier::Try)
14044        } else if self.parse_keyword(Keyword::CATCH) {
14045            Some(TransactionModifier::Catch)
14046        } else {
14047            None
14048        };
14049        let transaction = match self.parse_one_of_keywords(&[Keyword::TRANSACTION, Keyword::WORK]) {
14050            Some(Keyword::TRANSACTION) => Some(BeginTransactionKind::Transaction),
14051            Some(Keyword::WORK) => Some(BeginTransactionKind::Work),
14052            _ => None,
14053        };
14054        Ok(Statement::StartTransaction {
14055            modes: self.parse_transaction_modes()?,
14056            begin: true,
14057            transaction,
14058            modifier,
14059            statements: vec![],
14060            exception_statements: None,
14061            has_end_keyword: false,
14062        })
14063    }
14064
14065    pub fn parse_end(&mut self) -> Result<Statement, ParserError> {
14066        let modifier = if !self.dialect.supports_end_transaction_modifier() {
14067            None
14068        } else if self.parse_keyword(Keyword::TRY) {
14069            Some(TransactionModifier::Try)
14070        } else if self.parse_keyword(Keyword::CATCH) {
14071            Some(TransactionModifier::Catch)
14072        } else {
14073            None
14074        };
14075        Ok(Statement::Commit {
14076            chain: self.parse_commit_rollback_chain()?,
14077            end: true,
14078            modifier,
14079        })
14080    }
14081
14082    pub fn parse_transaction_modes(&mut self) -> Result<Vec<TransactionMode>, ParserError> {
14083        let mut modes = vec![];
14084        let mut required = false;
14085        loop {
14086            let mode = if self.parse_keywords(&[Keyword::ISOLATION, Keyword::LEVEL]) {
14087                let iso_level = if self.parse_keywords(&[Keyword::READ, Keyword::UNCOMMITTED]) {
14088                    TransactionIsolationLevel::ReadUncommitted
14089                } else if self.parse_keywords(&[Keyword::READ, Keyword::COMMITTED]) {
14090                    TransactionIsolationLevel::ReadCommitted
14091                } else if self.parse_keywords(&[Keyword::REPEATABLE, Keyword::READ]) {
14092                    TransactionIsolationLevel::RepeatableRead
14093                } else if self.parse_keyword(Keyword::SERIALIZABLE) {
14094                    TransactionIsolationLevel::Serializable
14095                } else if self.parse_keyword(Keyword::SNAPSHOT) {
14096                    TransactionIsolationLevel::Snapshot
14097                } else {
14098                    self.expected("isolation level", self.peek_token())?
14099                };
14100                TransactionMode::IsolationLevel(iso_level)
14101            } else if self.parse_keywords(&[Keyword::READ, Keyword::ONLY]) {
14102                TransactionMode::AccessMode(TransactionAccessMode::ReadOnly)
14103            } else if self.parse_keywords(&[Keyword::READ, Keyword::WRITE]) {
14104                TransactionMode::AccessMode(TransactionAccessMode::ReadWrite)
14105            } else if required {
14106                self.expected("transaction mode", self.peek_token())?
14107            } else {
14108                break;
14109            };
14110            modes.push(mode);
14111            // ANSI requires a comma after each transaction mode, but
14112            // PostgreSQL, for historical reasons, does not. We follow
14113            // PostgreSQL in making the comma optional, since that is strictly
14114            // more general.
14115            required = self.consume_token(&Token::Comma);
14116        }
14117        Ok(modes)
14118    }
14119
14120    pub fn parse_commit(&mut self) -> Result<Statement, ParserError> {
14121        Ok(Statement::Commit {
14122            chain: self.parse_commit_rollback_chain()?,
14123            end: false,
14124            modifier: None,
14125        })
14126    }
14127
14128    pub fn parse_rollback(&mut self) -> Result<Statement, ParserError> {
14129        let chain = self.parse_commit_rollback_chain()?;
14130        let savepoint = self.parse_rollback_savepoint()?;
14131
14132        Ok(Statement::Rollback { chain, savepoint })
14133    }
14134
14135    pub fn parse_commit_rollback_chain(&mut self) -> Result<bool, ParserError> {
14136        let _ = self.parse_one_of_keywords(&[Keyword::TRANSACTION, Keyword::WORK]);
14137        if self.parse_keyword(Keyword::AND) {
14138            let chain = !self.parse_keyword(Keyword::NO);
14139            self.expect_keyword_is(Keyword::CHAIN)?;
14140            Ok(chain)
14141        } else {
14142            Ok(false)
14143        }
14144    }
14145
14146    pub fn parse_rollback_savepoint(&mut self) -> Result<Option<Ident>, ParserError> {
14147        if self.parse_keyword(Keyword::TO) {
14148            let _ = self.parse_keyword(Keyword::SAVEPOINT);
14149            let savepoint = self.parse_identifier()?;
14150
14151            Ok(Some(savepoint))
14152        } else {
14153            Ok(None)
14154        }
14155    }
14156
14157    /// Parse a 'RAISERROR' statement
14158    pub fn parse_raiserror(&mut self) -> Result<Statement, ParserError> {
14159        self.expect_token(&Token::LParen)?;
14160        let message = Box::new(self.parse_expr()?);
14161        self.expect_token(&Token::Comma)?;
14162        let severity = Box::new(self.parse_expr()?);
14163        self.expect_token(&Token::Comma)?;
14164        let state = Box::new(self.parse_expr()?);
14165        let arguments = if self.consume_token(&Token::Comma) {
14166            self.parse_comma_separated(Parser::parse_expr)?
14167        } else {
14168            vec![]
14169        };
14170        self.expect_token(&Token::RParen)?;
14171        let options = if self.parse_keyword(Keyword::WITH) {
14172            self.parse_comma_separated(Parser::parse_raiserror_option)?
14173        } else {
14174            vec![]
14175        };
14176        Ok(Statement::RaisError {
14177            message,
14178            severity,
14179            state,
14180            arguments,
14181            options,
14182        })
14183    }
14184
14185    pub fn parse_raiserror_option(&mut self) -> Result<RaisErrorOption, ParserError> {
14186        match self.expect_one_of_keywords(&[Keyword::LOG, Keyword::NOWAIT, Keyword::SETERROR])? {
14187            Keyword::LOG => Ok(RaisErrorOption::Log),
14188            Keyword::NOWAIT => Ok(RaisErrorOption::NoWait),
14189            Keyword::SETERROR => Ok(RaisErrorOption::SetError),
14190            _ => self.expected(
14191                "LOG, NOWAIT OR SETERROR raiserror option",
14192                self.peek_token(),
14193            ),
14194        }
14195    }
14196
14197    pub fn parse_deallocate(&mut self) -> Result<Statement, ParserError> {
14198        let prepare = self.parse_keyword(Keyword::PREPARE);
14199        let name = self.parse_identifier()?;
14200        Ok(Statement::Deallocate { name, prepare })
14201    }
14202
14203    pub fn parse_execute(&mut self) -> Result<Statement, ParserError> {
14204        let name = if self.dialect.supports_execute_immediate()
14205            && self.parse_keyword(Keyword::IMMEDIATE)
14206        {
14207            None
14208        } else {
14209            let name = self.parse_object_name(false)?;
14210            Some(name)
14211        };
14212
14213        let has_parentheses = self.consume_token(&Token::LParen);
14214
14215        let end_token = match (has_parentheses, self.peek_token().token) {
14216            (true, _) => Token::RParen,
14217            (false, Token::EOF) => Token::EOF,
14218            (false, Token::Word(w)) if w.keyword == Keyword::USING => Token::Word(w),
14219            (false, _) => Token::SemiColon,
14220        };
14221
14222        let parameters = self.parse_comma_separated0(Parser::parse_expr, end_token)?;
14223
14224        if has_parentheses {
14225            self.expect_token(&Token::RParen)?;
14226        }
14227
14228        let into = if self.parse_keyword(Keyword::INTO) {
14229            self.parse_comma_separated(Self::parse_identifier)?
14230        } else {
14231            vec![]
14232        };
14233
14234        let using = if self.parse_keyword(Keyword::USING) {
14235            self.parse_comma_separated(Self::parse_expr_with_alias)?
14236        } else {
14237            vec![]
14238        };
14239
14240        Ok(Statement::Execute {
14241            immediate: name.is_none(),
14242            name,
14243            parameters,
14244            has_parentheses,
14245            into,
14246            using,
14247        })
14248    }
14249
14250    pub fn parse_prepare(&mut self) -> Result<Statement, ParserError> {
14251        let name = self.parse_identifier()?;
14252
14253        let mut data_types = vec![];
14254        if self.consume_token(&Token::LParen) {
14255            data_types = self.parse_comma_separated(Parser::parse_data_type)?;
14256            self.expect_token(&Token::RParen)?;
14257        }
14258
14259        self.expect_keyword_is(Keyword::AS)?;
14260        let statement = Box::new(self.parse_statement()?);
14261        Ok(Statement::Prepare {
14262            name,
14263            data_types,
14264            statement,
14265        })
14266    }
14267
14268    pub fn parse_unload(&mut self) -> Result<Statement, ParserError> {
14269        self.expect_token(&Token::LParen)?;
14270        let query = self.parse_query()?;
14271        self.expect_token(&Token::RParen)?;
14272
14273        self.expect_keyword_is(Keyword::TO)?;
14274        let to = self.parse_identifier()?;
14275
14276        let with_options = self.parse_options(Keyword::WITH)?;
14277
14278        Ok(Statement::Unload {
14279            query,
14280            to,
14281            with: with_options,
14282        })
14283    }
14284
14285    pub fn parse_merge_clauses(&mut self) -> Result<Vec<MergeClause>, ParserError> {
14286        let mut clauses = vec![];
14287        loop {
14288            if self.peek_token() == Token::EOF || self.peek_token() == Token::SemiColon {
14289                break;
14290            }
14291            self.expect_keyword_is(Keyword::WHEN)?;
14292
14293            let mut clause_kind = MergeClauseKind::Matched;
14294            if self.parse_keyword(Keyword::NOT) {
14295                clause_kind = MergeClauseKind::NotMatched;
14296            }
14297            self.expect_keyword_is(Keyword::MATCHED)?;
14298
14299            if matches!(clause_kind, MergeClauseKind::NotMatched)
14300                && self.parse_keywords(&[Keyword::BY, Keyword::SOURCE])
14301            {
14302                clause_kind = MergeClauseKind::NotMatchedBySource;
14303            } else if matches!(clause_kind, MergeClauseKind::NotMatched)
14304                && self.parse_keywords(&[Keyword::BY, Keyword::TARGET])
14305            {
14306                clause_kind = MergeClauseKind::NotMatchedByTarget;
14307            }
14308
14309            let predicate = if self.parse_keyword(Keyword::AND) {
14310                Some(self.parse_expr()?)
14311            } else {
14312                None
14313            };
14314
14315            self.expect_keyword_is(Keyword::THEN)?;
14316
14317            let merge_clause = match self.parse_one_of_keywords(&[
14318                Keyword::UPDATE,
14319                Keyword::INSERT,
14320                Keyword::DELETE,
14321            ]) {
14322                Some(Keyword::UPDATE) => {
14323                    if matches!(
14324                        clause_kind,
14325                        MergeClauseKind::NotMatched | MergeClauseKind::NotMatchedByTarget
14326                    ) {
14327                        return Err(ParserError::ParserError(format!(
14328                            "UPDATE is not allowed in a {clause_kind} merge clause"
14329                        )));
14330                    }
14331                    self.expect_keyword_is(Keyword::SET)?;
14332                    MergeAction::Update {
14333                        assignments: self.parse_comma_separated(Parser::parse_assignment)?,
14334                    }
14335                }
14336                Some(Keyword::DELETE) => {
14337                    if matches!(
14338                        clause_kind,
14339                        MergeClauseKind::NotMatched | MergeClauseKind::NotMatchedByTarget
14340                    ) {
14341                        return Err(ParserError::ParserError(format!(
14342                            "DELETE is not allowed in a {clause_kind} merge clause"
14343                        )));
14344                    }
14345                    MergeAction::Delete
14346                }
14347                Some(Keyword::INSERT) => {
14348                    if !matches!(
14349                        clause_kind,
14350                        MergeClauseKind::NotMatched | MergeClauseKind::NotMatchedByTarget
14351                    ) {
14352                        return Err(ParserError::ParserError(format!(
14353                            "INSERT is not allowed in a {clause_kind} merge clause"
14354                        )));
14355                    }
14356                    let is_mysql = dialect_of!(self is MySqlDialect);
14357
14358                    let columns = self.parse_parenthesized_column_list(Optional, is_mysql)?;
14359                    let kind = if dialect_of!(self is BigQueryDialect | GenericDialect)
14360                        && self.parse_keyword(Keyword::ROW)
14361                    {
14362                        MergeInsertKind::Row
14363                    } else {
14364                        self.expect_keyword_is(Keyword::VALUES)?;
14365                        let values = self.parse_values(is_mysql)?;
14366                        MergeInsertKind::Values(values)
14367                    };
14368                    MergeAction::Insert(MergeInsertExpr { columns, kind })
14369                }
14370                _ => {
14371                    return Err(ParserError::ParserError(
14372                        "expected UPDATE, DELETE or INSERT in merge clause".to_string(),
14373                    ));
14374                }
14375            };
14376            clauses.push(MergeClause {
14377                clause_kind,
14378                predicate,
14379                action: merge_clause,
14380            });
14381        }
14382        Ok(clauses)
14383    }
14384
14385    pub fn parse_merge(&mut self) -> Result<Statement, ParserError> {
14386        let into = self.parse_keyword(Keyword::INTO);
14387
14388        let table = self.parse_table_factor()?;
14389
14390        self.expect_keyword_is(Keyword::USING)?;
14391        let source = self.parse_table_factor()?;
14392        self.expect_keyword_is(Keyword::ON)?;
14393        let on = self.parse_expr()?;
14394        let clauses = self.parse_merge_clauses()?;
14395
14396        Ok(Statement::Merge {
14397            into,
14398            table,
14399            source,
14400            on: Box::new(on),
14401            clauses,
14402        })
14403    }
14404
14405    fn parse_pragma_value(&mut self) -> Result<Value, ParserError> {
14406        match self.parse_value()?.value {
14407            v @ Value::SingleQuotedString(_) => Ok(v),
14408            v @ Value::DoubleQuotedString(_) => Ok(v),
14409            v @ Value::Number(_, _) => Ok(v),
14410            v @ Value::Placeholder(_) => Ok(v),
14411            _ => {
14412                self.prev_token();
14413                self.expected("number or string or ? placeholder", self.peek_token())
14414            }
14415        }
14416    }
14417
14418    // PRAGMA [schema-name '.'] pragma-name [('=' pragma-value) | '(' pragma-value ')']
14419    // DuckDB additionally accepts a function-call argument list inside the
14420    // parens: several positional values and/or `name = value` named ones,
14421    // e.g. `PRAGMA create_fts_index('docs', 'order_id', 'body', overwrite = 1)`.
14422    pub fn parse_pragma(&mut self) -> Result<Statement, ParserError> {
14423        let name = self.parse_object_name(false)?;
14424        if self.consume_token(&Token::LParen) {
14425            // The classic single-value form first — `maybe_parse` backtracks
14426            // on any error, so a second argument (which trips the `RParen`
14427            // check below since a comma sits there instead) or a named one
14428            // (which `parse_pragma_value` doesn't accept at all) both fall
14429            // through untouched, and every PRAGMA that already parsed this
14430            // way keeps the exact same `value`/`is_eq` fields it always had.
14431            if let Some(value) = self.maybe_parse(|parser| {
14432                let value = parser.parse_pragma_value()?;
14433                parser.expect_token(&Token::RParen)?;
14434                Ok(value)
14435            })? {
14436                return Ok(Statement::Pragma {
14437                    name,
14438                    value: Some(value),
14439                    is_eq: false,
14440                    args: vec![],
14441                });
14442            }
14443
14444            if dialect_of!(self is DuckDbDialect | GenericDialect) {
14445                let args = self.parse_comma_separated(Parser::parse_function_args)?;
14446                self.expect_token(&Token::RParen)?;
14447                return Ok(Statement::Pragma {
14448                    name,
14449                    value: None,
14450                    is_eq: false,
14451                    args,
14452                });
14453            }
14454
14455            self.expected("pragma value", self.peek_token())
14456        } else if self.consume_token(&Token::Eq) {
14457            Ok(Statement::Pragma {
14458                name,
14459                value: Some(self.parse_pragma_value()?),
14460                is_eq: true,
14461                args: vec![],
14462            })
14463        } else {
14464            Ok(Statement::Pragma {
14465                name,
14466                value: None,
14467                is_eq: false,
14468                args: vec![],
14469            })
14470        }
14471    }
14472
14473    /// `[FORCE] INSTALL extension_name [FROM repository]`
14474    pub fn parse_install(&mut self, force: bool) -> Result<Statement, ParserError> {
14475        let extension_name = self.parse_identifier()?;
14476        let repository = if self.parse_keyword(Keyword::FROM) {
14477            Some(self.parse_install_repository()?)
14478        } else {
14479            None
14480        };
14481
14482        Ok(Statement::Install {
14483            extension_name,
14484            force,
14485            repository,
14486        })
14487    }
14488
14489    /// The `<repository>` in `INSTALL … FROM <repository>`: a bare
14490    /// identifier (`core`, `community`, a registered custom name), or a
14491    /// quoted URL. A leading quote is the only signal that distinguishes
14492    /// them, so peek rather than try one and backtrack.
14493    fn parse_install_repository(&mut self) -> Result<InstallRepository, ParserError> {
14494        match self.peek_token().token {
14495            Token::SingleQuotedString(_) | Token::DoubleQuotedString(_) => {
14496                Ok(InstallRepository::Url(self.parse_literal_string()?))
14497            }
14498            _ => Ok(InstallRepository::Alias(self.parse_identifier()?)),
14499        }
14500    }
14501
14502    /// Parse a SQL LOAD statement
14503    pub fn parse_load(&mut self) -> Result<Statement, ParserError> {
14504        if self.dialect.supports_load_extension() {
14505            let extension_name = self.parse_identifier()?;
14506            Ok(Statement::Load { extension_name })
14507        } else if self.parse_keyword(Keyword::DATA) && self.dialect.supports_load_data() {
14508            let local = self.parse_one_of_keywords(&[Keyword::LOCAL]).is_some();
14509            self.expect_keyword_is(Keyword::INPATH)?;
14510            let inpath = self.parse_literal_string()?;
14511            let overwrite = self.parse_one_of_keywords(&[Keyword::OVERWRITE]).is_some();
14512            self.expect_keyword_is(Keyword::INTO)?;
14513            self.expect_keyword_is(Keyword::TABLE)?;
14514            let table_name = self.parse_object_name(false)?;
14515            let partitioned = self.parse_insert_partition()?;
14516            let table_format = self.parse_load_data_table_format()?;
14517            Ok(Statement::LoadData {
14518                local,
14519                inpath,
14520                overwrite,
14521                table_name,
14522                partitioned,
14523                table_format,
14524            })
14525        } else {
14526            self.expected(
14527                "`DATA` or an extension name after `LOAD`",
14528                self.peek_token(),
14529            )
14530        }
14531    }
14532
14533    /// ```sql
14534    /// OPTIMIZE TABLE [db.]name [ON CLUSTER cluster] [PARTITION partition | PARTITION ID 'partition_id'] [FINAL] [DEDUPLICATE [BY expression]]
14535    /// ```
14536    /// [ClickHouse](https://clickhouse.com/docs/en/sql-reference/statements/optimize)
14537    pub fn parse_optimize_table(&mut self) -> Result<Statement, ParserError> {
14538        self.expect_keyword_is(Keyword::TABLE)?;
14539        let name = self.parse_object_name(false)?;
14540        let on_cluster = self.parse_optional_on_cluster()?;
14541
14542        let partition = if self.parse_keyword(Keyword::PARTITION) {
14543            if self.parse_keyword(Keyword::ID) {
14544                Some(Partition::Identifier(self.parse_identifier()?))
14545            } else {
14546                Some(Partition::Expr(self.parse_expr()?))
14547            }
14548        } else {
14549            None
14550        };
14551
14552        let include_final = self.parse_keyword(Keyword::FINAL);
14553        let deduplicate = if self.parse_keyword(Keyword::DEDUPLICATE) {
14554            if self.parse_keyword(Keyword::BY) {
14555                Some(Deduplicate::ByExpression(self.parse_expr()?))
14556            } else {
14557                Some(Deduplicate::All)
14558            }
14559        } else {
14560            None
14561        };
14562
14563        Ok(Statement::OptimizeTable {
14564            name,
14565            on_cluster,
14566            partition,
14567            include_final,
14568            deduplicate,
14569        })
14570    }
14571
14572    /// ```sql
14573    /// CREATE [ { TEMPORARY | TEMP } ] SEQUENCE [ IF NOT EXISTS ] <sequence_name>
14574    /// ```
14575    ///
14576    /// See [Postgres docs](https://www.postgresql.org/docs/current/sql-createsequence.html) for more details.
14577    pub fn parse_create_sequence(&mut self, temporary: bool) -> Result<Statement, ParserError> {
14578        //[ IF NOT EXISTS ]
14579        let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
14580        //name
14581        let name = self.parse_object_name(false)?;
14582        //[ AS data_type ]
14583        let mut data_type: Option<DataType> = None;
14584        if self.parse_keywords(&[Keyword::AS]) {
14585            data_type = Some(self.parse_data_type()?)
14586        }
14587        let sequence_options = self.parse_create_sequence_options()?;
14588        // [ OWNED BY { table_name.column_name | NONE } ]
14589        let owned_by = if self.parse_keywords(&[Keyword::OWNED, Keyword::BY]) {
14590            if self.parse_keywords(&[Keyword::NONE]) {
14591                Some(ObjectName::from(vec![Ident::new("NONE")]))
14592            } else {
14593                Some(self.parse_object_name(false)?)
14594            }
14595        } else {
14596            None
14597        };
14598        Ok(Statement::CreateSequence {
14599            temporary,
14600            if_not_exists,
14601            name,
14602            data_type,
14603            sequence_options,
14604            owned_by,
14605        })
14606    }
14607
14608    fn parse_create_sequence_options(&mut self) -> Result<Vec<SequenceOptions>, ParserError> {
14609        let mut sequence_options = vec![];
14610        //[ INCREMENT [ BY ] increment ]
14611        if self.parse_keywords(&[Keyword::INCREMENT]) {
14612            if self.parse_keywords(&[Keyword::BY]) {
14613                sequence_options.push(SequenceOptions::IncrementBy(self.parse_number()?, true));
14614            } else {
14615                sequence_options.push(SequenceOptions::IncrementBy(self.parse_number()?, false));
14616            }
14617        }
14618        //[ MINVALUE minvalue | NO MINVALUE ]
14619        if self.parse_keyword(Keyword::MINVALUE) {
14620            sequence_options.push(SequenceOptions::MinValue(Some(self.parse_number()?)));
14621        } else if self.parse_keywords(&[Keyword::NO, Keyword::MINVALUE]) {
14622            sequence_options.push(SequenceOptions::MinValue(None));
14623        }
14624        //[ MAXVALUE maxvalue | NO MAXVALUE ]
14625        if self.parse_keywords(&[Keyword::MAXVALUE]) {
14626            sequence_options.push(SequenceOptions::MaxValue(Some(self.parse_number()?)));
14627        } else if self.parse_keywords(&[Keyword::NO, Keyword::MAXVALUE]) {
14628            sequence_options.push(SequenceOptions::MaxValue(None));
14629        }
14630
14631        //[ START [ WITH ] start ]
14632        if self.parse_keywords(&[Keyword::START]) {
14633            if self.parse_keywords(&[Keyword::WITH]) {
14634                sequence_options.push(SequenceOptions::StartWith(self.parse_number()?, true));
14635            } else {
14636                sequence_options.push(SequenceOptions::StartWith(self.parse_number()?, false));
14637            }
14638        }
14639        //[ CACHE cache ]
14640        if self.parse_keywords(&[Keyword::CACHE]) {
14641            sequence_options.push(SequenceOptions::Cache(self.parse_number()?));
14642        }
14643        // [ [ NO ] CYCLE ]
14644        if self.parse_keywords(&[Keyword::NO, Keyword::CYCLE]) {
14645            sequence_options.push(SequenceOptions::Cycle(true));
14646        } else if self.parse_keywords(&[Keyword::CYCLE]) {
14647            sequence_options.push(SequenceOptions::Cycle(false));
14648        }
14649
14650        Ok(sequence_options)
14651    }
14652
14653    /// The index of the first unprocessed token.
14654    pub fn index(&self) -> usize {
14655        self.index
14656    }
14657
14658    pub fn parse_named_window(&mut self) -> Result<NamedWindowDefinition, ParserError> {
14659        let ident = self.parse_identifier()?;
14660        self.expect_keyword_is(Keyword::AS)?;
14661
14662        let window_expr = if self.consume_token(&Token::LParen) {
14663            NamedWindowExpr::WindowSpec(self.parse_window_spec()?)
14664        } else if self.dialect.supports_window_clause_named_window_reference() {
14665            NamedWindowExpr::NamedWindow(self.parse_identifier()?)
14666        } else {
14667            return self.expected("(", self.peek_token());
14668        };
14669
14670        Ok(NamedWindowDefinition(ident, window_expr))
14671    }
14672
14673    pub fn parse_create_procedure(&mut self, or_alter: bool) -> Result<Statement, ParserError> {
14674        let name = self.parse_object_name(false)?;
14675        let params = self.parse_optional_procedure_parameters()?;
14676        self.expect_keyword_is(Keyword::AS)?;
14677        self.expect_keyword_is(Keyword::BEGIN)?;
14678        let statements = self.parse_statements()?;
14679        self.expect_keyword_is(Keyword::END)?;
14680        Ok(Statement::CreateProcedure {
14681            name,
14682            or_alter,
14683            params,
14684            body: statements,
14685        })
14686    }
14687
14688    pub fn parse_window_spec(&mut self) -> Result<WindowSpec, ParserError> {
14689        let window_name = match self.peek_token().token {
14690            Token::Word(word) if word.keyword == Keyword::NoKeyword => {
14691                self.parse_optional_indent()?
14692            }
14693            _ => None,
14694        };
14695
14696        let partition_by = if self.parse_keywords(&[Keyword::PARTITION, Keyword::BY]) {
14697            self.parse_comma_separated(Parser::parse_expr)?
14698        } else {
14699            vec![]
14700        };
14701        let order_by = if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
14702            self.parse_comma_separated(Parser::parse_order_by_expr)?
14703        } else {
14704            vec![]
14705        };
14706
14707        let window_frame = if !self.consume_token(&Token::RParen) {
14708            let window_frame = self.parse_window_frame()?;
14709            self.expect_token(&Token::RParen)?;
14710            Some(window_frame)
14711        } else {
14712            None
14713        };
14714        Ok(WindowSpec {
14715            window_name,
14716            partition_by,
14717            order_by,
14718            window_frame,
14719        })
14720    }
14721
14722    pub fn parse_create_type(&mut self) -> Result<Statement, ParserError> {
14723        let name = self.parse_object_name(false)?;
14724        self.expect_keyword_is(Keyword::AS)?;
14725
14726        if self.parse_keyword(Keyword::ENUM) {
14727            return self.parse_create_type_enum(name);
14728        }
14729
14730        let mut attributes = vec![];
14731        if !self.consume_token(&Token::LParen) || self.consume_token(&Token::RParen) {
14732            return Ok(Statement::CreateType {
14733                name,
14734                representation: UserDefinedTypeRepresentation::Composite { attributes },
14735            });
14736        }
14737
14738        loop {
14739            let attr_name = self.parse_identifier()?;
14740            let attr_data_type = self.parse_data_type()?;
14741            let attr_collation = if self.parse_keyword(Keyword::COLLATE) {
14742                Some(self.parse_object_name(false)?)
14743            } else {
14744                None
14745            };
14746            attributes.push(UserDefinedTypeCompositeAttributeDef {
14747                name: attr_name,
14748                data_type: attr_data_type,
14749                collation: attr_collation,
14750            });
14751            let comma = self.consume_token(&Token::Comma);
14752            if self.consume_token(&Token::RParen) {
14753                // allow a trailing comma
14754                break;
14755            } else if !comma {
14756                return self.expected("',' or ')' after attribute definition", self.peek_token());
14757            }
14758        }
14759
14760        Ok(Statement::CreateType {
14761            name,
14762            representation: UserDefinedTypeRepresentation::Composite { attributes },
14763        })
14764    }
14765
14766    /// Parse remainder of `CREATE TYPE AS ENUM` statement (see [Statement::CreateType] and [Self::parse_create_type])
14767    ///
14768    /// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-createtype.html)
14769    pub fn parse_create_type_enum(&mut self, name: ObjectName) -> Result<Statement, ParserError> {
14770        self.expect_token(&Token::LParen)?;
14771        let labels = self.parse_comma_separated0(|p| p.parse_identifier(), Token::RParen)?;
14772        self.expect_token(&Token::RParen)?;
14773
14774        Ok(Statement::CreateType {
14775            name,
14776            representation: UserDefinedTypeRepresentation::Enum { labels },
14777        })
14778    }
14779
14780    fn parse_parenthesized_identifiers(&mut self) -> Result<Vec<Ident>, ParserError> {
14781        self.expect_token(&Token::LParen)?;
14782        let partitions = self.parse_comma_separated(|p| p.parse_identifier())?;
14783        self.expect_token(&Token::RParen)?;
14784        Ok(partitions)
14785    }
14786
14787    fn parse_column_position(&mut self) -> Result<Option<MySQLColumnPosition>, ParserError> {
14788        if dialect_of!(self is MySqlDialect | GenericDialect) {
14789            if self.parse_keyword(Keyword::FIRST) {
14790                Ok(Some(MySQLColumnPosition::First))
14791            } else if self.parse_keyword(Keyword::AFTER) {
14792                let ident = self.parse_identifier()?;
14793                Ok(Some(MySQLColumnPosition::After(ident)))
14794            } else {
14795                Ok(None)
14796            }
14797        } else {
14798            Ok(None)
14799        }
14800    }
14801
14802    /// Consume the parser and return its underlying token buffer
14803    pub fn into_tokens(self) -> Vec<TokenWithSpan> {
14804        self.tokens
14805    }
14806
14807    /// Returns true if the next keyword indicates a sub query, i.e. SELECT or WITH
14808    fn peek_sub_query(&mut self) -> bool {
14809        if self
14810            .parse_one_of_keywords(&[Keyword::SELECT, Keyword::WITH])
14811            .is_some()
14812        {
14813            self.prev_token();
14814            return true;
14815        }
14816        false
14817    }
14818
14819    pub(crate) fn parse_show_stmt_options(&mut self) -> Result<ShowStatementOptions, ParserError> {
14820        let show_in;
14821        let mut filter_position = None;
14822        if self.dialect.supports_show_like_before_in() {
14823            if let Some(filter) = self.parse_show_statement_filter()? {
14824                filter_position = Some(ShowStatementFilterPosition::Infix(filter));
14825            }
14826            show_in = self.maybe_parse_show_stmt_in()?;
14827        } else {
14828            show_in = self.maybe_parse_show_stmt_in()?;
14829            if let Some(filter) = self.parse_show_statement_filter()? {
14830                filter_position = Some(ShowStatementFilterPosition::Suffix(filter));
14831            }
14832        }
14833        let starts_with = self.maybe_parse_show_stmt_starts_with()?;
14834        let limit = self.maybe_parse_show_stmt_limit()?;
14835        let from = self.maybe_parse_show_stmt_from()?;
14836        Ok(ShowStatementOptions {
14837            filter_position,
14838            show_in,
14839            starts_with,
14840            limit,
14841            limit_from: from,
14842        })
14843    }
14844
14845    fn maybe_parse_show_stmt_in(&mut self) -> Result<Option<ShowStatementIn>, ParserError> {
14846        let clause = match self.parse_one_of_keywords(&[Keyword::FROM, Keyword::IN]) {
14847            Some(Keyword::FROM) => ShowStatementInClause::FROM,
14848            Some(Keyword::IN) => ShowStatementInClause::IN,
14849            None => return Ok(None),
14850            _ => return self.expected("FROM or IN", self.peek_token()),
14851        };
14852
14853        let (parent_type, parent_name) = match self.parse_one_of_keywords(&[
14854            Keyword::ACCOUNT,
14855            Keyword::DATABASE,
14856            Keyword::SCHEMA,
14857            Keyword::TABLE,
14858            Keyword::VIEW,
14859        ]) {
14860            // If we see these next keywords it means we don't have a parent name
14861            Some(Keyword::DATABASE)
14862                if self.peek_keywords(&[Keyword::STARTS, Keyword::WITH])
14863                    | self.peek_keyword(Keyword::LIMIT) =>
14864            {
14865                (Some(ShowStatementInParentType::Database), None)
14866            }
14867            Some(Keyword::SCHEMA)
14868                if self.peek_keywords(&[Keyword::STARTS, Keyword::WITH])
14869                    | self.peek_keyword(Keyword::LIMIT) =>
14870            {
14871                (Some(ShowStatementInParentType::Schema), None)
14872            }
14873            Some(parent_kw) => {
14874                // The parent name here is still optional, for example:
14875                // SHOW TABLES IN ACCOUNT, so parsing the object name
14876                // may fail because the statement ends.
14877                let parent_name = self.maybe_parse(|p| p.parse_object_name(false))?;
14878                match parent_kw {
14879                    Keyword::ACCOUNT => (Some(ShowStatementInParentType::Account), parent_name),
14880                    Keyword::DATABASE => (Some(ShowStatementInParentType::Database), parent_name),
14881                    Keyword::SCHEMA => (Some(ShowStatementInParentType::Schema), parent_name),
14882                    Keyword::TABLE => (Some(ShowStatementInParentType::Table), parent_name),
14883                    Keyword::VIEW => (Some(ShowStatementInParentType::View), parent_name),
14884                    _ => {
14885                        return self.expected(
14886                            "one of ACCOUNT, DATABASE, SCHEMA, TABLE or VIEW",
14887                            self.peek_token(),
14888                        )
14889                    }
14890                }
14891            }
14892            None => {
14893                // Parsing MySQL style FROM tbl_name FROM db_name
14894                // which is equivalent to FROM tbl_name.db_name
14895                let mut parent_name = self.parse_object_name(false)?;
14896                if self
14897                    .parse_one_of_keywords(&[Keyword::FROM, Keyword::IN])
14898                    .is_some()
14899                {
14900                    parent_name
14901                        .0
14902                        .insert(0, ObjectNamePart::Identifier(self.parse_identifier()?));
14903                }
14904                (None, Some(parent_name))
14905            }
14906        };
14907
14908        Ok(Some(ShowStatementIn {
14909            clause,
14910            parent_type,
14911            parent_name,
14912        }))
14913    }
14914
14915    fn maybe_parse_show_stmt_starts_with(&mut self) -> Result<Option<Value>, ParserError> {
14916        if self.parse_keywords(&[Keyword::STARTS, Keyword::WITH]) {
14917            Ok(Some(self.parse_value()?.value))
14918        } else {
14919            Ok(None)
14920        }
14921    }
14922
14923    fn maybe_parse_show_stmt_limit(&mut self) -> Result<Option<Expr>, ParserError> {
14924        if self.parse_keyword(Keyword::LIMIT) {
14925            Ok(self.parse_limit()?)
14926        } else {
14927            Ok(None)
14928        }
14929    }
14930
14931    fn maybe_parse_show_stmt_from(&mut self) -> Result<Option<Value>, ParserError> {
14932        if self.parse_keyword(Keyword::FROM) {
14933            Ok(Some(self.parse_value()?.value))
14934        } else {
14935            Ok(None)
14936        }
14937    }
14938}
14939
14940impl Word {
14941    #[deprecated(since = "0.54.0", note = "please use `into_ident` instead")]
14942    pub fn to_ident(&self, span: Span) -> Ident {
14943        Ident {
14944            value: self.value.clone(),
14945            quote_style: self.quote_style,
14946            span,
14947        }
14948    }
14949
14950    /// Convert this word into an [`Ident`] identifier
14951    pub fn into_ident(self, span: Span) -> Ident {
14952        Ident {
14953            value: self.value,
14954            quote_style: self.quote_style,
14955            span,
14956        }
14957    }
14958}
14959
14960#[cfg(test)]
14961mod tests {
14962    use crate::test_utils::{all_dialects, TestedDialects};
14963
14964    use super::*;
14965
14966    #[test]
14967    fn test_prev_index() {
14968        let sql = "SELECT version";
14969        all_dialects().run_parser_method(sql, |parser| {
14970            assert_eq!(parser.peek_token(), Token::make_keyword("SELECT"));
14971            assert_eq!(parser.next_token(), Token::make_keyword("SELECT"));
14972            parser.prev_token();
14973            assert_eq!(parser.next_token(), Token::make_keyword("SELECT"));
14974            assert_eq!(parser.next_token(), Token::make_word("version", None));
14975            parser.prev_token();
14976            assert_eq!(parser.peek_token(), Token::make_word("version", None));
14977            assert_eq!(parser.next_token(), Token::make_word("version", None));
14978            assert_eq!(parser.peek_token(), Token::EOF);
14979            parser.prev_token();
14980            assert_eq!(parser.next_token(), Token::make_word("version", None));
14981            assert_eq!(parser.next_token(), Token::EOF);
14982            assert_eq!(parser.next_token(), Token::EOF);
14983            parser.prev_token();
14984        });
14985    }
14986
14987    #[test]
14988    fn test_peek_tokens() {
14989        all_dialects().run_parser_method("SELECT foo AS bar FROM baz", |parser| {
14990            assert!(matches!(
14991                parser.peek_tokens(),
14992                [Token::Word(Word {
14993                    keyword: Keyword::SELECT,
14994                    ..
14995                })]
14996            ));
14997
14998            assert!(matches!(
14999                parser.peek_tokens(),
15000                [
15001                    Token::Word(Word {
15002                        keyword: Keyword::SELECT,
15003                        ..
15004                    }),
15005                    Token::Word(_),
15006                    Token::Word(Word {
15007                        keyword: Keyword::AS,
15008                        ..
15009                    }),
15010                ]
15011            ));
15012
15013            for _ in 0..4 {
15014                parser.next_token();
15015            }
15016
15017            assert!(matches!(
15018                parser.peek_tokens(),
15019                [
15020                    Token::Word(Word {
15021                        keyword: Keyword::FROM,
15022                        ..
15023                    }),
15024                    Token::Word(_),
15025                    Token::EOF,
15026                    Token::EOF,
15027                ]
15028            ))
15029        })
15030    }
15031
15032    #[cfg(test)]
15033    mod test_parse_data_type {
15034        use crate::ast::{
15035            CharLengthUnits, CharacterLength, DataType, ExactNumberInfo, ObjectName, TimezoneInfo,
15036        };
15037        use crate::dialect::{AnsiDialect, GenericDialect};
15038        use crate::test_utils::TestedDialects;
15039
15040        macro_rules! test_parse_data_type {
15041            ($dialect:expr, $input:expr, $expected_type:expr $(,)?) => {{
15042                $dialect.run_parser_method(&*$input, |parser| {
15043                    let data_type = parser.parse_data_type().unwrap();
15044                    assert_eq!($expected_type, data_type);
15045                    assert_eq!($input.to_string(), data_type.to_string());
15046                });
15047            }};
15048        }
15049
15050        #[test]
15051        fn test_ansii_character_string_types() {
15052            // Character string types: <https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#character-string-type>
15053            let dialect =
15054                TestedDialects::new(vec![Box::new(GenericDialect {}), Box::new(AnsiDialect {})]);
15055
15056            test_parse_data_type!(dialect, "CHARACTER", DataType::Character(None));
15057
15058            test_parse_data_type!(
15059                dialect,
15060                "CHARACTER(20)",
15061                DataType::Character(Some(CharacterLength::IntegerLength {
15062                    length: 20,
15063                    unit: None
15064                }))
15065            );
15066
15067            test_parse_data_type!(
15068                dialect,
15069                "CHARACTER(20 CHARACTERS)",
15070                DataType::Character(Some(CharacterLength::IntegerLength {
15071                    length: 20,
15072                    unit: Some(CharLengthUnits::Characters)
15073                }))
15074            );
15075
15076            test_parse_data_type!(
15077                dialect,
15078                "CHARACTER(20 OCTETS)",
15079                DataType::Character(Some(CharacterLength::IntegerLength {
15080                    length: 20,
15081                    unit: Some(CharLengthUnits::Octets)
15082                }))
15083            );
15084
15085            test_parse_data_type!(dialect, "CHAR", DataType::Char(None));
15086
15087            test_parse_data_type!(
15088                dialect,
15089                "CHAR(20)",
15090                DataType::Char(Some(CharacterLength::IntegerLength {
15091                    length: 20,
15092                    unit: None
15093                }))
15094            );
15095
15096            test_parse_data_type!(
15097                dialect,
15098                "CHAR(20 CHARACTERS)",
15099                DataType::Char(Some(CharacterLength::IntegerLength {
15100                    length: 20,
15101                    unit: Some(CharLengthUnits::Characters)
15102                }))
15103            );
15104
15105            test_parse_data_type!(
15106                dialect,
15107                "CHAR(20 OCTETS)",
15108                DataType::Char(Some(CharacterLength::IntegerLength {
15109                    length: 20,
15110                    unit: Some(CharLengthUnits::Octets)
15111                }))
15112            );
15113
15114            test_parse_data_type!(
15115                dialect,
15116                "CHARACTER VARYING(20)",
15117                DataType::CharacterVarying(Some(CharacterLength::IntegerLength {
15118                    length: 20,
15119                    unit: None
15120                }))
15121            );
15122
15123            test_parse_data_type!(
15124                dialect,
15125                "CHARACTER VARYING(20 CHARACTERS)",
15126                DataType::CharacterVarying(Some(CharacterLength::IntegerLength {
15127                    length: 20,
15128                    unit: Some(CharLengthUnits::Characters)
15129                }))
15130            );
15131
15132            test_parse_data_type!(
15133                dialect,
15134                "CHARACTER VARYING(20 OCTETS)",
15135                DataType::CharacterVarying(Some(CharacterLength::IntegerLength {
15136                    length: 20,
15137                    unit: Some(CharLengthUnits::Octets)
15138                }))
15139            );
15140
15141            test_parse_data_type!(
15142                dialect,
15143                "CHAR VARYING(20)",
15144                DataType::CharVarying(Some(CharacterLength::IntegerLength {
15145                    length: 20,
15146                    unit: None
15147                }))
15148            );
15149
15150            test_parse_data_type!(
15151                dialect,
15152                "CHAR VARYING(20 CHARACTERS)",
15153                DataType::CharVarying(Some(CharacterLength::IntegerLength {
15154                    length: 20,
15155                    unit: Some(CharLengthUnits::Characters)
15156                }))
15157            );
15158
15159            test_parse_data_type!(
15160                dialect,
15161                "CHAR VARYING(20 OCTETS)",
15162                DataType::CharVarying(Some(CharacterLength::IntegerLength {
15163                    length: 20,
15164                    unit: Some(CharLengthUnits::Octets)
15165                }))
15166            );
15167
15168            test_parse_data_type!(
15169                dialect,
15170                "VARCHAR(20)",
15171                DataType::Varchar(Some(CharacterLength::IntegerLength {
15172                    length: 20,
15173                    unit: None
15174                }))
15175            );
15176        }
15177
15178        #[test]
15179        fn test_ansii_character_large_object_types() {
15180            // Character large object types: <https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#character-large-object-length>
15181            let dialect =
15182                TestedDialects::new(vec![Box::new(GenericDialect {}), Box::new(AnsiDialect {})]);
15183
15184            test_parse_data_type!(
15185                dialect,
15186                "CHARACTER LARGE OBJECT",
15187                DataType::CharacterLargeObject(None)
15188            );
15189            test_parse_data_type!(
15190                dialect,
15191                "CHARACTER LARGE OBJECT(20)",
15192                DataType::CharacterLargeObject(Some(20))
15193            );
15194
15195            test_parse_data_type!(
15196                dialect,
15197                "CHAR LARGE OBJECT",
15198                DataType::CharLargeObject(None)
15199            );
15200            test_parse_data_type!(
15201                dialect,
15202                "CHAR LARGE OBJECT(20)",
15203                DataType::CharLargeObject(Some(20))
15204            );
15205
15206            test_parse_data_type!(dialect, "CLOB", DataType::Clob(None));
15207            test_parse_data_type!(dialect, "CLOB(20)", DataType::Clob(Some(20)));
15208        }
15209
15210        #[test]
15211        fn test_parse_custom_types() {
15212            let dialect =
15213                TestedDialects::new(vec![Box::new(GenericDialect {}), Box::new(AnsiDialect {})]);
15214
15215            test_parse_data_type!(
15216                dialect,
15217                "GEOMETRY",
15218                DataType::Custom(ObjectName::from(vec!["GEOMETRY".into()]), vec![])
15219            );
15220
15221            test_parse_data_type!(
15222                dialect,
15223                "GEOMETRY(POINT)",
15224                DataType::Custom(
15225                    ObjectName::from(vec!["GEOMETRY".into()]),
15226                    vec!["POINT".to_string()]
15227                )
15228            );
15229
15230            test_parse_data_type!(
15231                dialect,
15232                "GEOMETRY(POINT, 4326)",
15233                DataType::Custom(
15234                    ObjectName::from(vec!["GEOMETRY".into()]),
15235                    vec!["POINT".to_string(), "4326".to_string()]
15236                )
15237            );
15238        }
15239
15240        #[test]
15241        fn test_ansii_exact_numeric_types() {
15242            // Exact numeric types: <https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#exact-numeric-type>
15243            let dialect =
15244                TestedDialects::new(vec![Box::new(GenericDialect {}), Box::new(AnsiDialect {})]);
15245
15246            test_parse_data_type!(dialect, "NUMERIC", DataType::Numeric(ExactNumberInfo::None));
15247
15248            test_parse_data_type!(
15249                dialect,
15250                "NUMERIC(2)",
15251                DataType::Numeric(ExactNumberInfo::Precision(2))
15252            );
15253
15254            test_parse_data_type!(
15255                dialect,
15256                "NUMERIC(2,10)",
15257                DataType::Numeric(ExactNumberInfo::PrecisionAndScale(2, 10))
15258            );
15259
15260            test_parse_data_type!(dialect, "DECIMAL", DataType::Decimal(ExactNumberInfo::None));
15261
15262            test_parse_data_type!(
15263                dialect,
15264                "DECIMAL(2)",
15265                DataType::Decimal(ExactNumberInfo::Precision(2))
15266            );
15267
15268            test_parse_data_type!(
15269                dialect,
15270                "DECIMAL(2,10)",
15271                DataType::Decimal(ExactNumberInfo::PrecisionAndScale(2, 10))
15272            );
15273
15274            test_parse_data_type!(dialect, "DEC", DataType::Dec(ExactNumberInfo::None));
15275
15276            test_parse_data_type!(
15277                dialect,
15278                "DEC(2)",
15279                DataType::Dec(ExactNumberInfo::Precision(2))
15280            );
15281
15282            test_parse_data_type!(
15283                dialect,
15284                "DEC(2,10)",
15285                DataType::Dec(ExactNumberInfo::PrecisionAndScale(2, 10))
15286            );
15287        }
15288
15289        #[test]
15290        fn test_ansii_date_type() {
15291            // Datetime types: <https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#datetime-type>
15292            let dialect =
15293                TestedDialects::new(vec![Box::new(GenericDialect {}), Box::new(AnsiDialect {})]);
15294
15295            test_parse_data_type!(dialect, "DATE", DataType::Date);
15296
15297            test_parse_data_type!(dialect, "TIME", DataType::Time(None, TimezoneInfo::None));
15298
15299            test_parse_data_type!(
15300                dialect,
15301                "TIME(6)",
15302                DataType::Time(Some(6), TimezoneInfo::None)
15303            );
15304
15305            test_parse_data_type!(
15306                dialect,
15307                "TIME WITH TIME ZONE",
15308                DataType::Time(None, TimezoneInfo::WithTimeZone)
15309            );
15310
15311            test_parse_data_type!(
15312                dialect,
15313                "TIME(6) WITH TIME ZONE",
15314                DataType::Time(Some(6), TimezoneInfo::WithTimeZone)
15315            );
15316
15317            test_parse_data_type!(
15318                dialect,
15319                "TIME WITHOUT TIME ZONE",
15320                DataType::Time(None, TimezoneInfo::WithoutTimeZone)
15321            );
15322
15323            test_parse_data_type!(
15324                dialect,
15325                "TIME(6) WITHOUT TIME ZONE",
15326                DataType::Time(Some(6), TimezoneInfo::WithoutTimeZone)
15327            );
15328
15329            test_parse_data_type!(
15330                dialect,
15331                "TIMESTAMP",
15332                DataType::Timestamp(None, TimezoneInfo::None)
15333            );
15334
15335            test_parse_data_type!(
15336                dialect,
15337                "TIMESTAMP(22)",
15338                DataType::Timestamp(Some(22), TimezoneInfo::None)
15339            );
15340
15341            test_parse_data_type!(
15342                dialect,
15343                "TIMESTAMP(22) WITH TIME ZONE",
15344                DataType::Timestamp(Some(22), TimezoneInfo::WithTimeZone)
15345            );
15346
15347            test_parse_data_type!(
15348                dialect,
15349                "TIMESTAMP(33) WITHOUT TIME ZONE",
15350                DataType::Timestamp(Some(33), TimezoneInfo::WithoutTimeZone)
15351            );
15352        }
15353    }
15354
15355    #[test]
15356    fn test_parse_schema_name() {
15357        // The expected name should be identical as the input name, that's why I don't receive both
15358        macro_rules! test_parse_schema_name {
15359            ($input:expr, $expected_name:expr $(,)?) => {{
15360                all_dialects().run_parser_method(&*$input, |parser| {
15361                    let schema_name = parser.parse_schema_name().unwrap();
15362                    // Validate that the structure is the same as expected
15363                    assert_eq!(schema_name, $expected_name);
15364                    // Validate that the input and the expected structure serialization are the same
15365                    assert_eq!(schema_name.to_string(), $input.to_string());
15366                });
15367            }};
15368        }
15369
15370        let dummy_name = ObjectName::from(vec![Ident::new("dummy_name")]);
15371        let dummy_authorization = Ident::new("dummy_authorization");
15372
15373        test_parse_schema_name!(
15374            format!("{dummy_name}"),
15375            SchemaName::Simple(dummy_name.clone())
15376        );
15377
15378        test_parse_schema_name!(
15379            format!("AUTHORIZATION {dummy_authorization}"),
15380            SchemaName::UnnamedAuthorization(dummy_authorization.clone()),
15381        );
15382        test_parse_schema_name!(
15383            format!("{dummy_name} AUTHORIZATION {dummy_authorization}"),
15384            SchemaName::NamedAuthorization(dummy_name.clone(), dummy_authorization.clone()),
15385        );
15386    }
15387
15388    #[test]
15389    fn mysql_parse_index_table_constraint() {
15390        macro_rules! test_parse_table_constraint {
15391            ($dialect:expr, $input:expr, $expected:expr $(,)?) => {{
15392                $dialect.run_parser_method(&*$input, |parser| {
15393                    let constraint = parser.parse_optional_table_constraint().unwrap().unwrap();
15394                    // Validate that the structure is the same as expected
15395                    assert_eq!(constraint, $expected);
15396                    // Validate that the input and the expected structure serialization are the same
15397                    assert_eq!(constraint.to_string(), $input.to_string());
15398                });
15399            }};
15400        }
15401
15402        let dialect =
15403            TestedDialects::new(vec![Box::new(GenericDialect {}), Box::new(MySqlDialect {})]);
15404
15405        test_parse_table_constraint!(
15406            dialect,
15407            "INDEX (c1)",
15408            TableConstraint::Index {
15409                display_as_key: false,
15410                name: None,
15411                index_type: None,
15412                columns: vec![Ident::new("c1")],
15413            }
15414        );
15415
15416        test_parse_table_constraint!(
15417            dialect,
15418            "KEY (c1)",
15419            TableConstraint::Index {
15420                display_as_key: true,
15421                name: None,
15422                index_type: None,
15423                columns: vec![Ident::new("c1")],
15424            }
15425        );
15426
15427        test_parse_table_constraint!(
15428            dialect,
15429            "INDEX 'index' (c1, c2)",
15430            TableConstraint::Index {
15431                display_as_key: false,
15432                name: Some(Ident::with_quote('\'', "index")),
15433                index_type: None,
15434                columns: vec![Ident::new("c1"), Ident::new("c2")],
15435            }
15436        );
15437
15438        test_parse_table_constraint!(
15439            dialect,
15440            "INDEX USING BTREE (c1)",
15441            TableConstraint::Index {
15442                display_as_key: false,
15443                name: None,
15444                index_type: Some(IndexType::BTree),
15445                columns: vec![Ident::new("c1")],
15446            }
15447        );
15448
15449        test_parse_table_constraint!(
15450            dialect,
15451            "INDEX USING HASH (c1)",
15452            TableConstraint::Index {
15453                display_as_key: false,
15454                name: None,
15455                index_type: Some(IndexType::Hash),
15456                columns: vec![Ident::new("c1")],
15457            }
15458        );
15459
15460        test_parse_table_constraint!(
15461            dialect,
15462            "INDEX idx_name USING BTREE (c1)",
15463            TableConstraint::Index {
15464                display_as_key: false,
15465                name: Some(Ident::new("idx_name")),
15466                index_type: Some(IndexType::BTree),
15467                columns: vec![Ident::new("c1")],
15468            }
15469        );
15470
15471        test_parse_table_constraint!(
15472            dialect,
15473            "INDEX idx_name USING HASH (c1)",
15474            TableConstraint::Index {
15475                display_as_key: false,
15476                name: Some(Ident::new("idx_name")),
15477                index_type: Some(IndexType::Hash),
15478                columns: vec![Ident::new("c1")],
15479            }
15480        );
15481    }
15482
15483    #[test]
15484    fn test_tokenizer_error_loc() {
15485        let sql = "foo '";
15486        let ast = Parser::parse_sql(&GenericDialect, sql);
15487        assert_eq!(
15488            ast,
15489            Err(ParserError::TokenizerError(
15490                "Unterminated string literal at Line: 1, Column: 5".to_string()
15491            ))
15492        );
15493    }
15494
15495    #[test]
15496    fn test_parser_error_loc() {
15497        let sql = "SELECT this is a syntax error";
15498        let ast = Parser::parse_sql(&GenericDialect, sql);
15499        assert_eq!(
15500            ast,
15501            Err(ParserError::ParserError(
15502                "Expected: [NOT] NULL | TRUE | FALSE | DISTINCT | [form] NORMALIZED FROM after IS, found: a at Line: 1, Column: 16"
15503                    .to_string()
15504            ))
15505        );
15506    }
15507
15508    #[test]
15509    fn test_nested_explain_error() {
15510        let sql = "EXPLAIN EXPLAIN SELECT 1";
15511        let ast = Parser::parse_sql(&GenericDialect, sql);
15512        assert_eq!(
15513            ast,
15514            Err(ParserError::ParserError(
15515                "Explain must be root of the plan".to_string()
15516            ))
15517        );
15518    }
15519
15520    #[test]
15521    fn test_parse_multipart_identifier_positive() {
15522        let dialect = TestedDialects::new(vec![Box::new(GenericDialect {})]);
15523
15524        // parse multipart with quotes
15525        let expected = vec![
15526            Ident {
15527                value: "CATALOG".to_string(),
15528                quote_style: None,
15529                span: Span::empty(),
15530            },
15531            Ident {
15532                value: "F(o)o. \"bar".to_string(),
15533                quote_style: Some('"'),
15534                span: Span::empty(),
15535            },
15536            Ident {
15537                value: "table".to_string(),
15538                quote_style: None,
15539                span: Span::empty(),
15540            },
15541        ];
15542        dialect.run_parser_method(r#"CATALOG."F(o)o. ""bar".table"#, |parser| {
15543            let actual = parser.parse_multipart_identifier().unwrap();
15544            assert_eq!(expected, actual);
15545        });
15546
15547        // allow whitespace between ident parts
15548        let expected = vec![
15549            Ident {
15550                value: "CATALOG".to_string(),
15551                quote_style: None,
15552                span: Span::empty(),
15553            },
15554            Ident {
15555                value: "table".to_string(),
15556                quote_style: None,
15557                span: Span::empty(),
15558            },
15559        ];
15560        dialect.run_parser_method("CATALOG . table", |parser| {
15561            let actual = parser.parse_multipart_identifier().unwrap();
15562            assert_eq!(expected, actual);
15563        });
15564    }
15565
15566    #[test]
15567    fn test_parse_multipart_identifier_negative() {
15568        macro_rules! test_parse_multipart_identifier_error {
15569            ($input:expr, $expected_err:expr $(,)?) => {{
15570                all_dialects().run_parser_method(&*$input, |parser| {
15571                    let actual_err = parser.parse_multipart_identifier().unwrap_err();
15572                    assert_eq!(actual_err.to_string(), $expected_err);
15573                });
15574            }};
15575        }
15576
15577        test_parse_multipart_identifier_error!(
15578            "",
15579            "sql parser error: Empty input when parsing identifier",
15580        );
15581
15582        test_parse_multipart_identifier_error!(
15583            "*schema.table",
15584            "sql parser error: Unexpected token in identifier: *",
15585        );
15586
15587        test_parse_multipart_identifier_error!(
15588            "schema.table*",
15589            "sql parser error: Unexpected token in identifier: *",
15590        );
15591
15592        test_parse_multipart_identifier_error!(
15593            "schema.table.",
15594            "sql parser error: Trailing period in identifier",
15595        );
15596
15597        test_parse_multipart_identifier_error!(
15598            "schema.*",
15599            "sql parser error: Unexpected token following period in identifier: *",
15600        );
15601    }
15602
15603    #[test]
15604    fn test_mysql_partition_selection() {
15605        let sql = "SELECT * FROM employees PARTITION (p0, p2)";
15606        let expected = vec!["p0", "p2"];
15607
15608        let ast: Vec<Statement> = Parser::parse_sql(&MySqlDialect {}, sql).unwrap();
15609        assert_eq!(ast.len(), 1);
15610        if let Statement::Query(v) = &ast[0] {
15611            if let SetExpr::Select(select) = &*v.body {
15612                assert_eq!(select.from.len(), 1);
15613                let from: &TableWithJoins = &select.from[0];
15614                let table_factor = &from.relation;
15615                if let TableFactor::Table { partitions, .. } = table_factor {
15616                    let actual: Vec<&str> = partitions
15617                        .iter()
15618                        .map(|ident| ident.value.as_str())
15619                        .collect();
15620                    assert_eq!(expected, actual);
15621                }
15622            }
15623        } else {
15624            panic!("fail to parse mysql partition selection");
15625        }
15626    }
15627
15628    #[test]
15629    fn test_replace_into_placeholders() {
15630        let sql = "REPLACE INTO t (a) VALUES (&a)";
15631
15632        assert!(Parser::parse_sql(&GenericDialect {}, sql).is_err());
15633    }
15634
15635    #[test]
15636    fn test_replace_into_set_placeholder() {
15637        let sql = "REPLACE INTO t SET ?";
15638
15639        assert!(Parser::parse_sql(&GenericDialect {}, sql).is_err());
15640    }
15641
15642    #[test]
15643    fn test_replace_incomplete() {
15644        let sql = r#"REPLACE"#;
15645
15646        assert!(Parser::parse_sql(&MySqlDialect {}, sql).is_err());
15647    }
15648
15649    // ---- DuckDB's Python-style `lambda x: expr` lambda syntax. ----
15650    // DuckDB is retiring the single-arrow lambda in favour of this form; both
15651    // must keep parsing so a model written in either survives introspection
15652    // (see MERIDIAN_PATCH.md #3).
15653
15654    // `lambda c: expr` (one param, no parens) parses to the same
15655    // `Expr::Lambda` node the one-param arrow form does, and `Display`
15656    // canonicalizes to that arrow form on round-trip.
15657    #[test]
15658    fn test_duckdb_lambda_colon_single_param() {
15659        let duckdb = TestedDialects::new(vec![Box::new(DuckDbDialect {})]);
15660        let expr = duckdb.expr_parses_to("lambda c: c + 1", "c -> c + 1");
15661        match expr {
15662            Expr::Lambda(LambdaFunction { params, .. }) => {
15663                assert_eq!(params, OneOrManyWithParens::One(Ident::new("c")));
15664            }
15665            other => panic!("expected Expr::Lambda, got {other:?}"),
15666        }
15667    }
15668
15669    // `lambda acc, v: expr` (several params, no parens) parses to the same
15670    // `Expr::Lambda` node the parenthesized arrow form does.
15671    #[test]
15672    fn test_duckdb_lambda_colon_multi_param() {
15673        let duckdb = TestedDialects::new(vec![Box::new(DuckDbDialect {})]);
15674        let expr = duckdb.expr_parses_to("lambda acc, v: acc + v", "(acc, v) -> acc + v");
15675        match expr {
15676            Expr::Lambda(LambdaFunction { params, .. }) => {
15677                assert_eq!(
15678                    params,
15679                    OneOrManyWithParens::Many(vec![Ident::new("acc"), Ident::new("v")])
15680                );
15681            }
15682            other => panic!("expected Expr::Lambda, got {other:?}"),
15683        }
15684    }
15685
15686    // Both pre-existing arrow forms still parse under DuckDB — the colon
15687    // syntax is additive, not a replacement.
15688    #[test]
15689    fn test_duckdb_lambda_arrow_forms_still_parse() {
15690        let duckdb = TestedDialects::new(vec![Box::new(DuckDbDialect {})]);
15691        duckdb.verified_expr("c -> c + 1");
15692        duckdb.verified_expr("(acc, v) -> acc + v");
15693    }
15694
15695    // The colon syntax is gated to DuckDB: ClickHouse and Databricks both set
15696    // `supports_lambda_functions` (their own arrow-only lambdas) but have no
15697    // `lambda x:` form, and a dialect that did not accept it before must still
15698    // reject it. Full-statement parsing (rather than a bare `parse_expr`) so a
15699    // stray `lambda` falling back to a plain identifier can't hide a pass —
15700    // the trailing `c: c + 1` has to fail to parse as anything for this to
15701    // count as rejection.
15702    #[test]
15703    fn test_lambda_colon_syntax_rejected_outside_duckdb() {
15704        let sql = "SELECT list_transform([1, 2, 3], lambda c: c + 1)";
15705        for dialect in [
15706            Box::new(ClickHouseDialect {}) as Box<dyn Dialect>,
15707            Box::new(DatabricksDialect {}) as Box<dyn Dialect>,
15708            Box::new(GenericDialect {}) as Box<dyn Dialect>,
15709        ] {
15710            let dialects = TestedDialects::new(vec![dialect]);
15711            assert!(
15712                dialects.parse_sql_statements(sql).is_err(),
15713                "colon-lambda syntax must be rejected outside DuckDB"
15714            );
15715        }
15716        // DuckDB itself accepts the same statement.
15717        let duckdb = TestedDialects::new(vec![Box::new(DuckDbDialect {})]);
15718        assert!(duckdb.parse_sql_statements(sql).is_ok());
15719    }
15720
15721    // ClickHouse's and Databricks' own arrow-form lambdas are untouched by
15722    // adding the DuckDB-only colon syntax.
15723    #[test]
15724    fn test_other_lambda_dialects_arrow_form_unaffected() {
15725        let clickhouse = TestedDialects::new(vec![Box::new(ClickHouseDialect {})]);
15726        clickhouse.verified_expr("x -> x + 1");
15727        let databricks = TestedDialects::new(vec![Box::new(DatabricksDialect {})]);
15728        databricks.verified_expr("x -> x + 1");
15729    }
15730}