Skip to content

Commit 0b41938

Browse files
committed
fix: only require DESCRIBE TABLE for Snowflake and ClickHouse dialect
1 parent 8c4d30b commit 0b41938

File tree

8 files changed

+101
-13
lines changed

8 files changed

+101
-13
lines changed

src/dialect/clickhouse.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,8 @@ impl Dialect for ClickHouseDialect {
3333
fn supports_select_wildcard_except(&self) -> bool {
3434
true
3535
}
36+
37+
fn describe_requires_table_keyword(&self) -> bool {
38+
true
39+
}
3640
}

src/dialect/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,16 @@ pub trait Dialect: Debug + Any {
488488
fn prec_unknown(&self) -> u8 {
489489
0
490490
}
491+
492+
/// Returns true if this dialect requires the `TABLE keyword after `DESCRIBE`
493+
///
494+
/// Defaults to false.
495+
///
496+
/// If true, the following statement is valid: `DESCRIBE TABLE my_table`
497+
/// If false, the following statement is valid: `DESCRIBE my_table`
498+
fn describe_requires_table_keyword(&self) -> bool {
499+
false
500+
}
491501
}
492502

493503
/// This represents the operators for which precedence must be defined

src/dialect/snowflake.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ impl Dialect for SnowflakeDialect {
154154
_ => None,
155155
}
156156
}
157+
158+
fn describe_requires_table_keyword(&self) -> bool {
159+
true
160+
}
157161
}
158162

159163
/// Parse snowflake create table statement.

src/parser/mod.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8185,15 +8185,20 @@ impl<'a> Parser<'a> {
81858185
format,
81868186
}),
81878187
_ => {
8188-
let mut hive_format = None;
8189-
match self.parse_one_of_keywords(&[Keyword::EXTENDED, Keyword::FORMATTED]) {
8190-
Some(Keyword::EXTENDED) => hive_format = Some(HiveDescribeFormat::Extended),
8191-
Some(Keyword::FORMATTED) => hive_format = Some(HiveDescribeFormat::Formatted),
8192-
_ => {}
8193-
}
8188+
let hive_format =
8189+
match self.parse_one_of_keywords(&[Keyword::EXTENDED, Keyword::FORMATTED]) {
8190+
Some(Keyword::EXTENDED) => Some(HiveDescribeFormat::Extended),
8191+
Some(Keyword::FORMATTED) => Some(HiveDescribeFormat::Formatted),
8192+
_ => None,
8193+
};
8194+
8195+
let has_table_keyword = if self.dialect.describe_requires_table_keyword() {
8196+
// only allow to use TABLE keyword for DESC|DESCRIBE statement
8197+
self.parse_keyword(Keyword::TABLE)
8198+
} else {
8199+
false
8200+
};
81948201

8195-
// only allow to use TABLE keyword for DESC|DESCRIBE statement
8196-
let has_table_keyword = self.parse_keyword(Keyword::TABLE);
81978202
let table_name = self.parse_object_name(false)?;
81988203
Ok(Statement::ExplainTable {
81998204
describe_alias,

tests/sqlparser_clickhouse.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,6 +1376,36 @@ fn parse_select_table_function_settings() {
13761376
}
13771377
}
13781378

1379+
#[test]
1380+
fn explain_describe() {
1381+
clickhouse().verified_stmt("DESCRIBE test.table");
1382+
clickhouse().verified_stmt("DESCRIBE TABLE test.table");
1383+
}
1384+
1385+
#[test]
1386+
fn explain_desc() {
1387+
clickhouse().verified_stmt("DESC test.table");
1388+
clickhouse().verified_stmt("DESC TABLE test.table");
1389+
}
1390+
1391+
#[test]
1392+
fn parse_explain_table() {
1393+
match clickhouse().verified_stmt("EXPLAIN TABLE test_identifier") {
1394+
Statement::ExplainTable {
1395+
describe_alias,
1396+
hive_format,
1397+
has_table_keyword,
1398+
table_name,
1399+
} => {
1400+
pretty_assertions::assert_eq!(describe_alias, DescribeAlias::Explain);
1401+
pretty_assertions::assert_eq!(hive_format, None);
1402+
pretty_assertions::assert_eq!(has_table_keyword, true);
1403+
pretty_assertions::assert_eq!("test_identifier", table_name.to_string());
1404+
}
1405+
_ => panic!("Unexpected Statement, must be ExplainTable"),
1406+
}
1407+
}
1408+
13791409
fn clickhouse() -> TestedDialects {
13801410
TestedDialects {
13811411
dialects: vec![Box::new(ClickHouseDialect {})],

tests/sqlparser_common.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4317,13 +4317,11 @@ fn parse_explain_table() {
43174317
#[test]
43184318
fn explain_describe() {
43194319
verified_stmt("DESCRIBE test.table");
4320-
verified_stmt("DESCRIBE TABLE test.table");
43214320
}
43224321

43234322
#[test]
43244323
fn explain_desc() {
43254324
verified_stmt("DESC test.table");
4326-
verified_stmt("DESC TABLE test.table");
43274325
}
43284326

43294327
#[test]

tests/sqlparser_hive.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ fn generic(options: Option<ParserOptions>) -> TestedDialects {
4444

4545
#[test]
4646
fn parse_describe() {
47-
let describe = r#"DESCRIBE namespace.`table`"#;
48-
hive().verified_stmt(describe);
49-
generic(None).verified_stmt(describe);
47+
hive_and_generic().verified_stmt(r#"DESCRIBE namespace.`table`"#);
48+
hive_and_generic().verified_stmt(r#"DESCRIBE namespace.table"#);
49+
hive_and_generic().verified_stmt(r#"DESCRIBE table"#);
5050
}
5151

5252
#[test]
@@ -414,3 +414,10 @@ fn hive() -> TestedDialects {
414414
options: None,
415415
}
416416
}
417+
418+
fn hive_and_generic() -> TestedDialects {
419+
TestedDialects {
420+
dialects: vec![Box::new(HiveDialect {}), Box::new(GenericDialect {})],
421+
options: None,
422+
}
423+
}

tests/sqlparser_snowflake.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2292,3 +2292,33 @@ fn test_parse_position() {
22922292
snowflake().verified_query("SELECT position('an', 'banana', 1)");
22932293
snowflake().verified_query("SELECT n, h, POSITION(n IN h) FROM pos");
22942294
}
2295+
2296+
#[test]
2297+
fn explain_describe() {
2298+
snowflake().verified_stmt("DESCRIBE test.table");
2299+
snowflake().verified_stmt("DESCRIBE TABLE test.table");
2300+
}
2301+
2302+
#[test]
2303+
fn explain_desc() {
2304+
snowflake().verified_stmt("DESC test.table");
2305+
snowflake().verified_stmt("DESC TABLE test.table");
2306+
}
2307+
2308+
#[test]
2309+
fn parse_explain_table() {
2310+
match snowflake().verified_stmt("EXPLAIN TABLE test_identifier") {
2311+
Statement::ExplainTable {
2312+
describe_alias,
2313+
hive_format,
2314+
has_table_keyword,
2315+
table_name,
2316+
} => {
2317+
assert_eq!(describe_alias, DescribeAlias::Explain);
2318+
assert_eq!(hive_format, None);
2319+
assert_eq!(has_table_keyword, true);
2320+
assert_eq!("test_identifier", table_name.to_string());
2321+
}
2322+
_ => panic!("Unexpected Statement, must be ExplainTable"),
2323+
}
2324+
}

0 commit comments

Comments
 (0)