|
| 1 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 | +// you may not use this file except in compliance with the License. |
| 3 | +// You may obtain a copy of the License at |
| 4 | +// |
| 5 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +// |
| 7 | +// Unless required by applicable law or agreed to in writing, software |
| 8 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +// See the License for the specific language governing permissions and |
| 11 | +// limitations under the License. |
| 12 | + |
| 13 | +#[macro_use] |
| 14 | +mod test_utils; |
| 15 | + |
| 16 | +use test_utils::*; |
| 17 | + |
| 18 | +use sqlparser::ast::*; |
| 19 | +use sqlparser::dialect::BigQueryDialect; |
| 20 | + |
| 21 | +#[test] |
| 22 | +fn parse_table_identifiers() { |
| 23 | + fn test_table_ident(ident: &str, expected: Vec<Ident>) { |
| 24 | + let sql = format!("SELECT 1 FROM {}", ident); |
| 25 | + let select = bigquery().verified_only_select(&sql); |
| 26 | + assert_eq!( |
| 27 | + select.from, |
| 28 | + vec![TableWithJoins { |
| 29 | + relation: TableFactor::Table { |
| 30 | + name: ObjectName(expected), |
| 31 | + alias: None, |
| 32 | + args: vec![], |
| 33 | + with_hints: vec![], |
| 34 | + }, |
| 35 | + joins: vec![] |
| 36 | + },] |
| 37 | + ); |
| 38 | + } |
| 39 | + fn test_table_ident_err(ident: &str) { |
| 40 | + let sql = format!("SELECT 1 FROM {}", ident); |
| 41 | + assert!(bigquery().parse_sql_statements(&sql).is_err()); |
| 42 | + } |
| 43 | + |
| 44 | + test_table_ident("da-sh-es", vec![Ident::new("da-sh-es")]); |
| 45 | + |
| 46 | + test_table_ident("`spa ce`", vec![Ident::with_quote('`', "spa ce")]); |
| 47 | + |
| 48 | + test_table_ident( |
| 49 | + "`!@#$%^&*()-=_+`", |
| 50 | + vec![Ident::with_quote('`', "!@#$%^&*()-=_+")], |
| 51 | + ); |
| 52 | + |
| 53 | + test_table_ident( |
| 54 | + "_5abc.dataField", |
| 55 | + vec![Ident::new("_5abc"), Ident::new("dataField")], |
| 56 | + ); |
| 57 | + test_table_ident( |
| 58 | + "`5abc`.dataField", |
| 59 | + vec![Ident::with_quote('`', "5abc"), Ident::new("dataField")], |
| 60 | + ); |
| 61 | + |
| 62 | + test_table_ident_err("5abc.dataField"); |
| 63 | + |
| 64 | + test_table_ident( |
| 65 | + "abc5.dataField", |
| 66 | + vec![Ident::new("abc5"), Ident::new("dataField")], |
| 67 | + ); |
| 68 | + |
| 69 | + test_table_ident_err("abc5!.dataField"); |
| 70 | + |
| 71 | + test_table_ident( |
| 72 | + "`GROUP`.dataField", |
| 73 | + vec![Ident::with_quote('`', "GROUP"), Ident::new("dataField")], |
| 74 | + ); |
| 75 | + |
| 76 | + // TODO: this should be error |
| 77 | + // test_table_ident_err("GROUP.dataField"); |
| 78 | + |
| 79 | + test_table_ident("abc5.GROUP", vec![Ident::new("abc5"), Ident::new("GROUP")]); |
| 80 | +} |
| 81 | + |
| 82 | +fn bigquery() -> TestedDialects { |
| 83 | + TestedDialects { |
| 84 | + dialects: vec![Box::new(BigQueryDialect {})], |
| 85 | + } |
| 86 | +} |
0 commit comments