Skip to content

Commit 2b043c9

Browse files
committed
Code review comments
1 parent 72a95c5 commit 2b043c9

File tree

4 files changed

+43
-20
lines changed

4 files changed

+43
-20
lines changed

src/ast/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7354,6 +7354,9 @@ impl Display for UtilityOption {
73547354
}
73557355
}
73567356

7357+
/// Represents the different options available for a SHOW <OBJECT>
7358+
/// statement to filter the results. Example from Snowflake:
7359+
/// https://docs.snowflake.com/en/sql-reference/sql/show-tables
73577360
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
73587361
pub struct ShowStatementOptions {
73597362
pub show_in: Option<ShowStatementIn>,
@@ -7365,7 +7368,7 @@ pub struct ShowStatementOptions {
73657368

73667369
impl Display for ShowStatementOptions {
73677370
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
7368-
let (life_in_infix, like_in_suffix) = match &self.filter_position {
7371+
let (like_in_infix, like_in_suffix) = match &self.filter_position {
73697372
Some(ShowStatementFilterPosition::Infix(filter)) => {
73707373
(format!(" {filter}"), "".to_string())
73717374
}
@@ -7376,7 +7379,7 @@ impl Display for ShowStatementOptions {
73767379
};
73777380
write!(
73787381
f,
7379-
"{life_in_infix}{show_in}{starts_with}{limit}{from}{like_in_suffix}",
7382+
"{like_in_infix}{show_in}{starts_with}{limit}{from}{like_in_suffix}",
73807383
show_in = match &self.show_in {
73817384
Some(i) => format!(" {i}"),
73827385
None => String::new(),

src/keywords.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ define_keywords!(
9292
AND,
9393
ANTI,
9494
ANY,
95+
APPLICATION,
9596
APPLY,
9697
ARCHIVE,
9798
ARE,

src/parser/mod.rs

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3195,6 +3195,22 @@ impl<'a> Parser<'a> {
31953195
})
31963196
}
31973197

3198+
/// Look for all of the expected keywords in sequence, without consuming them
3199+
fn peek_keyword(&mut self, expected: Keyword) -> bool {
3200+
let index = self.index;
3201+
let matched = self.parse_keyword(expected);
3202+
self.index = index;
3203+
matched
3204+
}
3205+
3206+
/// Look for all of the expected keywords in sequence, without consuming them
3207+
fn peek_keywords(&mut self, expected: &[Keyword]) -> bool {
3208+
let index = self.index;
3209+
let matched = self.parse_keywords(expected);
3210+
self.index = index;
3211+
matched
3212+
}
3213+
31983214
/// Return the first non-whitespace token that has not yet been processed
31993215
/// (or None if reached end-of-file) and mark it as processed. OK to call
32003216
/// repeatedly after reaching EOF.
@@ -12311,19 +12327,6 @@ impl<'a> Parser<'a> {
1231112327
false
1231212328
}
1231312329

12314-
/// Look for all of the expected keywords in sequence, without consuming them
12315-
fn peek_keywords(&mut self, expected: &[Keyword]) -> bool {
12316-
let index = self.index;
12317-
for kw in expected {
12318-
if !self.parse_keyword(*kw) {
12319-
self.index = index;
12320-
return false;
12321-
}
12322-
}
12323-
self.index = index;
12324-
true
12325-
}
12326-
1232712330
fn parse_show_stmt_options(&mut self) -> Result<ShowStatementOptions, ParserError> {
1232812331
let show_in;
1232912332
let mut filter_position = None;
@@ -12354,7 +12357,8 @@ impl<'a> Parser<'a> {
1235412357
let clause = match self.parse_one_of_keywords(&[Keyword::FROM, Keyword::IN]) {
1235512358
Some(Keyword::FROM) => ShowStatementInClause::FROM,
1235612359
Some(Keyword::IN) => ShowStatementInClause::IN,
12357-
_ => return Ok(None),
12360+
None => return Ok(None),
12361+
_ => return self.expected("FROM or IN", self.peek_token()),
1235812362
};
1235912363

1236012364
let (parent_type, parent_name) = match self.parse_one_of_keywords(&[
@@ -12364,13 +12368,23 @@ impl<'a> Parser<'a> {
1236412368
Keyword::TABLE,
1236512369
Keyword::VIEW,
1236612370
]) {
12367-
Some(Keyword::DATABASE) if self.peek_keywords(&[Keyword::STARTS, Keyword::WITH]) => {
12371+
// If we see these next keywords it means we don't have a parent name
12372+
Some(Keyword::DATABASE)
12373+
if self.peek_keywords(&[Keyword::STARTS, Keyword::WITH])
12374+
| self.peek_keyword(Keyword::LIMIT) =>
12375+
{
1236812376
(Some(ShowStatementInParentType::Database), None)
1236912377
}
12370-
Some(Keyword::SCHEMA) if self.peek_keywords(&[Keyword::STARTS, Keyword::WITH]) => {
12378+
Some(Keyword::SCHEMA)
12379+
if self.peek_keywords(&[Keyword::STARTS, Keyword::WITH])
12380+
| self.peek_keyword(Keyword::LIMIT) =>
12381+
{
1237112382
(Some(ShowStatementInParentType::Schema), None)
1237212383
}
1237312384
Some(parent_kw) => {
12385+
// The parent name here is still optional, for example:
12386+
// SHOW TABLES IN ACCOUNT, so parsing the object name
12387+
// may fail because the statement ends.
1237412388
let parent_name = match self.parse_object_name(false) {
1237512389
Ok(n) => Some(n),
1237612390
_ => None,
@@ -12381,7 +12395,12 @@ impl<'a> Parser<'a> {
1238112395
Keyword::SCHEMA => (Some(ShowStatementInParentType::Schema), parent_name),
1238212396
Keyword::TABLE => (Some(ShowStatementInParentType::Table), parent_name),
1238312397
Keyword::VIEW => (Some(ShowStatementInParentType::View), parent_name),
12384-
_ => unreachable!(),
12398+
_ => {
12399+
return self.expected(
12400+
"one of ACCOUNT, DATABASE, SCHEMA, TABLE or VIEW",
12401+
self.peek_token(),
12402+
)
12403+
}
1238512404
}
1238612405
}
1238712406
None => {

tests/sqlparser_snowflake.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2842,7 +2842,7 @@ fn test_show_views() {
28422842

28432843
#[test]
28442844
fn test_parse_show_columns_sql() {
2845+
snowflake().verified_stmt("SHOW COLUMNS IN TABLE");
28452846
snowflake().verified_stmt("SHOW COLUMNS IN TABLE abc");
28462847
snowflake().verified_stmt("SHOW COLUMNS LIKE '%xyz%' IN TABLE abc");
2847-
snowflake().verified_stmt("SHOW COLUMNS IN TABLE");
28482848
}

0 commit comments

Comments
 (0)