sqlx_models_parser/
parser.rs

1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License at
4//
5// http://www.apache.org/licenses/LICENSE-2.0
6//
7// Unless required by applicable law or agreed to in writing, software
8// distributed under the License is distributed on an "AS IS" BASIS,
9// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10// See the License for the specific language governing permissions and
11// limitations under the License.
12
13//! SQL Parser
14
15#[cfg(not(feature = "std"))]
16use alloc::{
17    boxed::Box,
18    format,
19    string::{String, ToString},
20    vec,
21    vec::Vec,
22};
23use core::fmt;
24
25use log::debug;
26
27use crate::ast::*;
28use crate::dialect::keywords::Keyword;
29use crate::dialect::*;
30use crate::tokenizer::*;
31
32#[derive(Debug, Clone, PartialEq)]
33pub enum ParserError {
34    TokenizerError(String),
35    ParserError(String),
36}
37
38// Use `Parser::expected` instead, if possible
39macro_rules! parser_err {
40    ($MSG:expr) => {
41        Err(ParserError::ParserError($MSG.to_string()))
42    };
43}
44
45// Returns a successful result if the optional expression is some
46macro_rules! return_ok_if_some {
47    ($e:expr) => {{
48        if let Some(v) = $e {
49            return Ok(v);
50        }
51    }};
52}
53
54#[derive(PartialEq)]
55pub enum IsOptional {
56    Optional,
57    Mandatory,
58}
59
60use IsOptional::*;
61
62pub enum IsLateral {
63    Lateral,
64    NotLateral,
65}
66
67use IsLateral::*;
68
69impl From<TokenizerError> for ParserError {
70    fn from(e: TokenizerError) -> Self {
71        ParserError::TokenizerError(format!(
72            "{} at Line: {}, Column {}",
73            e.message, e.line, e.col
74        ))
75    }
76}
77
78impl fmt::Display for ParserError {
79    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
80        write!(
81            f,
82            "sql parser error: {}",
83            match self {
84                ParserError::TokenizerError(s) => s,
85                ParserError::ParserError(s) => s,
86            }
87        )
88    }
89}
90
91#[cfg(feature = "std")]
92impl std::error::Error for ParserError {}
93
94pub struct Parser<'a> {
95    tokens: Vec<Token>,
96    /// The index of the first unprocessed token in `self.tokens`
97    index: usize,
98    dialect: &'a dyn Dialect,
99}
100
101impl<'a> Parser<'a> {
102    /// Parse the specified tokens
103    pub fn new(tokens: Vec<Token>, dialect: &'a dyn Dialect) -> Self {
104        Parser {
105            tokens,
106            index: 0,
107            dialect,
108        }
109    }
110
111    /// Parse a SQL statement and produce an Abstract Syntax Tree (AST)
112    pub fn parse_sql(dialect: &dyn Dialect, sql: &str) -> Result<Vec<Statement>, ParserError> {
113        let mut tokenizer = Tokenizer::new(dialect, sql);
114        let tokens = tokenizer.tokenize()?;
115        let mut parser = Parser::new(tokens, dialect);
116        let mut stmts = Vec::new();
117        let mut expecting_statement_delimiter = false;
118        debug!("Parsing sql '{}'...", sql);
119        loop {
120            // ignore empty statements (between successive statement delimiters)
121            while parser.consume_token(&Token::SemiColon) {
122                expecting_statement_delimiter = false;
123            }
124
125            if parser.peek_token() == Token::EOF {
126                break;
127            }
128            if expecting_statement_delimiter {
129                return parser.expected("end of statement", parser.peek_token());
130            }
131
132            let statement = parser.parse_statement()?;
133            stmts.push(statement);
134            expecting_statement_delimiter = true;
135        }
136        Ok(stmts)
137    }
138
139    /// Parse a single top-level statement (such as SELECT, INSERT, CREATE, etc.),
140    /// stopping before the statement separator, if any.
141    pub fn parse_statement(&mut self) -> Result<Statement, ParserError> {
142        use Statement::*;
143        match self.next_token() {
144            Token::Word(w) => match w.keyword {
145                Keyword::EXPLAIN => Ok(Explain(self.parse_explain()?)),
146                Keyword::ANALYZE => Ok(Analyze(self.parse_analyze()?)),
147                Keyword::SELECT | Keyword::WITH | Keyword::VALUES => {
148                    self.prev_token();
149                    Ok(Statement::Query(Box::new(self.parse_query()?)))
150                }
151                Keyword::TRUNCATE => Ok(Truncate(self.parse_truncate()?)),
152                Keyword::MSCK => Ok(Msck(self.parse_msck()?)),
153                Keyword::CREATE => Ok(self.parse_create()?),
154                Keyword::DROP => Ok(Drop(self.parse_drop()?)),
155                Keyword::DELETE => Ok(Delete(self.parse_delete()?)),
156                Keyword::INSERT => Ok(self.parse_insert()?),
157                Keyword::UPDATE => Ok(Update(self.parse_update()?)),
158                Keyword::ALTER => Ok(AlterTable(self.parse_alter()?)),
159                Keyword::COPY => Ok(Copy(self.parse_copy()?)),
160                Keyword::SET => Ok(self.parse_set()?),
161                Keyword::SHOW => Ok(self.parse_show()?),
162                Keyword::START => Ok(StartTransaction(self.parse_start_transaction()?)),
163                // `BEGIN` is a nonstandard but common alias for the
164                // standard `START TRANSACTION` statement. It is supported
165                // by at least PostgreSQL and MySQL.
166                Keyword::BEGIN => Ok(StartTransaction(self.parse_begin()?)),
167                Keyword::COMMIT => Ok(Commit(self.parse_commit()?)),
168                Keyword::ROLLBACK => Ok(Rollback(self.parse_rollback()?)),
169                Keyword::ASSERT => Ok(Assert(self.parse_assert()?)),
170                // `PREPARE`, `EXECUTE` and `DEALLOCATE` are Postgres-specific
171                // syntaxes. They are used for Postgres prepared statement.
172                Keyword::DEALLOCATE => Ok(Deallocate(self.parse_deallocate()?)),
173                Keyword::EXECUTE => Ok(Execute(self.parse_execute()?)),
174                Keyword::PREPARE => Ok(Prepare(self.parse_prepare()?)),
175                Keyword::REPLACE if dialect_of!(self is SQLiteDialect ) => {
176                    self.prev_token();
177                    Ok(self.parse_insert()?)
178                }
179                _ => self.expected("an SQL statement", Token::Word(w)),
180            },
181            Token::LParen => {
182                self.prev_token();
183                Ok(Statement::Query(Box::new(self.parse_query()?)))
184            }
185            unexpected => self.expected("an SQL statement", unexpected),
186        }
187    }
188
189    pub fn parse_msck(&mut self) -> Result<Msck, ParserError> {
190        let repair = self.parse_keyword(Keyword::REPAIR);
191        self.expect_keyword(Keyword::TABLE)?;
192        let table_name = self.parse_object_name()?;
193        let partition_action = self
194            .maybe_parse(|parser| {
195                let pa = match parser.parse_one_of_keywords(&[
196                    Keyword::ADD,
197                    Keyword::DROP,
198                    Keyword::SYNC,
199                ]) {
200                    Some(Keyword::ADD) => Some(AddDropSync::ADD),
201                    Some(Keyword::DROP) => Some(AddDropSync::DROP),
202                    Some(Keyword::SYNC) => Some(AddDropSync::SYNC),
203                    _ => None,
204                };
205                parser.expect_keyword(Keyword::PARTITIONS)?;
206                Ok(pa)
207            })
208            .unwrap_or_default();
209        Ok(Msck {
210            repair,
211            table_name,
212            partition_action,
213        })
214    }
215
216    pub fn parse_truncate(&mut self) -> Result<Truncate, ParserError> {
217        self.expect_keyword(Keyword::TABLE)?;
218        let table_name = self.parse_object_name()?;
219        let mut partitions = None;
220        if self.parse_keyword(Keyword::PARTITION) {
221            self.expect_token(&Token::LParen)?;
222            partitions = Some(self.parse_comma_separated(Parser::parse_expr)?);
223            self.expect_token(&Token::RParen)?;
224        }
225        Ok(Truncate {
226            table_name,
227            partitions,
228        })
229    }
230
231    pub fn parse_analyze(&mut self) -> Result<Analyze, ParserError> {
232        self.expect_keyword(Keyword::TABLE)?;
233        let table_name = self.parse_object_name()?;
234        let mut for_columns = false;
235        let mut cache_metadata = false;
236        let mut noscan = false;
237        let mut partitions = None;
238        let mut compute_statistics = false;
239        let mut columns = vec![];
240        loop {
241            match self.parse_one_of_keywords(&[
242                Keyword::PARTITION,
243                Keyword::FOR,
244                Keyword::CACHE,
245                Keyword::NOSCAN,
246                Keyword::COMPUTE,
247            ]) {
248                Some(Keyword::PARTITION) => {
249                    self.expect_token(&Token::LParen)?;
250                    partitions = Some(self.parse_comma_separated(Parser::parse_expr)?);
251                    self.expect_token(&Token::RParen)?;
252                }
253                Some(Keyword::NOSCAN) => noscan = true,
254                Some(Keyword::FOR) => {
255                    self.expect_keyword(Keyword::COLUMNS)?;
256
257                    columns = self
258                        .maybe_parse(|parser| {
259                            parser.parse_comma_separated(Parser::parse_identifier)
260                        })
261                        .unwrap_or_default();
262                    for_columns = true
263                }
264                Some(Keyword::CACHE) => {
265                    self.expect_keyword(Keyword::METADATA)?;
266                    cache_metadata = true
267                }
268                Some(Keyword::COMPUTE) => {
269                    self.expect_keyword(Keyword::STATISTICS)?;
270                    compute_statistics = true
271                }
272                _ => break,
273            }
274        }
275
276        Ok(Analyze {
277            table_name,
278            for_columns,
279            columns,
280            partitions,
281            cache_metadata,
282            noscan,
283            compute_statistics,
284        })
285    }
286
287    /// Parse a new expression
288    pub fn parse_expr(&mut self) -> Result<Expr, ParserError> {
289        self.parse_subexpr(0)
290    }
291
292    /// Parse tokens until the precedence changes
293    pub fn parse_subexpr(&mut self, precedence: u8) -> Result<Expr, ParserError> {
294        debug!("parsing expr");
295        let mut expr = self.parse_prefix()?;
296        debug!("prefix: {:?}", expr);
297        loop {
298            let next_precedence = self.get_next_precedence()?;
299            debug!("next precedence: {:?}", next_precedence);
300
301            if precedence >= next_precedence {
302                break;
303            }
304
305            expr = self.parse_infix(expr, next_precedence)?;
306        }
307        Ok(expr)
308    }
309
310    pub fn parse_assert(&mut self) -> Result<Assert, ParserError> {
311        let condition = self.parse_expr()?;
312        let message = if self.parse_keyword(Keyword::AS) {
313            Some(self.parse_expr()?)
314        } else {
315            None
316        };
317
318        Ok(Assert { condition, message })
319    }
320
321    /// Parse an expression prefix
322    pub fn parse_prefix(&mut self) -> Result<Expr, ParserError> {
323        // PostgreSQL allows any string literal to be preceded by a type name, indicating that the
324        // string literal represents a literal of that type. Some examples:
325        //
326        //      DATE '2020-05-20'
327        //      TIMESTAMP WITH TIME ZONE '2020-05-20 7:43:54'
328        //      BOOL 'true'
329        //
330        // The first two are standard SQL, while the latter is a PostgreSQL extension. Complicating
331        // matters is the fact that INTERVAL string literals may optionally be followed by special
332        // keywords, e.g.:
333        //
334        //      INTERVAL '7' DAY
335        //
336        // Note also that naively `SELECT date` looks like a syntax error because the `date` type
337        // name is not followed by a string literal, but in fact in PostgreSQL it is a valid
338        // expression that should parse as the column name "date".
339        return_ok_if_some!(self.maybe_parse(|parser| {
340            match parser.parse_data_type()? {
341                DataType::Interval => parser.parse_literal_interval(),
342                // PosgreSQL allows almost any identifier to be used as custom data type name,
343                // and we support that in `parse_data_type()`. But unlike Postgres we don't
344                // have a list of globally reserved keywords (since they vary across dialects),
345                // so given `NOT 'a' LIKE 'b'`, we'd accept `NOT` as a possible custom data type
346                // name, resulting in `NOT 'a'` being recognized as a `TypedString` instead of
347                // an unary negation `NOT ('a' LIKE 'b')`. To solve this, we don't accept the
348                // `type 'string'` syntax for the custom data types at all.
349                DataType::Custom(..) => parser_err!("dummy"),
350                data_type => Ok(Expr::TypedString(TypedString {
351                    data_type,
352                    value: parser.parse_literal_string()?,
353                })),
354            }
355        }));
356
357        let expr = match self.next_token() {
358            Token::Word(w) => match w.keyword {
359                Keyword::TRUE | Keyword::FALSE | Keyword::NULL => {
360                    self.prev_token();
361                    Ok(Expr::Value(self.parse_value()?))
362                }
363                Keyword::CASE => self.parse_case_expr(),
364                Keyword::CAST => self.parse_cast_expr(),
365                Keyword::TRY_CAST => self.parse_try_cast_expr(),
366                Keyword::EXISTS => self.parse_exists_expr(),
367                Keyword::EXTRACT => self.parse_extract_expr(),
368                Keyword::SUBSTRING => self.parse_substring_expr(),
369                Keyword::TRIM => self.parse_trim_expr(),
370                Keyword::INTERVAL => self.parse_literal_interval(),
371                Keyword::LISTAGG => self.parse_listagg_expr(),
372                Keyword::NOT => Ok(Expr::UnaryOp(UnaryOp {
373                    op: UnaryOperator::Not,
374                    expr: Box::new(self.parse_subexpr(Self::UNARY_NOT_PREC)?),
375                })),
376                // Here `w` is a word, check if it's a part of a multi-part
377                // identifier, a function call, or a simple identifier:
378                _ => match self.peek_token() {
379                    Token::LParen | Token::Period => {
380                        let mut id_parts: Vec<Ident> = vec![w.to_ident()];
381                        let mut ends_with_wildcard = false;
382                        while self.consume_token(&Token::Period) {
383                            match self.next_token() {
384                                Token::Word(w) => id_parts.push(w.to_ident()),
385                                Token::Mult => {
386                                    ends_with_wildcard = true;
387                                    break;
388                                }
389                                unexpected => {
390                                    return self
391                                        .expected("an identifier or a '*' after '.'", unexpected);
392                                }
393                            }
394                        }
395                        if ends_with_wildcard {
396                            Ok(Expr::QualifiedWildcard(id_parts))
397                        } else if self.consume_token(&Token::LParen) {
398                            self.prev_token();
399                            self.parse_function(ObjectName(id_parts))
400                        } else {
401                            Ok(Expr::CompoundIdentifier(id_parts))
402                        }
403                    }
404                    _ => Ok(Expr::Identifier(w.to_ident())),
405                },
406            }, // End of Token::Word
407            Token::Mult => Ok(Expr::Wildcard),
408            tok @ Token::Minus | tok @ Token::Plus => {
409                let op = if tok == Token::Plus {
410                    UnaryOperator::Plus
411                } else {
412                    UnaryOperator::Minus
413                };
414                Ok(Expr::UnaryOp(UnaryOp {
415                    op,
416                    expr: Box::new(self.parse_subexpr(Self::PLUS_MINUS_PREC)?),
417                }))
418            }
419            tok @ Token::DoubleExclamationMark
420            | tok @ Token::PGSquareRoot
421            | tok @ Token::PGCubeRoot
422            | tok @ Token::AtSign
423            | tok @ Token::Tilde
424                if dialect_of!(self is PostgreSqlDialect) =>
425            {
426                let op = match tok {
427                    Token::DoubleExclamationMark => UnaryOperator::PGPrefixFactorial,
428                    Token::PGSquareRoot => UnaryOperator::PGSquareRoot,
429                    Token::PGCubeRoot => UnaryOperator::PGCubeRoot,
430                    Token::AtSign => UnaryOperator::PGAbs,
431                    Token::Tilde => UnaryOperator::PGBitwiseNot,
432                    _ => unreachable!(),
433                };
434                Ok(Expr::UnaryOp(UnaryOp {
435                    op,
436                    expr: Box::new(self.parse_subexpr(Self::PLUS_MINUS_PREC)?),
437                }))
438            }
439            Token::Number(_, _)
440            | Token::SingleQuotedString(_)
441            | Token::NationalStringLiteral(_)
442            | Token::HexStringLiteral(_) => {
443                self.prev_token();
444                Ok(Expr::Value(self.parse_value()?))
445            }
446
447            Token::LParen => {
448                let expr =
449                    if self.parse_keyword(Keyword::SELECT) || self.parse_keyword(Keyword::WITH) {
450                        self.prev_token();
451                        Expr::Subquery(Box::new(self.parse_query()?))
452                    } else {
453                        Expr::Nested(Box::new(self.parse_expr()?))
454                    };
455                self.expect_token(&Token::RParen)?;
456                Ok(expr)
457            }
458            unexpected => self.expected("an expression:", unexpected),
459        }?;
460
461        if self.parse_keyword(Keyword::COLLATE) {
462            Ok(Expr::Collate(Collate {
463                expr: Box::new(expr),
464                collation: self.parse_object_name()?,
465            }))
466        } else {
467            Ok(expr)
468        }
469    }
470
471    pub fn parse_function(&mut self, name: ObjectName) -> Result<Expr, ParserError> {
472        self.expect_token(&Token::LParen)?;
473        let distinct = self.parse_all_or_distinct()?;
474        let args = self.parse_optional_args()?;
475        let over = if self.parse_keyword(Keyword::OVER) {
476            // TBD: support window names (`OVER mywin`) in place of inline specification
477            self.expect_token(&Token::LParen)?;
478            let partition_by = if self.parse_keywords(&[Keyword::PARTITION, Keyword::BY]) {
479                // a list of possibly-qualified column names
480                self.parse_comma_separated(Parser::parse_expr)?
481            } else {
482                vec![]
483            };
484            let order_by = if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
485                self.parse_comma_separated(Parser::parse_order_by_expr)?
486            } else {
487                vec![]
488            };
489            let window_frame = if !self.consume_token(&Token::RParen) {
490                let window_frame = self.parse_window_frame()?;
491                self.expect_token(&Token::RParen)?;
492                Some(window_frame)
493            } else {
494                None
495            };
496
497            Some(WindowSpec {
498                partition_by,
499                order_by,
500                window_frame,
501            })
502        } else {
503            None
504        };
505
506        Ok(Expr::Function(Function {
507            name,
508            args,
509            over,
510            distinct,
511        }))
512    }
513
514    pub fn parse_window_frame_units(&mut self) -> Result<WindowFrameUnits, ParserError> {
515        match self.next_token() {
516            Token::Word(w) => match w.keyword {
517                Keyword::ROWS => Ok(WindowFrameUnits::Rows),
518                Keyword::RANGE => Ok(WindowFrameUnits::Range),
519                Keyword::GROUPS => Ok(WindowFrameUnits::Groups),
520                _ => self.expected("ROWS, RANGE, GROUPS", Token::Word(w))?,
521            },
522            unexpected => self.expected("ROWS, RANGE, GROUPS", unexpected),
523        }
524    }
525
526    pub fn parse_window_frame(&mut self) -> Result<WindowFrame, ParserError> {
527        let units = self.parse_window_frame_units()?;
528        let (start_bound, end_bound) = if self.parse_keyword(Keyword::BETWEEN) {
529            let start_bound = self.parse_window_frame_bound()?;
530            self.expect_keyword(Keyword::AND)?;
531            let end_bound = Some(self.parse_window_frame_bound()?);
532            (start_bound, end_bound)
533        } else {
534            (self.parse_window_frame_bound()?, None)
535        };
536        Ok(WindowFrame {
537            units,
538            start_bound,
539            end_bound,
540        })
541    }
542
543    /// Parse `CURRENT ROW` or `{ <positive number> | UNBOUNDED } { PRECEDING | FOLLOWING }`
544    pub fn parse_window_frame_bound(&mut self) -> Result<WindowFrameBound, ParserError> {
545        if self.parse_keywords(&[Keyword::CURRENT, Keyword::ROW]) {
546            Ok(WindowFrameBound::CurrentRow)
547        } else {
548            let rows = if self.parse_keyword(Keyword::UNBOUNDED) {
549                None
550            } else {
551                Some(self.parse_literal_uint()?)
552            };
553            if self.parse_keyword(Keyword::PRECEDING) {
554                Ok(WindowFrameBound::Preceding(rows))
555            } else if self.parse_keyword(Keyword::FOLLOWING) {
556                Ok(WindowFrameBound::Following(rows))
557            } else {
558                self.expected("PRECEDING or FOLLOWING", self.peek_token())
559            }
560        }
561    }
562
563    pub fn parse_case_expr(&mut self) -> Result<Expr, ParserError> {
564        let mut operand = None;
565        if !self.parse_keyword(Keyword::WHEN) {
566            operand = Some(Box::new(self.parse_expr()?));
567            self.expect_keyword(Keyword::WHEN)?;
568        }
569        let mut conditions = vec![];
570        let mut results = vec![];
571        loop {
572            conditions.push(self.parse_expr()?);
573            self.expect_keyword(Keyword::THEN)?;
574            results.push(self.parse_expr()?);
575            if !self.parse_keyword(Keyword::WHEN) {
576                break;
577            }
578        }
579        let else_result = if self.parse_keyword(Keyword::ELSE) {
580            Some(Box::new(self.parse_expr()?))
581        } else {
582            None
583        };
584        self.expect_keyword(Keyword::END)?;
585        Ok(Expr::Case(Case {
586            operand,
587            conditions,
588            results,
589            else_result,
590        }))
591    }
592
593    /// Parse a SQL CAST function e.g. `CAST(expr AS FLOAT)`
594    pub fn parse_cast_expr(&mut self) -> Result<Expr, ParserError> {
595        self.expect_token(&Token::LParen)?;
596        let expr = self.parse_expr()?;
597        self.expect_keyword(Keyword::AS)?;
598        let data_type = self.parse_data_type()?;
599        self.expect_token(&Token::RParen)?;
600        Ok(Expr::Cast(Cast {
601            expr: Box::new(expr),
602            data_type,
603        }))
604    }
605
606    /// Parse a SQL TRY_CAST function e.g. `TRY_CAST(expr AS FLOAT)`
607    pub fn parse_try_cast_expr(&mut self) -> Result<Expr, ParserError> {
608        self.expect_token(&Token::LParen)?;
609        let expr = self.parse_expr()?;
610        self.expect_keyword(Keyword::AS)?;
611        let data_type = self.parse_data_type()?;
612        self.expect_token(&Token::RParen)?;
613        Ok(Expr::TryCast(TryCast {
614            expr: Box::new(expr),
615            data_type,
616        }))
617    }
618
619    /// Parse a SQL EXISTS expression e.g. `WHERE EXISTS(SELECT ...)`.
620    pub fn parse_exists_expr(&mut self) -> Result<Expr, ParserError> {
621        self.expect_token(&Token::LParen)?;
622        let exists_node = Expr::Exists(Box::new(self.parse_query()?));
623        self.expect_token(&Token::RParen)?;
624        Ok(exists_node)
625    }
626
627    pub fn parse_extract_expr(&mut self) -> Result<Expr, ParserError> {
628        self.expect_token(&Token::LParen)?;
629        let field = self.parse_date_time_field()?;
630        self.expect_keyword(Keyword::FROM)?;
631        let expr = self.parse_expr()?;
632        self.expect_token(&Token::RParen)?;
633        Ok(Expr::Extract(Extract {
634            field,
635            expr: Box::new(expr),
636        }))
637    }
638
639    pub fn parse_substring_expr(&mut self) -> Result<Expr, ParserError> {
640        // PARSE SUBSTRING (EXPR [FROM 1] [FOR 3])
641        self.expect_token(&Token::LParen)?;
642        let expr = self.parse_expr()?;
643        let mut from_expr = None;
644        let mut to_expr = None;
645        if self.parse_keyword(Keyword::FROM) {
646            from_expr = Some(self.parse_expr()?);
647        }
648        if self.parse_keyword(Keyword::FOR) {
649            to_expr = Some(self.parse_expr()?);
650        }
651        self.expect_token(&Token::RParen)?;
652
653        Ok(Expr::Substring(Substring {
654            expr: Box::new(expr),
655            substring_from: from_expr.map(Box::new),
656            substring_for: to_expr.map(Box::new),
657        }))
658    }
659
660    /// TRIM (WHERE 'text' FROM 'text')\
661    /// TRIM ('text')
662    pub fn parse_trim_expr(&mut self) -> Result<Expr, ParserError> {
663        self.expect_token(&Token::LParen)?;
664        let mut where_expr = None;
665        if let Token::Word(word) = self.peek_token() {
666            if [Keyword::BOTH, Keyword::LEADING, Keyword::TRAILING]
667                .iter()
668                .any(|d| word.keyword == *d)
669            {
670                let trim_where = self.parse_trim_where()?;
671                let sub_expr = self.parse_expr()?;
672                self.expect_keyword(Keyword::FROM)?;
673                where_expr = Some((trim_where, Box::new(sub_expr)));
674            }
675        }
676        let expr = self.parse_expr()?;
677        self.expect_token(&Token::RParen)?;
678
679        Ok(Expr::Trim(Trim {
680            expr: Box::new(expr),
681            trim_where: where_expr,
682        }))
683    }
684
685    pub fn parse_trim_where(&mut self) -> Result<TrimWhereField, ParserError> {
686        match self.next_token() {
687            Token::Word(w) => match w.keyword {
688                Keyword::BOTH => Ok(TrimWhereField::Both),
689                Keyword::LEADING => Ok(TrimWhereField::Leading),
690                Keyword::TRAILING => Ok(TrimWhereField::Trailing),
691                _ => self.expected("trim_where field", Token::Word(w))?,
692            },
693            unexpected => self.expected("trim_where field", unexpected),
694        }
695    }
696
697    /// Parse a SQL LISTAGG expression, e.g. `LISTAGG(...) WITHIN GROUP (ORDER BY ...)`.
698    pub fn parse_listagg_expr(&mut self) -> Result<Expr, ParserError> {
699        self.expect_token(&Token::LParen)?;
700        let distinct = self.parse_all_or_distinct()?;
701        let expr = Box::new(self.parse_expr()?);
702        // While ANSI SQL would would require the separator, Redshift makes this optional. Here we
703        // choose to make the separator optional as this provides the more general implementation.
704        let separator = if self.consume_token(&Token::Comma) {
705            Some(Box::new(self.parse_expr()?))
706        } else {
707            None
708        };
709        let on_overflow = if self.parse_keywords(&[Keyword::ON, Keyword::OVERFLOW]) {
710            if self.parse_keyword(Keyword::ERROR) {
711                Some(ListAggOnOverflow::Error)
712            } else {
713                self.expect_keyword(Keyword::TRUNCATE)?;
714                let filler = match self.peek_token() {
715                    Token::Word(w)
716                        if w.keyword == Keyword::WITH || w.keyword == Keyword::WITHOUT =>
717                    {
718                        None
719                    }
720                    Token::SingleQuotedString(_)
721                    | Token::NationalStringLiteral(_)
722                    | Token::HexStringLiteral(_) => Some(Box::new(self.parse_expr()?)),
723                    unexpected => {
724                        self.expected("either filler, WITH, or WITHOUT in LISTAGG", unexpected)?
725                    }
726                };
727                let with_count = self.parse_keyword(Keyword::WITH);
728                if !with_count && !self.parse_keyword(Keyword::WITHOUT) {
729                    self.expected("either WITH or WITHOUT in LISTAGG", self.peek_token())?;
730                }
731                self.expect_keyword(Keyword::COUNT)?;
732                Some(ListAggOnOverflow::Truncate { filler, with_count })
733            }
734        } else {
735            None
736        };
737        self.expect_token(&Token::RParen)?;
738        // Once again ANSI SQL requires WITHIN GROUP, but Redshift does not. Again we choose the
739        // more general implementation.
740        let within_group = if self.parse_keywords(&[Keyword::WITHIN, Keyword::GROUP]) {
741            self.expect_token(&Token::LParen)?;
742            self.expect_keywords(&[Keyword::ORDER, Keyword::BY])?;
743            let order_by_expr = self.parse_comma_separated(Parser::parse_order_by_expr)?;
744            self.expect_token(&Token::RParen)?;
745            order_by_expr
746        } else {
747            vec![]
748        };
749        Ok(Expr::ListAgg(ListAgg {
750            distinct,
751            expr,
752            separator,
753            on_overflow,
754            within_group,
755        }))
756    }
757
758    // This function parses date/time fields for both the EXTRACT function-like
759    // operator and interval qualifiers. EXTRACT supports a wider set of
760    // date/time fields than interval qualifiers, so this function may need to
761    // be split in two.
762    pub fn parse_date_time_field(&mut self) -> Result<DateTimeField, ParserError> {
763        match self.next_token() {
764            Token::Word(w) => match w.keyword {
765                Keyword::YEAR => Ok(DateTimeField::Year),
766                Keyword::MONTH => Ok(DateTimeField::Month),
767                Keyword::DAY => Ok(DateTimeField::Day),
768                Keyword::HOUR => Ok(DateTimeField::Hour),
769                Keyword::MINUTE => Ok(DateTimeField::Minute),
770                Keyword::SECOND => Ok(DateTimeField::Second),
771                _ => self.expected("date/time field", Token::Word(w))?,
772            },
773            unexpected => self.expected("date/time field", unexpected),
774        }
775    }
776
777    /// Parse an INTERVAL literal.
778    ///
779    /// Some syntactically valid intervals:
780    ///
781    ///   1. `INTERVAL '1' DAY`
782    ///   2. `INTERVAL '1-1' YEAR TO MONTH`
783    ///   3. `INTERVAL '1' SECOND`
784    ///   4. `INTERVAL '1:1:1.1' HOUR (5) TO SECOND (5)`
785    ///   5. `INTERVAL '1.1' SECOND (2, 2)`
786    ///   6. `INTERVAL '1:1' HOUR (5) TO MINUTE (5)`
787    ///
788    /// Note that we do not currently attempt to parse the quoted value.
789    pub fn parse_literal_interval(&mut self) -> Result<Expr, ParserError> {
790        // The SQL standard allows an optional sign before the value string, but
791        // it is not clear if any implementations support that syntax, so we
792        // don't currently try to parse it. (The sign can instead be included
793        // inside the value string.)
794
795        // The first token in an interval is a string literal which specifies
796        // the duration of the interval.
797        let value = self.parse_literal_string()?;
798
799        // Following the string literal is a qualifier which indicates the units
800        // of the duration specified in the string literal.
801        //
802        // Note that PostgreSQL allows omitting the qualifier, so we provide
803        // this more general implemenation.
804        let leading_field = match self.peek_token() {
805            Token::Word(kw)
806                if [
807                    Keyword::YEAR,
808                    Keyword::MONTH,
809                    Keyword::DAY,
810                    Keyword::HOUR,
811                    Keyword::MINUTE,
812                    Keyword::SECOND,
813                ]
814                .iter()
815                .any(|d| kw.keyword == *d) =>
816            {
817                Some(self.parse_date_time_field()?)
818            }
819            _ => None,
820        };
821
822        let (leading_precision, last_field, fsec_precision) =
823            if leading_field == Some(DateTimeField::Second) {
824                // SQL mandates special syntax for `SECOND TO SECOND` literals.
825                // Instead of
826                //     `SECOND [(<leading precision>)] TO SECOND[(<fractional seconds precision>)]`
827                // one must use the special format:
828                //     `SECOND [( <leading precision> [ , <fractional seconds precision>] )]`
829                let last_field = None;
830                let (leading_precision, fsec_precision) = self.parse_optional_precision_scale()?;
831                (leading_precision, last_field, fsec_precision)
832            } else {
833                let leading_precision = self.parse_optional_precision()?;
834                if self.parse_keyword(Keyword::TO) {
835                    let last_field = Some(self.parse_date_time_field()?);
836                    let fsec_precision = if last_field == Some(DateTimeField::Second) {
837                        self.parse_optional_precision()?
838                    } else {
839                        None
840                    };
841                    (leading_precision, last_field, fsec_precision)
842                } else {
843                    (leading_precision, None, None)
844                }
845            };
846
847        Ok(Expr::Value(Value::Interval {
848            value,
849            leading_field,
850            leading_precision,
851            last_field,
852            fractional_seconds_precision: fsec_precision,
853        }))
854    }
855
856    /// Parse an operator following an expression
857    pub fn parse_infix(&mut self, expr: Expr, precedence: u8) -> Result<Expr, ParserError> {
858        let tok = self.next_token();
859        let regular_binary_operator = match &tok {
860            Token::Spaceship => Some(BinaryOperator::Spaceship),
861            Token::DoubleEq => Some(BinaryOperator::Eq),
862            Token::Eq => Some(BinaryOperator::Eq),
863            Token::Neq => Some(BinaryOperator::NotEq),
864            Token::Gt => Some(BinaryOperator::Gt),
865            Token::GtEq => Some(BinaryOperator::GtEq),
866            Token::Lt => Some(BinaryOperator::Lt),
867            Token::LtEq => Some(BinaryOperator::LtEq),
868            Token::Plus => Some(BinaryOperator::Plus),
869            Token::Minus => Some(BinaryOperator::Minus),
870            Token::Mult => Some(BinaryOperator::Multiply),
871            Token::Mod => Some(BinaryOperator::Modulo),
872            Token::StringConcat => Some(BinaryOperator::StringConcat),
873            Token::Pipe => Some(BinaryOperator::BitwiseOr),
874            Token::Caret => Some(BinaryOperator::BitwiseXor),
875            Token::Ampersand => Some(BinaryOperator::BitwiseAnd),
876            Token::Div => Some(BinaryOperator::Divide),
877            Token::ShiftLeft if dialect_of!(self is PostgreSqlDialect) => {
878                Some(BinaryOperator::PGBitwiseShiftLeft)
879            }
880            Token::ShiftRight if dialect_of!(self is PostgreSqlDialect) => {
881                Some(BinaryOperator::PGBitwiseShiftRight)
882            }
883            Token::Sharp if dialect_of!(self is PostgreSqlDialect) => {
884                Some(BinaryOperator::PGBitwiseXor)
885            }
886            Token::Tilde => Some(BinaryOperator::PGRegexMatch),
887            Token::TildeAsterisk => Some(BinaryOperator::PGRegexIMatch),
888            Token::ExclamationMarkTilde => Some(BinaryOperator::PGRegexNotMatch),
889            Token::ExclamationMarkTildeAsterisk => Some(BinaryOperator::PGRegexNotIMatch),
890            Token::Word(w) => match w.keyword {
891                Keyword::AND => Some(BinaryOperator::And),
892                Keyword::OR => Some(BinaryOperator::Or),
893                Keyword::LIKE => Some(BinaryOperator::Like),
894                Keyword::ILIKE => Some(BinaryOperator::ILike),
895                Keyword::NOT => {
896                    if self.parse_keyword(Keyword::LIKE) {
897                        Some(BinaryOperator::NotLike)
898                    } else if self.parse_keyword(Keyword::ILIKE) {
899                        Some(BinaryOperator::NotILike)
900                    } else {
901                        None
902                    }
903                }
904                _ => None,
905            },
906            _ => None,
907        };
908
909        if let Some(op) = regular_binary_operator {
910            Ok(Expr::BinaryOp(BinaryOp {
911                left: Box::new(expr),
912                op,
913                right: Box::new(self.parse_subexpr(precedence)?),
914            }))
915        } else if let Token::Word(w) = &tok {
916            match w.keyword {
917                Keyword::IS => {
918                    if self.parse_keyword(Keyword::NULL) {
919                        Ok(Expr::IsNull(Box::new(expr)))
920                    } else if self.parse_keywords(&[Keyword::NOT, Keyword::NULL]) {
921                        Ok(Expr::IsNotNull(Box::new(expr)))
922                    } else {
923                        self.expected("NULL or NOT NULL after IS", self.peek_token())
924                    }
925                }
926                Keyword::NOT | Keyword::IN | Keyword::BETWEEN => {
927                    self.prev_token();
928                    let negated = self.parse_keyword(Keyword::NOT);
929                    if self.parse_keyword(Keyword::IN) {
930                        self.parse_in(expr, negated)
931                    } else if self.parse_keyword(Keyword::BETWEEN) {
932                        self.parse_between(expr, negated)
933                    } else {
934                        self.expected("IN or BETWEEN after NOT", self.peek_token())
935                    }
936                }
937                // Can only happen if `get_next_precedence` got out of sync with this function
938                _ => parser_err!(format!("No infix parser for token {:?}", tok)),
939            }
940        } else if Token::DoubleColon == tok {
941            self.parse_pg_cast(expr)
942        } else if Token::ExclamationMark == tok {
943            // PostgreSQL factorial operation
944            Ok(Expr::UnaryOp(UnaryOp {
945                op: UnaryOperator::PGPostfixFactorial,
946                expr: Box::new(expr),
947            }))
948        } else if Token::LBracket == tok {
949            self.parse_map_access(expr)
950        } else {
951            // Can only happen if `get_next_precedence` got out of sync with this function
952            parser_err!(format!("No infix parser for token {:?}", tok))
953        }
954    }
955
956    pub fn parse_map_access(&mut self, expr: Expr) -> Result<Expr, ParserError> {
957        let key = self.parse_literal_string()?;
958        let tok = self.consume_token(&Token::RBracket);
959        debug!("Tok: {}", tok);
960        match expr {
961            e @ Expr::Identifier(_) | e @ Expr::CompoundIdentifier(_) => {
962                Ok(Expr::MapAccess(MapAccess {
963                    column: Box::new(e),
964                    key,
965                }))
966            }
967            _ => Ok(expr),
968        }
969    }
970
971    /// Parses the parens following the `[ NOT ] IN` operator
972    pub fn parse_in(&mut self, expr: Expr, negated: bool) -> Result<Expr, ParserError> {
973        self.expect_token(&Token::LParen)?;
974        let in_op = if self.parse_keyword(Keyword::SELECT) || self.parse_keyword(Keyword::WITH) {
975            self.prev_token();
976            Expr::InSubquery(InSubquery {
977                expr: Box::new(expr),
978                subquery: Box::new(self.parse_query()?),
979                negated,
980            })
981        } else {
982            Expr::InList(InList {
983                expr: Box::new(expr),
984                list: self.parse_comma_separated(Parser::parse_expr)?,
985                negated,
986            })
987        };
988        self.expect_token(&Token::RParen)?;
989        Ok(in_op)
990    }
991
992    /// Parses `BETWEEN <low> AND <high>`, assuming the `BETWEEN` keyword was already consumed
993    pub fn parse_between(&mut self, expr: Expr, negated: bool) -> Result<Expr, ParserError> {
994        // Stop parsing subexpressions for <low> and <high> on tokens with
995        // precedence lower than that of `BETWEEN`, such as `AND`, `IS`, etc.
996        let low = self.parse_subexpr(Self::BETWEEN_PREC)?;
997        self.expect_keyword(Keyword::AND)?;
998        let high = self.parse_subexpr(Self::BETWEEN_PREC)?;
999        Ok(Expr::Between(Between {
1000            expr: Box::new(expr),
1001            negated,
1002            low: Box::new(low),
1003            high: Box::new(high),
1004        }))
1005    }
1006
1007    /// Parse a postgresql casting style which is in the form of `expr::datatype`
1008    pub fn parse_pg_cast(&mut self, expr: Expr) -> Result<Expr, ParserError> {
1009        Ok(Expr::Cast(Cast {
1010            expr: Box::new(expr),
1011            data_type: self.parse_data_type()?,
1012        }))
1013    }
1014
1015    const UNARY_NOT_PREC: u8 = 15;
1016    const BETWEEN_PREC: u8 = 20;
1017    const PLUS_MINUS_PREC: u8 = 30;
1018
1019    /// Get the precedence of the next token
1020    pub fn get_next_precedence(&self) -> Result<u8, ParserError> {
1021        let token = self.peek_token();
1022        debug!("get_next_precedence() {:?}", token);
1023        match token {
1024            Token::Word(w) if w.keyword == Keyword::OR => Ok(5),
1025            Token::Word(w) if w.keyword == Keyword::AND => Ok(10),
1026            Token::Word(w) if w.keyword == Keyword::NOT => match self.peek_nth_token(1) {
1027                // The precedence of NOT varies depending on keyword that
1028                // follows it. If it is followed by IN, BETWEEN, or LIKE,
1029                // it takes on the precedence of those tokens. Otherwise it
1030                // is not an infix operator, and therefore has zero
1031                // precedence.
1032                Token::Word(w) if w.keyword == Keyword::IN => Ok(Self::BETWEEN_PREC),
1033                Token::Word(w) if w.keyword == Keyword::BETWEEN => Ok(Self::BETWEEN_PREC),
1034                Token::Word(w) if w.keyword == Keyword::LIKE => Ok(Self::BETWEEN_PREC),
1035                Token::Word(w) if w.keyword == Keyword::ILIKE => Ok(Self::BETWEEN_PREC),
1036                _ => Ok(0),
1037            },
1038            Token::Word(w) if w.keyword == Keyword::IS => Ok(17),
1039            Token::Word(w) if w.keyword == Keyword::IN => Ok(Self::BETWEEN_PREC),
1040            Token::Word(w) if w.keyword == Keyword::BETWEEN => Ok(Self::BETWEEN_PREC),
1041            Token::Word(w) if w.keyword == Keyword::LIKE => Ok(Self::BETWEEN_PREC),
1042            Token::Word(w) if w.keyword == Keyword::ILIKE => Ok(Self::BETWEEN_PREC),
1043            Token::Eq
1044            | Token::Lt
1045            | Token::LtEq
1046            | Token::Neq
1047            | Token::Gt
1048            | Token::GtEq
1049            | Token::DoubleEq
1050            | Token::Tilde
1051            | Token::TildeAsterisk
1052            | Token::ExclamationMarkTilde
1053            | Token::ExclamationMarkTildeAsterisk
1054            | Token::Spaceship => Ok(20),
1055            Token::Pipe => Ok(21),
1056            Token::Caret | Token::Sharp | Token::ShiftRight | Token::ShiftLeft => Ok(22),
1057            Token::Ampersand => Ok(23),
1058            Token::Plus | Token::Minus => Ok(Self::PLUS_MINUS_PREC),
1059            Token::Mult | Token::Div | Token::Mod | Token::StringConcat => Ok(40),
1060            Token::DoubleColon => Ok(50),
1061            Token::ExclamationMark => Ok(50),
1062            Token::LBracket | Token::RBracket => Ok(10),
1063            _ => Ok(0),
1064        }
1065    }
1066
1067    /// Return the first non-whitespace token that has not yet been processed
1068    /// (or None if reached end-of-file)
1069    pub fn peek_token(&self) -> Token {
1070        self.peek_nth_token(0)
1071    }
1072
1073    /// Return nth non-whitespace token that has not yet been processed
1074    pub fn peek_nth_token(&self, mut n: usize) -> Token {
1075        let mut index = self.index;
1076        loop {
1077            index += 1;
1078            match self.tokens.get(index - 1) {
1079                Some(Token::Whitespace(_)) => continue,
1080                non_whitespace => {
1081                    if n == 0 {
1082                        return non_whitespace.cloned().unwrap_or(Token::EOF);
1083                    }
1084                    n -= 1;
1085                }
1086            }
1087        }
1088    }
1089
1090    /// Return the first non-whitespace token that has not yet been processed
1091    /// (or None if reached end-of-file) and mark it as processed. OK to call
1092    /// repeatedly after reaching EOF.
1093    pub fn next_token(&mut self) -> Token {
1094        loop {
1095            self.index += 1;
1096            match self.tokens.get(self.index - 1) {
1097                Some(Token::Whitespace(_)) => continue,
1098                token => return token.cloned().unwrap_or(Token::EOF),
1099            }
1100        }
1101    }
1102
1103    /// Return the first unprocessed token, possibly whitespace.
1104    pub fn next_token_no_skip(&mut self) -> Option<&Token> {
1105        self.index += 1;
1106        self.tokens.get(self.index - 1)
1107    }
1108
1109    /// Push back the last one non-whitespace token. Must be called after
1110    /// `next_token()`, otherwise might panic. OK to call after
1111    /// `next_token()` indicates an EOF.
1112    pub fn prev_token(&mut self) {
1113        loop {
1114            assert!(self.index > 0);
1115            self.index -= 1;
1116            if let Some(Token::Whitespace(_)) = self.tokens.get(self.index) {
1117                continue;
1118            }
1119            return;
1120        }
1121    }
1122
1123    /// Report unexpected token
1124    fn expected<T>(&self, expected: &str, found: Token) -> Result<T, ParserError> {
1125        parser_err!(format!("Expected {}, found: {}", expected, found))
1126    }
1127
1128    /// Look for an expected keyword and consume it if it exists
1129    #[must_use]
1130    pub fn parse_keyword(&mut self, expected: Keyword) -> bool {
1131        match self.peek_token() {
1132            Token::Word(w) if expected == w.keyword => {
1133                self.next_token();
1134                true
1135            }
1136            _ => false,
1137        }
1138    }
1139
1140    /// Look for an expected sequence of keywords and consume them if they exist
1141    #[must_use]
1142    pub fn parse_keywords(&mut self, keywords: &[Keyword]) -> bool {
1143        let index = self.index;
1144        for &keyword in keywords {
1145            if !self.parse_keyword(keyword) {
1146                // println!("parse_keywords aborting .. did not find {:?}", keyword);
1147                // reset index and return immediately
1148                self.index = index;
1149                return false;
1150            }
1151        }
1152        true
1153    }
1154
1155    /// Look for one of the given keywords and return the one that matches.
1156    #[must_use]
1157    pub fn parse_one_of_keywords(&mut self, keywords: &[Keyword]) -> Option<Keyword> {
1158        match self.peek_token() {
1159            Token::Word(w) => {
1160                keywords
1161                    .iter()
1162                    .find(|keyword| **keyword == w.keyword)
1163                    .map(|keyword| {
1164                        self.next_token();
1165                        *keyword
1166                    })
1167            }
1168            _ => None,
1169        }
1170    }
1171
1172    /// Bail out if the current token is not one of the expected keywords, or consume it if it is
1173    pub fn expect_one_of_keywords(&mut self, keywords: &[Keyword]) -> Result<Keyword, ParserError> {
1174        if let Some(keyword) = self.parse_one_of_keywords(keywords) {
1175            Ok(keyword)
1176        } else {
1177            let keywords: Vec<String> = keywords.iter().map(|x| format!("{:?}", x)).collect();
1178            self.expected(
1179                &format!("one of {}", keywords.join(" or ")),
1180                self.peek_token(),
1181            )
1182        }
1183    }
1184
1185    /// Bail out if the current token is not an expected keyword, or consume it if it is
1186    pub fn expect_keyword(&mut self, expected: Keyword) -> Result<(), ParserError> {
1187        if self.parse_keyword(expected) {
1188            Ok(())
1189        } else {
1190            self.expected(format!("{:?}", &expected).as_str(), self.peek_token())
1191        }
1192    }
1193
1194    /// Bail out if the following tokens are not the expected sequence of
1195    /// keywords, or consume them if they are.
1196    pub fn expect_keywords(&mut self, expected: &[Keyword]) -> Result<(), ParserError> {
1197        for &kw in expected {
1198            self.expect_keyword(kw)?;
1199        }
1200        Ok(())
1201    }
1202
1203    /// Consume the next token if it matches the expected token, otherwise return false
1204    #[must_use]
1205    pub fn consume_token(&mut self, expected: &Token) -> bool {
1206        if self.peek_token() == *expected {
1207            self.next_token();
1208            true
1209        } else {
1210            false
1211        }
1212    }
1213
1214    /// Bail out if the current token is not an expected keyword, or consume it if it is
1215    pub fn expect_token(&mut self, expected: &Token) -> Result<(), ParserError> {
1216        if self.consume_token(expected) {
1217            Ok(())
1218        } else {
1219            self.expected(&expected.to_string(), self.peek_token())
1220        }
1221    }
1222
1223    /// Parse a comma-separated list of 1+ items accepted by `F`
1224    pub fn parse_comma_separated<T, F>(&mut self, mut f: F) -> Result<Vec<T>, ParserError>
1225    where
1226        F: FnMut(&mut Parser<'a>) -> Result<T, ParserError>,
1227    {
1228        let mut values = vec![];
1229        loop {
1230            values.push(f(self)?);
1231            if !self.consume_token(&Token::Comma) {
1232                break;
1233            }
1234        }
1235        Ok(values)
1236    }
1237
1238    /// Run a parser method `f`, reverting back to the current position
1239    /// if unsuccessful.
1240    #[must_use]
1241    fn maybe_parse<T, F>(&mut self, mut f: F) -> Option<T>
1242    where
1243        F: FnMut(&mut Parser) -> Result<T, ParserError>,
1244    {
1245        let index = self.index;
1246        if let Ok(t) = f(self) {
1247            Some(t)
1248        } else {
1249            self.index = index;
1250            None
1251        }
1252    }
1253
1254    /// Parse either `ALL` or `DISTINCT`. Returns `true` if `DISTINCT` is parsed and results in a
1255    /// `ParserError` if both `ALL` and `DISTINCT` are fround.
1256    pub fn parse_all_or_distinct(&mut self) -> Result<bool, ParserError> {
1257        let all = self.parse_keyword(Keyword::ALL);
1258        let distinct = self.parse_keyword(Keyword::DISTINCT);
1259        if all && distinct {
1260            return parser_err!("Cannot specify both ALL and DISTINCT".to_string());
1261        } else {
1262            Ok(distinct)
1263        }
1264    }
1265
1266    /// Parse a SQL CREATE statement
1267    pub fn parse_create(&mut self) -> Result<Statement, ParserError> {
1268        use Statement::*;
1269        let or_replace = self.parse_keywords(&[Keyword::OR, Keyword::REPLACE]);
1270        let temporary = self
1271            .parse_one_of_keywords(&[Keyword::TEMP, Keyword::TEMPORARY])
1272            .is_some();
1273        if self.parse_keyword(Keyword::TABLE) {
1274            Ok(Statement::CreateTable(Box::new(
1275                self.parse_create_table(or_replace, temporary)?,
1276            )))
1277        } else if self.parse_keyword(Keyword::MATERIALIZED) || self.parse_keyword(Keyword::VIEW) {
1278            self.prev_token();
1279            Ok(Statement::CreateView(self.parse_create_view(or_replace)?))
1280        } else if self.parse_keyword(Keyword::EXTERNAL) {
1281            Ok(Statement::CreateTable(Box::new(
1282                self.parse_create_external_table(or_replace)?,
1283            )))
1284        } else if or_replace {
1285            self.expected(
1286                "[EXTERNAL] TABLE or [MATERIALIZED] VIEW after CREATE OR REPLACE",
1287                self.peek_token(),
1288            )
1289        } else if self.parse_keyword(Keyword::INDEX) {
1290            Ok(CreateIndex(self.parse_create_index(false)?))
1291        } else if self.parse_keywords(&[Keyword::UNIQUE, Keyword::INDEX]) {
1292            Ok(CreateIndex(self.parse_create_index(true)?))
1293        } else if self.parse_keyword(Keyword::VIRTUAL) {
1294            Ok(CreateVirtualTable(self.parse_create_virtual_table()?))
1295        } else if self.parse_keyword(Keyword::SCHEMA) {
1296            Ok(CreateSchema(self.parse_create_schema()?))
1297        } else {
1298            self.expected("an object type after CREATE", self.peek_token())
1299        }
1300    }
1301
1302    /// SQLite-specific `CREATE VIRTUAL TABLE`
1303    pub fn parse_create_virtual_table(&mut self) -> Result<CreateVirtualTable, ParserError> {
1304        self.expect_keyword(Keyword::TABLE)?;
1305        let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
1306        let table_name = self.parse_object_name()?;
1307        self.expect_keyword(Keyword::USING)?;
1308        let module_name = self.parse_identifier()?;
1309        // SQLite docs note that module "arguments syntax is sufficiently
1310        // general that the arguments can be made to appear as column
1311        // definitions in a traditional CREATE TABLE statement", but
1312        // we don't implement that.
1313        let module_args = self.parse_parenthesized_column_list(Optional)?;
1314        Ok(CreateVirtualTable {
1315            name: table_name,
1316            if_not_exists,
1317            module_name,
1318            module_args,
1319        })
1320    }
1321
1322    pub fn parse_create_schema(&mut self) -> Result<CreateSchema, ParserError> {
1323        let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
1324        let schema_name = self.parse_object_name()?;
1325        Ok(CreateSchema {
1326            schema_name,
1327            if_not_exists,
1328        })
1329    }
1330
1331    pub fn parse_create_database(&mut self) -> Result<CreateDatabase, ParserError> {
1332        let ine = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
1333        let db_name = self.parse_object_name()?;
1334        let mut location = None;
1335        let mut managed_location = None;
1336        loop {
1337            match self.parse_one_of_keywords(&[Keyword::LOCATION, Keyword::MANAGEDLOCATION]) {
1338                Some(Keyword::LOCATION) => location = Some(self.parse_literal_string()?),
1339                Some(Keyword::MANAGEDLOCATION) => {
1340                    managed_location = Some(self.parse_literal_string()?)
1341                }
1342                _ => break,
1343            }
1344        }
1345        Ok(CreateDatabase {
1346            db_name,
1347            if_not_exists: ine,
1348            location,
1349            managed_location,
1350        })
1351    }
1352
1353    pub fn parse_create_external_table(
1354        &mut self,
1355        or_replace: bool,
1356    ) -> Result<CreateTable, ParserError> {
1357        self.expect_keyword(Keyword::TABLE)?;
1358        let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
1359        let table_name = self.parse_object_name()?;
1360        let (columns, constraints) = self.parse_columns()?;
1361
1362        let hive_distribution = self.parse_hive_distribution()?;
1363        let hive_formats = self.parse_hive_formats()?;
1364
1365        let file_format = if let Some(ff) = &hive_formats.storage {
1366            match ff {
1367                HiveIOFormat::FileFormat { format } => Some(format.clone()),
1368                _ => None,
1369            }
1370        } else {
1371            None
1372        };
1373        let location = hive_formats.location.clone();
1374        let table_properties = self.parse_options(Keyword::TBLPROPERTIES)?;
1375        Ok(CreateTable {
1376            name: table_name,
1377            columns,
1378            constraints,
1379            hive_distribution,
1380            hive_formats: Some(hive_formats),
1381            with_options: vec![],
1382            table_properties,
1383            or_replace,
1384            if_not_exists,
1385            external: true,
1386            temporary: false,
1387            file_format,
1388            location,
1389            query: None,
1390            without_rowid: false,
1391            like: None,
1392        })
1393    }
1394
1395    pub fn parse_file_format(&mut self) -> Result<FileFormat, ParserError> {
1396        match self.next_token() {
1397            Token::Word(w) => match w.keyword {
1398                Keyword::AVRO => Ok(FileFormat::AVRO),
1399                Keyword::JSONFILE => Ok(FileFormat::JSONFILE),
1400                Keyword::ORC => Ok(FileFormat::ORC),
1401                Keyword::PARQUET => Ok(FileFormat::PARQUET),
1402                Keyword::RCFILE => Ok(FileFormat::RCFILE),
1403                Keyword::SEQUENCEFILE => Ok(FileFormat::SEQUENCEFILE),
1404                Keyword::TEXTFILE => Ok(FileFormat::TEXTFILE),
1405                _ => self.expected("fileformat", Token::Word(w)),
1406            },
1407            unexpected => self.expected("fileformat", unexpected),
1408        }
1409    }
1410
1411    pub fn parse_create_view(&mut self, or_replace: bool) -> Result<CreateView, ParserError> {
1412        let materialized = self.parse_keyword(Keyword::MATERIALIZED);
1413        self.expect_keyword(Keyword::VIEW)?;
1414        // Many dialects support `OR ALTER` right after `CREATE`, but we don't (yet).
1415        // ANSI SQL and Postgres support RECURSIVE here, but we don't support it either.
1416        let name = self.parse_object_name()?;
1417        let columns = self.parse_parenthesized_column_list(Optional)?;
1418        let with_options = self.parse_options(Keyword::WITH)?;
1419        self.expect_keyword(Keyword::AS)?;
1420        let query = Box::new(self.parse_query()?);
1421        // Optional `WITH [ CASCADED | LOCAL ] CHECK OPTION` is widely supported here.
1422        Ok(CreateView {
1423            name,
1424            columns,
1425            query,
1426            materialized,
1427            or_replace,
1428            with_options,
1429        })
1430    }
1431
1432    pub fn parse_drop(&mut self) -> Result<Drop, ParserError> {
1433        let object_type = if self.parse_keyword(Keyword::TABLE) {
1434            ObjectType::Table
1435        } else if self.parse_keyword(Keyword::VIEW) {
1436            ObjectType::View
1437        } else if self.parse_keyword(Keyword::INDEX) {
1438            ObjectType::Index
1439        } else if self.parse_keyword(Keyword::SCHEMA) {
1440            ObjectType::Schema
1441        } else {
1442            return self.expected("TABLE, VIEW, INDEX or SCHEMA after DROP", self.peek_token());
1443        };
1444        // Many dialects support the non standard `IF EXISTS` clause and allow
1445        // specifying multiple objects to delete in a single statement
1446        let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
1447        let names = self.parse_comma_separated(Parser::parse_object_name)?;
1448        let cascade = self.parse_keyword(Keyword::CASCADE);
1449        let restrict = self.parse_keyword(Keyword::RESTRICT);
1450        let purge = self.parse_keyword(Keyword::PURGE);
1451        if cascade && restrict {
1452            return parser_err!("Cannot specify both CASCADE and RESTRICT in DROP");
1453        }
1454        Ok(Drop {
1455            object_type,
1456            if_exists,
1457            names,
1458            cascade,
1459            purge,
1460        })
1461    }
1462
1463    pub fn parse_create_index(&mut self, unique: bool) -> Result<CreateIndex, ParserError> {
1464        let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
1465        let index_name = self.parse_object_name()?;
1466        self.expect_keyword(Keyword::ON)?;
1467        let table_name = self.parse_object_name()?;
1468        self.expect_token(&Token::LParen)?;
1469        let columns = self.parse_comma_separated(Parser::parse_order_by_expr)?;
1470        self.expect_token(&Token::RParen)?;
1471        Ok(CreateIndex {
1472            name: index_name,
1473            table_name,
1474            columns,
1475            unique,
1476            if_not_exists,
1477        })
1478    }
1479
1480    //TODO: Implement parsing for Skewed and Clustered
1481    pub fn parse_hive_distribution(&mut self) -> Result<HiveDistributionStyle, ParserError> {
1482        if self.parse_keywords(&[Keyword::PARTITIONED, Keyword::BY]) {
1483            self.expect_token(&Token::LParen)?;
1484            let columns = self.parse_comma_separated(Parser::parse_column_def)?;
1485            self.expect_token(&Token::RParen)?;
1486            Ok(HiveDistributionStyle::PARTITIONED { columns })
1487        } else {
1488            Ok(HiveDistributionStyle::NONE)
1489        }
1490    }
1491
1492    pub fn parse_hive_formats(&mut self) -> Result<HiveFormat, ParserError> {
1493        let mut hive_format = HiveFormat::default();
1494        loop {
1495            match self.parse_one_of_keywords(&[Keyword::ROW, Keyword::STORED, Keyword::LOCATION]) {
1496                Some(Keyword::ROW) => {
1497                    hive_format.row_format = Some(self.parse_row_format()?);
1498                }
1499                Some(Keyword::STORED) => {
1500                    self.expect_keyword(Keyword::AS)?;
1501                    if self.parse_keyword(Keyword::INPUTFORMAT) {
1502                        let input_format = self.parse_expr()?;
1503                        self.expect_keyword(Keyword::OUTPUTFORMAT)?;
1504                        let output_format = self.parse_expr()?;
1505                        hive_format.storage = Some(HiveIOFormat::IOF {
1506                            input_format,
1507                            output_format,
1508                        });
1509                    } else {
1510                        let format = self.parse_file_format()?;
1511                        hive_format.storage = Some(HiveIOFormat::FileFormat { format });
1512                    }
1513                }
1514                Some(Keyword::LOCATION) => {
1515                    hive_format.location = Some(self.parse_literal_string()?);
1516                }
1517                None => break,
1518                _ => break,
1519            }
1520        }
1521
1522        Ok(hive_format)
1523    }
1524
1525    pub fn parse_row_format(&mut self) -> Result<HiveRowFormat, ParserError> {
1526        self.expect_keyword(Keyword::FORMAT)?;
1527        match self.parse_one_of_keywords(&[Keyword::SERDE, Keyword::DELIMITED]) {
1528            Some(Keyword::SERDE) => {
1529                let class = self.parse_literal_string()?;
1530                Ok(HiveRowFormat::SERDE { class })
1531            }
1532            _ => Ok(HiveRowFormat::DELIMITED),
1533        }
1534    }
1535
1536    pub fn parse_create_table(
1537        &mut self,
1538        or_replace: bool,
1539        temporary: bool,
1540    ) -> Result<CreateTable, ParserError> {
1541        let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
1542        let table_name = self.parse_object_name()?;
1543        let like = if self.parse_keyword(Keyword::LIKE) || self.parse_keyword(Keyword::ILIKE) {
1544            self.parse_object_name().ok()
1545        } else {
1546            None
1547        };
1548        // parse optional column list (schema)
1549        let (columns, constraints) = self.parse_columns()?;
1550
1551        // SQLite supports `WITHOUT ROWID` at the end of `CREATE TABLE`
1552        let without_rowid = self.parse_keywords(&[Keyword::WITHOUT, Keyword::ROWID]);
1553
1554        let hive_distribution = self.parse_hive_distribution()?;
1555        let hive_formats = self.parse_hive_formats()?;
1556        // PostgreSQL supports `WITH ( options )`, before `AS`
1557        let with_options = self.parse_options(Keyword::WITH)?;
1558        let table_properties = self.parse_options(Keyword::TBLPROPERTIES)?;
1559        // Parse optional `AS ( query )`
1560        let query = if self.parse_keyword(Keyword::AS) {
1561            Some(Box::new(self.parse_query()?))
1562        } else {
1563            None
1564        };
1565
1566        Ok(CreateTable {
1567            name: table_name,
1568            temporary,
1569            columns,
1570            constraints,
1571            with_options,
1572            table_properties,
1573            or_replace,
1574            if_not_exists,
1575            hive_distribution,
1576            hive_formats: Some(hive_formats),
1577            external: false,
1578            file_format: None,
1579            location: None,
1580            query,
1581            without_rowid,
1582            like,
1583        })
1584    }
1585
1586    fn parse_columns(&mut self) -> Result<(Vec<ColumnDef>, Vec<TableConstraint>), ParserError> {
1587        let mut columns = vec![];
1588        let mut constraints = vec![];
1589        if !self.consume_token(&Token::LParen) || self.consume_token(&Token::RParen) {
1590            return Ok((columns, constraints));
1591        }
1592
1593        loop {
1594            if let Some(constraint) = self.parse_optional_table_constraint()? {
1595                constraints.push(constraint);
1596            } else if let Token::Word(_) = self.peek_token() {
1597                columns.push(self.parse_column_def()?);
1598            } else {
1599                return self.expected("column name or constraint definition", self.peek_token());
1600            }
1601            let comma = self.consume_token(&Token::Comma);
1602            if self.consume_token(&Token::RParen) {
1603                // allow a trailing comma, even though it's not in standard
1604                break;
1605            } else if !comma {
1606                return self.expected("',' or ')' after column definition", self.peek_token());
1607            }
1608        }
1609
1610        Ok((columns, constraints))
1611    }
1612
1613    fn parse_column_def(&mut self) -> Result<ColumnDef, ParserError> {
1614        let name = self.parse_identifier()?;
1615        let data_type = self.parse_data_type()?;
1616        let collation = if self.parse_keyword(Keyword::COLLATE) {
1617            Some(self.parse_object_name()?)
1618        } else {
1619            None
1620        };
1621        let mut options = vec![];
1622        loop {
1623            if self.parse_keyword(Keyword::CONSTRAINT) {
1624                let name = Some(self.parse_identifier()?);
1625                if let Some(option) = self.parse_optional_column_option()? {
1626                    options.push(ColumnOptionDef { name, option });
1627                } else {
1628                    return self.expected(
1629                        "constraint details after CONSTRAINT <name>",
1630                        self.peek_token(),
1631                    );
1632                }
1633            } else if let Some(option) = self.parse_optional_column_option()? {
1634                options.push(ColumnOptionDef { name: None, option });
1635            } else {
1636                break;
1637            };
1638        }
1639        Ok(ColumnDef {
1640            name,
1641            data_type,
1642            collation,
1643            options,
1644        })
1645    }
1646
1647    pub fn parse_optional_column_option(&mut self) -> Result<Option<ColumnOption>, ParserError> {
1648        if self.parse_keywords(&[Keyword::NOT, Keyword::NULL]) {
1649            Ok(Some(ColumnOption::NotNull))
1650        } else if self.parse_keyword(Keyword::NULL) {
1651            Ok(Some(ColumnOption::Null))
1652        } else if self.parse_keyword(Keyword::DEFAULT) {
1653            Ok(Some(ColumnOption::Default(self.parse_expr()?)))
1654        } else if self.parse_keywords(&[Keyword::PRIMARY, Keyword::KEY]) {
1655            Ok(Some(ColumnOption::Unique { is_primary: true }))
1656        } else if self.parse_keyword(Keyword::UNIQUE) {
1657            Ok(Some(ColumnOption::Unique { is_primary: false }))
1658        } else if self.parse_keyword(Keyword::REFERENCES) {
1659            let foreign_table = self.parse_object_name()?;
1660            // PostgreSQL allows omitting the column list and
1661            // uses the primary key column of the foreign table by default
1662            let referred_columns = self.parse_parenthesized_column_list(Optional)?;
1663            let mut on_delete = None;
1664            let mut on_update = None;
1665            loop {
1666                if on_delete.is_none() && self.parse_keywords(&[Keyword::ON, Keyword::DELETE]) {
1667                    on_delete = Some(self.parse_referential_action()?);
1668                } else if on_update.is_none()
1669                    && self.parse_keywords(&[Keyword::ON, Keyword::UPDATE])
1670                {
1671                    on_update = Some(self.parse_referential_action()?);
1672                } else {
1673                    break;
1674                }
1675            }
1676            Ok(Some(ColumnOption::ForeignKey {
1677                foreign_table,
1678                referred_columns,
1679                on_delete,
1680                on_update,
1681            }))
1682        } else if self.parse_keyword(Keyword::CHECK) {
1683            self.expect_token(&Token::LParen)?;
1684            let expr = self.parse_expr()?;
1685            self.expect_token(&Token::RParen)?;
1686            Ok(Some(ColumnOption::Check(expr)))
1687        } else if self.parse_keyword(Keyword::AUTO_INCREMENT)
1688            && dialect_of!(self is MySqlDialect |  GenericDialect)
1689        {
1690            // Support AUTO_INCREMENT for MySQL
1691            Ok(Some(ColumnOption::DialectSpecific(vec![
1692                Token::make_keyword("AUTO_INCREMENT"),
1693            ])))
1694        } else if self.parse_keyword(Keyword::AUTOINCREMENT)
1695            && dialect_of!(self is SQLiteDialect |  GenericDialect)
1696        {
1697            // Support AUTOINCREMENT for SQLite
1698            Ok(Some(ColumnOption::DialectSpecific(vec![
1699                Token::make_keyword("AUTOINCREMENT"),
1700            ])))
1701        } else {
1702            Ok(None)
1703        }
1704    }
1705
1706    pub fn parse_referential_action(&mut self) -> Result<ReferentialAction, ParserError> {
1707        if self.parse_keyword(Keyword::RESTRICT) {
1708            Ok(ReferentialAction::Restrict)
1709        } else if self.parse_keyword(Keyword::CASCADE) {
1710            Ok(ReferentialAction::Cascade)
1711        } else if self.parse_keywords(&[Keyword::SET, Keyword::NULL]) {
1712            Ok(ReferentialAction::SetNull)
1713        } else if self.parse_keywords(&[Keyword::NO, Keyword::ACTION]) {
1714            Ok(ReferentialAction::NoAction)
1715        } else if self.parse_keywords(&[Keyword::SET, Keyword::DEFAULT]) {
1716            Ok(ReferentialAction::SetDefault)
1717        } else {
1718            self.expected(
1719                "one of RESTRICT, CASCADE, SET NULL, NO ACTION or SET DEFAULT",
1720                self.peek_token(),
1721            )
1722        }
1723    }
1724
1725    pub fn parse_optional_table_constraint(
1726        &mut self,
1727    ) -> Result<Option<TableConstraint>, ParserError> {
1728        let name = if self.parse_keyword(Keyword::CONSTRAINT) {
1729            Some(self.parse_identifier()?)
1730        } else {
1731            None
1732        };
1733
1734        match self.next_token() {
1735            Token::Word(w) if w.keyword == Keyword::PRIMARY || w.keyword == Keyword::UNIQUE => {
1736                let is_primary = w.keyword == Keyword::PRIMARY;
1737                if is_primary {
1738                    self.expect_keyword(Keyword::KEY)?;
1739                }
1740                let columns = self.parse_parenthesized_column_list(Mandatory)?;
1741                Ok(Some(TableConstraint::Unique(Unique {
1742                    name,
1743                    columns,
1744                    is_primary,
1745                })))
1746            }
1747            Token::Word(w) if w.keyword == Keyword::FOREIGN => {
1748                self.expect_keyword(Keyword::KEY)?;
1749                let columns = self.parse_parenthesized_column_list(Mandatory)?;
1750                self.expect_keyword(Keyword::REFERENCES)?;
1751                let foreign_table = self.parse_object_name()?;
1752                let referred_columns = self.parse_parenthesized_column_list(Mandatory)?;
1753                let mut on_delete = None;
1754                let mut on_update = None;
1755                loop {
1756                    if on_delete.is_none() && self.parse_keywords(&[Keyword::ON, Keyword::DELETE]) {
1757                        on_delete = Some(self.parse_referential_action()?);
1758                    } else if on_update.is_none()
1759                        && self.parse_keywords(&[Keyword::ON, Keyword::UPDATE])
1760                    {
1761                        on_update = Some(self.parse_referential_action()?);
1762                    } else {
1763                        break;
1764                    }
1765                }
1766                Ok(Some(TableConstraint::ForeignKey(ForeignKey {
1767                    name,
1768                    columns,
1769                    foreign_table,
1770                    referred_columns,
1771                    on_delete,
1772                    on_update,
1773                })))
1774            }
1775            Token::Word(w) if w.keyword == Keyword::CHECK => {
1776                self.expect_token(&Token::LParen)?;
1777                let expr = Box::new(self.parse_expr()?);
1778                self.expect_token(&Token::RParen)?;
1779                Ok(Some(TableConstraint::Check(Check { name, expr })))
1780            }
1781            unexpected => {
1782                if name.is_some() {
1783                    self.expected("PRIMARY, UNIQUE, FOREIGN, or CHECK", unexpected)
1784                } else {
1785                    self.prev_token();
1786                    Ok(None)
1787                }
1788            }
1789        }
1790    }
1791
1792    pub fn parse_options(&mut self, keyword: Keyword) -> Result<Vec<SqlOption>, ParserError> {
1793        if self.parse_keyword(keyword) {
1794            self.expect_token(&Token::LParen)?;
1795            let options = self.parse_comma_separated(Parser::parse_sql_option)?;
1796            self.expect_token(&Token::RParen)?;
1797            Ok(options)
1798        } else {
1799            Ok(vec![])
1800        }
1801    }
1802
1803    pub fn parse_sql_option(&mut self) -> Result<SqlOption, ParserError> {
1804        let name = self.parse_identifier()?;
1805        self.expect_token(&Token::Eq)?;
1806        let value = self.parse_value()?;
1807        Ok(SqlOption { name, value })
1808    }
1809
1810    pub fn parse_alter(&mut self) -> Result<AlterTable, ParserError> {
1811        self.expect_keyword(Keyword::TABLE)?;
1812        let _ = self.parse_keyword(Keyword::ONLY);
1813        let table_name = self.parse_object_name()?;
1814        let operation = if self.parse_keyword(Keyword::ADD) {
1815            if let Some(constraint) = self.parse_optional_table_constraint()? {
1816                AlterTableOperation::AddConstraint(constraint)
1817            } else {
1818                let if_not_exists =
1819                    self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
1820                if self.parse_keyword(Keyword::PARTITION) {
1821                    self.expect_token(&Token::LParen)?;
1822                    let partitions = self.parse_comma_separated(Parser::parse_expr)?;
1823                    self.expect_token(&Token::RParen)?;
1824                    AlterTableOperation::AddPartitions {
1825                        if_not_exists,
1826                        new_partitions: partitions,
1827                    }
1828                } else {
1829                    let _ = self.parse_keyword(Keyword::COLUMN);
1830                    let column_def = self.parse_column_def()?;
1831                    AlterTableOperation::AddColumn { column_def }
1832                }
1833            }
1834        } else if self.parse_keyword(Keyword::RENAME) {
1835            if self.parse_keyword(Keyword::TO) {
1836                let table_name = self.parse_object_name()?;
1837                AlterTableOperation::RenameTable { table_name }
1838            } else {
1839                let _ = self.parse_keyword(Keyword::COLUMN);
1840                let old_column_name = self.parse_identifier()?;
1841                self.expect_keyword(Keyword::TO)?;
1842                let new_column_name = self.parse_identifier()?;
1843                AlterTableOperation::RenameColumn {
1844                    old_column_name,
1845                    new_column_name,
1846                }
1847            }
1848        } else if self.parse_keyword(Keyword::DROP) {
1849            if self.parse_keywords(&[Keyword::IF, Keyword::EXISTS, Keyword::PARTITION]) {
1850                self.expect_token(&Token::LParen)?;
1851                let partitions = self.parse_comma_separated(Parser::parse_expr)?;
1852                self.expect_token(&Token::RParen)?;
1853                AlterTableOperation::DropPartitions {
1854                    partitions,
1855                    if_exists: true,
1856                }
1857            } else if self.parse_keyword(Keyword::PARTITION) {
1858                self.expect_token(&Token::LParen)?;
1859                let partitions = self.parse_comma_separated(Parser::parse_expr)?;
1860                self.expect_token(&Token::RParen)?;
1861                AlterTableOperation::DropPartitions {
1862                    partitions,
1863                    if_exists: false,
1864                }
1865            } else if self.parse_keyword(Keyword::CONSTRAINT) {
1866                let name = self.parse_identifier()?;
1867                let cascade = self.parse_keyword(Keyword::CASCADE);
1868                let mut restrict = false;
1869                if !cascade {
1870                    restrict = self.parse_keyword(Keyword::RESTRICT);
1871                }
1872                AlterTableOperation::DropConstraint {
1873                    name,
1874                    cascade,
1875                    restrict,
1876                }
1877            } else {
1878                let _ = self.parse_keyword(Keyword::COLUMN);
1879                let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
1880                let column_name = self.parse_identifier()?;
1881                let cascade = self.parse_keyword(Keyword::CASCADE);
1882                AlterTableOperation::DropColumn {
1883                    column_name,
1884                    if_exists,
1885                    cascade,
1886                }
1887            }
1888        } else if self.parse_keyword(Keyword::PARTITION) {
1889            self.expect_token(&Token::LParen)?;
1890            let before = self.parse_comma_separated(Parser::parse_expr)?;
1891            self.expect_token(&Token::RParen)?;
1892            self.expect_keyword(Keyword::RENAME)?;
1893            self.expect_keywords(&[Keyword::TO, Keyword::PARTITION])?;
1894            self.expect_token(&Token::LParen)?;
1895            let renames = self.parse_comma_separated(Parser::parse_expr)?;
1896            self.expect_token(&Token::RParen)?;
1897            AlterTableOperation::RenamePartitions {
1898                old_partitions: before,
1899                new_partitions: renames,
1900            }
1901        } else {
1902            return self.expected(
1903                "ADD, RENAME, PARTITION or DROP after ALTER TABLE",
1904                self.peek_token(),
1905            );
1906        };
1907        Ok(AlterTable {
1908            name: table_name,
1909            operation,
1910        })
1911    }
1912
1913    /// Parse a copy statement
1914    pub fn parse_copy(&mut self) -> Result<Copy, ParserError> {
1915        let table_name = self.parse_object_name()?;
1916        let columns = self.parse_parenthesized_column_list(Optional)?;
1917        self.expect_keywords(&[Keyword::FROM, Keyword::STDIN])?;
1918        self.expect_token(&Token::SemiColon)?;
1919        let values = self.parse_tsv();
1920        Ok(Copy {
1921            table_name,
1922            columns,
1923            values,
1924        })
1925    }
1926
1927    /// Parse a tab separated values in
1928    /// COPY payload
1929    fn parse_tsv(&mut self) -> Vec<Option<String>> {
1930        self.parse_tab_value()
1931    }
1932
1933    fn parse_tab_value(&mut self) -> Vec<Option<String>> {
1934        let mut values = vec![];
1935        let mut content = String::from("");
1936        while let Some(t) = self.next_token_no_skip() {
1937            match t {
1938                Token::Whitespace(Whitespace::Tab) => {
1939                    values.push(Some(content.to_string()));
1940                    content.clear();
1941                }
1942                Token::Whitespace(Whitespace::Newline) => {
1943                    values.push(Some(content.to_string()));
1944                    content.clear();
1945                }
1946                Token::Backslash => {
1947                    if self.consume_token(&Token::Period) {
1948                        return values;
1949                    }
1950                    if let Token::Word(w) = self.next_token() {
1951                        if w.value == "N" {
1952                            values.push(None);
1953                        }
1954                    }
1955                }
1956                _ => {
1957                    content.push_str(&t.to_string());
1958                }
1959            }
1960        }
1961        values
1962    }
1963
1964    /// Parse a literal value (numbers, strings, date/time, booleans)
1965    fn parse_value(&mut self) -> Result<Value, ParserError> {
1966        match self.next_token() {
1967            Token::Word(w) => match w.keyword {
1968                Keyword::TRUE => Ok(Value::Boolean(true)),
1969                Keyword::FALSE => Ok(Value::Boolean(false)),
1970                Keyword::NULL => Ok(Value::Null),
1971                Keyword::NoKeyword if w.quote_style.is_some() => match w.quote_style {
1972                    Some('"') => Ok(Value::DoubleQuotedString(w.value)),
1973                    Some('\'') => Ok(Value::SingleQuotedString(w.value)),
1974                    _ => self.expected("A value?", Token::Word(w))?,
1975                },
1976                _ => self.expected("a concrete value", Token::Word(w)),
1977            },
1978            // The call to n.parse() returns a bigdecimal when the
1979            // bigdecimal feature is enabled, and is otherwise a no-op
1980            // (i.e., it returns the input string).
1981            Token::Number(ref n, l) => match n.parse() {
1982                Ok(n) => Ok(Value::Number(n, l)),
1983                Err(e) => parser_err!(format!("Could not parse '{}' as number: {}", n, e)),
1984            },
1985            Token::SingleQuotedString(ref s) => Ok(Value::SingleQuotedString(s.to_string())),
1986            Token::NationalStringLiteral(ref s) => Ok(Value::NationalStringLiteral(s.to_string())),
1987            Token::HexStringLiteral(ref s) => Ok(Value::HexStringLiteral(s.to_string())),
1988            unexpected => self.expected("a value", unexpected),
1989        }
1990    }
1991
1992    pub fn parse_number_value(&mut self) -> Result<Value, ParserError> {
1993        match self.parse_value()? {
1994            v @ Value::Number(_, _) => Ok(v),
1995            _ => {
1996                self.prev_token();
1997                self.expected("literal number", self.peek_token())
1998            }
1999        }
2000    }
2001
2002    /// Parse an unsigned literal integer/long
2003    pub fn parse_literal_uint(&mut self) -> Result<u64, ParserError> {
2004        match self.next_token() {
2005            Token::Number(s, _) => s.parse::<u64>().map_err(|e| {
2006                ParserError::ParserError(format!("Could not parse '{}' as u64: {}", s, e))
2007            }),
2008            unexpected => self.expected("literal int", unexpected),
2009        }
2010    }
2011
2012    /// Parse a literal string
2013    pub fn parse_literal_string(&mut self) -> Result<String, ParserError> {
2014        match self.next_token() {
2015            Token::Word(Word { value, keyword, .. }) if keyword == Keyword::NoKeyword => Ok(value),
2016            Token::SingleQuotedString(s) => Ok(s),
2017            unexpected => self.expected("literal string", unexpected),
2018        }
2019    }
2020
2021    /// Parse a SQL datatype (in the context of a CREATE TABLE statement for example)
2022    pub fn parse_data_type(&mut self) -> Result<DataType, ParserError> {
2023        match self.next_token() {
2024            Token::Word(w) => match w.keyword {
2025                Keyword::BOOLEAN => Ok(DataType::Boolean),
2026                Keyword::FLOAT => Ok(DataType::Float(self.parse_optional_precision()?)),
2027                Keyword::REAL => Ok(DataType::Real),
2028                Keyword::DOUBLE => {
2029                    let _ = self.parse_keyword(Keyword::PRECISION);
2030                    Ok(DataType::Double)
2031                }
2032                Keyword::TINYINT => Ok(DataType::TinyInt(self.parse_optional_precision()?)),
2033                Keyword::SMALLINT => Ok(DataType::SmallInt(self.parse_optional_precision()?)),
2034                Keyword::INT | Keyword::INTEGER => {
2035                    Ok(DataType::Int(self.parse_optional_precision()?))
2036                }
2037                Keyword::BIGINT => Ok(DataType::BigInt(self.parse_optional_precision()?)),
2038                Keyword::VARCHAR => Ok(DataType::Varchar(self.parse_optional_precision()?)),
2039                Keyword::CHAR | Keyword::CHARACTER => {
2040                    if self.parse_keyword(Keyword::VARYING) {
2041                        Ok(DataType::Varchar(self.parse_optional_precision()?))
2042                    } else {
2043                        Ok(DataType::Char(self.parse_optional_precision()?))
2044                    }
2045                }
2046                Keyword::UUID => Ok(DataType::Uuid),
2047                Keyword::DATE => Ok(DataType::Date),
2048                Keyword::TIMESTAMP => {
2049                    // TBD: we throw away "with/without timezone" information
2050                    if self.parse_keyword(Keyword::WITH) || self.parse_keyword(Keyword::WITHOUT) {
2051                        self.expect_keywords(&[Keyword::TIME, Keyword::ZONE])?;
2052                    }
2053                    Ok(DataType::Timestamp)
2054                }
2055                Keyword::TIME => {
2056                    // TBD: we throw away "with/without timezone" information
2057                    if self.parse_keyword(Keyword::WITH) || self.parse_keyword(Keyword::WITHOUT) {
2058                        self.expect_keywords(&[Keyword::TIME, Keyword::ZONE])?;
2059                    }
2060                    Ok(DataType::Time)
2061                }
2062                // Interval types can be followed by a complicated interval
2063                // qualifier that we don't currently support. See
2064                // parse_interval_literal for a taste.
2065                Keyword::INTERVAL => Ok(DataType::Interval),
2066                Keyword::REGCLASS => Ok(DataType::Regclass),
2067                Keyword::STRING => Ok(DataType::String),
2068                Keyword::TEXT => {
2069                    if self.consume_token(&Token::LBracket) {
2070                        // Note: this is postgresql-specific
2071                        self.expect_token(&Token::RBracket)?;
2072                        Ok(DataType::Array(Box::new(DataType::Text)))
2073                    } else {
2074                        Ok(DataType::Text)
2075                    }
2076                }
2077                Keyword::BYTEA => Ok(DataType::Bytea),
2078                Keyword::NUMERIC | Keyword::DECIMAL | Keyword::DEC => {
2079                    let (precision, scale) = self.parse_optional_precision_scale()?;
2080                    Ok(DataType::Decimal(precision, scale))
2081                }
2082                _ => {
2083                    self.prev_token();
2084                    let type_name = self.parse_object_name()?;
2085                    Ok(DataType::Custom(type_name))
2086                }
2087            },
2088            unexpected => self.expected("a data type name", unexpected),
2089        }
2090    }
2091
2092    /// Parse `AS identifier` (or simply `identifier` if it's not a reserved keyword)
2093    /// Some examples with aliases: `SELECT 1 foo`, `SELECT COUNT(*) AS cnt`,
2094    /// `SELECT ... FROM t1 foo, t2 bar`, `SELECT ... FROM (...) AS bar`
2095    pub fn parse_optional_alias(
2096        &mut self,
2097        reserved_kwds: &[Keyword],
2098    ) -> Result<Option<Ident>, ParserError> {
2099        let after_as = self.parse_keyword(Keyword::AS);
2100        match self.next_token() {
2101            // Accept any identifier after `AS` (though many dialects have restrictions on
2102            // keywords that may appear here). If there's no `AS`: don't parse keywords,
2103            // which may start a construct allowed in this position, to be parsed as aliases.
2104            // (For example, in `FROM t1 JOIN` the `JOIN` will always be parsed as a keyword,
2105            // not an alias.)
2106            Token::Word(w) if after_as || !reserved_kwds.contains(&w.keyword) => {
2107                Ok(Some(w.to_ident()))
2108            }
2109            // MSSQL supports single-quoted strings as aliases for columns
2110            // We accept them as table aliases too, although MSSQL does not.
2111            //
2112            // Note, that this conflicts with an obscure rule from the SQL
2113            // standard, which we don't implement:
2114            // https://crate.io/docs/sql-99/en/latest/chapters/07.html#character-string-literal-s
2115            //    "[Obscure Rule] SQL allows you to break a long <character
2116            //    string literal> up into two or more smaller <character string
2117            //    literal>s, split by a <separator> that includes a newline
2118            //    character. When it sees such a <literal>, your DBMS will
2119            //    ignore the <separator> and treat the multiple strings as
2120            //    a single <literal>."
2121            Token::SingleQuotedString(s) => Ok(Some(Ident::with_quote('\'', s))),
2122            not_an_ident => {
2123                if after_as {
2124                    return self.expected("an identifier after AS", not_an_ident);
2125                }
2126                self.prev_token();
2127                Ok(None) // no alias found
2128            }
2129        }
2130    }
2131
2132    /// Parse `AS identifier` when the AS is describing a table-valued object,
2133    /// like in `... FROM generate_series(1, 10) AS t (col)`. In this case
2134    /// the alias is allowed to optionally name the columns in the table, in
2135    /// addition to the table itself.
2136    pub fn parse_optional_table_alias(
2137        &mut self,
2138        reserved_kwds: &[Keyword],
2139    ) -> Result<Option<TableAlias>, ParserError> {
2140        match self.parse_optional_alias(reserved_kwds)? {
2141            Some(name) => {
2142                let columns = self.parse_parenthesized_column_list(Optional)?;
2143                Ok(Some(TableAlias { name, columns }))
2144            }
2145            None => Ok(None),
2146        }
2147    }
2148
2149    /// Parse a possibly qualified, possibly quoted identifier, e.g.
2150    /// `foo` or `myschema."table"
2151    pub fn parse_object_name(&mut self) -> Result<ObjectName, ParserError> {
2152        let mut idents = vec![];
2153        loop {
2154            idents.push(self.parse_identifier()?);
2155            if !self.consume_token(&Token::Period) {
2156                break;
2157            }
2158        }
2159        Ok(ObjectName(idents))
2160    }
2161
2162    /// Parse identifiers
2163    pub fn parse_identifiers(&mut self) -> Result<Vec<Ident>, ParserError> {
2164        let mut idents = vec![];
2165        loop {
2166            match self.next_token() {
2167                Token::Word(w) => idents.push(w.to_ident()),
2168                Token::EOF => break,
2169                _ => {}
2170            }
2171        }
2172        Ok(idents)
2173    }
2174
2175    /// Parse a simple one-word identifier (possibly quoted, possibly a keyword)
2176    pub fn parse_identifier(&mut self) -> Result<Ident, ParserError> {
2177        match self.next_token() {
2178            Token::Word(w) => Ok(w.to_ident()),
2179            Token::SingleQuotedString(s) => Ok(Ident::with_quote('\'', s)),
2180            unexpected => self.expected("identifier", unexpected),
2181        }
2182    }
2183
2184    /// Parse a parenthesized comma-separated list of unqualified, possibly quoted identifiers
2185    pub fn parse_parenthesized_column_list(
2186        &mut self,
2187        optional: IsOptional,
2188    ) -> Result<Vec<Ident>, ParserError> {
2189        if self.consume_token(&Token::LParen) {
2190            let cols = self.parse_comma_separated(Parser::parse_identifier)?;
2191            self.expect_token(&Token::RParen)?;
2192            Ok(cols)
2193        } else if optional == Optional {
2194            Ok(vec![])
2195        } else {
2196            self.expected("a list of columns in parentheses", self.peek_token())
2197        }
2198    }
2199
2200    pub fn parse_optional_precision(&mut self) -> Result<Option<u64>, ParserError> {
2201        if self.consume_token(&Token::LParen) {
2202            let n = self.parse_literal_uint()?;
2203            self.expect_token(&Token::RParen)?;
2204            Ok(Some(n))
2205        } else {
2206            Ok(None)
2207        }
2208    }
2209
2210    pub fn parse_optional_precision_scale(
2211        &mut self,
2212    ) -> Result<(Option<u64>, Option<u64>), ParserError> {
2213        if self.consume_token(&Token::LParen) {
2214            let n = self.parse_literal_uint()?;
2215            let scale = if self.consume_token(&Token::Comma) {
2216                Some(self.parse_literal_uint()?)
2217            } else {
2218                None
2219            };
2220            self.expect_token(&Token::RParen)?;
2221            Ok((Some(n), scale))
2222        } else {
2223            Ok((None, None))
2224        }
2225    }
2226
2227    pub fn parse_delete(&mut self) -> Result<Delete, ParserError> {
2228        self.expect_keyword(Keyword::FROM)?;
2229        let table_name = self.parse_object_name()?;
2230        let selection = if self.parse_keyword(Keyword::WHERE) {
2231            Some(self.parse_expr()?)
2232        } else {
2233            None
2234        };
2235
2236        Ok(Delete {
2237            table_name,
2238            selection,
2239        })
2240    }
2241
2242    pub fn parse_explain(&mut self) -> Result<Explain, ParserError> {
2243        let analyze = self.parse_keyword(Keyword::ANALYZE);
2244        let verbose = self.parse_keyword(Keyword::VERBOSE);
2245
2246        let statement = Box::new(self.parse_statement()?);
2247
2248        Ok(Explain {
2249            analyze,
2250            verbose,
2251            statement,
2252        })
2253    }
2254
2255    /// Parse a query expression, i.e. a `SELECT` statement optionally
2256    /// preceeded with some `WITH` CTE declarations and optionally followed
2257    /// by `ORDER BY`. Unlike some other parse_... methods, this one doesn't
2258    /// expect the initial keyword to be already consumed
2259    pub fn parse_query(&mut self) -> Result<Query, ParserError> {
2260        let with = if self.parse_keyword(Keyword::WITH) {
2261            Some(With {
2262                recursive: self.parse_keyword(Keyword::RECURSIVE),
2263                cte_tables: self.parse_comma_separated(Parser::parse_cte)?,
2264            })
2265        } else {
2266            None
2267        };
2268
2269        if !self.parse_keyword(Keyword::INSERT) {
2270            let body = self.parse_query_body(0)?;
2271
2272            let order_by = if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
2273                self.parse_comma_separated(Parser::parse_order_by_expr)?
2274            } else {
2275                vec![]
2276            };
2277
2278            let limit = if self.parse_keyword(Keyword::LIMIT) {
2279                self.parse_limit()?
2280            } else {
2281                None
2282            };
2283
2284            let offset = if self.parse_keyword(Keyword::OFFSET) {
2285                Some(self.parse_offset()?)
2286            } else {
2287                None
2288            };
2289
2290            let fetch = if self.parse_keyword(Keyword::FETCH) {
2291                Some(self.parse_fetch()?)
2292            } else {
2293                None
2294            };
2295
2296            Ok(Query {
2297                with,
2298                body,
2299                order_by,
2300                limit,
2301                offset,
2302                fetch,
2303            })
2304        } else {
2305            let insert = self.parse_insert()?;
2306            Ok(Query {
2307                with,
2308                body: SetExpr::Insert(insert),
2309                limit: None,
2310                order_by: vec![],
2311                offset: None,
2312                fetch: None,
2313            })
2314        }
2315    }
2316
2317    /// Parse a CTE (`alias [( col1, col2, ... )] AS (subquery)`)
2318    fn parse_cte(&mut self) -> Result<Cte, ParserError> {
2319        let name = self.parse_identifier()?;
2320
2321        let mut cte = if self.parse_keyword(Keyword::AS) {
2322            self.expect_token(&Token::LParen)?;
2323            let query = self.parse_query()?;
2324            self.expect_token(&Token::RParen)?;
2325            let alias = TableAlias {
2326                name,
2327                columns: vec![],
2328            };
2329            Cte {
2330                alias,
2331                query,
2332                from: None,
2333            }
2334        } else {
2335            let columns = self.parse_parenthesized_column_list(Optional)?;
2336            self.expect_keyword(Keyword::AS)?;
2337            self.expect_token(&Token::LParen)?;
2338            let query = self.parse_query()?;
2339            self.expect_token(&Token::RParen)?;
2340            let alias = TableAlias { name, columns };
2341            Cte {
2342                alias,
2343                query,
2344                from: None,
2345            }
2346        };
2347        if self.parse_keyword(Keyword::FROM) {
2348            cte.from = Some(self.parse_identifier()?);
2349        }
2350        Ok(cte)
2351    }
2352
2353    /// Parse a "query body", which is an expression with roughly the
2354    /// following grammar:
2355    /// ```text
2356    ///   query_body ::= restricted_select | '(' subquery ')' | set_operation
2357    ///   restricted_select ::= 'SELECT' [expr_list] [ from ] [ where ] [ groupby_having ]
2358    ///   subquery ::= query_body [ order_by_limit ]
2359    ///   set_operation ::= query_body { 'UNION' | 'EXCEPT' | 'INTERSECT' } [ 'ALL' ] query_body
2360    /// ```
2361    fn parse_query_body(&mut self, precedence: u8) -> Result<SetExpr, ParserError> {
2362        // We parse the expression using a Pratt parser, as in `parse_expr()`.
2363        // Start by parsing a restricted SELECT or a `(subquery)`:
2364        let mut expr = if self.parse_keyword(Keyword::SELECT) {
2365            SetExpr::Select(Box::new(self.parse_select()?))
2366        } else if self.consume_token(&Token::LParen) {
2367            // CTEs are not allowed here, but the parser currently accepts them
2368            let subquery = self.parse_query()?;
2369            self.expect_token(&Token::RParen)?;
2370            SetExpr::Query(Box::new(subquery))
2371        } else if self.parse_keyword(Keyword::VALUES) {
2372            SetExpr::Values(self.parse_values()?)
2373        } else {
2374            return self.expected(
2375                "SELECT, VALUES, or a subquery in the query body",
2376                self.peek_token(),
2377            );
2378        };
2379
2380        loop {
2381            // The query can be optionally followed by a set operator:
2382            let op = self.parse_set_operator(&self.peek_token());
2383            let next_precedence = match op {
2384                // UNION and EXCEPT have the same binding power and evaluate left-to-right
2385                Some(SetOperator::Union) | Some(SetOperator::Except) => 10,
2386                // INTERSECT has higher precedence than UNION/EXCEPT
2387                Some(SetOperator::Intersect) => 20,
2388                // Unexpected token or EOF => stop parsing the query body
2389                None => break,
2390            };
2391            if precedence >= next_precedence {
2392                break;
2393            }
2394            self.next_token(); // skip past the set operator
2395            expr = SetExpr::SetOperation {
2396                left: Box::new(expr),
2397                op: op.unwrap(),
2398                all: self.parse_keyword(Keyword::ALL),
2399                right: Box::new(self.parse_query_body(next_precedence)?),
2400            };
2401        }
2402
2403        Ok(expr)
2404    }
2405
2406    fn parse_set_operator(&mut self, token: &Token) -> Option<SetOperator> {
2407        match token {
2408            Token::Word(w) if w.keyword == Keyword::UNION => Some(SetOperator::Union),
2409            Token::Word(w) if w.keyword == Keyword::EXCEPT => Some(SetOperator::Except),
2410            Token::Word(w) if w.keyword == Keyword::INTERSECT => Some(SetOperator::Intersect),
2411            _ => None,
2412        }
2413    }
2414
2415    /// Parse a restricted `SELECT` statement (no CTEs / `UNION` / `ORDER BY`),
2416    /// assuming the initial `SELECT` was already consumed
2417    pub fn parse_select(&mut self) -> Result<Select, ParserError> {
2418        let distinct = self.parse_all_or_distinct()?;
2419
2420        let top = if self.parse_keyword(Keyword::TOP) {
2421            Some(self.parse_top()?)
2422        } else {
2423            None
2424        };
2425
2426        let projection = self.parse_comma_separated(Parser::parse_select_item)?;
2427
2428        // Note that for keywords to be properly handled here, they need to be
2429        // added to `RESERVED_FOR_COLUMN_ALIAS` / `RESERVED_FOR_TABLE_ALIAS`,
2430        // otherwise they may be parsed as an alias as part of the `projection`
2431        // or `from`.
2432
2433        let from = if self.parse_keyword(Keyword::FROM) {
2434            self.parse_comma_separated(Parser::parse_table_and_joins)?
2435        } else {
2436            vec![]
2437        };
2438        let mut lateral_views = vec![];
2439        loop {
2440            if self.parse_keywords(&[Keyword::LATERAL, Keyword::VIEW]) {
2441                let outer = self.parse_keyword(Keyword::OUTER);
2442                let lateral_view = self.parse_expr()?;
2443                let lateral_view_name = self.parse_object_name()?;
2444                let lateral_col_alias = self
2445                    .parse_comma_separated(|parser| {
2446                        parser.parse_optional_alias(&[
2447                            Keyword::WHERE,
2448                            Keyword::GROUP,
2449                            Keyword::CLUSTER,
2450                            Keyword::HAVING,
2451                            Keyword::LATERAL,
2452                        ]) // This couldn't possibly be a bad idea
2453                    })?
2454                    .into_iter()
2455                    .flatten()
2456                    .collect();
2457
2458                lateral_views.push(LateralView {
2459                    lateral_view,
2460                    lateral_view_name,
2461                    lateral_col_alias,
2462                    outer,
2463                });
2464            } else {
2465                break;
2466            }
2467        }
2468
2469        let selection = if self.parse_keyword(Keyword::WHERE) {
2470            Some(self.parse_expr()?)
2471        } else {
2472            None
2473        };
2474
2475        let group_by = if self.parse_keywords(&[Keyword::GROUP, Keyword::BY]) {
2476            self.parse_comma_separated(Parser::parse_expr)?
2477        } else {
2478            vec![]
2479        };
2480
2481        let cluster_by = if self.parse_keywords(&[Keyword::CLUSTER, Keyword::BY]) {
2482            self.parse_comma_separated(Parser::parse_expr)?
2483        } else {
2484            vec![]
2485        };
2486
2487        let distribute_by = if self.parse_keywords(&[Keyword::DISTRIBUTE, Keyword::BY]) {
2488            self.parse_comma_separated(Parser::parse_expr)?
2489        } else {
2490            vec![]
2491        };
2492
2493        let sort_by = if self.parse_keywords(&[Keyword::SORT, Keyword::BY]) {
2494            self.parse_comma_separated(Parser::parse_expr)?
2495        } else {
2496            vec![]
2497        };
2498
2499        let having = if self.parse_keyword(Keyword::HAVING) {
2500            Some(self.parse_expr()?)
2501        } else {
2502            None
2503        };
2504
2505        Ok(Select {
2506            distinct,
2507            top,
2508            projection,
2509            from,
2510            lateral_views,
2511            selection,
2512            group_by,
2513            cluster_by,
2514            distribute_by,
2515            sort_by,
2516            having,
2517        })
2518    }
2519
2520    pub fn parse_set(&mut self) -> Result<Statement, ParserError> {
2521        let modifier =
2522            self.parse_one_of_keywords(&[Keyword::SESSION, Keyword::LOCAL, Keyword::HIVEVAR]);
2523        if let Some(Keyword::HIVEVAR) = modifier {
2524            self.expect_token(&Token::Colon)?;
2525        }
2526        let variable = self.parse_identifier()?;
2527        if self.consume_token(&Token::Eq) || self.parse_keyword(Keyword::TO) {
2528            let mut values = vec![];
2529            loop {
2530                let token = self.peek_token();
2531                let value = match (self.parse_value(), token) {
2532                    (Ok(value), _) => SetVariableValue::Literal(value),
2533                    (Err(_), Token::Word(ident)) => SetVariableValue::Ident(ident.to_ident()),
2534                    (Err(_), unexpected) => self.expected("variable value", unexpected)?,
2535                };
2536                values.push(value);
2537                if self.consume_token(&Token::Comma) {
2538                    continue;
2539                }
2540                return Ok(Statement::SetVariable(SetVariable {
2541                    local: modifier == Some(Keyword::LOCAL),
2542                    hivevar: Some(Keyword::HIVEVAR) == modifier,
2543                    variable,
2544                    value: values,
2545                }));
2546            }
2547        } else if variable.value == "TRANSACTION" && modifier.is_none() {
2548            Ok(Statement::SetTransaction(SetTransaction {
2549                modes: self.parse_transaction_modes()?,
2550            }))
2551        } else {
2552            self.expected("equals sign or TO", self.peek_token())
2553        }
2554    }
2555
2556    pub fn parse_show(&mut self) -> Result<Statement, ParserError> {
2557        if self
2558            .parse_one_of_keywords(&[
2559                Keyword::EXTENDED,
2560                Keyword::FULL,
2561                Keyword::COLUMNS,
2562                Keyword::FIELDS,
2563            ])
2564            .is_some()
2565        {
2566            self.prev_token();
2567            Ok(Statement::ShowColumns(self.parse_show_columns()?))
2568        } else if self.parse_one_of_keywords(&[Keyword::CREATE]).is_some() {
2569            Ok(Statement::ShowCreate(self.parse_show_create()?))
2570        } else {
2571            Ok(Statement::ShowVariable(ShowVariable {
2572                variable: self.parse_identifiers()?,
2573            }))
2574        }
2575    }
2576
2577    fn parse_show_create(&mut self) -> Result<ShowCreate, ParserError> {
2578        let obj_type = match self.expect_one_of_keywords(&[
2579            Keyword::TABLE,
2580            Keyword::TRIGGER,
2581            Keyword::FUNCTION,
2582            Keyword::PROCEDURE,
2583            Keyword::EVENT,
2584        ])? {
2585            Keyword::TABLE => Ok(ShowCreateObject::Table),
2586            Keyword::TRIGGER => Ok(ShowCreateObject::Trigger),
2587            Keyword::FUNCTION => Ok(ShowCreateObject::Function),
2588            Keyword::PROCEDURE => Ok(ShowCreateObject::Procedure),
2589            Keyword::EVENT => Ok(ShowCreateObject::Event),
2590            keyword => Err(ParserError::ParserError(format!(
2591                "Unable to map keyword to ShowCreateObject: {:?}",
2592                keyword
2593            ))),
2594        }?;
2595
2596        let obj_name = self.parse_object_name()?;
2597
2598        Ok(ShowCreate { obj_type, obj_name })
2599    }
2600
2601    fn parse_show_columns(&mut self) -> Result<ShowColumns, ParserError> {
2602        let extended = self.parse_keyword(Keyword::EXTENDED);
2603        let full = self.parse_keyword(Keyword::FULL);
2604        self.expect_one_of_keywords(&[Keyword::COLUMNS, Keyword::FIELDS])?;
2605        self.expect_one_of_keywords(&[Keyword::FROM, Keyword::IN])?;
2606        let table_name = self.parse_object_name()?;
2607        // MySQL also supports FROM <database> here. In other words, MySQL
2608        // allows both FROM <table> FROM <database> and FROM <database>.<table>,
2609        // while we only support the latter for now.
2610        let filter = self.parse_show_statement_filter()?;
2611        Ok(ShowColumns {
2612            extended,
2613            full,
2614            table_name,
2615            filter,
2616        })
2617    }
2618
2619    fn parse_show_statement_filter(&mut self) -> Result<Option<ShowStatementFilter>, ParserError> {
2620        if self.parse_keyword(Keyword::LIKE) {
2621            Ok(Some(ShowStatementFilter::Like(
2622                self.parse_literal_string()?,
2623            )))
2624        } else if self.parse_keyword(Keyword::ILIKE) {
2625            Ok(Some(ShowStatementFilter::ILike(
2626                self.parse_literal_string()?,
2627            )))
2628        } else if self.parse_keyword(Keyword::WHERE) {
2629            Ok(Some(ShowStatementFilter::Where(self.parse_expr()?)))
2630        } else {
2631            Ok(None)
2632        }
2633    }
2634
2635    pub fn parse_table_and_joins(&mut self) -> Result<TableWithJoins, ParserError> {
2636        let relation = self.parse_table_factor()?;
2637
2638        // Note that for keywords to be properly handled here, they need to be
2639        // added to `RESERVED_FOR_TABLE_ALIAS`, otherwise they may be parsed as
2640        // a table alias.
2641        let mut joins = vec![];
2642        loop {
2643            let join = if self.parse_keyword(Keyword::CROSS) {
2644                let join_operator = if self.parse_keyword(Keyword::JOIN) {
2645                    JoinOperator::CrossJoin
2646                } else if self.parse_keyword(Keyword::APPLY) {
2647                    // MSSQL extension, similar to CROSS JOIN LATERAL
2648                    JoinOperator::CrossApply
2649                } else {
2650                    return self.expected("JOIN or APPLY after CROSS", self.peek_token());
2651                };
2652                Join {
2653                    relation: self.parse_table_factor()?,
2654                    join_operator,
2655                }
2656            } else if self.parse_keyword(Keyword::OUTER) {
2657                // MSSQL extension, similar to LEFT JOIN LATERAL .. ON 1=1
2658                self.expect_keyword(Keyword::APPLY)?;
2659                Join {
2660                    relation: self.parse_table_factor()?,
2661                    join_operator: JoinOperator::OuterApply,
2662                }
2663            } else {
2664                let natural = self.parse_keyword(Keyword::NATURAL);
2665                let peek_keyword = if let Token::Word(w) = self.peek_token() {
2666                    w.keyword
2667                } else {
2668                    Keyword::NoKeyword
2669                };
2670
2671                let join_operator_type = match peek_keyword {
2672                    Keyword::INNER | Keyword::JOIN => {
2673                        let _ = self.parse_keyword(Keyword::INNER);
2674                        self.expect_keyword(Keyword::JOIN)?;
2675                        JoinOperator::Inner
2676                    }
2677                    kw @ Keyword::LEFT | kw @ Keyword::RIGHT | kw @ Keyword::FULL => {
2678                        let _ = self.next_token();
2679                        let _ = self.parse_keyword(Keyword::OUTER);
2680                        self.expect_keyword(Keyword::JOIN)?;
2681                        match kw {
2682                            Keyword::LEFT => JoinOperator::LeftOuter,
2683                            Keyword::RIGHT => JoinOperator::RightOuter,
2684                            Keyword::FULL => JoinOperator::FullOuter,
2685                            _ => unreachable!(),
2686                        }
2687                    }
2688                    Keyword::OUTER => {
2689                        return self.expected("LEFT, RIGHT, or FULL", self.peek_token());
2690                    }
2691                    _ if natural => {
2692                        return self.expected("a join type after NATURAL", self.peek_token());
2693                    }
2694                    _ => break,
2695                };
2696                let relation = self.parse_table_factor()?;
2697                let join_constraint = self.parse_join_constraint(natural)?;
2698                Join {
2699                    relation,
2700                    join_operator: join_operator_type(join_constraint),
2701                }
2702            };
2703            joins.push(join);
2704        }
2705        Ok(TableWithJoins { relation, joins })
2706    }
2707
2708    /// A table name or a parenthesized subquery, followed by optional `[AS] alias`
2709    pub fn parse_table_factor(&mut self) -> Result<TableFactor, ParserError> {
2710        if self.parse_keyword(Keyword::LATERAL) {
2711            // LATERAL must always be followed by a subquery.
2712            if !self.consume_token(&Token::LParen) {
2713                self.expected("subquery after LATERAL", self.peek_token())?;
2714            }
2715            self.parse_derived_table_factor(Lateral)
2716        } else if self.parse_keyword(Keyword::TABLE) {
2717            // parse table function (SELECT * FROM TABLE (<expr>) [ AS <alias> ])
2718            self.expect_token(&Token::LParen)?;
2719            let expr = self.parse_expr()?;
2720            self.expect_token(&Token::RParen)?;
2721            let alias = self.parse_optional_table_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?;
2722            Ok(TableFactor::TableFunction { expr, alias })
2723        } else if self.consume_token(&Token::LParen) {
2724            // A left paren introduces either a derived table (i.e., a subquery)
2725            // or a nested join. It's nearly impossible to determine ahead of
2726            // time which it is... so we just try to parse both.
2727            //
2728            // Here's an example that demonstrates the complexity:
2729            //                     /-------------------------------------------------------\
2730            //                     | /-----------------------------------\                 |
2731            //     SELECT * FROM ( ( ( (SELECT 1) UNION (SELECT 2) ) AS t1 NATURAL JOIN t2 ) )
2732            //                   ^ ^ ^ ^
2733            //                   | | | |
2734            //                   | | | |
2735            //                   | | | (4) belongs to a SetExpr::Query inside the subquery
2736            //                   | | (3) starts a derived table (subquery)
2737            //                   | (2) starts a nested join
2738            //                   (1) an additional set of parens around a nested join
2739            //
2740
2741            // If the recently consumed '(' starts a derived table, the call to
2742            // `parse_derived_table_factor` below will return success after parsing the
2743            // subquery, followed by the closing ')', and the alias of the derived table.
2744            // In the example above this is case (3).
2745            return_ok_if_some!(
2746                self.maybe_parse(|parser| parser.parse_derived_table_factor(NotLateral))
2747            );
2748            // A parsing error from `parse_derived_table_factor` indicates that the '(' we've
2749            // recently consumed does not start a derived table (cases 1, 2, or 4).
2750            // `maybe_parse` will ignore such an error and rewind to be after the opening '('.
2751
2752            // Inside the parentheses we expect to find an (A) table factor
2753            // followed by some joins or (B) another level of nesting.
2754            let mut table_and_joins = self.parse_table_and_joins()?;
2755
2756            if !table_and_joins.joins.is_empty() {
2757                self.expect_token(&Token::RParen)?;
2758                Ok(TableFactor::NestedJoin(Box::new(table_and_joins))) // (A)
2759            } else if let TableFactor::NestedJoin(_) = &table_and_joins.relation {
2760                // (B): `table_and_joins` (what we found inside the parentheses)
2761                // is a nested join `(foo JOIN bar)`, not followed by other joins.
2762                self.expect_token(&Token::RParen)?;
2763                Ok(TableFactor::NestedJoin(Box::new(table_and_joins)))
2764            } else if dialect_of!(self is SnowflakeDialect | GenericDialect) {
2765                // Dialect-specific behavior: Snowflake diverges from the
2766                // standard and from most of the other implementations by
2767                // allowing extra parentheses not only around a join (B), but
2768                // around lone table names (e.g. `FROM (mytable [AS alias])`)
2769                // and around derived tables (e.g. `FROM ((SELECT ...)
2770                // [AS alias])`) as well.
2771                self.expect_token(&Token::RParen)?;
2772
2773                if let Some(outer_alias) =
2774                    self.parse_optional_table_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?
2775                {
2776                    // Snowflake also allows specifying an alias *after* parens
2777                    // e.g. `FROM (mytable) AS alias`
2778                    match &mut table_and_joins.relation {
2779                        TableFactor::Derived { alias, .. }
2780                        | TableFactor::Table { alias, .. }
2781                        | TableFactor::TableFunction { alias, .. } => {
2782                            // but not `FROM (mytable AS alias1) AS alias2`.
2783                            if let Some(inner_alias) = alias {
2784                                return Err(ParserError::ParserError(format!(
2785                                    "duplicate alias {}",
2786                                    inner_alias
2787                                )));
2788                            }
2789                            // Act as if the alias was specified normally next
2790                            // to the table name: `(mytable) AS alias` ->
2791                            // `(mytable AS alias)`
2792                            alias.replace(outer_alias);
2793                        }
2794                        TableFactor::NestedJoin(_) => unreachable!(),
2795                    };
2796                }
2797                // Do not store the extra set of parens in the AST
2798                Ok(table_and_joins.relation)
2799            } else {
2800                // The SQL spec prohibits derived tables and bare tables from
2801                // appearing alone in parentheses (e.g. `FROM (mytable)`)
2802                self.expected("joined table", self.peek_token())
2803            }
2804        } else {
2805            let name = self.parse_object_name()?;
2806            // Postgres, MSSQL: table-valued functions:
2807            let args = if self.consume_token(&Token::LParen) {
2808                self.parse_optional_args()?
2809            } else {
2810                vec![]
2811            };
2812            let alias = self.parse_optional_table_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?;
2813            // MSSQL-specific table hints:
2814            let mut with_hints = vec![];
2815            if self.parse_keyword(Keyword::WITH) {
2816                if self.consume_token(&Token::LParen) {
2817                    with_hints = self.parse_comma_separated(Parser::parse_expr)?;
2818                    self.expect_token(&Token::RParen)?;
2819                } else {
2820                    // rewind, as WITH may belong to the next statement's CTE
2821                    self.prev_token();
2822                }
2823            };
2824            Ok(TableFactor::Table {
2825                name,
2826                alias,
2827                args,
2828                with_hints,
2829            })
2830        }
2831    }
2832
2833    pub fn parse_derived_table_factor(
2834        &mut self,
2835        lateral: IsLateral,
2836    ) -> Result<TableFactor, ParserError> {
2837        let subquery = Box::new(self.parse_query()?);
2838        self.expect_token(&Token::RParen)?;
2839        let alias = self.parse_optional_table_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?;
2840        Ok(TableFactor::Derived {
2841            lateral: match lateral {
2842                Lateral => true,
2843                NotLateral => false,
2844            },
2845            subquery,
2846            alias,
2847        })
2848    }
2849
2850    fn parse_join_constraint(&mut self, natural: bool) -> Result<JoinConstraint, ParserError> {
2851        if natural {
2852            Ok(JoinConstraint::Natural)
2853        } else if self.parse_keyword(Keyword::ON) {
2854            let constraint = self.parse_expr()?;
2855            Ok(JoinConstraint::On(constraint))
2856        } else if self.parse_keyword(Keyword::USING) {
2857            let columns = self.parse_parenthesized_column_list(Mandatory)?;
2858            Ok(JoinConstraint::Using(columns))
2859        } else {
2860            Ok(JoinConstraint::None)
2861            //self.expected("ON, or USING after JOIN", self.peek_token())
2862        }
2863    }
2864
2865    /// Parse an INSERT statement
2866    pub fn parse_insert(&mut self) -> Result<Statement, ParserError> {
2867        let or = if !dialect_of!(self is SQLiteDialect) {
2868            None
2869        } else if self.parse_keywords(&[Keyword::OR, Keyword::REPLACE]) {
2870            Some(SqliteOnConflict::Replace)
2871        } else if self.parse_keywords(&[Keyword::OR, Keyword::ROLLBACK]) {
2872            Some(SqliteOnConflict::Rollback)
2873        } else if self.parse_keywords(&[Keyword::OR, Keyword::ABORT]) {
2874            Some(SqliteOnConflict::Abort)
2875        } else if self.parse_keywords(&[Keyword::OR, Keyword::FAIL]) {
2876            Some(SqliteOnConflict::Fail)
2877        } else if self.parse_keywords(&[Keyword::OR, Keyword::IGNORE]) {
2878            Some(SqliteOnConflict::Ignore)
2879        } else if self.parse_keyword(Keyword::REPLACE) {
2880            Some(SqliteOnConflict::Replace)
2881        } else {
2882            None
2883        };
2884        let action = self.expect_one_of_keywords(&[Keyword::INTO, Keyword::OVERWRITE])?;
2885        let overwrite = action == Keyword::OVERWRITE;
2886        let local = self.parse_keyword(Keyword::LOCAL);
2887
2888        if self.parse_keyword(Keyword::DIRECTORY) {
2889            let path = self.parse_literal_string()?;
2890            let file_format = if self.parse_keywords(&[Keyword::STORED, Keyword::AS]) {
2891                Some(self.parse_file_format()?)
2892            } else {
2893                None
2894            };
2895            let source = Box::new(self.parse_query()?);
2896            Ok(Statement::Directory(Directory {
2897                local,
2898                path,
2899                overwrite,
2900                file_format,
2901                source,
2902            }))
2903        } else {
2904            // Hive lets you put table here regardless
2905            let table = self.parse_keyword(Keyword::TABLE);
2906            let table_name = self.parse_object_name()?;
2907            let columns = self.parse_parenthesized_column_list(Optional)?;
2908
2909            let partitioned = if self.parse_keyword(Keyword::PARTITION) {
2910                self.expect_token(&Token::LParen)?;
2911                let r = Some(self.parse_comma_separated(Parser::parse_expr)?);
2912                self.expect_token(&Token::RParen)?;
2913                r
2914            } else {
2915                None
2916            };
2917
2918            // Hive allows you to specify columns after partitions as well if you want.
2919            let after_columns = self.parse_parenthesized_column_list(Optional)?;
2920
2921            let source = Box::new(self.parse_query()?);
2922            Ok(Statement::Insert(Insert {
2923                or,
2924                table_name,
2925                overwrite,
2926                partitioned,
2927                columns,
2928                after_columns,
2929                source,
2930                table,
2931            }))
2932        }
2933    }
2934
2935    pub fn parse_update(&mut self) -> Result<Update, ParserError> {
2936        let table_name = self.parse_object_name()?;
2937        self.expect_keyword(Keyword::SET)?;
2938        let assignments = self.parse_comma_separated(Parser::parse_assignment)?;
2939        let selection = if self.parse_keyword(Keyword::WHERE) {
2940            Some(self.parse_expr()?)
2941        } else {
2942            None
2943        };
2944        Ok(Update {
2945            table_name,
2946            assignments,
2947            selection,
2948        })
2949    }
2950
2951    /// Parse a `var = expr` assignment, used in an UPDATE statement
2952    pub fn parse_assignment(&mut self) -> Result<Assignment, ParserError> {
2953        let id = self.parse_identifier()?;
2954        self.expect_token(&Token::Eq)?;
2955        let value = self.parse_expr()?;
2956        Ok(Assignment { id, value })
2957    }
2958
2959    fn parse_function_args(&mut self) -> Result<FunctionArg, ParserError> {
2960        if self.peek_nth_token(1) == Token::RArrow {
2961            let name = self.parse_identifier()?;
2962
2963            self.expect_token(&Token::RArrow)?;
2964            let arg = self.parse_expr()?;
2965
2966            Ok(FunctionArg::Named { name, arg })
2967        } else {
2968            Ok(FunctionArg::Unnamed(self.parse_expr()?))
2969        }
2970    }
2971
2972    pub fn parse_optional_args(&mut self) -> Result<Vec<FunctionArg>, ParserError> {
2973        if self.consume_token(&Token::RParen) {
2974            Ok(vec![])
2975        } else {
2976            let args = self.parse_comma_separated(Parser::parse_function_args)?;
2977            self.expect_token(&Token::RParen)?;
2978            Ok(args)
2979        }
2980    }
2981
2982    /// Parse a comma-delimited list of projections after SELECT
2983    pub fn parse_select_item(&mut self) -> Result<SelectItem, ParserError> {
2984        let expr = self.parse_expr()?;
2985        if let Expr::Wildcard = expr {
2986            Ok(SelectItem::Wildcard)
2987        } else if let Expr::QualifiedWildcard(prefix) = expr {
2988            Ok(SelectItem::QualifiedWildcard(ObjectName(prefix)))
2989        } else {
2990            // `expr` is a regular SQL expression and can be followed by an alias
2991            if let Some(alias) = self.parse_optional_alias(keywords::RESERVED_FOR_COLUMN_ALIAS)? {
2992                Ok(SelectItem::ExprWithAlias { expr, alias })
2993            } else {
2994                Ok(SelectItem::UnnamedExpr(expr))
2995            }
2996        }
2997    }
2998
2999    /// Parse an expression, optionally followed by ASC or DESC (used in ORDER BY)
3000    pub fn parse_order_by_expr(&mut self) -> Result<OrderByExpr, ParserError> {
3001        let expr = self.parse_expr()?;
3002
3003        let asc = if self.parse_keyword(Keyword::ASC) {
3004            Some(true)
3005        } else if self.parse_keyword(Keyword::DESC) {
3006            Some(false)
3007        } else {
3008            None
3009        };
3010
3011        let nulls_first = if self.parse_keywords(&[Keyword::NULLS, Keyword::FIRST]) {
3012            Some(true)
3013        } else if self.parse_keywords(&[Keyword::NULLS, Keyword::LAST]) {
3014            Some(false)
3015        } else {
3016            None
3017        };
3018
3019        Ok(OrderByExpr {
3020            expr,
3021            asc,
3022            nulls_first,
3023        })
3024    }
3025
3026    /// Parse a TOP clause, MSSQL equivalent of LIMIT,
3027    /// that follows after SELECT [DISTINCT].
3028    pub fn parse_top(&mut self) -> Result<Top, ParserError> {
3029        let quantity = if self.consume_token(&Token::LParen) {
3030            let quantity = self.parse_expr()?;
3031            self.expect_token(&Token::RParen)?;
3032            Some(quantity)
3033        } else {
3034            Some(Expr::Value(self.parse_number_value()?))
3035        };
3036
3037        let percent = self.parse_keyword(Keyword::PERCENT);
3038
3039        let with_ties = self.parse_keywords(&[Keyword::WITH, Keyword::TIES]);
3040
3041        Ok(Top {
3042            with_ties,
3043            percent,
3044            quantity,
3045        })
3046    }
3047
3048    /// Parse a LIMIT clause
3049    pub fn parse_limit(&mut self) -> Result<Option<Expr>, ParserError> {
3050        if self.parse_keyword(Keyword::ALL) {
3051            Ok(None)
3052        } else {
3053            Ok(Some(Expr::Value(self.parse_number_value()?)))
3054        }
3055    }
3056
3057    /// Parse an OFFSET clause
3058    pub fn parse_offset(&mut self) -> Result<Offset, ParserError> {
3059        let value = Expr::Value(self.parse_number_value()?);
3060        let rows = if self.parse_keyword(Keyword::ROW) {
3061            OffsetRows::Row
3062        } else if self.parse_keyword(Keyword::ROWS) {
3063            OffsetRows::Rows
3064        } else {
3065            OffsetRows::None
3066        };
3067        Ok(Offset { value, rows })
3068    }
3069
3070    /// Parse a FETCH clause
3071    pub fn parse_fetch(&mut self) -> Result<Fetch, ParserError> {
3072        self.expect_one_of_keywords(&[Keyword::FIRST, Keyword::NEXT])?;
3073        let (quantity, percent) = if self
3074            .parse_one_of_keywords(&[Keyword::ROW, Keyword::ROWS])
3075            .is_some()
3076        {
3077            (None, false)
3078        } else {
3079            let quantity = Expr::Value(self.parse_value()?);
3080            let percent = self.parse_keyword(Keyword::PERCENT);
3081            self.expect_one_of_keywords(&[Keyword::ROW, Keyword::ROWS])?;
3082            (Some(quantity), percent)
3083        };
3084        let with_ties = if self.parse_keyword(Keyword::ONLY) {
3085            false
3086        } else if self.parse_keywords(&[Keyword::WITH, Keyword::TIES]) {
3087            true
3088        } else {
3089            return self.expected("one of ONLY or WITH TIES", self.peek_token());
3090        };
3091        Ok(Fetch {
3092            with_ties,
3093            percent,
3094            quantity,
3095        })
3096    }
3097
3098    pub fn parse_values(&mut self) -> Result<Values, ParserError> {
3099        let values = self.parse_comma_separated(|parser| {
3100            parser.expect_token(&Token::LParen)?;
3101            let exprs = parser.parse_comma_separated(Parser::parse_expr)?;
3102            parser.expect_token(&Token::RParen)?;
3103            Ok(exprs)
3104        })?;
3105        Ok(Values(values))
3106    }
3107
3108    pub fn parse_start_transaction(&mut self) -> Result<StartTransaction, ParserError> {
3109        self.expect_keyword(Keyword::TRANSACTION)?;
3110        Ok(StartTransaction {
3111            modes: self.parse_transaction_modes()?,
3112        })
3113    }
3114
3115    pub fn parse_begin(&mut self) -> Result<StartTransaction, ParserError> {
3116        let _ = self.parse_one_of_keywords(&[Keyword::TRANSACTION, Keyword::WORK]);
3117        Ok(StartTransaction {
3118            modes: self.parse_transaction_modes()?,
3119        })
3120    }
3121
3122    pub fn parse_transaction_modes(&mut self) -> Result<Vec<TransactionMode>, ParserError> {
3123        let mut modes = vec![];
3124        let mut required = false;
3125        loop {
3126            let mode = if self.parse_keywords(&[Keyword::ISOLATION, Keyword::LEVEL]) {
3127                let iso_level = if self.parse_keywords(&[Keyword::READ, Keyword::UNCOMMITTED]) {
3128                    TransactionIsolationLevel::ReadUncommitted
3129                } else if self.parse_keywords(&[Keyword::READ, Keyword::COMMITTED]) {
3130                    TransactionIsolationLevel::ReadCommitted
3131                } else if self.parse_keywords(&[Keyword::REPEATABLE, Keyword::READ]) {
3132                    TransactionIsolationLevel::RepeatableRead
3133                } else if self.parse_keyword(Keyword::SERIALIZABLE) {
3134                    TransactionIsolationLevel::Serializable
3135                } else {
3136                    self.expected("isolation level", self.peek_token())?
3137                };
3138                TransactionMode::IsolationLevel(iso_level)
3139            } else if self.parse_keywords(&[Keyword::READ, Keyword::ONLY]) {
3140                TransactionMode::AccessMode(TransactionAccessMode::ReadOnly)
3141            } else if self.parse_keywords(&[Keyword::READ, Keyword::WRITE]) {
3142                TransactionMode::AccessMode(TransactionAccessMode::ReadWrite)
3143            } else if required {
3144                self.expected("transaction mode", self.peek_token())?
3145            } else {
3146                break;
3147            };
3148            modes.push(mode);
3149            // ANSI requires a comma after each transaction mode, but
3150            // PostgreSQL, for historical reasons, does not. We follow
3151            // PostgreSQL in making the comma optional, since that is strictly
3152            // more general.
3153            required = self.consume_token(&Token::Comma);
3154        }
3155        Ok(modes)
3156    }
3157
3158    pub fn parse_commit(&mut self) -> Result<Commit, ParserError> {
3159        Ok(Commit {
3160            chain: self.parse_commit_rollback_chain()?,
3161        })
3162    }
3163
3164    pub fn parse_rollback(&mut self) -> Result<Rollback, ParserError> {
3165        Ok(Rollback {
3166            chain: self.parse_commit_rollback_chain()?,
3167        })
3168    }
3169
3170    pub fn parse_commit_rollback_chain(&mut self) -> Result<bool, ParserError> {
3171        let _ = self.parse_one_of_keywords(&[Keyword::TRANSACTION, Keyword::WORK]);
3172        if self.parse_keyword(Keyword::AND) {
3173            let chain = !self.parse_keyword(Keyword::NO);
3174            self.expect_keyword(Keyword::CHAIN)?;
3175            Ok(chain)
3176        } else {
3177            Ok(false)
3178        }
3179    }
3180
3181    fn parse_deallocate(&mut self) -> Result<Deallocate, ParserError> {
3182        let prepare = self.parse_keyword(Keyword::PREPARE);
3183        let name = self.parse_identifier()?;
3184        Ok(Deallocate { name, prepare })
3185    }
3186
3187    fn parse_execute(&mut self) -> Result<Execute, ParserError> {
3188        let name = self.parse_identifier()?;
3189
3190        let mut parameters = vec![];
3191        if self.consume_token(&Token::LParen) {
3192            parameters = self.parse_comma_separated(Parser::parse_expr)?;
3193            self.expect_token(&Token::RParen)?;
3194        }
3195
3196        Ok(Execute { name, parameters })
3197    }
3198
3199    fn parse_prepare(&mut self) -> Result<Prepare, ParserError> {
3200        let name = self.parse_identifier()?;
3201
3202        let mut data_types = vec![];
3203        if self.consume_token(&Token::LParen) {
3204            data_types = self.parse_comma_separated(Parser::parse_data_type)?;
3205            self.expect_token(&Token::RParen)?;
3206        }
3207
3208        self.expect_keyword(Keyword::AS)?;
3209        let statement = Box::new(self.parse_statement()?);
3210        Ok(Prepare {
3211            name,
3212            data_types,
3213            statement,
3214        })
3215    }
3216}
3217impl Word {
3218    pub fn to_ident(&self) -> Ident {
3219        Ident {
3220            value: self.value.clone(),
3221            quote_style: self.quote_style,
3222        }
3223    }
3224}
3225
3226#[cfg(test)]
3227mod tests {
3228    use super::*;
3229    use crate::test_utils::all_dialects;
3230
3231    #[test]
3232    fn test_prev_index() {
3233        let sql = "SELECT version";
3234        all_dialects().run_parser_method(sql, |parser| {
3235            assert_eq!(parser.peek_token(), Token::make_keyword("SELECT"));
3236            assert_eq!(parser.next_token(), Token::make_keyword("SELECT"));
3237            parser.prev_token();
3238            assert_eq!(parser.next_token(), Token::make_keyword("SELECT"));
3239            assert_eq!(parser.next_token(), Token::make_word("version", None));
3240            parser.prev_token();
3241            assert_eq!(parser.peek_token(), Token::make_word("version", None));
3242            assert_eq!(parser.next_token(), Token::make_word("version", None));
3243            assert_eq!(parser.peek_token(), Token::EOF);
3244            parser.prev_token();
3245            assert_eq!(parser.next_token(), Token::make_word("version", None));
3246            assert_eq!(parser.next_token(), Token::EOF);
3247            assert_eq!(parser.next_token(), Token::EOF);
3248            parser.prev_token();
3249        });
3250    }
3251}