BigQuery: support unquoted hyphen in table/view declaration (#1178)

This commit is contained in:
Ifeanyi Ubah 2024-04-09 23:05:31 +02:00 committed by GitHub
parent 241da85d67
commit 8dd213cff2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 51 additions and 4 deletions

View file

@ -3749,7 +3749,8 @@ impl<'a> Parser<'a> {
&& self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
// Many dialects support `OR ALTER` right after `CREATE`, but we don't (yet).
// ANSI SQL and Postgres support RECURSIVE here, but we don't support it either.
let name = self.parse_object_name(false)?;
let allow_unquoted_hyphen = dialect_of!(self is BigQueryDialect);
let name = self.parse_object_name(allow_unquoted_hyphen)?;
let columns = self.parse_view_columns()?;
let mut options = CreateTableOptions::None;
let with_options = self.parse_options(Keyword::WITH)?;
@ -4736,8 +4737,9 @@ impl<'a> Parser<'a> {
global: Option<bool>,
transient: bool,
) -> Result<Statement, ParserError> {
let allow_unquoted_hyphen = dialect_of!(self is BigQueryDialect);
let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
let table_name = self.parse_object_name(false)?;
let table_name = self.parse_object_name(allow_unquoted_hyphen)?;
// Clickhouse has `ON CLUSTER 'cluster'` syntax for DDLs
let on_cluster = if self.parse_keywords(&[Keyword::ON, Keyword::CLUSTER]) {
@ -4752,13 +4754,13 @@ impl<'a> Parser<'a> {
};
let like = if self.parse_keyword(Keyword::LIKE) || self.parse_keyword(Keyword::ILIKE) {
self.parse_object_name(false).ok()
self.parse_object_name(allow_unquoted_hyphen).ok()
} else {
None
};
let clone = if self.parse_keyword(Keyword::CLONE) {
self.parse_object_name(false).ok()
self.parse_object_name(allow_unquoted_hyphen).ok()
} else {
None
};

View file

@ -206,6 +206,51 @@ fn parse_create_view_if_not_exists() {
}
}
#[test]
fn parse_create_view_with_unquoted_hyphen() {
let sql = "CREATE VIEW IF NOT EXISTS my-pro-ject.mydataset.myview AS SELECT 1";
match bigquery().verified_stmt(sql) {
Statement::CreateView {
name,
query,
if_not_exists,
..
} => {
assert_eq!("my-pro-ject.mydataset.myview", name.to_string());
assert_eq!("SELECT 1", query.to_string());
assert!(if_not_exists);
}
_ => unreachable!(),
}
}
#[test]
fn parse_create_table_with_unquoted_hyphen() {
let sql = "CREATE TABLE my-pro-ject.mydataset.mytable (x INT64)";
match bigquery().verified_stmt(sql) {
Statement::CreateTable { name, columns, .. } => {
assert_eq!(
name,
ObjectName(vec![
"my-pro-ject".into(),
"mydataset".into(),
"mytable".into()
])
);
assert_eq!(
vec![ColumnDef {
name: Ident::new("x"),
data_type: DataType::Int64,
collation: None,
options: vec![]
},],
columns
);
}
_ => unreachable!(),
}
}
#[test]
fn parse_create_table_with_options() {
let sql = concat!(