Skip to content

Commit 6160914

Browse files
git-hulkayman-sigma
authored andcommitted
Allow to use the TABLE keyword in DESC|DESCRIBE|EXPLAIN TABLE statement (apache#1351)
1 parent ca7ca16 commit 6160914

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
@@ -2720,6 +2720,11 @@ pub enum Statement {
27202720
describe_alias: DescribeAlias,
27212721
/// Hive style `FORMATTED | EXTENDED`
27222722
hive_format: Option<HiveDescribeFormat>,
2723+
/// Snowflake and ClickHouse support `DESC|DESCRIBE TABLE <table_name>` syntax
2724+
///
2725+
/// [Snowflake](https://docs.snowflake.com/en/sql-reference/sql/desc-table.html)
2726+
/// [ClickHouse](https://clickhouse.com/docs/en/sql-reference/statements/describe-table)
2727+
has_table_keyword: bool,
27232728
/// Table name
27242729
#[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
27252730
table_name: ObjectName,
@@ -2893,13 +2898,17 @@ impl fmt::Display for Statement {
28932898
Statement::ExplainTable {
28942899
describe_alias,
28952900
hive_format,
2901+
has_table_keyword,
28962902
table_name,
28972903
} => {
28982904
write!(f, "{describe_alias} ")?;
28992905

29002906
if let Some(format) = hive_format {
29012907
write!(f, "{} ", format)?;
29022908
}
2909+
if *has_table_keyword {
2910+
write!(f, "TABLE ")?;
2911+
}
29032912

29042913
write!(f, "{table_name}")
29052914
}

src/parser/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -7986,10 +7986,13 @@ impl<'a> Parser<'a> {
79867986
_ => {}
79877987
}
79887988

7989+
// only allow to use TABLE keyword for DESC|DESCRIBE statement
7990+
let has_table_keyword = self.parse_keyword(Keyword::TABLE);
79897991
let table_name = self.parse_object_name(false)?;
79907992
Ok(Statement::ExplainTable {
79917993
describe_alias,
79927994
hive_format,
7995+
has_table_keyword,
79937996
table_name,
79947997
})
79957998
}

tests/sqlparser_common.rs

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

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

42074223
#[test]
42084224
fn explain_describe() {
42094225
verified_stmt("DESCRIBE test.table");
4226+
verified_stmt("DESCRIBE TABLE test.table");
42104227
}
42114228

42124229
#[test]
42134230
fn explain_desc() {
42144231
verified_stmt("DESC test.table");
4232+
verified_stmt("DESC TABLE test.table");
42154233
}
42164234

42174235
#[test]

0 commit comments

Comments
 (0)