Skip to content

Commit 0b8ba91

Browse files
DanCodedThisVediniffyio
authored
Add suppport for Show Objects statement for the Snowflake parser (#1702)
Co-authored-by: Denys Tsomenko <[email protected]> Co-authored-by: Ifeanyi Ubah <[email protected]>
1 parent 443f492 commit 0b8ba91

File tree

6 files changed

+97
-2
lines changed

6 files changed

+97
-2
lines changed

src/ast/mod.rs

+25
Original file line numberDiff line numberDiff line change
@@ -3010,6 +3010,12 @@ pub enum Statement {
30103010
show_options: ShowStatementOptions,
30113011
},
30123012
/// ```sql
3013+
/// SHOW OBJECTS LIKE 'line%' IN mydb.public
3014+
/// ```
3015+
/// Snowflake-specific statement
3016+
/// <https://docs.snowflake.com/en/sql-reference/sql/show-objects>
3017+
ShowObjects(ShowObjects),
3018+
/// ```sql
30133019
/// SHOW TABLES
30143020
/// ```
30153021
ShowTables {
@@ -4703,6 +4709,17 @@ impl fmt::Display for Statement {
47034709
)?;
47044710
Ok(())
47054711
}
4712+
Statement::ShowObjects(ShowObjects {
4713+
terse,
4714+
show_options,
4715+
}) => {
4716+
write!(
4717+
f,
4718+
"SHOW {terse}OBJECTS{show_options}",
4719+
terse = if *terse { "TERSE " } else { "" },
4720+
)?;
4721+
Ok(())
4722+
}
47064723
Statement::ShowTables {
47074724
terse,
47084725
history,
@@ -8343,6 +8360,14 @@ impl fmt::Display for ShowStatementIn {
83438360
}
83448361
}
83458362

8363+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
8364+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
8365+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
8366+
pub struct ShowObjects {
8367+
pub terse: bool,
8368+
pub show_options: ShowStatementOptions,
8369+
}
8370+
83468371
/// MSSQL's json null clause
83478372
///
83488373
/// ```plaintext

src/ast/spans.rs

+1
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,7 @@ impl Spanned for Statement {
496496
Statement::DropConnector { .. } => Span::empty(),
497497
Statement::ShowDatabases { .. } => Span::empty(),
498498
Statement::ShowSchemas { .. } => Span::empty(),
499+
Statement::ShowObjects { .. } => Span::empty(),
499500
Statement::ShowViews { .. } => Span::empty(),
500501
Statement::LISTEN { .. } => Span::empty(),
501502
Statement::NOTIFY { .. } => Span::empty(),

src/dialect/snowflake.rs

+24-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::ast::helpers::stmt_data_loading::{
2525
use crate::ast::{
2626
ColumnOption, ColumnPolicy, ColumnPolicyProperty, CopyIntoSnowflakeKind, Ident,
2727
IdentityParameters, IdentityProperty, IdentityPropertyFormatKind, IdentityPropertyKind,
28-
IdentityPropertyOrder, ObjectName, RowAccessPolicy, Statement, TagsColumnOption,
28+
IdentityPropertyOrder, ObjectName, RowAccessPolicy, ShowObjects, Statement, TagsColumnOption,
2929
WrappedCollection,
3030
};
3131
use crate::dialect::{Dialect, Precedence};
@@ -185,6 +185,19 @@ impl Dialect for SnowflakeDialect {
185185
return Some(parse_file_staging_command(kw, parser));
186186
}
187187

188+
if parser.parse_keyword(Keyword::SHOW) {
189+
let terse = parser.parse_keyword(Keyword::TERSE);
190+
if parser.parse_keyword(Keyword::OBJECTS) {
191+
return Some(parse_show_objects(terse, parser));
192+
}
193+
//Give back Keyword::TERSE
194+
if terse {
195+
parser.prev_token();
196+
}
197+
//Give back Keyword::SHOW
198+
parser.prev_token();
199+
}
200+
188201
None
189202
}
190203

@@ -1092,3 +1105,13 @@ fn parse_column_tags(parser: &mut Parser, with: bool) -> Result<TagsColumnOption
10921105

10931106
Ok(TagsColumnOption { with, tags })
10941107
}
1108+
1109+
/// Parse snowflake show objects.
1110+
/// <https://docs.snowflake.com/en/sql-reference/sql/show-objects>
1111+
fn parse_show_objects(terse: bool, parser: &mut Parser) -> Result<Statement, ParserError> {
1112+
let show_options = parser.parse_show_stmt_options()?;
1113+
Ok(Statement::ShowObjects(ShowObjects {
1114+
terse,
1115+
show_options,
1116+
}))
1117+
}

src/keywords.rs

+1
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,7 @@ define_keywords!(
588588
NUMERIC,
589589
NVARCHAR,
590590
OBJECT,
591+
OBJECTS,
591592
OCCURRENCES_REGEX,
592593
OCTETS,
593594
OCTET_LENGTH,

src/parser/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14231,7 +14231,7 @@ impl<'a> Parser<'a> {
1423114231
false
1423214232
}
1423314233

14234-
fn parse_show_stmt_options(&mut self) -> Result<ShowStatementOptions, ParserError> {
14234+
pub(crate) fn parse_show_stmt_options(&mut self) -> Result<ShowStatementOptions, ParserError> {
1423514235
let show_in;
1423614236
let mut filter_position = None;
1423714237
if self.dialect.supports_show_like_before_in() {

tests/sqlparser_snowflake.rs

+45
Original file line numberDiff line numberDiff line change
@@ -3083,6 +3083,7 @@ fn test_parentheses_overflow() {
30833083
#[test]
30843084
fn test_show_databases() {
30853085
snowflake().verified_stmt("SHOW DATABASES");
3086+
snowflake().verified_stmt("SHOW TERSE DATABASES");
30863087
snowflake().verified_stmt("SHOW DATABASES HISTORY");
30873088
snowflake().verified_stmt("SHOW DATABASES LIKE '%abc%'");
30883089
snowflake().verified_stmt("SHOW DATABASES STARTS WITH 'demo_db'");
@@ -3095,6 +3096,7 @@ fn test_show_databases() {
30953096
#[test]
30963097
fn test_parse_show_schemas() {
30973098
snowflake().verified_stmt("SHOW SCHEMAS");
3099+
snowflake().verified_stmt("SHOW TERSE SCHEMAS");
30983100
snowflake().verified_stmt("SHOW SCHEMAS IN ACCOUNT");
30993101
snowflake().verified_stmt("SHOW SCHEMAS IN ACCOUNT abc");
31003102
snowflake().verified_stmt("SHOW SCHEMAS IN DATABASE");
@@ -3104,9 +3106,51 @@ fn test_parse_show_schemas() {
31043106
snowflake().verified_stmt("SHOW SCHEMAS IN DATABASE STARTS WITH 'abc' LIMIT 20 FROM 'xyz'");
31053107
}
31063108

3109+
#[test]
3110+
fn test_parse_show_objects() {
3111+
snowflake().verified_stmt("SHOW OBJECTS");
3112+
snowflake().verified_stmt("SHOW OBJECTS IN abc");
3113+
snowflake().verified_stmt("SHOW OBJECTS LIKE '%test%' IN abc");
3114+
snowflake().verified_stmt("SHOW OBJECTS IN ACCOUNT");
3115+
snowflake().verified_stmt("SHOW OBJECTS IN DATABASE");
3116+
snowflake().verified_stmt("SHOW OBJECTS IN DATABASE abc");
3117+
snowflake().verified_stmt("SHOW OBJECTS IN SCHEMA");
3118+
snowflake().verified_stmt("SHOW OBJECTS IN SCHEMA abc");
3119+
snowflake().verified_stmt("SHOW TERSE OBJECTS");
3120+
snowflake().verified_stmt("SHOW TERSE OBJECTS IN abc");
3121+
snowflake().verified_stmt("SHOW TERSE OBJECTS LIKE '%test%' IN abc");
3122+
snowflake().verified_stmt("SHOW TERSE OBJECTS LIKE '%test%' IN abc STARTS WITH 'b'");
3123+
snowflake().verified_stmt("SHOW TERSE OBJECTS LIKE '%test%' IN abc STARTS WITH 'b' LIMIT 10");
3124+
snowflake()
3125+
.verified_stmt("SHOW TERSE OBJECTS LIKE '%test%' IN abc STARTS WITH 'b' LIMIT 10 FROM 'x'");
3126+
match snowflake().verified_stmt("SHOW TERSE OBJECTS LIKE '%test%' IN abc") {
3127+
Statement::ShowObjects(ShowObjects {
3128+
terse,
3129+
show_options,
3130+
}) => {
3131+
assert!(terse);
3132+
let name = match show_options.show_in {
3133+
Some(ShowStatementIn {
3134+
parent_name: Some(val),
3135+
..
3136+
}) => val.to_string(),
3137+
_ => unreachable!(),
3138+
};
3139+
assert_eq!("abc", name);
3140+
let like = match show_options.filter_position {
3141+
Some(ShowStatementFilterPosition::Infix(ShowStatementFilter::Like(val))) => val,
3142+
_ => unreachable!(),
3143+
};
3144+
assert_eq!("%test%", like);
3145+
}
3146+
_ => unreachable!(),
3147+
}
3148+
}
3149+
31073150
#[test]
31083151
fn test_parse_show_tables() {
31093152
snowflake().verified_stmt("SHOW TABLES");
3153+
snowflake().verified_stmt("SHOW TERSE TABLES");
31103154
snowflake().verified_stmt("SHOW TABLES IN ACCOUNT");
31113155
snowflake().verified_stmt("SHOW TABLES IN DATABASE");
31123156
snowflake().verified_stmt("SHOW TABLES IN DATABASE xyz");
@@ -3129,6 +3173,7 @@ fn test_parse_show_tables() {
31293173
#[test]
31303174
fn test_show_views() {
31313175
snowflake().verified_stmt("SHOW VIEWS");
3176+
snowflake().verified_stmt("SHOW TERSE VIEWS");
31323177
snowflake().verified_stmt("SHOW VIEWS IN ACCOUNT");
31333178
snowflake().verified_stmt("SHOW VIEWS IN DATABASE");
31343179
snowflake().verified_stmt("SHOW VIEWS IN DATABASE xyz");

0 commit comments

Comments
 (0)