@@ -256,22 +256,10 @@ impl ParserOptions {
256256 }
257257}
258258
259- #[ derive( Copy , Clone ) ]
260- enum ParserState {
261- /// The default state of the parser.
262- Normal ,
263- /// The state when parsing a CONNECT BY expression. This allows parsing
264- /// PRIOR expressions while still allowing prior as an identifier name
265- /// in other contexts.
266- ConnectBy ,
267- }
268-
269259pub struct Parser < ' a > {
270260 tokens : Vec < TokenWithLocation > ,
271261 /// The index of the first unprocessed token in `self.tokens`
272262 index : usize ,
273- /// The current state of the parser.
274- state : ParserState ,
275263 /// The current dialect to use
276264 dialect : & ' a dyn Dialect ,
277265 /// Additional options that allow you to mix & match behavior
@@ -302,7 +290,6 @@ impl<'a> Parser<'a> {
302290 Self {
303291 tokens : vec ! [ ] ,
304292 index : 0 ,
305- state : ParserState :: Normal ,
306293 dialect,
307294 recursion_counter : RecursionCounter :: new ( DEFAULT_REMAINING_DEPTH ) ,
308295 options : ParserOptions :: default ( ) ,
@@ -979,10 +966,6 @@ impl<'a> Parser<'a> {
979966 self . prev_token ( ) ;
980967 self . parse_bigquery_struct_literal ( )
981968 }
982- Keyword :: PRIOR if matches ! ( self . state, ParserState :: ConnectBy ) => {
983- let expr = self . parse_subexpr ( Self :: PLUS_MINUS_PREC ) ?;
984- Ok ( Expr :: Prior ( Box :: new ( expr) ) )
985- }
986969 // Here `w` is a word, check if it's a part of a multi-part
987970 // identifier, a function call, or a simple identifier:
988971 _ => match self . peek_token ( ) . token {
@@ -7179,7 +7162,7 @@ impl<'a> Parser<'a> {
71797162 // We parse the expression using a Pratt parser, as in `parse_expr()`.
71807163 // Start by parsing a restricted SELECT or a `(subquery)`:
71817164 let mut expr = if self . parse_keyword ( Keyword :: SELECT ) {
7182- self . parse_select ( ) ?
7165+ SetExpr :: Select ( Box :: new ( self . parse_select ( ) ?) )
71837166 } else if self . consume_token ( & Token :: LParen ) {
71847167 // CTEs are not allowed here, but the parser currently accepts them
71857168 let subquery = self . parse_query ( ) ?;
@@ -7267,7 +7250,7 @@ impl<'a> Parser<'a> {
72677250
72687251 /// Parse a restricted `SELECT` statement (no CTEs / `UNION` / `ORDER BY`),
72697252 /// assuming the initial `SELECT` was already consumed
7270- pub fn parse_select ( & mut self ) -> Result < SetExpr , ParserError > {
7253+ pub fn parse_select ( & mut self ) -> Result < Select , ParserError > {
72717254 let value_table_mode =
72727255 if dialect_of ! ( self is BigQueryDialect ) && self . parse_keyword ( Keyword :: AS ) {
72737256 if self . parse_keyword ( Keyword :: VALUE ) {
@@ -7319,18 +7302,6 @@ impl<'a> Parser<'a> {
73197302 vec ! [ ]
73207303 } ;
73217304
7322- if distinct. is_none ( )
7323- && top. is_none ( )
7324- && into. is_none ( )
7325- && !from. is_empty ( )
7326- && self
7327- . parse_one_of_keywords ( & [ Keyword :: START , Keyword :: CONNECT ] )
7328- . is_some ( )
7329- {
7330- self . prev_token ( ) ;
7331- return Ok ( SetExpr :: ConnectBy ( self . parse_connect_by ( projection, from) ?) ) ;
7332- }
7333-
73347305 let mut lateral_views = vec ! [ ] ;
73357306 loop {
73367307 if self . parse_keywords ( & [ Keyword :: LATERAL , Keyword :: VIEW ] ) {
@@ -7414,7 +7385,7 @@ impl<'a> Parser<'a> {
74147385 None
74157386 } ;
74167387
7417- Ok ( SetExpr :: Select ( Box :: new ( Select {
7388+ Ok ( Select {
74187389 distinct,
74197390 top,
74207391 projection,
@@ -7430,48 +7401,6 @@ impl<'a> Parser<'a> {
74307401 named_window : named_windows,
74317402 qualify,
74327403 value_table_mode,
7433- } ) ) )
7434- }
7435-
7436- fn with_state < T , F > ( & mut self , state : ParserState , mut f : F ) -> Result < T , ParserError >
7437- where
7438- F : FnMut ( & mut Parser ) -> Result < T , ParserError > ,
7439- {
7440- let current_state = self . state ;
7441- self . state = state;
7442- let res = f ( self ) ;
7443- self . state = current_state;
7444- res
7445- }
7446-
7447- pub fn parse_connect_by (
7448- & mut self ,
7449- projection : Vec < SelectItem > ,
7450- from : Vec < TableWithJoins > ,
7451- ) -> Result < ConnectBy , ParserError > {
7452- debug_assert ! ( !from. is_empty( ) ) ;
7453-
7454- let ( condition, relationships) = if self . parse_keywords ( & [ Keyword :: CONNECT , Keyword :: BY ] ) {
7455- let relationships = self . with_state ( ParserState :: ConnectBy , |parser| {
7456- parser. parse_comma_separated ( Parser :: parse_expr)
7457- } ) ?;
7458- self . expect_keywords ( & [ Keyword :: START , Keyword :: WITH ] ) ?;
7459- let condition = self . parse_expr ( ) ?;
7460- ( condition, relationships)
7461- } else {
7462- self . expect_keywords ( & [ Keyword :: START , Keyword :: WITH ] ) ?;
7463- let condition = self . parse_expr ( ) ?;
7464- self . expect_keywords ( & [ Keyword :: CONNECT , Keyword :: BY ] ) ?;
7465- let relationships = self . with_state ( ParserState :: ConnectBy , |parser| {
7466- parser. parse_comma_separated ( Parser :: parse_expr)
7467- } ) ?;
7468- ( condition, relationships)
7469- } ;
7470- Ok ( ConnectBy {
7471- projection,
7472- from,
7473- condition,
7474- relationships,
74757404 } )
74767405 }
74777406
0 commit comments