sql_from_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            "{}",
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
2033                Keyword::TINYINT => Ok(DataType::TinyInt(self.parse_optional_precision()?)),
2034                Keyword::SMALLINT => Ok(DataType::SmallInt(self.parse_optional_precision()?)),
2035                Keyword::INT | Keyword::INTEGER => {
2036                    Ok(DataType::Int(self.parse_optional_precision()?))
2037                }
2038                Keyword::BIGINT => Ok(DataType::BigInt(self.parse_optional_precision()?)),
2039                Keyword::VARCHAR => Ok(DataType::Varchar(self.parse_optional_precision()?)),
2040                Keyword::VARBINARY => Ok(DataType::Varbinary(self.parse_optional_precision()?)),
2041                Keyword::BLOB => Ok(DataType::Blob(self.parse_optional_precision()?)),
2042                Keyword::CHAR | Keyword::CHARACTER => {
2043                    if self.parse_keyword(Keyword::VARYING) {
2044                        Ok(DataType::Varchar(self.parse_optional_precision()?))
2045                    } else {
2046                        Ok(DataType::Char(self.parse_optional_precision()?))
2047                    }
2048                }
2049                Keyword::UUID => Ok(DataType::Uuid),
2050                Keyword::DATE => Ok(DataType::Date),
2051                Keyword::TIMESTAMP => {
2052                    // TBD: we throw away "with/without timezone" information
2053                    if self.parse_keyword(Keyword::WITH) || self.parse_keyword(Keyword::WITHOUT) {
2054                        self.expect_keywords(&[Keyword::TIME, Keyword::ZONE])?;
2055                    }
2056                    Ok(DataType::Timestamp)
2057                }
2058                Keyword::TIME => {
2059                    // TBD: we throw away "with/without timezone" information
2060                    if self.parse_keyword(Keyword::WITH) || self.parse_keyword(Keyword::WITHOUT) {
2061                        self.expect_keywords(&[Keyword::TIME, Keyword::ZONE])?;
2062                    }
2063                    Ok(DataType::Time)
2064                }
2065                // Interval types can be followed by a complicated interval
2066                // qualifier that we don't currently support. See
2067                // parse_interval_literal for a taste.
2068                Keyword::INTERVAL => Ok(DataType::Interval),
2069                Keyword::REGCLASS => Ok(DataType::Regclass),
2070                Keyword::STRING => Ok(DataType::String),
2071                Keyword::TEXT => {
2072                    if self.consume_token(&Token::LBracket) {
2073                        // Note: this is postgresql-specific
2074                        self.expect_token(&Token::RBracket)?;
2075                        Ok(DataType::Array(Box::new(DataType::Text)))
2076                    } else {
2077                        Ok(DataType::Text)
2078                    }
2079                }
2080                Keyword::JSON => Ok(DataType::Json),
2081                Keyword::SERIAL => Ok(DataType::Serial),
2082                Keyword::BIGSERIAL => Ok(DataType::BigSerial),
2083                Keyword::BYTEA => Ok(DataType::Bytea),
2084                Keyword::NUMERIC | Keyword::DECIMAL | Keyword::DEC => {
2085                    let (precision, scale) = self.parse_optional_precision_scale()?;
2086                    Ok(DataType::Decimal(precision, scale))
2087                }
2088                _ => {
2089                    self.prev_token();
2090                    let type_name = self.parse_object_name()?;
2091                    Ok(DataType::Custom(type_name))
2092                }
2093            },
2094            unexpected => self.expected("a data type name", unexpected),
2095        }
2096    }
2097
2098    /// Parse `AS identifier` (or simply `identifier` if it's not a reserved keyword)
2099    /// Some examples with aliases: `SELECT 1 foo`, `SELECT COUNT(*) AS cnt`,
2100    /// `SELECT ... FROM t1 foo, t2 bar`, `SELECT ... FROM (...) AS bar`
2101    pub fn parse_optional_alias(
2102        &mut self,
2103        reserved_kwds: &[Keyword],
2104    ) -> Result<Option<Ident>, ParserError> {
2105        let after_as = self.parse_keyword(Keyword::AS);
2106        match self.next_token() {
2107            // Accept any identifier after `AS` (though many dialects have restrictions on
2108            // keywords that may appear here). If there's no `AS`: don't parse keywords,
2109            // which may start a construct allowed in this position, to be parsed as aliases.
2110            // (For example, in `FROM t1 JOIN` the `JOIN` will always be parsed as a keyword,
2111            // not an alias.)
2112            Token::Word(w) if after_as || !reserved_kwds.contains(&w.keyword) => {
2113                Ok(Some(w.to_ident()))
2114            }
2115            // MSSQL supports single-quoted strings as aliases for columns
2116            // We accept them as table aliases too, although MSSQL does not.
2117            //
2118            // Note, that this conflicts with an obscure rule from the SQL
2119            // standard, which we don't implement:
2120            // https://crate.io/docs/sql-99/en/latest/chapters/07.html#character-string-literal-s
2121            //    "[Obscure Rule] SQL allows you to break a long <character
2122            //    string literal> up into two or more smaller <character string
2123            //    literal>s, split by a <separator> that includes a newline
2124            //    character. When it sees such a <literal>, your DBMS will
2125            //    ignore the <separator> and treat the multiple strings as
2126            //    a single <literal>."
2127            Token::SingleQuotedString(s) => Ok(Some(Ident::with_quote('\'', s))),
2128            not_an_ident => {
2129                if after_as {
2130                    return self.expected("an identifier after AS", not_an_ident);
2131                }
2132                self.prev_token();
2133                Ok(None) // no alias found
2134            }
2135        }
2136    }
2137
2138    /// Parse `AS identifier` when the AS is describing a table-valued object,
2139    /// like in `... FROM generate_series(1, 10) AS t (col)`. In this case
2140    /// the alias is allowed to optionally name the columns in the table, in
2141    /// addition to the table itself.
2142    pub fn parse_optional_table_alias(
2143        &mut self,
2144        reserved_kwds: &[Keyword],
2145    ) -> Result<Option<TableAlias>, ParserError> {
2146        match self.parse_optional_alias(reserved_kwds)? {
2147            Some(name) => {
2148                let columns = self.parse_parenthesized_column_list(Optional)?;
2149                Ok(Some(TableAlias { name, columns }))
2150            }
2151            None => Ok(None),
2152        }
2153    }
2154
2155    /// Parse a possibly qualified, possibly quoted identifier, e.g.
2156    /// `foo` or `myschema."table"
2157    pub fn parse_object_name(&mut self) -> Result<ObjectName, ParserError> {
2158        let mut idents = vec![];
2159        loop {
2160            idents.push(self.parse_identifier()?);
2161            if !self.consume_token(&Token::Period) {
2162                break;
2163            }
2164        }
2165        Ok(ObjectName(idents))
2166    }
2167
2168    /// Parse identifiers
2169    pub fn parse_identifiers(&mut self) -> Result<Vec<Ident>, ParserError> {
2170        let mut idents = vec![];
2171        loop {
2172            match self.next_token() {
2173                Token::Word(w) => idents.push(w.to_ident()),
2174                Token::EOF => break,
2175                _ => {}
2176            }
2177        }
2178        Ok(idents)
2179    }
2180
2181    /// Parse a simple one-word identifier (possibly quoted, possibly a keyword)
2182    pub fn parse_identifier(&mut self) -> Result<Ident, ParserError> {
2183        match self.next_token() {
2184            Token::Word(w) => Ok(w.to_ident()),
2185            Token::SingleQuotedString(s) => Ok(Ident::with_quote('\'', s)),
2186            unexpected => self.expected("identifier", unexpected),
2187        }
2188    }
2189
2190    /// Parse a parenthesized comma-separated list of unqualified, possibly quoted identifiers
2191    pub fn parse_parenthesized_column_list(
2192        &mut self,
2193        optional: IsOptional,
2194    ) -> Result<Vec<Ident>, ParserError> {
2195        if self.consume_token(&Token::LParen) {
2196            let cols = self.parse_comma_separated(Parser::parse_identifier)?;
2197            self.expect_token(&Token::RParen)?;
2198            Ok(cols)
2199        } else if optional == Optional {
2200            Ok(vec![])
2201        } else {
2202            self.expected("a list of columns in parentheses", self.peek_token())
2203        }
2204    }
2205
2206    pub fn parse_optional_precision(&mut self) -> Result<Option<u64>, ParserError> {
2207        if self.consume_token(&Token::LParen) {
2208            let n = self.parse_literal_uint()?;
2209            self.expect_token(&Token::RParen)?;
2210            Ok(Some(n))
2211        } else {
2212            Ok(None)
2213        }
2214    }
2215
2216    pub fn parse_optional_precision_scale(
2217        &mut self,
2218    ) -> Result<(Option<u64>, Option<u64>), ParserError> {
2219        if self.consume_token(&Token::LParen) {
2220            let n = self.parse_literal_uint()?;
2221            let scale = if self.consume_token(&Token::Comma) {
2222                Some(self.parse_literal_uint()?)
2223            } else {
2224                None
2225            };
2226            self.expect_token(&Token::RParen)?;
2227            Ok((Some(n), scale))
2228        } else {
2229            Ok((None, None))
2230        }
2231    }
2232
2233    pub fn parse_delete(&mut self) -> Result<Delete, ParserError> {
2234        self.expect_keyword(Keyword::FROM)?;
2235        let table_name = self.parse_object_name()?;
2236        let selection = if self.parse_keyword(Keyword::WHERE) {
2237            Some(self.parse_expr()?)
2238        } else {
2239            None
2240        };
2241
2242        Ok(Delete {
2243            table_name,
2244            selection,
2245        })
2246    }
2247
2248    pub fn parse_explain(&mut self) -> Result<Explain, ParserError> {
2249        let analyze = self.parse_keyword(Keyword::ANALYZE);
2250        let verbose = self.parse_keyword(Keyword::VERBOSE);
2251
2252        let statement = Box::new(self.parse_statement()?);
2253
2254        Ok(Explain {
2255            analyze,
2256            verbose,
2257            statement,
2258        })
2259    }
2260
2261    /// Parse a query expression, i.e. a `SELECT` statement optionally
2262    /// preceeded with some `WITH` CTE declarations and optionally followed
2263    /// by `ORDER BY`. Unlike some other parse_... methods, this one doesn't
2264    /// expect the initial keyword to be already consumed
2265    pub fn parse_query(&mut self) -> Result<Query, ParserError> {
2266        let with = if self.parse_keyword(Keyword::WITH) {
2267            Some(With {
2268                recursive: self.parse_keyword(Keyword::RECURSIVE),
2269                cte_tables: self.parse_comma_separated(Parser::parse_cte)?,
2270            })
2271        } else {
2272            None
2273        };
2274
2275        if !self.parse_keyword(Keyword::INSERT) {
2276            let body = self.parse_query_body(0)?;
2277
2278            let order_by = if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
2279                self.parse_comma_separated(Parser::parse_order_by_expr)?
2280            } else {
2281                vec![]
2282            };
2283
2284            let limit = if self.parse_keyword(Keyword::LIMIT) {
2285                self.parse_limit()?
2286            } else {
2287                None
2288            };
2289
2290            let offset = if self.parse_keyword(Keyword::OFFSET) {
2291                Some(self.parse_offset()?)
2292            } else {
2293                None
2294            };
2295
2296            let fetch = if self.parse_keyword(Keyword::FETCH) {
2297                Some(self.parse_fetch()?)
2298            } else {
2299                None
2300            };
2301
2302            Ok(Query {
2303                with,
2304                body,
2305                order_by,
2306                limit,
2307                offset,
2308                fetch,
2309            })
2310        } else {
2311            let insert = self.parse_insert()?;
2312            Ok(Query {
2313                with,
2314                body: SetExpr::Insert(insert),
2315                limit: None,
2316                order_by: vec![],
2317                offset: None,
2318                fetch: None,
2319            })
2320        }
2321    }
2322
2323    /// Parse a CTE (`alias [( col1, col2, ... )] AS (subquery)`)
2324    fn parse_cte(&mut self) -> Result<Cte, ParserError> {
2325        let name = self.parse_identifier()?;
2326
2327        let mut cte = if self.parse_keyword(Keyword::AS) {
2328            self.expect_token(&Token::LParen)?;
2329            let query = self.parse_query()?;
2330            self.expect_token(&Token::RParen)?;
2331            let alias = TableAlias {
2332                name,
2333                columns: vec![],
2334            };
2335            Cte {
2336                alias,
2337                query,
2338                from: None,
2339            }
2340        } else {
2341            let columns = self.parse_parenthesized_column_list(Optional)?;
2342            self.expect_keyword(Keyword::AS)?;
2343            self.expect_token(&Token::LParen)?;
2344            let query = self.parse_query()?;
2345            self.expect_token(&Token::RParen)?;
2346            let alias = TableAlias { name, columns };
2347            Cte {
2348                alias,
2349                query,
2350                from: None,
2351            }
2352        };
2353        if self.parse_keyword(Keyword::FROM) {
2354            cte.from = Some(self.parse_identifier()?);
2355        }
2356        Ok(cte)
2357    }
2358
2359    /// Parse a "query body", which is an expression with roughly the
2360    /// following grammar:
2361    /// ```text
2362    ///   query_body ::= restricted_select | '(' subquery ')' | set_operation
2363    ///   restricted_select ::= 'SELECT' [expr_list] [ from ] [ where ] [ groupby_having ]
2364    ///   subquery ::= query_body [ order_by_limit ]
2365    ///   set_operation ::= query_body { 'UNION' | 'EXCEPT' | 'INTERSECT' } [ 'ALL' ] query_body
2366    /// ```
2367    fn parse_query_body(&mut self, precedence: u8) -> Result<SetExpr, ParserError> {
2368        // We parse the expression using a Pratt parser, as in `parse_expr()`.
2369        // Start by parsing a restricted SELECT or a `(subquery)`:
2370        let mut expr = if self.parse_keyword(Keyword::SELECT) {
2371            SetExpr::Select(Box::new(self.parse_select()?))
2372        } else if self.consume_token(&Token::LParen) {
2373            // CTEs are not allowed here, but the parser currently accepts them
2374            let subquery = self.parse_query()?;
2375            self.expect_token(&Token::RParen)?;
2376            SetExpr::Query(Box::new(subquery))
2377        } else if self.parse_keyword(Keyword::VALUES) {
2378            SetExpr::Values(self.parse_values()?)
2379        } else {
2380            return self.expected(
2381                "SELECT, VALUES, or a subquery in the query body",
2382                self.peek_token(),
2383            );
2384        };
2385
2386        loop {
2387            // The query can be optionally followed by a set operator:
2388            let op = self.parse_set_operator(&self.peek_token());
2389            let next_precedence = match op {
2390                // UNION and EXCEPT have the same binding power and evaluate left-to-right
2391                Some(SetOperator::Union) | Some(SetOperator::Except) => 10,
2392                // INTERSECT has higher precedence than UNION/EXCEPT
2393                Some(SetOperator::Intersect) => 20,
2394                // Unexpected token or EOF => stop parsing the query body
2395                None => break,
2396            };
2397            if precedence >= next_precedence {
2398                break;
2399            }
2400            self.next_token(); // skip past the set operator
2401            expr = SetExpr::SetOperation {
2402                left: Box::new(expr),
2403                op: op.unwrap(),
2404                all: self.parse_keyword(Keyword::ALL),
2405                right: Box::new(self.parse_query_body(next_precedence)?),
2406            };
2407        }
2408
2409        Ok(expr)
2410    }
2411
2412    fn parse_set_operator(&mut self, token: &Token) -> Option<SetOperator> {
2413        match token {
2414            Token::Word(w) if w.keyword == Keyword::UNION => Some(SetOperator::Union),
2415            Token::Word(w) if w.keyword == Keyword::EXCEPT => Some(SetOperator::Except),
2416            Token::Word(w) if w.keyword == Keyword::INTERSECT => Some(SetOperator::Intersect),
2417            _ => None,
2418        }
2419    }
2420
2421    /// Parse a restricted `SELECT` statement (no CTEs / `UNION` / `ORDER BY`),
2422    /// assuming the initial `SELECT` was already consumed
2423    pub fn parse_select(&mut self) -> Result<Select, ParserError> {
2424        let distinct = self.parse_all_or_distinct()?;
2425
2426        let top = if self.parse_keyword(Keyword::TOP) {
2427            Some(self.parse_top()?)
2428        } else {
2429            None
2430        };
2431
2432        let projection = self.parse_comma_separated(Parser::parse_select_item)?;
2433
2434        // Note that for keywords to be properly handled here, they need to be
2435        // added to `RESERVED_FOR_COLUMN_ALIAS` / `RESERVED_FOR_TABLE_ALIAS`,
2436        // otherwise they may be parsed as an alias as part of the `projection`
2437        // or `from`.
2438
2439        let from = if self.parse_keyword(Keyword::FROM) {
2440            self.parse_comma_separated(Parser::parse_table_and_joins)?
2441        } else {
2442            vec![]
2443        };
2444        let mut lateral_views = vec![];
2445        loop {
2446            if self.parse_keywords(&[Keyword::LATERAL, Keyword::VIEW]) {
2447                let outer = self.parse_keyword(Keyword::OUTER);
2448                let lateral_view = self.parse_expr()?;
2449                let lateral_view_name = self.parse_object_name()?;
2450                let lateral_col_alias = self
2451                    .parse_comma_separated(|parser| {
2452                        parser.parse_optional_alias(&[
2453                            Keyword::WHERE,
2454                            Keyword::GROUP,
2455                            Keyword::CLUSTER,
2456                            Keyword::HAVING,
2457                            Keyword::LATERAL,
2458                        ]) // This couldn't possibly be a bad idea
2459                    })?
2460                    .into_iter()
2461                    .flatten()
2462                    .collect();
2463
2464                lateral_views.push(LateralView {
2465                    lateral_view,
2466                    lateral_view_name,
2467                    lateral_col_alias,
2468                    outer,
2469                });
2470            } else {
2471                break;
2472            }
2473        }
2474
2475        let selection = if self.parse_keyword(Keyword::WHERE) {
2476            Some(self.parse_expr()?)
2477        } else {
2478            None
2479        };
2480
2481        let group_by = if self.parse_keywords(&[Keyword::GROUP, Keyword::BY]) {
2482            self.parse_comma_separated(Parser::parse_expr)?
2483        } else {
2484            vec![]
2485        };
2486
2487        let cluster_by = if self.parse_keywords(&[Keyword::CLUSTER, Keyword::BY]) {
2488            self.parse_comma_separated(Parser::parse_expr)?
2489        } else {
2490            vec![]
2491        };
2492
2493        let distribute_by = if self.parse_keywords(&[Keyword::DISTRIBUTE, Keyword::BY]) {
2494            self.parse_comma_separated(Parser::parse_expr)?
2495        } else {
2496            vec![]
2497        };
2498
2499        let sort_by = if self.parse_keywords(&[Keyword::SORT, Keyword::BY]) {
2500            self.parse_comma_separated(Parser::parse_expr)?
2501        } else {
2502            vec![]
2503        };
2504
2505        let having = if self.parse_keyword(Keyword::HAVING) {
2506            Some(self.parse_expr()?)
2507        } else {
2508            None
2509        };
2510
2511        Ok(Select {
2512            distinct,
2513            top,
2514            projection,
2515            from,
2516            lateral_views,
2517            selection,
2518            group_by,
2519            cluster_by,
2520            distribute_by,
2521            sort_by,
2522            having,
2523        })
2524    }
2525
2526    pub fn parse_set(&mut self) -> Result<Statement, ParserError> {
2527        let modifier =
2528            self.parse_one_of_keywords(&[Keyword::SESSION, Keyword::LOCAL, Keyword::HIVEVAR]);
2529        if let Some(Keyword::HIVEVAR) = modifier {
2530            self.expect_token(&Token::Colon)?;
2531        }
2532        let variable = self.parse_identifier()?;
2533        if self.consume_token(&Token::Eq) || self.parse_keyword(Keyword::TO) {
2534            let mut values = vec![];
2535            loop {
2536                let token = self.peek_token();
2537                let value = match (self.parse_value(), token) {
2538                    (Ok(value), _) => SetVariableValue::Literal(value),
2539                    (Err(_), Token::Word(ident)) => SetVariableValue::Ident(ident.to_ident()),
2540                    (Err(_), unexpected) => self.expected("variable value", unexpected)?,
2541                };
2542                values.push(value);
2543                if self.consume_token(&Token::Comma) {
2544                    continue;
2545                }
2546                return Ok(Statement::SetVariable(SetVariable {
2547                    local: modifier == Some(Keyword::LOCAL),
2548                    hivevar: Some(Keyword::HIVEVAR) == modifier,
2549                    variable,
2550                    value: values,
2551                }));
2552            }
2553        } else if variable.value == "TRANSACTION" && modifier.is_none() {
2554            Ok(Statement::SetTransaction(SetTransaction {
2555                modes: self.parse_transaction_modes()?,
2556            }))
2557        } else {
2558            self.expected("equals sign or TO", self.peek_token())
2559        }
2560    }
2561
2562    pub fn parse_show(&mut self) -> Result<Statement, ParserError> {
2563        if self
2564            .parse_one_of_keywords(&[
2565                Keyword::EXTENDED,
2566                Keyword::FULL,
2567                Keyword::COLUMNS,
2568                Keyword::FIELDS,
2569            ])
2570            .is_some()
2571        {
2572            self.prev_token();
2573            Ok(Statement::ShowColumns(self.parse_show_columns()?))
2574        } else if self.parse_one_of_keywords(&[Keyword::CREATE]).is_some() {
2575            Ok(Statement::ShowCreate(self.parse_show_create()?))
2576        } else {
2577            Ok(Statement::ShowVariable(ShowVariable {
2578                variable: self.parse_identifiers()?,
2579            }))
2580        }
2581    }
2582
2583    fn parse_show_create(&mut self) -> Result<ShowCreate, ParserError> {
2584        let obj_type = match self.expect_one_of_keywords(&[
2585            Keyword::TABLE,
2586            Keyword::TRIGGER,
2587            Keyword::FUNCTION,
2588            Keyword::PROCEDURE,
2589            Keyword::EVENT,
2590        ])? {
2591            Keyword::TABLE => Ok(ShowCreateObject::Table),
2592            Keyword::TRIGGER => Ok(ShowCreateObject::Trigger),
2593            Keyword::FUNCTION => Ok(ShowCreateObject::Function),
2594            Keyword::PROCEDURE => Ok(ShowCreateObject::Procedure),
2595            Keyword::EVENT => Ok(ShowCreateObject::Event),
2596            keyword => Err(ParserError::ParserError(format!(
2597                "Unable to map keyword to ShowCreateObject: {:?}",
2598                keyword
2599            ))),
2600        }?;
2601
2602        let obj_name = self.parse_object_name()?;
2603
2604        Ok(ShowCreate { obj_type, obj_name })
2605    }
2606
2607    fn parse_show_columns(&mut self) -> Result<ShowColumns, ParserError> {
2608        let extended = self.parse_keyword(Keyword::EXTENDED);
2609        let full = self.parse_keyword(Keyword::FULL);
2610        self.expect_one_of_keywords(&[Keyword::COLUMNS, Keyword::FIELDS])?;
2611        self.expect_one_of_keywords(&[Keyword::FROM, Keyword::IN])?;
2612        let table_name = self.parse_object_name()?;
2613        // MySQL also supports FROM <database> here. In other words, MySQL
2614        // allows both FROM <table> FROM <database> and FROM <database>.<table>,
2615        // while we only support the latter for now.
2616        let filter = self.parse_show_statement_filter()?;
2617        Ok(ShowColumns {
2618            extended,
2619            full,
2620            table_name,
2621            filter,
2622        })
2623    }
2624
2625    fn parse_show_statement_filter(&mut self) -> Result<Option<ShowStatementFilter>, ParserError> {
2626        if self.parse_keyword(Keyword::LIKE) {
2627            Ok(Some(ShowStatementFilter::Like(
2628                self.parse_literal_string()?,
2629            )))
2630        } else if self.parse_keyword(Keyword::ILIKE) {
2631            Ok(Some(ShowStatementFilter::ILike(
2632                self.parse_literal_string()?,
2633            )))
2634        } else if self.parse_keyword(Keyword::WHERE) {
2635            Ok(Some(ShowStatementFilter::Where(self.parse_expr()?)))
2636        } else {
2637            Ok(None)
2638        }
2639    }
2640
2641    pub fn parse_table_and_joins(&mut self) -> Result<TableWithJoins, ParserError> {
2642        let relation = self.parse_table_factor()?;
2643
2644        // Note that for keywords to be properly handled here, they need to be
2645        // added to `RESERVED_FOR_TABLE_ALIAS`, otherwise they may be parsed as
2646        // a table alias.
2647        let mut joins = vec![];
2648        loop {
2649            let join = if self.parse_keyword(Keyword::CROSS) {
2650                let join_operator = if self.parse_keyword(Keyword::JOIN) {
2651                    JoinOperator::CrossJoin
2652                } else if self.parse_keyword(Keyword::APPLY) {
2653                    // MSSQL extension, similar to CROSS JOIN LATERAL
2654                    JoinOperator::CrossApply
2655                } else {
2656                    return self.expected("JOIN or APPLY after CROSS", self.peek_token());
2657                };
2658                Join {
2659                    relation: self.parse_table_factor()?,
2660                    join_operator,
2661                }
2662            } else if self.parse_keyword(Keyword::OUTER) {
2663                // MSSQL extension, similar to LEFT JOIN LATERAL .. ON 1=1
2664                self.expect_keyword(Keyword::APPLY)?;
2665                Join {
2666                    relation: self.parse_table_factor()?,
2667                    join_operator: JoinOperator::OuterApply,
2668                }
2669            } else {
2670                let natural = self.parse_keyword(Keyword::NATURAL);
2671                let peek_keyword = if let Token::Word(w) = self.peek_token() {
2672                    w.keyword
2673                } else {
2674                    Keyword::NoKeyword
2675                };
2676
2677                let join_operator_type = match peek_keyword {
2678                    Keyword::INNER | Keyword::JOIN => {
2679                        let _ = self.parse_keyword(Keyword::INNER);
2680                        self.expect_keyword(Keyword::JOIN)?;
2681                        JoinOperator::Inner
2682                    }
2683                    kw @ Keyword::LEFT | kw @ Keyword::RIGHT | kw @ Keyword::FULL => {
2684                        let _ = self.next_token();
2685                        let _ = self.parse_keyword(Keyword::OUTER);
2686                        self.expect_keyword(Keyword::JOIN)?;
2687                        match kw {
2688                            Keyword::LEFT => JoinOperator::LeftOuter,
2689                            Keyword::RIGHT => JoinOperator::RightOuter,
2690                            Keyword::FULL => JoinOperator::FullOuter,
2691                            _ => unreachable!(),
2692                        }
2693                    }
2694                    Keyword::OUTER => {
2695                        return self.expected("LEFT, RIGHT, or FULL", self.peek_token());
2696                    }
2697                    _ if natural => {
2698                        return self.expected("a join type after NATURAL", self.peek_token());
2699                    }
2700                    _ => break,
2701                };
2702                let relation = self.parse_table_factor()?;
2703                let join_constraint = self.parse_join_constraint(natural)?;
2704                Join {
2705                    relation,
2706                    join_operator: join_operator_type(join_constraint),
2707                }
2708            };
2709            joins.push(join);
2710        }
2711        Ok(TableWithJoins { relation, joins })
2712    }
2713
2714    /// A table name or a parenthesized subquery, followed by optional `[AS] alias`
2715    pub fn parse_table_factor(&mut self) -> Result<TableFactor, ParserError> {
2716        if self.parse_keyword(Keyword::LATERAL) {
2717            // LATERAL must always be followed by a subquery.
2718            if !self.consume_token(&Token::LParen) {
2719                self.expected("subquery after LATERAL", self.peek_token())?;
2720            }
2721            self.parse_derived_table_factor(Lateral)
2722        } else if self.parse_keyword(Keyword::TABLE) {
2723            // parse table function (SELECT * FROM TABLE (<expr>) [ AS <alias> ])
2724            self.expect_token(&Token::LParen)?;
2725            let expr = self.parse_expr()?;
2726            self.expect_token(&Token::RParen)?;
2727            let alias = self.parse_optional_table_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?;
2728            Ok(TableFactor::TableFunction { expr, alias })
2729        } else if self.consume_token(&Token::LParen) {
2730            // A left paren introduces either a derived table (i.e., a subquery)
2731            // or a nested join. It's nearly impossible to determine ahead of
2732            // time which it is... so we just try to parse both.
2733            //
2734            // Here's an example that demonstrates the complexity:
2735            //                     /-------------------------------------------------------\
2736            //                     | /-----------------------------------\                 |
2737            //     SELECT * FROM ( ( ( (SELECT 1) UNION (SELECT 2) ) AS t1 NATURAL JOIN t2 ) )
2738            //                   ^ ^ ^ ^
2739            //                   | | | |
2740            //                   | | | |
2741            //                   | | | (4) belongs to a SetExpr::Query inside the subquery
2742            //                   | | (3) starts a derived table (subquery)
2743            //                   | (2) starts a nested join
2744            //                   (1) an additional set of parens around a nested join
2745            //
2746
2747            // If the recently consumed '(' starts a derived table, the call to
2748            // `parse_derived_table_factor` below will return success after parsing the
2749            // subquery, followed by the closing ')', and the alias of the derived table.
2750            // In the example above this is case (3).
2751            return_ok_if_some!(
2752                self.maybe_parse(|parser| parser.parse_derived_table_factor(NotLateral))
2753            );
2754            // A parsing error from `parse_derived_table_factor` indicates that the '(' we've
2755            // recently consumed does not start a derived table (cases 1, 2, or 4).
2756            // `maybe_parse` will ignore such an error and rewind to be after the opening '('.
2757
2758            // Inside the parentheses we expect to find an (A) table factor
2759            // followed by some joins or (B) another level of nesting.
2760            let mut table_and_joins = self.parse_table_and_joins()?;
2761
2762            if !table_and_joins.joins.is_empty() {
2763                self.expect_token(&Token::RParen)?;
2764                Ok(TableFactor::NestedJoin(Box::new(table_and_joins))) // (A)
2765            } else if let TableFactor::NestedJoin(_) = &table_and_joins.relation {
2766                // (B): `table_and_joins` (what we found inside the parentheses)
2767                // is a nested join `(foo JOIN bar)`, not followed by other joins.
2768                self.expect_token(&Token::RParen)?;
2769                Ok(TableFactor::NestedJoin(Box::new(table_and_joins)))
2770            } else if dialect_of!(self is SnowflakeDialect | GenericDialect) {
2771                // Dialect-specific behavior: Snowflake diverges from the
2772                // standard and from most of the other implementations by
2773                // allowing extra parentheses not only around a join (B), but
2774                // around lone table names (e.g. `FROM (mytable [AS alias])`)
2775                // and around derived tables (e.g. `FROM ((SELECT ...)
2776                // [AS alias])`) as well.
2777                self.expect_token(&Token::RParen)?;
2778
2779                if let Some(outer_alias) =
2780                    self.parse_optional_table_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?
2781                {
2782                    // Snowflake also allows specifying an alias *after* parens
2783                    // e.g. `FROM (mytable) AS alias`
2784                    match &mut table_and_joins.relation {
2785                        TableFactor::Derived { alias, .. }
2786                        | TableFactor::Table { alias, .. }
2787                        | TableFactor::TableFunction { alias, .. } => {
2788                            // but not `FROM (mytable AS alias1) AS alias2`.
2789                            if let Some(inner_alias) = alias {
2790                                return Err(ParserError::ParserError(format!(
2791                                    "duplicate alias {}",
2792                                    inner_alias
2793                                )));
2794                            }
2795                            // Act as if the alias was specified normally next
2796                            // to the table name: `(mytable) AS alias` ->
2797                            // `(mytable AS alias)`
2798                            alias.replace(outer_alias);
2799                        }
2800                        TableFactor::NestedJoin(_) => unreachable!(),
2801                    };
2802                }
2803                // Do not store the extra set of parens in the AST
2804                Ok(table_and_joins.relation)
2805            } else {
2806                // The SQL spec prohibits derived tables and bare tables from
2807                // appearing alone in parentheses (e.g. `FROM (mytable)`)
2808                self.expected("joined table", self.peek_token())
2809            }
2810        } else {
2811            let name = self.parse_object_name()?;
2812            // Postgres, MSSQL: table-valued functions:
2813            let args = if self.consume_token(&Token::LParen) {
2814                self.parse_optional_args()?
2815            } else {
2816                vec![]
2817            };
2818            let alias = self.parse_optional_table_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?;
2819            // MSSQL-specific table hints:
2820            let mut with_hints = vec![];
2821            if self.parse_keyword(Keyword::WITH) {
2822                if self.consume_token(&Token::LParen) {
2823                    with_hints = self.parse_comma_separated(Parser::parse_expr)?;
2824                    self.expect_token(&Token::RParen)?;
2825                } else {
2826                    // rewind, as WITH may belong to the next statement's CTE
2827                    self.prev_token();
2828                }
2829            };
2830            Ok(TableFactor::Table {
2831                name,
2832                alias,
2833                args,
2834                with_hints,
2835            })
2836        }
2837    }
2838
2839    pub fn parse_derived_table_factor(
2840        &mut self,
2841        lateral: IsLateral,
2842    ) -> Result<TableFactor, ParserError> {
2843        let subquery = Box::new(self.parse_query()?);
2844        self.expect_token(&Token::RParen)?;
2845        let alias = self.parse_optional_table_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?;
2846        Ok(TableFactor::Derived {
2847            lateral: match lateral {
2848                Lateral => true,
2849                NotLateral => false,
2850            },
2851            subquery,
2852            alias,
2853        })
2854    }
2855
2856    fn parse_join_constraint(&mut self, natural: bool) -> Result<JoinConstraint, ParserError> {
2857        if natural {
2858            Ok(JoinConstraint::Natural)
2859        } else if self.parse_keyword(Keyword::ON) {
2860            let constraint = self.parse_expr()?;
2861            Ok(JoinConstraint::On(constraint))
2862        } else if self.parse_keyword(Keyword::USING) {
2863            let columns = self.parse_parenthesized_column_list(Mandatory)?;
2864            Ok(JoinConstraint::Using(columns))
2865        } else {
2866            Ok(JoinConstraint::None)
2867            //self.expected("ON, or USING after JOIN", self.peek_token())
2868        }
2869    }
2870
2871    /// Parse an INSERT statement
2872    pub fn parse_insert(&mut self) -> Result<Statement, ParserError> {
2873        let or = if !dialect_of!(self is SQLiteDialect) {
2874            None
2875        } else if self.parse_keywords(&[Keyword::OR, Keyword::REPLACE]) {
2876            Some(SqliteOnConflict::Replace)
2877        } else if self.parse_keywords(&[Keyword::OR, Keyword::ROLLBACK]) {
2878            Some(SqliteOnConflict::Rollback)
2879        } else if self.parse_keywords(&[Keyword::OR, Keyword::ABORT]) {
2880            Some(SqliteOnConflict::Abort)
2881        } else if self.parse_keywords(&[Keyword::OR, Keyword::FAIL]) {
2882            Some(SqliteOnConflict::Fail)
2883        } else if self.parse_keywords(&[Keyword::OR, Keyword::IGNORE]) {
2884            Some(SqliteOnConflict::Ignore)
2885        } else if self.parse_keyword(Keyword::REPLACE) {
2886            Some(SqliteOnConflict::Replace)
2887        } else {
2888            None
2889        };
2890        let action = self.expect_one_of_keywords(&[Keyword::INTO, Keyword::OVERWRITE])?;
2891        let overwrite = action == Keyword::OVERWRITE;
2892        let local = self.parse_keyword(Keyword::LOCAL);
2893
2894        if self.parse_keyword(Keyword::DIRECTORY) {
2895            let path = self.parse_literal_string()?;
2896            let file_format = if self.parse_keywords(&[Keyword::STORED, Keyword::AS]) {
2897                Some(self.parse_file_format()?)
2898            } else {
2899                None
2900            };
2901            let source = Box::new(self.parse_query()?);
2902            Ok(Statement::Directory(Directory {
2903                local,
2904                path,
2905                overwrite,
2906                file_format,
2907                source,
2908            }))
2909        } else {
2910            // Hive lets you put table here regardless
2911            let table = self.parse_keyword(Keyword::TABLE);
2912            let table_name = self.parse_object_name()?;
2913            let columns = self.parse_parenthesized_column_list(Optional)?;
2914
2915            let partitioned = if self.parse_keyword(Keyword::PARTITION) {
2916                self.expect_token(&Token::LParen)?;
2917                let r = Some(self.parse_comma_separated(Parser::parse_expr)?);
2918                self.expect_token(&Token::RParen)?;
2919                r
2920            } else {
2921                None
2922            };
2923
2924            // Hive allows you to specify columns after partitions as well if you want.
2925            let after_columns = self.parse_parenthesized_column_list(Optional)?;
2926
2927            let source = Box::new(self.parse_query()?);
2928            Ok(Statement::Insert(Insert {
2929                or,
2930                table_name,
2931                overwrite,
2932                partitioned,
2933                columns,
2934                after_columns,
2935                source,
2936                table,
2937            }))
2938        }
2939    }
2940
2941    pub fn parse_update(&mut self) -> Result<Update, ParserError> {
2942        let table_name = self.parse_object_name()?;
2943        self.expect_keyword(Keyword::SET)?;
2944        let assignments = self.parse_comma_separated(Parser::parse_assignment)?;
2945        let selection = if self.parse_keyword(Keyword::WHERE) {
2946            Some(self.parse_expr()?)
2947        } else {
2948            None
2949        };
2950        Ok(Update {
2951            table_name,
2952            assignments,
2953            selection,
2954        })
2955    }
2956
2957    /// Parse a `var = expr` assignment, used in an UPDATE statement
2958    pub fn parse_assignment(&mut self) -> Result<Assignment, ParserError> {
2959        let id = self.parse_identifier()?;
2960        self.expect_token(&Token::Eq)?;
2961        let value = self.parse_expr()?;
2962        Ok(Assignment { id, value })
2963    }
2964
2965    fn parse_function_args(&mut self) -> Result<FunctionArg, ParserError> {
2966        if self.peek_nth_token(1) == Token::RArrow {
2967            let name = self.parse_identifier()?;
2968
2969            self.expect_token(&Token::RArrow)?;
2970            let arg = self.parse_expr()?;
2971
2972            Ok(FunctionArg::Named { name, arg })
2973        } else {
2974            Ok(FunctionArg::Unnamed(self.parse_expr()?))
2975        }
2976    }
2977
2978    pub fn parse_optional_args(&mut self) -> Result<Vec<FunctionArg>, ParserError> {
2979        if self.consume_token(&Token::RParen) {
2980            Ok(vec![])
2981        } else {
2982            let args = self.parse_comma_separated(Parser::parse_function_args)?;
2983            self.expect_token(&Token::RParen)?;
2984            Ok(args)
2985        }
2986    }
2987
2988    /// Parse a comma-delimited list of projections after SELECT
2989    pub fn parse_select_item(&mut self) -> Result<SelectItem, ParserError> {
2990        let expr = self.parse_expr()?;
2991        if let Expr::Wildcard = expr {
2992            Ok(SelectItem::Wildcard)
2993        } else if let Expr::QualifiedWildcard(prefix) = expr {
2994            Ok(SelectItem::QualifiedWildcard(ObjectName(prefix)))
2995        } else {
2996            // `expr` is a regular SQL expression and can be followed by an alias
2997            if let Some(alias) = self.parse_optional_alias(keywords::RESERVED_FOR_COLUMN_ALIAS)? {
2998                Ok(SelectItem::ExprWithAlias { expr, alias })
2999            } else {
3000                Ok(SelectItem::UnnamedExpr(expr))
3001            }
3002        }
3003    }
3004
3005    /// Parse an expression, optionally followed by ASC or DESC (used in ORDER BY)
3006    pub fn parse_order_by_expr(&mut self) -> Result<OrderByExpr, ParserError> {
3007        let expr = self.parse_expr()?;
3008
3009        let asc = if self.parse_keyword(Keyword::ASC) {
3010            Some(true)
3011        } else if self.parse_keyword(Keyword::DESC) {
3012            Some(false)
3013        } else {
3014            None
3015        };
3016
3017        let nulls_first = if self.parse_keywords(&[Keyword::NULLS, Keyword::FIRST]) {
3018            Some(true)
3019        } else if self.parse_keywords(&[Keyword::NULLS, Keyword::LAST]) {
3020            Some(false)
3021        } else {
3022            None
3023        };
3024
3025        Ok(OrderByExpr {
3026            expr,
3027            asc,
3028            nulls_first,
3029        })
3030    }
3031
3032    /// Parse a TOP clause, MSSQL equivalent of LIMIT,
3033    /// that follows after SELECT [DISTINCT].
3034    pub fn parse_top(&mut self) -> Result<Top, ParserError> {
3035        let quantity = if self.consume_token(&Token::LParen) {
3036            let quantity = self.parse_expr()?;
3037            self.expect_token(&Token::RParen)?;
3038            Some(quantity)
3039        } else {
3040            Some(Expr::Value(self.parse_number_value()?))
3041        };
3042
3043        let percent = self.parse_keyword(Keyword::PERCENT);
3044
3045        let with_ties = self.parse_keywords(&[Keyword::WITH, Keyword::TIES]);
3046
3047        Ok(Top {
3048            with_ties,
3049            percent,
3050            quantity,
3051        })
3052    }
3053
3054    /// Parse a LIMIT clause
3055    pub fn parse_limit(&mut self) -> Result<Option<Expr>, ParserError> {
3056        if self.parse_keyword(Keyword::ALL) {
3057            Ok(None)
3058        } else {
3059            Ok(Some(Expr::Value(self.parse_number_value()?)))
3060        }
3061    }
3062
3063    /// Parse an OFFSET clause
3064    pub fn parse_offset(&mut self) -> Result<Offset, ParserError> {
3065        let value = Expr::Value(self.parse_number_value()?);
3066        let rows = if self.parse_keyword(Keyword::ROW) {
3067            OffsetRows::Row
3068        } else if self.parse_keyword(Keyword::ROWS) {
3069            OffsetRows::Rows
3070        } else {
3071            OffsetRows::None
3072        };
3073        Ok(Offset { value, rows })
3074    }
3075
3076    /// Parse a FETCH clause
3077    pub fn parse_fetch(&mut self) -> Result<Fetch, ParserError> {
3078        self.expect_one_of_keywords(&[Keyword::FIRST, Keyword::NEXT])?;
3079        let (quantity, percent) = if self
3080            .parse_one_of_keywords(&[Keyword::ROW, Keyword::ROWS])
3081            .is_some()
3082        {
3083            (None, false)
3084        } else {
3085            let quantity = Expr::Value(self.parse_value()?);
3086            let percent = self.parse_keyword(Keyword::PERCENT);
3087            self.expect_one_of_keywords(&[Keyword::ROW, Keyword::ROWS])?;
3088            (Some(quantity), percent)
3089        };
3090        let with_ties = if self.parse_keyword(Keyword::ONLY) {
3091            false
3092        } else if self.parse_keywords(&[Keyword::WITH, Keyword::TIES]) {
3093            true
3094        } else {
3095            return self.expected("one of ONLY or WITH TIES", self.peek_token());
3096        };
3097        Ok(Fetch {
3098            with_ties,
3099            percent,
3100            quantity,
3101        })
3102    }
3103
3104    pub fn parse_values(&mut self) -> Result<Values, ParserError> {
3105        let values = self.parse_comma_separated(|parser| {
3106            parser.expect_token(&Token::LParen)?;
3107            let exprs = parser.parse_comma_separated(Parser::parse_expr)?;
3108            parser.expect_token(&Token::RParen)?;
3109            Ok(exprs)
3110        })?;
3111        Ok(Values(values))
3112    }
3113
3114    pub fn parse_start_transaction(&mut self) -> Result<StartTransaction, ParserError> {
3115        self.expect_keyword(Keyword::TRANSACTION)?;
3116        Ok(StartTransaction {
3117            modes: self.parse_transaction_modes()?,
3118        })
3119    }
3120
3121    pub fn parse_begin(&mut self) -> Result<StartTransaction, ParserError> {
3122        let _ = self.parse_one_of_keywords(&[Keyword::TRANSACTION, Keyword::WORK]);
3123        Ok(StartTransaction {
3124            modes: self.parse_transaction_modes()?,
3125        })
3126    }
3127
3128    pub fn parse_transaction_modes(&mut self) -> Result<Vec<TransactionMode>, ParserError> {
3129        let mut modes = vec![];
3130        let mut required = false;
3131        loop {
3132            let mode = if self.parse_keywords(&[Keyword::ISOLATION, Keyword::LEVEL]) {
3133                let iso_level = if self.parse_keywords(&[Keyword::READ, Keyword::UNCOMMITTED]) {
3134                    TransactionIsolationLevel::ReadUncommitted
3135                } else if self.parse_keywords(&[Keyword::READ, Keyword::COMMITTED]) {
3136                    TransactionIsolationLevel::ReadCommitted
3137                } else if self.parse_keywords(&[Keyword::REPEATABLE, Keyword::READ]) {
3138                    TransactionIsolationLevel::RepeatableRead
3139                } else if self.parse_keyword(Keyword::SERIALIZABLE) {
3140                    TransactionIsolationLevel::Serializable
3141                } else {
3142                    self.expected("isolation level", self.peek_token())?
3143                };
3144                TransactionMode::IsolationLevel(iso_level)
3145            } else if self.parse_keywords(&[Keyword::READ, Keyword::ONLY]) {
3146                TransactionMode::AccessMode(TransactionAccessMode::ReadOnly)
3147            } else if self.parse_keywords(&[Keyword::READ, Keyword::WRITE]) {
3148                TransactionMode::AccessMode(TransactionAccessMode::ReadWrite)
3149            } else if required {
3150                self.expected("transaction mode", self.peek_token())?
3151            } else {
3152                break;
3153            };
3154            modes.push(mode);
3155            // ANSI requires a comma after each transaction mode, but
3156            // PostgreSQL, for historical reasons, does not. We follow
3157            // PostgreSQL in making the comma optional, since that is strictly
3158            // more general.
3159            required = self.consume_token(&Token::Comma);
3160        }
3161        Ok(modes)
3162    }
3163
3164    pub fn parse_commit(&mut self) -> Result<Commit, ParserError> {
3165        Ok(Commit {
3166            chain: self.parse_commit_rollback_chain()?,
3167        })
3168    }
3169
3170    pub fn parse_rollback(&mut self) -> Result<Rollback, ParserError> {
3171        Ok(Rollback {
3172            chain: self.parse_commit_rollback_chain()?,
3173        })
3174    }
3175
3176    pub fn parse_commit_rollback_chain(&mut self) -> Result<bool, ParserError> {
3177        let _ = self.parse_one_of_keywords(&[Keyword::TRANSACTION, Keyword::WORK]);
3178        if self.parse_keyword(Keyword::AND) {
3179            let chain = !self.parse_keyword(Keyword::NO);
3180            self.expect_keyword(Keyword::CHAIN)?;
3181            Ok(chain)
3182        } else {
3183            Ok(false)
3184        }
3185    }
3186
3187    fn parse_deallocate(&mut self) -> Result<Deallocate, ParserError> {
3188        let prepare = self.parse_keyword(Keyword::PREPARE);
3189        let name = self.parse_identifier()?;
3190        Ok(Deallocate { name, prepare })
3191    }
3192
3193    fn parse_execute(&mut self) -> Result<Execute, ParserError> {
3194        let name = self.parse_identifier()?;
3195
3196        let mut parameters = vec![];
3197        if self.consume_token(&Token::LParen) {
3198            parameters = self.parse_comma_separated(Parser::parse_expr)?;
3199            self.expect_token(&Token::RParen)?;
3200        }
3201
3202        Ok(Execute { name, parameters })
3203    }
3204
3205    fn parse_prepare(&mut self) -> Result<Prepare, ParserError> {
3206        let name = self.parse_identifier()?;
3207
3208        let mut data_types = vec![];
3209        if self.consume_token(&Token::LParen) {
3210            data_types = self.parse_comma_separated(Parser::parse_data_type)?;
3211            self.expect_token(&Token::RParen)?;
3212        }
3213
3214        self.expect_keyword(Keyword::AS)?;
3215        let statement = Box::new(self.parse_statement()?);
3216        Ok(Prepare {
3217            name,
3218            data_types,
3219            statement,
3220        })
3221    }
3222}
3223impl Word {
3224    pub fn to_ident(&self) -> Ident {
3225        Ident {
3226            value: self.value.clone(),
3227            quote_style: self.quote_style,
3228        }
3229    }
3230}
3231
3232#[cfg(test)]
3233mod tests {
3234    use super::*;
3235    use crate::test_utils::all_dialects;
3236
3237    #[test]
3238    fn test_prev_index() {
3239        let sql = "SELECT version";
3240        all_dialects().run_parser_method(sql, |parser| {
3241            assert_eq!(parser.peek_token(), Token::make_keyword("SELECT"));
3242            assert_eq!(parser.next_token(), Token::make_keyword("SELECT"));
3243            parser.prev_token();
3244            assert_eq!(parser.next_token(), Token::make_keyword("SELECT"));
3245            assert_eq!(parser.next_token(), Token::make_word("version", None));
3246            parser.prev_token();
3247            assert_eq!(parser.peek_token(), Token::make_word("version", None));
3248            assert_eq!(parser.next_token(), Token::make_word("version", None));
3249            assert_eq!(parser.peek_token(), Token::EOF);
3250            parser.prev_token();
3251            assert_eq!(parser.next_token(), Token::make_word("version", None));
3252            assert_eq!(parser.next_token(), Token::EOF);
3253            assert_eq!(parser.next_token(), Token::EOF);
3254            parser.prev_token();
3255        });
3256    }
3257}