mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-07-07 17:04:59 +00:00
feat: support different USE statement syntaxes (#1387)
This commit is contained in:
parent
19e694aa91
commit
7282ce22f9
11 changed files with 386 additions and 17 deletions
|
@ -2322,3 +2322,81 @@ fn parse_explain_table() {
|
|||
_ => panic!("Unexpected Statement, must be ExplainTable"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_use() {
|
||||
let valid_object_names = ["mydb", "CATALOG", "DEFAULT"];
|
||||
let quote_styles = ['\'', '"', '`'];
|
||||
for object_name in &valid_object_names {
|
||||
// Test single identifier without quotes
|
||||
std::assert_eq!(
|
||||
snowflake().verified_stmt(&format!("USE {}", object_name)),
|
||||
Statement::Use(Use::Object(ObjectName(vec![Ident::new(
|
||||
object_name.to_string()
|
||||
)])))
|
||||
);
|
||||
for "e in "e_styles {
|
||||
// Test single identifier with different type of quotes
|
||||
std::assert_eq!(
|
||||
snowflake().verified_stmt(&format!("USE {}{}{}", quote, object_name, quote)),
|
||||
Statement::Use(Use::Object(ObjectName(vec![Ident::with_quote(
|
||||
quote,
|
||||
object_name.to_string(),
|
||||
)])))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
for "e in "e_styles {
|
||||
// Test double identifier with different type of quotes
|
||||
std::assert_eq!(
|
||||
snowflake().verified_stmt(&format!("USE {0}CATALOG{0}.{0}my_schema{0}", quote)),
|
||||
Statement::Use(Use::Object(ObjectName(vec![
|
||||
Ident::with_quote(quote, "CATALOG"),
|
||||
Ident::with_quote(quote, "my_schema")
|
||||
])))
|
||||
);
|
||||
}
|
||||
// Test double identifier without quotes
|
||||
std::assert_eq!(
|
||||
snowflake().verified_stmt("USE mydb.my_schema"),
|
||||
Statement::Use(Use::Object(ObjectName(vec![
|
||||
Ident::new("mydb"),
|
||||
Ident::new("my_schema")
|
||||
])))
|
||||
);
|
||||
|
||||
for "e in "e_styles {
|
||||
// Test single and double identifier with keyword and different type of quotes
|
||||
std::assert_eq!(
|
||||
snowflake().verified_stmt(&format!("USE DATABASE {0}my_database{0}", quote)),
|
||||
Statement::Use(Use::Database(ObjectName(vec![Ident::with_quote(
|
||||
quote,
|
||||
"my_database".to_string(),
|
||||
)])))
|
||||
);
|
||||
std::assert_eq!(
|
||||
snowflake().verified_stmt(&format!("USE SCHEMA {0}my_schema{0}", quote)),
|
||||
Statement::Use(Use::Schema(ObjectName(vec![Ident::with_quote(
|
||||
quote,
|
||||
"my_schema".to_string(),
|
||||
)])))
|
||||
);
|
||||
std::assert_eq!(
|
||||
snowflake().verified_stmt(&format!("USE SCHEMA {0}CATALOG{0}.{0}my_schema{0}", quote)),
|
||||
Statement::Use(Use::Schema(ObjectName(vec![
|
||||
Ident::with_quote(quote, "CATALOG"),
|
||||
Ident::with_quote(quote, "my_schema")
|
||||
])))
|
||||
);
|
||||
}
|
||||
|
||||
// Test invalid syntax - missing identifier
|
||||
let invalid_cases = ["USE SCHEMA", "USE DATABASE", "USE WAREHOUSE"];
|
||||
for sql in &invalid_cases {
|
||||
std::assert_eq!(
|
||||
snowflake().parse_sql_statements(sql).unwrap_err(),
|
||||
ParserError::ParserError("Expected: identifier, found: EOF".to_string()),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue