Skip to content

Commit 7fdb2ec

Browse files
authored
Allow to use the TABLE keyword in DESC|DESCRIBE|EXPLAIN TABLE statement (#1351)
1 parent 547d82f commit 7fdb2ec

File tree

3 files changed

+42
-12
lines changed

3 files changed

+42
-12
lines changed

src/ast/mod.rs

+9
Original file line numberDiff line numberDiff line change
@@ -2699,6 +2699,11 @@ pub enum Statement {
26992699
describe_alias: DescribeAlias,
27002700
/// Hive style `FORMATTED | EXTENDED`
27012701
hive_format: Option<HiveDescribeFormat>,
2702+
/// Snowflake and ClickHouse support `DESC|DESCRIBE TABLE <table_name>` syntax
2703+
///
2704+
/// [Snowflake](https://docs.snowflake.com/en/sql-reference/sql/desc-table.html)
2705+
/// [ClickHouse](https://clickhouse.com/docs/en/sql-reference/statements/describe-table)
2706+
has_table_keyword: bool,
27022707
/// Table name
27032708
#[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
27042709
table_name: ObjectName,
@@ -2872,13 +2877,17 @@ impl fmt::Display for Statement {
28722877
Statement::ExplainTable {
28732878
describe_alias,
28742879
hive_format,
2880+
has_table_keyword,
28752881
table_name,
28762882
} => {
28772883
write!(f, "{describe_alias} ")?;
28782884

28792885
if let Some(format) = hive_format {
28802886
write!(f, "{} ", format)?;
28812887
}
2888+
if *has_table_keyword {
2889+
write!(f, "TABLE ")?;
2890+
}
28822891

28832892
write!(f, "{table_name}")
28842893
}

src/parser/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -7972,10 +7972,13 @@ impl<'a> Parser<'a> {
79727972
_ => {}
79737973
}
79747974

7975+
// only allow to use TABLE keyword for DESC|DESCRIBE statement
7976+
let has_table_keyword = self.parse_keyword(Keyword::TABLE);
79757977
let table_name = self.parse_object_name(false)?;
79767978
Ok(Statement::ExplainTable {
79777979
describe_alias,
79787980
hive_format,
7981+
has_table_keyword,
79797982
table_name,
79807983
})
79817984
}

tests/sqlparser_common.rs

+30-12
Original file line numberDiff line numberDiff line change
@@ -4186,31 +4186,49 @@ fn run_explain_analyze(
41864186
#[test]
41874187
fn parse_explain_table() {
41884188
let validate_explain =
4189-
|query: &str, expected_describe_alias: DescribeAlias| match verified_stmt(query) {
4190-
Statement::ExplainTable {
4191-
describe_alias,
4192-
hive_format,
4193-
table_name,
4194-
} => {
4195-
assert_eq!(describe_alias, expected_describe_alias);
4196-
assert_eq!(hive_format, None);
4197-
assert_eq!("test_identifier", table_name.to_string());
4189+
|query: &str, expected_describe_alias: DescribeAlias, expected_table_keyword| {
4190+
match verified_stmt(query) {
4191+
Statement::ExplainTable {
4192+
describe_alias,
4193+
hive_format,
4194+
has_table_keyword,
4195+
table_name,
4196+
} => {
4197+
assert_eq!(describe_alias, expected_describe_alias);
4198+
assert_eq!(hive_format, None);
4199+
assert_eq!(has_table_keyword, expected_table_keyword);
4200+
assert_eq!("test_identifier", table_name.to_string());
4201+
}
4202+
_ => panic!("Unexpected Statement, must be ExplainTable"),
41984203
}
4199-
_ => panic!("Unexpected Statement, must be ExplainTable"),
42004204
};
42014205

4202-
validate_explain("EXPLAIN test_identifier", DescribeAlias::Explain);
4203-
validate_explain("DESCRIBE test_identifier", DescribeAlias::Describe);
4206+
validate_explain("EXPLAIN test_identifier", DescribeAlias::Explain, false);
4207+
validate_explain("DESCRIBE test_identifier", DescribeAlias::Describe, false);
4208+
validate_explain("DESC test_identifier", DescribeAlias::Desc, false);
4209+
validate_explain(
4210+
"EXPLAIN TABLE test_identifier",
4211+
DescribeAlias::Explain,
4212+
true,
4213+
);
4214+
validate_explain(
4215+
"DESCRIBE TABLE test_identifier",
4216+
DescribeAlias::Describe,
4217+
true,
4218+
);
4219+
validate_explain("DESC TABLE test_identifier", DescribeAlias::Desc, true);
42044220
}
42054221

42064222
#[test]
42074223
fn explain_describe() {
42084224
verified_stmt("DESCRIBE test.table");
4225+
verified_stmt("DESCRIBE TABLE test.table");
42094226
}
42104227

42114228
#[test]
42124229
fn explain_desc() {
42134230
verified_stmt("DESC test.table");
4231+
verified_stmt("DESC TABLE test.table");
42144232
}
42154233

42164234
#[test]

0 commit comments

Comments
 (0)