Skip to content

Commit 43ad299

Browse files
committed
refactor parse_with_options to handle the WITH instead of doing it at every callsite
1 parent afa1883 commit 43ad299

File tree

1 file changed

+19
-29
lines changed

1 file changed

+19
-29
lines changed

src/parser.rs

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -878,10 +878,7 @@ impl Parser {
878878
} else {
879879
SourceSchema::Raw(self.parse_literal_string()?)
880880
};
881-
let mut with_options = vec![];
882-
if self.parse_keyword("WITH") {
883-
with_options = self.parse_with_options()?;
884-
}
881+
let with_options = self.parse_with_options()?;
885882
Ok(Statement::CreateSource {
886883
name,
887884
url,
@@ -896,10 +893,7 @@ impl Parser {
896893
let from = self.parse_object_name()?;
897894
self.expect_keyword("INTO")?;
898895
let url = self.parse_literal_string()?;
899-
let mut with_options = vec![];
900-
if self.parse_keyword("WITH") {
901-
with_options = self.parse_with_options()?;
902-
}
896+
let with_options = self.parse_with_options()?;
903897
Ok(Statement::CreateSink {
904898
name,
905899
from,
@@ -937,11 +931,7 @@ impl Parser {
937931
// ANSI SQL and Postgres support RECURSIVE here, but we don't support it either.
938932
let name = self.parse_object_name()?;
939933
let columns = self.parse_parenthesized_column_list(Optional)?;
940-
let with_options = if self.parse_keyword("WITH") {
941-
self.parse_with_options()?
942-
} else {
943-
vec![]
944-
};
934+
let with_options = self.parse_with_options()?;
945935
self.expect_keyword("AS")?;
946936
let query = Box::new(self.parse_query()?);
947937
// Optional `WITH [ CASCADED | LOCAL ] CHECK OPTION` is widely supported here.
@@ -992,11 +982,7 @@ impl Parser {
992982
// parse optional column list (schema)
993983
let (columns, constraints) = self.parse_columns()?;
994984

995-
let with_options = if self.parse_keyword("WITH") {
996-
self.parse_with_options()?
997-
} else {
998-
vec![]
999-
};
985+
let with_options = self.parse_with_options()?;
1000986

1001987
Ok(Statement::CreateTable {
1002988
name: table_name,
@@ -1144,19 +1130,23 @@ impl Parser {
11441130
}
11451131

11461132
pub fn parse_with_options(&mut self) -> Result<Vec<SqlOption>, ParserError> {
1147-
self.expect_token(&Token::LParen)?;
1148-
let mut options = vec![];
1149-
loop {
1150-
let name = self.parse_identifier()?;
1151-
self.expect_token(&Token::Eq)?;
1152-
let value = self.parse_value()?;
1153-
options.push(SqlOption { name, value });
1154-
if !self.consume_token(&Token::Comma) {
1155-
break;
1133+
if self.parse_keyword("WITH") {
1134+
self.expect_token(&Token::LParen)?;
1135+
let mut options = vec![];
1136+
loop {
1137+
let name = self.parse_identifier()?;
1138+
self.expect_token(&Token::Eq)?;
1139+
let value = self.parse_value()?;
1140+
options.push(SqlOption { name, value });
1141+
if !self.consume_token(&Token::Comma) {
1142+
break;
1143+
}
11561144
}
1145+
self.expect_token(&Token::RParen)?;
1146+
Ok(options)
1147+
} else {
1148+
Ok(vec![])
11571149
}
1158-
self.expect_token(&Token::RParen)?;
1159-
Ok(options)
11601150
}
11611151

11621152
pub fn parse_alter(&mut self) -> Result<Statement, ParserError> {

0 commit comments

Comments
 (0)