This is an automated email from the ASF dual-hosted git repository.

iffyio pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion-sqlparser-rs.git


The following commit(s) were added to refs/heads/main by this push:
     new 0b8ba911 Add suppport for Show Objects statement for the Snowflake 
parser (#1702)
0b8ba911 is described below

commit 0b8ba91156a27ffd56a20339fa89a927611aa806
Author: DanCodedThis <[email protected]>
AuthorDate: Thu Feb 6 19:11:02 2025 +0200

    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]>
---
 src/ast/mod.rs               | 25 ++++++++++++++++++++++++
 src/ast/spans.rs             |  1 +
 src/dialect/snowflake.rs     | 25 +++++++++++++++++++++++-
 src/keywords.rs              |  1 +
 src/parser/mod.rs            |  2 +-
 tests/sqlparser_snowflake.rs | 45 ++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 97 insertions(+), 2 deletions(-)

diff --git a/src/ast/mod.rs b/src/ast/mod.rs
index dc944c9e..de2bbafc 100644
--- a/src/ast/mod.rs
+++ b/src/ast/mod.rs
@@ -3010,6 +3010,12 @@ pub enum Statement {
         show_options: ShowStatementOptions,
     },
     /// ```sql
+    /// SHOW OBJECTS LIKE 'line%' IN mydb.public
+    /// ```
+    /// Snowflake-specific statement
+    /// <https://docs.snowflake.com/en/sql-reference/sql/show-objects>
+    ShowObjects(ShowObjects),
+    /// ```sql
     /// SHOW TABLES
     /// ```
     ShowTables {
@@ -4703,6 +4709,17 @@ impl fmt::Display for Statement {
                 )?;
                 Ok(())
             }
+            Statement::ShowObjects(ShowObjects {
+                terse,
+                show_options,
+            }) => {
+                write!(
+                    f,
+                    "SHOW {terse}OBJECTS{show_options}",
+                    terse = if *terse { "TERSE " } else { "" },
+                )?;
+                Ok(())
+            }
             Statement::ShowTables {
                 terse,
                 history,
@@ -8343,6 +8360,14 @@ impl fmt::Display for ShowStatementIn {
     }
 }
 
+#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
+#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
+#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
+pub struct ShowObjects {
+    pub terse: bool,
+    pub show_options: ShowStatementOptions,
+}
+
 /// MSSQL's json null clause
 ///
 /// ```plaintext
diff --git a/src/ast/spans.rs b/src/ast/spans.rs
index f0c38942..1af5387c 100644
--- a/src/ast/spans.rs
+++ b/src/ast/spans.rs
@@ -496,6 +496,7 @@ impl Spanned for Statement {
             Statement::DropConnector { .. } => Span::empty(),
             Statement::ShowDatabases { .. } => Span::empty(),
             Statement::ShowSchemas { .. } => Span::empty(),
+            Statement::ShowObjects { .. } => Span::empty(),
             Statement::ShowViews { .. } => Span::empty(),
             Statement::LISTEN { .. } => Span::empty(),
             Statement::NOTIFY { .. } => Span::empty(),
diff --git a/src/dialect/snowflake.rs b/src/dialect/snowflake.rs
index fc192671..68166cbe 100644
--- a/src/dialect/snowflake.rs
+++ b/src/dialect/snowflake.rs
@@ -25,7 +25,7 @@ use crate::ast::helpers::stmt_data_loading::{
 use crate::ast::{
     ColumnOption, ColumnPolicy, ColumnPolicyProperty, CopyIntoSnowflakeKind, 
Ident,
     IdentityParameters, IdentityProperty, IdentityPropertyFormatKind, 
IdentityPropertyKind,
-    IdentityPropertyOrder, ObjectName, RowAccessPolicy, Statement, 
TagsColumnOption,
+    IdentityPropertyOrder, ObjectName, RowAccessPolicy, ShowObjects, 
Statement, TagsColumnOption,
     WrappedCollection,
 };
 use crate::dialect::{Dialect, Precedence};
@@ -185,6 +185,19 @@ impl Dialect for SnowflakeDialect {
             return Some(parse_file_staging_command(kw, parser));
         }
 
+        if parser.parse_keyword(Keyword::SHOW) {
+            let terse = parser.parse_keyword(Keyword::TERSE);
+            if parser.parse_keyword(Keyword::OBJECTS) {
+                return Some(parse_show_objects(terse, parser));
+            }
+            //Give back Keyword::TERSE
+            if terse {
+                parser.prev_token();
+            }
+            //Give back Keyword::SHOW
+            parser.prev_token();
+        }
+
         None
     }
 
@@ -1092,3 +1105,13 @@ fn parse_column_tags(parser: &mut Parser, with: bool) -> 
Result<TagsColumnOption
 
     Ok(TagsColumnOption { with, tags })
 }
+
+/// Parse snowflake show objects.
+/// <https://docs.snowflake.com/en/sql-reference/sql/show-objects>
+fn parse_show_objects(terse: bool, parser: &mut Parser) -> Result<Statement, 
ParserError> {
+    let show_options = parser.parse_show_stmt_options()?;
+    Ok(Statement::ShowObjects(ShowObjects {
+        terse,
+        show_options,
+    }))
+}
diff --git a/src/keywords.rs b/src/keywords.rs
index 5f36fa73..0178bf55 100644
--- a/src/keywords.rs
+++ b/src/keywords.rs
@@ -588,6 +588,7 @@ define_keywords!(
     NUMERIC,
     NVARCHAR,
     OBJECT,
+    OBJECTS,
     OCCURRENCES_REGEX,
     OCTETS,
     OCTET_LENGTH,
diff --git a/src/parser/mod.rs b/src/parser/mod.rs
index 6d84ff84..ca792415 100644
--- a/src/parser/mod.rs
+++ b/src/parser/mod.rs
@@ -14231,7 +14231,7 @@ impl<'a> Parser<'a> {
         false
     }
 
-    fn parse_show_stmt_options(&mut self) -> Result<ShowStatementOptions, 
ParserError> {
+    pub(crate) fn parse_show_stmt_options(&mut self) -> 
Result<ShowStatementOptions, ParserError> {
         let show_in;
         let mut filter_position = None;
         if self.dialect.supports_show_like_before_in() {
diff --git a/tests/sqlparser_snowflake.rs b/tests/sqlparser_snowflake.rs
index a18f1a4d..ffcd4e69 100644
--- a/tests/sqlparser_snowflake.rs
+++ b/tests/sqlparser_snowflake.rs
@@ -3083,6 +3083,7 @@ fn test_parentheses_overflow() {
 #[test]
 fn test_show_databases() {
     snowflake().verified_stmt("SHOW DATABASES");
+    snowflake().verified_stmt("SHOW TERSE DATABASES");
     snowflake().verified_stmt("SHOW DATABASES HISTORY");
     snowflake().verified_stmt("SHOW DATABASES LIKE '%abc%'");
     snowflake().verified_stmt("SHOW DATABASES STARTS WITH 'demo_db'");
@@ -3095,6 +3096,7 @@ fn test_show_databases() {
 #[test]
 fn test_parse_show_schemas() {
     snowflake().verified_stmt("SHOW SCHEMAS");
+    snowflake().verified_stmt("SHOW TERSE SCHEMAS");
     snowflake().verified_stmt("SHOW SCHEMAS IN ACCOUNT");
     snowflake().verified_stmt("SHOW SCHEMAS IN ACCOUNT abc");
     snowflake().verified_stmt("SHOW SCHEMAS IN DATABASE");
@@ -3104,9 +3106,51 @@ fn test_parse_show_schemas() {
     snowflake().verified_stmt("SHOW SCHEMAS IN DATABASE STARTS WITH 'abc' 
LIMIT 20 FROM 'xyz'");
 }
 
+#[test]
+fn test_parse_show_objects() {
+    snowflake().verified_stmt("SHOW OBJECTS");
+    snowflake().verified_stmt("SHOW OBJECTS IN abc");
+    snowflake().verified_stmt("SHOW OBJECTS LIKE '%test%' IN abc");
+    snowflake().verified_stmt("SHOW OBJECTS IN ACCOUNT");
+    snowflake().verified_stmt("SHOW OBJECTS IN DATABASE");
+    snowflake().verified_stmt("SHOW OBJECTS IN DATABASE abc");
+    snowflake().verified_stmt("SHOW OBJECTS IN SCHEMA");
+    snowflake().verified_stmt("SHOW OBJECTS IN SCHEMA abc");
+    snowflake().verified_stmt("SHOW TERSE OBJECTS");
+    snowflake().verified_stmt("SHOW TERSE OBJECTS IN abc");
+    snowflake().verified_stmt("SHOW TERSE OBJECTS LIKE '%test%' IN abc");
+    snowflake().verified_stmt("SHOW TERSE OBJECTS LIKE '%test%' IN abc STARTS 
WITH 'b'");
+    snowflake().verified_stmt("SHOW TERSE OBJECTS LIKE '%test%' IN abc STARTS 
WITH 'b' LIMIT 10");
+    snowflake()
+        .verified_stmt("SHOW TERSE OBJECTS LIKE '%test%' IN abc STARTS WITH 
'b' LIMIT 10 FROM 'x'");
+    match snowflake().verified_stmt("SHOW TERSE OBJECTS LIKE '%test%' IN abc") 
{
+        Statement::ShowObjects(ShowObjects {
+            terse,
+            show_options,
+        }) => {
+            assert!(terse);
+            let name = match show_options.show_in {
+                Some(ShowStatementIn {
+                    parent_name: Some(val),
+                    ..
+                }) => val.to_string(),
+                _ => unreachable!(),
+            };
+            assert_eq!("abc", name);
+            let like = match show_options.filter_position {
+                
Some(ShowStatementFilterPosition::Infix(ShowStatementFilter::Like(val))) => val,
+                _ => unreachable!(),
+            };
+            assert_eq!("%test%", like);
+        }
+        _ => unreachable!(),
+    }
+}
+
 #[test]
 fn test_parse_show_tables() {
     snowflake().verified_stmt("SHOW TABLES");
+    snowflake().verified_stmt("SHOW TERSE TABLES");
     snowflake().verified_stmt("SHOW TABLES IN ACCOUNT");
     snowflake().verified_stmt("SHOW TABLES IN DATABASE");
     snowflake().verified_stmt("SHOW TABLES IN DATABASE xyz");
@@ -3129,6 +3173,7 @@ fn test_parse_show_tables() {
 #[test]
 fn test_show_views() {
     snowflake().verified_stmt("SHOW VIEWS");
+    snowflake().verified_stmt("SHOW TERSE VIEWS");
     snowflake().verified_stmt("SHOW VIEWS IN ACCOUNT");
     snowflake().verified_stmt("SHOW VIEWS IN DATABASE");
     snowflake().verified_stmt("SHOW VIEWS IN DATABASE xyz");


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to