sql_ast/
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
15use log::debug;
16
17use super::ast::*;
18use super::dialect::keywords;
19use super::dialect::Dialect;
20use super::tokenizer::*;
21use std::error::Error;
22use std::fmt;
23
24#[derive(Debug, Clone, PartialEq)]
25pub enum ParserError {
26    TokenizerError(String),
27    ParserError(String),
28}
29
30// Use `Parser::expected` instead, if possible
31macro_rules! parser_err {
32    ($MSG:expr) => {
33        Err(ParserError::ParserError($MSG.to_string()))
34    };
35}
36
37#[derive(PartialEq)]
38pub enum IsOptional {
39    Optional,
40    Mandatory,
41}
42use IsOptional::*;
43
44pub enum IsLateral {
45    Lateral,
46    NotLateral,
47}
48use IsLateral::*;
49
50impl From<TokenizerError> for ParserError {
51    fn from(e: TokenizerError) -> Self {
52        ParserError::TokenizerError(format!("{:?}", e))
53    }
54}
55
56impl fmt::Display for ParserError {
57    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
58        write!(
59            f,
60            "sql parser error: {}",
61            match self {
62                ParserError::TokenizerError(s) => s,
63                ParserError::ParserError(s) => s,
64            }
65        )
66    }
67}
68
69impl Error for ParserError {}
70
71/// SQL Parser
72pub struct Parser {
73    tokens: Vec<Token>,
74    /// The index of the first unprocessed token in `self.tokens`
75    index: usize,
76}
77
78impl Parser {
79    /// Parse the specified tokens
80    pub fn new(tokens: Vec<Token>) -> Self {
81        Parser { tokens, index: 0 }
82    }
83
84    /// Parse a SQL statement and produce an Abstract Syntax Tree (AST)
85    pub fn parse_sql(dialect: &dyn Dialect, sql: String) -> Result<Vec<Statement>, ParserError> {
86        let mut tokenizer = Tokenizer::new(dialect, &sql);
87        let tokens = tokenizer.tokenize()?;
88        let mut parser = Parser::new(tokens);
89        let mut stmts = Vec::new();
90        let mut expecting_statement_delimiter = false;
91        debug!("Parsing sql '{}'...", sql);
92        loop {
93            // ignore empty statements (between successive statement delimiters)
94            while parser.consume_token(&Token::SemiColon) {
95                expecting_statement_delimiter = false;
96            }
97
98            if parser.peek_token().is_none() {
99                break;
100            } else if expecting_statement_delimiter {
101                return parser.expected("end of statement", parser.peek_token());
102            }
103
104            let statement = parser.parse_statement()?;
105            stmts.push(statement);
106            expecting_statement_delimiter = true;
107        }
108        Ok(stmts)
109    }
110
111    /// Parse a single top-level statement (such as SELECT, INSERT, CREATE, etc.),
112    /// stopping before the statement separator, if any.
113    pub fn parse_statement(&mut self) -> Result<Statement, ParserError> {
114        match self.next_token() {
115            Some(t) => match t {
116                Token::Word(ref w) if !w.keyword.is_empty() => match w.keyword.as_ref() {
117                    "SELECT" | "WITH" | "VALUES" => {
118                        self.prev_token();
119                        Ok(Statement::Query(Box::new(self.parse_query()?)))
120                    }
121                    "CREATE" => Ok(self.parse_create()?),
122                    "DROP" => Ok(self.parse_drop()?),
123                    "DELETE" => Ok(self.parse_delete()?),
124                    "INSERT" => Ok(self.parse_insert()?),
125                    "UPDATE" => Ok(self.parse_update()?),
126                    "ALTER" => Ok(self.parse_alter()?),
127                    "COPY" => Ok(self.parse_copy()?),
128                    _ => parser_err!(format!(
129                        "Unexpected keyword {:?} at the beginning of a statement",
130                        w.to_string()
131                    )),
132                },
133                Token::LParen => {
134                    self.prev_token();
135                    Ok(Statement::Query(Box::new(self.parse_query()?)))
136                }
137                unexpected => self.expected(
138                    "a keyword at the beginning of a statement",
139                    Some(unexpected),
140                ),
141            },
142            None => self.expected("SQL statement", None),
143        }
144    }
145
146    /// Parse a new expression
147    pub fn parse_expr(&mut self) -> Result<Expr, ParserError> {
148        self.parse_subexpr(0)
149    }
150
151    /// Parse tokens until the precedence changes
152    pub fn parse_subexpr(&mut self, precedence: u8) -> Result<Expr, ParserError> {
153        debug!("parsing expr");
154        let mut expr = self.parse_prefix()?;
155        debug!("prefix: {:?}", expr);
156        loop {
157            let next_precedence = self.get_next_precedence()?;
158            debug!("next precedence: {:?}", next_precedence);
159            if precedence >= next_precedence {
160                break;
161            }
162
163            expr = self.parse_infix(expr, next_precedence)?;
164        }
165        Ok(expr)
166    }
167
168    /// Parse an expression prefix
169    pub fn parse_prefix(&mut self) -> Result<Expr, ParserError> {
170        let tok = self
171            .next_token()
172            .ok_or_else(|| ParserError::ParserError("Unexpected EOF".to_string()))?;
173        let expr = match tok {
174            Token::Word(w) => match w.keyword.as_ref() {
175                "TRUE" | "FALSE" | "NULL" => {
176                    self.prev_token();
177                    Ok(Expr::Value(self.parse_value()?))
178                }
179                "CASE" => self.parse_case_expr(),
180                "CAST" => self.parse_cast_expr(),
181                "DATE" => Ok(Expr::Value(Value::Date(self.parse_literal_string()?))),
182                "EXISTS" => self.parse_exists_expr(),
183                "EXTRACT" => self.parse_extract_expr(),
184                "INTERVAL" => self.parse_literal_interval(),
185                "NOT" => Ok(Expr::UnaryOp {
186                    op: UnaryOperator::Not,
187                    expr: Box::new(self.parse_subexpr(Self::UNARY_NOT_PREC)?),
188                }),
189                "TIME" => Ok(Expr::Value(Value::Time(self.parse_literal_string()?))),
190                "TIMESTAMP" => Ok(Expr::Value(Value::Timestamp(self.parse_literal_string()?))),
191                // Here `w` is a word, check if it's a part of a multi-part
192                // identifier, a function call, or a simple identifier:
193                _ => match self.peek_token() {
194                    Some(Token::LParen) | Some(Token::Period) => {
195                        let mut id_parts: Vec<Ident> = vec![w.to_ident()];
196                        let mut ends_with_wildcard = false;
197                        while self.consume_token(&Token::Period) {
198                            match self.next_token() {
199                                Some(Token::Word(w)) => id_parts.push(w.to_ident()),
200                                Some(Token::Mult) => {
201                                    ends_with_wildcard = true;
202                                    break;
203                                }
204                                unexpected => {
205                                    return self
206                                        .expected("an identifier or a '*' after '.'", unexpected);
207                                }
208                            }
209                        }
210                        if ends_with_wildcard {
211                            Ok(Expr::QualifiedWildcard(id_parts))
212                        } else if self.consume_token(&Token::LParen) {
213                            self.prev_token();
214                            self.parse_function(ObjectName(id_parts))
215                        } else {
216                            Ok(Expr::CompoundIdentifier(id_parts))
217                        }
218                    }
219                    _ => Ok(Expr::Identifier(w.to_ident())),
220                },
221            }, // End of Token::Word
222            Token::Mult => Ok(Expr::Wildcard),
223            tok @ Token::Minus | tok @ Token::Plus => {
224                let op = if tok == Token::Plus {
225                    UnaryOperator::Plus
226                } else {
227                    UnaryOperator::Minus
228                };
229                Ok(Expr::UnaryOp {
230                    op,
231                    expr: Box::new(self.parse_subexpr(Self::PLUS_MINUS_PREC)?),
232                })
233            }
234            Token::Number(_)
235            | Token::SingleQuotedString(_)
236            | Token::NationalStringLiteral(_)
237            | Token::HexStringLiteral(_) => {
238                self.prev_token();
239                Ok(Expr::Value(self.parse_value()?))
240            }
241            Token::LParen => {
242                let expr = if self.parse_keyword("SELECT") || self.parse_keyword("WITH") {
243                    self.prev_token();
244                    Expr::Subquery(Box::new(self.parse_query()?))
245                } else {
246                    Expr::Nested(Box::new(self.parse_expr()?))
247                };
248                self.expect_token(&Token::RParen)?;
249                Ok(expr)
250            }
251            unexpected => self.expected("an expression", Some(unexpected)),
252        }?;
253
254        if self.parse_keyword("COLLATE") {
255            Ok(Expr::Collate {
256                expr: Box::new(expr),
257                collation: self.parse_object_name()?,
258            })
259        } else {
260            Ok(expr)
261        }
262    }
263
264    pub fn parse_function(&mut self, name: ObjectName) -> Result<Expr, ParserError> {
265        self.expect_token(&Token::LParen)?;
266        let all = self.parse_keyword("ALL");
267        let distinct = self.parse_keyword("DISTINCT");
268        if all && distinct {
269            return parser_err!(format!(
270                "Cannot specify both ALL and DISTINCT in function: {}",
271                name.to_string(),
272            ));
273        }
274        let args = self.parse_optional_args()?;
275        let over = if self.parse_keyword("OVER") {
276            // TBD: support window names (`OVER mywin`) in place of inline specification
277            self.expect_token(&Token::LParen)?;
278            let partition_by = if self.parse_keywords(vec!["PARTITION", "BY"]) {
279                // a list of possibly-qualified column names
280                self.parse_comma_separated(Parser::parse_expr)?
281            } else {
282                vec![]
283            };
284            let order_by = if self.parse_keywords(vec!["ORDER", "BY"]) {
285                self.parse_comma_separated(Parser::parse_order_by_expr)?
286            } else {
287                vec![]
288            };
289            let window_frame = if !self.consume_token(&Token::RParen) {
290                let window_frame = self.parse_window_frame()?;
291                self.expect_token(&Token::RParen)?;
292                Some(window_frame)
293            } else {
294                None
295            };
296
297            Some(WindowSpec {
298                partition_by,
299                order_by,
300                window_frame,
301            })
302        } else {
303            None
304        };
305
306        Ok(Expr::Function(Function {
307            name,
308            args,
309            over,
310            distinct,
311        }))
312    }
313
314    pub fn parse_window_frame(&mut self) -> Result<WindowFrame, ParserError> {
315        let units = match self.next_token() {
316            Some(Token::Word(w)) => w.keyword.parse::<WindowFrameUnits>()?,
317            unexpected => return self.expected("ROWS, RANGE, GROUPS", unexpected),
318        };
319        let (start_bound, end_bound) = if self.parse_keyword("BETWEEN") {
320            let start_bound = self.parse_window_frame_bound()?;
321            self.expect_keyword("AND")?;
322            let end_bound = Some(self.parse_window_frame_bound()?);
323            (start_bound, end_bound)
324        } else {
325            (self.parse_window_frame_bound()?, None)
326        };
327        Ok(WindowFrame {
328            units,
329            start_bound,
330            end_bound,
331        })
332    }
333
334    /// Parse `CURRENT ROW` or `{ <positive number> | UNBOUNDED } { PRECEDING | FOLLOWING }`
335    pub fn parse_window_frame_bound(&mut self) -> Result<WindowFrameBound, ParserError> {
336        if self.parse_keywords(vec!["CURRENT", "ROW"]) {
337            Ok(WindowFrameBound::CurrentRow)
338        } else {
339            let rows = if self.parse_keyword("UNBOUNDED") {
340                None
341            } else {
342                Some(self.parse_literal_uint()?)
343            };
344            if self.parse_keyword("PRECEDING") {
345                Ok(WindowFrameBound::Preceding(rows))
346            } else if self.parse_keyword("FOLLOWING") {
347                Ok(WindowFrameBound::Following(rows))
348            } else {
349                self.expected("PRECEDING or FOLLOWING", self.peek_token())
350            }
351        }
352    }
353
354    pub fn parse_case_expr(&mut self) -> Result<Expr, ParserError> {
355        let mut operand = None;
356        if !self.parse_keyword("WHEN") {
357            operand = Some(Box::new(self.parse_expr()?));
358            self.expect_keyword("WHEN")?;
359        }
360        let mut conditions = vec![];
361        let mut results = vec![];
362        loop {
363            conditions.push(self.parse_expr()?);
364            self.expect_keyword("THEN")?;
365            results.push(self.parse_expr()?);
366            if !self.parse_keyword("WHEN") {
367                break;
368            }
369        }
370        let else_result = if self.parse_keyword("ELSE") {
371            Some(Box::new(self.parse_expr()?))
372        } else {
373            None
374        };
375        self.expect_keyword("END")?;
376        Ok(Expr::Case {
377            operand,
378            conditions,
379            results,
380            else_result,
381        })
382    }
383
384    /// Parse a SQL CAST function e.g. `CAST(expr AS FLOAT)`
385    pub fn parse_cast_expr(&mut self) -> Result<Expr, ParserError> {
386        self.expect_token(&Token::LParen)?;
387        let expr = self.parse_expr()?;
388        self.expect_keyword("AS")?;
389        let data_type = self.parse_data_type()?;
390        self.expect_token(&Token::RParen)?;
391        Ok(Expr::Cast {
392            expr: Box::new(expr),
393            data_type,
394        })
395    }
396
397    /// Parse a SQL EXISTS expression e.g. `WHERE EXISTS(SELECT ...)`.
398    pub fn parse_exists_expr(&mut self) -> Result<Expr, ParserError> {
399        self.expect_token(&Token::LParen)?;
400        let exists_node = Expr::Exists(Box::new(self.parse_query()?));
401        self.expect_token(&Token::RParen)?;
402        Ok(exists_node)
403    }
404
405    pub fn parse_extract_expr(&mut self) -> Result<Expr, ParserError> {
406        self.expect_token(&Token::LParen)?;
407        let field = self.parse_date_time_field()?;
408        self.expect_keyword("FROM")?;
409        let expr = self.parse_expr()?;
410        self.expect_token(&Token::RParen)?;
411        Ok(Expr::Extract {
412            field,
413            expr: Box::new(expr),
414        })
415    }
416
417    // This function parses date/time fields for both the EXTRACT function-like
418    // operator and interval qualifiers. EXTRACT supports a wider set of
419    // date/time fields than interval qualifiers, so this function may need to
420    // be split in two.
421    pub fn parse_date_time_field(&mut self) -> Result<DateTimeField, ParserError> {
422        let tok = self.next_token();
423        if let Some(Token::Word(ref k)) = tok {
424            match k.keyword.as_ref() {
425                "YEAR" => Ok(DateTimeField::Year),
426                "MONTH" => Ok(DateTimeField::Month),
427                "DAY" => Ok(DateTimeField::Day),
428                "HOUR" => Ok(DateTimeField::Hour),
429                "MINUTE" => Ok(DateTimeField::Minute),
430                "SECOND" => Ok(DateTimeField::Second),
431                _ => self.expected("date/time field", tok)?,
432            }
433        } else {
434            self.expected("date/time field", tok)?
435        }
436    }
437
438    /// Parse an INTERVAL literal.
439    ///
440    /// Some syntactically valid intervals:
441    ///
442    ///   1. `INTERVAL '1' DAY`
443    ///   2. `INTERVAL '1-1' YEAR TO MONTH`
444    ///   3. `INTERVAL '1' SECOND`
445    ///   4. `INTERVAL '1:1:1.1' HOUR (5) TO SECOND (5)`
446    ///   5. `INTERVAL '1.1' SECOND (2, 2)`
447    ///   6. `INTERVAL '1:1' HOUR (5) TO MINUTE (5)`
448    ///
449    /// Note that we do not currently attempt to parse the quoted value.
450    pub fn parse_literal_interval(&mut self) -> Result<Expr, ParserError> {
451        // The SQL standard allows an optional sign before the value string, but
452        // it is not clear if any implementations support that syntax, so we
453        // don't currently try to parse it. (The sign can instead be included
454        // inside the value string.)
455
456        // The first token in an interval is a string literal which specifies
457        // the duration of the interval.
458        let value = self.parse_literal_string()?;
459
460        // Following the string literal is a qualifier which indicates the units
461        // of the duration specified in the string literal.
462        //
463        // Note that PostgreSQL allows omitting the qualifier, but we currently
464        // require at least the leading field, in accordance with the ANSI spec.
465        let leading_field = self.parse_date_time_field()?;
466
467        let (leading_precision, last_field, fsec_precision) =
468            if leading_field == DateTimeField::Second {
469                // SQL mandates special syntax for `SECOND TO SECOND` literals.
470                // Instead of
471                //     `SECOND [(<leading precision>)] TO SECOND[(<fractional seconds precision>)]`
472                // one must use the special format:
473                //     `SECOND [( <leading precision> [ , <fractional seconds precision>] )]`
474                let last_field = None;
475                let (leading_precision, fsec_precision) = self.parse_optional_precision_scale()?;
476                (leading_precision, last_field, fsec_precision)
477            } else {
478                let leading_precision = self.parse_optional_precision()?;
479                if self.parse_keyword("TO") {
480                    let last_field = Some(self.parse_date_time_field()?);
481                    let fsec_precision = if last_field == Some(DateTimeField::Second) {
482                        self.parse_optional_precision()?
483                    } else {
484                        None
485                    };
486                    (leading_precision, last_field, fsec_precision)
487                } else {
488                    (leading_precision, None, None)
489                }
490            };
491
492        Ok(Expr::Value(Value::Interval {
493            value,
494            leading_field,
495            leading_precision,
496            last_field,
497            fractional_seconds_precision: fsec_precision,
498        }))
499    }
500
501    /// Parse an operator following an expression
502    pub fn parse_infix(&mut self, expr: Expr, precedence: u8) -> Result<Expr, ParserError> {
503        debug!("parsing infix");
504        let tok = self.next_token().unwrap(); // safe as EOF's precedence is the lowest
505
506        let regular_binary_operator = match tok {
507            Token::Eq => Some(BinaryOperator::Eq),
508            Token::Neq => Some(BinaryOperator::NotEq),
509            Token::Gt => Some(BinaryOperator::Gt),
510            Token::GtEq => Some(BinaryOperator::GtEq),
511            Token::Lt => Some(BinaryOperator::Lt),
512            Token::LtEq => Some(BinaryOperator::LtEq),
513            Token::Plus => Some(BinaryOperator::Plus),
514            Token::Minus => Some(BinaryOperator::Minus),
515            Token::Mult => Some(BinaryOperator::Multiply),
516            Token::Mod => Some(BinaryOperator::Modulus),
517            Token::Div => Some(BinaryOperator::Divide),
518            Token::Word(ref k) => match k.keyword.as_ref() {
519                "AND" => Some(BinaryOperator::And),
520                "OR" => Some(BinaryOperator::Or),
521                "LIKE" => Some(BinaryOperator::Like),
522                "NOT" => {
523                    if self.parse_keyword("LIKE") {
524                        Some(BinaryOperator::NotLike)
525                    } else {
526                        None
527                    }
528                }
529                _ => None,
530            },
531            _ => None,
532        };
533
534        if let Some(op) = regular_binary_operator {
535            Ok(Expr::BinaryOp {
536                left: Box::new(expr),
537                op,
538                right: Box::new(self.parse_subexpr(precedence)?),
539            })
540        } else if let Token::Word(ref k) = tok {
541            match k.keyword.as_ref() {
542                "IS" => {
543                    if self.parse_keyword("NULL") {
544                        Ok(Expr::IsNull(Box::new(expr)))
545                    } else if self.parse_keywords(vec!["NOT", "NULL"]) {
546                        Ok(Expr::IsNotNull(Box::new(expr)))
547                    } else {
548                        self.expected("NULL or NOT NULL after IS", self.peek_token())
549                    }
550                }
551                "NOT" | "IN" | "BETWEEN" => {
552                    self.prev_token();
553                    let negated = self.parse_keyword("NOT");
554                    if self.parse_keyword("IN") {
555                        self.parse_in(expr, negated)
556                    } else if self.parse_keyword("BETWEEN") {
557                        self.parse_between(expr, negated)
558                    } else {
559                        self.expected("IN or BETWEEN after NOT", self.peek_token())
560                    }
561                }
562                // Can only happen if `get_next_precedence` got out of sync with this function
563                _ => panic!("No infix parser for token {:?}", tok),
564            }
565        } else if Token::DoubleColon == tok {
566            self.parse_pg_cast(expr)
567        } else {
568            // Can only happen if `get_next_precedence` got out of sync with this function
569            panic!("No infix parser for token {:?}", tok)
570        }
571    }
572
573    /// Parses the parens following the `[ NOT ] IN` operator
574    pub fn parse_in(&mut self, expr: Expr, negated: bool) -> Result<Expr, ParserError> {
575        self.expect_token(&Token::LParen)?;
576        let in_op = if self.parse_keyword("SELECT") || self.parse_keyword("WITH") {
577            self.prev_token();
578            Expr::InSubquery {
579                expr: Box::new(expr),
580                subquery: Box::new(self.parse_query()?),
581                negated,
582            }
583        } else {
584            Expr::InList {
585                expr: Box::new(expr),
586                list: self.parse_comma_separated(Parser::parse_expr)?,
587                negated,
588            }
589        };
590        self.expect_token(&Token::RParen)?;
591        Ok(in_op)
592    }
593
594    /// Parses `BETWEEN <low> AND <high>`, assuming the `BETWEEN` keyword was already consumed
595    pub fn parse_between(&mut self, expr: Expr, negated: bool) -> Result<Expr, ParserError> {
596        // Stop parsing subexpressions for <low> and <high> on tokens with
597        // precedence lower than that of `BETWEEN`, such as `AND`, `IS`, etc.
598        let low = self.parse_subexpr(Self::BETWEEN_PREC)?;
599        self.expect_keyword("AND")?;
600        let high = self.parse_subexpr(Self::BETWEEN_PREC)?;
601        Ok(Expr::Between {
602            expr: Box::new(expr),
603            negated,
604            low: Box::new(low),
605            high: Box::new(high),
606        })
607    }
608
609    /// Parse a postgresql casting style which is in the form of `expr::datatype`
610    pub fn parse_pg_cast(&mut self, expr: Expr) -> Result<Expr, ParserError> {
611        Ok(Expr::Cast {
612            expr: Box::new(expr),
613            data_type: self.parse_data_type()?,
614        })
615    }
616
617    const UNARY_NOT_PREC: u8 = 15;
618    const BETWEEN_PREC: u8 = 20;
619    const PLUS_MINUS_PREC: u8 = 30;
620
621    /// Get the precedence of the next token
622    pub fn get_next_precedence(&self) -> Result<u8, ParserError> {
623        if let Some(token) = self.peek_token() {
624            debug!("get_next_precedence() {:?}", token);
625
626            match &token {
627                Token::Word(k) if k.keyword == "OR" => Ok(5),
628                Token::Word(k) if k.keyword == "AND" => Ok(10),
629                Token::Word(k) if k.keyword == "NOT" => match &self.peek_nth_token(1) {
630                    // The precedence of NOT varies depending on keyword that
631                    // follows it. If it is followed by IN, BETWEEN, or LIKE,
632                    // it takes on the precedence of those tokens. Otherwise it
633                    // is not an infix operator, and therefore has zero
634                    // precedence.
635                    Some(Token::Word(k)) if k.keyword == "IN" => Ok(Self::BETWEEN_PREC),
636                    Some(Token::Word(k)) if k.keyword == "BETWEEN" => Ok(Self::BETWEEN_PREC),
637                    Some(Token::Word(k)) if k.keyword == "LIKE" => Ok(Self::BETWEEN_PREC),
638                    _ => Ok(0),
639                },
640                Token::Word(k) if k.keyword == "IS" => Ok(17),
641                Token::Word(k) if k.keyword == "IN" => Ok(Self::BETWEEN_PREC),
642                Token::Word(k) if k.keyword == "BETWEEN" => Ok(Self::BETWEEN_PREC),
643                Token::Word(k) if k.keyword == "LIKE" => Ok(Self::BETWEEN_PREC),
644                Token::Eq | Token::Lt | Token::LtEq | Token::Neq | Token::Gt | Token::GtEq => {
645                    Ok(20)
646                }
647                Token::Plus | Token::Minus => Ok(Self::PLUS_MINUS_PREC),
648                Token::Mult | Token::Div | Token::Mod => Ok(40),
649                Token::DoubleColon => Ok(50),
650                _ => Ok(0),
651            }
652        } else {
653            Ok(0)
654        }
655    }
656
657    /// Return the first non-whitespace token that has not yet been processed
658    /// (or None if reached end-of-file)
659    pub fn peek_token(&self) -> Option<Token> {
660        self.peek_nth_token(0)
661    }
662
663    /// Return nth non-whitespace token that has not yet been processed
664    pub fn peek_nth_token(&self, mut n: usize) -> Option<Token> {
665        let mut index = self.index;
666        loop {
667            index += 1;
668            match self.tokens.get(index - 1) {
669                Some(Token::Whitespace(_)) => continue,
670                non_whitespace => {
671                    if n == 0 {
672                        return non_whitespace.cloned();
673                    }
674                    n -= 1;
675                }
676            }
677        }
678    }
679
680    /// Return the first non-whitespace token that has not yet been processed
681    /// (or None if reached end-of-file) and mark it as processed. OK to call
682    /// repeatedly after reaching EOF.
683    pub fn next_token(&mut self) -> Option<Token> {
684        loop {
685            self.index += 1;
686            match self.tokens.get(self.index - 1) {
687                Some(Token::Whitespace(_)) => continue,
688                token => return token.cloned(),
689            }
690        }
691    }
692
693    /// Return the first unprocessed token, possibly whitespace.
694    pub fn next_token_no_skip(&mut self) -> Option<&Token> {
695        self.index += 1;
696        self.tokens.get(self.index - 1)
697    }
698
699    /// Push back the last one non-whitespace token. Must be called after
700    /// `next_token()`, otherwise might panic. OK to call after
701    /// `next_token()` indicates an EOF.
702    pub fn prev_token(&mut self) {
703        loop {
704            assert!(self.index > 0);
705            self.index -= 1;
706            if let Some(Token::Whitespace(_)) = self.tokens.get(self.index) {
707                continue;
708            }
709            return;
710        }
711    }
712
713    /// Report unexpected token
714    fn expected<T>(&self, expected: &str, found: Option<Token>) -> Result<T, ParserError> {
715        parser_err!(format!(
716            "Expected {}, found: {}",
717            expected,
718            found.map_or_else(|| "EOF".to_string(), |t| format!("{}", t))
719        ))
720    }
721
722    /// Look for an expected keyword and consume it if it exists
723    #[must_use]
724    pub fn parse_keyword(&mut self, expected: &'static str) -> bool {
725        // Ideally, we'd accept a enum variant, not a string, but since
726        // it's not trivial to maintain the enum without duplicating all
727        // the keywords three times, we'll settle for a run-time check that
728        // the string actually represents a known keyword...
729        assert!(keywords::ALL_KEYWORDS.contains(&expected));
730        match self.peek_token() {
731            Some(Token::Word(ref k)) if expected.eq_ignore_ascii_case(&k.keyword) => {
732                self.next_token();
733                true
734            }
735            _ => false,
736        }
737    }
738
739    /// Look for an expected sequence of keywords and consume them if they exist
740    #[must_use]
741    pub fn parse_keywords(&mut self, keywords: Vec<&'static str>) -> bool {
742        let index = self.index;
743        for keyword in keywords {
744            if !self.parse_keyword(keyword) {
745                //println!("parse_keywords aborting .. did not find {}", keyword);
746                // reset index and return immediately
747                self.index = index;
748                return false;
749            }
750        }
751        true
752    }
753
754    /// Look for one of the given keywords and return the one that matches.
755    #[must_use]
756    pub fn parse_one_of_keywords(&mut self, keywords: &[&'static str]) -> Option<&'static str> {
757        for keyword in keywords {
758            assert!(
759                keywords::ALL_KEYWORDS.contains(keyword),
760                "{} is not contained in keyword list",
761                keyword
762            );
763        }
764        match self.peek_token() {
765            Some(Token::Word(ref k)) => keywords
766                .iter()
767                .find(|keyword| keyword.eq_ignore_ascii_case(&k.keyword))
768                .map(|keyword| {
769                    self.next_token();
770                    *keyword
771                }),
772            _ => None,
773        }
774    }
775
776    /// Bail out if the current token is not one of the expected keywords, or consume it if it is
777    pub fn expect_one_of_keywords(
778        &mut self,
779        keywords: &[&'static str],
780    ) -> Result<&'static str, ParserError> {
781        if let Some(keyword) = self.parse_one_of_keywords(keywords) {
782            Ok(keyword)
783        } else {
784            self.expected(
785                &format!("one of {}", keywords.join(" or ")),
786                self.peek_token(),
787            )
788        }
789    }
790
791    /// Bail out if the current token is not an expected keyword, or consume it if it is
792    pub fn expect_keyword(&mut self, expected: &'static str) -> Result<(), ParserError> {
793        if self.parse_keyword(expected) {
794            Ok(())
795        } else {
796            self.expected(expected, self.peek_token())
797        }
798    }
799
800    /// Bail out if the following tokens are not the expected sequence of
801    /// keywords, or consume them if they are.
802    pub fn expect_keywords(&mut self, expected: &[&'static str]) -> Result<(), ParserError> {
803        for kw in expected {
804            self.expect_keyword(kw)?;
805        }
806        Ok(())
807    }
808
809    /// Consume the next token if it matches the expected token, otherwise return false
810    #[must_use]
811    pub fn consume_token(&mut self, expected: &Token) -> bool {
812        match &self.peek_token() {
813            Some(t) if *t == *expected => {
814                self.next_token();
815                true
816            }
817            _ => false,
818        }
819    }
820
821    /// Bail out if the current token is not an expected keyword, or consume it if it is
822    pub fn expect_token(&mut self, expected: &Token) -> Result<(), ParserError> {
823        if self.consume_token(expected) {
824            Ok(())
825        } else {
826            self.expected(&expected.to_string(), self.peek_token())
827        }
828    }
829
830    /// Parse a comma-separated list of 1+ items accepted by `F`
831    pub fn parse_comma_separated<T, F>(&mut self, mut f: F) -> Result<Vec<T>, ParserError>
832    where
833        F: FnMut(&mut Parser) -> Result<T, ParserError>,
834    {
835        let mut values = vec![];
836        loop {
837            values.push(f(self)?);
838            if !self.consume_token(&Token::Comma) {
839                break;
840            }
841        }
842        Ok(values)
843    }
844
845    /// Parse a SQL CREATE statement
846    pub fn parse_create(&mut self) -> Result<Statement, ParserError> {
847        if self.parse_keyword("TABLE") {
848            self.parse_create_table()
849        } else if self.parse_keyword("MATERIALIZED") || self.parse_keyword("VIEW") {
850            self.prev_token();
851            self.parse_create_view()
852        } else if self.parse_keyword("EXTERNAL") {
853            self.parse_create_external_table()
854        } else {
855            self.expected("TABLE or VIEW after CREATE", self.peek_token())
856        }
857    }
858
859    pub fn parse_create_external_table(&mut self) -> Result<Statement, ParserError> {
860        self.expect_keyword("TABLE")?;
861        let table_name = self.parse_object_name()?;
862        let (columns, constraints) = self.parse_columns()?;
863        self.expect_keywords(&["STORED", "AS"])?;
864        let file_format = self.parse_identifier()?.value.parse::<FileFormat>()?;
865
866        self.expect_keyword("LOCATION")?;
867        let location = self.parse_literal_string()?;
868
869        Ok(Statement::CreateTable {
870            name: table_name,
871            if_not_exists: false,
872            columns,
873            constraints,
874            with_options: vec![],
875            external: true,
876            file_format: Some(file_format),
877            location: Some(location),
878        })
879    }
880
881    pub fn parse_create_view(&mut self) -> Result<Statement, ParserError> {
882        let materialized = self.parse_keyword("MATERIALIZED");
883        self.expect_keyword("VIEW")?;
884        // Many dialects support `OR REPLACE` | `OR ALTER` right after `CREATE`, but we don't (yet).
885        // ANSI SQL and Postgres support RECURSIVE here, but we don't support it either.
886        let name = self.parse_object_name()?;
887        let columns = self.parse_parenthesized_column_list(Optional)?;
888        let with_options = self.parse_with_options()?;
889        self.expect_keyword("AS")?;
890        let query = Box::new(self.parse_query()?);
891        // Optional `WITH [ CASCADED | LOCAL ] CHECK OPTION` is widely supported here.
892        Ok(Statement::CreateView {
893            name,
894            columns,
895            query,
896            materialized,
897            with_options,
898        })
899    }
900
901    pub fn parse_drop(&mut self) -> Result<Statement, ParserError> {
902        let object_type = if self.parse_keyword("TABLE") {
903            ObjectType::Table
904        } else if self.parse_keyword("VIEW") {
905            ObjectType::View
906        } else {
907            return self.expected("TABLE or VIEW after DROP", self.peek_token());
908        };
909        // Many dialects support the non standard `IF EXISTS` clause and allow
910        // specifying multiple objects to delete in a single statement
911        let if_exists = self.parse_keywords(vec!["IF", "EXISTS"]);
912        let names = self.parse_comma_separated(Parser::parse_object_name)?;
913        let cascade = self.parse_keyword("CASCADE");
914        let restrict = self.parse_keyword("RESTRICT");
915        if cascade && restrict {
916            return parser_err!("Cannot specify both CASCADE and RESTRICT in DROP");
917        }
918        Ok(Statement::Drop {
919            object_type,
920            if_exists,
921            names,
922            cascade,
923        })
924    }
925
926    pub fn parse_create_table(&mut self) -> Result<Statement, ParserError> {
927        let if_not_exists = self.parse_keywords(vec!["IF", "NOT", "EXISTS"]);
928        let table_name = self.parse_object_name()?;
929        // parse optional column list (schema)
930        let (columns, constraints) = self.parse_columns()?;
931        let with_options = self.parse_with_options()?;
932
933        Ok(Statement::CreateTable {
934            name: table_name,
935            if_not_exists,
936            columns,
937            constraints,
938            with_options,
939            external: false,
940            file_format: None,
941            location: None,
942        })
943    }
944
945    fn parse_column_def(&mut self) -> Result<ColumnDef, ParserError> {
946        if let Some(Token::Word(column_name)) = self.peek_token() {
947            self.next_token();
948            let data_type = self.parse_data_type()?;
949            let collation = if self.parse_keyword("COLLATE") {
950                Some(self.parse_object_name()?)
951            } else {
952                None
953            };
954            let mut options = vec![];
955            loop {
956                match self.peek_token() {
957                    None | Some(Token::Comma) | Some(Token::RParen) => break,
958                    _ => options.push(self.parse_column_option_def()?),
959                }
960            }
961
962            Ok(ColumnDef {
963                name: column_name.to_ident(),
964                data_type,
965                collation,
966                options,
967            })
968        } else {
969            self.expected("column name", self.peek_token())
970        }
971    }
972
973    fn parse_columns(&mut self) -> Result<(Vec<ColumnDef>, Vec<TableConstraint>), ParserError> {
974        let mut columns = vec![];
975        let mut constraints = vec![];
976        if !self.consume_token(&Token::LParen) || self.consume_token(&Token::RParen) {
977            return Ok((columns, constraints));
978        }
979
980        loop {
981            if let Some(constraint) = self.parse_optional_table_constraint()? {
982                constraints.push(constraint);
983            } else if let Some(Token::Word(_column_name)) = self.peek_token() {
984                columns.push(self.parse_column_def()?);
985            } else {
986                return self.expected("column name or constraint definition", self.peek_token());
987            }
988            let comma = self.consume_token(&Token::Comma);
989            if self.consume_token(&Token::RParen) {
990                // allow a trailing comma, even though it's not in standard
991                break;
992            } else if !comma {
993                return self.expected("',' or ')' after column definition", self.peek_token());
994            }
995        }
996
997        Ok((columns, constraints))
998    }
999
1000    pub fn parse_column_option_def(&mut self) -> Result<ColumnOptionDef, ParserError> {
1001        let name = if self.parse_keyword("CONSTRAINT") {
1002            Some(self.parse_identifier()?)
1003        } else {
1004            None
1005        };
1006
1007        let option = if self.parse_keywords(vec!["NOT", "NULL"]) {
1008            ColumnOption::NotNull
1009        } else if self.parse_keyword("NULL") {
1010            ColumnOption::Null
1011        } else if self.parse_keyword("DEFAULT") {
1012            ColumnOption::Default(self.parse_expr()?)
1013        } else if self.parse_keywords(vec!["PRIMARY", "KEY"]) {
1014            ColumnOption::Unique { is_primary: true }
1015        } else if self.parse_keyword("UNIQUE") {
1016            ColumnOption::Unique { is_primary: false }
1017        } else if self.parse_keyword("REFERENCES") {
1018            let foreign_table = self.parse_object_name()?;
1019            let referred_columns = self.parse_parenthesized_column_list(Mandatory)?;
1020            ColumnOption::ForeignKey {
1021                foreign_table,
1022                referred_columns,
1023            }
1024        } else if self.parse_keyword("CHECK") {
1025            self.expect_token(&Token::LParen)?;
1026            let expr = self.parse_expr()?;
1027            self.expect_token(&Token::RParen)?;
1028            ColumnOption::Check(expr)
1029        } else {
1030            return self.expected("column option", self.peek_token());
1031        };
1032
1033        Ok(ColumnOptionDef { name, option })
1034    }
1035
1036    pub fn parse_optional_table_constraint(
1037        &mut self,
1038    ) -> Result<Option<TableConstraint>, ParserError> {
1039        let name = if self.parse_keyword("CONSTRAINT") {
1040            Some(self.parse_identifier()?)
1041        } else {
1042            None
1043        };
1044        match self.next_token() {
1045            Some(Token::Word(ref k)) if k.keyword == "PRIMARY" || k.keyword == "UNIQUE" => {
1046                let is_primary = k.keyword == "PRIMARY";
1047                if is_primary {
1048                    self.expect_keyword("KEY")?;
1049                }
1050                let columns = self.parse_parenthesized_column_list(Mandatory)?;
1051                Ok(Some(TableConstraint::Unique {
1052                    name,
1053                    columns,
1054                    is_primary,
1055                }))
1056            }
1057            Some(Token::Word(ref k)) if k.keyword == "FOREIGN" => {
1058                self.expect_keyword("KEY")?;
1059                let columns = self.parse_parenthesized_column_list(Mandatory)?;
1060                self.expect_keyword("REFERENCES")?;
1061                let foreign_table = self.parse_object_name()?;
1062                let referred_columns = self.parse_parenthesized_column_list(Mandatory)?;
1063                Ok(Some(TableConstraint::ForeignKey {
1064                    name,
1065                    columns,
1066                    foreign_table,
1067                    referred_columns,
1068                }))
1069            }
1070            Some(Token::Word(ref k)) if k.keyword == "CHECK" => {
1071                self.expect_token(&Token::LParen)?;
1072                let expr = Box::new(self.parse_expr()?);
1073                self.expect_token(&Token::RParen)?;
1074                Ok(Some(TableConstraint::Check { name, expr }))
1075            }
1076            unexpected => {
1077                if name.is_some() {
1078                    self.expected("PRIMARY, UNIQUE, FOREIGN, or CHECK", unexpected)
1079                } else {
1080                    self.prev_token();
1081                    Ok(None)
1082                }
1083            }
1084        }
1085    }
1086
1087    pub fn parse_with_options(&mut self) -> Result<Vec<SqlOption>, ParserError> {
1088        if self.parse_keyword("WITH") {
1089            self.expect_token(&Token::LParen)?;
1090            let options = self.parse_comma_separated(Parser::parse_sql_option)?;
1091            self.expect_token(&Token::RParen)?;
1092            Ok(options)
1093        } else {
1094            Ok(vec![])
1095        }
1096    }
1097
1098    pub fn parse_sql_option(&mut self) -> Result<SqlOption, ParserError> {
1099        let name = self.parse_identifier()?;
1100        self.expect_token(&Token::Eq)?;
1101        let value = self.parse_value()?;
1102        Ok(SqlOption { name, value })
1103    }
1104
1105    pub fn parse_alter(&mut self) -> Result<Statement, ParserError> {
1106        self.expect_keyword("TABLE")?;
1107        let _ = self.parse_keyword("ONLY");
1108        let table_name = self.parse_object_name()?;
1109        let operation = if self.parse_keyword("ADD") {
1110            if self.parse_keyword("COLUMN") {
1111                AlterTableOperation::AddColumn(self.parse_column_def()?)
1112            } else if let Some(constraint) = self.parse_optional_table_constraint()? {
1113                AlterTableOperation::AddConstraint(constraint)
1114            } else {
1115                return self.expected(
1116                    "a column or a constraint in ALTER TABLE .. ADD",
1117                    self.peek_token(),
1118                );
1119            }
1120        } else if self.parse_keyword("DROP") {
1121            if self.parse_keyword("COLUMN") {
1122                let if_exists = self.parse_keywords(vec!["IF", "EXISTS"]);
1123                let column = self.parse_identifier()?;
1124                let cascade = self.parse_keyword("CASCADE");
1125                AlterTableOperation::DropColumn {
1126                    column,
1127                    if_exists,
1128                    cascade,
1129                }
1130            } else {
1131                return self.expected("a column in ALTER TABLE .. DROP", self.peek_token());
1132            }
1133        } else {
1134            return self.expected("ADD or DROP after ALTER TABLE", self.peek_token());
1135        };
1136        Ok(Statement::AlterTable {
1137            name: table_name,
1138            operation,
1139        })
1140    }
1141
1142    /// Parse a copy statement
1143    pub fn parse_copy(&mut self) -> Result<Statement, ParserError> {
1144        let table_name = self.parse_object_name()?;
1145        let columns = self.parse_parenthesized_column_list(Optional)?;
1146        self.expect_keywords(&["FROM", "STDIN"])?;
1147        self.expect_token(&Token::SemiColon)?;
1148        let values = self.parse_tsv()?;
1149        Ok(Statement::Copy {
1150            table_name,
1151            columns,
1152            values,
1153        })
1154    }
1155
1156    /// Parse a tab separated values in
1157    /// COPY payload
1158    fn parse_tsv(&mut self) -> Result<Vec<Option<String>>, ParserError> {
1159        let values = self.parse_tab_value()?;
1160        Ok(values)
1161    }
1162
1163    fn parse_tab_value(&mut self) -> Result<Vec<Option<String>>, ParserError> {
1164        let mut values = vec![];
1165        let mut content = String::from("");
1166        while let Some(t) = self.next_token_no_skip() {
1167            match t {
1168                Token::Whitespace(Whitespace::Tab) => {
1169                    values.push(Some(content.to_string()));
1170                    content.clear();
1171                }
1172                Token::Whitespace(Whitespace::Newline) => {
1173                    values.push(Some(content.to_string()));
1174                    content.clear();
1175                }
1176                Token::Backslash => {
1177                    if self.consume_token(&Token::Period) {
1178                        return Ok(values);
1179                    }
1180                    if let Some(token) = self.next_token() {
1181                        if let Token::Word(Word { value: v, .. }) = token {
1182                            if v == "N" {
1183                                values.push(None);
1184                            }
1185                        }
1186                    } else {
1187                        continue;
1188                    }
1189                }
1190                _ => {
1191                    content.push_str(&t.to_string());
1192                }
1193            }
1194        }
1195        Ok(values)
1196    }
1197
1198    /// Parse a literal value (numbers, strings, date/time, booleans)
1199    fn parse_value(&mut self) -> Result<Value, ParserError> {
1200        match self.next_token() {
1201            Some(t) => match t {
1202                Token::Word(k) => match k.keyword.as_ref() {
1203                    "TRUE" => Ok(Value::Boolean(true)),
1204                    "FALSE" => Ok(Value::Boolean(false)),
1205                    "NULL" => Ok(Value::Null),
1206                    _ => {
1207                        parser_err!(format!("No value parser for keyword {}", k.keyword))
1208                    }
1209                },
1210                // The call to n.parse() returns a bigdecimal when the
1211                // bigdecimal feature is enabled, and is otherwise a no-op
1212                // (i.e., it returns the input string).
1213                Token::Number(ref n) => match n.parse() {
1214                    Ok(n) => Ok(Value::Number(n)),
1215                    Err(e) => parser_err!(format!("Could not parse '{}' as number: {}", n, e)),
1216                },
1217                Token::SingleQuotedString(ref s) => Ok(Value::SingleQuotedString(s.to_string())),
1218                Token::NationalStringLiteral(ref s) => {
1219                    Ok(Value::NationalStringLiteral(s.to_string()))
1220                }
1221                Token::HexStringLiteral(ref s) => Ok(Value::HexStringLiteral(s.to_string())),
1222                _ => parser_err!(format!("Unsupported value: {:?}", t)),
1223            },
1224            None => parser_err!("Expecting a value, but found EOF"),
1225        }
1226    }
1227
1228    pub fn parse_number_value(&mut self) -> Result<Value, ParserError> {
1229        match self.parse_value()? {
1230            v @ Value::Number(_) => Ok(v),
1231            _ => {
1232                self.prev_token();
1233                self.expected("literal number", self.peek_token())
1234            }
1235        }
1236    }
1237
1238    /// Parse an unsigned literal integer/long
1239    pub fn parse_literal_uint(&mut self) -> Result<u64, ParserError> {
1240        match self.next_token() {
1241            Some(Token::Number(s)) => s.parse::<u64>().map_err(|e| {
1242                ParserError::ParserError(format!("Could not parse '{}' as u64: {}", s, e))
1243            }),
1244            other => self.expected("literal int", other),
1245        }
1246    }
1247
1248    /// Parse a literal string
1249    pub fn parse_literal_string(&mut self) -> Result<String, ParserError> {
1250        match self.next_token() {
1251            Some(Token::SingleQuotedString(ref s)) => Ok(s.clone()),
1252            other => self.expected("literal string", other),
1253        }
1254    }
1255
1256    /// Parse a SQL datatype (in the context of a CREATE TABLE statement for example)
1257    pub fn parse_data_type(&mut self) -> Result<DataType, ParserError> {
1258        match self.next_token() {
1259            Some(Token::Word(k)) => match k.keyword.as_ref() {
1260                "BOOLEAN" => Ok(DataType::Boolean),
1261                "FLOAT" => Ok(DataType::Float(self.parse_optional_precision()?)),
1262                "REAL" => Ok(DataType::Real),
1263                "DOUBLE" => {
1264                    let _ = self.parse_keyword("PRECISION");
1265                    Ok(DataType::Double)
1266                }
1267                "SMALLINT" => Ok(DataType::SmallInt),
1268                "INT" | "INTEGER" => Ok(DataType::Int),
1269                "BIGINT" => Ok(DataType::BigInt),
1270                "VARCHAR" => Ok(DataType::Varchar(self.parse_optional_precision()?)),
1271                "CHAR" | "CHARACTER" => {
1272                    if self.parse_keyword("VARYING") {
1273                        Ok(DataType::Varchar(self.parse_optional_precision()?))
1274                    } else {
1275                        Ok(DataType::Char(self.parse_optional_precision()?))
1276                    }
1277                }
1278                "UUID" => Ok(DataType::Uuid),
1279                "DATE" => Ok(DataType::Date),
1280                "TIMESTAMP" => {
1281                    // TBD: we throw away "with/without timezone" information
1282                    if self.parse_keyword("WITH") || self.parse_keyword("WITHOUT") {
1283                        self.expect_keywords(&["TIME", "ZONE"])?;
1284                    }
1285                    Ok(DataType::Timestamp)
1286                }
1287                "TIME" => {
1288                    // TBD: we throw away "with/without timezone" information
1289                    if self.parse_keyword("WITH") || self.parse_keyword("WITHOUT") {
1290                        self.expect_keywords(&["TIME", "ZONE"])?;
1291                    }
1292                    Ok(DataType::Time)
1293                }
1294                // Interval types can be followed by a complicated interval
1295                // qualifier that we don't currently support. See
1296                // parse_interval_literal for a taste.
1297                "INTERVAL" => Ok(DataType::Interval),
1298                "REGCLASS" => Ok(DataType::Regclass),
1299                "TEXT" => {
1300                    if self.consume_token(&Token::LBracket) {
1301                        // Note: this is postgresql-specific
1302                        self.expect_token(&Token::RBracket)?;
1303                        Ok(DataType::Array(Box::new(DataType::Text)))
1304                    } else {
1305                        Ok(DataType::Text)
1306                    }
1307                }
1308                "BYTEA" => Ok(DataType::Bytea),
1309                "NUMERIC" | "DECIMAL" | "DEC" => {
1310                    let (precision, scale) = self.parse_optional_precision_scale()?;
1311                    Ok(DataType::Decimal(precision, scale))
1312                }
1313                _ => {
1314                    self.prev_token();
1315                    let type_name = self.parse_object_name()?;
1316                    Ok(DataType::Custom(type_name))
1317                }
1318            },
1319            other => self.expected("a data type name", other),
1320        }
1321    }
1322
1323    /// Parse `AS identifier` (or simply `identifier` if it's not a reserved keyword)
1324    /// Some examples with aliases: `SELECT 1 foo`, `SELECT COUNT(*) AS cnt`,
1325    /// `SELECT ... FROM t1 foo, t2 bar`, `SELECT ... FROM (...) AS bar`
1326    pub fn parse_optional_alias(
1327        &mut self,
1328        reserved_kwds: &[&str],
1329    ) -> Result<Option<Ident>, ParserError> {
1330        let after_as = self.parse_keyword("AS");
1331        match self.next_token() {
1332            // Accept any identifier after `AS` (though many dialects have restrictions on
1333            // keywords that may appear here). If there's no `AS`: don't parse keywords,
1334            // which may start a construct allowed in this position, to be parsed as aliases.
1335            // (For example, in `FROM t1 JOIN` the `JOIN` will always be parsed as a keyword,
1336            // not an alias.)
1337            Some(Token::Word(ref w))
1338                if after_as || !reserved_kwds.contains(&w.keyword.as_str()) =>
1339            {
1340                Ok(Some(w.to_ident()))
1341            }
1342            // MSSQL supports single-quoted strings as aliases for columns
1343            // We accept them as table aliases too, although MSSQL does not.
1344            Some(Token::SingleQuotedString(ref s)) => Ok(Some(Ident::with_quote('\'', s.clone()))),
1345            not_an_ident => {
1346                if after_as {
1347                    return self.expected("an identifier after AS", not_an_ident);
1348                }
1349                self.prev_token();
1350                Ok(None) // no alias found
1351            }
1352        }
1353    }
1354
1355    /// Parse `AS identifier` when the AS is describing a table-valued object,
1356    /// like in `... FROM generate_series(1, 10) AS t (col)`. In this case
1357    /// the alias is allowed to optionally name the columns in the table, in
1358    /// addition to the table itself.
1359    pub fn parse_optional_table_alias(
1360        &mut self,
1361        reserved_kwds: &[&str],
1362    ) -> Result<Option<TableAlias>, ParserError> {
1363        match self.parse_optional_alias(reserved_kwds)? {
1364            Some(name) => {
1365                let columns = self.parse_parenthesized_column_list(Optional)?;
1366                Ok(Some(TableAlias { name, columns }))
1367            }
1368            None => Ok(None),
1369        }
1370    }
1371
1372    /// Parse a possibly qualified, possibly quoted identifier, e.g.
1373    /// `foo` or `myschema."table"`
1374    pub fn parse_object_name(&mut self) -> Result<ObjectName, ParserError> {
1375        let mut idents = vec![];
1376        loop {
1377            idents.push(self.parse_identifier()?);
1378            if !self.consume_token(&Token::Period) {
1379                break;
1380            }
1381        }
1382        Ok(ObjectName(idents))
1383    }
1384
1385    /// Parse a simple one-word identifier (possibly quoted, possibly a keyword)
1386    pub fn parse_identifier(&mut self) -> Result<Ident, ParserError> {
1387        match self.next_token() {
1388            Some(Token::Word(w)) => Ok(w.to_ident()),
1389            unexpected => self.expected("identifier", unexpected),
1390        }
1391    }
1392
1393    /// Parse a parenthesized comma-separated list of unqualified, possibly quoted identifiers
1394    pub fn parse_parenthesized_column_list(
1395        &mut self,
1396        optional: IsOptional,
1397    ) -> Result<Vec<Ident>, ParserError> {
1398        if self.consume_token(&Token::LParen) {
1399            let cols = self.parse_comma_separated(Parser::parse_identifier)?;
1400            self.expect_token(&Token::RParen)?;
1401            Ok(cols)
1402        } else if optional == Optional {
1403            Ok(vec![])
1404        } else {
1405            self.expected("a list of columns in parentheses", self.peek_token())
1406        }
1407    }
1408
1409    pub fn parse_optional_precision(&mut self) -> Result<Option<u64>, ParserError> {
1410        if self.consume_token(&Token::LParen) {
1411            let n = self.parse_literal_uint()?;
1412            self.expect_token(&Token::RParen)?;
1413            Ok(Some(n))
1414        } else {
1415            Ok(None)
1416        }
1417    }
1418
1419    pub fn parse_optional_precision_scale(
1420        &mut self,
1421    ) -> Result<(Option<u64>, Option<u64>), ParserError> {
1422        if self.consume_token(&Token::LParen) {
1423            let n = self.parse_literal_uint()?;
1424            let scale = if self.consume_token(&Token::Comma) {
1425                Some(self.parse_literal_uint()?)
1426            } else {
1427                None
1428            };
1429            self.expect_token(&Token::RParen)?;
1430            Ok((Some(n), scale))
1431        } else {
1432            Ok((None, None))
1433        }
1434    }
1435
1436    pub fn parse_delete(&mut self) -> Result<Statement, ParserError> {
1437        self.expect_keyword("FROM")?;
1438        let table_name = self.parse_object_name()?;
1439        let selection = if self.parse_keyword("WHERE") {
1440            Some(self.parse_expr()?)
1441        } else {
1442            None
1443        };
1444
1445        Ok(Statement::Delete {
1446            table_name,
1447            selection,
1448        })
1449    }
1450
1451    /// Parse a query expression, i.e. a `SELECT` statement optionally
1452    /// preceeded with some `WITH` CTE declarations and optionally followed
1453    /// by `ORDER BY`. Unlike some other parse_... methods, this one doesn't
1454    /// expect the initial keyword to be already consumed
1455    pub fn parse_query(&mut self) -> Result<Query, ParserError> {
1456        let ctes = if self.parse_keyword("WITH") {
1457            // TODO: optional RECURSIVE
1458            self.parse_comma_separated(Parser::parse_cte)?
1459        } else {
1460            vec![]
1461        };
1462
1463        let body = self.parse_query_body(0)?;
1464
1465        let order_by = if self.parse_keywords(vec!["ORDER", "BY"]) {
1466            self.parse_comma_separated(Parser::parse_order_by_expr)?
1467        } else {
1468            vec![]
1469        };
1470
1471        let limit = if self.parse_keyword("LIMIT") {
1472            self.parse_limit()?
1473        } else {
1474            None
1475        };
1476
1477        let offset = if self.parse_keyword("OFFSET") {
1478            Some(self.parse_offset()?)
1479        } else {
1480            None
1481        };
1482
1483        let fetch = if self.parse_keyword("FETCH") {
1484            Some(self.parse_fetch()?)
1485        } else {
1486            None
1487        };
1488
1489        Ok(Query {
1490            ctes,
1491            body,
1492            limit,
1493            order_by,
1494            offset,
1495            fetch,
1496        })
1497    }
1498
1499    /// Parse a CTE (`alias [( col1, col2, ... )] AS (subquery)`)
1500    fn parse_cte(&mut self) -> Result<Cte, ParserError> {
1501        let alias = TableAlias {
1502            name: self.parse_identifier()?,
1503            columns: self.parse_parenthesized_column_list(Optional)?,
1504        };
1505        self.expect_keyword("AS")?;
1506        self.expect_token(&Token::LParen)?;
1507        let query = self.parse_query()?;
1508        self.expect_token(&Token::RParen)?;
1509        Ok(Cte { alias, query })
1510    }
1511
1512    /// Parse a "query body", which is an expression with roughly the
1513    /// following grammar:
1514    /// ```text
1515    ///   query_body ::= restricted_select | '(' subquery ')' | set_operation
1516    ///   restricted_select ::= 'SELECT' [expr_list] [ from ] [ where ] [ groupby_having ]
1517    ///   subquery ::= query_body [ order_by_limit ]
1518    ///   set_operation ::= query_body { 'UNION' | 'EXCEPT' | 'INTERSECT' } [ 'ALL' ] query_body
1519    /// ```
1520    fn parse_query_body(&mut self, precedence: u8) -> Result<SetExpr, ParserError> {
1521        // We parse the expression using a Pratt parser, as in `parse_expr()`.
1522        // Start by parsing a restricted SELECT or a `(subquery)`:
1523        let mut expr = if self.parse_keyword("SELECT") {
1524            SetExpr::Select(Box::new(self.parse_select()?))
1525        } else if self.consume_token(&Token::LParen) {
1526            // CTEs are not allowed here, but the parser currently accepts them
1527            let subquery = self.parse_query()?;
1528            self.expect_token(&Token::RParen)?;
1529            SetExpr::Query(Box::new(subquery))
1530        } else if self.parse_keyword("VALUES") {
1531            SetExpr::Values(self.parse_values()?)
1532        } else {
1533            return self.expected(
1534                "SELECT, VALUES, or a subquery in the query body",
1535                self.peek_token(),
1536            );
1537        };
1538
1539        loop {
1540            // The query can be optionally followed by a set operator:
1541            let next_token = self.peek_token();
1542            let op = self.parse_set_operator(&next_token);
1543            let next_precedence = match op {
1544                // UNION and EXCEPT have the same binding power and evaluate left-to-right
1545                Some(SetOperator::Union) | Some(SetOperator::Except) => 10,
1546                // INTERSECT has higher precedence than UNION/EXCEPT
1547                Some(SetOperator::Intersect) => 20,
1548                // Unexpected token or EOF => stop parsing the query body
1549                None => break,
1550            };
1551            if precedence >= next_precedence {
1552                break;
1553            }
1554            self.next_token(); // skip past the set operator
1555            expr = SetExpr::SetOperation {
1556                left: Box::new(expr),
1557                op: op.unwrap(),
1558                all: self.parse_keyword("ALL"),
1559                right: Box::new(self.parse_query_body(next_precedence)?),
1560            };
1561        }
1562
1563        Ok(expr)
1564    }
1565
1566    fn parse_set_operator(&mut self, token: &Option<Token>) -> Option<SetOperator> {
1567        match token {
1568            Some(Token::Word(w)) if w.keyword == "UNION" => Some(SetOperator::Union),
1569            Some(Token::Word(w)) if w.keyword == "EXCEPT" => Some(SetOperator::Except),
1570            Some(Token::Word(w)) if w.keyword == "INTERSECT" => Some(SetOperator::Intersect),
1571            _ => None,
1572        }
1573    }
1574
1575    /// Parse a restricted `SELECT` statement (no CTEs / `UNION` / `ORDER BY`),
1576    /// assuming the initial `SELECT` was already consumed
1577    pub fn parse_select(&mut self) -> Result<Select, ParserError> {
1578        let all = self.parse_keyword("ALL");
1579        let distinct = self.parse_keyword("DISTINCT");
1580        if all && distinct {
1581            return parser_err!("Cannot specify both ALL and DISTINCT in SELECT");
1582        }
1583        let projection = self.parse_comma_separated(Parser::parse_select_item)?;
1584
1585        // Note that for keywords to be properly handled here, they need to be
1586        // added to `RESERVED_FOR_COLUMN_ALIAS` / `RESERVED_FOR_TABLE_ALIAS`,
1587        // otherwise they may be parsed as an alias as part of the `projection`
1588        // or `from`.
1589
1590        let from = if self.parse_keyword("FROM") {
1591            self.parse_comma_separated(Parser::parse_table_and_joins)?
1592        } else {
1593            vec![]
1594        };
1595
1596        let selection = if self.parse_keyword("WHERE") {
1597            Some(self.parse_expr()?)
1598        } else {
1599            None
1600        };
1601
1602        let group_by = if self.parse_keywords(vec!["GROUP", "BY"]) {
1603            self.parse_comma_separated(Parser::parse_expr)?
1604        } else {
1605            vec![]
1606        };
1607
1608        let having = if self.parse_keyword("HAVING") {
1609            Some(self.parse_expr()?)
1610        } else {
1611            None
1612        };
1613
1614        Ok(Select {
1615            distinct,
1616            projection,
1617            from,
1618            selection,
1619            group_by,
1620            having,
1621        })
1622    }
1623
1624    pub fn parse_table_and_joins(&mut self) -> Result<TableWithJoins, ParserError> {
1625        let relation = self.parse_table_factor()?;
1626
1627        // Note that for keywords to be properly handled here, they need to be
1628        // added to `RESERVED_FOR_TABLE_ALIAS`, otherwise they may be parsed as
1629        // a table alias.
1630        let mut joins = vec![];
1631        loop {
1632            let join = if self.parse_keyword("CROSS") {
1633                let join_operator = if self.parse_keyword("JOIN") {
1634                    JoinOperator::CrossJoin
1635                } else if self.parse_keyword("APPLY") {
1636                    // MSSQL extension, similar to CROSS JOIN LATERAL
1637                    JoinOperator::CrossApply
1638                } else {
1639                    return self.expected("JOIN or APPLY after CROSS", self.peek_token());
1640                };
1641                Join {
1642                    relation: self.parse_table_factor()?,
1643                    join_operator,
1644                }
1645            } else if self.parse_keyword("OUTER") {
1646                // MSSQL extension, similar to LEFT JOIN LATERAL .. ON 1=1
1647                self.expect_keyword("APPLY")?;
1648                Join {
1649                    relation: self.parse_table_factor()?,
1650                    join_operator: JoinOperator::OuterApply,
1651                }
1652            } else {
1653                let natural = self.parse_keyword("NATURAL");
1654                let peek_keyword = if let Some(Token::Word(kw)) = self.peek_token() {
1655                    kw.keyword
1656                } else {
1657                    String::default()
1658                };
1659
1660                let join_operator_type = match peek_keyword.as_ref() {
1661                    "INNER" | "JOIN" => {
1662                        let _ = self.parse_keyword("INNER");
1663                        self.expect_keyword("JOIN")?;
1664                        JoinOperator::Inner
1665                    }
1666                    kw @ "LEFT" | kw @ "RIGHT" | kw @ "FULL" => {
1667                        let _ = self.next_token();
1668                        let _ = self.parse_keyword("OUTER");
1669                        self.expect_keyword("JOIN")?;
1670                        match kw {
1671                            "LEFT" => JoinOperator::LeftOuter,
1672                            "RIGHT" => JoinOperator::RightOuter,
1673                            "FULL" => JoinOperator::FullOuter,
1674                            _ => unreachable!(),
1675                        }
1676                    }
1677                    "OUTER" => return self.expected("LEFT, RIGHT, or FULL", self.peek_token()),
1678                    _ if natural => {
1679                        return self.expected("a join type after NATURAL", self.peek_token());
1680                    }
1681                    _ => break,
1682                };
1683                let relation = self.parse_table_factor()?;
1684                let join_constraint = self.parse_join_constraint(natural)?;
1685                Join {
1686                    relation,
1687                    join_operator: join_operator_type(join_constraint),
1688                }
1689            };
1690            joins.push(join);
1691        }
1692        Ok(TableWithJoins { relation, joins })
1693    }
1694
1695    /// A table name or a parenthesized subquery, followed by optional `[AS] alias`
1696    pub fn parse_table_factor(&mut self) -> Result<TableFactor, ParserError> {
1697        if self.parse_keyword("LATERAL") {
1698            // LATERAL must always be followed by a subquery.
1699            if !self.consume_token(&Token::LParen) {
1700                self.expected("subquery after LATERAL", self.peek_token())?;
1701            }
1702            return self.parse_derived_table_factor(Lateral);
1703        }
1704
1705        if self.consume_token(&Token::LParen) {
1706            let index = self.index;
1707            // A left paren introduces either a derived table (i.e., a subquery)
1708            // or a nested join. It's nearly impossible to determine ahead of
1709            // time which it is... so we just try to parse both.
1710            //
1711            // Here's an example that demonstrates the complexity:
1712            //                     /-------------------------------------------------------\
1713            //                     | /-----------------------------------\                 |
1714            //     SELECT * FROM ( ( ( (SELECT 1) UNION (SELECT 2) ) AS t1 NATURAL JOIN t2 ) )
1715            //                   ^ ^ ^ ^
1716            //                   | | | |
1717            //                   | | | |
1718            //                   | | | (4) belongs to a SetExpr::Query inside the subquery
1719            //                   | | (3) starts a derived table (subquery)
1720            //                   | (2) starts a nested join
1721            //                   (1) an additional set of parens around a nested join
1722            //
1723            match self.parse_derived_table_factor(NotLateral) {
1724                // The recently consumed '(' started a derived table, and we've
1725                // parsed the subquery, followed by the closing ')', and the
1726                // alias of the derived table. In the example above this is
1727                // case (3), and the next token would be `NATURAL`.
1728                Ok(table_factor) => Ok(table_factor),
1729                Err(_) => {
1730                    // The '(' we've recently consumed does not start a derived
1731                    // table. For valid input this can happen either when the
1732                    // token following the paren can't start a query (e.g. `foo`
1733                    // in `FROM (foo NATURAL JOIN bar)`, or when the '(' we've
1734                    // consumed is followed by another '(' that starts a
1735                    // derived table, like (3), or another nested join (2).
1736                    //
1737                    // Ignore the error and back up to where we were before.
1738                    // Either we'll be able to parse a valid nested join, or
1739                    // we won't, and we'll return that error instead.
1740                    self.index = index;
1741                    let table_and_joins = self.parse_table_and_joins()?;
1742                    match table_and_joins.relation {
1743                        TableFactor::NestedJoin { .. } => (),
1744                        _ => {
1745                            if table_and_joins.joins.is_empty() {
1746                                // The SQL spec prohibits derived tables and bare
1747                                // tables from appearing alone in parentheses.
1748                                self.expected("joined table", self.peek_token())?
1749                            }
1750                        }
1751                    }
1752                    self.expect_token(&Token::RParen)?;
1753                    Ok(TableFactor::NestedJoin(Box::new(table_and_joins)))
1754                }
1755            }
1756        } else {
1757            let name = self.parse_object_name()?;
1758            // Postgres, MSSQL: table-valued functions:
1759            let args = if self.consume_token(&Token::LParen) {
1760                self.parse_optional_args()?
1761            } else {
1762                vec![]
1763            };
1764            let alias = self.parse_optional_table_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?;
1765            // MSSQL-specific table hints:
1766            let mut with_hints = vec![];
1767            if self.parse_keyword("WITH") {
1768                if self.consume_token(&Token::LParen) {
1769                    with_hints = self.parse_comma_separated(Parser::parse_expr)?;
1770                    self.expect_token(&Token::RParen)?;
1771                } else {
1772                    // rewind, as WITH may belong to the next statement's CTE
1773                    self.prev_token();
1774                }
1775            };
1776            Ok(TableFactor::Table {
1777                name,
1778                alias,
1779                args,
1780                with_hints,
1781            })
1782        }
1783    }
1784
1785    pub fn parse_derived_table_factor(
1786        &mut self,
1787        lateral: IsLateral,
1788    ) -> Result<TableFactor, ParserError> {
1789        let subquery = Box::new(self.parse_query()?);
1790        self.expect_token(&Token::RParen)?;
1791        let alias = self.parse_optional_table_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?;
1792        Ok(TableFactor::Derived {
1793            lateral: match lateral {
1794                Lateral => true,
1795                NotLateral => false,
1796            },
1797            subquery,
1798            alias,
1799        })
1800    }
1801
1802    fn parse_join_constraint(&mut self, natural: bool) -> Result<JoinConstraint, ParserError> {
1803        if natural {
1804            Ok(JoinConstraint::Natural)
1805        } else if self.parse_keyword("ON") {
1806            let constraint = self.parse_expr()?;
1807            Ok(JoinConstraint::On(constraint))
1808        } else if self.parse_keyword("USING") {
1809            let columns = self.parse_parenthesized_column_list(Mandatory)?;
1810            Ok(JoinConstraint::Using(columns))
1811        } else {
1812            self.expected("ON, or USING after JOIN", self.peek_token())
1813        }
1814    }
1815
1816    /// Parse an INSERT statement
1817    pub fn parse_insert(&mut self) -> Result<Statement, ParserError> {
1818        self.expect_keyword("INTO")?;
1819        let table_name = self.parse_object_name()?;
1820        let columns = self.parse_parenthesized_column_list(Optional)?;
1821        let source = Box::new(self.parse_query()?);
1822        Ok(Statement::Insert {
1823            table_name,
1824            columns,
1825            source,
1826        })
1827    }
1828
1829    pub fn parse_update(&mut self) -> Result<Statement, ParserError> {
1830        let table_name = self.parse_object_name()?;
1831        self.expect_keyword("SET")?;
1832        let assignments = self.parse_comma_separated(Parser::parse_assignment)?;
1833        let selection = if self.parse_keyword("WHERE") {
1834            Some(self.parse_expr()?)
1835        } else {
1836            None
1837        };
1838        Ok(Statement::Update {
1839            table_name,
1840            assignments,
1841            selection,
1842        })
1843    }
1844
1845    /// Parse a `var = expr` assignment, used in an UPDATE statement
1846    pub fn parse_assignment(&mut self) -> Result<Assignment, ParserError> {
1847        let id = self.parse_identifier()?;
1848        self.expect_token(&Token::Eq)?;
1849        let value = self.parse_expr()?;
1850        Ok(Assignment { id, value })
1851    }
1852
1853    pub fn parse_optional_args(&mut self) -> Result<Vec<Expr>, ParserError> {
1854        if self.consume_token(&Token::RParen) {
1855            Ok(vec![])
1856        } else {
1857            let args = self.parse_comma_separated(Parser::parse_expr)?;
1858            self.expect_token(&Token::RParen)?;
1859            Ok(args)
1860        }
1861    }
1862
1863    /// Parse a comma-delimited list of projections after SELECT
1864    pub fn parse_select_item(&mut self) -> Result<SelectItem, ParserError> {
1865        let expr = self.parse_expr()?;
1866        if let Expr::Wildcard = expr {
1867            Ok(SelectItem::Wildcard)
1868        } else if let Expr::QualifiedWildcard(prefix) = expr {
1869            Ok(SelectItem::QualifiedWildcard(ObjectName(prefix)))
1870        } else {
1871            // `expr` is a regular SQL expression and can be followed by an alias
1872            if let Some(alias) = self.parse_optional_alias(keywords::RESERVED_FOR_COLUMN_ALIAS)? {
1873                Ok(SelectItem::ExprWithAlias { expr, alias })
1874            } else {
1875                Ok(SelectItem::UnnamedExpr(expr))
1876            }
1877        }
1878    }
1879
1880    /// Parse an expression, optionally followed by ASC or DESC (used in ORDER BY)
1881    pub fn parse_order_by_expr(&mut self) -> Result<OrderByExpr, ParserError> {
1882        let expr = self.parse_expr()?;
1883
1884        let asc = if self.parse_keyword("ASC") {
1885            Some(true)
1886        } else if self.parse_keyword("DESC") {
1887            Some(false)
1888        } else {
1889            None
1890        };
1891        Ok(OrderByExpr { expr, asc })
1892    }
1893
1894    /// Parse a LIMIT clause
1895    pub fn parse_limit(&mut self) -> Result<Option<Expr>, ParserError> {
1896        if self.parse_keyword("ALL") {
1897            Ok(None)
1898        } else {
1899            Ok(Some(Expr::Value(self.parse_number_value()?)))
1900        }
1901    }
1902
1903    /// Parse an OFFSET clause
1904    pub fn parse_offset(&mut self) -> Result<Expr, ParserError> {
1905        let value = Expr::Value(self.parse_number_value()?);
1906        self.expect_one_of_keywords(&["ROW", "ROWS"])?;
1907        Ok(value)
1908    }
1909
1910    /// Parse a FETCH clause
1911    pub fn parse_fetch(&mut self) -> Result<Fetch, ParserError> {
1912        self.expect_one_of_keywords(&["FIRST", "NEXT"])?;
1913        let (quantity, percent) = if self.parse_one_of_keywords(&["ROW", "ROWS"]).is_some() {
1914            (None, false)
1915        } else {
1916            let quantity = Expr::Value(self.parse_value()?);
1917            let percent = self.parse_keyword("PERCENT");
1918            self.expect_one_of_keywords(&["ROW", "ROWS"])?;
1919            (Some(quantity), percent)
1920        };
1921        let with_ties = if self.parse_keyword("ONLY") {
1922            false
1923        } else if self.parse_keywords(vec!["WITH", "TIES"]) {
1924            true
1925        } else {
1926            return self.expected("one of ONLY or WITH TIES", self.peek_token());
1927        };
1928        Ok(Fetch {
1929            with_ties,
1930            percent,
1931            quantity,
1932        })
1933    }
1934
1935    pub fn parse_values(&mut self) -> Result<Values, ParserError> {
1936        let values = self.parse_comma_separated(|parser| {
1937            parser.expect_token(&Token::LParen)?;
1938            let exprs = parser.parse_comma_separated(Parser::parse_expr)?;
1939            parser.expect_token(&Token::RParen)?;
1940            Ok(exprs)
1941        })?;
1942        Ok(Values(values))
1943    }
1944}
1945
1946impl Word {
1947    pub fn to_ident(&self) -> Ident {
1948        Ident {
1949            value: self.value.clone(),
1950            quote_style: self.quote_style,
1951        }
1952    }
1953}
1954
1955#[cfg(test)]
1956mod tests {
1957    use super::*;
1958    use crate::test_utils::all_dialects;
1959
1960    #[test]
1961    fn test_prev_index() {
1962        let sql = "SELECT version";
1963        all_dialects().run_parser_method(sql, |parser| {
1964            assert_eq!(parser.peek_token(), Some(Token::make_keyword("SELECT")));
1965            assert_eq!(parser.next_token(), Some(Token::make_keyword("SELECT")));
1966            parser.prev_token();
1967            assert_eq!(parser.next_token(), Some(Token::make_keyword("SELECT")));
1968            assert_eq!(parser.next_token(), Some(Token::make_word("version", None)));
1969            parser.prev_token();
1970            assert_eq!(parser.peek_token(), Some(Token::make_word("version", None)));
1971            assert_eq!(parser.next_token(), Some(Token::make_word("version", None)));
1972            assert_eq!(parser.peek_token(), None);
1973            parser.prev_token();
1974            assert_eq!(parser.next_token(), Some(Token::make_word("version", None)));
1975            assert_eq!(parser.next_token(), None);
1976            assert_eq!(parser.next_token(), None);
1977            parser.prev_token();
1978        });
1979    }
1980}