mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-07-08 01:15:00 +00:00
fix: only require DESCRIBE TABLE
for Snowflake and ClickHouse dialect (#1386)
This commit is contained in:
parent
8c4d30bb6d
commit
dd78084ca0
8 changed files with 103 additions and 32 deletions
|
@ -33,4 +33,8 @@ impl Dialect for ClickHouseDialect {
|
||||||
fn supports_select_wildcard_except(&self) -> bool {
|
fn supports_select_wildcard_except(&self) -> bool {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn describe_requires_table_keyword(&self) -> bool {
|
||||||
|
true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -485,9 +485,20 @@ pub trait Dialect: Debug + Any {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the precedence when the precedence is otherwise unknown
|
||||||
fn prec_unknown(&self) -> u8 {
|
fn prec_unknown(&self) -> u8 {
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns true if this dialect requires the `TABLE` keyword after `DESCRIBE`
|
||||||
|
///
|
||||||
|
/// Defaults to false.
|
||||||
|
///
|
||||||
|
/// If true, the following statement is valid: `DESCRIBE TABLE my_table`
|
||||||
|
/// If false, the following statements are valid: `DESCRIBE my_table` and `DESCRIBE table`
|
||||||
|
fn describe_requires_table_keyword(&self) -> bool {
|
||||||
|
false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This represents the operators for which precedence must be defined
|
/// This represents the operators for which precedence must be defined
|
||||||
|
|
|
@ -154,6 +154,10 @@ impl Dialect for SnowflakeDialect {
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn describe_requires_table_keyword(&self) -> bool {
|
||||||
|
true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parse snowflake create table statement.
|
/// Parse snowflake create table statement.
|
||||||
|
|
|
@ -8185,15 +8185,20 @@ impl<'a> Parser<'a> {
|
||||||
format,
|
format,
|
||||||
}),
|
}),
|
||||||
_ => {
|
_ => {
|
||||||
let mut hive_format = None;
|
let hive_format =
|
||||||
match self.parse_one_of_keywords(&[Keyword::EXTENDED, Keyword::FORMATTED]) {
|
match self.parse_one_of_keywords(&[Keyword::EXTENDED, Keyword::FORMATTED]) {
|
||||||
Some(Keyword::EXTENDED) => hive_format = Some(HiveDescribeFormat::Extended),
|
Some(Keyword::EXTENDED) => Some(HiveDescribeFormat::Extended),
|
||||||
Some(Keyword::FORMATTED) => hive_format = Some(HiveDescribeFormat::Formatted),
|
Some(Keyword::FORMATTED) => Some(HiveDescribeFormat::Formatted),
|
||||||
_ => {}
|
_ => None,
|
||||||
}
|
};
|
||||||
|
|
||||||
|
let has_table_keyword = if self.dialect.describe_requires_table_keyword() {
|
||||||
|
// only allow to use TABLE keyword for DESC|DESCRIBE statement
|
||||||
|
self.parse_keyword(Keyword::TABLE)
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
};
|
||||||
|
|
||||||
// only allow to use TABLE keyword for DESC|DESCRIBE statement
|
|
||||||
let has_table_keyword = self.parse_keyword(Keyword::TABLE);
|
|
||||||
let table_name = self.parse_object_name(false)?;
|
let table_name = self.parse_object_name(false)?;
|
||||||
Ok(Statement::ExplainTable {
|
Ok(Statement::ExplainTable {
|
||||||
describe_alias,
|
describe_alias,
|
||||||
|
|
|
@ -1376,6 +1376,36 @@ fn parse_select_table_function_settings() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn explain_describe() {
|
||||||
|
clickhouse().verified_stmt("DESCRIBE test.table");
|
||||||
|
clickhouse().verified_stmt("DESCRIBE TABLE test.table");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn explain_desc() {
|
||||||
|
clickhouse().verified_stmt("DESC test.table");
|
||||||
|
clickhouse().verified_stmt("DESC TABLE test.table");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_explain_table() {
|
||||||
|
match clickhouse().verified_stmt("EXPLAIN TABLE test_identifier") {
|
||||||
|
Statement::ExplainTable {
|
||||||
|
describe_alias,
|
||||||
|
hive_format,
|
||||||
|
has_table_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!("test_identifier", table_name.to_string());
|
||||||
|
}
|
||||||
|
_ => panic!("Unexpected Statement, must be ExplainTable"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn clickhouse() -> TestedDialects {
|
fn clickhouse() -> TestedDialects {
|
||||||
TestedDialects {
|
TestedDialects {
|
||||||
dialects: vec![Box::new(ClickHouseDialect {})],
|
dialects: vec![Box::new(ClickHouseDialect {})],
|
||||||
|
|
|
@ -4301,29 +4301,16 @@ fn parse_explain_table() {
|
||||||
validate_explain("EXPLAIN test_identifier", DescribeAlias::Explain, false);
|
validate_explain("EXPLAIN test_identifier", DescribeAlias::Explain, false);
|
||||||
validate_explain("DESCRIBE test_identifier", DescribeAlias::Describe, false);
|
validate_explain("DESCRIBE test_identifier", DescribeAlias::Describe, false);
|
||||||
validate_explain("DESC test_identifier", DescribeAlias::Desc, 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]
|
#[test]
|
||||||
fn explain_describe() {
|
fn explain_describe() {
|
||||||
verified_stmt("DESCRIBE test.table");
|
verified_stmt("DESCRIBE test.table");
|
||||||
verified_stmt("DESCRIBE TABLE test.table");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn explain_desc() {
|
fn explain_desc() {
|
||||||
verified_stmt("DESC test.table");
|
verified_stmt("DESC test.table");
|
||||||
verified_stmt("DESC TABLE test.table");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
@ -21,7 +21,7 @@ use sqlparser::ast::{
|
||||||
UnaryOperator, Value,
|
UnaryOperator, Value,
|
||||||
};
|
};
|
||||||
use sqlparser::dialect::{GenericDialect, HiveDialect, MsSqlDialect};
|
use sqlparser::dialect::{GenericDialect, HiveDialect, MsSqlDialect};
|
||||||
use sqlparser::parser::{ParserError, ParserOptions};
|
use sqlparser::parser::ParserError;
|
||||||
use sqlparser::test_utils::*;
|
use sqlparser::test_utils::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -35,18 +35,11 @@ fn parse_table_create() {
|
||||||
hive().verified_stmt(serdeproperties);
|
hive().verified_stmt(serdeproperties);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generic(options: Option<ParserOptions>) -> TestedDialects {
|
|
||||||
TestedDialects {
|
|
||||||
dialects: vec![Box::new(GenericDialect {})],
|
|
||||||
options,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_describe() {
|
fn parse_describe() {
|
||||||
let describe = r#"DESCRIBE namespace.`table`"#;
|
hive_and_generic().verified_stmt(r#"DESCRIBE namespace.`table`"#);
|
||||||
hive().verified_stmt(describe);
|
hive_and_generic().verified_stmt(r#"DESCRIBE namespace.table"#);
|
||||||
generic(None).verified_stmt(describe);
|
hive_and_generic().verified_stmt(r#"DESCRIBE table"#);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -414,3 +407,10 @@ fn hive() -> TestedDialects {
|
||||||
options: None,
|
options: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn hive_and_generic() -> TestedDialects {
|
||||||
|
TestedDialects {
|
||||||
|
dialects: vec![Box::new(HiveDialect {}), Box::new(GenericDialect {})],
|
||||||
|
options: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -2292,3 +2292,33 @@ fn test_parse_position() {
|
||||||
snowflake().verified_query("SELECT position('an', 'banana', 1)");
|
snowflake().verified_query("SELECT position('an', 'banana', 1)");
|
||||||
snowflake().verified_query("SELECT n, h, POSITION(n IN h) FROM pos");
|
snowflake().verified_query("SELECT n, h, POSITION(n IN h) FROM pos");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn explain_describe() {
|
||||||
|
snowflake().verified_stmt("DESCRIBE test.table");
|
||||||
|
snowflake().verified_stmt("DESCRIBE TABLE test.table");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn explain_desc() {
|
||||||
|
snowflake().verified_stmt("DESC test.table");
|
||||||
|
snowflake().verified_stmt("DESC TABLE test.table");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_explain_table() {
|
||||||
|
match snowflake().verified_stmt("EXPLAIN TABLE test_identifier") {
|
||||||
|
Statement::ExplainTable {
|
||||||
|
describe_alias,
|
||||||
|
hive_format,
|
||||||
|
has_table_keyword,
|
||||||
|
table_name,
|
||||||
|
} => {
|
||||||
|
assert_eq!(describe_alias, DescribeAlias::Explain);
|
||||||
|
assert_eq!(hive_format, None);
|
||||||
|
assert_eq!(has_table_keyword, true);
|
||||||
|
assert_eq!("test_identifier", table_name.to_string());
|
||||||
|
}
|
||||||
|
_ => panic!("Unexpected Statement, must be ExplainTable"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue