Skip to content

Allow to use the TABLE keyword in DESC|DESCRIBE|EXPLAIN TABLE statement #1351

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 29, 2024
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: 9 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2699,6 +2699,11 @@ pub enum Statement {
describe_alias: DescribeAlias,
/// Hive style `FORMATTED | EXTENDED`
hive_format: Option<HiveDescribeFormat>,
/// Snowflake and ClickHouse support `DESC|DESCRIBE TABLE <table_name>` syntax
///
/// [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,
/// Table name
#[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
table_name: ObjectName,
Expand Down Expand Up @@ -2872,13 +2877,17 @@ impl fmt::Display for Statement {
Statement::ExplainTable {
describe_alias,
hive_format,
has_table_keyword,
table_name,
} => {
write!(f, "{describe_alias} ")?;

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

write!(f, "{table_name}")
}
Expand Down
3 changes: 3 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7972,10 +7972,13 @@ impl<'a> Parser<'a> {
_ => {}
}

// only allow to use TABLE keyword for DESC|DESCRIBE statement
let has_table_keyword = self.parse_keyword(Keyword::TABLE);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't add the dialect guard since the TABLE keyword is allowed in multiple dialects(Snowflake and ClicHouse), and it should be good to let the downstream determine if it's a valid syntax.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #1385

let table_name = self.parse_object_name(false)?;
Ok(Statement::ExplainTable {
describe_alias,
hive_format,
has_table_keyword,
table_name,
})
}
Expand Down
42 changes: 30 additions & 12 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4186,31 +4186,49 @@ fn run_explain_analyze(
#[test]
fn parse_explain_table() {
let validate_explain =
|query: &str, expected_describe_alias: DescribeAlias| match verified_stmt(query) {
Statement::ExplainTable {
describe_alias,
hive_format,
table_name,
} => {
assert_eq!(describe_alias, expected_describe_alias);
assert_eq!(hive_format, None);
assert_eq!("test_identifier", table_name.to_string());
|query: &str, expected_describe_alias: DescribeAlias, expected_table_keyword| {
match verified_stmt(query) {
Statement::ExplainTable {
describe_alias,
hive_format,
has_table_keyword,
table_name,
} => {
assert_eq!(describe_alias, expected_describe_alias);
assert_eq!(hive_format, None);
assert_eq!(has_table_keyword, expected_table_keyword);
assert_eq!("test_identifier", table_name.to_string());
}
_ => panic!("Unexpected Statement, must be ExplainTable"),
}
_ => panic!("Unexpected Statement, must be ExplainTable"),
};

validate_explain("EXPLAIN test_identifier", DescribeAlias::Explain);
validate_explain("DESCRIBE test_identifier", DescribeAlias::Describe);
validate_explain("EXPLAIN test_identifier", DescribeAlias::Explain, false);
validate_explain("DESCRIBE test_identifier", DescribeAlias::Describe, false);
validate_explain("DESC test_identifier", DescribeAlias::Desc, false);
validate_explain(
"EXPLAIN TABLE test_identifier",
DescribeAlias::Explain,
true,
);
validate_explain(
"DESCRIBE TABLE test_identifier",
DescribeAlias::Describe,
true,
);
validate_explain("DESC TABLE test_identifier", DescribeAlias::Desc, true);
}

#[test]
fn explain_describe() {
verified_stmt("DESCRIBE test.table");
verified_stmt("DESCRIBE TABLE test.table");
}

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

#[test]
Expand Down
Loading