Skip to content

Commit 8dd213c

Browse files
authored
BigQuery: support unquoted hyphen in table/view declaration (apache#1178)
1 parent 241da85 commit 8dd213c

2 files changed

Lines changed: 51 additions & 4 deletions

File tree

src/parser/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3749,7 +3749,8 @@ impl<'a> Parser<'a> {
37493749
&& self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
37503750
// Many dialects support `OR ALTER` right after `CREATE`, but we don't (yet).
37513751
// ANSI SQL and Postgres support RECURSIVE here, but we don't support it either.
3752-
let name = self.parse_object_name(false)?;
3752+
let allow_unquoted_hyphen = dialect_of!(self is BigQueryDialect);
3753+
let name = self.parse_object_name(allow_unquoted_hyphen)?;
37533754
let columns = self.parse_view_columns()?;
37543755
let mut options = CreateTableOptions::None;
37553756
let with_options = self.parse_options(Keyword::WITH)?;
@@ -4736,8 +4737,9 @@ impl<'a> Parser<'a> {
47364737
global: Option<bool>,
47374738
transient: bool,
47384739
) -> Result<Statement, ParserError> {
4740+
let allow_unquoted_hyphen = dialect_of!(self is BigQueryDialect);
47394741
let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
4740-
let table_name = self.parse_object_name(false)?;
4742+
let table_name = self.parse_object_name(allow_unquoted_hyphen)?;
47414743

47424744
// Clickhouse has `ON CLUSTER 'cluster'` syntax for DDLs
47434745
let on_cluster = if self.parse_keywords(&[Keyword::ON, Keyword::CLUSTER]) {
@@ -4752,13 +4754,13 @@ impl<'a> Parser<'a> {
47524754
};
47534755

47544756
let like = if self.parse_keyword(Keyword::LIKE) || self.parse_keyword(Keyword::ILIKE) {
4755-
self.parse_object_name(false).ok()
4757+
self.parse_object_name(allow_unquoted_hyphen).ok()
47564758
} else {
47574759
None
47584760
};
47594761

47604762
let clone = if self.parse_keyword(Keyword::CLONE) {
4761-
self.parse_object_name(false).ok()
4763+
self.parse_object_name(allow_unquoted_hyphen).ok()
47624764
} else {
47634765
None
47644766
};

tests/sqlparser_bigquery.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,51 @@ fn parse_create_view_if_not_exists() {
206206
}
207207
}
208208

209+
#[test]
210+
fn parse_create_view_with_unquoted_hyphen() {
211+
let sql = "CREATE VIEW IF NOT EXISTS my-pro-ject.mydataset.myview AS SELECT 1";
212+
match bigquery().verified_stmt(sql) {
213+
Statement::CreateView {
214+
name,
215+
query,
216+
if_not_exists,
217+
..
218+
} => {
219+
assert_eq!("my-pro-ject.mydataset.myview", name.to_string());
220+
assert_eq!("SELECT 1", query.to_string());
221+
assert!(if_not_exists);
222+
}
223+
_ => unreachable!(),
224+
}
225+
}
226+
227+
#[test]
228+
fn parse_create_table_with_unquoted_hyphen() {
229+
let sql = "CREATE TABLE my-pro-ject.mydataset.mytable (x INT64)";
230+
match bigquery().verified_stmt(sql) {
231+
Statement::CreateTable { name, columns, .. } => {
232+
assert_eq!(
233+
name,
234+
ObjectName(vec![
235+
"my-pro-ject".into(),
236+
"mydataset".into(),
237+
"mytable".into()
238+
])
239+
);
240+
assert_eq!(
241+
vec![ColumnDef {
242+
name: Ident::new("x"),
243+
data_type: DataType::Int64,
244+
collation: None,
245+
options: vec![]
246+
},],
247+
columns
248+
);
249+
}
250+
_ => unreachable!(),
251+
}
252+
}
253+
209254
#[test]
210255
fn parse_create_table_with_options() {
211256
let sql = concat!(

0 commit comments

Comments
 (0)