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
9 changes: 8 additions & 1 deletion src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4643,6 +4643,10 @@ pub enum Statement {
/// [Snowflake](https://docs.snowflake.com/en/sql-reference/sql/desc-table.html)
/// [ClickHouse](https://clickhouse.com/docs/en/sql-reference/statements/describe-table)
has_table_keyword: bool,
/// Snowflake supports `DESC|DESCRIBE VIEW <view_name>` syntax.
///
/// [Snowflake](https://docs.snowflake.com/en/sql-reference/sql/desc-view)
has_view_keyword: bool,
/// Table name
#[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
table_name: ObjectName,
Expand Down Expand Up @@ -5128,14 +5132,17 @@ impl fmt::Display for Statement {
describe_alias,
hive_format,
has_table_keyword,
has_view_keyword,
table_name,
} => {
write!(f, "{describe_alias} ")?;

if let Some(format) = hive_format {
write!(f, "{format} ")?;
}
if *has_table_keyword {
if *has_view_keyword {
write!(f, "VIEW ")?;
} else if *has_table_keyword {
write!(f, "TABLE ")?;
}

Expand Down
4 changes: 4 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14048,12 +14048,16 @@ impl<'a> Parser<'a> {
} else {
false
};
let has_view_keyword = dialect_of!(self is SnowflakeDialect)
&& !has_table_keyword
&& self.parse_keyword(Keyword::VIEW);

let table_name = self.parse_object_name(false)?;
Ok(Statement::ExplainTable {
describe_alias,
hive_format,
has_table_keyword,
has_view_keyword,
table_name,
})
}
Expand Down
2 changes: 2 additions & 0 deletions tests/sqlparser_clickhouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1745,11 +1745,13 @@ fn parse_explain_table() {
describe_alias,
hive_format,
has_table_keyword,
has_view_keyword,
table_name,
} => {
pretty_assertions::assert_eq!(describe_alias, DescribeAlias::Explain);
pretty_assertions::assert_eq!(hive_format, None);
pretty_assertions::assert_eq!(has_table_keyword, true);
pretty_assertions::assert_eq!(has_view_keyword, false);
pretty_assertions::assert_eq!("test_identifier", table_name.to_string());
}
_ => panic!("Unexpected Statement, must be ExplainTable"),
Expand Down
2 changes: 2 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5409,11 +5409,13 @@ fn parse_explain_table() {
describe_alias,
hive_format,
has_table_keyword,
has_view_keyword,
table_name,
} => {
assert_eq!(describe_alias, expected_describe_alias);
assert_eq!(hive_format, None);
assert_eq!(has_table_keyword, expected_table_keyword);
assert!(!has_view_keyword);
assert_eq!("test_identifier", table_name.to_string());
}
_ => panic!("Unexpected Statement, must be ExplainTable"),
Expand Down
24 changes: 24 additions & 0 deletions tests/sqlparser_snowflake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3359,12 +3359,14 @@ fn test_parse_position() {
fn explain_describe() {
snowflake().verified_stmt("DESCRIBE test.table");
snowflake().verified_stmt("DESCRIBE TABLE test.table");
snowflake().verified_stmt("DESCRIBE VIEW test.view");
}

#[test]
fn explain_desc() {
snowflake().verified_stmt("DESC test.table");
snowflake().verified_stmt("DESC TABLE test.table");
snowflake().verified_stmt("DESC VIEW test.view");
}

#[test]
Expand All @@ -3374,17 +3376,39 @@ fn parse_explain_table() {
describe_alias,
hive_format,
has_table_keyword,
has_view_keyword,
table_name,
} => {
assert_eq!(describe_alias, DescribeAlias::Explain);
assert_eq!(hive_format, None);
assert_eq!(has_table_keyword, true);
assert_eq!(has_view_keyword, false);
assert_eq!("test_identifier", table_name.to_string());
}
_ => panic!("Unexpected Statement, must be ExplainTable"),
}
}

#[test]
fn parse_describe_view() {
match snowflake().verified_stmt("DESCRIBE VIEW test_view") {
Statement::ExplainTable {
describe_alias,
hive_format,
has_table_keyword,
has_view_keyword,
table_name,
} => {
assert_eq!(describe_alias, DescribeAlias::Describe);
assert_eq!(hive_format, None);
assert!(!has_table_keyword);
assert!(has_view_keyword);
assert_eq!("test_view", table_name.to_string());
}
_ => panic!("Unexpected Statement, must be ExplainTable"),
}
}

#[test]
fn parse_use() {
let valid_object_names = ["mydb", "CATALOG", "DEFAULT"];
Expand Down
Loading