Skip to content

Commit 0ab3de6

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

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
@@ -3028,6 +3028,12 @@ pub enum Statement {
30283028
show_options: ShowStatementOptions,
30293029
},
30303030
/// ```sql
3031+
/// SHOW OBJECTS LIKE 'line%' IN mydb.public
3032+
/// ```
3033+
/// Snowflake-specific statement
3034+
/// <https://docs.snowflake.com/en/sql-reference/sql/show-objects>
3035+
ShowObjects(ShowObjects),
3036+
/// ```sql
30313037
/// SHOW TABLES
30323038
/// ```
30333039
ShowTables {
@@ -4721,6 +4727,17 @@ impl fmt::Display for Statement {
47214727
)?;
47224728
Ok(())
47234729
}
4730+
Statement::ShowObjects(ShowObjects {
4731+
terse,
4732+
show_options,
4733+
}) => {
4734+
write!(
4735+
f,
4736+
"SHOW {terse}OBJECTS{show_options}",
4737+
terse = if *terse { "TERSE " } else { "" },
4738+
)?;
4739+
Ok(())
4740+
}
47244741
Statement::ShowTables {
47254742
terse,
47264743
history,
@@ -8361,6 +8378,14 @@ impl fmt::Display for ShowStatementIn {
83618378
}
83628379
}
83638380

8381+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
8382+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
8383+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
8384+
pub struct ShowObjects {
8385+
pub terse: bool,
8386+
pub show_options: ShowStatementOptions,
8387+
}
8388+
83648389
/// MSSQL's json null clause
83658390
///
83668391
/// ```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
@@ -14241,7 +14241,7 @@ impl<'a> Parser<'a> {
1424114241
false
1424214242
}
1424314243

14244-
fn parse_show_stmt_options(&mut self) -> Result<ShowStatementOptions, ParserError> {
14244+
pub(crate) fn parse_show_stmt_options(&mut self) -> Result<ShowStatementOptions, ParserError> {
1424514245
let show_in;
1424614246
let mut filter_position = None;
1424714247
if self.dialect.supports_show_like_before_in() {

tests/sqlparser_snowflake.rs

+45
Original file line numberDiff line numberDiff line change
@@ -3090,6 +3090,7 @@ fn test_parentheses_overflow() {
30903090
#[test]
30913091
fn test_show_databases() {
30923092
snowflake().verified_stmt("SHOW DATABASES");
3093+
snowflake().verified_stmt("SHOW TERSE DATABASES");
30933094
snowflake().verified_stmt("SHOW DATABASES HISTORY");
30943095
snowflake().verified_stmt("SHOW DATABASES LIKE '%abc%'");
30953096
snowflake().verified_stmt("SHOW DATABASES STARTS WITH 'demo_db'");
@@ -3102,6 +3103,7 @@ fn test_show_databases() {
31023103
#[test]
31033104
fn test_parse_show_schemas() {
31043105
snowflake().verified_stmt("SHOW SCHEMAS");
3106+
snowflake().verified_stmt("SHOW TERSE SCHEMAS");
31053107
snowflake().verified_stmt("SHOW SCHEMAS IN ACCOUNT");
31063108
snowflake().verified_stmt("SHOW SCHEMAS IN ACCOUNT abc");
31073109
snowflake().verified_stmt("SHOW SCHEMAS IN DATABASE");
@@ -3111,9 +3113,51 @@ fn test_parse_show_schemas() {
31113113
snowflake().verified_stmt("SHOW SCHEMAS IN DATABASE STARTS WITH 'abc' LIMIT 20 FROM 'xyz'");
31123114
}
31133115

3116+
#[test]
3117+
fn test_parse_show_objects() {
3118+
snowflake().verified_stmt("SHOW OBJECTS");
3119+
snowflake().verified_stmt("SHOW OBJECTS IN abc");
3120+
snowflake().verified_stmt("SHOW OBJECTS LIKE '%test%' IN abc");
3121+
snowflake().verified_stmt("SHOW OBJECTS IN ACCOUNT");
3122+
snowflake().verified_stmt("SHOW OBJECTS IN DATABASE");
3123+
snowflake().verified_stmt("SHOW OBJECTS IN DATABASE abc");
3124+
snowflake().verified_stmt("SHOW OBJECTS IN SCHEMA");
3125+
snowflake().verified_stmt("SHOW OBJECTS IN SCHEMA abc");
3126+
snowflake().verified_stmt("SHOW TERSE OBJECTS");
3127+
snowflake().verified_stmt("SHOW TERSE OBJECTS IN abc");
3128+
snowflake().verified_stmt("SHOW TERSE OBJECTS LIKE '%test%' IN abc");
3129+
snowflake().verified_stmt("SHOW TERSE OBJECTS LIKE '%test%' IN abc STARTS WITH 'b'");
3130+
snowflake().verified_stmt("SHOW TERSE OBJECTS LIKE '%test%' IN abc STARTS WITH 'b' LIMIT 10");
3131+
snowflake()
3132+
.verified_stmt("SHOW TERSE OBJECTS LIKE '%test%' IN abc STARTS WITH 'b' LIMIT 10 FROM 'x'");
3133+
match snowflake().verified_stmt("SHOW TERSE OBJECTS LIKE '%test%' IN abc") {
3134+
Statement::ShowObjects(ShowObjects {
3135+
terse,
3136+
show_options,
3137+
}) => {
3138+
assert!(terse);
3139+
let name = match show_options.show_in {
3140+
Some(ShowStatementIn {
3141+
parent_name: Some(val),
3142+
..
3143+
}) => val.to_string(),
3144+
_ => unreachable!(),
3145+
};
3146+
assert_eq!("abc", name);
3147+
let like = match show_options.filter_position {
3148+
Some(ShowStatementFilterPosition::Infix(ShowStatementFilter::Like(val))) => val,
3149+
_ => unreachable!(),
3150+
};
3151+
assert_eq!("%test%", like);
3152+
}
3153+
_ => unreachable!(),
3154+
}
3155+
}
3156+
31143157
#[test]
31153158
fn test_parse_show_tables() {
31163159
snowflake().verified_stmt("SHOW TABLES");
3160+
snowflake().verified_stmt("SHOW TERSE TABLES");
31173161
snowflake().verified_stmt("SHOW TABLES IN ACCOUNT");
31183162
snowflake().verified_stmt("SHOW TABLES IN DATABASE");
31193163
snowflake().verified_stmt("SHOW TABLES IN DATABASE xyz");
@@ -3136,6 +3180,7 @@ fn test_parse_show_tables() {
31363180
#[test]
31373181
fn test_show_views() {
31383182
snowflake().verified_stmt("SHOW VIEWS");
3183+
snowflake().verified_stmt("SHOW TERSE VIEWS");
31393184
snowflake().verified_stmt("SHOW VIEWS IN ACCOUNT");
31403185
snowflake().verified_stmt("SHOW VIEWS IN DATABASE");
31413186
snowflake().verified_stmt("SHOW VIEWS IN DATABASE xyz");

0 commit comments

Comments
 (0)