From d94a09bf69870211da5289c6dd92e3eed379e805 Mon Sep 17 00:00:00 2001 From: togami2864 Date: Tue, 24 Jan 2023 17:22:26 +0900 Subject: [PATCH 1/2] feat: support json keyword for bigquery --- src/ast/data_type.rs | 3 +++ src/parser.rs | 1 + tests/sqlparser_common.rs | 25 +++++++++++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/src/ast/data_type.rs b/src/ast/data_type.rs index ba90659ab..59fce9f4b 100644 --- a/src/ast/data_type.rs +++ b/src/ast/data_type.rs @@ -140,6 +140,8 @@ pub enum DataType { Timestamp(Option, TimezoneInfo), /// Interval Interval, + /// JSON type used in BigQuery + JSON, /// Regclass used in postgresql serial Regclass, /// Text @@ -244,6 +246,7 @@ impl fmt::Display for DataType { format_datetime_precision_and_tz(f, "TIMESTAMP", precision, timezone_info) } DataType::Interval => write!(f, "INTERVAL"), + DataType::JSON => write!(f, "JSON"), DataType::Regclass => write!(f, "REGCLASS"), DataType::Text => write!(f, "TEXT"), DataType::String => write!(f, "STRING"), diff --git a/src/parser.rs b/src/parser.rs index 34398ec4c..d15212569 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -4332,6 +4332,7 @@ impl<'a> Parser<'a> { // qualifier that we don't currently support. See // parse_interval for a taste. Keyword::INTERVAL => Ok(DataType::Interval), + Keyword::JSON => Ok(DataType::JSON), Keyword::REGCLASS => Ok(DataType::Regclass), Keyword::STRING => Ok(DataType::String), Keyword::TEXT => Ok(DataType::Text), diff --git a/tests/sqlparser_common.rs b/tests/sqlparser_common.rs index c880eee7c..4b8dfa1b5 100644 --- a/tests/sqlparser_common.rs +++ b/tests/sqlparser_common.rs @@ -3608,6 +3608,31 @@ fn parse_at_timezone() { ); } +#[test] +fn parse_json_keyword() { + let sql = r#"SELECT JSON '{ + "id": 10, + "type": "fruit", + "name": "apple", + "on_menu": true, + "recipes": + { + "salads": + [ + { "id": 2001, "type": "Walnut Apple Salad" }, + { "id": 2002, "type": "Apple Spinach Salad" } + ], + "desserts": + [ + { "id": 3001, "type": "Apple Pie" }, + { "id": 3002, "type": "Apple Scones" }, + { "id": 3003, "type": "Apple Crumble" } + ] + } +}'"#; + verified_only_select(sql); +} + #[test] fn parse_simple_math_expr_plus() { let sql = "SELECT a + b, 2 + a, 2.5 + a, a_f + b_f, 2 + a_f, 2.5 + a_f FROM c"; From 013ce74cdd785245e64654d75c20a4808220ef0d Mon Sep 17 00:00:00 2001 From: togami2864 Date: Tue, 24 Jan 2023 17:30:13 +0900 Subject: [PATCH 2/2] chore: fix test --- tests/sqlparser_common.rs | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/tests/sqlparser_common.rs b/tests/sqlparser_common.rs index 4b8dfa1b5..3d94819b4 100644 --- a/tests/sqlparser_common.rs +++ b/tests/sqlparser_common.rs @@ -3630,7 +3630,34 @@ fn parse_json_keyword() { ] } }'"#; - verified_only_select(sql); + let select = verified_only_select(sql); + assert_eq!( + &Expr::TypedString { + data_type: DataType::JSON, + value: r#"{ + "id": 10, + "type": "fruit", + "name": "apple", + "on_menu": true, + "recipes": + { + "salads": + [ + { "id": 2001, "type": "Walnut Apple Salad" }, + { "id": 2002, "type": "Apple Spinach Salad" } + ], + "desserts": + [ + { "id": 3001, "type": "Apple Pie" }, + { "id": 3002, "type": "Apple Scones" }, + { "id": 3003, "type": "Apple Crumble" } + ] + } +}"# + .into() + }, + expr_from_projection(only(&select.projection)), + ); } #[test]