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::fmt;
24
25use log::debug;
26
27use IsLateral::*;
28use IsOptional::*;
29
30use crate::ast::helpers::stmt_create_table::CreateTableBuilder;
31use crate::ast::*;
32use crate::dialect::*;
33use crate::keywords::{self, Keyword, ALL_KEYWORDS};
34use crate::tokenizer::*;
35
36mod alter;
37
38#[derive(Debug, Clone, PartialEq, Eq)]
39pub enum ParserError {
40    TokenizerError(String),
41    ParserError(String),
42    RecursionLimitExceeded,
43}
44
45// Use `Parser::expected` instead, if possible
46macro_rules! parser_err {
47    ($MSG:expr, $loc:expr) => {
48        Err(ParserError::ParserError(format!("{}{}", $MSG, $loc)))
49    };
50}
51
52// Returns a successful result if the optional expression is some
53macro_rules! return_ok_if_some {
54    ($e:expr) => {{
55        if let Some(v) = $e {
56            return Ok(v);
57        }
58    }};
59}
60
61#[cfg(feature = "std")]
62/// Implementation [`RecursionCounter`] if std is available
63mod recursion {
64    use core::sync::atomic::{AtomicUsize, Ordering};
65    use std::rc::Rc;
66
67    use super::ParserError;
68
69    /// Tracks remaining recursion depth. This value is decremented on
70    /// each call to `try_decrease()`, when it reaches 0 an error will
71    /// be returned.
72    ///
73    /// Note: Uses an Rc and AtomicUsize in order to satisfy the Rust
74    /// borrow checker so the automatic DepthGuard decrement a
75    /// reference to the counter. The actual value is not modified
76    /// concurrently
77    pub(crate) struct RecursionCounter {
78        remaining_depth: Rc<AtomicUsize>,
79    }
80
81    impl RecursionCounter {
82        /// Creates a [`RecursionCounter`] with the specified maximum
83        /// depth
84        pub fn new(remaining_depth: usize) -> Self {
85            Self {
86                remaining_depth: Rc::new(remaining_depth.into()),
87            }
88        }
89
90        /// Decreases the remaining depth by 1.
91        ///
92        /// Returns `Err` if the remaining depth falls to 0.
93        ///
94        /// Returns a [`DepthGuard`] which will adds 1 to the
95        /// remaining depth upon drop;
96        pub fn try_decrease(&self) -> Result<DepthGuard, ParserError> {
97            let old_value = self.remaining_depth.fetch_sub(1, Ordering::SeqCst);
98            // ran out of space
99            if old_value == 0 {
100                Err(ParserError::RecursionLimitExceeded)
101            } else {
102                Ok(DepthGuard::new(Rc::clone(&self.remaining_depth)))
103            }
104        }
105    }
106
107    /// Guard that increass the remaining depth by 1 on drop
108    pub struct DepthGuard {
109        remaining_depth: Rc<AtomicUsize>,
110    }
111
112    impl DepthGuard {
113        fn new(remaining_depth: Rc<AtomicUsize>) -> Self {
114            Self { remaining_depth }
115        }
116    }
117    impl Drop for DepthGuard {
118        fn drop(&mut self) {
119            self.remaining_depth.fetch_add(1, Ordering::SeqCst);
120        }
121    }
122}
123
124#[cfg(not(feature = "std"))]
125mod recursion {
126    /// Implementation [`RecursionCounter`] if std is NOT available (and does not
127    /// guard against stack overflow).
128    ///
129    /// Has the same API as the std RecursionCounter implementation
130    /// but does not actually limit stack depth.
131    pub(crate) struct RecursionCounter {}
132
133    impl RecursionCounter {
134        pub fn new(_remaining_depth: usize) -> Self {
135            Self {}
136        }
137        pub fn try_decrease(&self) -> Result<DepthGuard, super::ParserError> {
138            Ok(DepthGuard {})
139        }
140    }
141
142    pub struct DepthGuard {}
143}
144
145use recursion::RecursionCounter;
146
147#[derive(PartialEq, Eq)]
148pub enum IsOptional {
149    Optional,
150    Mandatory,
151}
152
153pub enum IsLateral {
154    Lateral,
155    NotLateral,
156}
157
158pub enum WildcardExpr {
159    Expr(Expr),
160    QualifiedWildcard(ObjectName),
161    Wildcard,
162}
163
164impl From<WildcardExpr> for FunctionArgExpr {
165    fn from(wildcard_expr: WildcardExpr) -> Self {
166        match wildcard_expr {
167            WildcardExpr::Expr(expr) => Self::Expr(expr),
168            WildcardExpr::QualifiedWildcard(prefix) => Self::QualifiedWildcard(prefix),
169            WildcardExpr::Wildcard => Self::Wildcard,
170        }
171    }
172}
173
174impl From<TokenizerError> for ParserError {
175    fn from(e: TokenizerError) -> Self {
176        ParserError::TokenizerError(e.to_string())
177    }
178}
179
180impl fmt::Display for ParserError {
181    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
182        write!(
183            f,
184            "sql parser error: {}",
185            match self {
186                ParserError::TokenizerError(s) => s,
187                ParserError::ParserError(s) => s,
188                ParserError::RecursionLimitExceeded => "recursion limit exceeded",
189            }
190        )
191    }
192}
193
194#[cfg(feature = "std")]
195impl std::error::Error for ParserError {}
196
197// By default, allow expressions up to this deep before erroring
198const DEFAULT_REMAINING_DEPTH: usize = 50;
199
200/// Composite types declarations using angle brackets syntax can be arbitrary
201/// nested such that the following declaration is possible:
202///      `ARRAY<ARRAY<INT>>`
203/// But the tokenizer recognizes the `>>` as a ShiftRight token.
204/// We work-around that limitation when parsing a data type by accepting
205/// either a `>` or `>>` token in such cases, remembering which variant we
206/// matched.
207/// In the latter case having matched a `>>`, the parent type will not look to
208/// match its closing `>` as a result since that will have taken place at the
209/// child type.
210///
211/// See [Parser::parse_data_type] for details
212struct MatchedTrailingBracket(bool);
213
214impl From<bool> for MatchedTrailingBracket {
215    fn from(value: bool) -> Self {
216        Self(value)
217    }
218}
219
220/// Options that control how the [`Parser`] parses SQL text
221#[derive(Debug, Clone, PartialEq, Eq)]
222pub struct ParserOptions {
223    pub trailing_commas: bool,
224    /// Controls how literal values are unescaped. See
225    /// [`Tokenizer::with_unescape`] for more details.
226    pub unescape: bool,
227}
228
229impl Default for ParserOptions {
230    fn default() -> Self {
231        Self {
232            trailing_commas: false,
233            unescape: true,
234        }
235    }
236}
237
238impl ParserOptions {
239    /// Create a new [`ParserOptions`]
240    pub fn new() -> Self {
241        Default::default()
242    }
243
244    /// Set if trailing commas are allowed.
245    ///
246    /// If this option is `false` (the default), the following SQL will
247    /// not parse. If the option is `true`, the SQL will parse.
248    ///
249    /// ```sql
250    ///  SELECT
251    ///   foo,
252    ///   bar,
253    ///  FROM baz
254    /// ```
255    pub fn with_trailing_commas(mut self, trailing_commas: bool) -> Self {
256        self.trailing_commas = trailing_commas;
257        self
258    }
259
260    /// Set if literal values are unescaped. Defaults to true. See
261    /// [`Tokenizer::with_unescape`] for more details.
262    pub fn with_unescape(mut self, unescape: bool) -> Self {
263        self.unescape = unescape;
264        self
265    }
266}
267
268pub struct Parser<'a> {
269    tokens: Vec<TokenWithLocation>,
270    /// The index of the first unprocessed token in `self.tokens`
271    index: usize,
272    /// The current dialect to use
273    dialect: &'a dyn Dialect,
274    /// Additional options that allow you to mix & match behavior
275    /// otherwise constrained to certain dialects (e.g. trailing
276    /// commas) and/or format of parse (e.g. unescaping)
277    options: ParserOptions,
278    /// ensure the stack does not overflow by limiting recursion depth
279    recursion_counter: RecursionCounter,
280}
281
282impl<'a> Parser<'a> {
283    /// Create a parser for a [`Dialect`]
284    ///
285    /// See also [`Parser::parse_sql`]
286    ///
287    /// Example:
288    /// ```
289    /// # use sqlparser::{parser::{Parser, ParserError}, dialect::GenericDialect};
290    /// # fn main() -> Result<(), ParserError> {
291    /// let dialect = GenericDialect{};
292    /// let statements = Parser::new(&dialect)
293    ///   .try_with_sql("SELECT * FROM foo")?
294    ///   .parse_statements()?;
295    /// # Ok(())
296    /// # }
297    /// ```
298    pub fn new(dialect: &'a dyn Dialect) -> Self {
299        Self {
300            tokens: vec![],
301            index: 0,
302            dialect,
303            recursion_counter: RecursionCounter::new(DEFAULT_REMAINING_DEPTH),
304            options: ParserOptions::default(),
305        }
306    }
307
308    /// Specify the maximum recursion limit while parsing.
309    ///
310    ///
311    /// [`Parser`] prevents stack overflows by returning
312    /// [`ParserError::RecursionLimitExceeded`] if the parser exceeds
313    /// this depth while processing the query.
314    ///
315    /// Example:
316    /// ```
317    /// # use sqlparser::{parser::{Parser, ParserError}, dialect::GenericDialect};
318    /// # fn main() -> Result<(), ParserError> {
319    /// let dialect = GenericDialect{};
320    /// let result = Parser::new(&dialect)
321    ///   .with_recursion_limit(1)
322    ///   .try_with_sql("SELECT * FROM foo WHERE (a OR (b OR (c OR d)))")?
323    ///   .parse_statements();
324    ///   assert_eq!(result, Err(ParserError::RecursionLimitExceeded));
325    /// # Ok(())
326    /// # }
327    /// ```
328    pub fn with_recursion_limit(mut self, recursion_limit: usize) -> Self {
329        self.recursion_counter = RecursionCounter::new(recursion_limit);
330        self
331    }
332
333    /// Specify additional parser options
334    ///
335    ///
336    /// [`Parser`] supports additional options ([`ParserOptions`])
337    /// that allow you to mix & match behavior otherwise constrained
338    /// to certain dialects (e.g. trailing commas).
339    ///
340    /// Example:
341    /// ```
342    /// # use sqlparser::{parser::{Parser, ParserError, ParserOptions}, dialect::GenericDialect};
343    /// # fn main() -> Result<(), ParserError> {
344    /// let dialect = GenericDialect{};
345    /// let options = ParserOptions::new()
346    ///    .with_trailing_commas(true)
347    ///    .with_unescape(false);
348    /// let result = Parser::new(&dialect)
349    ///   .with_options(options)
350    ///   .try_with_sql("SELECT a, b, COUNT(*), FROM foo GROUP BY a, b,")?
351    ///   .parse_statements();
352    ///   assert!(matches!(result, Ok(_)));
353    /// # Ok(())
354    /// # }
355    /// ```
356    pub fn with_options(mut self, options: ParserOptions) -> Self {
357        self.options = options;
358        self
359    }
360
361    /// Reset this parser to parse the specified token stream
362    pub fn with_tokens_with_locations(mut self, tokens: Vec<TokenWithLocation>) -> Self {
363        self.tokens = tokens;
364        self.index = 0;
365        self
366    }
367
368    /// Reset this parser state to parse the specified tokens
369    pub fn with_tokens(self, tokens: Vec<Token>) -> Self {
370        // Put in dummy locations
371        let tokens_with_locations: Vec<TokenWithLocation> = tokens
372            .into_iter()
373            .map(|token| TokenWithLocation {
374                token,
375                location: Location { line: 0, column: 0 },
376            })
377            .collect();
378        self.with_tokens_with_locations(tokens_with_locations)
379    }
380
381    /// Tokenize the sql string and sets this [`Parser`]'s state to
382    /// parse the resulting tokens
383    ///
384    /// Returns an error if there was an error tokenizing the SQL string.
385    ///
386    /// See example on [`Parser::new()`] for an example
387    pub fn try_with_sql(self, sql: &str) -> Result<Self, ParserError> {
388        debug!("Parsing sql '{}'...", sql);
389        let tokens = Tokenizer::new(self.dialect, sql)
390            .with_unescape(self.options.unescape)
391            .tokenize_with_location()?;
392        Ok(self.with_tokens_with_locations(tokens))
393    }
394
395    /// Parse potentially multiple statements
396    ///
397    /// Example
398    /// ```
399    /// # use sqlparser::{parser::{Parser, ParserError}, dialect::GenericDialect};
400    /// # fn main() -> Result<(), ParserError> {
401    /// let dialect = GenericDialect{};
402    /// let statements = Parser::new(&dialect)
403    ///   // Parse a SQL string with 2 separate statements
404    ///   .try_with_sql("SELECT * FROM foo; SELECT * FROM bar;")?
405    ///   .parse_statements()?;
406    /// assert_eq!(statements.len(), 2);
407    /// # Ok(())
408    /// # }
409    /// ```
410    pub fn parse_statements(&mut self) -> Result<Vec<Statement>, ParserError> {
411        let mut stmts = Vec::new();
412        let mut expecting_statement_delimiter = false;
413        loop {
414            // ignore empty statements (between successive statement delimiters)
415            while self.consume_token(&Token::SemiColon) {
416                expecting_statement_delimiter = false;
417            }
418
419            match self.peek_token().token {
420                Token::EOF => break,
421
422                // end of statement
423                Token::Word(word) if word.keyword == Keyword::END => break,
424                _ => {}
425            }
426
427            if expecting_statement_delimiter {
428                return self.expected("end of statement", self.peek_token());
429            }
430
431            let statement = self.parse_statement()?;
432            stmts.push(statement);
433            expecting_statement_delimiter = true;
434        }
435        Ok(stmts)
436    }
437
438    /// Convenience method to parse a string with one or more SQL
439    /// statements into produce an Abstract Syntax Tree (AST).
440    ///
441    /// Example
442    /// ```
443    /// # use sqlparser::{parser::{Parser, ParserError}, dialect::GenericDialect};
444    /// # fn main() -> Result<(), ParserError> {
445    /// let dialect = GenericDialect{};
446    /// let statements = Parser::parse_sql(
447    ///   &dialect, "SELECT * FROM foo"
448    /// )?;
449    /// assert_eq!(statements.len(), 1);
450    /// # Ok(())
451    /// # }
452    /// ```
453    pub fn parse_sql(dialect: &dyn Dialect, sql: &str) -> Result<Vec<Statement>, ParserError> {
454        Parser::new(dialect).try_with_sql(sql)?.parse_statements()
455    }
456
457    /// Parse a single top-level statement (such as SELECT, INSERT, CREATE, etc.),
458    /// stopping before the statement separator, if any.
459    pub fn parse_statement(&mut self) -> Result<Statement, ParserError> {
460        let _guard = self.recursion_counter.try_decrease()?;
461
462        // allow the dialect to override statement parsing
463        if let Some(statement) = self.dialect.parse_statement(self) {
464            return statement;
465        }
466
467        let next_token = self.next_token();
468        match &next_token.token {
469            Token::Word(w) => match w.keyword {
470                Keyword::KILL => Ok(self.parse_kill()?),
471                Keyword::DESCRIBE => Ok(self.parse_explain(true)?),
472                Keyword::EXPLAIN => Ok(self.parse_explain(false)?),
473                Keyword::ANALYZE => Ok(self.parse_analyze()?),
474                Keyword::SELECT | Keyword::WITH | Keyword::VALUES => {
475                    self.prev_token();
476                    Ok(Statement::Query(Box::new(self.parse_query()?)))
477                }
478                Keyword::TRUNCATE => Ok(self.parse_truncate()?),
479                Keyword::ATTACH => Ok(self.parse_attach_database()?),
480                Keyword::MSCK => Ok(self.parse_msck()?),
481                Keyword::CREATE => Ok(self.parse_create()?),
482                Keyword::CACHE => Ok(self.parse_cache_table()?),
483                Keyword::DROP => Ok(self.parse_drop()?),
484                Keyword::DISCARD => Ok(self.parse_discard()?),
485                Keyword::DECLARE => Ok(self.parse_declare()?),
486                Keyword::FETCH => Ok(self.parse_fetch_statement()?),
487                Keyword::DELETE => Ok(self.parse_delete()?),
488                Keyword::INSERT => Ok(self.parse_insert()?),
489                Keyword::UNCACHE => Ok(self.parse_uncache_table()?),
490                Keyword::UPDATE => Ok(self.parse_update()?),
491                Keyword::ALTER => Ok(self.parse_alter()?),
492                Keyword::COPY => Ok(self.parse_copy()?),
493                Keyword::CLOSE => Ok(self.parse_close()?),
494                Keyword::SET => Ok(self.parse_set()?),
495                Keyword::SHOW => Ok(self.parse_show()?),
496                Keyword::USE => Ok(self.parse_use()?),
497                Keyword::GRANT => Ok(self.parse_grant()?),
498                Keyword::REVOKE => Ok(self.parse_revoke()?),
499                Keyword::START => Ok(self.parse_start_transaction()?),
500                // `BEGIN` is a nonstandard but common alias for the
501                // standard `START TRANSACTION` statement. It is supported
502                // by at least PostgreSQL and MySQL.
503                Keyword::BEGIN => Ok(self.parse_begin()?),
504                Keyword::SAVEPOINT => Ok(self.parse_savepoint()?),
505                Keyword::COMMIT => Ok(self.parse_commit()?),
506                Keyword::ROLLBACK => Ok(self.parse_rollback()?),
507                Keyword::ASSERT => Ok(self.parse_assert()?),
508                // `PREPARE`, `EXECUTE` and `DEALLOCATE` are Postgres-specific
509                // syntaxes. They are used for Postgres prepared statement.
510                Keyword::DEALLOCATE => Ok(self.parse_deallocate()?),
511                Keyword::EXECUTE => Ok(self.parse_execute()?),
512                Keyword::PREPARE => Ok(self.parse_prepare()?),
513                Keyword::MERGE => Ok(self.parse_merge()?),
514                // `PRAGMA` is sqlite specific https://www.sqlite.org/pragma.html
515                Keyword::PRAGMA => Ok(self.parse_pragma()?),
516                _ => self.expected("an SQL statement", next_token),
517            },
518            Token::LParen => {
519                self.prev_token();
520                Ok(Statement::Query(Box::new(self.parse_query()?)))
521            }
522            _ => self.expected("an SQL statement", next_token),
523        }
524    }
525
526    pub fn parse_msck(&mut self) -> Result<Statement, ParserError> {
527        let repair = self.parse_keyword(Keyword::REPAIR);
528        self.expect_keyword(Keyword::TABLE)?;
529        let table_name = self.parse_object_name()?;
530        let partition_action = self
531            .maybe_parse(|parser| {
532                let pa = match parser.parse_one_of_keywords(&[
533                    Keyword::ADD,
534                    Keyword::DROP,
535                    Keyword::SYNC,
536                ]) {
537                    Some(Keyword::ADD) => Some(AddDropSync::ADD),
538                    Some(Keyword::DROP) => Some(AddDropSync::DROP),
539                    Some(Keyword::SYNC) => Some(AddDropSync::SYNC),
540                    _ => None,
541                };
542                parser.expect_keyword(Keyword::PARTITIONS)?;
543                Ok(pa)
544            })
545            .unwrap_or_default();
546        Ok(Statement::Msck {
547            repair,
548            table_name,
549            partition_action,
550        })
551    }
552
553    pub fn parse_truncate(&mut self) -> Result<Statement, ParserError> {
554        let table = self.parse_keyword(Keyword::TABLE);
555        let table_name = self.parse_object_name()?;
556        let mut partitions = None;
557        if self.parse_keyword(Keyword::PARTITION) {
558            self.expect_token(&Token::LParen)?;
559            partitions = Some(self.parse_comma_separated(Parser::parse_expr)?);
560            self.expect_token(&Token::RParen)?;
561        }
562        Ok(Statement::Truncate {
563            table_name,
564            partitions,
565            table,
566        })
567    }
568
569    pub fn parse_attach_database(&mut self) -> Result<Statement, ParserError> {
570        let database = self.parse_keyword(Keyword::DATABASE);
571        let database_file_name = self.parse_expr()?;
572        self.expect_keyword(Keyword::AS)?;
573        let schema_name = self.parse_identifier()?;
574        Ok(Statement::AttachDatabase {
575            database,
576            schema_name,
577            database_file_name,
578        })
579    }
580
581    pub fn parse_analyze(&mut self) -> Result<Statement, ParserError> {
582        self.expect_keyword(Keyword::TABLE)?;
583        let table_name = self.parse_object_name()?;
584        let mut for_columns = false;
585        let mut cache_metadata = false;
586        let mut noscan = false;
587        let mut partitions = None;
588        let mut compute_statistics = false;
589        let mut columns = vec![];
590        loop {
591            match self.parse_one_of_keywords(&[
592                Keyword::PARTITION,
593                Keyword::FOR,
594                Keyword::CACHE,
595                Keyword::NOSCAN,
596                Keyword::COMPUTE,
597            ]) {
598                Some(Keyword::PARTITION) => {
599                    self.expect_token(&Token::LParen)?;
600                    partitions = Some(self.parse_comma_separated(Parser::parse_expr)?);
601                    self.expect_token(&Token::RParen)?;
602                }
603                Some(Keyword::NOSCAN) => noscan = true,
604                Some(Keyword::FOR) => {
605                    self.expect_keyword(Keyword::COLUMNS)?;
606
607                    columns = self
608                        .maybe_parse(|parser| {
609                            parser.parse_comma_separated(Parser::parse_identifier)
610                        })
611                        .unwrap_or_default();
612                    for_columns = true
613                }
614                Some(Keyword::CACHE) => {
615                    self.expect_keyword(Keyword::METADATA)?;
616                    cache_metadata = true
617                }
618                Some(Keyword::COMPUTE) => {
619                    self.expect_keyword(Keyword::STATISTICS)?;
620                    compute_statistics = true
621                }
622                _ => break,
623            }
624        }
625
626        Ok(Statement::Analyze {
627            table_name,
628            for_columns,
629            columns,
630            partitions,
631            cache_metadata,
632            noscan,
633            compute_statistics,
634        })
635    }
636
637    /// Parse a new expression including wildcard & qualified wildcard
638    pub fn parse_wildcard_expr(&mut self) -> Result<WildcardExpr, ParserError> {
639        let index = self.index;
640
641        let next_token = self.next_token();
642        match next_token.token {
643            t @ (Token::Word(_) | Token::SingleQuotedString(_)) => {
644                if self.peek_token().token == Token::Period {
645                    let mut id_parts: Vec<Ident> = vec![match t {
646                        Token::Word(w) => w.to_ident(),
647                        Token::SingleQuotedString(s) => Ident::with_quote('\'', s),
648                        _ => unreachable!(), // We matched above
649                    }];
650
651                    while self.consume_token(&Token::Period) {
652                        let next_token = self.next_token();
653                        match next_token.token {
654                            Token::Word(w) => id_parts.push(w.to_ident()),
655                            Token::SingleQuotedString(s) => {
656                                // SQLite has single-quoted identifiers
657                                id_parts.push(Ident::with_quote('\'', s))
658                            }
659                            Token::Mul => {
660                                return Ok(WildcardExpr::QualifiedWildcard(ObjectName(id_parts)));
661                            }
662                            _ => {
663                                return self
664                                    .expected("an identifier or a '*' after '.'", next_token);
665                            }
666                        }
667                    }
668                }
669            }
670            Token::Mul => {
671                return Ok(WildcardExpr::Wildcard);
672            }
673            _ => (),
674        };
675
676        self.index = index;
677        self.parse_expr().map(WildcardExpr::Expr)
678    }
679
680    /// Parse a new expression
681    pub fn parse_expr(&mut self) -> Result<Expr, ParserError> {
682        let _guard = self.recursion_counter.try_decrease()?;
683        self.parse_subexpr(0)
684    }
685
686    /// Parse tokens until the precedence changes
687    pub fn parse_subexpr(&mut self, precedence: u8) -> Result<Expr, ParserError> {
688        debug!("parsing expr");
689        let mut expr = self.parse_prefix()?;
690        debug!("prefix: {:?}", expr);
691        loop {
692            let next_precedence = self.get_next_precedence()?;
693            debug!("next precedence: {:?}", next_precedence);
694
695            if precedence >= next_precedence {
696                break;
697            }
698
699            expr = self.parse_infix(expr, next_precedence)?;
700        }
701        Ok(expr)
702    }
703
704    pub fn parse_interval_expr(&mut self) -> Result<Expr, ParserError> {
705        let precedence = 0;
706        let mut expr = self.parse_prefix()?;
707
708        loop {
709            let next_precedence = self.get_next_interval_precedence()?;
710
711            if precedence >= next_precedence {
712                break;
713            }
714
715            expr = self.parse_infix(expr, next_precedence)?;
716        }
717
718        Ok(expr)
719    }
720
721    /// Get the precedence of the next token
722    /// With AND, OR, and XOR
723    pub fn get_next_interval_precedence(&self) -> Result<u8, ParserError> {
724        let token = self.peek_token();
725
726        match token.token {
727            Token::Word(w) if w.keyword == Keyword::AND => Ok(0),
728            Token::Word(w) if w.keyword == Keyword::OR => Ok(0),
729            Token::Word(w) if w.keyword == Keyword::XOR => Ok(0),
730            _ => self.get_next_precedence(),
731        }
732    }
733
734    pub fn parse_assert(&mut self) -> Result<Statement, ParserError> {
735        let condition = self.parse_expr()?;
736        let message = if self.parse_keyword(Keyword::AS) {
737            Some(self.parse_expr()?)
738        } else {
739            None
740        };
741
742        Ok(Statement::Assert { condition, message })
743    }
744
745    pub fn parse_savepoint(&mut self) -> Result<Statement, ParserError> {
746        let name = self.parse_identifier()?;
747        Ok(Statement::Savepoint { name })
748    }
749
750    /// Parse an expression prefix
751    pub fn parse_prefix(&mut self) -> Result<Expr, ParserError> {
752        // allow the dialect to override prefix parsing
753        if let Some(prefix) = self.dialect.parse_prefix(self) {
754            return prefix;
755        }
756
757        // PostgreSQL allows any string literal to be preceded by a type name, indicating that the
758        // string literal represents a literal of that type. Some examples:
759        //
760        //      DATE '2020-05-20'
761        //      TIMESTAMP WITH TIME ZONE '2020-05-20 7:43:54'
762        //      BOOL 'true'
763        //
764        // The first two are standard SQL, while the latter is a PostgreSQL extension. Complicating
765        // matters is the fact that INTERVAL string literals may optionally be followed by special
766        // keywords, e.g.:
767        //
768        //      INTERVAL '7' DAY
769        //
770        // Note also that naively `SELECT date` looks like a syntax error because the `date` type
771        // name is not followed by a string literal, but in fact in PostgreSQL it is a valid
772        // expression that should parse as the column name "date".
773        let loc = self.peek_token().location;
774        return_ok_if_some!(self.maybe_parse(|parser| {
775            match parser.parse_data_type()? {
776                DataType::Interval => parser.parse_interval(),
777                // PostgreSQL allows almost any identifier to be used as custom data type name,
778                // and we support that in `parse_data_type()`. But unlike Postgres we don't
779                // have a list of globally reserved keywords (since they vary across dialects),
780                // so given `NOT 'a' LIKE 'b'`, we'd accept `NOT` as a possible custom data type
781                // name, resulting in `NOT 'a'` being recognized as a `TypedString` instead of
782                // an unary negation `NOT ('a' LIKE 'b')`. To solve this, we don't accept the
783                // `type 'string'` syntax for the custom data types at all.
784                DataType::Custom(..) => parser_err!("dummy", loc),
785                data_type => Ok(Expr::TypedString {
786                    data_type,
787                    value: parser.parse_literal_string()?,
788                }),
789            }
790        }));
791
792        let next_token = self.next_token();
793        let expr = match next_token.token {
794            Token::Word(w) => match w.keyword {
795                Keyword::TRUE | Keyword::FALSE | Keyword::NULL => {
796                    self.prev_token();
797                    Ok(Expr::Value(self.parse_value()?))
798                }
799                Keyword::CURRENT_CATALOG
800                | Keyword::CURRENT_USER
801                | Keyword::SESSION_USER
802                | Keyword::USER
803                    if dialect_of!(self is PostgreSqlDialect | GenericDialect) =>
804                {
805                    Ok(Expr::Function(Function {
806                        name: ObjectName(vec![w.to_ident()]),
807                        args: vec![],
808                        null_treatment: None,
809                        filter: None,
810                        over: None,
811                        distinct: false,
812                        special: true,
813                        order_by: vec![],
814                    }))
815                }
816                Keyword::CURRENT_TIMESTAMP
817                | Keyword::CURRENT_TIME
818                | Keyword::CURRENT_DATE
819                | Keyword::LOCALTIME
820                | Keyword::LOCALTIMESTAMP => {
821                    self.parse_time_functions(ObjectName(vec![w.to_ident()]))
822                }
823                Keyword::CASE => self.parse_case_expr(),
824                Keyword::CAST => self.parse_cast_expr(),
825                Keyword::TRY_CAST => self.parse_try_cast_expr(),
826                Keyword::SAFE_CAST => self.parse_safe_cast_expr(),
827                Keyword::EXISTS => self.parse_exists_expr(false),
828                Keyword::EXTRACT => self.parse_extract_expr(),
829                Keyword::CEIL => self.parse_ceil_floor_expr(true),
830                Keyword::FLOOR => self.parse_ceil_floor_expr(false),
831                Keyword::POSITION if self.peek_token().token == Token::LParen => {
832                    self.parse_position_expr()
833                }
834                Keyword::SUBSTRING => self.parse_substring_expr(),
835                Keyword::OVERLAY => self.parse_overlay_expr(),
836                Keyword::TRIM => self.parse_trim_expr(),
837                Keyword::INTERVAL => self.parse_interval(),
838                Keyword::LISTAGG => self.parse_listagg_expr(),
839                // Treat ARRAY[1,2,3] as an array [1,2,3], otherwise try as subquery or a function call
840                Keyword::ARRAY if self.peek_token() == Token::LBracket => {
841                    self.expect_token(&Token::LBracket)?;
842                    self.parse_array_expr(true)
843                }
844                Keyword::ARRAY
845                    if self.peek_token() == Token::LParen
846                        && !dialect_of!(self is ClickHouseDialect) =>
847                {
848                    self.expect_token(&Token::LParen)?;
849                    self.parse_array_subquery()
850                }
851                Keyword::ARRAY_AGG => self.parse_array_agg_expr(),
852                Keyword::NOT => self.parse_not(),
853                Keyword::MATCH if dialect_of!(self is MySqlDialect | GenericDialect) => {
854                    self.parse_match_against()
855                }
856                Keyword::STRUCT if dialect_of!(self is BigQueryDialect | GenericDialect) => {
857                    self.prev_token();
858                    self.parse_bigquery_struct_literal()
859                }
860                // Here `w` is a word, check if it's a part of a multi-part
861                // identifier, a function call, or a simple identifier:
862                _ => match self.peek_token().token {
863                    Token::LParen | Token::Period => {
864                        let mut id_parts: Vec<Ident> = vec![w.to_ident()];
865                        while self.consume_token(&Token::Period) {
866                            let next_token = self.next_token();
867                            match next_token.token {
868                                Token::Word(w) => id_parts.push(w.to_ident()),
869                                Token::SingleQuotedString(s) => {
870                                    id_parts.push(Ident::with_quote('\'', s))
871                                }
872                                _ => {
873                                    return self
874                                        .expected("an identifier or a '*' after '.'", next_token);
875                                }
876                            }
877                        }
878
879                        if self.consume_token(&Token::LParen) {
880                            self.prev_token();
881                            self.parse_function(ObjectName(id_parts))
882                        } else {
883                            Ok(Expr::CompoundIdentifier(id_parts))
884                        }
885                    }
886                    // string introducer https://dev.mysql.com/doc/refman/8.0/en/charset-introducer.html
887                    Token::SingleQuotedString(_)
888                    | Token::DoubleQuotedString(_)
889                    | Token::HexStringLiteral(_)
890                        if w.value.starts_with('_') =>
891                    {
892                        Ok(Expr::IntroducedString {
893                            introducer: w.value,
894                            value: self.parse_introduced_string_value()?,
895                        })
896                    }
897                    _ => Ok(Expr::Identifier(w.to_ident())),
898                },
899            }, // End of Token::Word
900            // array `[1, 2, 3]`
901            Token::LBracket => self.parse_array_expr(false),
902            tok @ Token::Minus | tok @ Token::Plus => {
903                let op = if tok == Token::Plus {
904                    UnaryOperator::Plus
905                } else {
906                    UnaryOperator::Minus
907                };
908                Ok(Expr::UnaryOp {
909                    op,
910                    expr: Box::new(self.parse_subexpr(Self::MUL_DIV_MOD_OP_PREC)?),
911                })
912            }
913            tok @ Token::DoubleExclamationMark
914            | tok @ Token::PGSquareRoot
915            | tok @ Token::PGCubeRoot
916            | tok @ Token::AtSign
917            | tok @ Token::Tilde
918                if dialect_of!(self is PostgreSqlDialect) =>
919            {
920                let op = match tok {
921                    Token::DoubleExclamationMark => UnaryOperator::PGPrefixFactorial,
922                    Token::PGSquareRoot => UnaryOperator::PGSquareRoot,
923                    Token::PGCubeRoot => UnaryOperator::PGCubeRoot,
924                    Token::AtSign => UnaryOperator::PGAbs,
925                    Token::Tilde => UnaryOperator::PGBitwiseNot,
926                    _ => unreachable!(),
927                };
928                Ok(Expr::UnaryOp {
929                    op,
930                    expr: Box::new(self.parse_subexpr(Self::PLUS_MINUS_PREC)?),
931                })
932            }
933            Token::EscapedStringLiteral(_) if dialect_of!(self is PostgreSqlDialect | GenericDialect) =>
934            {
935                self.prev_token();
936                Ok(Expr::Value(self.parse_value()?))
937            }
938            Token::Number(_, _)
939            | Token::SingleQuotedString(_)
940            | Token::DoubleQuotedString(_)
941            | Token::DollarQuotedString(_)
942            | Token::SingleQuotedByteStringLiteral(_)
943            | Token::DoubleQuotedByteStringLiteral(_)
944            | Token::RawStringLiteral(_)
945            | Token::NationalStringLiteral(_)
946            | Token::HexStringLiteral(_) => {
947                self.prev_token();
948                Ok(Expr::Value(self.parse_value()?))
949            }
950            Token::LParen => {
951                let expr =
952                    if self.parse_keyword(Keyword::SELECT) || self.parse_keyword(Keyword::WITH) {
953                        self.prev_token();
954                        Expr::Subquery(Box::new(self.parse_query()?))
955                    } else {
956                        let exprs = self.parse_comma_separated(Parser::parse_expr)?;
957                        match exprs.len() {
958                            0 => unreachable!(), // parse_comma_separated ensures 1 or more
959                            1 => Expr::Nested(Box::new(exprs.into_iter().next().unwrap())),
960                            _ => Expr::Tuple(exprs),
961                        }
962                    };
963                self.expect_token(&Token::RParen)?;
964                if !self.consume_token(&Token::Period) {
965                    Ok(expr)
966                } else {
967                    let tok = self.next_token();
968                    let key = match tok.token {
969                        Token::Word(word) => word.to_ident(),
970                        _ => {
971                            return parser_err!(
972                                format!("Expected identifier, found: {tok}"),
973                                tok.location
974                            )
975                        }
976                    };
977                    Ok(Expr::CompositeAccess {
978                        expr: Box::new(expr),
979                        key,
980                    })
981                }
982            }
983            Token::Placeholder(_) | Token::Colon | Token::AtSign => {
984                self.prev_token();
985                Ok(Expr::Value(self.parse_value()?))
986            }
987            _ => self.expected("an expression:", next_token),
988        }?;
989
990        if self.parse_keyword(Keyword::COLLATE) {
991            Ok(Expr::Collate {
992                expr: Box::new(expr),
993                collation: self.parse_object_name()?,
994            })
995        } else {
996            Ok(expr)
997        }
998    }
999
1000    pub fn parse_function(&mut self, name: ObjectName) -> Result<Expr, ParserError> {
1001        self.expect_token(&Token::LParen)?;
1002        let distinct = self.parse_all_or_distinct()?.is_some();
1003        let (args, order_by) = self.parse_optional_args_with_orderby()?;
1004        let filter = if self.dialect.supports_filter_during_aggregation()
1005            && self.parse_keyword(Keyword::FILTER)
1006            && self.consume_token(&Token::LParen)
1007            && self.parse_keyword(Keyword::WHERE)
1008        {
1009            let filter = Some(Box::new(self.parse_expr()?));
1010            self.expect_token(&Token::RParen)?;
1011            filter
1012        } else {
1013            None
1014        };
1015        let null_treatment = match self.parse_one_of_keywords(&[Keyword::RESPECT, Keyword::IGNORE])
1016        {
1017            Some(keyword) => {
1018                self.expect_keyword(Keyword::NULLS)?;
1019
1020                match keyword {
1021                    Keyword::RESPECT => Some(NullTreatment::RespectNulls),
1022                    Keyword::IGNORE => Some(NullTreatment::IgnoreNulls),
1023                    _ => None,
1024                }
1025            }
1026            None => None,
1027        };
1028        let over = if self.parse_keyword(Keyword::OVER) {
1029            if self.consume_token(&Token::LParen) {
1030                let window_spec = self.parse_window_spec()?;
1031                Some(WindowType::WindowSpec(window_spec))
1032            } else {
1033                Some(WindowType::NamedWindow(self.parse_identifier()?))
1034            }
1035        } else {
1036            None
1037        };
1038        Ok(Expr::Function(Function {
1039            name,
1040            args,
1041            null_treatment,
1042            filter,
1043            over,
1044            distinct,
1045            special: false,
1046            order_by,
1047        }))
1048    }
1049
1050    pub fn parse_time_functions(&mut self, name: ObjectName) -> Result<Expr, ParserError> {
1051        let (args, order_by, special) = if self.consume_token(&Token::LParen) {
1052            let (args, order_by) = self.parse_optional_args_with_orderby()?;
1053            (args, order_by, false)
1054        } else {
1055            (vec![], vec![], true)
1056        };
1057        Ok(Expr::Function(Function {
1058            name,
1059            args,
1060            null_treatment: None,
1061            filter: None,
1062            over: None,
1063            distinct: false,
1064            special,
1065            order_by,
1066        }))
1067    }
1068
1069    pub fn parse_window_frame_units(&mut self) -> Result<WindowFrameUnits, ParserError> {
1070        let next_token = self.next_token();
1071        match &next_token.token {
1072            Token::Word(w) => match w.keyword {
1073                Keyword::ROWS => Ok(WindowFrameUnits::Rows),
1074                Keyword::RANGE => Ok(WindowFrameUnits::Range),
1075                Keyword::GROUPS => Ok(WindowFrameUnits::Groups),
1076                _ => self.expected("ROWS, RANGE, GROUPS", next_token)?,
1077            },
1078            _ => self.expected("ROWS, RANGE, GROUPS", next_token),
1079        }
1080    }
1081
1082    pub fn parse_window_frame(&mut self) -> Result<WindowFrame, ParserError> {
1083        let units = self.parse_window_frame_units()?;
1084        let (start_bound, end_bound) = if self.parse_keyword(Keyword::BETWEEN) {
1085            let start_bound = self.parse_window_frame_bound()?;
1086            self.expect_keyword(Keyword::AND)?;
1087            let end_bound = Some(self.parse_window_frame_bound()?);
1088            (start_bound, end_bound)
1089        } else {
1090            (self.parse_window_frame_bound()?, None)
1091        };
1092        Ok(WindowFrame {
1093            units,
1094            start_bound,
1095            end_bound,
1096        })
1097    }
1098
1099    /// Parse `CURRENT ROW` or `{ <positive number> | UNBOUNDED } { PRECEDING | FOLLOWING }`
1100    pub fn parse_window_frame_bound(&mut self) -> Result<WindowFrameBound, ParserError> {
1101        if self.parse_keywords(&[Keyword::CURRENT, Keyword::ROW]) {
1102            Ok(WindowFrameBound::CurrentRow)
1103        } else {
1104            let rows = if self.parse_keyword(Keyword::UNBOUNDED) {
1105                None
1106            } else {
1107                Some(Box::new(match self.peek_token().token {
1108                    Token::SingleQuotedString(_) => self.parse_interval()?,
1109                    _ => self.parse_expr()?,
1110                }))
1111            };
1112            if self.parse_keyword(Keyword::PRECEDING) {
1113                Ok(WindowFrameBound::Preceding(rows))
1114            } else if self.parse_keyword(Keyword::FOLLOWING) {
1115                Ok(WindowFrameBound::Following(rows))
1116            } else {
1117                self.expected("PRECEDING or FOLLOWING", self.peek_token())
1118            }
1119        }
1120    }
1121
1122    /// parse a group by expr. a group by expr can be one of group sets, roll up, cube, or simple
1123    /// expr.
1124    fn parse_group_by_expr(&mut self) -> Result<Expr, ParserError> {
1125        if self.dialect.supports_group_by_expr() {
1126            if self.parse_keywords(&[Keyword::GROUPING, Keyword::SETS]) {
1127                self.expect_token(&Token::LParen)?;
1128                let result = self.parse_comma_separated(|p| p.parse_tuple(false, true))?;
1129                self.expect_token(&Token::RParen)?;
1130                Ok(Expr::GroupingSets(result))
1131            } else if self.parse_keyword(Keyword::CUBE) {
1132                self.expect_token(&Token::LParen)?;
1133                let result = self.parse_comma_separated(|p| p.parse_tuple(true, true))?;
1134                self.expect_token(&Token::RParen)?;
1135                Ok(Expr::Cube(result))
1136            } else if self.parse_keyword(Keyword::ROLLUP) {
1137                self.expect_token(&Token::LParen)?;
1138                let result = self.parse_comma_separated(|p| p.parse_tuple(true, true))?;
1139                self.expect_token(&Token::RParen)?;
1140                Ok(Expr::Rollup(result))
1141            } else {
1142                self.parse_expr()
1143            }
1144        } else {
1145            // TODO parse rollup for other dialects
1146            self.parse_expr()
1147        }
1148    }
1149
1150    /// parse a tuple with `(` and `)`.
1151    /// If `lift_singleton` is true, then a singleton tuple is lifted to a tuple of length 1, otherwise it will fail.
1152    /// If `allow_empty` is true, then an empty tuple is allowed.
1153    fn parse_tuple(
1154        &mut self,
1155        lift_singleton: bool,
1156        allow_empty: bool,
1157    ) -> Result<Vec<Expr>, ParserError> {
1158        if lift_singleton {
1159            if self.consume_token(&Token::LParen) {
1160                let result = if allow_empty && self.consume_token(&Token::RParen) {
1161                    vec![]
1162                } else {
1163                    let result = self.parse_comma_separated(Parser::parse_expr)?;
1164                    self.expect_token(&Token::RParen)?;
1165                    result
1166                };
1167                Ok(result)
1168            } else {
1169                Ok(vec![self.parse_expr()?])
1170            }
1171        } else {
1172            self.expect_token(&Token::LParen)?;
1173            let result = if allow_empty && self.consume_token(&Token::RParen) {
1174                vec![]
1175            } else {
1176                let result = self.parse_comma_separated(Parser::parse_expr)?;
1177                self.expect_token(&Token::RParen)?;
1178                result
1179            };
1180            Ok(result)
1181        }
1182    }
1183
1184    pub fn parse_case_expr(&mut self) -> Result<Expr, ParserError> {
1185        let mut operand = None;
1186        if !self.parse_keyword(Keyword::WHEN) {
1187            operand = Some(Box::new(self.parse_expr()?));
1188            self.expect_keyword(Keyword::WHEN)?;
1189        }
1190        let mut conditions = vec![];
1191        let mut results = vec![];
1192        loop {
1193            conditions.push(self.parse_expr()?);
1194            self.expect_keyword(Keyword::THEN)?;
1195            results.push(self.parse_expr()?);
1196            if !self.parse_keyword(Keyword::WHEN) {
1197                break;
1198            }
1199        }
1200        let else_result = if self.parse_keyword(Keyword::ELSE) {
1201            Some(Box::new(self.parse_expr()?))
1202        } else {
1203            None
1204        };
1205        self.expect_keyword(Keyword::END)?;
1206        Ok(Expr::Case {
1207            operand,
1208            conditions,
1209            results,
1210            else_result,
1211        })
1212    }
1213
1214    pub fn parse_optional_cast_format(&mut self) -> Result<Option<CastFormat>, ParserError> {
1215        if self.parse_keyword(Keyword::FORMAT) {
1216            let value = self.parse_value()?;
1217            if self.parse_keywords(&[Keyword::AT, Keyword::TIME, Keyword::ZONE]) {
1218                Ok(Some(CastFormat::ValueAtTimeZone(
1219                    value,
1220                    self.parse_value()?,
1221                )))
1222            } else {
1223                Ok(Some(CastFormat::Value(value)))
1224            }
1225        } else {
1226            Ok(None)
1227        }
1228    }
1229
1230    /// Parse a SQL CAST function e.g. `CAST(expr AS FLOAT)`
1231    pub fn parse_cast_expr(&mut self) -> Result<Expr, ParserError> {
1232        self.expect_token(&Token::LParen)?;
1233        let expr = self.parse_expr()?;
1234        self.expect_keyword(Keyword::AS)?;
1235        let data_type = self.parse_data_type()?;
1236        let format = self.parse_optional_cast_format()?;
1237        self.expect_token(&Token::RParen)?;
1238        Ok(Expr::Cast {
1239            expr: Box::new(expr),
1240            data_type,
1241            format,
1242        })
1243    }
1244
1245    /// Parse a SQL TRY_CAST function e.g. `TRY_CAST(expr AS FLOAT)`
1246    pub fn parse_try_cast_expr(&mut self) -> Result<Expr, ParserError> {
1247        self.expect_token(&Token::LParen)?;
1248        let expr = self.parse_expr()?;
1249        self.expect_keyword(Keyword::AS)?;
1250        let data_type = self.parse_data_type()?;
1251        let format = self.parse_optional_cast_format()?;
1252        self.expect_token(&Token::RParen)?;
1253        Ok(Expr::TryCast {
1254            expr: Box::new(expr),
1255            data_type,
1256            format,
1257        })
1258    }
1259
1260    /// Parse a BigQuery SAFE_CAST function e.g. `SAFE_CAST(expr AS FLOAT64)`
1261    pub fn parse_safe_cast_expr(&mut self) -> Result<Expr, ParserError> {
1262        self.expect_token(&Token::LParen)?;
1263        let expr = self.parse_expr()?;
1264        self.expect_keyword(Keyword::AS)?;
1265        let data_type = self.parse_data_type()?;
1266        let format = self.parse_optional_cast_format()?;
1267        self.expect_token(&Token::RParen)?;
1268        Ok(Expr::SafeCast {
1269            expr: Box::new(expr),
1270            data_type,
1271            format,
1272        })
1273    }
1274
1275    /// Parse a SQL EXISTS expression e.g. `WHERE EXISTS(SELECT ...)`.
1276    pub fn parse_exists_expr(&mut self, negated: bool) -> Result<Expr, ParserError> {
1277        self.expect_token(&Token::LParen)?;
1278        let exists_node = Expr::Exists {
1279            negated,
1280            subquery: Box::new(self.parse_query()?),
1281        };
1282        self.expect_token(&Token::RParen)?;
1283        Ok(exists_node)
1284    }
1285
1286    pub fn parse_extract_expr(&mut self) -> Result<Expr, ParserError> {
1287        self.expect_token(&Token::LParen)?;
1288        let field = self.parse_date_time_field()?;
1289        self.expect_keyword(Keyword::FROM)?;
1290        let expr = self.parse_expr()?;
1291        self.expect_token(&Token::RParen)?;
1292        Ok(Expr::Extract {
1293            field,
1294            expr: Box::new(expr),
1295        })
1296    }
1297
1298    pub fn parse_ceil_floor_expr(&mut self, is_ceil: bool) -> Result<Expr, ParserError> {
1299        self.expect_token(&Token::LParen)?;
1300        let expr = self.parse_expr()?;
1301        // Parse `CEIL/FLOOR(expr)`
1302        let mut field = DateTimeField::NoDateTime;
1303        let keyword_to = self.parse_keyword(Keyword::TO);
1304        if keyword_to {
1305            // Parse `CEIL/FLOOR(expr TO DateTimeField)`
1306            field = self.parse_date_time_field()?;
1307        }
1308        self.expect_token(&Token::RParen)?;
1309        if is_ceil {
1310            Ok(Expr::Ceil {
1311                expr: Box::new(expr),
1312                field,
1313            })
1314        } else {
1315            Ok(Expr::Floor {
1316                expr: Box::new(expr),
1317                field,
1318            })
1319        }
1320    }
1321
1322    pub fn parse_position_expr(&mut self) -> Result<Expr, ParserError> {
1323        // PARSE SELECT POSITION('@' in field)
1324        self.expect_token(&Token::LParen)?;
1325
1326        // Parse the subexpr till the IN keyword
1327        let expr = self.parse_subexpr(Self::BETWEEN_PREC)?;
1328        if self.parse_keyword(Keyword::IN) {
1329            let from = self.parse_expr()?;
1330            self.expect_token(&Token::RParen)?;
1331            Ok(Expr::Position {
1332                expr: Box::new(expr),
1333                r#in: Box::new(from),
1334            })
1335        } else {
1336            parser_err!(
1337                "Position function must include IN keyword".to_string(),
1338                self.peek_token().location
1339            )
1340        }
1341    }
1342
1343    pub fn parse_substring_expr(&mut self) -> Result<Expr, ParserError> {
1344        if self.dialect.supports_substring_from_for_expr() {
1345            // PARSE SUBSTRING (EXPR [FROM 1] [FOR 3])
1346            self.expect_token(&Token::LParen)?;
1347            let expr = self.parse_expr()?;
1348            let mut from_expr = None;
1349            if self.parse_keyword(Keyword::FROM) || self.consume_token(&Token::Comma) {
1350                from_expr = Some(self.parse_expr()?);
1351            }
1352
1353            let mut to_expr = None;
1354            if self.parse_keyword(Keyword::FOR) || self.consume_token(&Token::Comma) {
1355                to_expr = Some(self.parse_expr()?);
1356            }
1357            self.expect_token(&Token::RParen)?;
1358
1359            Ok(Expr::Substring {
1360                expr: Box::new(expr),
1361                substring_from: from_expr.map(Box::new),
1362                substring_for: to_expr.map(Box::new),
1363                special: false,
1364            })
1365        } else {
1366            // PARSE SUBSTRING(EXPR, start, length)
1367            self.expect_token(&Token::LParen)?;
1368            let expr = self.parse_expr()?;
1369
1370            self.expect_token(&Token::Comma)?;
1371            let from_expr = Some(self.parse_expr()?);
1372
1373            self.expect_token(&Token::Comma)?;
1374            let to_expr = Some(self.parse_expr()?);
1375
1376            self.expect_token(&Token::RParen)?;
1377
1378            Ok(Expr::Substring {
1379                expr: Box::new(expr),
1380                substring_from: from_expr.map(Box::new),
1381                substring_for: to_expr.map(Box::new),
1382                special: true,
1383            })
1384        }
1385    }
1386
1387    pub fn parse_overlay_expr(&mut self) -> Result<Expr, ParserError> {
1388        // PARSE OVERLAY (EXPR PLACING EXPR FROM 1 [FOR 3])
1389        self.expect_token(&Token::LParen)?;
1390        let expr = self.parse_expr()?;
1391        self.expect_keyword(Keyword::PLACING)?;
1392        let what_expr = self.parse_expr()?;
1393        self.expect_keyword(Keyword::FROM)?;
1394        let from_expr = self.parse_expr()?;
1395        let mut for_expr = None;
1396        if self.parse_keyword(Keyword::FOR) {
1397            for_expr = Some(self.parse_expr()?);
1398        }
1399        self.expect_token(&Token::RParen)?;
1400
1401        Ok(Expr::Overlay {
1402            expr: Box::new(expr),
1403            overlay_what: Box::new(what_expr),
1404            overlay_from: Box::new(from_expr),
1405            overlay_for: for_expr.map(Box::new),
1406        })
1407    }
1408
1409    /// ```sql
1410    /// TRIM ([WHERE] ['text' FROM] 'text')
1411    /// TRIM ('text')
1412    /// TRIM(<expr>, [, characters]) -- only Snowflake or BigQuery
1413    /// ```
1414    pub fn parse_trim_expr(&mut self) -> Result<Expr, ParserError> {
1415        self.expect_token(&Token::LParen)?;
1416        let mut trim_where = None;
1417        if let Token::Word(word) = self.peek_token().token {
1418            if [Keyword::BOTH, Keyword::LEADING, Keyword::TRAILING]
1419                .iter()
1420                .any(|d| word.keyword == *d)
1421            {
1422                trim_where = Some(self.parse_trim_where()?);
1423            }
1424        }
1425        let expr = self.parse_expr()?;
1426        if self.parse_keyword(Keyword::FROM) {
1427            let trim_what = Box::new(expr);
1428            let expr = self.parse_expr()?;
1429            self.expect_token(&Token::RParen)?;
1430            Ok(Expr::Trim {
1431                expr: Box::new(expr),
1432                trim_where,
1433                trim_what: Some(trim_what),
1434                trim_characters: None,
1435            })
1436        } else if self.consume_token(&Token::Comma)
1437            && dialect_of!(self is SnowflakeDialect | BigQueryDialect | GenericDialect)
1438        {
1439            let characters = self.parse_comma_separated(Parser::parse_expr)?;
1440            self.expect_token(&Token::RParen)?;
1441            Ok(Expr::Trim {
1442                expr: Box::new(expr),
1443                trim_where: None,
1444                trim_what: None,
1445                trim_characters: Some(characters),
1446            })
1447        } else {
1448            self.expect_token(&Token::RParen)?;
1449            Ok(Expr::Trim {
1450                expr: Box::new(expr),
1451                trim_where,
1452                trim_what: None,
1453                trim_characters: None,
1454            })
1455        }
1456    }
1457
1458    pub fn parse_trim_where(&mut self) -> Result<TrimWhereField, ParserError> {
1459        let next_token = self.next_token();
1460        match &next_token.token {
1461            Token::Word(w) => match w.keyword {
1462                Keyword::BOTH => Ok(TrimWhereField::Both),
1463                Keyword::LEADING => Ok(TrimWhereField::Leading),
1464                Keyword::TRAILING => Ok(TrimWhereField::Trailing),
1465                _ => self.expected("trim_where field", next_token)?,
1466            },
1467            _ => self.expected("trim_where field", next_token),
1468        }
1469    }
1470
1471    /// Parses an array expression `[ex1, ex2, ..]`
1472    /// if `named` is `true`, came from an expression like  `ARRAY[ex1, ex2]`
1473    pub fn parse_array_expr(&mut self, named: bool) -> Result<Expr, ParserError> {
1474        if self.peek_token().token == Token::RBracket {
1475            let _ = self.next_token(); // consume ]
1476            Ok(Expr::Array(Array {
1477                elem: vec![],
1478                named,
1479            }))
1480        } else {
1481            let exprs = self.parse_comma_separated(Parser::parse_expr)?;
1482            self.expect_token(&Token::RBracket)?;
1483            Ok(Expr::Array(Array { elem: exprs, named }))
1484        }
1485    }
1486
1487    // Parses an array constructed from a subquery
1488    pub fn parse_array_subquery(&mut self) -> Result<Expr, ParserError> {
1489        let query = self.parse_query()?;
1490        self.expect_token(&Token::RParen)?;
1491        Ok(Expr::ArraySubquery(Box::new(query)))
1492    }
1493
1494    /// Parse a SQL LISTAGG expression, e.g. `LISTAGG(...) WITHIN GROUP (ORDER BY ...)`.
1495    pub fn parse_listagg_expr(&mut self) -> Result<Expr, ParserError> {
1496        self.expect_token(&Token::LParen)?;
1497        let distinct = self.parse_all_or_distinct()?.is_some();
1498        let expr = Box::new(self.parse_expr()?);
1499        // While ANSI SQL would would require the separator, Redshift makes this optional. Here we
1500        // choose to make the separator optional as this provides the more general implementation.
1501        let separator = if self.consume_token(&Token::Comma) {
1502            Some(Box::new(self.parse_expr()?))
1503        } else {
1504            None
1505        };
1506        let on_overflow = if self.parse_keywords(&[Keyword::ON, Keyword::OVERFLOW]) {
1507            if self.parse_keyword(Keyword::ERROR) {
1508                Some(ListAggOnOverflow::Error)
1509            } else {
1510                self.expect_keyword(Keyword::TRUNCATE)?;
1511                let filler = match self.peek_token().token {
1512                    Token::Word(w)
1513                        if w.keyword == Keyword::WITH || w.keyword == Keyword::WITHOUT =>
1514                    {
1515                        None
1516                    }
1517                    Token::SingleQuotedString(_)
1518                    | Token::EscapedStringLiteral(_)
1519                    | Token::NationalStringLiteral(_)
1520                    | Token::HexStringLiteral(_) => Some(Box::new(self.parse_expr()?)),
1521                    _ => self.expected(
1522                        "either filler, WITH, or WITHOUT in LISTAGG",
1523                        self.peek_token(),
1524                    )?,
1525                };
1526                let with_count = self.parse_keyword(Keyword::WITH);
1527                if !with_count && !self.parse_keyword(Keyword::WITHOUT) {
1528                    self.expected("either WITH or WITHOUT in LISTAGG", self.peek_token())?;
1529                }
1530                self.expect_keyword(Keyword::COUNT)?;
1531                Some(ListAggOnOverflow::Truncate { filler, with_count })
1532            }
1533        } else {
1534            None
1535        };
1536        self.expect_token(&Token::RParen)?;
1537        // Once again ANSI SQL requires WITHIN GROUP, but Redshift does not. Again we choose the
1538        // more general implementation.
1539        let within_group = if self.parse_keywords(&[Keyword::WITHIN, Keyword::GROUP]) {
1540            self.expect_token(&Token::LParen)?;
1541            self.expect_keywords(&[Keyword::ORDER, Keyword::BY])?;
1542            let order_by_expr = self.parse_comma_separated(Parser::parse_order_by_expr)?;
1543            self.expect_token(&Token::RParen)?;
1544            order_by_expr
1545        } else {
1546            vec![]
1547        };
1548        Ok(Expr::ListAgg(ListAgg {
1549            distinct,
1550            expr,
1551            separator,
1552            on_overflow,
1553            within_group,
1554        }))
1555    }
1556
1557    pub fn parse_array_agg_expr(&mut self) -> Result<Expr, ParserError> {
1558        self.expect_token(&Token::LParen)?;
1559        let distinct = self.parse_keyword(Keyword::DISTINCT);
1560        let expr = Box::new(self.parse_expr()?);
1561        // ANSI SQL and BigQuery define ORDER BY inside function.
1562        if !self.dialect.supports_within_after_array_aggregation() {
1563            let order_by = if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
1564                Some(self.parse_comma_separated(Parser::parse_order_by_expr)?)
1565            } else {
1566                None
1567            };
1568            let limit = if self.parse_keyword(Keyword::LIMIT) {
1569                self.parse_limit()?.map(Box::new)
1570            } else {
1571                None
1572            };
1573            self.expect_token(&Token::RParen)?;
1574            return Ok(Expr::ArrayAgg(ArrayAgg {
1575                distinct,
1576                expr,
1577                order_by,
1578                limit,
1579                within_group: false,
1580            }));
1581        }
1582        // Snowflake defines ORDER BY in within group instead of inside the function like
1583        // ANSI SQL.
1584        self.expect_token(&Token::RParen)?;
1585        let within_group = if self.parse_keywords(&[Keyword::WITHIN, Keyword::GROUP]) {
1586            self.expect_token(&Token::LParen)?;
1587            let order_by = if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
1588                Some(self.parse_comma_separated(Parser::parse_order_by_expr)?)
1589            } else {
1590                None
1591            };
1592            self.expect_token(&Token::RParen)?;
1593            order_by
1594        } else {
1595            None
1596        };
1597
1598        Ok(Expr::ArrayAgg(ArrayAgg {
1599            distinct,
1600            expr,
1601            order_by: within_group,
1602            limit: None,
1603            within_group: true,
1604        }))
1605    }
1606
1607    // This function parses date/time fields for the EXTRACT function-like
1608    // operator, interval qualifiers, and the ceil/floor operations.
1609    // EXTRACT supports a wider set of date/time fields than interval qualifiers,
1610    // so this function may need to be split in two.
1611    pub fn parse_date_time_field(&mut self) -> Result<DateTimeField, ParserError> {
1612        let next_token = self.next_token();
1613        match &next_token.token {
1614            Token::Word(w) => match w.keyword {
1615                Keyword::YEAR => Ok(DateTimeField::Year),
1616                Keyword::MONTH => Ok(DateTimeField::Month),
1617                Keyword::WEEK => Ok(DateTimeField::Week),
1618                Keyword::DAY => Ok(DateTimeField::Day),
1619                Keyword::DAYOFWEEK => Ok(DateTimeField::DayOfWeek),
1620                Keyword::DAYOFYEAR => Ok(DateTimeField::DayOfYear),
1621                Keyword::DATE => Ok(DateTimeField::Date),
1622                Keyword::HOUR => Ok(DateTimeField::Hour),
1623                Keyword::MINUTE => Ok(DateTimeField::Minute),
1624                Keyword::SECOND => Ok(DateTimeField::Second),
1625                Keyword::CENTURY => Ok(DateTimeField::Century),
1626                Keyword::DECADE => Ok(DateTimeField::Decade),
1627                Keyword::DOY => Ok(DateTimeField::Doy),
1628                Keyword::DOW => Ok(DateTimeField::Dow),
1629                Keyword::EPOCH => Ok(DateTimeField::Epoch),
1630                Keyword::ISODOW => Ok(DateTimeField::Isodow),
1631                Keyword::ISOYEAR => Ok(DateTimeField::Isoyear),
1632                Keyword::ISOWEEK => Ok(DateTimeField::IsoWeek),
1633                Keyword::JULIAN => Ok(DateTimeField::Julian),
1634                Keyword::MICROSECOND => Ok(DateTimeField::Microsecond),
1635                Keyword::MICROSECONDS => Ok(DateTimeField::Microseconds),
1636                Keyword::MILLENIUM => Ok(DateTimeField::Millenium),
1637                Keyword::MILLENNIUM => Ok(DateTimeField::Millennium),
1638                Keyword::MILLISECOND => Ok(DateTimeField::Millisecond),
1639                Keyword::MILLISECONDS => Ok(DateTimeField::Milliseconds),
1640                Keyword::NANOSECOND => Ok(DateTimeField::Nanosecond),
1641                Keyword::NANOSECONDS => Ok(DateTimeField::Nanoseconds),
1642                Keyword::QUARTER => Ok(DateTimeField::Quarter),
1643                Keyword::TIME => Ok(DateTimeField::Time),
1644                Keyword::TIMEZONE => Ok(DateTimeField::Timezone),
1645                Keyword::TIMEZONE_HOUR => Ok(DateTimeField::TimezoneHour),
1646                Keyword::TIMEZONE_MINUTE => Ok(DateTimeField::TimezoneMinute),
1647                _ => self.expected("date/time field", next_token),
1648            },
1649            _ => self.expected("date/time field", next_token),
1650        }
1651    }
1652
1653    pub fn parse_not(&mut self) -> Result<Expr, ParserError> {
1654        match self.peek_token().token {
1655            Token::Word(w) => match w.keyword {
1656                Keyword::EXISTS => {
1657                    let negated = true;
1658                    let _ = self.parse_keyword(Keyword::EXISTS);
1659                    self.parse_exists_expr(negated)
1660                }
1661                _ => Ok(Expr::UnaryOp {
1662                    op: UnaryOperator::Not,
1663                    expr: Box::new(self.parse_subexpr(Self::UNARY_NOT_PREC)?),
1664                }),
1665            },
1666            _ => Ok(Expr::UnaryOp {
1667                op: UnaryOperator::Not,
1668                expr: Box::new(self.parse_subexpr(Self::UNARY_NOT_PREC)?),
1669            }),
1670        }
1671    }
1672
1673    /// Parses fulltext expressions [(1)]
1674    ///
1675    /// # Errors
1676    /// This method will raise an error if the column list is empty or with invalid identifiers,
1677    /// the match expression is not a literal string, or if the search modifier is not valid.
1678    ///
1679    /// [(1)]: Expr::MatchAgainst
1680    pub fn parse_match_against(&mut self) -> Result<Expr, ParserError> {
1681        let columns = self.parse_parenthesized_column_list(Mandatory, false)?;
1682
1683        self.expect_keyword(Keyword::AGAINST)?;
1684
1685        self.expect_token(&Token::LParen)?;
1686
1687        // MySQL is too permissive about the value, IMO we can't validate it perfectly on syntax level.
1688        let match_value = self.parse_value()?;
1689
1690        let in_natural_language_mode_keywords = &[
1691            Keyword::IN,
1692            Keyword::NATURAL,
1693            Keyword::LANGUAGE,
1694            Keyword::MODE,
1695        ];
1696
1697        let with_query_expansion_keywords = &[Keyword::WITH, Keyword::QUERY, Keyword::EXPANSION];
1698
1699        let in_boolean_mode_keywords = &[Keyword::IN, Keyword::BOOLEAN, Keyword::MODE];
1700
1701        let opt_search_modifier = if self.parse_keywords(in_natural_language_mode_keywords) {
1702            if self.parse_keywords(with_query_expansion_keywords) {
1703                Some(SearchModifier::InNaturalLanguageModeWithQueryExpansion)
1704            } else {
1705                Some(SearchModifier::InNaturalLanguageMode)
1706            }
1707        } else if self.parse_keywords(in_boolean_mode_keywords) {
1708            Some(SearchModifier::InBooleanMode)
1709        } else if self.parse_keywords(with_query_expansion_keywords) {
1710            Some(SearchModifier::WithQueryExpansion)
1711        } else {
1712            None
1713        };
1714
1715        self.expect_token(&Token::RParen)?;
1716
1717        Ok(Expr::MatchAgainst {
1718            columns,
1719            match_value,
1720            opt_search_modifier,
1721        })
1722    }
1723
1724    /// Parse an INTERVAL expression.
1725    ///
1726    /// Some syntactically valid intervals:
1727    ///
1728    ///   1. `INTERVAL '1' DAY`
1729    ///   2. `INTERVAL '1-1' YEAR TO MONTH`
1730    ///   3. `INTERVAL '1' SECOND`
1731    ///   4. `INTERVAL '1:1:1.1' HOUR (5) TO SECOND (5)`
1732    ///   5. `INTERVAL '1.1' SECOND (2, 2)`
1733    ///   6. `INTERVAL '1:1' HOUR (5) TO MINUTE (5)`
1734    ///   7. (MySql and BigQuey only):`INTERVAL 1 DAY`
1735    ///
1736    /// Note that we do not currently attempt to parse the quoted value.
1737    pub fn parse_interval(&mut self) -> Result<Expr, ParserError> {
1738        // The SQL standard allows an optional sign before the value string, but
1739        // it is not clear if any implementations support that syntax, so we
1740        // don't currently try to parse it. (The sign can instead be included
1741        // inside the value string.)
1742
1743        // The first token in an interval is a string literal which specifies
1744        // the duration of the interval.
1745        let value = self.parse_interval_expr()?;
1746
1747        // Following the string literal is a qualifier which indicates the units
1748        // of the duration specified in the string literal.
1749        //
1750        // Note that PostgreSQL allows omitting the qualifier, so we provide
1751        // this more general implementation.
1752        let leading_field = match self.peek_token().token {
1753            Token::Word(kw)
1754                if [
1755                    Keyword::YEAR,
1756                    Keyword::MONTH,
1757                    Keyword::WEEK,
1758                    Keyword::DAY,
1759                    Keyword::HOUR,
1760                    Keyword::MINUTE,
1761                    Keyword::SECOND,
1762                    Keyword::CENTURY,
1763                    Keyword::DECADE,
1764                    Keyword::DOW,
1765                    Keyword::DOY,
1766                    Keyword::EPOCH,
1767                    Keyword::ISODOW,
1768                    Keyword::ISOYEAR,
1769                    Keyword::JULIAN,
1770                    Keyword::MICROSECOND,
1771                    Keyword::MICROSECONDS,
1772                    Keyword::MILLENIUM,
1773                    Keyword::MILLENNIUM,
1774                    Keyword::MILLISECOND,
1775                    Keyword::MILLISECONDS,
1776                    Keyword::NANOSECOND,
1777                    Keyword::NANOSECONDS,
1778                    Keyword::QUARTER,
1779                    Keyword::TIMEZONE,
1780                    Keyword::TIMEZONE_HOUR,
1781                    Keyword::TIMEZONE_MINUTE,
1782                ]
1783                .iter()
1784                .any(|d| kw.keyword == *d) =>
1785            {
1786                Some(self.parse_date_time_field()?)
1787            }
1788            _ => None,
1789        };
1790
1791        let (leading_precision, last_field, fsec_precision) =
1792            if leading_field == Some(DateTimeField::Second) {
1793                // SQL mandates special syntax for `SECOND TO SECOND` literals.
1794                // Instead of
1795                //     `SECOND [(<leading precision>)] TO SECOND[(<fractional seconds precision>)]`
1796                // one must use the special format:
1797                //     `SECOND [( <leading precision> [ , <fractional seconds precision>] )]`
1798                let last_field = None;
1799                let (leading_precision, fsec_precision) = self.parse_optional_precision_scale()?;
1800                (leading_precision, last_field, fsec_precision)
1801            } else {
1802                let leading_precision = self.parse_optional_precision()?;
1803                if self.parse_keyword(Keyword::TO) {
1804                    let last_field = Some(self.parse_date_time_field()?);
1805                    let fsec_precision = if last_field == Some(DateTimeField::Second) {
1806                        self.parse_optional_precision()?
1807                    } else {
1808                        None
1809                    };
1810                    (leading_precision, last_field, fsec_precision)
1811                } else {
1812                    (leading_precision, None, None)
1813                }
1814            };
1815
1816        Ok(Expr::Interval(Interval {
1817            value: Box::new(value),
1818            leading_field,
1819            leading_precision,
1820            last_field,
1821            fractional_seconds_precision: fsec_precision,
1822        }))
1823    }
1824
1825    /// Bigquery specific: Parse a struct literal
1826    /// Syntax
1827    /// ```sql
1828    /// -- typed
1829    /// STRUCT<[field_name] field_type, ...>( expr1 [, ... ])
1830    /// -- typeless
1831    /// STRUCT( expr1 [AS field_name] [, ... ])
1832    /// ```
1833    fn parse_bigquery_struct_literal(&mut self) -> Result<Expr, ParserError> {
1834        let (fields, trailing_bracket) =
1835            self.parse_struct_type_def(Self::parse_big_query_struct_field_def)?;
1836        if trailing_bracket.0 {
1837            return parser_err!("unmatched > in STRUCT literal", self.peek_token().location);
1838        }
1839
1840        self.expect_token(&Token::LParen)?;
1841        let values = self
1842            .parse_comma_separated(|parser| parser.parse_struct_field_expr(!fields.is_empty()))?;
1843        self.expect_token(&Token::RParen)?;
1844
1845        Ok(Expr::Struct { values, fields })
1846    }
1847
1848    /// Parse an expression value for a bigquery struct [1]
1849    /// Syntax
1850    /// ```sql
1851    /// expr [AS name]
1852    /// ```
1853    ///
1854    /// Parameter typed_syntax is set to true if the expression
1855    /// is to be parsed as a field expression declared using typed
1856    /// struct syntax [2], and false if using typeless struct syntax [3].
1857    ///
1858    /// [1]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#constructing_a_struct
1859    /// [2]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#typed_struct_syntax
1860    /// [3]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#typeless_struct_syntax
1861    fn parse_struct_field_expr(&mut self, typed_syntax: bool) -> Result<Expr, ParserError> {
1862        let expr = self.parse_expr()?;
1863        if self.parse_keyword(Keyword::AS) {
1864            if typed_syntax {
1865                return parser_err!("Typed syntax does not allow AS", {
1866                    self.prev_token();
1867                    self.peek_token().location
1868                });
1869            }
1870            let field_name = self.parse_identifier()?;
1871            Ok(Expr::Named {
1872                expr: expr.into(),
1873                name: field_name,
1874            })
1875        } else {
1876            Ok(expr)
1877        }
1878    }
1879
1880    /// Parse a Struct type definition as a sequence of field-value pairs.
1881    /// The syntax of the Struct elem differs by dialect so it is customised
1882    /// by the `elem_parser` argument.
1883    ///
1884    /// Syntax
1885    /// ```sql
1886    /// Hive:
1887    /// STRUCT<field_name: field_type>
1888    ///
1889    /// BigQuery:
1890    /// STRUCT<[field_name] field_type>
1891    /// ```
1892    fn parse_struct_type_def<F>(
1893        &mut self,
1894        mut elem_parser: F,
1895    ) -> Result<(Vec<StructField>, MatchedTrailingBracket), ParserError>
1896    where
1897        F: FnMut(&mut Parser<'a>) -> Result<(StructField, MatchedTrailingBracket), ParserError>,
1898    {
1899        let start_token = self.peek_token();
1900        self.expect_keyword(Keyword::STRUCT)?;
1901
1902        // Nothing to do if we have no type information.
1903        if Token::Lt != self.peek_token() {
1904            return Ok((Default::default(), false.into()));
1905        }
1906        self.next_token();
1907
1908        let mut field_defs = vec![];
1909        let trailing_bracket = loop {
1910            let (def, trailing_bracket) = elem_parser(self)?;
1911            field_defs.push(def);
1912            if !self.consume_token(&Token::Comma) {
1913                break trailing_bracket;
1914            }
1915
1916            // Angle brackets are balanced so we only expect the trailing `>>` after
1917            // we've matched all field types for the current struct.
1918            // e.g. this is invalid syntax `STRUCT<STRUCT<INT>>>, INT>(NULL)`
1919            if trailing_bracket.0 {
1920                return parser_err!("unmatched > in STRUCT definition", start_token.location);
1921            }
1922        };
1923
1924        Ok((
1925            field_defs,
1926            self.expect_closing_angle_bracket(trailing_bracket)?,
1927        ))
1928    }
1929
1930    /// Parse a field definition in a BigQuery struct.
1931    /// Syntax:
1932    ///
1933    /// ```sql
1934    /// [field_name] field_type
1935    /// ```
1936    fn parse_big_query_struct_field_def(
1937        &mut self,
1938    ) -> Result<(StructField, MatchedTrailingBracket), ParserError> {
1939        let is_anonymous_field = if let Token::Word(w) = self.peek_token().token {
1940            ALL_KEYWORDS
1941                .binary_search(&w.value.to_uppercase().as_str())
1942                .is_ok()
1943        } else {
1944            false
1945        };
1946
1947        let field_name = if is_anonymous_field {
1948            None
1949        } else {
1950            Some(self.parse_identifier()?)
1951        };
1952
1953        let (field_type, trailing_bracket) = self.parse_data_type_helper()?;
1954
1955        Ok((
1956            StructField {
1957                field_name,
1958                field_type,
1959            },
1960            trailing_bracket,
1961        ))
1962    }
1963
1964    /// For nested types that use the angle bracket syntax, this matches either
1965    /// `>`, `>>` or nothing depending on which variant is expected (specified by the previously
1966    /// matched `trailing_bracket` argument). It returns whether there is a trailing
1967    /// left to be matched - (i.e. if '>>' was matched).
1968    fn expect_closing_angle_bracket(
1969        &mut self,
1970        trailing_bracket: MatchedTrailingBracket,
1971    ) -> Result<MatchedTrailingBracket, ParserError> {
1972        let trailing_bracket = if !trailing_bracket.0 {
1973            match self.peek_token().token {
1974                Token::Gt => {
1975                    self.next_token();
1976                    false.into()
1977                }
1978                Token::ShiftRight => {
1979                    self.next_token();
1980                    true.into()
1981                }
1982                _ => return self.expected(">", self.peek_token()),
1983            }
1984        } else {
1985            false.into()
1986        };
1987
1988        Ok(trailing_bracket)
1989    }
1990
1991    /// Parse an operator following an expression
1992    pub fn parse_infix(&mut self, expr: Expr, precedence: u8) -> Result<Expr, ParserError> {
1993        // allow the dialect to override infix parsing
1994        if let Some(infix) = self.dialect.parse_infix(self, &expr, precedence) {
1995            return infix;
1996        }
1997
1998        let tok = self.next_token();
1999
2000        let regular_binary_operator = match &tok.token {
2001            Token::Spaceship => Some(BinaryOperator::Spaceship),
2002            Token::DoubleEq => Some(BinaryOperator::Eq),
2003            Token::Eq => Some(BinaryOperator::Eq),
2004            Token::Neq => Some(BinaryOperator::NotEq),
2005            Token::Gt => Some(BinaryOperator::Gt),
2006            Token::GtEq => Some(BinaryOperator::GtEq),
2007            Token::Lt => Some(BinaryOperator::Lt),
2008            Token::LtEq => Some(BinaryOperator::LtEq),
2009            Token::Plus => Some(BinaryOperator::Plus),
2010            Token::Minus => Some(BinaryOperator::Minus),
2011            Token::Mul => Some(BinaryOperator::Multiply),
2012            Token::Mod => Some(BinaryOperator::Modulo),
2013            Token::StringConcat => Some(BinaryOperator::StringConcat),
2014            Token::Pipe => Some(BinaryOperator::BitwiseOr),
2015            Token::Caret => {
2016                // In PostgreSQL, ^ stands for the exponentiation operation,
2017                // and # stands for XOR. See https://www.postgresql.org/docs/current/functions-math.html
2018                if dialect_of!(self is PostgreSqlDialect) {
2019                    Some(BinaryOperator::PGExp)
2020                } else {
2021                    Some(BinaryOperator::BitwiseXor)
2022                }
2023            }
2024            Token::Ampersand => Some(BinaryOperator::BitwiseAnd),
2025            Token::Div => Some(BinaryOperator::Divide),
2026            Token::DuckIntDiv if dialect_of!(self is DuckDbDialect | GenericDialect) => {
2027                Some(BinaryOperator::DuckIntegerDivide)
2028            }
2029            Token::ShiftLeft if dialect_of!(self is PostgreSqlDialect | DuckDbDialect | GenericDialect) => {
2030                Some(BinaryOperator::PGBitwiseShiftLeft)
2031            }
2032            Token::ShiftRight if dialect_of!(self is PostgreSqlDialect | DuckDbDialect | GenericDialect) => {
2033                Some(BinaryOperator::PGBitwiseShiftRight)
2034            }
2035            Token::Sharp if dialect_of!(self is PostgreSqlDialect) => {
2036                Some(BinaryOperator::PGBitwiseXor)
2037            }
2038            Token::Overlap if dialect_of!(self is PostgreSqlDialect | GenericDialect) => {
2039                Some(BinaryOperator::PGOverlap)
2040            }
2041            Token::Tilde => Some(BinaryOperator::PGRegexMatch),
2042            Token::TildeAsterisk => Some(BinaryOperator::PGRegexIMatch),
2043            Token::ExclamationMarkTilde => Some(BinaryOperator::PGRegexNotMatch),
2044            Token::ExclamationMarkTildeAsterisk => Some(BinaryOperator::PGRegexNotIMatch),
2045            Token::Word(w) => match w.keyword {
2046                Keyword::AND => Some(BinaryOperator::And),
2047                Keyword::OR => Some(BinaryOperator::Or),
2048                Keyword::XOR => Some(BinaryOperator::Xor),
2049                Keyword::OPERATOR if dialect_of!(self is PostgreSqlDialect | GenericDialect) => {
2050                    self.expect_token(&Token::LParen)?;
2051                    // there are special rules for operator names in
2052                    // postgres so we can not use 'parse_object'
2053                    // or similar.
2054                    // See https://www.postgresql.org/docs/current/sql-createoperator.html
2055                    let mut idents = vec![];
2056                    loop {
2057                        idents.push(self.next_token().to_string());
2058                        if !self.consume_token(&Token::Period) {
2059                            break;
2060                        }
2061                    }
2062                    self.expect_token(&Token::RParen)?;
2063                    Some(BinaryOperator::PGCustomBinaryOperator(idents))
2064                }
2065                _ => None,
2066            },
2067            _ => None,
2068        };
2069
2070        if let Some(op) = regular_binary_operator {
2071            if let Some(keyword) = self.parse_one_of_keywords(&[Keyword::ANY, Keyword::ALL]) {
2072                self.expect_token(&Token::LParen)?;
2073                let right = self.parse_subexpr(precedence)?;
2074                self.expect_token(&Token::RParen)?;
2075
2076                if !matches!(
2077                    op,
2078                    BinaryOperator::Gt
2079                        | BinaryOperator::Lt
2080                        | BinaryOperator::GtEq
2081                        | BinaryOperator::LtEq
2082                        | BinaryOperator::Eq
2083                        | BinaryOperator::NotEq
2084                ) {
2085                    return parser_err!(
2086                        format!(
2087                        "Expected one of [=, >, <, =>, =<, !=] as comparison operator, found: {op}"
2088                    ),
2089                        tok.location
2090                    );
2091                };
2092
2093                Ok(match keyword {
2094                    Keyword::ALL => Expr::AllOp {
2095                        left: Box::new(expr),
2096                        compare_op: op,
2097                        right: Box::new(right),
2098                    },
2099                    Keyword::ANY => Expr::AnyOp {
2100                        left: Box::new(expr),
2101                        compare_op: op,
2102                        right: Box::new(right),
2103                    },
2104                    _ => unreachable!(),
2105                })
2106            } else {
2107                Ok(Expr::BinaryOp {
2108                    left: Box::new(expr),
2109                    op,
2110                    right: Box::new(self.parse_subexpr(precedence)?),
2111                })
2112            }
2113        } else if let Token::Word(w) = &tok.token {
2114            match w.keyword {
2115                Keyword::IS => {
2116                    if self.parse_keyword(Keyword::NULL) {
2117                        Ok(Expr::IsNull(Box::new(expr)))
2118                    } else if self.parse_keywords(&[Keyword::NOT, Keyword::NULL]) {
2119                        Ok(Expr::IsNotNull(Box::new(expr)))
2120                    } else if self.parse_keywords(&[Keyword::TRUE]) {
2121                        Ok(Expr::IsTrue(Box::new(expr)))
2122                    } else if self.parse_keywords(&[Keyword::NOT, Keyword::TRUE]) {
2123                        Ok(Expr::IsNotTrue(Box::new(expr)))
2124                    } else if self.parse_keywords(&[Keyword::FALSE]) {
2125                        Ok(Expr::IsFalse(Box::new(expr)))
2126                    } else if self.parse_keywords(&[Keyword::NOT, Keyword::FALSE]) {
2127                        Ok(Expr::IsNotFalse(Box::new(expr)))
2128                    } else if self.parse_keywords(&[Keyword::UNKNOWN]) {
2129                        Ok(Expr::IsUnknown(Box::new(expr)))
2130                    } else if self.parse_keywords(&[Keyword::NOT, Keyword::UNKNOWN]) {
2131                        Ok(Expr::IsNotUnknown(Box::new(expr)))
2132                    } else if self.parse_keywords(&[Keyword::DISTINCT, Keyword::FROM]) {
2133                        let expr2 = self.parse_expr()?;
2134                        Ok(Expr::IsDistinctFrom(Box::new(expr), Box::new(expr2)))
2135                    } else if self.parse_keywords(&[Keyword::NOT, Keyword::DISTINCT, Keyword::FROM])
2136                    {
2137                        let expr2 = self.parse_expr()?;
2138                        Ok(Expr::IsNotDistinctFrom(Box::new(expr), Box::new(expr2)))
2139                    } else {
2140                        self.expected(
2141                            "[NOT] NULL or TRUE|FALSE or [NOT] DISTINCT FROM after IS",
2142                            self.peek_token(),
2143                        )
2144                    }
2145                }
2146                Keyword::AT => {
2147                    // if self.parse_keyword(Keyword::TIME) {
2148                    //     self.expect_keyword(Keyword::ZONE)?;
2149                    if self.parse_keywords(&[Keyword::TIME, Keyword::ZONE]) {
2150                        let time_zone = self.next_token();
2151                        match time_zone.token {
2152                            Token::SingleQuotedString(time_zone) => {
2153                                log::trace!("Peek token: {:?}", self.peek_token());
2154                                Ok(Expr::AtTimeZone {
2155                                    timestamp: Box::new(expr),
2156                                    time_zone,
2157                                })
2158                            }
2159                            _ => self.expected(
2160                                "Expected Token::SingleQuotedString after AT TIME ZONE",
2161                                time_zone,
2162                            ),
2163                        }
2164                    } else {
2165                        self.expected("Expected Token::Word after AT", tok)
2166                    }
2167                }
2168                Keyword::NOT
2169                | Keyword::IN
2170                | Keyword::BETWEEN
2171                | Keyword::LIKE
2172                | Keyword::ILIKE
2173                | Keyword::SIMILAR
2174                | Keyword::REGEXP
2175                | Keyword::RLIKE => {
2176                    self.prev_token();
2177                    let negated = self.parse_keyword(Keyword::NOT);
2178                    let regexp = self.parse_keyword(Keyword::REGEXP);
2179                    let rlike = self.parse_keyword(Keyword::RLIKE);
2180                    if regexp || rlike {
2181                        Ok(Expr::RLike {
2182                            negated,
2183                            expr: Box::new(expr),
2184                            pattern: Box::new(self.parse_subexpr(Self::LIKE_PREC)?),
2185                            regexp,
2186                        })
2187                    } else if self.parse_keyword(Keyword::IN) {
2188                        self.parse_in(expr, negated)
2189                    } else if self.parse_keyword(Keyword::BETWEEN) {
2190                        self.parse_between(expr, negated)
2191                    } else if self.parse_keyword(Keyword::LIKE) {
2192                        Ok(Expr::Like {
2193                            negated,
2194                            expr: Box::new(expr),
2195                            pattern: Box::new(self.parse_subexpr(Self::LIKE_PREC)?),
2196                            escape_char: self.parse_escape_char()?,
2197                        })
2198                    } else if self.parse_keyword(Keyword::ILIKE) {
2199                        Ok(Expr::ILike {
2200                            negated,
2201                            expr: Box::new(expr),
2202                            pattern: Box::new(self.parse_subexpr(Self::LIKE_PREC)?),
2203                            escape_char: self.parse_escape_char()?,
2204                        })
2205                    } else if self.parse_keywords(&[Keyword::SIMILAR, Keyword::TO]) {
2206                        Ok(Expr::SimilarTo {
2207                            negated,
2208                            expr: Box::new(expr),
2209                            pattern: Box::new(self.parse_subexpr(Self::LIKE_PREC)?),
2210                            escape_char: self.parse_escape_char()?,
2211                        })
2212                    } else {
2213                        self.expected("IN or BETWEEN after NOT", self.peek_token())
2214                    }
2215                }
2216                // Can only happen if `get_next_precedence` got out of sync with this function
2217                _ => parser_err!(
2218                    format!("No infix parser for token {:?}", tok.token),
2219                    tok.location
2220                ),
2221            }
2222        } else if Token::DoubleColon == tok {
2223            self.parse_pg_cast(expr)
2224        } else if Token::ExclamationMark == tok {
2225            // PostgreSQL factorial operation
2226            Ok(Expr::UnaryOp {
2227                op: UnaryOperator::PGPostfixFactorial,
2228                expr: Box::new(expr),
2229            })
2230        } else if Token::LBracket == tok {
2231            if dialect_of!(self is PostgreSqlDialect | GenericDialect) {
2232                // parse index
2233                return self.parse_array_index(expr);
2234            }
2235            self.parse_map_access(expr)
2236        } else if Token::Colon == tok {
2237            Ok(Expr::JsonAccess {
2238                left: Box::new(expr),
2239                operator: JsonOperator::Colon,
2240                right: Box::new(Expr::Value(self.parse_value()?)),
2241            })
2242        } else if Token::Arrow == tok
2243            || Token::LongArrow == tok
2244            || Token::HashArrow == tok
2245            || Token::HashLongArrow == tok
2246            || Token::AtArrow == tok
2247            || Token::ArrowAt == tok
2248            || Token::HashMinus == tok
2249            || Token::AtQuestion == tok
2250            || Token::AtAt == tok
2251        {
2252            let operator = match tok.token {
2253                Token::Arrow => JsonOperator::Arrow,
2254                Token::LongArrow => JsonOperator::LongArrow,
2255                Token::HashArrow => JsonOperator::HashArrow,
2256                Token::HashLongArrow => JsonOperator::HashLongArrow,
2257                Token::AtArrow => JsonOperator::AtArrow,
2258                Token::ArrowAt => JsonOperator::ArrowAt,
2259                Token::HashMinus => JsonOperator::HashMinus,
2260                Token::AtQuestion => JsonOperator::AtQuestion,
2261                Token::AtAt => JsonOperator::AtAt,
2262                _ => unreachable!(),
2263            };
2264            Ok(Expr::JsonAccess {
2265                left: Box::new(expr),
2266                operator,
2267                right: Box::new(self.parse_expr()?),
2268            })
2269        } else {
2270            // Can only happen if `get_next_precedence` got out of sync with this function
2271            parser_err!(
2272                format!("No infix parser for token {:?}", tok.token),
2273                tok.location
2274            )
2275        }
2276    }
2277
2278    /// parse the ESCAPE CHAR portion of LIKE, ILIKE, and SIMILAR TO
2279    pub fn parse_escape_char(&mut self) -> Result<Option<char>, ParserError> {
2280        if self.parse_keyword(Keyword::ESCAPE) {
2281            Ok(Some(self.parse_literal_char()?))
2282        } else {
2283            Ok(None)
2284        }
2285    }
2286
2287    pub fn parse_array_index(&mut self, expr: Expr) -> Result<Expr, ParserError> {
2288        let index = self.parse_expr()?;
2289        self.expect_token(&Token::RBracket)?;
2290        let mut indexes: Vec<Expr> = vec![index];
2291        while self.consume_token(&Token::LBracket) {
2292            let index = self.parse_expr()?;
2293            self.expect_token(&Token::RBracket)?;
2294            indexes.push(index);
2295        }
2296        Ok(Expr::ArrayIndex {
2297            obj: Box::new(expr),
2298            indexes,
2299        })
2300    }
2301
2302    pub fn parse_map_access(&mut self, expr: Expr) -> Result<Expr, ParserError> {
2303        let key = self.parse_map_key()?;
2304        let tok = self.consume_token(&Token::RBracket);
2305        debug!("Tok: {}", tok);
2306        let mut key_parts: Vec<Expr> = vec![key];
2307        while self.consume_token(&Token::LBracket) {
2308            let key = self.parse_map_key()?;
2309            let tok = self.consume_token(&Token::RBracket);
2310            debug!("Tok: {}", tok);
2311            key_parts.push(key);
2312        }
2313        match expr {
2314            e @ Expr::Identifier(_) | e @ Expr::CompoundIdentifier(_) => Ok(Expr::MapAccess {
2315                column: Box::new(e),
2316                keys: key_parts,
2317            }),
2318            _ => Ok(expr),
2319        }
2320    }
2321
2322    /// Parses the parens following the `[ NOT ] IN` operator
2323    pub fn parse_in(&mut self, expr: Expr, negated: bool) -> Result<Expr, ParserError> {
2324        // BigQuery allows `IN UNNEST(array_expression)`
2325        // https://cloud.google.com/bigquery/docs/reference/standard-sql/operators#in_operators
2326        if self.parse_keyword(Keyword::UNNEST) {
2327            self.expect_token(&Token::LParen)?;
2328            let array_expr = self.parse_expr()?;
2329            self.expect_token(&Token::RParen)?;
2330            return Ok(Expr::InUnnest {
2331                expr: Box::new(expr),
2332                array_expr: Box::new(array_expr),
2333                negated,
2334            });
2335        }
2336        self.expect_token(&Token::LParen)?;
2337        let in_op = if self.parse_keyword(Keyword::SELECT) || self.parse_keyword(Keyword::WITH) {
2338            self.prev_token();
2339            Expr::InSubquery {
2340                expr: Box::new(expr),
2341                subquery: Box::new(self.parse_query()?),
2342                negated,
2343            }
2344        } else {
2345            Expr::InList {
2346                expr: Box::new(expr),
2347                list: if self.dialect.supports_in_empty_list() {
2348                    self.parse_comma_separated0(Parser::parse_expr)?
2349                } else {
2350                    self.parse_comma_separated(Parser::parse_expr)?
2351                },
2352                negated,
2353            }
2354        };
2355        self.expect_token(&Token::RParen)?;
2356        Ok(in_op)
2357    }
2358
2359    /// Parses `BETWEEN <low> AND <high>`, assuming the `BETWEEN` keyword was already consumed
2360    pub fn parse_between(&mut self, expr: Expr, negated: bool) -> Result<Expr, ParserError> {
2361        // Stop parsing subexpressions for <low> and <high> on tokens with
2362        // precedence lower than that of `BETWEEN`, such as `AND`, `IS`, etc.
2363        let low = self.parse_subexpr(Self::BETWEEN_PREC)?;
2364        self.expect_keyword(Keyword::AND)?;
2365        let high = self.parse_subexpr(Self::BETWEEN_PREC)?;
2366        Ok(Expr::Between {
2367            expr: Box::new(expr),
2368            negated,
2369            low: Box::new(low),
2370            high: Box::new(high),
2371        })
2372    }
2373
2374    /// Parse a postgresql casting style which is in the form of `expr::datatype`
2375    pub fn parse_pg_cast(&mut self, expr: Expr) -> Result<Expr, ParserError> {
2376        Ok(Expr::Cast {
2377            expr: Box::new(expr),
2378            data_type: self.parse_data_type()?,
2379            format: None,
2380        })
2381    }
2382
2383    // use https://www.postgresql.org/docs/7.0/operators.htm#AEN2026 as a reference
2384    const MUL_DIV_MOD_OP_PREC: u8 = 40;
2385    const PLUS_MINUS_PREC: u8 = 30;
2386    const XOR_PREC: u8 = 24;
2387    const TIME_ZONE_PREC: u8 = 20;
2388    const BETWEEN_PREC: u8 = 20;
2389    const LIKE_PREC: u8 = 19;
2390    const IS_PREC: u8 = 17;
2391    const UNARY_NOT_PREC: u8 = 15;
2392    const AND_PREC: u8 = 10;
2393    const OR_PREC: u8 = 5;
2394
2395    /// Get the precedence of the next token
2396    pub fn get_next_precedence(&self) -> Result<u8, ParserError> {
2397        // allow the dialect to override precedence logic
2398        if let Some(precedence) = self.dialect.get_next_precedence(self) {
2399            return precedence;
2400        }
2401
2402        let token = self.peek_token();
2403        debug!("get_next_precedence() {:?}", token);
2404        let token_0 = self.peek_nth_token(0);
2405        let token_1 = self.peek_nth_token(1);
2406        let token_2 = self.peek_nth_token(2);
2407        debug!("0: {token_0} 1: {token_1} 2: {token_2}");
2408        match token.token {
2409            Token::Word(w) if w.keyword == Keyword::OR => Ok(Self::OR_PREC),
2410            Token::Word(w) if w.keyword == Keyword::AND => Ok(Self::AND_PREC),
2411            Token::Word(w) if w.keyword == Keyword::XOR => Ok(Self::XOR_PREC),
2412
2413            Token::Word(w) if w.keyword == Keyword::AT => {
2414                match (self.peek_nth_token(1).token, self.peek_nth_token(2).token) {
2415                    (Token::Word(w), Token::Word(w2))
2416                        if w.keyword == Keyword::TIME && w2.keyword == Keyword::ZONE =>
2417                    {
2418                        Ok(Self::TIME_ZONE_PREC)
2419                    }
2420                    _ => Ok(0),
2421                }
2422            }
2423
2424            Token::Word(w) if w.keyword == Keyword::NOT => match self.peek_nth_token(1).token {
2425                // The precedence of NOT varies depending on keyword that
2426                // follows it. If it is followed by IN, BETWEEN, or LIKE,
2427                // it takes on the precedence of those tokens. Otherwise it
2428                // is not an infix operator, and therefore has zero
2429                // precedence.
2430                Token::Word(w) if w.keyword == Keyword::IN => Ok(Self::BETWEEN_PREC),
2431                Token::Word(w) if w.keyword == Keyword::BETWEEN => Ok(Self::BETWEEN_PREC),
2432                Token::Word(w) if w.keyword == Keyword::LIKE => Ok(Self::LIKE_PREC),
2433                Token::Word(w) if w.keyword == Keyword::ILIKE => Ok(Self::LIKE_PREC),
2434                Token::Word(w) if w.keyword == Keyword::RLIKE => Ok(Self::LIKE_PREC),
2435                Token::Word(w) if w.keyword == Keyword::REGEXP => Ok(Self::LIKE_PREC),
2436                Token::Word(w) if w.keyword == Keyword::SIMILAR => Ok(Self::LIKE_PREC),
2437                _ => Ok(0),
2438            },
2439            Token::Word(w) if w.keyword == Keyword::IS => Ok(Self::IS_PREC),
2440            Token::Word(w) if w.keyword == Keyword::IN => Ok(Self::BETWEEN_PREC),
2441            Token::Word(w) if w.keyword == Keyword::BETWEEN => Ok(Self::BETWEEN_PREC),
2442            Token::Word(w) if w.keyword == Keyword::LIKE => Ok(Self::LIKE_PREC),
2443            Token::Word(w) if w.keyword == Keyword::ILIKE => Ok(Self::LIKE_PREC),
2444            Token::Word(w) if w.keyword == Keyword::RLIKE => Ok(Self::LIKE_PREC),
2445            Token::Word(w) if w.keyword == Keyword::REGEXP => Ok(Self::LIKE_PREC),
2446            Token::Word(w) if w.keyword == Keyword::SIMILAR => Ok(Self::LIKE_PREC),
2447            Token::Word(w) if w.keyword == Keyword::OPERATOR => Ok(Self::BETWEEN_PREC),
2448            Token::Word(w) if w.keyword == Keyword::DIV => Ok(Self::MUL_DIV_MOD_OP_PREC),
2449            Token::Eq
2450            | Token::Lt
2451            | Token::LtEq
2452            | Token::Neq
2453            | Token::Gt
2454            | Token::GtEq
2455            | Token::DoubleEq
2456            | Token::Tilde
2457            | Token::TildeAsterisk
2458            | Token::ExclamationMarkTilde
2459            | Token::ExclamationMarkTildeAsterisk
2460            | Token::Spaceship => Ok(20),
2461            Token::Pipe => Ok(21),
2462            Token::Caret | Token::Sharp | Token::ShiftRight | Token::ShiftLeft => Ok(22),
2463            Token::Ampersand => Ok(23),
2464            Token::Plus | Token::Minus => Ok(Self::PLUS_MINUS_PREC),
2465            Token::Mul | Token::Div | Token::DuckIntDiv | Token::Mod | Token::StringConcat => {
2466                Ok(Self::MUL_DIV_MOD_OP_PREC)
2467            }
2468            Token::DoubleColon => Ok(50),
2469            Token::Colon => Ok(50),
2470            Token::ExclamationMark => Ok(50),
2471            Token::LBracket
2472            | Token::LongArrow
2473            | Token::Arrow
2474            | Token::Overlap
2475            | Token::HashArrow
2476            | Token::HashLongArrow
2477            | Token::AtArrow
2478            | Token::ArrowAt
2479            | Token::HashMinus
2480            | Token::AtQuestion
2481            | Token::AtAt => Ok(50),
2482            _ => Ok(0),
2483        }
2484    }
2485
2486    /// Return the first non-whitespace token that has not yet been processed
2487    /// (or None if reached end-of-file)
2488    pub fn peek_token(&self) -> TokenWithLocation {
2489        self.peek_nth_token(0)
2490    }
2491
2492    /// Return nth non-whitespace token that has not yet been processed
2493    pub fn peek_nth_token(&self, mut n: usize) -> TokenWithLocation {
2494        let mut index = self.index;
2495        loop {
2496            index += 1;
2497            match self.tokens.get(index - 1) {
2498                Some(TokenWithLocation {
2499                    token: Token::Whitespace(_),
2500                    location: _,
2501                }) => continue,
2502                non_whitespace => {
2503                    if n == 0 {
2504                        return non_whitespace.cloned().unwrap_or(TokenWithLocation {
2505                            token: Token::EOF,
2506                            location: Location { line: 0, column: 0 },
2507                        });
2508                    }
2509                    n -= 1;
2510                }
2511            }
2512        }
2513    }
2514
2515    /// Return the first non-whitespace token that has not yet been processed
2516    /// (or None if reached end-of-file) and mark it as processed. OK to call
2517    /// repeatedly after reaching EOF.
2518    pub fn next_token(&mut self) -> TokenWithLocation {
2519        loop {
2520            self.index += 1;
2521            match self.tokens.get(self.index - 1) {
2522                Some(TokenWithLocation {
2523                    token: Token::Whitespace(_),
2524                    location: _,
2525                }) => continue,
2526                token => {
2527                    return token
2528                        .cloned()
2529                        .unwrap_or_else(|| TokenWithLocation::wrap(Token::EOF))
2530                }
2531            }
2532        }
2533    }
2534
2535    /// Return the first unprocessed token, possibly whitespace.
2536    pub fn next_token_no_skip(&mut self) -> Option<&TokenWithLocation> {
2537        self.index += 1;
2538        self.tokens.get(self.index - 1)
2539    }
2540
2541    /// Push back the last one non-whitespace token. Must be called after
2542    /// `next_token()`, otherwise might panic. OK to call after
2543    /// `next_token()` indicates an EOF.
2544    pub fn prev_token(&mut self) {
2545        loop {
2546            assert!(self.index > 0);
2547            self.index -= 1;
2548            if let Some(TokenWithLocation {
2549                token: Token::Whitespace(_),
2550                location: _,
2551            }) = self.tokens.get(self.index)
2552            {
2553                continue;
2554            }
2555            return;
2556        }
2557    }
2558
2559    /// Report `found` was encountered instead of `expected`
2560    pub fn expected<T>(&self, expected: &str, found: TokenWithLocation) -> Result<T, ParserError> {
2561        parser_err!(
2562            format!("Expected {expected}, found: {found}"),
2563            found.location
2564        )
2565    }
2566
2567    /// If the current token is the `expected` keyword, consume it and returns
2568    /// true. Otherwise, no tokens are consumed and returns false.
2569    #[must_use]
2570    pub fn parse_keyword(&mut self, expected: Keyword) -> bool {
2571        match self.peek_token().token {
2572            Token::Word(w) if expected == w.keyword => {
2573                self.next_token();
2574                true
2575            }
2576            _ => false,
2577        }
2578    }
2579
2580    /// If the current and subsequent tokens exactly match the `keywords`
2581    /// sequence, consume them and returns true. Otherwise, no tokens are
2582    /// consumed and returns false
2583    #[must_use]
2584    pub fn parse_keywords(&mut self, keywords: &[Keyword]) -> bool {
2585        let index = self.index;
2586        for &keyword in keywords {
2587            if !self.parse_keyword(keyword) {
2588                // println!("parse_keywords aborting .. did not find {:?}", keyword);
2589                // reset index and return immediately
2590                self.index = index;
2591                return false;
2592            }
2593        }
2594        true
2595    }
2596
2597    /// If the current token is one of the given `keywords`, consume the token
2598    /// and return the keyword that matches. Otherwise, no tokens are consumed
2599    /// and returns `None`.
2600    #[must_use]
2601    pub fn parse_one_of_keywords(&mut self, keywords: &[Keyword]) -> Option<Keyword> {
2602        match self.peek_token().token {
2603            Token::Word(w) => {
2604                keywords
2605                    .iter()
2606                    .find(|keyword| **keyword == w.keyword)
2607                    .map(|keyword| {
2608                        self.next_token();
2609                        *keyword
2610                    })
2611            }
2612            _ => None,
2613        }
2614    }
2615
2616    /// If the current token is one of the expected keywords, consume the token
2617    /// and return the keyword that matches. Otherwise, return an error.
2618    pub fn expect_one_of_keywords(&mut self, keywords: &[Keyword]) -> Result<Keyword, ParserError> {
2619        if let Some(keyword) = self.parse_one_of_keywords(keywords) {
2620            Ok(keyword)
2621        } else {
2622            let keywords: Vec<String> = keywords.iter().map(|x| format!("{x:?}")).collect();
2623            self.expected(
2624                &format!("one of {}", keywords.join(" or ")),
2625                self.peek_token(),
2626            )
2627        }
2628    }
2629
2630    /// If the current token is the `expected` keyword, consume the token.
2631    /// Otherwise return an error.
2632    pub fn expect_keyword(&mut self, expected: Keyword) -> Result<(), ParserError> {
2633        if self.parse_keyword(expected) {
2634            Ok(())
2635        } else {
2636            self.expected(format!("{:?}", &expected).as_str(), self.peek_token())
2637        }
2638    }
2639
2640    /// If the current and subsequent tokens exactly match the `keywords`
2641    /// sequence, consume them and returns Ok. Otherwise, return an Error.
2642    pub fn expect_keywords(&mut self, expected: &[Keyword]) -> Result<(), ParserError> {
2643        for &kw in expected {
2644            self.expect_keyword(kw)?;
2645        }
2646        Ok(())
2647    }
2648
2649    /// Consume the next token if it matches the expected token, otherwise return false
2650    #[must_use]
2651    pub fn consume_token(&mut self, expected: &Token) -> bool {
2652        if self.peek_token() == *expected {
2653            self.next_token();
2654            true
2655        } else {
2656            false
2657        }
2658    }
2659
2660    /// Bail out if the current token is not an expected keyword, or consume it if it is
2661    pub fn expect_token(&mut self, expected: &Token) -> Result<(), ParserError> {
2662        if self.consume_token(expected) {
2663            Ok(())
2664        } else {
2665            self.expected(&expected.to_string(), self.peek_token())
2666        }
2667    }
2668
2669    /// Parse a comma-separated list of 1+ SelectItem
2670    pub fn parse_projection(&mut self) -> Result<Vec<SelectItem>, ParserError> {
2671        // BigQuery allows trailing commas, but only in project lists
2672        // e.g. `SELECT 1, 2, FROM t`
2673        // https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical#trailing_commas
2674        //
2675        // This pattern could be captured better with RAII type semantics, but it's quite a bit of
2676        // code to add for just one case, so we'll just do it manually here.
2677        let old_value = self.options.trailing_commas;
2678        self.options.trailing_commas |= dialect_of!(self is BigQueryDialect);
2679
2680        let ret = self.parse_comma_separated(|p| p.parse_select_item());
2681        self.options.trailing_commas = old_value;
2682
2683        ret
2684    }
2685
2686    /// Parse a comma-separated list of 1+ items accepted by `F`
2687    pub fn parse_comma_separated<T, F>(&mut self, mut f: F) -> Result<Vec<T>, ParserError>
2688    where
2689        F: FnMut(&mut Parser<'a>) -> Result<T, ParserError>,
2690    {
2691        let mut values = vec![];
2692        loop {
2693            values.push(f(self)?);
2694            if !self.consume_token(&Token::Comma) {
2695                break;
2696            } else if self.options.trailing_commas {
2697                match self.peek_token().token {
2698                    Token::Word(kw)
2699                        if keywords::RESERVED_FOR_COLUMN_ALIAS
2700                            .iter()
2701                            .any(|d| kw.keyword == *d) =>
2702                    {
2703                        break;
2704                    }
2705                    Token::RParen
2706                    | Token::SemiColon
2707                    | Token::EOF
2708                    | Token::RBracket
2709                    | Token::RBrace => break,
2710                    _ => continue,
2711                }
2712            }
2713        }
2714        Ok(values)
2715    }
2716
2717    /// Parse a comma-separated list of 0+ items accepted by `F`
2718    pub fn parse_comma_separated0<T, F>(&mut self, f: F) -> Result<Vec<T>, ParserError>
2719    where
2720        F: FnMut(&mut Parser<'a>) -> Result<T, ParserError>,
2721    {
2722        // ()
2723        if matches!(self.peek_token().token, Token::RParen) {
2724            return Ok(vec![]);
2725        }
2726        // (,)
2727        if self.options.trailing_commas
2728            && matches!(self.peek_nth_token(0).token, Token::Comma)
2729            && matches!(self.peek_nth_token(1).token, Token::RParen)
2730        {
2731            let _ = self.consume_token(&Token::Comma);
2732            return Ok(vec![]);
2733        }
2734
2735        self.parse_comma_separated(f)
2736    }
2737
2738    /// Run a parser method `f`, reverting back to the current position
2739    /// if unsuccessful.
2740    #[must_use]
2741    fn maybe_parse<T, F>(&mut self, mut f: F) -> Option<T>
2742    where
2743        F: FnMut(&mut Parser) -> Result<T, ParserError>,
2744    {
2745        let index = self.index;
2746        if let Ok(t) = f(self) {
2747            Some(t)
2748        } else {
2749            self.index = index;
2750            None
2751        }
2752    }
2753
2754    /// Parse either `ALL`, `DISTINCT` or `DISTINCT ON (...)`. Returns `None` if `ALL` is parsed
2755    /// and results in a `ParserError` if both `ALL` and `DISTINCT` are found.
2756    pub fn parse_all_or_distinct(&mut self) -> Result<Option<Distinct>, ParserError> {
2757        let loc = self.peek_token().location;
2758        let all = self.parse_keyword(Keyword::ALL);
2759        let distinct = self.parse_keyword(Keyword::DISTINCT);
2760        if !distinct {
2761            return Ok(None);
2762        }
2763        if all {
2764            return parser_err!("Cannot specify both ALL and DISTINCT".to_string(), loc);
2765        }
2766        let on = self.parse_keyword(Keyword::ON);
2767        if !on {
2768            return Ok(Some(Distinct::Distinct));
2769        }
2770
2771        self.expect_token(&Token::LParen)?;
2772        let col_names = if self.consume_token(&Token::RParen) {
2773            self.prev_token();
2774            Vec::new()
2775        } else {
2776            self.parse_comma_separated(Parser::parse_expr)?
2777        };
2778        self.expect_token(&Token::RParen)?;
2779        Ok(Some(Distinct::On(col_names)))
2780    }
2781
2782    /// Parse a SQL CREATE statement
2783    pub fn parse_create(&mut self) -> Result<Statement, ParserError> {
2784        let or_replace = self.parse_keywords(&[Keyword::OR, Keyword::REPLACE]);
2785        let or_alter = self.parse_keywords(&[Keyword::OR, Keyword::ALTER]);
2786        let local = self.parse_one_of_keywords(&[Keyword::LOCAL]).is_some();
2787        let global = self.parse_one_of_keywords(&[Keyword::GLOBAL]).is_some();
2788        let transient = self.parse_one_of_keywords(&[Keyword::TRANSIENT]).is_some();
2789        let global: Option<bool> = if global {
2790            Some(true)
2791        } else if local {
2792            Some(false)
2793        } else {
2794            None
2795        };
2796        let temporary = self
2797            .parse_one_of_keywords(&[Keyword::TEMP, Keyword::TEMPORARY])
2798            .is_some();
2799        if self.parse_keyword(Keyword::TABLE) {
2800            self.parse_create_table(or_replace, temporary, global, transient)
2801        } else if self.parse_keyword(Keyword::MATERIALIZED) || self.parse_keyword(Keyword::VIEW) {
2802            self.prev_token();
2803            self.parse_create_view(or_replace, temporary)
2804        } else if self.parse_keyword(Keyword::EXTERNAL) {
2805            self.parse_create_external_table(or_replace)
2806        } else if self.parse_keyword(Keyword::FUNCTION) {
2807            self.parse_create_function(or_replace, temporary)
2808        } else if self.parse_keyword(Keyword::MACRO) {
2809            self.parse_create_macro(or_replace, temporary)
2810        } else if or_replace {
2811            self.expected(
2812                "[EXTERNAL] TABLE or [MATERIALIZED] VIEW or FUNCTION after CREATE OR REPLACE",
2813                self.peek_token(),
2814            )
2815        } else if self.parse_keyword(Keyword::INDEX) {
2816            self.parse_create_index(false)
2817        } else if self.parse_keywords(&[Keyword::UNIQUE, Keyword::INDEX]) {
2818            self.parse_create_index(true)
2819        } else if self.parse_keyword(Keyword::VIRTUAL) {
2820            self.parse_create_virtual_table()
2821        } else if self.parse_keyword(Keyword::SCHEMA) {
2822            self.parse_create_schema()
2823        } else if self.parse_keyword(Keyword::DATABASE) {
2824            self.parse_create_database()
2825        } else if self.parse_keyword(Keyword::ROLE) {
2826            self.parse_create_role()
2827        } else if self.parse_keyword(Keyword::SEQUENCE) {
2828            self.parse_create_sequence(temporary)
2829        } else if self.parse_keyword(Keyword::TYPE) {
2830            self.parse_create_type()
2831        } else if self.parse_keyword(Keyword::PROCEDURE) {
2832            self.parse_create_procedure(or_alter)
2833        } else {
2834            self.expected("an object type after CREATE", self.peek_token())
2835        }
2836    }
2837
2838    /// Parse a CACHE TABLE statement
2839    pub fn parse_cache_table(&mut self) -> Result<Statement, ParserError> {
2840        let (mut table_flag, mut options, mut has_as, mut query) = (None, vec![], false, None);
2841        if self.parse_keyword(Keyword::TABLE) {
2842            let table_name = self.parse_object_name()?;
2843            if self.peek_token().token != Token::EOF {
2844                if let Token::Word(word) = self.peek_token().token {
2845                    if word.keyword == Keyword::OPTIONS {
2846                        options = self.parse_options(Keyword::OPTIONS)?
2847                    }
2848                };
2849
2850                if self.peek_token().token != Token::EOF {
2851                    let (a, q) = self.parse_as_query()?;
2852                    has_as = a;
2853                    query = Some(q);
2854                }
2855
2856                Ok(Statement::Cache {
2857                    table_flag,
2858                    table_name,
2859                    has_as,
2860                    options,
2861                    query,
2862                })
2863            } else {
2864                Ok(Statement::Cache {
2865                    table_flag,
2866                    table_name,
2867                    has_as,
2868                    options,
2869                    query,
2870                })
2871            }
2872        } else {
2873            table_flag = Some(self.parse_object_name()?);
2874            if self.parse_keyword(Keyword::TABLE) {
2875                let table_name = self.parse_object_name()?;
2876                if self.peek_token() != Token::EOF {
2877                    if let Token::Word(word) = self.peek_token().token {
2878                        if word.keyword == Keyword::OPTIONS {
2879                            options = self.parse_options(Keyword::OPTIONS)?
2880                        }
2881                    };
2882
2883                    if self.peek_token() != Token::EOF {
2884                        let (a, q) = self.parse_as_query()?;
2885                        has_as = a;
2886                        query = Some(q);
2887                    }
2888
2889                    Ok(Statement::Cache {
2890                        table_flag,
2891                        table_name,
2892                        has_as,
2893                        options,
2894                        query,
2895                    })
2896                } else {
2897                    Ok(Statement::Cache {
2898                        table_flag,
2899                        table_name,
2900                        has_as,
2901                        options,
2902                        query,
2903                    })
2904                }
2905            } else {
2906                if self.peek_token() == Token::EOF {
2907                    self.prev_token();
2908                }
2909                self.expected("a `TABLE` keyword", self.peek_token())
2910            }
2911        }
2912    }
2913
2914    /// Parse 'AS' before as query,such as `WITH XXX AS SELECT XXX` oer `CACHE TABLE AS SELECT XXX`
2915    pub fn parse_as_query(&mut self) -> Result<(bool, Query), ParserError> {
2916        match self.peek_token().token {
2917            Token::Word(word) => match word.keyword {
2918                Keyword::AS => {
2919                    self.next_token();
2920                    Ok((true, self.parse_query()?))
2921                }
2922                _ => Ok((false, self.parse_query()?)),
2923            },
2924            _ => self.expected("a QUERY statement", self.peek_token()),
2925        }
2926    }
2927
2928    /// Parse a UNCACHE TABLE statement
2929    pub fn parse_uncache_table(&mut self) -> Result<Statement, ParserError> {
2930        let has_table = self.parse_keyword(Keyword::TABLE);
2931        if has_table {
2932            let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
2933            let table_name = self.parse_object_name()?;
2934            if self.peek_token().token == Token::EOF {
2935                Ok(Statement::UNCache {
2936                    table_name,
2937                    if_exists,
2938                })
2939            } else {
2940                self.expected("an `EOF`", self.peek_token())
2941            }
2942        } else {
2943            self.expected("a `TABLE` keyword", self.peek_token())
2944        }
2945    }
2946
2947    /// SQLite-specific `CREATE VIRTUAL TABLE`
2948    pub fn parse_create_virtual_table(&mut self) -> Result<Statement, ParserError> {
2949        self.expect_keyword(Keyword::TABLE)?;
2950        let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
2951        let table_name = self.parse_object_name()?;
2952        self.expect_keyword(Keyword::USING)?;
2953        let module_name = self.parse_identifier()?;
2954        // SQLite docs note that module "arguments syntax is sufficiently
2955        // general that the arguments can be made to appear as column
2956        // definitions in a traditional CREATE TABLE statement", but
2957        // we don't implement that.
2958        let module_args = self.parse_parenthesized_column_list(Optional, false)?;
2959        Ok(Statement::CreateVirtualTable {
2960            name: table_name,
2961            if_not_exists,
2962            module_name,
2963            module_args,
2964        })
2965    }
2966
2967    pub fn parse_create_schema(&mut self) -> Result<Statement, ParserError> {
2968        let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
2969
2970        let schema_name = self.parse_schema_name()?;
2971
2972        Ok(Statement::CreateSchema {
2973            schema_name,
2974            if_not_exists,
2975        })
2976    }
2977
2978    fn parse_schema_name(&mut self) -> Result<SchemaName, ParserError> {
2979        if self.parse_keyword(Keyword::AUTHORIZATION) {
2980            Ok(SchemaName::UnnamedAuthorization(self.parse_identifier()?))
2981        } else {
2982            let name = self.parse_object_name()?;
2983
2984            if self.parse_keyword(Keyword::AUTHORIZATION) {
2985                Ok(SchemaName::NamedAuthorization(
2986                    name,
2987                    self.parse_identifier()?,
2988                ))
2989            } else {
2990                Ok(SchemaName::Simple(name))
2991            }
2992        }
2993    }
2994
2995    pub fn parse_create_database(&mut self) -> Result<Statement, ParserError> {
2996        let ine = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
2997        let db_name = self.parse_object_name()?;
2998        let mut location = None;
2999        let mut managed_location = None;
3000        loop {
3001            match self.parse_one_of_keywords(&[Keyword::LOCATION, Keyword::MANAGEDLOCATION]) {
3002                Some(Keyword::LOCATION) => location = Some(self.parse_literal_string()?),
3003                Some(Keyword::MANAGEDLOCATION) => {
3004                    managed_location = Some(self.parse_literal_string()?)
3005                }
3006                _ => break,
3007            }
3008        }
3009        Ok(Statement::CreateDatabase {
3010            db_name,
3011            if_not_exists: ine,
3012            location,
3013            managed_location,
3014        })
3015    }
3016
3017    pub fn parse_optional_create_function_using(
3018        &mut self,
3019    ) -> Result<Option<CreateFunctionUsing>, ParserError> {
3020        if !self.parse_keyword(Keyword::USING) {
3021            return Ok(None);
3022        };
3023        let keyword =
3024            self.expect_one_of_keywords(&[Keyword::JAR, Keyword::FILE, Keyword::ARCHIVE])?;
3025
3026        let uri = self.parse_literal_string()?;
3027
3028        match keyword {
3029            Keyword::JAR => Ok(Some(CreateFunctionUsing::Jar(uri))),
3030            Keyword::FILE => Ok(Some(CreateFunctionUsing::File(uri))),
3031            Keyword::ARCHIVE => Ok(Some(CreateFunctionUsing::Archive(uri))),
3032            _ => self.expected(
3033                "JAR, FILE or ARCHIVE, got {:?}",
3034                TokenWithLocation::wrap(Token::make_keyword(format!("{keyword:?}").as_str())),
3035            ),
3036        }
3037    }
3038
3039    pub fn parse_create_function(
3040        &mut self,
3041        or_replace: bool,
3042        temporary: bool,
3043    ) -> Result<Statement, ParserError> {
3044        if dialect_of!(self is HiveDialect) {
3045            let name = self.parse_object_name()?;
3046            self.expect_keyword(Keyword::AS)?;
3047            let class_name = self.parse_function_definition()?;
3048            let params = CreateFunctionBody {
3049                as_: Some(class_name),
3050                using: self.parse_optional_create_function_using()?,
3051                ..Default::default()
3052            };
3053
3054            Ok(Statement::CreateFunction {
3055                or_replace,
3056                temporary,
3057                name,
3058                args: None,
3059                return_type: None,
3060                params,
3061            })
3062        } else if dialect_of!(self is PostgreSqlDialect) {
3063            let name = self.parse_object_name()?;
3064            self.expect_token(&Token::LParen)?;
3065            let args = if self.consume_token(&Token::RParen) {
3066                self.prev_token();
3067                None
3068            } else {
3069                Some(self.parse_comma_separated(Parser::parse_function_arg)?)
3070            };
3071
3072            self.expect_token(&Token::RParen)?;
3073
3074            let return_type = if self.parse_keyword(Keyword::RETURNS) {
3075                Some(self.parse_data_type()?)
3076            } else {
3077                None
3078            };
3079
3080            let params = self.parse_create_function_body()?;
3081
3082            Ok(Statement::CreateFunction {
3083                or_replace,
3084                temporary,
3085                name,
3086                args,
3087                return_type,
3088                params,
3089            })
3090        } else if dialect_of!(self is DuckDbDialect) {
3091            self.parse_create_macro(or_replace, temporary)
3092        } else {
3093            self.prev_token();
3094            self.expected("an object type after CREATE", self.peek_token())
3095        }
3096    }
3097
3098    fn parse_function_arg(&mut self) -> Result<OperateFunctionArg, ParserError> {
3099        let mode = if self.parse_keyword(Keyword::IN) {
3100            Some(ArgMode::In)
3101        } else if self.parse_keyword(Keyword::OUT) {
3102            Some(ArgMode::Out)
3103        } else if self.parse_keyword(Keyword::INOUT) {
3104            Some(ArgMode::InOut)
3105        } else {
3106            None
3107        };
3108
3109        // parse: [ argname ] argtype
3110        let mut name = None;
3111        let mut data_type = self.parse_data_type()?;
3112        if let DataType::Custom(n, _) = &data_type {
3113            // the first token is actually a name
3114            name = Some(n.0[0].clone());
3115            data_type = self.parse_data_type()?;
3116        }
3117
3118        let default_expr = if self.parse_keyword(Keyword::DEFAULT) || self.consume_token(&Token::Eq)
3119        {
3120            Some(self.parse_expr()?)
3121        } else {
3122            None
3123        };
3124        Ok(OperateFunctionArg {
3125            mode,
3126            name,
3127            data_type,
3128            default_expr,
3129        })
3130    }
3131
3132    fn parse_create_function_body(&mut self) -> Result<CreateFunctionBody, ParserError> {
3133        let mut body = CreateFunctionBody::default();
3134        loop {
3135            fn ensure_not_set<T>(field: &Option<T>, name: &str) -> Result<(), ParserError> {
3136                if field.is_some() {
3137                    return Err(ParserError::ParserError(format!(
3138                        "{name} specified more than once",
3139                    )));
3140                }
3141                Ok(())
3142            }
3143            if self.parse_keyword(Keyword::AS) {
3144                ensure_not_set(&body.as_, "AS")?;
3145                body.as_ = Some(self.parse_function_definition()?);
3146            } else if self.parse_keyword(Keyword::LANGUAGE) {
3147                ensure_not_set(&body.language, "LANGUAGE")?;
3148                body.language = Some(self.parse_identifier()?);
3149            } else if self.parse_keyword(Keyword::IMMUTABLE) {
3150                ensure_not_set(&body.behavior, "IMMUTABLE | STABLE | VOLATILE")?;
3151                body.behavior = Some(FunctionBehavior::Immutable);
3152            } else if self.parse_keyword(Keyword::STABLE) {
3153                ensure_not_set(&body.behavior, "IMMUTABLE | STABLE | VOLATILE")?;
3154                body.behavior = Some(FunctionBehavior::Stable);
3155            } else if self.parse_keyword(Keyword::VOLATILE) {
3156                ensure_not_set(&body.behavior, "IMMUTABLE | STABLE | VOLATILE")?;
3157                body.behavior = Some(FunctionBehavior::Volatile);
3158            } else if self.parse_keyword(Keyword::RETURN) {
3159                ensure_not_set(&body.return_, "RETURN")?;
3160                body.return_ = Some(self.parse_expr()?);
3161            } else {
3162                return Ok(body);
3163            }
3164        }
3165    }
3166
3167    pub fn parse_create_macro(
3168        &mut self,
3169        or_replace: bool,
3170        temporary: bool,
3171    ) -> Result<Statement, ParserError> {
3172        if dialect_of!(self is DuckDbDialect |  GenericDialect) {
3173            let name = self.parse_object_name()?;
3174            self.expect_token(&Token::LParen)?;
3175            let args = if self.consume_token(&Token::RParen) {
3176                self.prev_token();
3177                None
3178            } else {
3179                Some(self.parse_comma_separated(Parser::parse_macro_arg)?)
3180            };
3181
3182            self.expect_token(&Token::RParen)?;
3183            self.expect_keyword(Keyword::AS)?;
3184
3185            Ok(Statement::CreateMacro {
3186                or_replace,
3187                temporary,
3188                name,
3189                args,
3190                definition: if self.parse_keyword(Keyword::TABLE) {
3191                    MacroDefinition::Table(self.parse_query()?)
3192                } else {
3193                    MacroDefinition::Expr(self.parse_expr()?)
3194                },
3195            })
3196        } else {
3197            self.prev_token();
3198            self.expected("an object type after CREATE", self.peek_token())
3199        }
3200    }
3201
3202    fn parse_macro_arg(&mut self) -> Result<MacroArg, ParserError> {
3203        let name = self.parse_identifier()?;
3204
3205        let default_expr =
3206            if self.consume_token(&Token::DuckAssignment) || self.consume_token(&Token::RArrow) {
3207                Some(self.parse_expr()?)
3208            } else {
3209                None
3210            };
3211        Ok(MacroArg { name, default_expr })
3212    }
3213
3214    pub fn parse_create_external_table(
3215        &mut self,
3216        or_replace: bool,
3217    ) -> Result<Statement, ParserError> {
3218        self.expect_keyword(Keyword::TABLE)?;
3219        let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
3220        let table_name = self.parse_object_name()?;
3221        let (columns, constraints) = self.parse_columns()?;
3222
3223        let hive_distribution = self.parse_hive_distribution()?;
3224        let hive_formats = self.parse_hive_formats()?;
3225
3226        let file_format = if let Some(ff) = &hive_formats.storage {
3227            match ff {
3228                HiveIOFormat::FileFormat { format } => Some(*format),
3229                _ => None,
3230            }
3231        } else {
3232            None
3233        };
3234        let location = hive_formats.location.clone();
3235        let table_properties = self.parse_options(Keyword::TBLPROPERTIES)?;
3236        Ok(CreateTableBuilder::new(table_name)
3237            .columns(columns)
3238            .constraints(constraints)
3239            .hive_distribution(hive_distribution)
3240            .hive_formats(Some(hive_formats))
3241            .table_properties(table_properties)
3242            .or_replace(or_replace)
3243            .if_not_exists(if_not_exists)
3244            .external(true)
3245            .file_format(file_format)
3246            .location(location)
3247            .build())
3248    }
3249
3250    pub fn parse_file_format(&mut self) -> Result<FileFormat, ParserError> {
3251        let next_token = self.next_token();
3252        match &next_token.token {
3253            Token::Word(w) => match w.keyword {
3254                Keyword::AVRO => Ok(FileFormat::AVRO),
3255                Keyword::JSONFILE => Ok(FileFormat::JSONFILE),
3256                Keyword::ORC => Ok(FileFormat::ORC),
3257                Keyword::PARQUET => Ok(FileFormat::PARQUET),
3258                Keyword::RCFILE => Ok(FileFormat::RCFILE),
3259                Keyword::SEQUENCEFILE => Ok(FileFormat::SEQUENCEFILE),
3260                Keyword::TEXTFILE => Ok(FileFormat::TEXTFILE),
3261                _ => self.expected("fileformat", next_token),
3262            },
3263            _ => self.expected("fileformat", next_token),
3264        }
3265    }
3266
3267    pub fn parse_analyze_format(&mut self) -> Result<AnalyzeFormat, ParserError> {
3268        let next_token = self.next_token();
3269        match &next_token.token {
3270            Token::Word(w) => match w.keyword {
3271                Keyword::TEXT => Ok(AnalyzeFormat::TEXT),
3272                Keyword::GRAPHVIZ => Ok(AnalyzeFormat::GRAPHVIZ),
3273                Keyword::JSON => Ok(AnalyzeFormat::JSON),
3274                _ => self.expected("fileformat", next_token),
3275            },
3276            _ => self.expected("fileformat", next_token),
3277        }
3278    }
3279
3280    pub fn parse_create_view(
3281        &mut self,
3282        or_replace: bool,
3283        temporary: bool,
3284    ) -> Result<Statement, ParserError> {
3285        let materialized = self.parse_keyword(Keyword::MATERIALIZED);
3286        self.expect_keyword(Keyword::VIEW)?;
3287        let if_not_exists = dialect_of!(self is SQLiteDialect|GenericDialect)
3288            && self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
3289        // Many dialects support `OR ALTER` right after `CREATE`, but we don't (yet).
3290        // ANSI SQL and Postgres support RECURSIVE here, but we don't support it either.
3291        let name = self.parse_object_name()?;
3292        let columns = self.parse_parenthesized_column_list(Optional, false)?;
3293        let with_options = self.parse_options(Keyword::WITH)?;
3294
3295        let cluster_by = if self.parse_keyword(Keyword::CLUSTER) {
3296            self.expect_keyword(Keyword::BY)?;
3297            self.parse_parenthesized_column_list(Optional, false)?
3298        } else {
3299            vec![]
3300        };
3301
3302        self.expect_keyword(Keyword::AS)?;
3303        let query = Box::new(self.parse_query()?);
3304        // Optional `WITH [ CASCADED | LOCAL ] CHECK OPTION` is widely supported here.
3305
3306        let with_no_schema_binding = dialect_of!(self is RedshiftSqlDialect | GenericDialect)
3307            && self.parse_keywords(&[
3308                Keyword::WITH,
3309                Keyword::NO,
3310                Keyword::SCHEMA,
3311                Keyword::BINDING,
3312            ]);
3313
3314        Ok(Statement::CreateView {
3315            name,
3316            columns,
3317            query,
3318            materialized,
3319            or_replace,
3320            with_options,
3321            cluster_by,
3322            with_no_schema_binding,
3323            if_not_exists,
3324            temporary,
3325        })
3326    }
3327
3328    pub fn parse_create_role(&mut self) -> Result<Statement, ParserError> {
3329        let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
3330        let names = self.parse_comma_separated(Parser::parse_object_name)?;
3331
3332        let _ = self.parse_keyword(Keyword::WITH); // [ WITH ]
3333
3334        let optional_keywords = if dialect_of!(self is MsSqlDialect) {
3335            vec![Keyword::AUTHORIZATION]
3336        } else if dialect_of!(self is PostgreSqlDialect) {
3337            vec![
3338                Keyword::LOGIN,
3339                Keyword::NOLOGIN,
3340                Keyword::INHERIT,
3341                Keyword::NOINHERIT,
3342                Keyword::BYPASSRLS,
3343                Keyword::NOBYPASSRLS,
3344                Keyword::PASSWORD,
3345                Keyword::CREATEDB,
3346                Keyword::NOCREATEDB,
3347                Keyword::CREATEROLE,
3348                Keyword::NOCREATEROLE,
3349                Keyword::SUPERUSER,
3350                Keyword::NOSUPERUSER,
3351                Keyword::REPLICATION,
3352                Keyword::NOREPLICATION,
3353                Keyword::CONNECTION,
3354                Keyword::VALID,
3355                Keyword::IN,
3356                Keyword::ROLE,
3357                Keyword::ADMIN,
3358                Keyword::USER,
3359            ]
3360        } else {
3361            vec![]
3362        };
3363
3364        // MSSQL
3365        let mut authorization_owner = None;
3366        // Postgres
3367        let mut login = None;
3368        let mut inherit = None;
3369        let mut bypassrls = None;
3370        let mut password = None;
3371        let mut create_db = None;
3372        let mut create_role = None;
3373        let mut superuser = None;
3374        let mut replication = None;
3375        let mut connection_limit = None;
3376        let mut valid_until = None;
3377        let mut in_role = vec![];
3378        let mut in_group = vec![];
3379        let mut role = vec![];
3380        let mut user = vec![];
3381        let mut admin = vec![];
3382
3383        while let Some(keyword) = self.parse_one_of_keywords(&optional_keywords) {
3384            let loc = self
3385                .tokens
3386                .get(self.index - 1)
3387                .map_or(Location { line: 0, column: 0 }, |t| t.location);
3388            match keyword {
3389                Keyword::AUTHORIZATION => {
3390                    if authorization_owner.is_some() {
3391                        parser_err!("Found multiple AUTHORIZATION", loc)
3392                    } else {
3393                        authorization_owner = Some(self.parse_object_name()?);
3394                        Ok(())
3395                    }
3396                }
3397                Keyword::LOGIN | Keyword::NOLOGIN => {
3398                    if login.is_some() {
3399                        parser_err!("Found multiple LOGIN or NOLOGIN", loc)
3400                    } else {
3401                        login = Some(keyword == Keyword::LOGIN);
3402                        Ok(())
3403                    }
3404                }
3405                Keyword::INHERIT | Keyword::NOINHERIT => {
3406                    if inherit.is_some() {
3407                        parser_err!("Found multiple INHERIT or NOINHERIT", loc)
3408                    } else {
3409                        inherit = Some(keyword == Keyword::INHERIT);
3410                        Ok(())
3411                    }
3412                }
3413                Keyword::BYPASSRLS | Keyword::NOBYPASSRLS => {
3414                    if bypassrls.is_some() {
3415                        parser_err!("Found multiple BYPASSRLS or NOBYPASSRLS", loc)
3416                    } else {
3417                        bypassrls = Some(keyword == Keyword::BYPASSRLS);
3418                        Ok(())
3419                    }
3420                }
3421                Keyword::CREATEDB | Keyword::NOCREATEDB => {
3422                    if create_db.is_some() {
3423                        parser_err!("Found multiple CREATEDB or NOCREATEDB", loc)
3424                    } else {
3425                        create_db = Some(keyword == Keyword::CREATEDB);
3426                        Ok(())
3427                    }
3428                }
3429                Keyword::CREATEROLE | Keyword::NOCREATEROLE => {
3430                    if create_role.is_some() {
3431                        parser_err!("Found multiple CREATEROLE or NOCREATEROLE", loc)
3432                    } else {
3433                        create_role = Some(keyword == Keyword::CREATEROLE);
3434                        Ok(())
3435                    }
3436                }
3437                Keyword::SUPERUSER | Keyword::NOSUPERUSER => {
3438                    if superuser.is_some() {
3439                        parser_err!("Found multiple SUPERUSER or NOSUPERUSER", loc)
3440                    } else {
3441                        superuser = Some(keyword == Keyword::SUPERUSER);
3442                        Ok(())
3443                    }
3444                }
3445                Keyword::REPLICATION | Keyword::NOREPLICATION => {
3446                    if replication.is_some() {
3447                        parser_err!("Found multiple REPLICATION or NOREPLICATION", loc)
3448                    } else {
3449                        replication = Some(keyword == Keyword::REPLICATION);
3450                        Ok(())
3451                    }
3452                }
3453                Keyword::PASSWORD => {
3454                    if password.is_some() {
3455                        parser_err!("Found multiple PASSWORD", loc)
3456                    } else {
3457                        password = if self.parse_keyword(Keyword::NULL) {
3458                            Some(Password::NullPassword)
3459                        } else {
3460                            Some(Password::Password(Expr::Value(self.parse_value()?)))
3461                        };
3462                        Ok(())
3463                    }
3464                }
3465                Keyword::CONNECTION => {
3466                    self.expect_keyword(Keyword::LIMIT)?;
3467                    if connection_limit.is_some() {
3468                        parser_err!("Found multiple CONNECTION LIMIT", loc)
3469                    } else {
3470                        connection_limit = Some(Expr::Value(self.parse_number_value()?));
3471                        Ok(())
3472                    }
3473                }
3474                Keyword::VALID => {
3475                    self.expect_keyword(Keyword::UNTIL)?;
3476                    if valid_until.is_some() {
3477                        parser_err!("Found multiple VALID UNTIL", loc)
3478                    } else {
3479                        valid_until = Some(Expr::Value(self.parse_value()?));
3480                        Ok(())
3481                    }
3482                }
3483                Keyword::IN => {
3484                    if self.parse_keyword(Keyword::ROLE) {
3485                        if !in_role.is_empty() {
3486                            parser_err!("Found multiple IN ROLE", loc)
3487                        } else {
3488                            in_role = self.parse_comma_separated(Parser::parse_identifier)?;
3489                            Ok(())
3490                        }
3491                    } else if self.parse_keyword(Keyword::GROUP) {
3492                        if !in_group.is_empty() {
3493                            parser_err!("Found multiple IN GROUP", loc)
3494                        } else {
3495                            in_group = self.parse_comma_separated(Parser::parse_identifier)?;
3496                            Ok(())
3497                        }
3498                    } else {
3499                        self.expected("ROLE or GROUP after IN", self.peek_token())
3500                    }
3501                }
3502                Keyword::ROLE => {
3503                    if !role.is_empty() {
3504                        parser_err!("Found multiple ROLE", loc)
3505                    } else {
3506                        role = self.parse_comma_separated(Parser::parse_identifier)?;
3507                        Ok(())
3508                    }
3509                }
3510                Keyword::USER => {
3511                    if !user.is_empty() {
3512                        parser_err!("Found multiple USER", loc)
3513                    } else {
3514                        user = self.parse_comma_separated(Parser::parse_identifier)?;
3515                        Ok(())
3516                    }
3517                }
3518                Keyword::ADMIN => {
3519                    if !admin.is_empty() {
3520                        parser_err!("Found multiple ADMIN", loc)
3521                    } else {
3522                        admin = self.parse_comma_separated(Parser::parse_identifier)?;
3523                        Ok(())
3524                    }
3525                }
3526                _ => break,
3527            }?
3528        }
3529
3530        Ok(Statement::CreateRole {
3531            names,
3532            if_not_exists,
3533            login,
3534            inherit,
3535            bypassrls,
3536            password,
3537            create_db,
3538            create_role,
3539            replication,
3540            superuser,
3541            connection_limit,
3542            valid_until,
3543            in_role,
3544            in_group,
3545            role,
3546            user,
3547            admin,
3548            authorization_owner,
3549        })
3550    }
3551
3552    pub fn parse_drop(&mut self) -> Result<Statement, ParserError> {
3553        // MySQL dialect supports `TEMPORARY`
3554        let temporary = dialect_of!(self is MySqlDialect | GenericDialect)
3555            && self.parse_keyword(Keyword::TEMPORARY);
3556
3557        let object_type = if self.parse_keyword(Keyword::TABLE) {
3558            ObjectType::Table
3559        } else if self.parse_keyword(Keyword::VIEW) {
3560            ObjectType::View
3561        } else if self.parse_keyword(Keyword::INDEX) {
3562            ObjectType::Index
3563        } else if self.parse_keyword(Keyword::ROLE) {
3564            ObjectType::Role
3565        } else if self.parse_keyword(Keyword::SCHEMA) {
3566            ObjectType::Schema
3567        } else if self.parse_keyword(Keyword::SEQUENCE) {
3568            ObjectType::Sequence
3569        } else if self.parse_keyword(Keyword::STAGE) {
3570            ObjectType::Stage
3571        } else if self.parse_keyword(Keyword::FUNCTION) {
3572            return self.parse_drop_function();
3573        } else {
3574            return self.expected(
3575                "TABLE, VIEW, INDEX, ROLE, SCHEMA, FUNCTION, STAGE or SEQUENCE after DROP",
3576                self.peek_token(),
3577            );
3578        };
3579        // Many dialects support the non standard `IF EXISTS` clause and allow
3580        // specifying multiple objects to delete in a single statement
3581        let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
3582        let names = self.parse_comma_separated(Parser::parse_object_name)?;
3583
3584        let loc = self.peek_token().location;
3585        let cascade = self.parse_keyword(Keyword::CASCADE);
3586        let restrict = self.parse_keyword(Keyword::RESTRICT);
3587        let purge = self.parse_keyword(Keyword::PURGE);
3588        if cascade && restrict {
3589            return parser_err!("Cannot specify both CASCADE and RESTRICT in DROP", loc);
3590        }
3591        if object_type == ObjectType::Role && (cascade || restrict || purge) {
3592            return parser_err!(
3593                "Cannot specify CASCADE, RESTRICT, or PURGE in DROP ROLE",
3594                loc
3595            );
3596        }
3597        Ok(Statement::Drop {
3598            object_type,
3599            if_exists,
3600            names,
3601            cascade,
3602            restrict,
3603            purge,
3604            temporary,
3605        })
3606    }
3607
3608    /// ```sql
3609    /// DROP FUNCTION [ IF EXISTS ] name [ ( [ [ argmode ] [ argname ] argtype [, ...] ] ) ] [, ...]
3610    /// [ CASCADE | RESTRICT ]
3611    /// ```
3612    fn parse_drop_function(&mut self) -> Result<Statement, ParserError> {
3613        let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
3614        let func_desc = self.parse_comma_separated(Parser::parse_drop_function_desc)?;
3615        let option = match self.parse_one_of_keywords(&[Keyword::CASCADE, Keyword::RESTRICT]) {
3616            Some(Keyword::CASCADE) => Some(ReferentialAction::Cascade),
3617            Some(Keyword::RESTRICT) => Some(ReferentialAction::Restrict),
3618            _ => None,
3619        };
3620        Ok(Statement::DropFunction {
3621            if_exists,
3622            func_desc,
3623            option,
3624        })
3625    }
3626
3627    fn parse_drop_function_desc(&mut self) -> Result<DropFunctionDesc, ParserError> {
3628        let name = self.parse_object_name()?;
3629
3630        let args = if self.consume_token(&Token::LParen) {
3631            if self.consume_token(&Token::RParen) {
3632                None
3633            } else {
3634                let args = self.parse_comma_separated(Parser::parse_function_arg)?;
3635                self.expect_token(&Token::RParen)?;
3636                Some(args)
3637            }
3638        } else {
3639            None
3640        };
3641
3642        Ok(DropFunctionDesc { name, args })
3643    }
3644
3645    /// ```sql
3646    /// DECLARE name [ BINARY ] [ ASENSITIVE | INSENSITIVE ] [ [ NO ] SCROLL ]
3647    ///     CURSOR [ { WITH | WITHOUT } HOLD ] FOR query
3648    /// ```
3649    pub fn parse_declare(&mut self) -> Result<Statement, ParserError> {
3650        let name = self.parse_identifier()?;
3651
3652        let binary = self.parse_keyword(Keyword::BINARY);
3653        let sensitive = if self.parse_keyword(Keyword::INSENSITIVE) {
3654            Some(true)
3655        } else if self.parse_keyword(Keyword::ASENSITIVE) {
3656            Some(false)
3657        } else {
3658            None
3659        };
3660        let scroll = if self.parse_keyword(Keyword::SCROLL) {
3661            Some(true)
3662        } else if self.parse_keywords(&[Keyword::NO, Keyword::SCROLL]) {
3663            Some(false)
3664        } else {
3665            None
3666        };
3667
3668        self.expect_keyword(Keyword::CURSOR)?;
3669
3670        let hold = match self.parse_one_of_keywords(&[Keyword::WITH, Keyword::WITHOUT]) {
3671            Some(keyword) => {
3672                self.expect_keyword(Keyword::HOLD)?;
3673
3674                match keyword {
3675                    Keyword::WITH => Some(true),
3676                    Keyword::WITHOUT => Some(false),
3677                    _ => unreachable!(),
3678                }
3679            }
3680            None => None,
3681        };
3682
3683        self.expect_keyword(Keyword::FOR)?;
3684
3685        let query = self.parse_query()?;
3686
3687        Ok(Statement::Declare {
3688            name,
3689            binary,
3690            sensitive,
3691            scroll,
3692            hold,
3693            query: Box::new(query),
3694        })
3695    }
3696
3697    // FETCH [ direction { FROM | IN } ] cursor INTO target;
3698    pub fn parse_fetch_statement(&mut self) -> Result<Statement, ParserError> {
3699        let direction = if self.parse_keyword(Keyword::NEXT) {
3700            FetchDirection::Next
3701        } else if self.parse_keyword(Keyword::PRIOR) {
3702            FetchDirection::Prior
3703        } else if self.parse_keyword(Keyword::FIRST) {
3704            FetchDirection::First
3705        } else if self.parse_keyword(Keyword::LAST) {
3706            FetchDirection::Last
3707        } else if self.parse_keyword(Keyword::ABSOLUTE) {
3708            FetchDirection::Absolute {
3709                limit: self.parse_number_value()?,
3710            }
3711        } else if self.parse_keyword(Keyword::RELATIVE) {
3712            FetchDirection::Relative {
3713                limit: self.parse_number_value()?,
3714            }
3715        } else if self.parse_keyword(Keyword::FORWARD) {
3716            if self.parse_keyword(Keyword::ALL) {
3717                FetchDirection::ForwardAll
3718            } else {
3719                FetchDirection::Forward {
3720                    // TODO: Support optional
3721                    limit: Some(self.parse_number_value()?),
3722                }
3723            }
3724        } else if self.parse_keyword(Keyword::BACKWARD) {
3725            if self.parse_keyword(Keyword::ALL) {
3726                FetchDirection::BackwardAll
3727            } else {
3728                FetchDirection::Backward {
3729                    // TODO: Support optional
3730                    limit: Some(self.parse_number_value()?),
3731                }
3732            }
3733        } else if self.parse_keyword(Keyword::ALL) {
3734            FetchDirection::All
3735        } else {
3736            FetchDirection::Count {
3737                limit: self.parse_number_value()?,
3738            }
3739        };
3740
3741        self.expect_one_of_keywords(&[Keyword::FROM, Keyword::IN])?;
3742
3743        let name = self.parse_identifier()?;
3744
3745        let into = if self.parse_keyword(Keyword::INTO) {
3746            Some(self.parse_object_name()?)
3747        } else {
3748            None
3749        };
3750
3751        Ok(Statement::Fetch {
3752            name,
3753            direction,
3754            into,
3755        })
3756    }
3757
3758    pub fn parse_discard(&mut self) -> Result<Statement, ParserError> {
3759        let object_type = if self.parse_keyword(Keyword::ALL) {
3760            DiscardObject::ALL
3761        } else if self.parse_keyword(Keyword::PLANS) {
3762            DiscardObject::PLANS
3763        } else if self.parse_keyword(Keyword::SEQUENCES) {
3764            DiscardObject::SEQUENCES
3765        } else if self.parse_keyword(Keyword::TEMP) || self.parse_keyword(Keyword::TEMPORARY) {
3766            DiscardObject::TEMP
3767        } else {
3768            return self.expected(
3769                "ALL, PLANS, SEQUENCES, TEMP or TEMPORARY after DISCARD",
3770                self.peek_token(),
3771            );
3772        };
3773        Ok(Statement::Discard { object_type })
3774    }
3775
3776    pub fn parse_create_index(&mut self, unique: bool) -> Result<Statement, ParserError> {
3777        let concurrently = self.parse_keyword(Keyword::CONCURRENTLY);
3778        let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
3779        let index_name = if if_not_exists || !self.parse_keyword(Keyword::ON) {
3780            let index_name = self.parse_object_name()?;
3781            self.expect_keyword(Keyword::ON)?;
3782            Some(index_name)
3783        } else {
3784            None
3785        };
3786        let table_name = self.parse_object_name()?;
3787        let using = if self.parse_keyword(Keyword::USING) {
3788            Some(self.parse_identifier()?)
3789        } else {
3790            None
3791        };
3792        self.expect_token(&Token::LParen)?;
3793        let columns = self.parse_comma_separated(Parser::parse_order_by_expr)?;
3794        self.expect_token(&Token::RParen)?;
3795
3796        let include = if self.parse_keyword(Keyword::INCLUDE) {
3797            self.expect_token(&Token::LParen)?;
3798            let columns = self.parse_comma_separated(Parser::parse_identifier)?;
3799            self.expect_token(&Token::RParen)?;
3800            columns
3801        } else {
3802            vec![]
3803        };
3804
3805        let nulls_distinct = if self.parse_keyword(Keyword::NULLS) {
3806            let not = self.parse_keyword(Keyword::NOT);
3807            self.expect_keyword(Keyword::DISTINCT)?;
3808            Some(!not)
3809        } else {
3810            None
3811        };
3812
3813        let predicate = if self.parse_keyword(Keyword::WHERE) {
3814            Some(self.parse_expr()?)
3815        } else {
3816            None
3817        };
3818
3819        Ok(Statement::CreateIndex {
3820            name: index_name,
3821            table_name,
3822            using,
3823            columns,
3824            unique,
3825            concurrently,
3826            if_not_exists,
3827            include,
3828            nulls_distinct,
3829            predicate,
3830        })
3831    }
3832
3833    //TODO: Implement parsing for Skewed and Clustered
3834    pub fn parse_hive_distribution(&mut self) -> Result<HiveDistributionStyle, ParserError> {
3835        if self.parse_keywords(&[Keyword::PARTITIONED, Keyword::BY]) {
3836            self.expect_token(&Token::LParen)?;
3837            let columns = self.parse_comma_separated(Parser::parse_column_def)?;
3838            self.expect_token(&Token::RParen)?;
3839            Ok(HiveDistributionStyle::PARTITIONED { columns })
3840        } else {
3841            Ok(HiveDistributionStyle::NONE)
3842        }
3843    }
3844
3845    pub fn parse_hive_formats(&mut self) -> Result<HiveFormat, ParserError> {
3846        let mut hive_format = HiveFormat::default();
3847        loop {
3848            match self.parse_one_of_keywords(&[Keyword::ROW, Keyword::STORED, Keyword::LOCATION]) {
3849                Some(Keyword::ROW) => {
3850                    hive_format.row_format = Some(self.parse_row_format()?);
3851                }
3852                Some(Keyword::STORED) => {
3853                    self.expect_keyword(Keyword::AS)?;
3854                    if self.parse_keyword(Keyword::INPUTFORMAT) {
3855                        let input_format = self.parse_expr()?;
3856                        self.expect_keyword(Keyword::OUTPUTFORMAT)?;
3857                        let output_format = self.parse_expr()?;
3858                        hive_format.storage = Some(HiveIOFormat::IOF {
3859                            input_format,
3860                            output_format,
3861                        });
3862                    } else {
3863                        let format = self.parse_file_format()?;
3864                        hive_format.storage = Some(HiveIOFormat::FileFormat { format });
3865                    }
3866                }
3867                Some(Keyword::LOCATION) => {
3868                    hive_format.location = Some(self.parse_literal_string()?);
3869                }
3870                None => break,
3871                _ => break,
3872            }
3873        }
3874
3875        Ok(hive_format)
3876    }
3877
3878    pub fn parse_row_format(&mut self) -> Result<HiveRowFormat, ParserError> {
3879        self.expect_keyword(Keyword::FORMAT)?;
3880        match self.parse_one_of_keywords(&[Keyword::SERDE, Keyword::DELIMITED]) {
3881            Some(Keyword::SERDE) => {
3882                let class = self.parse_literal_string()?;
3883                Ok(HiveRowFormat::SERDE { class })
3884            }
3885            _ => Ok(HiveRowFormat::DELIMITED),
3886        }
3887    }
3888
3889    pub fn parse_create_table(
3890        &mut self,
3891        or_replace: bool,
3892        temporary: bool,
3893        global: Option<bool>,
3894        transient: bool,
3895    ) -> Result<Statement, ParserError> {
3896        let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
3897        let table_name = self.parse_object_name()?;
3898
3899        // Clickhouse has `ON CLUSTER 'cluster'` syntax for DDLs
3900        let on_cluster = if self.parse_keywords(&[Keyword::ON, Keyword::CLUSTER]) {
3901            let next_token = self.next_token();
3902            match next_token.token {
3903                Token::SingleQuotedString(s) => Some(s),
3904                Token::Word(s) => Some(s.to_string()),
3905                _ => self.expected("identifier or cluster literal", next_token)?,
3906            }
3907        } else {
3908            None
3909        };
3910
3911        let like = if self.parse_keyword(Keyword::LIKE) || self.parse_keyword(Keyword::ILIKE) {
3912            self.parse_object_name().ok()
3913        } else {
3914            None
3915        };
3916
3917        let clone = if self.parse_keyword(Keyword::CLONE) {
3918            self.parse_object_name().ok()
3919        } else {
3920            None
3921        };
3922
3923        // parse optional column list (schema)
3924        let (columns, constraints) = self.parse_columns()?;
3925
3926        // SQLite supports `WITHOUT ROWID` at the end of `CREATE TABLE`
3927        let without_rowid = self.parse_keywords(&[Keyword::WITHOUT, Keyword::ROWID]);
3928
3929        let hive_distribution = self.parse_hive_distribution()?;
3930        let hive_formats = self.parse_hive_formats()?;
3931        // PostgreSQL supports `WITH ( options )`, before `AS`
3932        let with_options = self.parse_options(Keyword::WITH)?;
3933        let table_properties = self.parse_options(Keyword::TBLPROPERTIES)?;
3934
3935        let engine = if self.parse_keyword(Keyword::ENGINE) {
3936            self.expect_token(&Token::Eq)?;
3937            let next_token = self.next_token();
3938            match next_token.token {
3939                Token::Word(w) => Some(w.value),
3940                _ => self.expected("identifier", next_token)?,
3941            }
3942        } else {
3943            None
3944        };
3945
3946        let comment = if self.parse_keyword(Keyword::COMMENT) {
3947            let _ = self.consume_token(&Token::Eq);
3948            let next_token = self.next_token();
3949            match next_token.token {
3950                Token::SingleQuotedString(str) => Some(str),
3951                _ => self.expected("comment", next_token)?,
3952            }
3953        } else {
3954            None
3955        };
3956
3957        let auto_increment_offset = if self.parse_keyword(Keyword::AUTO_INCREMENT) {
3958            let _ = self.consume_token(&Token::Eq);
3959            let next_token = self.next_token();
3960            match next_token.token {
3961                Token::Number(s, _) => Some(s.parse::<u32>().expect("literal int")),
3962                _ => self.expected("literal int", next_token)?,
3963            }
3964        } else {
3965            None
3966        };
3967
3968        let order_by = if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
3969            if self.consume_token(&Token::LParen) {
3970                let columns = if self.peek_token() != Token::RParen {
3971                    self.parse_comma_separated(Parser::parse_identifier)?
3972                } else {
3973                    vec![]
3974                };
3975                self.expect_token(&Token::RParen)?;
3976                Some(columns)
3977            } else {
3978                Some(vec![self.parse_identifier()?])
3979            }
3980        } else {
3981            None
3982        };
3983
3984        // Parse optional `AS ( query )`
3985        let query = if self.parse_keyword(Keyword::AS) {
3986            Some(Box::new(self.parse_query()?))
3987        } else {
3988            None
3989        };
3990
3991        let default_charset = if self.parse_keywords(&[Keyword::DEFAULT, Keyword::CHARSET]) {
3992            self.expect_token(&Token::Eq)?;
3993            let next_token = self.next_token();
3994            match next_token.token {
3995                Token::Word(w) => Some(w.value),
3996                _ => self.expected("identifier", next_token)?,
3997            }
3998        } else {
3999            None
4000        };
4001
4002        let collation = if self.parse_keywords(&[Keyword::COLLATE]) {
4003            self.expect_token(&Token::Eq)?;
4004            let next_token = self.next_token();
4005            match next_token.token {
4006                Token::Word(w) => Some(w.value),
4007                _ => self.expected("identifier", next_token)?,
4008            }
4009        } else {
4010            None
4011        };
4012
4013        let on_commit: Option<OnCommit> =
4014            if self.parse_keywords(&[Keyword::ON, Keyword::COMMIT, Keyword::DELETE, Keyword::ROWS])
4015            {
4016                Some(OnCommit::DeleteRows)
4017            } else if self.parse_keywords(&[
4018                Keyword::ON,
4019                Keyword::COMMIT,
4020                Keyword::PRESERVE,
4021                Keyword::ROWS,
4022            ]) {
4023                Some(OnCommit::PreserveRows)
4024            } else if self.parse_keywords(&[Keyword::ON, Keyword::COMMIT, Keyword::DROP]) {
4025                Some(OnCommit::Drop)
4026            } else {
4027                None
4028            };
4029
4030        let strict = self.parse_keyword(Keyword::STRICT);
4031        Ok(CreateTableBuilder::new(table_name)
4032            .temporary(temporary)
4033            .columns(columns)
4034            .constraints(constraints)
4035            .with_options(with_options)
4036            .table_properties(table_properties)
4037            .or_replace(or_replace)
4038            .if_not_exists(if_not_exists)
4039            .transient(transient)
4040            .hive_distribution(hive_distribution)
4041            .hive_formats(Some(hive_formats))
4042            .global(global)
4043            .query(query)
4044            .without_rowid(without_rowid)
4045            .like(like)
4046            .clone_clause(clone)
4047            .engine(engine)
4048            .comment(comment)
4049            .auto_increment_offset(auto_increment_offset)
4050            .order_by(order_by)
4051            .default_charset(default_charset)
4052            .collation(collation)
4053            .on_commit(on_commit)
4054            .on_cluster(on_cluster)
4055            .strict(strict)
4056            .build())
4057    }
4058
4059    pub fn parse_optional_procedure_parameters(
4060        &mut self,
4061    ) -> Result<Option<Vec<ProcedureParam>>, ParserError> {
4062        let mut params = vec![];
4063        if !self.consume_token(&Token::LParen) || self.consume_token(&Token::RParen) {
4064            return Ok(Some(params));
4065        }
4066        loop {
4067            if let Token::Word(_) = self.peek_token().token {
4068                params.push(self.parse_procedure_param()?)
4069            }
4070            let comma = self.consume_token(&Token::Comma);
4071            if self.consume_token(&Token::RParen) {
4072                // allow a trailing comma, even though it's not in standard
4073                break;
4074            } else if !comma {
4075                return self.expected("',' or ')' after parameter definition", self.peek_token());
4076            }
4077        }
4078        Ok(Some(params))
4079    }
4080
4081    pub fn parse_columns(&mut self) -> Result<(Vec<ColumnDef>, Vec<TableConstraint>), ParserError> {
4082        let mut columns = vec![];
4083        let mut constraints = vec![];
4084        if !self.consume_token(&Token::LParen) || self.consume_token(&Token::RParen) {
4085            return Ok((columns, constraints));
4086        }
4087
4088        loop {
4089            if let Some(constraint) = self.parse_optional_table_constraint()? {
4090                constraints.push(constraint);
4091            } else if let Token::Word(_) = self.peek_token().token {
4092                columns.push(self.parse_column_def()?);
4093            } else {
4094                return self.expected("column name or constraint definition", self.peek_token());
4095            }
4096            let comma = self.consume_token(&Token::Comma);
4097            if self.consume_token(&Token::RParen) {
4098                // allow a trailing comma, even though it's not in standard
4099                break;
4100            } else if !comma {
4101                return self.expected("',' or ')' after column definition", self.peek_token());
4102            }
4103        }
4104
4105        Ok((columns, constraints))
4106    }
4107
4108    pub fn parse_procedure_param(&mut self) -> Result<ProcedureParam, ParserError> {
4109        let name = self.parse_identifier()?;
4110        let data_type = self.parse_data_type()?;
4111        Ok(ProcedureParam { name, data_type })
4112    }
4113
4114    pub fn parse_column_def(&mut self) -> Result<ColumnDef, ParserError> {
4115        let name = self.parse_identifier()?;
4116        let data_type = self.parse_data_type()?;
4117        let collation = if self.parse_keyword(Keyword::COLLATE) {
4118            Some(self.parse_object_name()?)
4119        } else {
4120            None
4121        };
4122        let mut options = vec![];
4123        loop {
4124            if self.parse_keyword(Keyword::CONSTRAINT) {
4125                let name = Some(self.parse_identifier()?);
4126                if let Some(option) = self.parse_optional_column_option()? {
4127                    options.push(ColumnOptionDef { name, option });
4128                } else {
4129                    return self.expected(
4130                        "constraint details after CONSTRAINT <name>",
4131                        self.peek_token(),
4132                    );
4133                }
4134            } else if let Some(option) = self.parse_optional_column_option()? {
4135                options.push(ColumnOptionDef { name: None, option });
4136            } else {
4137                break;
4138            };
4139        }
4140        Ok(ColumnDef {
4141            name,
4142            data_type,
4143            collation,
4144            options,
4145        })
4146    }
4147
4148    pub fn parse_optional_column_option(&mut self) -> Result<Option<ColumnOption>, ParserError> {
4149        if self.parse_keywords(&[Keyword::CHARACTER, Keyword::SET]) {
4150            Ok(Some(ColumnOption::CharacterSet(self.parse_object_name()?)))
4151        } else if self.parse_keywords(&[Keyword::NOT, Keyword::NULL]) {
4152            Ok(Some(ColumnOption::NotNull))
4153        } else if self.parse_keywords(&[Keyword::COMMENT]) {
4154            let next_token = self.next_token();
4155            match next_token.token {
4156                Token::SingleQuotedString(value, ..) => Ok(Some(ColumnOption::Comment(value))),
4157                _ => self.expected("string", next_token),
4158            }
4159        } else if self.parse_keyword(Keyword::NULL) {
4160            Ok(Some(ColumnOption::Null))
4161        } else if self.parse_keyword(Keyword::DEFAULT) {
4162            Ok(Some(ColumnOption::Default(self.parse_expr()?)))
4163        } else if self.parse_keywords(&[Keyword::PRIMARY, Keyword::KEY]) {
4164            Ok(Some(ColumnOption::Unique { is_primary: true }))
4165        } else if self.parse_keyword(Keyword::UNIQUE) {
4166            Ok(Some(ColumnOption::Unique { is_primary: false }))
4167        } else if self.parse_keyword(Keyword::REFERENCES) {
4168            let foreign_table = self.parse_object_name()?;
4169            // PostgreSQL allows omitting the column list and
4170            // uses the primary key column of the foreign table by default
4171            let referred_columns = self.parse_parenthesized_column_list(Optional, false)?;
4172            let mut on_delete = None;
4173            let mut on_update = None;
4174            loop {
4175                if on_delete.is_none() && self.parse_keywords(&[Keyword::ON, Keyword::DELETE]) {
4176                    on_delete = Some(self.parse_referential_action()?);
4177                } else if on_update.is_none()
4178                    && self.parse_keywords(&[Keyword::ON, Keyword::UPDATE])
4179                {
4180                    on_update = Some(self.parse_referential_action()?);
4181                } else {
4182                    break;
4183                }
4184            }
4185            Ok(Some(ColumnOption::ForeignKey {
4186                foreign_table,
4187                referred_columns,
4188                on_delete,
4189                on_update,
4190            }))
4191        } else if self.parse_keyword(Keyword::CHECK) {
4192            self.expect_token(&Token::LParen)?;
4193            let expr = self.parse_expr()?;
4194            self.expect_token(&Token::RParen)?;
4195            Ok(Some(ColumnOption::Check(expr)))
4196        } else if self.parse_keyword(Keyword::AUTO_INCREMENT)
4197            && dialect_of!(self is MySqlDialect | GenericDialect)
4198        {
4199            // Support AUTO_INCREMENT for MySQL
4200            Ok(Some(ColumnOption::DialectSpecific(vec![
4201                Token::make_keyword("AUTO_INCREMENT"),
4202            ])))
4203        } else if self.parse_keyword(Keyword::AUTOINCREMENT)
4204            && dialect_of!(self is SQLiteDialect |  GenericDialect)
4205        {
4206            // Support AUTOINCREMENT for SQLite
4207            Ok(Some(ColumnOption::DialectSpecific(vec![
4208                Token::make_keyword("AUTOINCREMENT"),
4209            ])))
4210        } else if self.parse_keywords(&[Keyword::ON, Keyword::UPDATE])
4211            && dialect_of!(self is MySqlDialect | GenericDialect)
4212        {
4213            let expr = self.parse_expr()?;
4214            Ok(Some(ColumnOption::OnUpdate(expr)))
4215        } else if self.parse_keyword(Keyword::GENERATED) {
4216            self.parse_optional_column_option_generated()
4217        } else {
4218            Ok(None)
4219        }
4220    }
4221    fn parse_optional_column_option_generated(
4222        &mut self,
4223    ) -> Result<Option<ColumnOption>, ParserError> {
4224        if self.parse_keywords(&[Keyword::ALWAYS, Keyword::AS, Keyword::IDENTITY]) {
4225            let mut sequence_options = vec![];
4226            if self.expect_token(&Token::LParen).is_ok() {
4227                sequence_options = self.parse_create_sequence_options()?;
4228                self.expect_token(&Token::RParen)?;
4229            }
4230            Ok(Some(ColumnOption::Generated {
4231                generated_as: GeneratedAs::Always,
4232                sequence_options: Some(sequence_options),
4233                generation_expr: None,
4234            }))
4235        } else if self.parse_keywords(&[
4236            Keyword::BY,
4237            Keyword::DEFAULT,
4238            Keyword::AS,
4239            Keyword::IDENTITY,
4240        ]) {
4241            let mut sequence_options = vec![];
4242            if self.expect_token(&Token::LParen).is_ok() {
4243                sequence_options = self.parse_create_sequence_options()?;
4244                self.expect_token(&Token::RParen)?;
4245            }
4246            Ok(Some(ColumnOption::Generated {
4247                generated_as: GeneratedAs::ByDefault,
4248                sequence_options: Some(sequence_options),
4249                generation_expr: None,
4250            }))
4251        } else if self.parse_keywords(&[Keyword::ALWAYS, Keyword::AS]) {
4252            if self.expect_token(&Token::LParen).is_ok() {
4253                let expr = self.parse_expr()?;
4254                self.expect_token(&Token::RParen)?;
4255                let _ = self.parse_keywords(&[Keyword::STORED]);
4256                Ok(Some(ColumnOption::Generated {
4257                    generated_as: GeneratedAs::ExpStored,
4258                    sequence_options: None,
4259                    generation_expr: Some(expr),
4260                }))
4261            } else {
4262                Ok(None)
4263            }
4264        } else {
4265            Ok(None)
4266        }
4267    }
4268
4269    pub fn parse_referential_action(&mut self) -> Result<ReferentialAction, ParserError> {
4270        if self.parse_keyword(Keyword::RESTRICT) {
4271            Ok(ReferentialAction::Restrict)
4272        } else if self.parse_keyword(Keyword::CASCADE) {
4273            Ok(ReferentialAction::Cascade)
4274        } else if self.parse_keywords(&[Keyword::SET, Keyword::NULL]) {
4275            Ok(ReferentialAction::SetNull)
4276        } else if self.parse_keywords(&[Keyword::NO, Keyword::ACTION]) {
4277            Ok(ReferentialAction::NoAction)
4278        } else if self.parse_keywords(&[Keyword::SET, Keyword::DEFAULT]) {
4279            Ok(ReferentialAction::SetDefault)
4280        } else {
4281            self.expected(
4282                "one of RESTRICT, CASCADE, SET NULL, NO ACTION or SET DEFAULT",
4283                self.peek_token(),
4284            )
4285        }
4286    }
4287
4288    pub fn parse_optional_table_constraint(
4289        &mut self,
4290    ) -> Result<Option<TableConstraint>, ParserError> {
4291        let name = if self.parse_keyword(Keyword::CONSTRAINT) {
4292            Some(self.parse_identifier()?)
4293        } else {
4294            None
4295        };
4296
4297        let next_token = self.next_token();
4298        match next_token.token {
4299            Token::Word(w) if w.keyword == Keyword::PRIMARY || w.keyword == Keyword::UNIQUE => {
4300                let is_primary = w.keyword == Keyword::PRIMARY;
4301
4302                // parse optional [KEY]
4303                let _ = self.parse_keyword(Keyword::KEY);
4304
4305                // optional constraint name
4306                let name = self
4307                    .maybe_parse(|parser| parser.parse_identifier())
4308                    .or(name);
4309
4310                let columns = self.parse_parenthesized_column_list(Mandatory, false)?;
4311                Ok(Some(TableConstraint::Unique {
4312                    name,
4313                    columns,
4314                    is_primary,
4315                }))
4316            }
4317            Token::Word(w) if w.keyword == Keyword::FOREIGN => {
4318                self.expect_keyword(Keyword::KEY)?;
4319                let columns = self.parse_parenthesized_column_list(Mandatory, false)?;
4320                self.expect_keyword(Keyword::REFERENCES)?;
4321                let foreign_table = self.parse_object_name()?;
4322                let referred_columns = self.parse_parenthesized_column_list(Mandatory, false)?;
4323                let mut on_delete = None;
4324                let mut on_update = None;
4325                loop {
4326                    if on_delete.is_none() && self.parse_keywords(&[Keyword::ON, Keyword::DELETE]) {
4327                        on_delete = Some(self.parse_referential_action()?);
4328                    } else if on_update.is_none()
4329                        && self.parse_keywords(&[Keyword::ON, Keyword::UPDATE])
4330                    {
4331                        on_update = Some(self.parse_referential_action()?);
4332                    } else {
4333                        break;
4334                    }
4335                }
4336                Ok(Some(TableConstraint::ForeignKey {
4337                    name,
4338                    columns,
4339                    foreign_table,
4340                    referred_columns,
4341                    on_delete,
4342                    on_update,
4343                }))
4344            }
4345            Token::Word(w) if w.keyword == Keyword::CHECK => {
4346                self.expect_token(&Token::LParen)?;
4347                let expr = Box::new(self.parse_expr()?);
4348                self.expect_token(&Token::RParen)?;
4349                Ok(Some(TableConstraint::Check { name, expr }))
4350            }
4351            Token::Word(w)
4352                if (w.keyword == Keyword::INDEX || w.keyword == Keyword::KEY)
4353                    && dialect_of!(self is GenericDialect | MySqlDialect) =>
4354            {
4355                let display_as_key = w.keyword == Keyword::KEY;
4356
4357                let name = match self.peek_token().token {
4358                    Token::Word(word) if word.keyword == Keyword::USING => None,
4359                    _ => self.maybe_parse(|parser| parser.parse_identifier()),
4360                };
4361
4362                let index_type = if self.parse_keyword(Keyword::USING) {
4363                    Some(self.parse_index_type()?)
4364                } else {
4365                    None
4366                };
4367                let columns = self.parse_parenthesized_column_list(Mandatory, false)?;
4368
4369                Ok(Some(TableConstraint::Index {
4370                    display_as_key,
4371                    name,
4372                    index_type,
4373                    columns,
4374                }))
4375            }
4376            Token::Word(w)
4377                if (w.keyword == Keyword::FULLTEXT || w.keyword == Keyword::SPATIAL)
4378                    && dialect_of!(self is GenericDialect | MySqlDialect) =>
4379            {
4380                if let Some(name) = name {
4381                    return self.expected(
4382                        "FULLTEXT or SPATIAL option without constraint name",
4383                        TokenWithLocation {
4384                            token: Token::make_keyword(&name.to_string()),
4385                            location: next_token.location,
4386                        },
4387                    );
4388                }
4389
4390                let fulltext = w.keyword == Keyword::FULLTEXT;
4391
4392                let index_type_display = if self.parse_keyword(Keyword::KEY) {
4393                    KeyOrIndexDisplay::Key
4394                } else if self.parse_keyword(Keyword::INDEX) {
4395                    KeyOrIndexDisplay::Index
4396                } else {
4397                    KeyOrIndexDisplay::None
4398                };
4399
4400                let opt_index_name = self.maybe_parse(|parser| parser.parse_identifier());
4401
4402                let columns = self.parse_parenthesized_column_list(Mandatory, false)?;
4403
4404                Ok(Some(TableConstraint::FulltextOrSpatial {
4405                    fulltext,
4406                    index_type_display,
4407                    opt_index_name,
4408                    columns,
4409                }))
4410            }
4411            _ => {
4412                if name.is_some() {
4413                    self.expected("PRIMARY, UNIQUE, FOREIGN, or CHECK", next_token)
4414                } else {
4415                    self.prev_token();
4416                    Ok(None)
4417                }
4418            }
4419        }
4420    }
4421
4422    pub fn parse_options(&mut self, keyword: Keyword) -> Result<Vec<SqlOption>, ParserError> {
4423        if self.parse_keyword(keyword) {
4424            self.expect_token(&Token::LParen)?;
4425            let options = self.parse_comma_separated(Parser::parse_sql_option)?;
4426            self.expect_token(&Token::RParen)?;
4427            Ok(options)
4428        } else {
4429            Ok(vec![])
4430        }
4431    }
4432
4433    pub fn parse_index_type(&mut self) -> Result<IndexType, ParserError> {
4434        if self.parse_keyword(Keyword::BTREE) {
4435            Ok(IndexType::BTree)
4436        } else if self.parse_keyword(Keyword::HASH) {
4437            Ok(IndexType::Hash)
4438        } else {
4439            self.expected("index type {BTREE | HASH}", self.peek_token())
4440        }
4441    }
4442
4443    pub fn parse_sql_option(&mut self) -> Result<SqlOption, ParserError> {
4444        let name = self.parse_identifier()?;
4445        self.expect_token(&Token::Eq)?;
4446        let value = self.parse_value()?;
4447        Ok(SqlOption { name, value })
4448    }
4449
4450    pub fn parse_partition(&mut self) -> Result<Partition, ParserError> {
4451        self.expect_token(&Token::LParen)?;
4452        let partitions = self.parse_comma_separated(Parser::parse_expr)?;
4453        self.expect_token(&Token::RParen)?;
4454        Ok(Partition { partitions })
4455    }
4456
4457    pub fn parse_alter_table_operation(&mut self) -> Result<AlterTableOperation, ParserError> {
4458        let operation = if self.parse_keyword(Keyword::ADD) {
4459            if let Some(constraint) = self.parse_optional_table_constraint()? {
4460                AlterTableOperation::AddConstraint(constraint)
4461            } else {
4462                let if_not_exists =
4463                    self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
4464                let mut new_partitions = vec![];
4465                loop {
4466                    if self.parse_keyword(Keyword::PARTITION) {
4467                        new_partitions.push(self.parse_partition()?);
4468                    } else {
4469                        break;
4470                    }
4471                }
4472                if !new_partitions.is_empty() {
4473                    AlterTableOperation::AddPartitions {
4474                        if_not_exists,
4475                        new_partitions,
4476                    }
4477                } else {
4478                    let column_keyword = self.parse_keyword(Keyword::COLUMN);
4479
4480                    let if_not_exists = if dialect_of!(self is PostgreSqlDialect | BigQueryDialect | DuckDbDialect | GenericDialect)
4481                    {
4482                        self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS])
4483                            || if_not_exists
4484                    } else {
4485                        false
4486                    };
4487
4488                    let column_def = self.parse_column_def()?;
4489                    AlterTableOperation::AddColumn {
4490                        column_keyword,
4491                        if_not_exists,
4492                        column_def,
4493                    }
4494                }
4495            }
4496        } else if self.parse_keyword(Keyword::RENAME) {
4497            if dialect_of!(self is PostgreSqlDialect) && self.parse_keyword(Keyword::CONSTRAINT) {
4498                let old_name = self.parse_identifier()?;
4499                self.expect_keyword(Keyword::TO)?;
4500                let new_name = self.parse_identifier()?;
4501                AlterTableOperation::RenameConstraint { old_name, new_name }
4502            } else if self.parse_keyword(Keyword::TO) {
4503                let table_name = self.parse_object_name()?;
4504                AlterTableOperation::RenameTable { table_name }
4505            } else {
4506                let _ = self.parse_keyword(Keyword::COLUMN); // [ COLUMN ]
4507                let old_column_name = self.parse_identifier()?;
4508                self.expect_keyword(Keyword::TO)?;
4509                let new_column_name = self.parse_identifier()?;
4510                AlterTableOperation::RenameColumn {
4511                    old_column_name,
4512                    new_column_name,
4513                }
4514            }
4515        } else if self.parse_keyword(Keyword::DROP) {
4516            if self.parse_keywords(&[Keyword::IF, Keyword::EXISTS, Keyword::PARTITION]) {
4517                self.expect_token(&Token::LParen)?;
4518                let partitions = self.parse_comma_separated(Parser::parse_expr)?;
4519                self.expect_token(&Token::RParen)?;
4520                AlterTableOperation::DropPartitions {
4521                    partitions,
4522                    if_exists: true,
4523                }
4524            } else if self.parse_keyword(Keyword::PARTITION) {
4525                self.expect_token(&Token::LParen)?;
4526                let partitions = self.parse_comma_separated(Parser::parse_expr)?;
4527                self.expect_token(&Token::RParen)?;
4528                AlterTableOperation::DropPartitions {
4529                    partitions,
4530                    if_exists: false,
4531                }
4532            } else if self.parse_keyword(Keyword::CONSTRAINT) {
4533                let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
4534                let name = self.parse_identifier()?;
4535                let cascade = self.parse_keyword(Keyword::CASCADE);
4536                AlterTableOperation::DropConstraint {
4537                    if_exists,
4538                    name,
4539                    cascade,
4540                }
4541            } else if self.parse_keywords(&[Keyword::PRIMARY, Keyword::KEY])
4542                && dialect_of!(self is MySqlDialect | GenericDialect)
4543            {
4544                AlterTableOperation::DropPrimaryKey
4545            } else {
4546                let _ = self.parse_keyword(Keyword::COLUMN); // [ COLUMN ]
4547                let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
4548                let column_name = self.parse_identifier()?;
4549                let cascade = self.parse_keyword(Keyword::CASCADE);
4550                AlterTableOperation::DropColumn {
4551                    column_name,
4552                    if_exists,
4553                    cascade,
4554                }
4555            }
4556        } else if self.parse_keyword(Keyword::PARTITION) {
4557            self.expect_token(&Token::LParen)?;
4558            let before = self.parse_comma_separated(Parser::parse_expr)?;
4559            self.expect_token(&Token::RParen)?;
4560            self.expect_keyword(Keyword::RENAME)?;
4561            self.expect_keywords(&[Keyword::TO, Keyword::PARTITION])?;
4562            self.expect_token(&Token::LParen)?;
4563            let renames = self.parse_comma_separated(Parser::parse_expr)?;
4564            self.expect_token(&Token::RParen)?;
4565            AlterTableOperation::RenamePartitions {
4566                old_partitions: before,
4567                new_partitions: renames,
4568            }
4569        } else if self.parse_keyword(Keyword::CHANGE) {
4570            let _ = self.parse_keyword(Keyword::COLUMN); // [ COLUMN ]
4571            let old_name = self.parse_identifier()?;
4572            let new_name = self.parse_identifier()?;
4573            let data_type = self.parse_data_type()?;
4574            let mut options = vec![];
4575            while let Some(option) = self.parse_optional_column_option()? {
4576                options.push(option);
4577            }
4578
4579            AlterTableOperation::ChangeColumn {
4580                old_name,
4581                new_name,
4582                data_type,
4583                options,
4584            }
4585        } else if self.parse_keyword(Keyword::ALTER) {
4586            let _ = self.parse_keyword(Keyword::COLUMN); // [ COLUMN ]
4587            let column_name = self.parse_identifier()?;
4588            let is_postgresql = dialect_of!(self is PostgreSqlDialect);
4589
4590            let op = if self.parse_keywords(&[Keyword::SET, Keyword::NOT, Keyword::NULL]) {
4591                AlterColumnOperation::SetNotNull {}
4592            } else if self.parse_keywords(&[Keyword::DROP, Keyword::NOT, Keyword::NULL]) {
4593                AlterColumnOperation::DropNotNull {}
4594            } else if self.parse_keywords(&[Keyword::SET, Keyword::DEFAULT]) {
4595                AlterColumnOperation::SetDefault {
4596                    value: self.parse_expr()?,
4597                }
4598            } else if self.parse_keywords(&[Keyword::DROP, Keyword::DEFAULT]) {
4599                AlterColumnOperation::DropDefault {}
4600            } else if self.parse_keywords(&[Keyword::SET, Keyword::DATA, Keyword::TYPE])
4601                || (is_postgresql && self.parse_keyword(Keyword::TYPE))
4602            {
4603                let data_type = self.parse_data_type()?;
4604                let using = if is_postgresql && self.parse_keyword(Keyword::USING) {
4605                    Some(self.parse_expr()?)
4606                } else {
4607                    None
4608                };
4609                AlterColumnOperation::SetDataType { data_type, using }
4610            } else {
4611                return self.expected(
4612                    "SET/DROP NOT NULL, SET DEFAULT, SET DATA TYPE after ALTER COLUMN",
4613                    self.peek_token(),
4614                );
4615            };
4616            AlterTableOperation::AlterColumn { column_name, op }
4617        } else if self.parse_keyword(Keyword::SWAP) {
4618            self.expect_keyword(Keyword::WITH)?;
4619            let table_name = self.parse_object_name()?;
4620            AlterTableOperation::SwapWith { table_name }
4621        } else {
4622            return self.expected(
4623                "ADD, RENAME, PARTITION, SWAP or DROP after ALTER TABLE",
4624                self.peek_token(),
4625            );
4626        };
4627        Ok(operation)
4628    }
4629
4630    pub fn parse_alter(&mut self) -> Result<Statement, ParserError> {
4631        let object_type = self.expect_one_of_keywords(&[
4632            Keyword::VIEW,
4633            Keyword::TABLE,
4634            Keyword::INDEX,
4635            Keyword::ROLE,
4636        ])?;
4637        match object_type {
4638            Keyword::VIEW => self.parse_alter_view(),
4639            Keyword::TABLE => {
4640                let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
4641                let only = self.parse_keyword(Keyword::ONLY); // [ ONLY ]
4642                let table_name = self.parse_object_name()?;
4643                let operations = self.parse_comma_separated(Parser::parse_alter_table_operation)?;
4644                Ok(Statement::AlterTable {
4645                    name: table_name,
4646                    if_exists,
4647                    only,
4648                    operations,
4649                })
4650            }
4651            Keyword::INDEX => {
4652                let index_name = self.parse_object_name()?;
4653                let operation = if self.parse_keyword(Keyword::RENAME) {
4654                    if self.parse_keyword(Keyword::TO) {
4655                        let index_name = self.parse_object_name()?;
4656                        AlterIndexOperation::RenameIndex { index_name }
4657                    } else {
4658                        return self.expected("TO after RENAME", self.peek_token());
4659                    }
4660                } else {
4661                    return self.expected("RENAME after ALTER INDEX", self.peek_token());
4662                };
4663
4664                Ok(Statement::AlterIndex {
4665                    name: index_name,
4666                    operation,
4667                })
4668            }
4669            Keyword::ROLE => self.parse_alter_role(),
4670            // unreachable because expect_one_of_keywords used above
4671            _ => unreachable!(),
4672        }
4673    }
4674
4675    pub fn parse_alter_view(&mut self) -> Result<Statement, ParserError> {
4676        let name = self.parse_object_name()?;
4677        let columns = self.parse_parenthesized_column_list(Optional, false)?;
4678
4679        let with_options = self.parse_options(Keyword::WITH)?;
4680
4681        self.expect_keyword(Keyword::AS)?;
4682        let query = Box::new(self.parse_query()?);
4683
4684        Ok(Statement::AlterView {
4685            name,
4686            columns,
4687            query,
4688            with_options,
4689        })
4690    }
4691
4692    /// Parse a copy statement
4693    pub fn parse_copy(&mut self) -> Result<Statement, ParserError> {
4694        let source;
4695        if self.consume_token(&Token::LParen) {
4696            source = CopySource::Query(Box::new(self.parse_query()?));
4697            self.expect_token(&Token::RParen)?;
4698        } else {
4699            let table_name = self.parse_object_name()?;
4700            let columns = self.parse_parenthesized_column_list(Optional, false)?;
4701            source = CopySource::Table {
4702                table_name,
4703                columns,
4704            };
4705        }
4706        let to = match self.parse_one_of_keywords(&[Keyword::FROM, Keyword::TO]) {
4707            Some(Keyword::FROM) => false,
4708            Some(Keyword::TO) => true,
4709            _ => self.expected("FROM or TO", self.peek_token())?,
4710        };
4711        if !to {
4712            // Use a separate if statement to prevent Rust compiler from complaining about
4713            // "if statement in this position is unstable: https://github.com/rust-lang/rust/issues/53667"
4714            if let CopySource::Query(_) = source {
4715                return Err(ParserError::ParserError(
4716                    "COPY ... FROM does not support query as a source".to_string(),
4717                ));
4718            }
4719        }
4720        let target = if self.parse_keyword(Keyword::STDIN) {
4721            CopyTarget::Stdin
4722        } else if self.parse_keyword(Keyword::STDOUT) {
4723            CopyTarget::Stdout
4724        } else if self.parse_keyword(Keyword::PROGRAM) {
4725            CopyTarget::Program {
4726                command: self.parse_literal_string()?,
4727            }
4728        } else {
4729            CopyTarget::File {
4730                filename: self.parse_literal_string()?,
4731            }
4732        };
4733        let _ = self.parse_keyword(Keyword::WITH); // [ WITH ]
4734        let mut options = vec![];
4735        if self.consume_token(&Token::LParen) {
4736            options = self.parse_comma_separated(Parser::parse_copy_option)?;
4737            self.expect_token(&Token::RParen)?;
4738        }
4739        let mut legacy_options = vec![];
4740        while let Some(opt) = self.maybe_parse(|parser| parser.parse_copy_legacy_option()) {
4741            legacy_options.push(opt);
4742        }
4743        let values = if let CopyTarget::Stdin = target {
4744            self.expect_token(&Token::SemiColon)?;
4745            self.parse_tsv()
4746        } else {
4747            vec![]
4748        };
4749        Ok(Statement::Copy {
4750            source,
4751            to,
4752            target,
4753            options,
4754            legacy_options,
4755            values,
4756        })
4757    }
4758
4759    pub fn parse_close(&mut self) -> Result<Statement, ParserError> {
4760        let cursor = if self.parse_keyword(Keyword::ALL) {
4761            CloseCursor::All
4762        } else {
4763            let name = self.parse_identifier()?;
4764
4765            CloseCursor::Specific { name }
4766        };
4767
4768        Ok(Statement::Close { cursor })
4769    }
4770
4771    fn parse_copy_option(&mut self) -> Result<CopyOption, ParserError> {
4772        let ret = match self.parse_one_of_keywords(&[
4773            Keyword::FORMAT,
4774            Keyword::FREEZE,
4775            Keyword::DELIMITER,
4776            Keyword::NULL,
4777            Keyword::HEADER,
4778            Keyword::QUOTE,
4779            Keyword::ESCAPE,
4780            Keyword::FORCE_QUOTE,
4781            Keyword::FORCE_NOT_NULL,
4782            Keyword::FORCE_NULL,
4783            Keyword::ENCODING,
4784        ]) {
4785            Some(Keyword::FORMAT) => CopyOption::Format(self.parse_identifier()?),
4786            Some(Keyword::FREEZE) => CopyOption::Freeze(!matches!(
4787                self.parse_one_of_keywords(&[Keyword::TRUE, Keyword::FALSE]),
4788                Some(Keyword::FALSE)
4789            )),
4790            Some(Keyword::DELIMITER) => CopyOption::Delimiter(self.parse_literal_char()?),
4791            Some(Keyword::NULL) => CopyOption::Null(self.parse_literal_string()?),
4792            Some(Keyword::HEADER) => CopyOption::Header(!matches!(
4793                self.parse_one_of_keywords(&[Keyword::TRUE, Keyword::FALSE]),
4794                Some(Keyword::FALSE)
4795            )),
4796            Some(Keyword::QUOTE) => CopyOption::Quote(self.parse_literal_char()?),
4797            Some(Keyword::ESCAPE) => CopyOption::Escape(self.parse_literal_char()?),
4798            Some(Keyword::FORCE_QUOTE) => {
4799                CopyOption::ForceQuote(self.parse_parenthesized_column_list(Mandatory, false)?)
4800            }
4801            Some(Keyword::FORCE_NOT_NULL) => {
4802                CopyOption::ForceNotNull(self.parse_parenthesized_column_list(Mandatory, false)?)
4803            }
4804            Some(Keyword::FORCE_NULL) => {
4805                CopyOption::ForceNull(self.parse_parenthesized_column_list(Mandatory, false)?)
4806            }
4807            Some(Keyword::ENCODING) => CopyOption::Encoding(self.parse_literal_string()?),
4808            _ => self.expected("option", self.peek_token())?,
4809        };
4810        Ok(ret)
4811    }
4812
4813    fn parse_copy_legacy_option(&mut self) -> Result<CopyLegacyOption, ParserError> {
4814        let ret = match self.parse_one_of_keywords(&[
4815            Keyword::BINARY,
4816            Keyword::DELIMITER,
4817            Keyword::NULL,
4818            Keyword::CSV,
4819        ]) {
4820            Some(Keyword::BINARY) => CopyLegacyOption::Binary,
4821            Some(Keyword::DELIMITER) => {
4822                let _ = self.parse_keyword(Keyword::AS); // [ AS ]
4823                CopyLegacyOption::Delimiter(self.parse_literal_char()?)
4824            }
4825            Some(Keyword::NULL) => {
4826                let _ = self.parse_keyword(Keyword::AS); // [ AS ]
4827                CopyLegacyOption::Null(self.parse_literal_string()?)
4828            }
4829            Some(Keyword::CSV) => CopyLegacyOption::Csv({
4830                let mut opts = vec![];
4831                while let Some(opt) =
4832                    self.maybe_parse(|parser| parser.parse_copy_legacy_csv_option())
4833                {
4834                    opts.push(opt);
4835                }
4836                opts
4837            }),
4838            _ => self.expected("option", self.peek_token())?,
4839        };
4840        Ok(ret)
4841    }
4842
4843    fn parse_copy_legacy_csv_option(&mut self) -> Result<CopyLegacyCsvOption, ParserError> {
4844        let ret = match self.parse_one_of_keywords(&[
4845            Keyword::HEADER,
4846            Keyword::QUOTE,
4847            Keyword::ESCAPE,
4848            Keyword::FORCE,
4849        ]) {
4850            Some(Keyword::HEADER) => CopyLegacyCsvOption::Header,
4851            Some(Keyword::QUOTE) => {
4852                let _ = self.parse_keyword(Keyword::AS); // [ AS ]
4853                CopyLegacyCsvOption::Quote(self.parse_literal_char()?)
4854            }
4855            Some(Keyword::ESCAPE) => {
4856                let _ = self.parse_keyword(Keyword::AS); // [ AS ]
4857                CopyLegacyCsvOption::Escape(self.parse_literal_char()?)
4858            }
4859            Some(Keyword::FORCE) if self.parse_keywords(&[Keyword::NOT, Keyword::NULL]) => {
4860                CopyLegacyCsvOption::ForceNotNull(
4861                    self.parse_comma_separated(Parser::parse_identifier)?,
4862                )
4863            }
4864            Some(Keyword::FORCE) if self.parse_keywords(&[Keyword::QUOTE]) => {
4865                CopyLegacyCsvOption::ForceQuote(
4866                    self.parse_comma_separated(Parser::parse_identifier)?,
4867                )
4868            }
4869            _ => self.expected("csv option", self.peek_token())?,
4870        };
4871        Ok(ret)
4872    }
4873
4874    fn parse_literal_char(&mut self) -> Result<char, ParserError> {
4875        let s = self.parse_literal_string()?;
4876        if s.len() != 1 {
4877            let loc = self
4878                .tokens
4879                .get(self.index - 1)
4880                .map_or(Location { line: 0, column: 0 }, |t| t.location);
4881            return parser_err!(format!("Expect a char, found {s:?}"), loc);
4882        }
4883        Ok(s.chars().next().unwrap())
4884    }
4885
4886    /// Parse a tab separated values in
4887    /// COPY payload
4888    pub fn parse_tsv(&mut self) -> Vec<Option<String>> {
4889        self.parse_tab_value()
4890    }
4891
4892    pub fn parse_tab_value(&mut self) -> Vec<Option<String>> {
4893        let mut values = vec![];
4894        let mut content = String::from("");
4895        while let Some(t) = self.next_token_no_skip().map(|t| &t.token) {
4896            match t {
4897                Token::Whitespace(Whitespace::Tab) => {
4898                    values.push(Some(content.to_string()));
4899                    content.clear();
4900                }
4901                Token::Whitespace(Whitespace::Newline) => {
4902                    values.push(Some(content.to_string()));
4903                    content.clear();
4904                }
4905                Token::Backslash => {
4906                    if self.consume_token(&Token::Period) {
4907                        return values;
4908                    }
4909                    if let Token::Word(w) = self.next_token().token {
4910                        if w.value == "N" {
4911                            values.push(None);
4912                        }
4913                    }
4914                }
4915                _ => {
4916                    content.push_str(&t.to_string());
4917                }
4918            }
4919        }
4920        values
4921    }
4922
4923    /// Parse a literal value (numbers, strings, date/time, booleans)
4924    pub fn parse_value(&mut self) -> Result<Value, ParserError> {
4925        let next_token = self.next_token();
4926        let location = next_token.location;
4927        match next_token.token {
4928            Token::Word(w) => match w.keyword {
4929                Keyword::TRUE => Ok(Value::Boolean(true)),
4930                Keyword::FALSE => Ok(Value::Boolean(false)),
4931                Keyword::NULL => Ok(Value::Null),
4932                Keyword::NoKeyword if w.quote_style.is_some() => match w.quote_style {
4933                    Some('"') => Ok(Value::DoubleQuotedString(w.value)),
4934                    Some('\'') => Ok(Value::SingleQuotedString(w.value)),
4935                    _ => self.expected(
4936                        "A value?",
4937                        TokenWithLocation {
4938                            token: Token::Word(w),
4939                            location,
4940                        },
4941                    )?,
4942                },
4943                // Case when Snowflake Semi-structured data like key:value
4944                Keyword::NoKeyword | Keyword::LOCATION | Keyword::TYPE | Keyword::DATE if dialect_of!(self is SnowflakeDialect | GenericDialect) => {
4945                    Ok(Value::UnQuotedString(w.value))
4946                }
4947                _ => self.expected(
4948                    "a concrete value",
4949                    TokenWithLocation {
4950                        token: Token::Word(w),
4951                        location,
4952                    },
4953                ),
4954            },
4955            // The call to n.parse() returns a bigdecimal when the
4956            // bigdecimal feature is enabled, and is otherwise a no-op
4957            // (i.e., it returns the input string).
4958            Token::Number(ref n, l) => match n.parse() {
4959                Ok(n) => Ok(Value::Number(n, l)),
4960                Err(e) => parser_err!(format!("Could not parse '{n}' as number: {e}"), location),
4961            },
4962            Token::SingleQuotedString(ref s) => Ok(Value::SingleQuotedString(s.to_string())),
4963            Token::DoubleQuotedString(ref s) => Ok(Value::DoubleQuotedString(s.to_string())),
4964            Token::DollarQuotedString(ref s) => Ok(Value::DollarQuotedString(s.clone())),
4965            Token::SingleQuotedByteStringLiteral(ref s) => {
4966                Ok(Value::SingleQuotedByteStringLiteral(s.clone()))
4967            }
4968            Token::DoubleQuotedByteStringLiteral(ref s) => {
4969                Ok(Value::DoubleQuotedByteStringLiteral(s.clone()))
4970            }
4971            Token::RawStringLiteral(ref s) => Ok(Value::RawStringLiteral(s.clone())),
4972            Token::NationalStringLiteral(ref s) => Ok(Value::NationalStringLiteral(s.to_string())),
4973            Token::EscapedStringLiteral(ref s) => Ok(Value::EscapedStringLiteral(s.to_string())),
4974            Token::HexStringLiteral(ref s) => Ok(Value::HexStringLiteral(s.to_string())),
4975            Token::Placeholder(ref s) => Ok(Value::Placeholder(s.to_string())),
4976            tok @ Token::Colon | tok @ Token::AtSign => {
4977                // Not calling self.parse_identifier()? because only in placeholder we want to check numbers as idfentifies
4978                // This because snowflake allows numbers as placeholders
4979                let next_token = self.next_token();
4980                let ident = match next_token.token {
4981                    Token::Word(w) => Ok(w.to_ident()),
4982                    Token::Number(w, false) => Ok(Ident::new(w)),
4983                    _ => self.expected("placeholder", next_token),
4984                }?;
4985                let placeholder = tok.to_string() + &ident.value;
4986                Ok(Value::Placeholder(placeholder))
4987            }
4988            unexpected => self.expected(
4989                "a value",
4990                TokenWithLocation {
4991                    token: unexpected,
4992                    location,
4993                },
4994            ),
4995        }
4996    }
4997
4998    pub fn parse_number_value(&mut self) -> Result<Value, ParserError> {
4999        match self.parse_value()? {
5000            v @ Value::Number(_, _) => Ok(v),
5001            v @ Value::Placeholder(_) => Ok(v),
5002            _ => {
5003                self.prev_token();
5004                self.expected("literal number", self.peek_token())
5005            }
5006        }
5007    }
5008
5009    fn parse_introduced_string_value(&mut self) -> Result<Value, ParserError> {
5010        let next_token = self.next_token();
5011        let location = next_token.location;
5012        match next_token.token {
5013            Token::SingleQuotedString(ref s) => Ok(Value::SingleQuotedString(s.to_string())),
5014            Token::DoubleQuotedString(ref s) => Ok(Value::DoubleQuotedString(s.to_string())),
5015            Token::HexStringLiteral(ref s) => Ok(Value::HexStringLiteral(s.to_string())),
5016            unexpected => self.expected(
5017                "a string value",
5018                TokenWithLocation {
5019                    token: unexpected,
5020                    location,
5021                },
5022            ),
5023        }
5024    }
5025
5026    /// Parse an unsigned literal integer/long
5027    pub fn parse_literal_uint(&mut self) -> Result<u64, ParserError> {
5028        let next_token = self.next_token();
5029        match next_token.token {
5030            Token::Number(s, _) => s.parse::<u64>().map_err(|e| {
5031                ParserError::ParserError(format!("Could not parse '{s}' as u64: {e}"))
5032            }),
5033            _ => self.expected("literal int", next_token),
5034        }
5035    }
5036
5037    pub fn parse_function_definition(&mut self) -> Result<FunctionDefinition, ParserError> {
5038        let peek_token = self.peek_token();
5039        match peek_token.token {
5040            Token::DollarQuotedString(value) if dialect_of!(self is PostgreSqlDialect) => {
5041                self.next_token();
5042                Ok(FunctionDefinition::DoubleDollarDef(value.value))
5043            }
5044            _ => Ok(FunctionDefinition::SingleQuotedDef(
5045                self.parse_literal_string()?,
5046            )),
5047        }
5048    }
5049    /// Parse a literal string
5050    pub fn parse_literal_string(&mut self) -> Result<String, ParserError> {
5051        let next_token = self.next_token();
5052        match next_token.token {
5053            Token::Word(Word {
5054                value,
5055                keyword: Keyword::NoKeyword,
5056                ..
5057            }) => Ok(value),
5058            Token::SingleQuotedString(s) => Ok(s),
5059            Token::DoubleQuotedString(s) => Ok(s),
5060            Token::EscapedStringLiteral(s) if dialect_of!(self is PostgreSqlDialect | GenericDialect) => {
5061                Ok(s)
5062            }
5063            _ => self.expected("literal string", next_token),
5064        }
5065    }
5066
5067    /// Parse a map key string
5068    pub fn parse_map_key(&mut self) -> Result<Expr, ParserError> {
5069        let next_token = self.next_token();
5070        match next_token.token {
5071            // handle bigquery offset subscript operator which overlaps with OFFSET operator
5072            Token::Word(Word { value, keyword, .. })
5073                if (dialect_of!(self is BigQueryDialect) && keyword == Keyword::OFFSET) =>
5074            {
5075                self.parse_function(ObjectName(vec![Ident::new(value)]))
5076            }
5077            Token::Word(Word { value, keyword, .. }) if (keyword == Keyword::NoKeyword) => {
5078                if self.peek_token() == Token::LParen {
5079                    return self.parse_function(ObjectName(vec![Ident::new(value)]));
5080                }
5081                Ok(Expr::Value(Value::SingleQuotedString(value)))
5082            }
5083            Token::SingleQuotedString(s) => Ok(Expr::Value(Value::SingleQuotedString(s))),
5084            #[cfg(not(feature = "bigdecimal"))]
5085            Token::Number(s, _) => Ok(Expr::Value(Value::Number(s, false))),
5086            #[cfg(feature = "bigdecimal")]
5087            Token::Number(s, _) => Ok(Expr::Value(Value::Number(s.parse().unwrap(), false))),
5088            _ => self.expected("literal string, number or function", next_token),
5089        }
5090    }
5091
5092    /// Parse a SQL datatype (in the context of a CREATE TABLE statement for example)
5093    pub fn parse_data_type(&mut self) -> Result<DataType, ParserError> {
5094        let (ty, trailing_bracket) = self.parse_data_type_helper()?;
5095        if trailing_bracket.0 {
5096            return parser_err!(
5097                format!("unmatched > after parsing data type {ty}"),
5098                self.peek_token()
5099            );
5100        }
5101
5102        Ok(ty)
5103    }
5104
5105    fn parse_data_type_helper(
5106        &mut self,
5107    ) -> Result<(DataType, MatchedTrailingBracket), ParserError> {
5108        let next_token = self.next_token();
5109        let mut trailing_bracket = false.into();
5110        let mut data = match next_token.token {
5111            Token::Word(w) => match w.keyword {
5112                Keyword::BOOLEAN => Ok(DataType::Boolean),
5113                Keyword::BOOL => Ok(DataType::Bool),
5114                Keyword::FLOAT => Ok(DataType::Float(self.parse_optional_precision()?)),
5115                Keyword::REAL => Ok(DataType::Real),
5116                Keyword::FLOAT4 => Ok(DataType::Float4),
5117                Keyword::FLOAT64 => Ok(DataType::Float64),
5118                Keyword::FLOAT8 => Ok(DataType::Float8),
5119                Keyword::DOUBLE => {
5120                    if self.parse_keyword(Keyword::PRECISION) {
5121                        Ok(DataType::DoublePrecision)
5122                    } else {
5123                        Ok(DataType::Double)
5124                    }
5125                }
5126                Keyword::TINYINT => {
5127                    let optional_precision = self.parse_optional_precision();
5128                    if self.parse_keyword(Keyword::UNSIGNED) {
5129                        Ok(DataType::UnsignedTinyInt(optional_precision?))
5130                    } else {
5131                        Ok(DataType::TinyInt(optional_precision?))
5132                    }
5133                }
5134                Keyword::INT2 => {
5135                    let optional_precision = self.parse_optional_precision();
5136                    if self.parse_keyword(Keyword::UNSIGNED) {
5137                        Ok(DataType::UnsignedInt2(optional_precision?))
5138                    } else {
5139                        Ok(DataType::Int2(optional_precision?))
5140                    }
5141                }
5142                Keyword::SMALLINT => {
5143                    let optional_precision = self.parse_optional_precision();
5144                    if self.parse_keyword(Keyword::UNSIGNED) {
5145                        Ok(DataType::UnsignedSmallInt(optional_precision?))
5146                    } else {
5147                        Ok(DataType::SmallInt(optional_precision?))
5148                    }
5149                }
5150                Keyword::MEDIUMINT => {
5151                    let optional_precision = self.parse_optional_precision();
5152                    if self.parse_keyword(Keyword::UNSIGNED) {
5153                        Ok(DataType::UnsignedMediumInt(optional_precision?))
5154                    } else {
5155                        Ok(DataType::MediumInt(optional_precision?))
5156                    }
5157                }
5158                Keyword::INT => {
5159                    let optional_precision = self.parse_optional_precision();
5160                    if self.parse_keyword(Keyword::UNSIGNED) {
5161                        Ok(DataType::UnsignedInt(optional_precision?))
5162                    } else {
5163                        Ok(DataType::Int(optional_precision?))
5164                    }
5165                }
5166                Keyword::INT4 => {
5167                    let optional_precision = self.parse_optional_precision();
5168                    if self.parse_keyword(Keyword::UNSIGNED) {
5169                        Ok(DataType::UnsignedInt4(optional_precision?))
5170                    } else {
5171                        Ok(DataType::Int4(optional_precision?))
5172                    }
5173                }
5174                Keyword::INT64 => Ok(DataType::Int64),
5175                Keyword::INTEGER => {
5176                    let optional_precision = self.parse_optional_precision();
5177                    if self.parse_keyword(Keyword::UNSIGNED) {
5178                        Ok(DataType::UnsignedInteger(optional_precision?))
5179                    } else {
5180                        Ok(DataType::Integer(optional_precision?))
5181                    }
5182                }
5183                Keyword::BIGINT => {
5184                    let optional_precision = self.parse_optional_precision();
5185                    if self.parse_keyword(Keyword::UNSIGNED) {
5186                        Ok(DataType::UnsignedBigInt(optional_precision?))
5187                    } else {
5188                        Ok(DataType::BigInt(optional_precision?))
5189                    }
5190                }
5191                Keyword::INT8 => {
5192                    let optional_precision = self.parse_optional_precision();
5193                    if self.parse_keyword(Keyword::UNSIGNED) {
5194                        Ok(DataType::UnsignedInt8(optional_precision?))
5195                    } else {
5196                        Ok(DataType::Int8(optional_precision?))
5197                    }
5198                }
5199                Keyword::VARCHAR => Ok(DataType::Varchar(self.parse_optional_character_length()?)),
5200                Keyword::NVARCHAR => Ok(DataType::Nvarchar(self.parse_optional_precision()?)),
5201                Keyword::CHARACTER => {
5202                    if self.parse_keyword(Keyword::VARYING) {
5203                        Ok(DataType::CharacterVarying(
5204                            self.parse_optional_character_length()?,
5205                        ))
5206                    } else if self.parse_keywords(&[Keyword::LARGE, Keyword::OBJECT]) {
5207                        Ok(DataType::CharacterLargeObject(
5208                            self.parse_optional_precision()?,
5209                        ))
5210                    } else {
5211                        Ok(DataType::Character(self.parse_optional_character_length()?))
5212                    }
5213                }
5214                Keyword::CHAR => {
5215                    if self.parse_keyword(Keyword::VARYING) {
5216                        Ok(DataType::CharVarying(
5217                            self.parse_optional_character_length()?,
5218                        ))
5219                    } else if self.parse_keywords(&[Keyword::LARGE, Keyword::OBJECT]) {
5220                        Ok(DataType::CharLargeObject(self.parse_optional_precision()?))
5221                    } else {
5222                        Ok(DataType::Char(self.parse_optional_character_length()?))
5223                    }
5224                }
5225                Keyword::CLOB => Ok(DataType::Clob(self.parse_optional_precision()?)),
5226                Keyword::BINARY => Ok(DataType::Binary(self.parse_optional_precision()?)),
5227                Keyword::VARBINARY => Ok(DataType::Varbinary(self.parse_optional_precision()?)),
5228                Keyword::BLOB => Ok(DataType::Blob(self.parse_optional_precision()?)),
5229                Keyword::BYTES => Ok(DataType::Bytes(self.parse_optional_precision()?)),
5230                Keyword::UUID => Ok(DataType::Uuid),
5231                Keyword::DATE => Ok(DataType::Date),
5232                Keyword::DATETIME => Ok(DataType::Datetime(self.parse_optional_precision()?)),
5233                Keyword::TIMESTAMP => {
5234                    let precision = self.parse_optional_precision()?;
5235                    let tz = if self.parse_keyword(Keyword::WITH) {
5236                        self.expect_keywords(&[Keyword::TIME, Keyword::ZONE])?;
5237                        TimezoneInfo::WithTimeZone
5238                    } else if self.parse_keyword(Keyword::WITHOUT) {
5239                        self.expect_keywords(&[Keyword::TIME, Keyword::ZONE])?;
5240                        TimezoneInfo::WithoutTimeZone
5241                    } else {
5242                        TimezoneInfo::None
5243                    };
5244                    Ok(DataType::Timestamp(precision, tz))
5245                }
5246                Keyword::TIMESTAMPTZ => Ok(DataType::Timestamp(
5247                    self.parse_optional_precision()?,
5248                    TimezoneInfo::Tz,
5249                )),
5250                Keyword::TIME => {
5251                    let precision = self.parse_optional_precision()?;
5252                    let tz = if self.parse_keyword(Keyword::WITH) {
5253                        self.expect_keywords(&[Keyword::TIME, Keyword::ZONE])?;
5254                        TimezoneInfo::WithTimeZone
5255                    } else if self.parse_keyword(Keyword::WITHOUT) {
5256                        self.expect_keywords(&[Keyword::TIME, Keyword::ZONE])?;
5257                        TimezoneInfo::WithoutTimeZone
5258                    } else {
5259                        TimezoneInfo::None
5260                    };
5261                    Ok(DataType::Time(precision, tz))
5262                }
5263                Keyword::TIMETZ => Ok(DataType::Time(
5264                    self.parse_optional_precision()?,
5265                    TimezoneInfo::Tz,
5266                )),
5267                // Interval types can be followed by a complicated interval
5268                // qualifier that we don't currently support. See
5269                // parse_interval for a taste.
5270                Keyword::INTERVAL => Ok(DataType::Interval),
5271                Keyword::JSON => Ok(DataType::JSON),
5272                Keyword::REGCLASS => Ok(DataType::Regclass),
5273                Keyword::STRING => Ok(DataType::String(self.parse_optional_precision()?)),
5274                Keyword::TEXT => Ok(DataType::Text),
5275                Keyword::BYTEA => Ok(DataType::Bytea),
5276                Keyword::NUMERIC => Ok(DataType::Numeric(
5277                    self.parse_exact_number_optional_precision_scale()?,
5278                )),
5279                Keyword::DECIMAL => Ok(DataType::Decimal(
5280                    self.parse_exact_number_optional_precision_scale()?,
5281                )),
5282                Keyword::DEC => Ok(DataType::Dec(
5283                    self.parse_exact_number_optional_precision_scale()?,
5284                )),
5285                Keyword::BIGNUMERIC => Ok(DataType::BigNumeric(
5286                    self.parse_exact_number_optional_precision_scale()?,
5287                )),
5288                Keyword::BIGDECIMAL => Ok(DataType::BigDecimal(
5289                    self.parse_exact_number_optional_precision_scale()?,
5290                )),
5291                Keyword::ENUM => Ok(DataType::Enum(self.parse_string_values()?)),
5292                Keyword::SET => Ok(DataType::Set(self.parse_string_values()?)),
5293                Keyword::ARRAY => {
5294                    if dialect_of!(self is SnowflakeDialect) {
5295                        Ok(DataType::Array(ArrayElemTypeDef::None))
5296                    } else {
5297                        self.expect_token(&Token::Lt)?;
5298                        let (inside_type, _trailing_bracket) = self.parse_data_type_helper()?;
5299                        trailing_bracket = self.expect_closing_angle_bracket(_trailing_bracket)?;
5300                        Ok(DataType::Array(ArrayElemTypeDef::AngleBracket(Box::new(
5301                            inside_type,
5302                        ))))
5303                    }
5304                }
5305                Keyword::STRUCT if dialect_of!(self is BigQueryDialect) => {
5306                    self.prev_token();
5307                    let (field_defs, _trailing_bracket) =
5308                        self.parse_struct_type_def(Self::parse_big_query_struct_field_def)?;
5309                    trailing_bracket = _trailing_bracket;
5310                    Ok(DataType::Struct(field_defs))
5311                }
5312                _ => {
5313                    self.prev_token();
5314                    let type_name = self.parse_object_name()?;
5315                    if let Some(modifiers) = self.parse_optional_type_modifiers()? {
5316                        Ok(DataType::Custom(type_name, modifiers))
5317                    } else {
5318                        Ok(DataType::Custom(type_name, vec![]))
5319                    }
5320                }
5321            },
5322            _ => self.expected("a data type name", next_token),
5323        }?;
5324
5325        // Parse array data types. Note: this is postgresql-specific and different from
5326        // Keyword::ARRAY syntax from above
5327        while self.consume_token(&Token::LBracket) {
5328            self.expect_token(&Token::RBracket)?;
5329            data = DataType::Array(ArrayElemTypeDef::SquareBracket(Box::new(data)))
5330        }
5331        Ok((data, trailing_bracket))
5332    }
5333
5334    pub fn parse_string_values(&mut self) -> Result<Vec<String>, ParserError> {
5335        self.expect_token(&Token::LParen)?;
5336        let mut values = Vec::new();
5337        loop {
5338            let next_token = self.next_token();
5339            match next_token.token {
5340                Token::SingleQuotedString(value) => values.push(value),
5341                _ => self.expected("a string", next_token)?,
5342            }
5343            let next_token = self.next_token();
5344            match next_token.token {
5345                Token::Comma => (),
5346                Token::RParen => break,
5347                _ => self.expected(", or }", next_token)?,
5348            }
5349        }
5350        Ok(values)
5351    }
5352
5353    /// Strictly parse `identifier AS identifier`
5354    pub fn parse_identifier_with_alias(&mut self) -> Result<IdentWithAlias, ParserError> {
5355        let ident = self.parse_identifier()?;
5356        self.expect_keyword(Keyword::AS)?;
5357        let alias = self.parse_identifier()?;
5358        Ok(IdentWithAlias { ident, alias })
5359    }
5360
5361    /// Parse `AS identifier` (or simply `identifier` if it's not a reserved keyword)
5362    /// Some examples with aliases: `SELECT 1 foo`, `SELECT COUNT(*) AS cnt`,
5363    /// `SELECT ... FROM t1 foo, t2 bar`, `SELECT ... FROM (...) AS bar`
5364    pub fn parse_optional_alias(
5365        &mut self,
5366        reserved_kwds: &[Keyword],
5367    ) -> Result<Option<Ident>, ParserError> {
5368        let after_as = self.parse_keyword(Keyword::AS);
5369        let next_token = self.next_token();
5370        match next_token.token {
5371            // Accept any identifier after `AS` (though many dialects have restrictions on
5372            // keywords that may appear here). If there's no `AS`: don't parse keywords,
5373            // which may start a construct allowed in this position, to be parsed as aliases.
5374            // (For example, in `FROM t1 JOIN` the `JOIN` will always be parsed as a keyword,
5375            // not an alias.)
5376            Token::Word(w) if after_as || !reserved_kwds.contains(&w.keyword) => {
5377                Ok(Some(w.to_ident()))
5378            }
5379            // MSSQL supports single-quoted strings as aliases for columns
5380            // We accept them as table aliases too, although MSSQL does not.
5381            //
5382            // Note, that this conflicts with an obscure rule from the SQL
5383            // standard, which we don't implement:
5384            // https://crate.io/docs/sql-99/en/latest/chapters/07.html#character-string-literal-s
5385            //    "[Obscure Rule] SQL allows you to break a long <character
5386            //    string literal> up into two or more smaller <character string
5387            //    literal>s, split by a <separator> that includes a newline
5388            //    character. When it sees such a <literal>, your DBMS will
5389            //    ignore the <separator> and treat the multiple strings as
5390            //    a single <literal>."
5391            Token::SingleQuotedString(s) => Ok(Some(Ident::with_quote('\'', s))),
5392            // Support for MySql dialect double quoted string, `AS "HOUR"` for example
5393            Token::DoubleQuotedString(s) => Ok(Some(Ident::with_quote('\"', s))),
5394            _ => {
5395                if after_as {
5396                    return self.expected("an identifier after AS", next_token);
5397                }
5398                self.prev_token();
5399                Ok(None) // no alias found
5400            }
5401        }
5402    }
5403
5404    /// Parse `AS identifier` when the AS is describing a table-valued object,
5405    /// like in `... FROM generate_series(1, 10) AS t (col)`. In this case
5406    /// the alias is allowed to optionally name the columns in the table, in
5407    /// addition to the table itself.
5408    pub fn parse_optional_table_alias(
5409        &mut self,
5410        reserved_kwds: &[Keyword],
5411    ) -> Result<Option<TableAlias>, ParserError> {
5412        match self.parse_optional_alias(reserved_kwds)? {
5413            Some(name) => {
5414                let columns = self.parse_parenthesized_column_list(Optional, false)?;
5415                Ok(Some(TableAlias { name, columns }))
5416            }
5417            None => Ok(None),
5418        }
5419    }
5420
5421    /// Parse a possibly qualified, possibly quoted identifier, e.g.
5422    /// `foo` or `myschema."table"
5423    pub fn parse_object_name(&mut self) -> Result<ObjectName, ParserError> {
5424        let mut idents = vec![];
5425        loop {
5426            idents.push(self.parse_identifier()?);
5427            if !self.consume_token(&Token::Period) {
5428                break;
5429            }
5430        }
5431
5432        // BigQuery accepts any number of quoted identifiers of a table name.
5433        // https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical#quoted_identifiers
5434        if dialect_of!(self is BigQueryDialect)
5435            && idents.iter().any(|ident| ident.value.contains('.'))
5436        {
5437            idents = idents
5438                .into_iter()
5439                .flat_map(|ident| {
5440                    ident
5441                        .value
5442                        .split('.')
5443                        .map(|value| Ident {
5444                            value: value.into(),
5445                            quote_style: ident.quote_style,
5446                        })
5447                        .collect::<Vec<_>>()
5448                })
5449                .collect()
5450        }
5451
5452        Ok(ObjectName(idents))
5453    }
5454
5455    /// Parse identifiers
5456    pub fn parse_identifiers(&mut self) -> Result<Vec<Ident>, ParserError> {
5457        let mut idents = vec![];
5458        loop {
5459            match self.peek_token().token {
5460                Token::Word(w) => {
5461                    idents.push(w.to_ident());
5462                }
5463                Token::EOF | Token::Eq => break,
5464                _ => {}
5465            }
5466            self.next_token();
5467        }
5468        Ok(idents)
5469    }
5470
5471    /// Parse identifiers of form ident1[.identN]*
5472    ///
5473    /// Similar in functionality to [parse_identifiers], with difference
5474    /// being this function is much more strict about parsing a valid multipart identifier, not
5475    /// allowing extraneous tokens to be parsed, otherwise it fails.
5476    ///
5477    /// For example:
5478    ///
5479    /// ```rust
5480    /// use sqlparser::ast::Ident;
5481    /// use sqlparser::dialect::GenericDialect;
5482    /// use sqlparser::parser::Parser;
5483    ///
5484    /// let dialect = GenericDialect {};
5485    /// let expected = vec![Ident::new("one"), Ident::new("two")];
5486    ///
5487    /// // expected usage
5488    /// let sql = "one.two";
5489    /// let mut parser = Parser::new(&dialect).try_with_sql(sql).unwrap();
5490    /// let actual = parser.parse_multipart_identifier().unwrap();
5491    /// assert_eq!(&actual, &expected);
5492    ///
5493    /// // parse_identifiers is more loose on what it allows, parsing successfully
5494    /// let sql = "one + two";
5495    /// let mut parser = Parser::new(&dialect).try_with_sql(sql).unwrap();
5496    /// let actual = parser.parse_identifiers().unwrap();
5497    /// assert_eq!(&actual, &expected);
5498    ///
5499    /// // expected to strictly fail due to + separator
5500    /// let sql = "one + two";
5501    /// let mut parser = Parser::new(&dialect).try_with_sql(sql).unwrap();
5502    /// let actual = parser.parse_multipart_identifier().unwrap_err();
5503    /// assert_eq!(
5504    ///     actual.to_string(),
5505    ///     "sql parser error: Unexpected token in identifier: +"
5506    /// );
5507    /// ```
5508    ///
5509    /// [parse_identifiers]: Parser::parse_identifiers
5510    pub fn parse_multipart_identifier(&mut self) -> Result<Vec<Ident>, ParserError> {
5511        let mut idents = vec![];
5512
5513        // expecting at least one word for identifier
5514        match self.next_token().token {
5515            Token::Word(w) => idents.push(w.to_ident()),
5516            Token::EOF => {
5517                return Err(ParserError::ParserError(
5518                    "Empty input when parsing identifier".to_string(),
5519                ))?
5520            }
5521            token => {
5522                return Err(ParserError::ParserError(format!(
5523                    "Unexpected token in identifier: {token}"
5524                )))?
5525            }
5526        };
5527
5528        // parse optional next parts if exist
5529        loop {
5530            match self.next_token().token {
5531                // ensure that optional period is succeeded by another identifier
5532                Token::Period => match self.next_token().token {
5533                    Token::Word(w) => idents.push(w.to_ident()),
5534                    Token::EOF => {
5535                        return Err(ParserError::ParserError(
5536                            "Trailing period in identifier".to_string(),
5537                        ))?
5538                    }
5539                    token => {
5540                        return Err(ParserError::ParserError(format!(
5541                            "Unexpected token following period in identifier: {token}"
5542                        )))?
5543                    }
5544                },
5545                Token::EOF => break,
5546                token => {
5547                    return Err(ParserError::ParserError(format!(
5548                        "Unexpected token in identifier: {token}"
5549                    )))?
5550                }
5551            }
5552        }
5553
5554        Ok(idents)
5555    }
5556
5557    /// Parse a simple one-word identifier (possibly quoted, possibly a keyword)
5558    pub fn parse_identifier(&mut self) -> Result<Ident, ParserError> {
5559        let next_token = self.next_token();
5560        match next_token.token {
5561            Token::Word(w) => Ok(w.to_ident()),
5562            Token::SingleQuotedString(s) => Ok(Ident::with_quote('\'', s)),
5563            Token::DoubleQuotedString(s) => Ok(Ident::with_quote('\"', s)),
5564            _ => self.expected("identifier", next_token),
5565        }
5566    }
5567
5568    /// Parse a parenthesized comma-separated list of unqualified, possibly quoted identifiers
5569    pub fn parse_parenthesized_column_list(
5570        &mut self,
5571        optional: IsOptional,
5572        allow_empty: bool,
5573    ) -> Result<Vec<Ident>, ParserError> {
5574        if self.consume_token(&Token::LParen) {
5575            if allow_empty && self.peek_token().token == Token::RParen {
5576                self.next_token();
5577                Ok(vec![])
5578            } else {
5579                let cols = self.parse_comma_separated(Parser::parse_identifier)?;
5580                self.expect_token(&Token::RParen)?;
5581                Ok(cols)
5582            }
5583        } else if optional == Optional {
5584            Ok(vec![])
5585        } else {
5586            self.expected("a list of columns in parentheses", self.peek_token())
5587        }
5588    }
5589
5590    pub fn parse_precision(&mut self) -> Result<u64, ParserError> {
5591        self.expect_token(&Token::LParen)?;
5592        let n = self.parse_literal_uint()?;
5593        self.expect_token(&Token::RParen)?;
5594        Ok(n)
5595    }
5596
5597    pub fn parse_optional_precision(&mut self) -> Result<Option<u64>, ParserError> {
5598        if self.consume_token(&Token::LParen) {
5599            let n = self.parse_literal_uint()?;
5600            self.expect_token(&Token::RParen)?;
5601            Ok(Some(n))
5602        } else {
5603            Ok(None)
5604        }
5605    }
5606
5607    pub fn parse_optional_character_length(
5608        &mut self,
5609    ) -> Result<Option<CharacterLength>, ParserError> {
5610        if self.consume_token(&Token::LParen) {
5611            let character_length = self.parse_character_length()?;
5612            self.expect_token(&Token::RParen)?;
5613            Ok(Some(character_length))
5614        } else {
5615            Ok(None)
5616        }
5617    }
5618
5619    pub fn parse_character_length(&mut self) -> Result<CharacterLength, ParserError> {
5620        let length = self.parse_literal_uint()?;
5621        let unit = if self.parse_keyword(Keyword::CHARACTERS) {
5622            Some(CharLengthUnits::Characters)
5623        } else if self.parse_keyword(Keyword::OCTETS) {
5624            Some(CharLengthUnits::Octets)
5625        } else {
5626            None
5627        };
5628
5629        Ok(CharacterLength { length, unit })
5630    }
5631
5632    pub fn parse_optional_precision_scale(
5633        &mut self,
5634    ) -> Result<(Option<u64>, Option<u64>), ParserError> {
5635        if self.consume_token(&Token::LParen) {
5636            let n = self.parse_literal_uint()?;
5637            let scale = if self.consume_token(&Token::Comma) {
5638                Some(self.parse_literal_uint()?)
5639            } else {
5640                None
5641            };
5642            self.expect_token(&Token::RParen)?;
5643            Ok((Some(n), scale))
5644        } else {
5645            Ok((None, None))
5646        }
5647    }
5648
5649    pub fn parse_exact_number_optional_precision_scale(
5650        &mut self,
5651    ) -> Result<ExactNumberInfo, ParserError> {
5652        if self.consume_token(&Token::LParen) {
5653            let precision = self.parse_literal_uint()?;
5654            let scale = if self.consume_token(&Token::Comma) {
5655                Some(self.parse_literal_uint()?)
5656            } else {
5657                None
5658            };
5659
5660            self.expect_token(&Token::RParen)?;
5661
5662            match scale {
5663                None => Ok(ExactNumberInfo::Precision(precision)),
5664                Some(scale) => Ok(ExactNumberInfo::PrecisionAndScale(precision, scale)),
5665            }
5666        } else {
5667            Ok(ExactNumberInfo::None)
5668        }
5669    }
5670
5671    pub fn parse_optional_type_modifiers(&mut self) -> Result<Option<Vec<String>>, ParserError> {
5672        if self.consume_token(&Token::LParen) {
5673            let mut modifiers = Vec::new();
5674            loop {
5675                let next_token = self.next_token();
5676                match next_token.token {
5677                    Token::Word(w) => modifiers.push(w.to_string()),
5678                    Token::Number(n, _) => modifiers.push(n),
5679                    Token::SingleQuotedString(s) => modifiers.push(s),
5680
5681                    Token::Comma => {
5682                        continue;
5683                    }
5684                    Token::RParen => {
5685                        break;
5686                    }
5687                    _ => self.expected("type modifiers", next_token)?,
5688                }
5689            }
5690
5691            Ok(Some(modifiers))
5692        } else {
5693            Ok(None)
5694        }
5695    }
5696
5697    pub fn parse_delete(&mut self) -> Result<Statement, ParserError> {
5698        let tables = if !self.parse_keyword(Keyword::FROM) {
5699            let tables = self.parse_comma_separated(Parser::parse_object_name)?;
5700            self.expect_keyword(Keyword::FROM)?;
5701            tables
5702        } else {
5703            vec![]
5704        };
5705
5706        let from = self.parse_comma_separated(Parser::parse_table_and_joins)?;
5707        let using = if self.parse_keyword(Keyword::USING) {
5708            Some(self.parse_comma_separated(Parser::parse_table_and_joins)?)
5709        } else {
5710            None
5711        };
5712        let selection = if self.parse_keyword(Keyword::WHERE) {
5713            Some(self.parse_expr()?)
5714        } else {
5715            None
5716        };
5717        let returning = if self.parse_keyword(Keyword::RETURNING) {
5718            Some(self.parse_comma_separated(Parser::parse_select_item)?)
5719        } else {
5720            None
5721        };
5722        let order_by = if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
5723            self.parse_comma_separated(Parser::parse_order_by_expr)?
5724        } else {
5725            vec![]
5726        };
5727        let limit = if self.parse_keyword(Keyword::LIMIT) {
5728            self.parse_limit()?
5729        } else {
5730            None
5731        };
5732
5733        Ok(Statement::Delete {
5734            tables,
5735            from,
5736            using,
5737            selection,
5738            returning,
5739            order_by,
5740            limit,
5741        })
5742    }
5743
5744    // KILL [CONNECTION | QUERY | MUTATION] processlist_id
5745    pub fn parse_kill(&mut self) -> Result<Statement, ParserError> {
5746        let modifier_keyword =
5747            self.parse_one_of_keywords(&[Keyword::CONNECTION, Keyword::QUERY, Keyword::MUTATION]);
5748
5749        let id = self.parse_literal_uint()?;
5750
5751        let modifier = match modifier_keyword {
5752            Some(Keyword::CONNECTION) => Some(KillType::Connection),
5753            Some(Keyword::QUERY) => Some(KillType::Query),
5754            Some(Keyword::MUTATION) => {
5755                if dialect_of!(self is ClickHouseDialect | GenericDialect) {
5756                    Some(KillType::Mutation)
5757                } else {
5758                    self.expected(
5759                        "Unsupported type for KILL, allowed: CONNECTION | QUERY",
5760                        self.peek_token(),
5761                    )?
5762                }
5763            }
5764            _ => None,
5765        };
5766
5767        Ok(Statement::Kill { modifier, id })
5768    }
5769
5770    pub fn parse_explain(&mut self, describe_alias: bool) -> Result<Statement, ParserError> {
5771        let analyze = self.parse_keyword(Keyword::ANALYZE);
5772        let verbose = self.parse_keyword(Keyword::VERBOSE);
5773        let mut format = None;
5774        if self.parse_keyword(Keyword::FORMAT) {
5775            format = Some(self.parse_analyze_format()?);
5776        }
5777
5778        match self.maybe_parse(|parser| parser.parse_statement()) {
5779            Some(Statement::Explain { .. }) | Some(Statement::ExplainTable { .. }) => Err(
5780                ParserError::ParserError("Explain must be root of the plan".to_string()),
5781            ),
5782            Some(statement) => Ok(Statement::Explain {
5783                describe_alias,
5784                analyze,
5785                verbose,
5786                statement: Box::new(statement),
5787                format,
5788            }),
5789            _ => {
5790                let table_name = self.parse_object_name()?;
5791                Ok(Statement::ExplainTable {
5792                    describe_alias,
5793                    table_name,
5794                })
5795            }
5796        }
5797    }
5798
5799    /// Parse a query expression, i.e. a `SELECT` statement optionally
5800    /// preceded with some `WITH` CTE declarations and optionally followed
5801    /// by `ORDER BY`. Unlike some other parse_... methods, this one doesn't
5802    /// expect the initial keyword to be already consumed
5803    pub fn parse_query(&mut self) -> Result<Query, ParserError> {
5804        let _guard = self.recursion_counter.try_decrease()?;
5805        let with = if self.parse_keyword(Keyword::WITH) {
5806            Some(With {
5807                recursive: self.parse_keyword(Keyword::RECURSIVE),
5808                cte_tables: self.parse_comma_separated(Parser::parse_cte)?,
5809            })
5810        } else {
5811            None
5812        };
5813
5814        if self.parse_keyword(Keyword::INSERT) {
5815            let insert = self.parse_insert()?;
5816
5817            Ok(Query {
5818                with,
5819                body: Box::new(SetExpr::Insert(insert)),
5820                limit: None,
5821                limit_by: vec![],
5822                order_by: vec![],
5823                offset: None,
5824                fetch: None,
5825                locks: vec![],
5826            })
5827        } else if self.parse_keyword(Keyword::UPDATE) {
5828            let update = self.parse_update()?;
5829            Ok(Query {
5830                with,
5831                body: Box::new(SetExpr::Update(update)),
5832                limit: None,
5833                limit_by: vec![],
5834                order_by: vec![],
5835                offset: None,
5836                fetch: None,
5837                locks: vec![],
5838            })
5839        } else {
5840            let body = Box::new(self.parse_query_body(0)?);
5841
5842            let order_by = if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
5843                self.parse_comma_separated(Parser::parse_order_by_expr)?
5844            } else {
5845                vec![]
5846            };
5847
5848            let mut limit = None;
5849            let mut offset = None;
5850
5851            for _x in 0..2 {
5852                if limit.is_none() && self.parse_keyword(Keyword::LIMIT) {
5853                    limit = self.parse_limit()?
5854                }
5855
5856                if offset.is_none() && self.parse_keyword(Keyword::OFFSET) {
5857                    offset = Some(self.parse_offset()?)
5858                }
5859
5860                if dialect_of!(self is GenericDialect | MySqlDialect | ClickHouseDialect)
5861                    && limit.is_some()
5862                    && offset.is_none()
5863                    && self.consume_token(&Token::Comma)
5864                {
5865                    // MySQL style LIMIT x,y => LIMIT y OFFSET x.
5866                    // Check <https://dev.mysql.com/doc/refman/8.0/en/select.html> for more details.
5867                    offset = Some(Offset {
5868                        value: limit.unwrap(),
5869                        rows: OffsetRows::None,
5870                    });
5871                    limit = Some(self.parse_expr()?);
5872                }
5873            }
5874
5875            let limit_by = if dialect_of!(self is ClickHouseDialect | GenericDialect)
5876                && self.parse_keyword(Keyword::BY)
5877            {
5878                self.parse_comma_separated(Parser::parse_expr)?
5879            } else {
5880                vec![]
5881            };
5882
5883            let fetch = if self.parse_keyword(Keyword::FETCH) {
5884                Some(self.parse_fetch()?)
5885            } else {
5886                None
5887            };
5888
5889            let mut locks = Vec::new();
5890            while self.parse_keyword(Keyword::FOR) {
5891                locks.push(self.parse_lock()?);
5892            }
5893
5894            Ok(Query {
5895                with,
5896                body,
5897                order_by,
5898                limit,
5899                limit_by,
5900                offset,
5901                fetch,
5902                locks,
5903            })
5904        }
5905    }
5906
5907    /// Parse a CTE (`alias [( col1, col2, ... )] AS (subquery)`)
5908    pub fn parse_cte(&mut self) -> Result<Cte, ParserError> {
5909        let name = self.parse_identifier()?;
5910
5911        let mut cte = if self.parse_keyword(Keyword::AS) {
5912            self.expect_token(&Token::LParen)?;
5913            let query = Box::new(self.parse_query()?);
5914            self.expect_token(&Token::RParen)?;
5915            let alias = TableAlias {
5916                name,
5917                columns: vec![],
5918            };
5919            Cte {
5920                alias,
5921                query,
5922                from: None,
5923            }
5924        } else {
5925            let columns = self.parse_parenthesized_column_list(Optional, false)?;
5926            self.expect_keyword(Keyword::AS)?;
5927            self.expect_token(&Token::LParen)?;
5928            let query = Box::new(self.parse_query()?);
5929            self.expect_token(&Token::RParen)?;
5930            let alias = TableAlias { name, columns };
5931            Cte {
5932                alias,
5933                query,
5934                from: None,
5935            }
5936        };
5937        if self.parse_keyword(Keyword::FROM) {
5938            cte.from = Some(self.parse_identifier()?);
5939        }
5940        Ok(cte)
5941    }
5942
5943    /// Parse a "query body", which is an expression with roughly the
5944    /// following grammar:
5945    /// ```sql
5946    ///   query_body ::= restricted_select | '(' subquery ')' | set_operation
5947    ///   restricted_select ::= 'SELECT' [expr_list] [ from ] [ where ] [ groupby_having ]
5948    ///   subquery ::= query_body [ order_by_limit ]
5949    ///   set_operation ::= query_body { 'UNION' | 'EXCEPT' | 'INTERSECT' } [ 'ALL' ] query_body
5950    /// ```
5951    pub fn parse_query_body(&mut self, precedence: u8) -> Result<SetExpr, ParserError> {
5952        // We parse the expression using a Pratt parser, as in `parse_expr()`.
5953        // Start by parsing a restricted SELECT or a `(subquery)`:
5954        let mut expr = if self.parse_keyword(Keyword::SELECT) {
5955            SetExpr::Select(Box::new(self.parse_select()?))
5956        } else if self.consume_token(&Token::LParen) {
5957            // CTEs are not allowed here, but the parser currently accepts them
5958            let subquery = self.parse_query()?;
5959            self.expect_token(&Token::RParen)?;
5960            SetExpr::Query(Box::new(subquery))
5961        } else if self.parse_keyword(Keyword::VALUES) {
5962            let is_mysql = dialect_of!(self is MySqlDialect);
5963            SetExpr::Values(self.parse_values(is_mysql)?)
5964        } else if self.parse_keyword(Keyword::TABLE) {
5965            SetExpr::Table(Box::new(self.parse_as_table()?))
5966        } else {
5967            return self.expected(
5968                "SELECT, VALUES, or a subquery in the query body",
5969                self.peek_token(),
5970            );
5971        };
5972
5973        loop {
5974            // The query can be optionally followed by a set operator:
5975            let op = self.parse_set_operator(&self.peek_token().token);
5976            let next_precedence = match op {
5977                // UNION and EXCEPT have the same binding power and evaluate left-to-right
5978                Some(SetOperator::Union) | Some(SetOperator::Except) => 10,
5979                // INTERSECT has higher precedence than UNION/EXCEPT
5980                Some(SetOperator::Intersect) => 20,
5981                // Unexpected token or EOF => stop parsing the query body
5982                None => break,
5983            };
5984            if precedence >= next_precedence {
5985                break;
5986            }
5987            self.next_token(); // skip past the set operator
5988            let set_quantifier = self.parse_set_quantifier(&op);
5989            expr = SetExpr::SetOperation {
5990                left: Box::new(expr),
5991                op: op.unwrap(),
5992                set_quantifier,
5993                right: Box::new(self.parse_query_body(next_precedence)?),
5994            };
5995        }
5996
5997        Ok(expr)
5998    }
5999
6000    pub fn parse_set_operator(&mut self, token: &Token) -> Option<SetOperator> {
6001        match token {
6002            Token::Word(w) if w.keyword == Keyword::UNION => Some(SetOperator::Union),
6003            Token::Word(w) if w.keyword == Keyword::EXCEPT => Some(SetOperator::Except),
6004            Token::Word(w) if w.keyword == Keyword::INTERSECT => Some(SetOperator::Intersect),
6005            _ => None,
6006        }
6007    }
6008
6009    pub fn parse_set_quantifier(&mut self, op: &Option<SetOperator>) -> SetQuantifier {
6010        match op {
6011            Some(SetOperator::Union) => {
6012                if self.parse_keywords(&[Keyword::DISTINCT, Keyword::BY, Keyword::NAME]) {
6013                    SetQuantifier::DistinctByName
6014                } else if self.parse_keywords(&[Keyword::BY, Keyword::NAME]) {
6015                    SetQuantifier::ByName
6016                } else if self.parse_keyword(Keyword::ALL) {
6017                    if self.parse_keywords(&[Keyword::BY, Keyword::NAME]) {
6018                        SetQuantifier::AllByName
6019                    } else {
6020                        SetQuantifier::All
6021                    }
6022                } else if self.parse_keyword(Keyword::DISTINCT) {
6023                    SetQuantifier::Distinct
6024                } else {
6025                    SetQuantifier::None
6026                }
6027            }
6028            Some(SetOperator::Except) | Some(SetOperator::Intersect) => {
6029                if self.parse_keyword(Keyword::ALL) {
6030                    SetQuantifier::All
6031                } else if self.parse_keyword(Keyword::DISTINCT) {
6032                    SetQuantifier::Distinct
6033                } else {
6034                    SetQuantifier::None
6035                }
6036            }
6037            _ => SetQuantifier::None,
6038        }
6039    }
6040
6041    /// Parse a restricted `SELECT` statement (no CTEs / `UNION` / `ORDER BY`),
6042    /// assuming the initial `SELECT` was already consumed
6043    pub fn parse_select(&mut self) -> Result<Select, ParserError> {
6044        let distinct = self.parse_all_or_distinct()?;
6045
6046        let top = if self.parse_keyword(Keyword::TOP) {
6047            Some(self.parse_top()?)
6048        } else {
6049            None
6050        };
6051
6052        let projection = self.parse_projection()?;
6053
6054        let into = if self.parse_keyword(Keyword::INTO) {
6055            let temporary = self
6056                .parse_one_of_keywords(&[Keyword::TEMP, Keyword::TEMPORARY])
6057                .is_some();
6058            let unlogged = self.parse_keyword(Keyword::UNLOGGED);
6059            let table = self.parse_keyword(Keyword::TABLE);
6060            let name = self.parse_object_name()?;
6061            Some(SelectInto {
6062                temporary,
6063                unlogged,
6064                table,
6065                name,
6066            })
6067        } else {
6068            None
6069        };
6070
6071        // Note that for keywords to be properly handled here, they need to be
6072        // added to `RESERVED_FOR_COLUMN_ALIAS` / `RESERVED_FOR_TABLE_ALIAS`,
6073        // otherwise they may be parsed as an alias as part of the `projection`
6074        // or `from`.
6075
6076        let from = if self.parse_keyword(Keyword::FROM) {
6077            self.parse_comma_separated(Parser::parse_table_and_joins)?
6078        } else {
6079            vec![]
6080        };
6081
6082        let mut lateral_views = vec![];
6083        loop {
6084            if self.parse_keywords(&[Keyword::LATERAL, Keyword::VIEW]) {
6085                let outer = self.parse_keyword(Keyword::OUTER);
6086                let lateral_view = self.parse_expr()?;
6087                let lateral_view_name = self.parse_object_name()?;
6088                let lateral_col_alias = self
6089                    .parse_comma_separated(|parser| {
6090                        parser.parse_optional_alias(&[
6091                            Keyword::WHERE,
6092                            Keyword::GROUP,
6093                            Keyword::CLUSTER,
6094                            Keyword::HAVING,
6095                            Keyword::LATERAL,
6096                        ]) // This couldn't possibly be a bad idea
6097                    })?
6098                    .into_iter()
6099                    .flatten()
6100                    .collect();
6101
6102                lateral_views.push(LateralView {
6103                    lateral_view,
6104                    lateral_view_name,
6105                    lateral_col_alias,
6106                    outer,
6107                });
6108            } else {
6109                break;
6110            }
6111        }
6112
6113        let selection = if self.parse_keyword(Keyword::WHERE) {
6114            Some(self.parse_expr()?)
6115        } else {
6116            None
6117        };
6118
6119        let group_by = if self.parse_keywords(&[Keyword::GROUP, Keyword::BY]) {
6120            if self.parse_keyword(Keyword::ALL) {
6121                GroupByExpr::All
6122            } else {
6123                GroupByExpr::Expressions(self.parse_comma_separated(Parser::parse_group_by_expr)?)
6124            }
6125        } else {
6126            GroupByExpr::Expressions(vec![])
6127        };
6128
6129        let cluster_by = if self.parse_keywords(&[Keyword::CLUSTER, Keyword::BY]) {
6130            self.parse_comma_separated(Parser::parse_expr)?
6131        } else {
6132            vec![]
6133        };
6134
6135        let distribute_by = if self.parse_keywords(&[Keyword::DISTRIBUTE, Keyword::BY]) {
6136            self.parse_comma_separated(Parser::parse_expr)?
6137        } else {
6138            vec![]
6139        };
6140
6141        let sort_by = if self.parse_keywords(&[Keyword::SORT, Keyword::BY]) {
6142            self.parse_comma_separated(Parser::parse_expr)?
6143        } else {
6144            vec![]
6145        };
6146
6147        let having = if self.parse_keyword(Keyword::HAVING) {
6148            Some(self.parse_expr()?)
6149        } else {
6150            None
6151        };
6152
6153        let named_windows = if self.parse_keyword(Keyword::WINDOW) {
6154            self.parse_comma_separated(Parser::parse_named_window)?
6155        } else {
6156            vec![]
6157        };
6158
6159        let qualify = if self.parse_keyword(Keyword::QUALIFY) {
6160            Some(self.parse_expr()?)
6161        } else {
6162            None
6163        };
6164
6165        Ok(Select {
6166            distinct,
6167            top,
6168            projection,
6169            into,
6170            from,
6171            lateral_views,
6172            selection,
6173            group_by,
6174            cluster_by,
6175            distribute_by,
6176            sort_by,
6177            having,
6178            named_window: named_windows,
6179            qualify,
6180        })
6181    }
6182
6183    /// Parse `CREATE TABLE x AS TABLE y`
6184    pub fn parse_as_table(&mut self) -> Result<Table, ParserError> {
6185        let token1 = self.next_token();
6186        let token2 = self.next_token();
6187        let token3 = self.next_token();
6188
6189        let table_name;
6190        let schema_name;
6191        if token2 == Token::Period {
6192            match token1.token {
6193                Token::Word(w) => {
6194                    schema_name = w.value;
6195                }
6196                _ => {
6197                    return self.expected("Schema name", token1);
6198                }
6199            }
6200            match token3.token {
6201                Token::Word(w) => {
6202                    table_name = w.value;
6203                }
6204                _ => {
6205                    return self.expected("Table name", token3);
6206                }
6207            }
6208            Ok(Table {
6209                table_name: Some(table_name),
6210                schema_name: Some(schema_name),
6211            })
6212        } else {
6213            match token1.token {
6214                Token::Word(w) => {
6215                    table_name = w.value;
6216                }
6217                _ => {
6218                    return self.expected("Table name", token1);
6219                }
6220            }
6221            Ok(Table {
6222                table_name: Some(table_name),
6223                schema_name: None,
6224            })
6225        }
6226    }
6227
6228    pub fn parse_set(&mut self) -> Result<Statement, ParserError> {
6229        let modifier =
6230            self.parse_one_of_keywords(&[Keyword::SESSION, Keyword::LOCAL, Keyword::HIVEVAR]);
6231        if let Some(Keyword::HIVEVAR) = modifier {
6232            self.expect_token(&Token::Colon)?;
6233        } else if self.parse_keyword(Keyword::ROLE) {
6234            let context_modifier = match modifier {
6235                Some(Keyword::LOCAL) => ContextModifier::Local,
6236                Some(Keyword::SESSION) => ContextModifier::Session,
6237                _ => ContextModifier::None,
6238            };
6239
6240            let role_name = if self.parse_keyword(Keyword::NONE) {
6241                None
6242            } else {
6243                Some(self.parse_identifier()?)
6244            };
6245            return Ok(Statement::SetRole {
6246                context_modifier,
6247                role_name,
6248            });
6249        }
6250
6251        let variable = if self.parse_keywords(&[Keyword::TIME, Keyword::ZONE]) {
6252            ObjectName(vec!["TIMEZONE".into()])
6253        } else {
6254            self.parse_object_name()?
6255        };
6256
6257        if variable.to_string().eq_ignore_ascii_case("NAMES")
6258            && dialect_of!(self is MySqlDialect | GenericDialect)
6259        {
6260            if self.parse_keyword(Keyword::DEFAULT) {
6261                return Ok(Statement::SetNamesDefault {});
6262            }
6263
6264            let charset_name = self.parse_literal_string()?;
6265            let collation_name = if self.parse_one_of_keywords(&[Keyword::COLLATE]).is_some() {
6266                Some(self.parse_literal_string()?)
6267            } else {
6268                None
6269            };
6270
6271            Ok(Statement::SetNames {
6272                charset_name,
6273                collation_name,
6274            })
6275        } else if self.consume_token(&Token::Eq) || self.parse_keyword(Keyword::TO) {
6276            let mut values = vec![];
6277            loop {
6278                let value = if let Ok(expr) = self.parse_expr() {
6279                    expr
6280                } else {
6281                    self.expected("variable value", self.peek_token())?
6282                };
6283
6284                values.push(value);
6285                if self.consume_token(&Token::Comma) {
6286                    continue;
6287                }
6288                return Ok(Statement::SetVariable {
6289                    local: modifier == Some(Keyword::LOCAL),
6290                    hivevar: Some(Keyword::HIVEVAR) == modifier,
6291                    variable,
6292                    value: values,
6293                });
6294            }
6295        } else if variable.to_string().eq_ignore_ascii_case("TIMEZONE") {
6296            // for some db (e.g. postgresql), SET TIME ZONE <value> is an alias for SET TIMEZONE [TO|=] <value>
6297            match self.parse_expr() {
6298                Ok(expr) => Ok(Statement::SetTimeZone {
6299                    local: modifier == Some(Keyword::LOCAL),
6300                    value: expr,
6301                }),
6302                _ => self.expected("timezone value", self.peek_token())?,
6303            }
6304        } else if variable.to_string() == "CHARACTERISTICS" {
6305            self.expect_keywords(&[Keyword::AS, Keyword::TRANSACTION])?;
6306            Ok(Statement::SetTransaction {
6307                modes: self.parse_transaction_modes()?,
6308                snapshot: None,
6309                session: true,
6310            })
6311        } else if variable.to_string() == "TRANSACTION" && modifier.is_none() {
6312            if self.parse_keyword(Keyword::SNAPSHOT) {
6313                let snaphot_id = self.parse_value()?;
6314                return Ok(Statement::SetTransaction {
6315                    modes: vec![],
6316                    snapshot: Some(snaphot_id),
6317                    session: false,
6318                });
6319            }
6320            Ok(Statement::SetTransaction {
6321                modes: self.parse_transaction_modes()?,
6322                snapshot: None,
6323                session: false,
6324            })
6325        } else {
6326            self.expected("equals sign or TO", self.peek_token())
6327        }
6328    }
6329
6330    pub fn parse_show(&mut self) -> Result<Statement, ParserError> {
6331        let extended = self.parse_keyword(Keyword::EXTENDED);
6332        let full = self.parse_keyword(Keyword::FULL);
6333        if self
6334            .parse_one_of_keywords(&[Keyword::COLUMNS, Keyword::FIELDS])
6335            .is_some()
6336        {
6337            Ok(self.parse_show_columns(extended, full)?)
6338        } else if self.parse_keyword(Keyword::TABLES) {
6339            Ok(self.parse_show_tables(extended, full)?)
6340        } else if self.parse_keyword(Keyword::FUNCTIONS) {
6341            Ok(self.parse_show_functions()?)
6342        } else if extended || full {
6343            Err(ParserError::ParserError(
6344                "EXTENDED/FULL are not supported with this type of SHOW query".to_string(),
6345            ))
6346        } else if self.parse_one_of_keywords(&[Keyword::CREATE]).is_some() {
6347            Ok(self.parse_show_create()?)
6348        } else if self.parse_keyword(Keyword::COLLATION) {
6349            Ok(self.parse_show_collation()?)
6350        } else if self.parse_keyword(Keyword::VARIABLES)
6351            && dialect_of!(self is MySqlDialect | GenericDialect)
6352        {
6353            // TODO: Support GLOBAL|SESSION
6354            Ok(Statement::ShowVariables {
6355                filter: self.parse_show_statement_filter()?,
6356            })
6357        } else {
6358            Ok(Statement::ShowVariable {
6359                variable: self.parse_identifiers()?,
6360            })
6361        }
6362    }
6363
6364    pub fn parse_show_create(&mut self) -> Result<Statement, ParserError> {
6365        let obj_type = match self.expect_one_of_keywords(&[
6366            Keyword::TABLE,
6367            Keyword::TRIGGER,
6368            Keyword::FUNCTION,
6369            Keyword::PROCEDURE,
6370            Keyword::EVENT,
6371            Keyword::VIEW,
6372        ])? {
6373            Keyword::TABLE => Ok(ShowCreateObject::Table),
6374            Keyword::TRIGGER => Ok(ShowCreateObject::Trigger),
6375            Keyword::FUNCTION => Ok(ShowCreateObject::Function),
6376            Keyword::PROCEDURE => Ok(ShowCreateObject::Procedure),
6377            Keyword::EVENT => Ok(ShowCreateObject::Event),
6378            Keyword::VIEW => Ok(ShowCreateObject::View),
6379            keyword => Err(ParserError::ParserError(format!(
6380                "Unable to map keyword to ShowCreateObject: {keyword:?}"
6381            ))),
6382        }?;
6383
6384        let obj_name = self.parse_object_name()?;
6385
6386        Ok(Statement::ShowCreate { obj_type, obj_name })
6387    }
6388
6389    pub fn parse_show_columns(
6390        &mut self,
6391        extended: bool,
6392        full: bool,
6393    ) -> Result<Statement, ParserError> {
6394        self.expect_one_of_keywords(&[Keyword::FROM, Keyword::IN])?;
6395        let object_name = self.parse_object_name()?;
6396        let table_name = match self.parse_one_of_keywords(&[Keyword::FROM, Keyword::IN]) {
6397            Some(_) => {
6398                let db_name = vec![self.parse_identifier()?];
6399                let ObjectName(table_name) = object_name;
6400                let object_name = db_name.into_iter().chain(table_name).collect();
6401                ObjectName(object_name)
6402            }
6403            None => object_name,
6404        };
6405        let filter = self.parse_show_statement_filter()?;
6406        Ok(Statement::ShowColumns {
6407            extended,
6408            full,
6409            table_name,
6410            filter,
6411        })
6412    }
6413
6414    pub fn parse_show_tables(
6415        &mut self,
6416        extended: bool,
6417        full: bool,
6418    ) -> Result<Statement, ParserError> {
6419        let db_name = match self.parse_one_of_keywords(&[Keyword::FROM, Keyword::IN]) {
6420            Some(_) => Some(self.parse_identifier()?),
6421            None => None,
6422        };
6423        let filter = self.parse_show_statement_filter()?;
6424        Ok(Statement::ShowTables {
6425            extended,
6426            full,
6427            db_name,
6428            filter,
6429        })
6430    }
6431
6432    pub fn parse_show_functions(&mut self) -> Result<Statement, ParserError> {
6433        let filter = self.parse_show_statement_filter()?;
6434        Ok(Statement::ShowFunctions { filter })
6435    }
6436
6437    pub fn parse_show_collation(&mut self) -> Result<Statement, ParserError> {
6438        let filter = self.parse_show_statement_filter()?;
6439        Ok(Statement::ShowCollation { filter })
6440    }
6441
6442    pub fn parse_show_statement_filter(
6443        &mut self,
6444    ) -> Result<Option<ShowStatementFilter>, ParserError> {
6445        if self.parse_keyword(Keyword::LIKE) {
6446            Ok(Some(ShowStatementFilter::Like(
6447                self.parse_literal_string()?,
6448            )))
6449        } else if self.parse_keyword(Keyword::ILIKE) {
6450            Ok(Some(ShowStatementFilter::ILike(
6451                self.parse_literal_string()?,
6452            )))
6453        } else if self.parse_keyword(Keyword::WHERE) {
6454            Ok(Some(ShowStatementFilter::Where(self.parse_expr()?)))
6455        } else {
6456            Ok(None)
6457        }
6458    }
6459
6460    pub fn parse_use(&mut self) -> Result<Statement, ParserError> {
6461        let db_name = self.parse_identifier()?;
6462        Ok(Statement::Use { db_name })
6463    }
6464
6465    pub fn parse_table_and_joins(&mut self) -> Result<TableWithJoins, ParserError> {
6466        let relation = self.parse_table_factor()?;
6467        // Note that for keywords to be properly handled here, they need to be
6468        // added to `RESERVED_FOR_TABLE_ALIAS`, otherwise they may be parsed as
6469        // a table alias.
6470        let mut joins = vec![];
6471        loop {
6472            let join = if self.parse_keyword(Keyword::CROSS) {
6473                let join_operator = if self.parse_keyword(Keyword::JOIN) {
6474                    JoinOperator::CrossJoin
6475                } else if self.parse_keyword(Keyword::APPLY) {
6476                    // MSSQL extension, similar to CROSS JOIN LATERAL
6477                    JoinOperator::CrossApply
6478                } else {
6479                    return self.expected("JOIN or APPLY after CROSS", self.peek_token());
6480                };
6481                Join {
6482                    relation: self.parse_table_factor()?,
6483                    join_operator,
6484                }
6485            } else if self.parse_keyword(Keyword::OUTER) {
6486                // MSSQL extension, similar to LEFT JOIN LATERAL .. ON 1=1
6487                self.expect_keyword(Keyword::APPLY)?;
6488                Join {
6489                    relation: self.parse_table_factor()?,
6490                    join_operator: JoinOperator::OuterApply,
6491                }
6492            } else {
6493                let natural = self.parse_keyword(Keyword::NATURAL);
6494                let peek_keyword = if let Token::Word(w) = self.peek_token().token {
6495                    w.keyword
6496                } else {
6497                    Keyword::NoKeyword
6498                };
6499
6500                let join_operator_type = match peek_keyword {
6501                    Keyword::INNER | Keyword::JOIN => {
6502                        let _ = self.parse_keyword(Keyword::INNER); // [ INNER ]
6503                        self.expect_keyword(Keyword::JOIN)?;
6504                        JoinOperator::Inner
6505                    }
6506                    kw @ Keyword::LEFT | kw @ Keyword::RIGHT => {
6507                        let _ = self.next_token(); // consume LEFT/RIGHT
6508                        let is_left = kw == Keyword::LEFT;
6509                        let join_type = self.parse_one_of_keywords(&[
6510                            Keyword::OUTER,
6511                            Keyword::SEMI,
6512                            Keyword::ANTI,
6513                            Keyword::JOIN,
6514                        ]);
6515                        match join_type {
6516                            Some(Keyword::OUTER) => {
6517                                self.expect_keyword(Keyword::JOIN)?;
6518                                if is_left {
6519                                    JoinOperator::LeftOuter
6520                                } else {
6521                                    JoinOperator::RightOuter
6522                                }
6523                            }
6524                            Some(Keyword::SEMI) => {
6525                                self.expect_keyword(Keyword::JOIN)?;
6526                                if is_left {
6527                                    JoinOperator::LeftSemi
6528                                } else {
6529                                    JoinOperator::RightSemi
6530                                }
6531                            }
6532                            Some(Keyword::ANTI) => {
6533                                self.expect_keyword(Keyword::JOIN)?;
6534                                if is_left {
6535                                    JoinOperator::LeftAnti
6536                                } else {
6537                                    JoinOperator::RightAnti
6538                                }
6539                            }
6540                            Some(Keyword::JOIN) => {
6541                                if is_left {
6542                                    JoinOperator::LeftOuter
6543                                } else {
6544                                    JoinOperator::RightOuter
6545                                }
6546                            }
6547                            _ => {
6548                                return Err(ParserError::ParserError(format!(
6549                                    "expected OUTER, SEMI, ANTI or JOIN after {kw:?}"
6550                                )))
6551                            }
6552                        }
6553                    }
6554                    Keyword::FULL => {
6555                        let _ = self.next_token(); // consume FULL
6556                        let _ = self.parse_keyword(Keyword::OUTER); // [ OUTER ]
6557                        self.expect_keyword(Keyword::JOIN)?;
6558                        JoinOperator::FullOuter
6559                    }
6560                    Keyword::OUTER => {
6561                        return self.expected("LEFT, RIGHT, or FULL", self.peek_token());
6562                    }
6563                    _ if natural => {
6564                        return self.expected("a join type after NATURAL", self.peek_token());
6565                    }
6566                    _ => break,
6567                };
6568                let relation = self.parse_table_factor()?;
6569                let join_constraint = self.parse_join_constraint(natural)?;
6570                Join {
6571                    relation,
6572                    join_operator: join_operator_type(join_constraint),
6573                }
6574            };
6575            joins.push(join);
6576        }
6577        Ok(TableWithJoins { relation, joins })
6578    }
6579
6580    /// A table name or a parenthesized subquery, followed by optional `[AS] alias`
6581    pub fn parse_table_factor(&mut self) -> Result<TableFactor, ParserError> {
6582        if self.parse_keyword(Keyword::LATERAL) {
6583            // LATERAL must always be followed by a subquery or table function.
6584            if self.consume_token(&Token::LParen) {
6585                self.parse_derived_table_factor(Lateral)
6586            } else {
6587                let name = self.parse_object_name()?;
6588                self.expect_token(&Token::LParen)?;
6589                let args = self.parse_optional_args()?;
6590                let alias = self.parse_optional_table_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?;
6591                Ok(TableFactor::Function {
6592                    lateral: true,
6593                    name,
6594                    args,
6595                    alias,
6596                })
6597            }
6598        } else if self.parse_keyword(Keyword::TABLE) {
6599            // parse table function (SELECT * FROM TABLE (<expr>) [ AS <alias> ])
6600            self.expect_token(&Token::LParen)?;
6601            let expr = self.parse_expr()?;
6602            self.expect_token(&Token::RParen)?;
6603            let alias = self.parse_optional_table_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?;
6604            Ok(TableFactor::TableFunction { expr, alias })
6605        } else if self.consume_token(&Token::LParen) {
6606            // A left paren introduces either a derived table (i.e., a subquery)
6607            // or a nested join. It's nearly impossible to determine ahead of
6608            // time which it is... so we just try to parse both.
6609            //
6610            // Here's an example that demonstrates the complexity:
6611            //                     /-------------------------------------------------------\
6612            //                     | /-----------------------------------\                 |
6613            //     SELECT * FROM ( ( ( (SELECT 1) UNION (SELECT 2) ) AS t1 NATURAL JOIN t2 ) )
6614            //                   ^ ^ ^ ^
6615            //                   | | | |
6616            //                   | | | |
6617            //                   | | | (4) belongs to a SetExpr::Query inside the subquery
6618            //                   | | (3) starts a derived table (subquery)
6619            //                   | (2) starts a nested join
6620            //                   (1) an additional set of parens around a nested join
6621            //
6622
6623            // If the recently consumed '(' starts a derived table, the call to
6624            // `parse_derived_table_factor` below will return success after parsing the
6625            // subquery, followed by the closing ')', and the alias of the derived table.
6626            // In the example above this is case (3).
6627            return_ok_if_some!(
6628                self.maybe_parse(|parser| parser.parse_derived_table_factor(NotLateral))
6629            );
6630            // A parsing error from `parse_derived_table_factor` indicates that the '(' we've
6631            // recently consumed does not start a derived table (cases 1, 2, or 4).
6632            // `maybe_parse` will ignore such an error and rewind to be after the opening '('.
6633
6634            // Inside the parentheses we expect to find an (A) table factor
6635            // followed by some joins or (B) another level of nesting.
6636            let mut table_and_joins = self.parse_table_and_joins()?;
6637
6638            #[allow(clippy::if_same_then_else)]
6639            if !table_and_joins.joins.is_empty() {
6640                self.expect_token(&Token::RParen)?;
6641                let alias = self.parse_optional_table_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?;
6642                Ok(TableFactor::NestedJoin {
6643                    table_with_joins: Box::new(table_and_joins),
6644                    alias,
6645                }) // (A)
6646            } else if let TableFactor::NestedJoin {
6647                table_with_joins: _,
6648                alias: _,
6649            } = &table_and_joins.relation
6650            {
6651                // (B): `table_and_joins` (what we found inside the parentheses)
6652                // is a nested join `(foo JOIN bar)`, not followed by other joins.
6653                self.expect_token(&Token::RParen)?;
6654                let alias = self.parse_optional_table_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?;
6655                Ok(TableFactor::NestedJoin {
6656                    table_with_joins: Box::new(table_and_joins),
6657                    alias,
6658                })
6659            } else if dialect_of!(self is SnowflakeDialect | GenericDialect) {
6660                // Dialect-specific behavior: Snowflake diverges from the
6661                // standard and from most of the other implementations by
6662                // allowing extra parentheses not only around a join (B), but
6663                // around lone table names (e.g. `FROM (mytable [AS alias])`)
6664                // and around derived tables (e.g. `FROM ((SELECT ...)
6665                // [AS alias])`) as well.
6666                self.expect_token(&Token::RParen)?;
6667
6668                if let Some(outer_alias) =
6669                    self.parse_optional_table_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?
6670                {
6671                    // Snowflake also allows specifying an alias *after* parens
6672                    // e.g. `FROM (mytable) AS alias`
6673                    match &mut table_and_joins.relation {
6674                        TableFactor::Derived { alias, .. }
6675                        | TableFactor::Table { alias, .. }
6676                        | TableFactor::Function { alias, .. }
6677                        | TableFactor::UNNEST { alias, .. }
6678                        | TableFactor::TableFunction { alias, .. }
6679                        | TableFactor::Pivot { alias, .. }
6680                        | TableFactor::Unpivot { alias, .. }
6681                        | TableFactor::NestedJoin { alias, .. } => {
6682                            // but not `FROM (mytable AS alias1) AS alias2`.
6683                            if let Some(inner_alias) = alias {
6684                                return Err(ParserError::ParserError(format!(
6685                                    "duplicate alias {inner_alias}"
6686                                )));
6687                            }
6688                            // Act as if the alias was specified normally next
6689                            // to the table name: `(mytable) AS alias` ->
6690                            // `(mytable AS alias)`
6691                            alias.replace(outer_alias);
6692                        }
6693                    };
6694                }
6695                // Do not store the extra set of parens in the AST
6696                Ok(table_and_joins.relation)
6697            } else {
6698                // The SQL spec prohibits derived tables and bare tables from
6699                // appearing alone in parentheses (e.g. `FROM (mytable)`)
6700                self.expected("joined table", self.peek_token())
6701            }
6702        } else if dialect_of!(self is BigQueryDialect | PostgreSqlDialect | GenericDialect)
6703            && self.parse_keyword(Keyword::UNNEST)
6704        {
6705            self.expect_token(&Token::LParen)?;
6706            let array_exprs = self.parse_comma_separated(Parser::parse_expr)?;
6707            self.expect_token(&Token::RParen)?;
6708
6709            let alias = match self.parse_optional_table_alias(keywords::RESERVED_FOR_TABLE_ALIAS) {
6710                Ok(Some(alias)) => Some(alias),
6711                Ok(None) => None,
6712                Err(e) => return Err(e),
6713            };
6714
6715            let with_offset = match self.expect_keywords(&[Keyword::WITH, Keyword::OFFSET]) {
6716                Ok(()) => true,
6717                Err(_) => false,
6718            };
6719
6720            let with_offset_alias = if with_offset {
6721                match self.parse_optional_alias(keywords::RESERVED_FOR_COLUMN_ALIAS) {
6722                    Ok(Some(alias)) => Some(alias),
6723                    Ok(None) => None,
6724                    Err(e) => return Err(e),
6725                }
6726            } else {
6727                None
6728            };
6729
6730            Ok(TableFactor::UNNEST {
6731                alias,
6732                array_exprs,
6733                with_offset,
6734                with_offset_alias,
6735            })
6736        } else {
6737            let name = self.parse_object_name()?;
6738
6739            let partitions: Vec<Ident> = if dialect_of!(self is MySqlDialect | GenericDialect)
6740                && self.parse_keyword(Keyword::PARTITION)
6741            {
6742                self.parse_partitions()?
6743            } else {
6744                vec![]
6745            };
6746
6747            // Parse potential version qualifier
6748            let version = self.parse_table_version()?;
6749
6750            // Postgres, MSSQL: table-valued functions:
6751            let args = if self.consume_token(&Token::LParen) {
6752                Some(self.parse_optional_args()?)
6753            } else {
6754                None
6755            };
6756
6757            let alias = self.parse_optional_table_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?;
6758
6759            // MSSQL-specific table hints:
6760            let mut with_hints = vec![];
6761            if self.parse_keyword(Keyword::WITH) {
6762                if self.consume_token(&Token::LParen) {
6763                    with_hints = self.parse_comma_separated(Parser::parse_expr)?;
6764                    self.expect_token(&Token::RParen)?;
6765                } else {
6766                    // rewind, as WITH may belong to the next statement's CTE
6767                    self.prev_token();
6768                }
6769            };
6770
6771            let mut table = TableFactor::Table {
6772                name,
6773                alias,
6774                args,
6775                with_hints,
6776                version,
6777                partitions,
6778            };
6779
6780            while let Some(kw) = self.parse_one_of_keywords(&[Keyword::PIVOT, Keyword::UNPIVOT]) {
6781                table = match kw {
6782                    Keyword::PIVOT => self.parse_pivot_table_factor(table)?,
6783                    Keyword::UNPIVOT => self.parse_unpivot_table_factor(table)?,
6784                    _ => unreachable!(),
6785                }
6786            }
6787
6788            Ok(table)
6789        }
6790    }
6791
6792    /// Parse a given table version specifier.
6793    ///
6794    /// For now it only supports timestamp versioning for BigQuery and MSSQL dialects.
6795    pub fn parse_table_version(&mut self) -> Result<Option<TableVersion>, ParserError> {
6796        if dialect_of!(self is BigQueryDialect | MsSqlDialect)
6797            && self.parse_keywords(&[Keyword::FOR, Keyword::SYSTEM_TIME, Keyword::AS, Keyword::OF])
6798        {
6799            let expr = self.parse_expr()?;
6800            Ok(Some(TableVersion::ForSystemTimeAsOf(expr)))
6801        } else {
6802            Ok(None)
6803        }
6804    }
6805
6806    pub fn parse_derived_table_factor(
6807        &mut self,
6808        lateral: IsLateral,
6809    ) -> Result<TableFactor, ParserError> {
6810        let subquery = Box::new(self.parse_query()?);
6811        self.expect_token(&Token::RParen)?;
6812        let alias = self.parse_optional_table_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?;
6813        Ok(TableFactor::Derived {
6814            lateral: match lateral {
6815                Lateral => true,
6816                NotLateral => false,
6817            },
6818            subquery,
6819            alias,
6820        })
6821    }
6822
6823    pub fn parse_pivot_table_factor(
6824        &mut self,
6825        table: TableFactor,
6826    ) -> Result<TableFactor, ParserError> {
6827        self.expect_token(&Token::LParen)?;
6828        let function_name = match self.next_token().token {
6829            Token::Word(w) => Ok(w.value),
6830            _ => self.expected("an aggregate function name", self.peek_token()),
6831        }?;
6832        let function = self.parse_function(ObjectName(vec![Ident::new(function_name)]))?;
6833        self.expect_keyword(Keyword::FOR)?;
6834        let value_column = self.parse_object_name()?.0;
6835        self.expect_keyword(Keyword::IN)?;
6836        self.expect_token(&Token::LParen)?;
6837        let pivot_values = self.parse_comma_separated(Parser::parse_value)?;
6838        self.expect_token(&Token::RParen)?;
6839        self.expect_token(&Token::RParen)?;
6840        let alias = self.parse_optional_table_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?;
6841        Ok(TableFactor::Pivot {
6842            table: Box::new(table),
6843            aggregate_function: function,
6844            value_column,
6845            pivot_values,
6846            alias,
6847        })
6848    }
6849
6850    pub fn parse_unpivot_table_factor(
6851        &mut self,
6852        table: TableFactor,
6853    ) -> Result<TableFactor, ParserError> {
6854        self.expect_token(&Token::LParen)?;
6855        let value = self.parse_identifier()?;
6856        self.expect_keyword(Keyword::FOR)?;
6857        let name = self.parse_identifier()?;
6858        self.expect_keyword(Keyword::IN)?;
6859        let columns = self.parse_parenthesized_column_list(Mandatory, false)?;
6860        self.expect_token(&Token::RParen)?;
6861        let alias = self.parse_optional_table_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?;
6862        Ok(TableFactor::Unpivot {
6863            table: Box::new(table),
6864            value,
6865            name,
6866            columns,
6867            alias,
6868        })
6869    }
6870
6871    pub fn parse_join_constraint(&mut self, natural: bool) -> Result<JoinConstraint, ParserError> {
6872        if natural {
6873            Ok(JoinConstraint::Natural)
6874        } else if self.parse_keyword(Keyword::ON) {
6875            let constraint = self.parse_expr()?;
6876            Ok(JoinConstraint::On(constraint))
6877        } else if self.parse_keyword(Keyword::USING) {
6878            let columns = self.parse_parenthesized_column_list(Mandatory, false)?;
6879            Ok(JoinConstraint::Using(columns))
6880        } else {
6881            Ok(JoinConstraint::None)
6882            //self.expected("ON, or USING after JOIN", self.peek_token())
6883        }
6884    }
6885
6886    /// Parse a GRANT statement.
6887    pub fn parse_grant(&mut self) -> Result<Statement, ParserError> {
6888        let (privileges, objects) = self.parse_grant_revoke_privileges_objects()?;
6889
6890        self.expect_keyword(Keyword::TO)?;
6891        let grantees = self.parse_comma_separated(Parser::parse_identifier)?;
6892
6893        let with_grant_option =
6894            self.parse_keywords(&[Keyword::WITH, Keyword::GRANT, Keyword::OPTION]);
6895
6896        let granted_by = self
6897            .parse_keywords(&[Keyword::GRANTED, Keyword::BY])
6898            .then(|| self.parse_identifier().unwrap());
6899
6900        Ok(Statement::Grant {
6901            privileges,
6902            objects,
6903            grantees,
6904            with_grant_option,
6905            granted_by,
6906        })
6907    }
6908
6909    pub fn parse_grant_revoke_privileges_objects(
6910        &mut self,
6911    ) -> Result<(Privileges, GrantObjects), ParserError> {
6912        let privileges = if self.parse_keyword(Keyword::ALL) {
6913            Privileges::All {
6914                with_privileges_keyword: self.parse_keyword(Keyword::PRIVILEGES),
6915            }
6916        } else {
6917            let (actions, err): (Vec<_>, Vec<_>) = self
6918                .parse_comma_separated(Parser::parse_grant_permission)?
6919                .into_iter()
6920                .map(|(kw, columns)| match kw {
6921                    Keyword::DELETE => Ok(Action::Delete),
6922                    Keyword::INSERT => Ok(Action::Insert { columns }),
6923                    Keyword::REFERENCES => Ok(Action::References { columns }),
6924                    Keyword::SELECT => Ok(Action::Select { columns }),
6925                    Keyword::TRIGGER => Ok(Action::Trigger),
6926                    Keyword::TRUNCATE => Ok(Action::Truncate),
6927                    Keyword::UPDATE => Ok(Action::Update { columns }),
6928                    Keyword::USAGE => Ok(Action::Usage),
6929                    Keyword::CONNECT => Ok(Action::Connect),
6930                    Keyword::CREATE => Ok(Action::Create),
6931                    Keyword::EXECUTE => Ok(Action::Execute),
6932                    Keyword::TEMPORARY => Ok(Action::Temporary),
6933                    // This will cover all future added keywords to
6934                    // parse_grant_permission and unhandled in this
6935                    // match
6936                    _ => Err(kw),
6937                })
6938                .partition(Result::is_ok);
6939
6940            if !err.is_empty() {
6941                let errors: Vec<Keyword> = err.into_iter().filter_map(|x| x.err()).collect();
6942                return Err(ParserError::ParserError(format!(
6943                    "INTERNAL ERROR: GRANT/REVOKE unexpected keyword(s) - {errors:?}"
6944                )));
6945            }
6946            let act = actions.into_iter().filter_map(|x| x.ok()).collect();
6947            Privileges::Actions(act)
6948        };
6949
6950        self.expect_keyword(Keyword::ON)?;
6951
6952        let objects = if self.parse_keywords(&[
6953            Keyword::ALL,
6954            Keyword::TABLES,
6955            Keyword::IN,
6956            Keyword::SCHEMA,
6957        ]) {
6958            GrantObjects::AllTablesInSchema {
6959                schemas: self.parse_comma_separated(Parser::parse_object_name)?,
6960            }
6961        } else if self.parse_keywords(&[
6962            Keyword::ALL,
6963            Keyword::SEQUENCES,
6964            Keyword::IN,
6965            Keyword::SCHEMA,
6966        ]) {
6967            GrantObjects::AllSequencesInSchema {
6968                schemas: self.parse_comma_separated(Parser::parse_object_name)?,
6969            }
6970        } else {
6971            let object_type =
6972                self.parse_one_of_keywords(&[Keyword::SEQUENCE, Keyword::SCHEMA, Keyword::TABLE]);
6973            let objects = self.parse_comma_separated(Parser::parse_object_name);
6974            match object_type {
6975                Some(Keyword::SCHEMA) => GrantObjects::Schemas(objects?),
6976                Some(Keyword::SEQUENCE) => GrantObjects::Sequences(objects?),
6977                Some(Keyword::TABLE) | None => GrantObjects::Tables(objects?),
6978                _ => unreachable!(),
6979            }
6980        };
6981
6982        Ok((privileges, objects))
6983    }
6984
6985    pub fn parse_grant_permission(&mut self) -> Result<(Keyword, Option<Vec<Ident>>), ParserError> {
6986        if let Some(kw) = self.parse_one_of_keywords(&[
6987            Keyword::CONNECT,
6988            Keyword::CREATE,
6989            Keyword::DELETE,
6990            Keyword::EXECUTE,
6991            Keyword::INSERT,
6992            Keyword::REFERENCES,
6993            Keyword::SELECT,
6994            Keyword::TEMPORARY,
6995            Keyword::TRIGGER,
6996            Keyword::TRUNCATE,
6997            Keyword::UPDATE,
6998            Keyword::USAGE,
6999        ]) {
7000            let columns = match kw {
7001                Keyword::INSERT | Keyword::REFERENCES | Keyword::SELECT | Keyword::UPDATE => {
7002                    let columns = self.parse_parenthesized_column_list(Optional, false)?;
7003                    if columns.is_empty() {
7004                        None
7005                    } else {
7006                        Some(columns)
7007                    }
7008                }
7009                _ => None,
7010            };
7011            Ok((kw, columns))
7012        } else {
7013            self.expected("a privilege keyword", self.peek_token())?
7014        }
7015    }
7016
7017    /// Parse a REVOKE statement
7018    pub fn parse_revoke(&mut self) -> Result<Statement, ParserError> {
7019        let (privileges, objects) = self.parse_grant_revoke_privileges_objects()?;
7020
7021        self.expect_keyword(Keyword::FROM)?;
7022        let grantees = self.parse_comma_separated(Parser::parse_identifier)?;
7023
7024        let granted_by = self
7025            .parse_keywords(&[Keyword::GRANTED, Keyword::BY])
7026            .then(|| self.parse_identifier().unwrap());
7027
7028        let loc = self.peek_token().location;
7029        let cascade = self.parse_keyword(Keyword::CASCADE);
7030        let restrict = self.parse_keyword(Keyword::RESTRICT);
7031        if cascade && restrict {
7032            return parser_err!("Cannot specify both CASCADE and RESTRICT in REVOKE", loc);
7033        }
7034
7035        Ok(Statement::Revoke {
7036            privileges,
7037            objects,
7038            grantees,
7039            granted_by,
7040            cascade,
7041        })
7042    }
7043
7044    /// Parse an INSERT statement
7045    pub fn parse_insert(&mut self) -> Result<Statement, ParserError> {
7046        let or = if !dialect_of!(self is SQLiteDialect) {
7047            None
7048        } else if self.parse_keywords(&[Keyword::OR, Keyword::REPLACE]) {
7049            Some(SqliteOnConflict::Replace)
7050        } else if self.parse_keywords(&[Keyword::OR, Keyword::ROLLBACK]) {
7051            Some(SqliteOnConflict::Rollback)
7052        } else if self.parse_keywords(&[Keyword::OR, Keyword::ABORT]) {
7053            Some(SqliteOnConflict::Abort)
7054        } else if self.parse_keywords(&[Keyword::OR, Keyword::FAIL]) {
7055            Some(SqliteOnConflict::Fail)
7056        } else if self.parse_keywords(&[Keyword::OR, Keyword::IGNORE]) {
7057            Some(SqliteOnConflict::Ignore)
7058        } else if self.parse_keyword(Keyword::REPLACE) {
7059            Some(SqliteOnConflict::Replace)
7060        } else {
7061            None
7062        };
7063
7064        let ignore = dialect_of!(self is MySqlDialect | GenericDialect)
7065            && self.parse_keyword(Keyword::IGNORE);
7066
7067        let action = self.parse_one_of_keywords(&[Keyword::INTO, Keyword::OVERWRITE]);
7068        let into = action == Some(Keyword::INTO);
7069        let overwrite = action == Some(Keyword::OVERWRITE);
7070
7071        let local = self.parse_keyword(Keyword::LOCAL);
7072
7073        if self.parse_keyword(Keyword::DIRECTORY) {
7074            let path = self.parse_literal_string()?;
7075            let file_format = if self.parse_keywords(&[Keyword::STORED, Keyword::AS]) {
7076                Some(self.parse_file_format()?)
7077            } else {
7078                None
7079            };
7080            let source = Box::new(self.parse_query()?);
7081            Ok(Statement::Directory {
7082                local,
7083                path,
7084                overwrite,
7085                file_format,
7086                source,
7087            })
7088        } else {
7089            // Hive lets you put table here regardless
7090            let table = self.parse_keyword(Keyword::TABLE);
7091            let table_name = self.parse_object_name()?;
7092            let is_mysql = dialect_of!(self is MySqlDialect);
7093            let columns = self.parse_parenthesized_column_list(Optional, is_mysql)?;
7094
7095            let partitioned = if self.parse_keyword(Keyword::PARTITION) {
7096                self.expect_token(&Token::LParen)?;
7097                let r = Some(self.parse_comma_separated(Parser::parse_expr)?);
7098                self.expect_token(&Token::RParen)?;
7099                r
7100            } else {
7101                None
7102            };
7103
7104            // Hive allows you to specify columns after partitions as well if you want.
7105            let after_columns = self.parse_parenthesized_column_list(Optional, false)?;
7106
7107            let source = Box::new(self.parse_query()?);
7108            let on = if self.parse_keyword(Keyword::ON) {
7109                if self.parse_keyword(Keyword::CONFLICT) {
7110                    let conflict_target =
7111                        if self.parse_keywords(&[Keyword::ON, Keyword::CONSTRAINT]) {
7112                            Some(ConflictTarget::OnConstraint(self.parse_object_name()?))
7113                        } else if self.peek_token() == Token::LParen {
7114                            Some(ConflictTarget::Columns(
7115                                self.parse_parenthesized_column_list(IsOptional::Mandatory, false)?,
7116                            ))
7117                        } else {
7118                            None
7119                        };
7120
7121                    self.expect_keyword(Keyword::DO)?;
7122                    let action = if self.parse_keyword(Keyword::NOTHING) {
7123                        OnConflictAction::DoNothing
7124                    } else {
7125                        self.expect_keyword(Keyword::UPDATE)?;
7126                        self.expect_keyword(Keyword::SET)?;
7127                        let assignments = self.parse_comma_separated(Parser::parse_assignment)?;
7128                        let selection = if self.parse_keyword(Keyword::WHERE) {
7129                            Some(self.parse_expr()?)
7130                        } else {
7131                            None
7132                        };
7133                        OnConflictAction::DoUpdate(DoUpdate {
7134                            assignments,
7135                            selection,
7136                        })
7137                    };
7138
7139                    Some(OnInsert::OnConflict(OnConflict {
7140                        conflict_target,
7141                        action,
7142                    }))
7143                } else {
7144                    self.expect_keyword(Keyword::DUPLICATE)?;
7145                    self.expect_keyword(Keyword::KEY)?;
7146                    self.expect_keyword(Keyword::UPDATE)?;
7147                    let l = self.parse_comma_separated(Parser::parse_assignment)?;
7148
7149                    Some(OnInsert::DuplicateKeyUpdate(l))
7150                }
7151            } else {
7152                None
7153            };
7154
7155            let returning = if self.parse_keyword(Keyword::RETURNING) {
7156                Some(self.parse_comma_separated(Parser::parse_select_item)?)
7157            } else {
7158                None
7159            };
7160
7161            Ok(Statement::Insert {
7162                or,
7163                table_name,
7164                ignore,
7165                into,
7166                overwrite,
7167                partitioned,
7168                columns,
7169                after_columns,
7170                source,
7171                table,
7172                on,
7173                returning,
7174            })
7175        }
7176    }
7177
7178    pub fn parse_update(&mut self) -> Result<Statement, ParserError> {
7179        let table = self.parse_table_and_joins()?;
7180        self.expect_keyword(Keyword::SET)?;
7181        let assignments = self.parse_comma_separated(Parser::parse_assignment)?;
7182        let from = if self.parse_keyword(Keyword::FROM)
7183            && dialect_of!(self is GenericDialect | PostgreSqlDialect | DuckDbDialect | BigQueryDialect | SnowflakeDialect | RedshiftSqlDialect | MsSqlDialect)
7184        {
7185            Some(self.parse_table_and_joins()?)
7186        } else {
7187            None
7188        };
7189        let selection = if self.parse_keyword(Keyword::WHERE) {
7190            Some(self.parse_expr()?)
7191        } else {
7192            None
7193        };
7194        let returning = if self.parse_keyword(Keyword::RETURNING) {
7195            Some(self.parse_comma_separated(Parser::parse_select_item)?)
7196        } else {
7197            None
7198        };
7199        Ok(Statement::Update {
7200            table,
7201            assignments,
7202            from,
7203            selection,
7204            returning,
7205        })
7206    }
7207
7208    /// Parse a `var = expr` assignment, used in an UPDATE statement
7209    pub fn parse_assignment(&mut self) -> Result<Assignment, ParserError> {
7210        let id = self.parse_identifiers()?;
7211        self.expect_token(&Token::Eq)?;
7212        let value = self.parse_expr()?;
7213        Ok(Assignment { id, value })
7214    }
7215
7216    pub fn parse_function_args(&mut self) -> Result<FunctionArg, ParserError> {
7217        if self.peek_nth_token(1) == Token::RArrow {
7218            let name = self.parse_identifier()?;
7219
7220            self.expect_token(&Token::RArrow)?;
7221            let arg = self.parse_wildcard_expr()?.into();
7222
7223            Ok(FunctionArg::Named { name, arg })
7224        } else {
7225            Ok(FunctionArg::Unnamed(self.parse_wildcard_expr()?.into()))
7226        }
7227    }
7228
7229    pub fn parse_optional_args(&mut self) -> Result<Vec<FunctionArg>, ParserError> {
7230        if self.consume_token(&Token::RParen) {
7231            Ok(vec![])
7232        } else {
7233            let args = self.parse_comma_separated(Parser::parse_function_args)?;
7234            self.expect_token(&Token::RParen)?;
7235            Ok(args)
7236        }
7237    }
7238
7239    pub fn parse_optional_args_with_orderby(
7240        &mut self,
7241    ) -> Result<(Vec<FunctionArg>, Vec<OrderByExpr>), ParserError> {
7242        if self.consume_token(&Token::RParen) {
7243            Ok((vec![], vec![]))
7244        } else {
7245            // Snowflake permits a subquery to be passed as an argument without
7246            // an enclosing set of parens if it's the only argument.
7247            if dialect_of!(self is SnowflakeDialect)
7248                && self
7249                    .parse_one_of_keywords(&[Keyword::WITH, Keyword::SELECT])
7250                    .is_some()
7251            {
7252                self.prev_token();
7253                let subquery = self.parse_query()?;
7254                self.expect_token(&Token::RParen)?;
7255                return Ok((
7256                    vec![FunctionArg::Unnamed(FunctionArgExpr::from(
7257                        WildcardExpr::Expr(Expr::Subquery(Box::new(subquery))),
7258                    ))],
7259                    vec![],
7260                ));
7261            }
7262
7263            let args = self.parse_comma_separated(Parser::parse_function_args)?;
7264            let order_by = if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
7265                self.parse_comma_separated(Parser::parse_order_by_expr)?
7266            } else {
7267                vec![]
7268            };
7269            self.expect_token(&Token::RParen)?;
7270            Ok((args, order_by))
7271        }
7272    }
7273
7274    /// Parse a comma-delimited list of projections after SELECT
7275    pub fn parse_select_item(&mut self) -> Result<SelectItem, ParserError> {
7276        match self.parse_wildcard_expr()? {
7277            WildcardExpr::Expr(expr) => {
7278                let expr: Expr = if self.dialect.supports_filter_during_aggregation()
7279                    && self.parse_keyword(Keyword::FILTER)
7280                {
7281                    let i = self.index - 1;
7282                    if self.consume_token(&Token::LParen) && self.parse_keyword(Keyword::WHERE) {
7283                        let filter = self.parse_expr()?;
7284                        self.expect_token(&Token::RParen)?;
7285                        Expr::AggregateExpressionWithFilter {
7286                            expr: Box::new(expr),
7287                            filter: Box::new(filter),
7288                        }
7289                    } else {
7290                        self.index = i;
7291                        expr
7292                    }
7293                } else {
7294                    expr
7295                };
7296                self.parse_optional_alias(keywords::RESERVED_FOR_COLUMN_ALIAS)
7297                    .map(|alias| match alias {
7298                        Some(alias) => SelectItem::ExprWithAlias { expr, alias },
7299                        None => SelectItem::UnnamedExpr(expr),
7300                    })
7301            }
7302            WildcardExpr::QualifiedWildcard(prefix) => Ok(SelectItem::QualifiedWildcard(
7303                prefix,
7304                self.parse_wildcard_additional_options()?,
7305            )),
7306            WildcardExpr::Wildcard => Ok(SelectItem::Wildcard(
7307                self.parse_wildcard_additional_options()?,
7308            )),
7309        }
7310    }
7311
7312    /// Parse an [`WildcardAdditionalOptions`] information for wildcard select items.
7313    ///
7314    /// If it is not possible to parse it, will return an option.
7315    pub fn parse_wildcard_additional_options(
7316        &mut self,
7317    ) -> Result<WildcardAdditionalOptions, ParserError> {
7318        let opt_exclude = if dialect_of!(self is GenericDialect | DuckDbDialect | SnowflakeDialect)
7319        {
7320            self.parse_optional_select_item_exclude()?
7321        } else {
7322            None
7323        };
7324        let opt_except = if dialect_of!(self is GenericDialect | BigQueryDialect | ClickHouseDialect)
7325        {
7326            self.parse_optional_select_item_except()?
7327        } else {
7328            None
7329        };
7330        let opt_rename = if dialect_of!(self is GenericDialect | SnowflakeDialect) {
7331            self.parse_optional_select_item_rename()?
7332        } else {
7333            None
7334        };
7335
7336        let opt_replace = if dialect_of!(self is GenericDialect | BigQueryDialect | ClickHouseDialect)
7337        {
7338            self.parse_optional_select_item_replace()?
7339        } else {
7340            None
7341        };
7342
7343        Ok(WildcardAdditionalOptions {
7344            opt_exclude,
7345            opt_except,
7346            opt_rename,
7347            opt_replace,
7348        })
7349    }
7350
7351    /// Parse an [`Exclude`](ExcludeSelectItem) information for wildcard select items.
7352    ///
7353    /// If it is not possible to parse it, will return an option.
7354    pub fn parse_optional_select_item_exclude(
7355        &mut self,
7356    ) -> Result<Option<ExcludeSelectItem>, ParserError> {
7357        let opt_exclude = if self.parse_keyword(Keyword::EXCLUDE) {
7358            if self.consume_token(&Token::LParen) {
7359                let columns = self.parse_comma_separated(|parser| parser.parse_identifier())?;
7360                self.expect_token(&Token::RParen)?;
7361                Some(ExcludeSelectItem::Multiple(columns))
7362            } else {
7363                let column = self.parse_identifier()?;
7364                Some(ExcludeSelectItem::Single(column))
7365            }
7366        } else {
7367            None
7368        };
7369
7370        Ok(opt_exclude)
7371    }
7372
7373    /// Parse an [`Except`](ExceptSelectItem) information for wildcard select items.
7374    ///
7375    /// If it is not possible to parse it, will return an option.
7376    pub fn parse_optional_select_item_except(
7377        &mut self,
7378    ) -> Result<Option<ExceptSelectItem>, ParserError> {
7379        let opt_except = if self.parse_keyword(Keyword::EXCEPT) {
7380            if self.peek_token().token == Token::LParen {
7381                let idents = self.parse_parenthesized_column_list(Mandatory, false)?;
7382                match &idents[..] {
7383                    [] => {
7384                        return self.expected(
7385                            "at least one column should be parsed by the expect clause",
7386                            self.peek_token(),
7387                        )?;
7388                    }
7389                    [first, idents @ ..] => Some(ExceptSelectItem {
7390                        first_element: first.clone(),
7391                        additional_elements: idents.to_vec(),
7392                    }),
7393                }
7394            } else {
7395                // Clickhouse allows EXCEPT column_name
7396                let ident = self.parse_identifier()?;
7397                Some(ExceptSelectItem {
7398                    first_element: ident,
7399                    additional_elements: vec![],
7400                })
7401            }
7402        } else {
7403            None
7404        };
7405
7406        Ok(opt_except)
7407    }
7408
7409    /// Parse a [`Rename`](RenameSelectItem) information for wildcard select items.
7410    pub fn parse_optional_select_item_rename(
7411        &mut self,
7412    ) -> Result<Option<RenameSelectItem>, ParserError> {
7413        let opt_rename = if self.parse_keyword(Keyword::RENAME) {
7414            if self.consume_token(&Token::LParen) {
7415                let idents =
7416                    self.parse_comma_separated(|parser| parser.parse_identifier_with_alias())?;
7417                self.expect_token(&Token::RParen)?;
7418                Some(RenameSelectItem::Multiple(idents))
7419            } else {
7420                let ident = self.parse_identifier_with_alias()?;
7421                Some(RenameSelectItem::Single(ident))
7422            }
7423        } else {
7424            None
7425        };
7426
7427        Ok(opt_rename)
7428    }
7429
7430    /// Parse a [`Replace`](ReplaceSelectItem) information for wildcard select items.
7431    pub fn parse_optional_select_item_replace(
7432        &mut self,
7433    ) -> Result<Option<ReplaceSelectItem>, ParserError> {
7434        let opt_replace = if self.parse_keyword(Keyword::REPLACE) {
7435            if self.consume_token(&Token::LParen) {
7436                let items = self.parse_comma_separated(|parser| {
7437                    Ok(Box::new(parser.parse_replace_elements()?))
7438                })?;
7439                self.expect_token(&Token::RParen)?;
7440                Some(ReplaceSelectItem { items })
7441            } else {
7442                let tok = self.next_token();
7443                return self.expected("( after REPLACE but", tok);
7444            }
7445        } else {
7446            None
7447        };
7448
7449        Ok(opt_replace)
7450    }
7451    pub fn parse_replace_elements(&mut self) -> Result<ReplaceSelectElement, ParserError> {
7452        let expr = self.parse_expr()?;
7453        let as_keyword = self.parse_keyword(Keyword::AS);
7454        let ident = self.parse_identifier()?;
7455        Ok(ReplaceSelectElement {
7456            expr,
7457            column_name: ident,
7458            as_keyword,
7459        })
7460    }
7461
7462    /// Parse an expression, optionally followed by ASC or DESC (used in ORDER BY)
7463    pub fn parse_order_by_expr(&mut self) -> Result<OrderByExpr, ParserError> {
7464        let expr = self.parse_expr()?;
7465
7466        let asc = if self.parse_keyword(Keyword::ASC) {
7467            Some(true)
7468        } else if self.parse_keyword(Keyword::DESC) {
7469            Some(false)
7470        } else {
7471            None
7472        };
7473
7474        let nulls_first = if self.parse_keywords(&[Keyword::NULLS, Keyword::FIRST]) {
7475            Some(true)
7476        } else if self.parse_keywords(&[Keyword::NULLS, Keyword::LAST]) {
7477            Some(false)
7478        } else {
7479            None
7480        };
7481
7482        Ok(OrderByExpr {
7483            expr,
7484            asc,
7485            nulls_first,
7486        })
7487    }
7488
7489    /// Parse a TOP clause, MSSQL equivalent of LIMIT,
7490    /// that follows after `SELECT [DISTINCT]`.
7491    pub fn parse_top(&mut self) -> Result<Top, ParserError> {
7492        let quantity = if self.consume_token(&Token::LParen) {
7493            let quantity = self.parse_expr()?;
7494            self.expect_token(&Token::RParen)?;
7495            Some(quantity)
7496        } else {
7497            Some(Expr::Value(self.parse_number_value()?))
7498        };
7499
7500        let percent = self.parse_keyword(Keyword::PERCENT);
7501
7502        let with_ties = self.parse_keywords(&[Keyword::WITH, Keyword::TIES]);
7503
7504        Ok(Top {
7505            with_ties,
7506            percent,
7507            quantity,
7508        })
7509    }
7510
7511    /// Parse a LIMIT clause
7512    pub fn parse_limit(&mut self) -> Result<Option<Expr>, ParserError> {
7513        if self.parse_keyword(Keyword::ALL) {
7514            Ok(None)
7515        } else {
7516            Ok(Some(self.parse_expr()?))
7517        }
7518    }
7519
7520    /// Parse an OFFSET clause
7521    pub fn parse_offset(&mut self) -> Result<Offset, ParserError> {
7522        let value = self.parse_expr()?;
7523        let rows = if self.parse_keyword(Keyword::ROW) {
7524            OffsetRows::Row
7525        } else if self.parse_keyword(Keyword::ROWS) {
7526            OffsetRows::Rows
7527        } else {
7528            OffsetRows::None
7529        };
7530        Ok(Offset { value, rows })
7531    }
7532
7533    /// Parse a FETCH clause
7534    pub fn parse_fetch(&mut self) -> Result<Fetch, ParserError> {
7535        self.expect_one_of_keywords(&[Keyword::FIRST, Keyword::NEXT])?;
7536        let (quantity, percent) = if self
7537            .parse_one_of_keywords(&[Keyword::ROW, Keyword::ROWS])
7538            .is_some()
7539        {
7540            (None, false)
7541        } else {
7542            let quantity = Expr::Value(self.parse_value()?);
7543            let percent = self.parse_keyword(Keyword::PERCENT);
7544            self.expect_one_of_keywords(&[Keyword::ROW, Keyword::ROWS])?;
7545            (Some(quantity), percent)
7546        };
7547        let with_ties = if self.parse_keyword(Keyword::ONLY) {
7548            false
7549        } else if self.parse_keywords(&[Keyword::WITH, Keyword::TIES]) {
7550            true
7551        } else {
7552            return self.expected("one of ONLY or WITH TIES", self.peek_token());
7553        };
7554        Ok(Fetch {
7555            with_ties,
7556            percent,
7557            quantity,
7558        })
7559    }
7560
7561    /// Parse a FOR UPDATE/FOR SHARE clause
7562    pub fn parse_lock(&mut self) -> Result<LockClause, ParserError> {
7563        let lock_type = match self.expect_one_of_keywords(&[Keyword::UPDATE, Keyword::SHARE])? {
7564            Keyword::UPDATE => LockType::Update,
7565            Keyword::SHARE => LockType::Share,
7566            _ => unreachable!(),
7567        };
7568        let of = if self.parse_keyword(Keyword::OF) {
7569            Some(self.parse_object_name()?)
7570        } else {
7571            None
7572        };
7573        let nonblock = if self.parse_keyword(Keyword::NOWAIT) {
7574            Some(NonBlock::Nowait)
7575        } else if self.parse_keywords(&[Keyword::SKIP, Keyword::LOCKED]) {
7576            Some(NonBlock::SkipLocked)
7577        } else {
7578            None
7579        };
7580        Ok(LockClause {
7581            lock_type,
7582            of,
7583            nonblock,
7584        })
7585    }
7586
7587    pub fn parse_values(&mut self, allow_empty: bool) -> Result<Values, ParserError> {
7588        let mut explicit_row = false;
7589
7590        let rows = self.parse_comma_separated(|parser| {
7591            if parser.parse_keyword(Keyword::ROW) {
7592                explicit_row = true;
7593            }
7594
7595            parser.expect_token(&Token::LParen)?;
7596            if allow_empty && parser.peek_token().token == Token::RParen {
7597                parser.next_token();
7598                Ok(vec![])
7599            } else {
7600                let exprs = parser.parse_comma_separated(Parser::parse_expr)?;
7601                parser.expect_token(&Token::RParen)?;
7602                Ok(exprs)
7603            }
7604        })?;
7605        Ok(Values { explicit_row, rows })
7606    }
7607
7608    pub fn parse_start_transaction(&mut self) -> Result<Statement, ParserError> {
7609        self.expect_keyword(Keyword::TRANSACTION)?;
7610        Ok(Statement::StartTransaction {
7611            modes: self.parse_transaction_modes()?,
7612            begin: false,
7613        })
7614    }
7615
7616    pub fn parse_begin(&mut self) -> Result<Statement, ParserError> {
7617        let _ = self.parse_one_of_keywords(&[Keyword::TRANSACTION, Keyword::WORK]);
7618        Ok(Statement::StartTransaction {
7619            modes: self.parse_transaction_modes()?,
7620            begin: true,
7621        })
7622    }
7623
7624    pub fn parse_transaction_modes(&mut self) -> Result<Vec<TransactionMode>, ParserError> {
7625        let mut modes = vec![];
7626        let mut required = false;
7627        loop {
7628            let mode = if self.parse_keywords(&[Keyword::ISOLATION, Keyword::LEVEL]) {
7629                let iso_level = if self.parse_keywords(&[Keyword::READ, Keyword::UNCOMMITTED]) {
7630                    TransactionIsolationLevel::ReadUncommitted
7631                } else if self.parse_keywords(&[Keyword::READ, Keyword::COMMITTED]) {
7632                    TransactionIsolationLevel::ReadCommitted
7633                } else if self.parse_keywords(&[Keyword::REPEATABLE, Keyword::READ]) {
7634                    TransactionIsolationLevel::RepeatableRead
7635                } else if self.parse_keyword(Keyword::SERIALIZABLE) {
7636                    TransactionIsolationLevel::Serializable
7637                } else {
7638                    self.expected("isolation level", self.peek_token())?
7639                };
7640                TransactionMode::IsolationLevel(iso_level)
7641            } else if self.parse_keywords(&[Keyword::READ, Keyword::ONLY]) {
7642                TransactionMode::AccessMode(TransactionAccessMode::ReadOnly)
7643            } else if self.parse_keywords(&[Keyword::READ, Keyword::WRITE]) {
7644                TransactionMode::AccessMode(TransactionAccessMode::ReadWrite)
7645            } else if required {
7646                self.expected("transaction mode", self.peek_token())?
7647            } else {
7648                break;
7649            };
7650            modes.push(mode);
7651            // ANSI requires a comma after each transaction mode, but
7652            // PostgreSQL, for historical reasons, does not. We follow
7653            // PostgreSQL in making the comma optional, since that is strictly
7654            // more general.
7655            required = self.consume_token(&Token::Comma);
7656        }
7657        Ok(modes)
7658    }
7659
7660    pub fn parse_commit(&mut self) -> Result<Statement, ParserError> {
7661        Ok(Statement::Commit {
7662            chain: self.parse_commit_rollback_chain()?,
7663        })
7664    }
7665
7666    pub fn parse_rollback(&mut self) -> Result<Statement, ParserError> {
7667        Ok(Statement::Rollback {
7668            chain: self.parse_commit_rollback_chain()?,
7669        })
7670    }
7671
7672    pub fn parse_commit_rollback_chain(&mut self) -> Result<bool, ParserError> {
7673        let _ = self.parse_one_of_keywords(&[Keyword::TRANSACTION, Keyword::WORK]);
7674        if self.parse_keyword(Keyword::AND) {
7675            let chain = !self.parse_keyword(Keyword::NO);
7676            self.expect_keyword(Keyword::CHAIN)?;
7677            Ok(chain)
7678        } else {
7679            Ok(false)
7680        }
7681    }
7682
7683    pub fn parse_deallocate(&mut self) -> Result<Statement, ParserError> {
7684        let prepare = self.parse_keyword(Keyword::PREPARE);
7685        let name = self.parse_identifier()?;
7686        Ok(Statement::Deallocate { name, prepare })
7687    }
7688
7689    pub fn parse_execute(&mut self) -> Result<Statement, ParserError> {
7690        let name = self.parse_identifier()?;
7691
7692        let mut parameters = vec![];
7693        if self.consume_token(&Token::LParen) {
7694            parameters = self.parse_comma_separated(Parser::parse_expr)?;
7695            self.expect_token(&Token::RParen)?;
7696        }
7697
7698        Ok(Statement::Execute { name, parameters })
7699    }
7700
7701    pub fn parse_prepare(&mut self) -> Result<Statement, ParserError> {
7702        let name = self.parse_identifier()?;
7703
7704        let mut data_types = vec![];
7705        if self.consume_token(&Token::LParen) {
7706            data_types = self.parse_comma_separated(Parser::parse_data_type)?;
7707            self.expect_token(&Token::RParen)?;
7708        }
7709
7710        self.expect_keyword(Keyword::AS)?;
7711        let statement = Box::new(self.parse_statement()?);
7712        Ok(Statement::Prepare {
7713            name,
7714            data_types,
7715            statement,
7716        })
7717    }
7718
7719    pub fn parse_merge_clauses(&mut self) -> Result<Vec<MergeClause>, ParserError> {
7720        let mut clauses: Vec<MergeClause> = vec![];
7721        loop {
7722            if self.peek_token() == Token::EOF || self.peek_token() == Token::SemiColon {
7723                break;
7724            }
7725            self.expect_keyword(Keyword::WHEN)?;
7726
7727            let is_not_matched = self.parse_keyword(Keyword::NOT);
7728            self.expect_keyword(Keyword::MATCHED)?;
7729
7730            let predicate = if self.parse_keyword(Keyword::AND) {
7731                Some(self.parse_expr()?)
7732            } else {
7733                None
7734            };
7735
7736            self.expect_keyword(Keyword::THEN)?;
7737
7738            clauses.push(
7739                match self.parse_one_of_keywords(&[
7740                    Keyword::UPDATE,
7741                    Keyword::INSERT,
7742                    Keyword::DELETE,
7743                ]) {
7744                    Some(Keyword::UPDATE) => {
7745                        if is_not_matched {
7746                            return Err(ParserError::ParserError(
7747                                "UPDATE in NOT MATCHED merge clause".to_string(),
7748                            ));
7749                        }
7750                        self.expect_keyword(Keyword::SET)?;
7751                        let assignments = self.parse_comma_separated(Parser::parse_assignment)?;
7752                        MergeClause::MatchedUpdate {
7753                            predicate,
7754                            assignments,
7755                        }
7756                    }
7757                    Some(Keyword::DELETE) => {
7758                        if is_not_matched {
7759                            return Err(ParserError::ParserError(
7760                                "DELETE in NOT MATCHED merge clause".to_string(),
7761                            ));
7762                        }
7763                        MergeClause::MatchedDelete(predicate)
7764                    }
7765                    Some(Keyword::INSERT) => {
7766                        if !is_not_matched {
7767                            return Err(ParserError::ParserError(
7768                                "INSERT in MATCHED merge clause".to_string(),
7769                            ));
7770                        }
7771                        let is_mysql = dialect_of!(self is MySqlDialect);
7772                        let columns = self.parse_parenthesized_column_list(Optional, is_mysql)?;
7773                        self.expect_keyword(Keyword::VALUES)?;
7774                        let values = self.parse_values(is_mysql)?;
7775                        MergeClause::NotMatched {
7776                            predicate,
7777                            columns,
7778                            values,
7779                        }
7780                    }
7781                    Some(_) => {
7782                        return Err(ParserError::ParserError(
7783                            "expected UPDATE, DELETE or INSERT in merge clause".to_string(),
7784                        ));
7785                    }
7786                    None => {
7787                        return Err(ParserError::ParserError(
7788                            "expected UPDATE, DELETE or INSERT in merge clause".to_string(),
7789                        ));
7790                    }
7791                },
7792            );
7793        }
7794        Ok(clauses)
7795    }
7796
7797    pub fn parse_merge(&mut self) -> Result<Statement, ParserError> {
7798        let into = self.parse_keyword(Keyword::INTO);
7799
7800        let table = self.parse_table_factor()?;
7801
7802        self.expect_keyword(Keyword::USING)?;
7803        let source = self.parse_table_factor()?;
7804        self.expect_keyword(Keyword::ON)?;
7805        let on = self.parse_expr()?;
7806        let clauses = self.parse_merge_clauses()?;
7807
7808        Ok(Statement::Merge {
7809            into,
7810            table,
7811            source,
7812            on: Box::new(on),
7813            clauses,
7814        })
7815    }
7816
7817    // PRAGMA [schema-name '.'] pragma-name [('=' pragma-value) | '(' pragma-value ')']
7818    pub fn parse_pragma(&mut self) -> Result<Statement, ParserError> {
7819        let name = self.parse_object_name()?;
7820        if self.consume_token(&Token::LParen) {
7821            let value = self.parse_number_value()?;
7822            self.expect_token(&Token::RParen)?;
7823            Ok(Statement::Pragma {
7824                name,
7825                value: Some(value),
7826                is_eq: false,
7827            })
7828        } else if self.consume_token(&Token::Eq) {
7829            Ok(Statement::Pragma {
7830                name,
7831                value: Some(self.parse_number_value()?),
7832                is_eq: true,
7833            })
7834        } else {
7835            Ok(Statement::Pragma {
7836                name,
7837                value: None,
7838                is_eq: false,
7839            })
7840        }
7841    }
7842
7843    /// ```sql
7844    /// CREATE [ { TEMPORARY | TEMP } ] SEQUENCE [ IF NOT EXISTS ] <sequence_name>
7845    /// ```
7846    ///
7847    /// See [Postgres docs](https://www.postgresql.org/docs/current/sql-createsequence.html) for more details.
7848    pub fn parse_create_sequence(&mut self, temporary: bool) -> Result<Statement, ParserError> {
7849        //[ IF NOT EXISTS ]
7850        let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
7851        //name
7852        let name = self.parse_object_name()?;
7853        //[ AS data_type ]
7854        let mut data_type: Option<DataType> = None;
7855        if self.parse_keywords(&[Keyword::AS]) {
7856            data_type = Some(self.parse_data_type()?)
7857        }
7858        let sequence_options = self.parse_create_sequence_options()?;
7859        // [ OWNED BY { table_name.column_name | NONE } ]
7860        let owned_by = if self.parse_keywords(&[Keyword::OWNED, Keyword::BY]) {
7861            if self.parse_keywords(&[Keyword::NONE]) {
7862                Some(ObjectName(vec![Ident::new("NONE")]))
7863            } else {
7864                Some(self.parse_object_name()?)
7865            }
7866        } else {
7867            None
7868        };
7869        Ok(Statement::CreateSequence {
7870            temporary,
7871            if_not_exists,
7872            name,
7873            data_type,
7874            sequence_options,
7875            owned_by,
7876        })
7877    }
7878
7879    fn parse_create_sequence_options(&mut self) -> Result<Vec<SequenceOptions>, ParserError> {
7880        let mut sequence_options = vec![];
7881        //[ INCREMENT [ BY ] increment ]
7882        if self.parse_keywords(&[Keyword::INCREMENT]) {
7883            if self.parse_keywords(&[Keyword::BY]) {
7884                sequence_options.push(SequenceOptions::IncrementBy(
7885                    Expr::Value(self.parse_number_value()?),
7886                    true,
7887                ));
7888            } else {
7889                sequence_options.push(SequenceOptions::IncrementBy(
7890                    Expr::Value(self.parse_number_value()?),
7891                    false,
7892                ));
7893            }
7894        }
7895        //[ MINVALUE minvalue | NO MINVALUE ]
7896        if self.parse_keyword(Keyword::MINVALUE) {
7897            sequence_options.push(SequenceOptions::MinValue(MinMaxValue::Some(Expr::Value(
7898                self.parse_number_value()?,
7899            ))));
7900        } else if self.parse_keywords(&[Keyword::NO, Keyword::MINVALUE]) {
7901            sequence_options.push(SequenceOptions::MinValue(MinMaxValue::None));
7902        } else {
7903            sequence_options.push(SequenceOptions::MinValue(MinMaxValue::Empty));
7904        }
7905        //[ MAXVALUE maxvalue | NO MAXVALUE ]
7906        if self.parse_keywords(&[Keyword::MAXVALUE]) {
7907            sequence_options.push(SequenceOptions::MaxValue(MinMaxValue::Some(Expr::Value(
7908                self.parse_number_value()?,
7909            ))));
7910        } else if self.parse_keywords(&[Keyword::NO, Keyword::MAXVALUE]) {
7911            sequence_options.push(SequenceOptions::MaxValue(MinMaxValue::None));
7912        } else {
7913            sequence_options.push(SequenceOptions::MaxValue(MinMaxValue::Empty));
7914        }
7915        //[ START [ WITH ] start ]
7916        if self.parse_keywords(&[Keyword::START]) {
7917            if self.parse_keywords(&[Keyword::WITH]) {
7918                sequence_options.push(SequenceOptions::StartWith(
7919                    Expr::Value(self.parse_number_value()?),
7920                    true,
7921                ));
7922            } else {
7923                sequence_options.push(SequenceOptions::StartWith(
7924                    Expr::Value(self.parse_number_value()?),
7925                    false,
7926                ));
7927            }
7928        }
7929        //[ CACHE cache ]
7930        if self.parse_keywords(&[Keyword::CACHE]) {
7931            sequence_options.push(SequenceOptions::Cache(Expr::Value(
7932                self.parse_number_value()?,
7933            )));
7934        }
7935        // [ [ NO ] CYCLE ]
7936        if self.parse_keywords(&[Keyword::NO]) {
7937            if self.parse_keywords(&[Keyword::CYCLE]) {
7938                sequence_options.push(SequenceOptions::Cycle(true));
7939            }
7940        } else if self.parse_keywords(&[Keyword::CYCLE]) {
7941            sequence_options.push(SequenceOptions::Cycle(false));
7942        }
7943        Ok(sequence_options)
7944    }
7945
7946    /// The index of the first unprocessed token.
7947    pub fn index(&self) -> usize {
7948        self.index
7949    }
7950
7951    pub fn parse_named_window(&mut self) -> Result<NamedWindowDefinition, ParserError> {
7952        let ident = self.parse_identifier()?;
7953        self.expect_keyword(Keyword::AS)?;
7954        self.expect_token(&Token::LParen)?;
7955        let window_spec = self.parse_window_spec()?;
7956        Ok(NamedWindowDefinition(ident, window_spec))
7957    }
7958
7959    pub fn parse_create_procedure(&mut self, or_alter: bool) -> Result<Statement, ParserError> {
7960        let name = self.parse_object_name()?;
7961        let params = self.parse_optional_procedure_parameters()?;
7962        self.expect_keyword(Keyword::AS)?;
7963        self.expect_keyword(Keyword::BEGIN)?;
7964        let statements = self.parse_statements()?;
7965        self.expect_keyword(Keyword::END)?;
7966        Ok(Statement::CreateProcedure {
7967            name,
7968            or_alter,
7969            params,
7970            body: statements,
7971        })
7972    }
7973
7974    pub fn parse_window_spec(&mut self) -> Result<WindowSpec, ParserError> {
7975        let partition_by = if self.parse_keywords(&[Keyword::PARTITION, Keyword::BY]) {
7976            self.parse_comma_separated(Parser::parse_expr)?
7977        } else {
7978            vec![]
7979        };
7980        let order_by = if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
7981            self.parse_comma_separated(Parser::parse_order_by_expr)?
7982        } else {
7983            vec![]
7984        };
7985        let window_frame = if !self.consume_token(&Token::RParen) {
7986            let window_frame = self.parse_window_frame()?;
7987            self.expect_token(&Token::RParen)?;
7988            Some(window_frame)
7989        } else {
7990            None
7991        };
7992        Ok(WindowSpec {
7993            partition_by,
7994            order_by,
7995            window_frame,
7996        })
7997    }
7998
7999    pub fn parse_create_type(&mut self) -> Result<Statement, ParserError> {
8000        let name = self.parse_object_name()?;
8001        self.expect_keyword(Keyword::AS)?;
8002
8003        let mut attributes = vec![];
8004        if !self.consume_token(&Token::LParen) || self.consume_token(&Token::RParen) {
8005            return Ok(Statement::CreateType {
8006                name,
8007                representation: UserDefinedTypeRepresentation::Composite { attributes },
8008            });
8009        }
8010
8011        loop {
8012            let attr_name = self.parse_identifier()?;
8013            let attr_data_type = self.parse_data_type()?;
8014            let attr_collation = if self.parse_keyword(Keyword::COLLATE) {
8015                Some(self.parse_object_name()?)
8016            } else {
8017                None
8018            };
8019            attributes.push(UserDefinedTypeCompositeAttributeDef {
8020                name: attr_name,
8021                data_type: attr_data_type,
8022                collation: attr_collation,
8023            });
8024            let comma = self.consume_token(&Token::Comma);
8025            if self.consume_token(&Token::RParen) {
8026                // allow a trailing comma
8027                break;
8028            } else if !comma {
8029                return self.expected("',' or ')' after attribute definition", self.peek_token());
8030            }
8031        }
8032
8033        Ok(Statement::CreateType {
8034            name,
8035            representation: UserDefinedTypeRepresentation::Composite { attributes },
8036        })
8037    }
8038
8039    fn parse_partitions(&mut self) -> Result<Vec<Ident>, ParserError> {
8040        self.expect_token(&Token::LParen)?;
8041        let partitions = self.parse_comma_separated(Parser::parse_identifier)?;
8042        self.expect_token(&Token::RParen)?;
8043        Ok(partitions)
8044    }
8045}
8046
8047impl Word {
8048    pub fn to_ident(&self) -> Ident {
8049        Ident {
8050            value: self.value.clone(),
8051            quote_style: self.quote_style,
8052        }
8053    }
8054}
8055
8056#[cfg(test)]
8057mod tests {
8058    use crate::test_utils::{all_dialects, TestedDialects};
8059
8060    use super::*;
8061
8062    #[test]
8063    fn test_prev_index() {
8064        let sql = "SELECT version";
8065        all_dialects().run_parser_method(sql, |parser| {
8066            assert_eq!(parser.peek_token(), Token::make_keyword("SELECT"));
8067            assert_eq!(parser.next_token(), Token::make_keyword("SELECT"));
8068            parser.prev_token();
8069            assert_eq!(parser.next_token(), Token::make_keyword("SELECT"));
8070            assert_eq!(parser.next_token(), Token::make_word("version", None));
8071            parser.prev_token();
8072            assert_eq!(parser.peek_token(), Token::make_word("version", None));
8073            assert_eq!(parser.next_token(), Token::make_word("version", None));
8074            assert_eq!(parser.peek_token(), Token::EOF);
8075            parser.prev_token();
8076            assert_eq!(parser.next_token(), Token::make_word("version", None));
8077            assert_eq!(parser.next_token(), Token::EOF);
8078            assert_eq!(parser.next_token(), Token::EOF);
8079            parser.prev_token();
8080        });
8081    }
8082
8083    #[cfg(test)]
8084    mod test_parse_data_type {
8085        use crate::ast::{
8086            CharLengthUnits, CharacterLength, DataType, ExactNumberInfo, ObjectName, TimezoneInfo,
8087        };
8088        use crate::dialect::{AnsiDialect, GenericDialect};
8089        use crate::test_utils::TestedDialects;
8090
8091        macro_rules! test_parse_data_type {
8092            ($dialect:expr, $input:expr, $expected_type:expr $(,)?) => {{
8093                $dialect.run_parser_method(&*$input, |parser| {
8094                    let data_type = parser.parse_data_type().unwrap();
8095                    assert_eq!($expected_type, data_type);
8096                    assert_eq!($input.to_string(), data_type.to_string());
8097                });
8098            }};
8099        }
8100
8101        #[test]
8102        fn test_ansii_character_string_types() {
8103            // Character string types: <https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#character-string-type>
8104            let dialect = TestedDialects {
8105                dialects: vec![Box::new(GenericDialect {}), Box::new(AnsiDialect {})],
8106                options: None,
8107            };
8108
8109            test_parse_data_type!(dialect, "CHARACTER", DataType::Character(None));
8110
8111            test_parse_data_type!(
8112                dialect,
8113                "CHARACTER(20)",
8114                DataType::Character(Some(CharacterLength {
8115                    length: 20,
8116                    unit: None
8117                }))
8118            );
8119
8120            test_parse_data_type!(
8121                dialect,
8122                "CHARACTER(20 CHARACTERS)",
8123                DataType::Character(Some(CharacterLength {
8124                    length: 20,
8125                    unit: Some(CharLengthUnits::Characters)
8126                }))
8127            );
8128
8129            test_parse_data_type!(
8130                dialect,
8131                "CHARACTER(20 OCTETS)",
8132                DataType::Character(Some(CharacterLength {
8133                    length: 20,
8134                    unit: Some(CharLengthUnits::Octets)
8135                }))
8136            );
8137
8138            test_parse_data_type!(dialect, "CHAR", DataType::Char(None));
8139
8140            test_parse_data_type!(
8141                dialect,
8142                "CHAR(20)",
8143                DataType::Char(Some(CharacterLength {
8144                    length: 20,
8145                    unit: None
8146                }))
8147            );
8148
8149            test_parse_data_type!(
8150                dialect,
8151                "CHAR(20 CHARACTERS)",
8152                DataType::Char(Some(CharacterLength {
8153                    length: 20,
8154                    unit: Some(CharLengthUnits::Characters)
8155                }))
8156            );
8157
8158            test_parse_data_type!(
8159                dialect,
8160                "CHAR(20 OCTETS)",
8161                DataType::Char(Some(CharacterLength {
8162                    length: 20,
8163                    unit: Some(CharLengthUnits::Octets)
8164                }))
8165            );
8166
8167            test_parse_data_type!(
8168                dialect,
8169                "CHARACTER VARYING(20)",
8170                DataType::CharacterVarying(Some(CharacterLength {
8171                    length: 20,
8172                    unit: None
8173                }))
8174            );
8175
8176            test_parse_data_type!(
8177                dialect,
8178                "CHARACTER VARYING(20 CHARACTERS)",
8179                DataType::CharacterVarying(Some(CharacterLength {
8180                    length: 20,
8181                    unit: Some(CharLengthUnits::Characters)
8182                }))
8183            );
8184
8185            test_parse_data_type!(
8186                dialect,
8187                "CHARACTER VARYING(20 OCTETS)",
8188                DataType::CharacterVarying(Some(CharacterLength {
8189                    length: 20,
8190                    unit: Some(CharLengthUnits::Octets)
8191                }))
8192            );
8193
8194            test_parse_data_type!(
8195                dialect,
8196                "CHAR VARYING(20)",
8197                DataType::CharVarying(Some(CharacterLength {
8198                    length: 20,
8199                    unit: None
8200                }))
8201            );
8202
8203            test_parse_data_type!(
8204                dialect,
8205                "CHAR VARYING(20 CHARACTERS)",
8206                DataType::CharVarying(Some(CharacterLength {
8207                    length: 20,
8208                    unit: Some(CharLengthUnits::Characters)
8209                }))
8210            );
8211
8212            test_parse_data_type!(
8213                dialect,
8214                "CHAR VARYING(20 OCTETS)",
8215                DataType::CharVarying(Some(CharacterLength {
8216                    length: 20,
8217                    unit: Some(CharLengthUnits::Octets)
8218                }))
8219            );
8220
8221            test_parse_data_type!(
8222                dialect,
8223                "VARCHAR(20)",
8224                DataType::Varchar(Some(CharacterLength {
8225                    length: 20,
8226                    unit: None
8227                }))
8228            );
8229        }
8230
8231        #[test]
8232        fn test_ansii_character_large_object_types() {
8233            // Character large object types: <https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#character-large-object-length>
8234            let dialect = TestedDialects {
8235                dialects: vec![Box::new(GenericDialect {}), Box::new(AnsiDialect {})],
8236                options: None,
8237            };
8238
8239            test_parse_data_type!(
8240                dialect,
8241                "CHARACTER LARGE OBJECT",
8242                DataType::CharacterLargeObject(None)
8243            );
8244            test_parse_data_type!(
8245                dialect,
8246                "CHARACTER LARGE OBJECT(20)",
8247                DataType::CharacterLargeObject(Some(20))
8248            );
8249
8250            test_parse_data_type!(
8251                dialect,
8252                "CHAR LARGE OBJECT",
8253                DataType::CharLargeObject(None)
8254            );
8255            test_parse_data_type!(
8256                dialect,
8257                "CHAR LARGE OBJECT(20)",
8258                DataType::CharLargeObject(Some(20))
8259            );
8260
8261            test_parse_data_type!(dialect, "CLOB", DataType::Clob(None));
8262            test_parse_data_type!(dialect, "CLOB(20)", DataType::Clob(Some(20)));
8263        }
8264
8265        #[test]
8266        fn test_parse_custom_types() {
8267            let dialect = TestedDialects {
8268                dialects: vec![Box::new(GenericDialect {}), Box::new(AnsiDialect {})],
8269                options: None,
8270            };
8271            test_parse_data_type!(
8272                dialect,
8273                "GEOMETRY",
8274                DataType::Custom(ObjectName(vec!["GEOMETRY".into()]), vec![])
8275            );
8276
8277            test_parse_data_type!(
8278                dialect,
8279                "GEOMETRY(POINT)",
8280                DataType::Custom(
8281                    ObjectName(vec!["GEOMETRY".into()]),
8282                    vec!["POINT".to_string()]
8283                )
8284            );
8285
8286            test_parse_data_type!(
8287                dialect,
8288                "GEOMETRY(POINT, 4326)",
8289                DataType::Custom(
8290                    ObjectName(vec!["GEOMETRY".into()]),
8291                    vec!["POINT".to_string(), "4326".to_string()]
8292                )
8293            );
8294        }
8295
8296        #[test]
8297        fn test_ansii_exact_numeric_types() {
8298            // Exact numeric types: <https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#exact-numeric-type>
8299            let dialect = TestedDialects {
8300                dialects: vec![Box::new(GenericDialect {}), Box::new(AnsiDialect {})],
8301                options: None,
8302            };
8303
8304            test_parse_data_type!(dialect, "NUMERIC", DataType::Numeric(ExactNumberInfo::None));
8305
8306            test_parse_data_type!(
8307                dialect,
8308                "NUMERIC(2)",
8309                DataType::Numeric(ExactNumberInfo::Precision(2))
8310            );
8311
8312            test_parse_data_type!(
8313                dialect,
8314                "NUMERIC(2,10)",
8315                DataType::Numeric(ExactNumberInfo::PrecisionAndScale(2, 10))
8316            );
8317
8318            test_parse_data_type!(dialect, "DECIMAL", DataType::Decimal(ExactNumberInfo::None));
8319
8320            test_parse_data_type!(
8321                dialect,
8322                "DECIMAL(2)",
8323                DataType::Decimal(ExactNumberInfo::Precision(2))
8324            );
8325
8326            test_parse_data_type!(
8327                dialect,
8328                "DECIMAL(2,10)",
8329                DataType::Decimal(ExactNumberInfo::PrecisionAndScale(2, 10))
8330            );
8331
8332            test_parse_data_type!(dialect, "DEC", DataType::Dec(ExactNumberInfo::None));
8333
8334            test_parse_data_type!(
8335                dialect,
8336                "DEC(2)",
8337                DataType::Dec(ExactNumberInfo::Precision(2))
8338            );
8339
8340            test_parse_data_type!(
8341                dialect,
8342                "DEC(2,10)",
8343                DataType::Dec(ExactNumberInfo::PrecisionAndScale(2, 10))
8344            );
8345        }
8346
8347        #[test]
8348        fn test_ansii_date_type() {
8349            // Datetime types: <https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#datetime-type>
8350            let dialect = TestedDialects {
8351                dialects: vec![Box::new(GenericDialect {}), Box::new(AnsiDialect {})],
8352                options: None,
8353            };
8354
8355            test_parse_data_type!(dialect, "DATE", DataType::Date);
8356
8357            test_parse_data_type!(dialect, "TIME", DataType::Time(None, TimezoneInfo::None));
8358
8359            test_parse_data_type!(
8360                dialect,
8361                "TIME(6)",
8362                DataType::Time(Some(6), TimezoneInfo::None)
8363            );
8364
8365            test_parse_data_type!(
8366                dialect,
8367                "TIME WITH TIME ZONE",
8368                DataType::Time(None, TimezoneInfo::WithTimeZone)
8369            );
8370
8371            test_parse_data_type!(
8372                dialect,
8373                "TIME(6) WITH TIME ZONE",
8374                DataType::Time(Some(6), TimezoneInfo::WithTimeZone)
8375            );
8376
8377            test_parse_data_type!(
8378                dialect,
8379                "TIME WITHOUT TIME ZONE",
8380                DataType::Time(None, TimezoneInfo::WithoutTimeZone)
8381            );
8382
8383            test_parse_data_type!(
8384                dialect,
8385                "TIME(6) WITHOUT TIME ZONE",
8386                DataType::Time(Some(6), TimezoneInfo::WithoutTimeZone)
8387            );
8388
8389            test_parse_data_type!(
8390                dialect,
8391                "TIMESTAMP",
8392                DataType::Timestamp(None, TimezoneInfo::None)
8393            );
8394
8395            test_parse_data_type!(
8396                dialect,
8397                "TIMESTAMP(22)",
8398                DataType::Timestamp(Some(22), TimezoneInfo::None)
8399            );
8400
8401            test_parse_data_type!(
8402                dialect,
8403                "TIMESTAMP(22) WITH TIME ZONE",
8404                DataType::Timestamp(Some(22), TimezoneInfo::WithTimeZone)
8405            );
8406
8407            test_parse_data_type!(
8408                dialect,
8409                "TIMESTAMP(33) WITHOUT TIME ZONE",
8410                DataType::Timestamp(Some(33), TimezoneInfo::WithoutTimeZone)
8411            );
8412        }
8413    }
8414
8415    #[test]
8416    fn test_parse_schema_name() {
8417        // The expected name should be identical as the input name, that's why I don't receive both
8418        macro_rules! test_parse_schema_name {
8419            ($input:expr, $expected_name:expr $(,)?) => {{
8420                all_dialects().run_parser_method(&*$input, |parser| {
8421                    let schema_name = parser.parse_schema_name().unwrap();
8422                    // Validate that the structure is the same as expected
8423                    assert_eq!(schema_name, $expected_name);
8424                    // Validate that the input and the expected structure serialization are the same
8425                    assert_eq!(schema_name.to_string(), $input.to_string());
8426                });
8427            }};
8428        }
8429
8430        let dummy_name = ObjectName(vec![Ident::new("dummy_name")]);
8431        let dummy_authorization = Ident::new("dummy_authorization");
8432
8433        test_parse_schema_name!(
8434            format!("{dummy_name}"),
8435            SchemaName::Simple(dummy_name.clone())
8436        );
8437
8438        test_parse_schema_name!(
8439            format!("AUTHORIZATION {dummy_authorization}"),
8440            SchemaName::UnnamedAuthorization(dummy_authorization.clone()),
8441        );
8442        test_parse_schema_name!(
8443            format!("{dummy_name} AUTHORIZATION {dummy_authorization}"),
8444            SchemaName::NamedAuthorization(dummy_name.clone(), dummy_authorization.clone()),
8445        );
8446    }
8447
8448    #[test]
8449    fn mysql_parse_index_table_constraint() {
8450        macro_rules! test_parse_table_constraint {
8451            ($dialect:expr, $input:expr, $expected:expr $(,)?) => {{
8452                $dialect.run_parser_method(&*$input, |parser| {
8453                    let constraint = parser.parse_optional_table_constraint().unwrap().unwrap();
8454                    // Validate that the structure is the same as expected
8455                    assert_eq!(constraint, $expected);
8456                    // Validate that the input and the expected structure serialization are the same
8457                    assert_eq!(constraint.to_string(), $input.to_string());
8458                });
8459            }};
8460        }
8461
8462        let dialect = TestedDialects {
8463            dialects: vec![Box::new(GenericDialect {}), Box::new(MySqlDialect {})],
8464            options: None,
8465        };
8466
8467        test_parse_table_constraint!(
8468            dialect,
8469            "INDEX (c1)",
8470            TableConstraint::Index {
8471                display_as_key: false,
8472                name: None,
8473                index_type: None,
8474                columns: vec![Ident::new("c1")],
8475            }
8476        );
8477
8478        test_parse_table_constraint!(
8479            dialect,
8480            "KEY (c1)",
8481            TableConstraint::Index {
8482                display_as_key: true,
8483                name: None,
8484                index_type: None,
8485                columns: vec![Ident::new("c1")],
8486            }
8487        );
8488
8489        test_parse_table_constraint!(
8490            dialect,
8491            "INDEX 'index' (c1, c2)",
8492            TableConstraint::Index {
8493                display_as_key: false,
8494                name: Some(Ident::with_quote('\'', "index")),
8495                index_type: None,
8496                columns: vec![Ident::new("c1"), Ident::new("c2")],
8497            }
8498        );
8499
8500        test_parse_table_constraint!(
8501            dialect,
8502            "INDEX USING BTREE (c1)",
8503            TableConstraint::Index {
8504                display_as_key: false,
8505                name: None,
8506                index_type: Some(IndexType::BTree),
8507                columns: vec![Ident::new("c1")],
8508            }
8509        );
8510
8511        test_parse_table_constraint!(
8512            dialect,
8513            "INDEX USING HASH (c1)",
8514            TableConstraint::Index {
8515                display_as_key: false,
8516                name: None,
8517                index_type: Some(IndexType::Hash),
8518                columns: vec![Ident::new("c1")],
8519            }
8520        );
8521
8522        test_parse_table_constraint!(
8523            dialect,
8524            "INDEX idx_name USING BTREE (c1)",
8525            TableConstraint::Index {
8526                display_as_key: false,
8527                name: Some(Ident::new("idx_name")),
8528                index_type: Some(IndexType::BTree),
8529                columns: vec![Ident::new("c1")],
8530            }
8531        );
8532
8533        test_parse_table_constraint!(
8534            dialect,
8535            "INDEX idx_name USING HASH (c1)",
8536            TableConstraint::Index {
8537                display_as_key: false,
8538                name: Some(Ident::new("idx_name")),
8539                index_type: Some(IndexType::Hash),
8540                columns: vec![Ident::new("c1")],
8541            }
8542        );
8543    }
8544
8545    #[test]
8546    fn test_tokenizer_error_loc() {
8547        let sql = "foo '";
8548        let ast = Parser::parse_sql(&GenericDialect, sql);
8549        assert_eq!(
8550            ast,
8551            Err(ParserError::TokenizerError(
8552                "Unterminated string literal at Line: 1, Column 5".to_string()
8553            ))
8554        );
8555    }
8556
8557    #[test]
8558    fn test_parser_error_loc() {
8559        let sql = "SELECT this is a syntax error";
8560        let ast = Parser::parse_sql(&GenericDialect, sql);
8561        assert_eq!(
8562            ast,
8563            Err(ParserError::ParserError(
8564                "Expected [NOT] NULL or TRUE|FALSE or [NOT] DISTINCT FROM after IS, found: a at Line: 1, Column 16"
8565                    .to_string()
8566            ))
8567        );
8568    }
8569
8570    #[test]
8571    fn test_nested_explain_error() {
8572        let sql = "EXPLAIN EXPLAIN SELECT 1";
8573        let ast = Parser::parse_sql(&GenericDialect, sql);
8574        assert_eq!(
8575            ast,
8576            Err(ParserError::ParserError(
8577                "Explain must be root of the plan".to_string()
8578            ))
8579        );
8580    }
8581
8582    #[test]
8583    fn test_parse_multipart_identifier_positive() {
8584        let dialect = TestedDialects {
8585            dialects: vec![Box::new(GenericDialect {})],
8586            options: None,
8587        };
8588
8589        // parse multipart with quotes
8590        let expected = vec![
8591            Ident {
8592                value: "CATALOG".to_string(),
8593                quote_style: None,
8594            },
8595            Ident {
8596                value: "F(o)o. \"bar".to_string(),
8597                quote_style: Some('"'),
8598            },
8599            Ident {
8600                value: "table".to_string(),
8601                quote_style: None,
8602            },
8603        ];
8604        dialect.run_parser_method(r#"CATALOG."F(o)o. ""bar".table"#, |parser| {
8605            let actual = parser.parse_multipart_identifier().unwrap();
8606            assert_eq!(expected, actual);
8607        });
8608
8609        // allow whitespace between ident parts
8610        let expected = vec![
8611            Ident {
8612                value: "CATALOG".to_string(),
8613                quote_style: None,
8614            },
8615            Ident {
8616                value: "table".to_string(),
8617                quote_style: None,
8618            },
8619        ];
8620        dialect.run_parser_method("CATALOG . table", |parser| {
8621            let actual = parser.parse_multipart_identifier().unwrap();
8622            assert_eq!(expected, actual);
8623        });
8624    }
8625
8626    #[test]
8627    fn test_parse_multipart_identifier_negative() {
8628        macro_rules! test_parse_multipart_identifier_error {
8629            ($input:expr, $expected_err:expr $(,)?) => {{
8630                all_dialects().run_parser_method(&*$input, |parser| {
8631                    let actual_err = parser.parse_multipart_identifier().unwrap_err();
8632                    assert_eq!(actual_err.to_string(), $expected_err);
8633                });
8634            }};
8635        }
8636
8637        test_parse_multipart_identifier_error!(
8638            "",
8639            "sql parser error: Empty input when parsing identifier",
8640        );
8641
8642        test_parse_multipart_identifier_error!(
8643            "*schema.table",
8644            "sql parser error: Unexpected token in identifier: *",
8645        );
8646
8647        test_parse_multipart_identifier_error!(
8648            "schema.table*",
8649            "sql parser error: Unexpected token in identifier: *",
8650        );
8651
8652        test_parse_multipart_identifier_error!(
8653            "schema.table.",
8654            "sql parser error: Trailing period in identifier",
8655        );
8656
8657        test_parse_multipart_identifier_error!(
8658            "schema.*",
8659            "sql parser error: Unexpected token following period in identifier: *",
8660        );
8661    }
8662
8663    #[test]
8664    fn test_mysql_partition_selection() {
8665        let sql = "SELECT * FROM employees PARTITION (p0, p2)";
8666        let expected = vec!["p0", "p2"];
8667
8668        let ast: Vec<Statement> = Parser::parse_sql(&MySqlDialect {}, sql).unwrap();
8669        assert_eq!(ast.len(), 1);
8670        if let Statement::Query(v) = &ast[0] {
8671            if let SetExpr::Select(select) = &*v.body {
8672                assert_eq!(select.from.len(), 1);
8673                let from: &TableWithJoins = &select.from[0];
8674                let table_factor = &from.relation;
8675                if let TableFactor::Table { partitions, .. } = table_factor {
8676                    let actual: Vec<&str> = partitions
8677                        .iter()
8678                        .map(|ident| ident.value.as_str())
8679                        .collect();
8680                    assert_eq!(expected, actual);
8681                }
8682            }
8683        } else {
8684            panic!("fail to parse mysql partition selection");
8685        }
8686    }
8687}