Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4235,6 +4235,15 @@ pub enum Statement {
show_options: ShowStatementOptions,
},
/// ```sql
/// SHOW STAGES [ LIKE '<pattern>' ] [ IN { ACCOUNT | DATABASE | SCHEMA } ]
/// ```
/// Snowflake-specific statement.
/// <https://docs.snowflake.com/en/sql-reference/sql/show-stages>
ShowStages {
/// Additional options for filtering and scoping the stage listing.
show_options: ShowStatementOptions,
},
/// ```sql
/// SHOW VIEWS
/// ```
ShowViews {
Expand Down Expand Up @@ -4652,6 +4661,18 @@ pub enum Statement {
table_name: ObjectName,
},
/// ```sql
/// DESC[RIBE] STAGE <stage_name>
/// ```
/// Snowflake-specific statement.
/// <https://docs.snowflake.com/en/sql-reference/sql/desc-stage>
DescribeStage {
/// `DESC | DESCRIBE` spelling used by the input statement.
describe_alias: DescribeAlias,
/// Stage name.
#[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
stage_name: ObjectName,
},
/// ```sql
/// [EXPLAIN | DESC | DESCRIBE] <statement>
/// ```
Explain {
Expand Down Expand Up @@ -5148,6 +5169,10 @@ impl fmt::Display for Statement {

write!(f, "{table_name}")
}
Statement::DescribeStage {
describe_alias,
stage_name,
} => write!(f, "{describe_alias} STAGE {stage_name}"),
Statement::Explain {
describe_alias,
verbose,
Expand Down Expand Up @@ -5959,6 +5984,9 @@ impl fmt::Display for Statement {
)?;
Ok(())
}
Statement::ShowStages { show_options } => {
write!(f, "SHOW STAGES{show_options}")
}
Statement::ShowViews {
terse,
materialized,
Expand Down
4 changes: 4 additions & 0 deletions src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ impl Spanned for Values {
/// - [Statement::ShowCreate]
/// - [Statement::ShowColumns]
/// - [Statement::ShowTables]
/// - [Statement::ShowStages]
/// - [Statement::ShowCollation]
/// - [Statement::StartTransaction]
/// - [Statement::Comment]
Expand All @@ -306,6 +307,7 @@ impl Spanned for Values {
/// - [Statement::Prepare]
/// - [Statement::Kill]
/// - [Statement::ExplainTable]
/// - [Statement::DescribeStage]
/// - [Statement::Explain]
/// - [Statement::Savepoint]
/// - [Statement::ReleaseSavepoint]
Expand Down Expand Up @@ -443,6 +445,7 @@ impl Spanned for Statement {
Statement::ShowCreate { .. } => Span::empty(),
Statement::ShowColumns { .. } => Span::empty(),
Statement::ShowTables { .. } => Span::empty(),
Statement::ShowStages { .. } => Span::empty(),
Statement::ShowCollation { .. } => Span::empty(),
Statement::ShowCharset { .. } => Span::empty(),
Statement::Use(u) => u.span(),
Expand Down Expand Up @@ -470,6 +473,7 @@ impl Spanned for Statement {
Statement::Prepare { .. } => Span::empty(),
Statement::Kill { .. } => Span::empty(),
Statement::ExplainTable { .. } => Span::empty(),
Statement::DescribeStage { .. } => Span::empty(),
Statement::Explain { .. } => Span::empty(),
Statement::Savepoint { .. } => Span::empty(),
Statement::ReleaseSavepoint { .. } => Span::empty(),
Expand Down
10 changes: 10 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1247,6 +1247,16 @@ pub trait Dialect: Debug + Any {
false
}

/// Returns true if this dialect supports Snowflake-style `SHOW STAGES`.
fn supports_show_stages(&self) -> bool {
false
}

/// Returns true if this dialect supports `DESC[RIBE] STAGE <name>`.
fn supports_describe_stage(&self) -> bool {
false
}

/// Returns true if this dialect supports the `COMMENT` statement
fn supports_comment_on(&self) -> bool {
false
Expand Down
8 changes: 8 additions & 0 deletions src/dialect/snowflake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,14 @@ impl Dialect for SnowflakeDialect {
true
}

fn supports_show_stages(&self) -> bool {
true
}

fn supports_describe_stage(&self) -> bool {
true
}

fn supports_left_associative_joins_without_parens(&self) -> bool {
false
}
Expand Down
1 change: 1 addition & 0 deletions src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,7 @@ define_keywords!(
SRID,
STABLE,
STAGE,
STAGES,
START,
STARTS,
STATEMENT,
Expand Down
14 changes: 14 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14020,6 +14020,16 @@ impl<'a> Parser<'a> {
}
}

if describe_alias != DescribeAlias::Explain
&& self.dialect.supports_describe_stage()
&& self.parse_keyword(Keyword::STAGE)
{
return Ok(Statement::DescribeStage {
describe_alias,
stage_name: self.parse_object_name(false)?,
});
}

match self.maybe_parse(|parser| parser.parse_statement())? {
Some(Statement::Explain { .. }) | Some(Statement::ExplainTable { .. }) => Err(
ParserError::ParserError("Explain must be root of the plan".to_string()),
Expand Down Expand Up @@ -15526,6 +15536,10 @@ impl<'a> Parser<'a> {
Ok(self.parse_show_columns(extended, full)?)
} else if self.parse_keyword(Keyword::TABLES) {
Ok(self.parse_show_tables(terse, extended, full, external)?)
} else if self.dialect.supports_show_stages() && self.parse_keyword(Keyword::STAGES) {
Ok(Statement::ShowStages {
show_options: self.parse_show_stmt_options()?,
})
} else if self.parse_keywords(&[Keyword::MATERIALIZED, Keyword::VIEWS]) {
Ok(self.parse_show_views(terse, true)?)
} else if self.parse_keyword(Keyword::VIEWS) {
Expand Down
51 changes: 51 additions & 0 deletions tests/sqlparser_snowflake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3409,6 +3409,28 @@ fn parse_describe_view() {
}
}

#[test]
fn parse_describe_stage() {
for sql in [
"DESCRIBE STAGE analytics.raw.events",
"DESC STAGE analytics.raw.events",
] {
match snowflake().verified_stmt(sql) {
Statement::DescribeStage {
describe_alias,
stage_name,
} => {
assert!(matches!(
describe_alias,
DescribeAlias::Describe | DescribeAlias::Desc
));
assert_eq!("analytics.raw.events", stage_name.to_string());
}
statement => panic!("unexpected statement: {statement:?}"),
}
}
}

#[test]
fn parse_use() {
let valid_object_names = ["mydb", "CATALOG", "DEFAULT"];
Expand Down Expand Up @@ -3701,6 +3723,35 @@ fn test_parse_show_tables() {
.verified_stmt("SHOW EXTERNAL TABLES IN SCHEMA STARTS WITH 'abc' LIMIT 20 FROM 'xyz'");
}

#[test]
fn test_parse_show_stages() {
snowflake().verified_stmt("SHOW STAGES");
snowflake().verified_stmt("SHOW STAGES LIKE 'events%'");
snowflake().verified_stmt("SHOW STAGES IN ACCOUNT");
snowflake().verified_stmt("SHOW STAGES IN DATABASE analytics");
snowflake().verified_stmt("SHOW STAGES IN SCHEMA analytics.raw");

match snowflake().verified_stmt("SHOW STAGES LIKE 'events%' IN SCHEMA analytics.raw") {
Statement::ShowStages { show_options } => {
assert!(matches!(
show_options.filter_position,
Some(ShowStatementFilterPosition::Infix(
ShowStatementFilter::Like(ref pattern)
)) if pattern == "events%"
));
assert!(matches!(
show_options.show_in,
Some(ShowStatementIn {
parent_type: Some(ShowStatementInParentType::Schema),
parent_name: Some(ref name),
..
}) if name.to_string() == "analytics.raw"
));
}
statement => panic!("unexpected statement: {statement:?}"),
}
}

#[test]
fn test_show_views() {
snowflake().verified_stmt("SHOW VIEWS");
Expand Down
Loading