Skip to content

Commit 79009f5

Browse files
togami2864alamb
andauthored
feat: support JSON keyword (#799)
* feat: support json keyword for bigquery * chore: fix test --------- Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
1 parent 488e8a8 commit 79009f5

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

src/ast/data_type.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ pub enum DataType {
140140
Timestamp(Option<u64>, TimezoneInfo),
141141
/// Interval
142142
Interval,
143+
/// JSON type used in BigQuery
144+
JSON,
143145
/// Regclass used in postgresql serial
144146
Regclass,
145147
/// Text
@@ -244,6 +246,7 @@ impl fmt::Display for DataType {
244246
format_datetime_precision_and_tz(f, "TIMESTAMP", precision, timezone_info)
245247
}
246248
DataType::Interval => write!(f, "INTERVAL"),
249+
DataType::JSON => write!(f, "JSON"),
247250
DataType::Regclass => write!(f, "REGCLASS"),
248251
DataType::Text => write!(f, "TEXT"),
249252
DataType::String => write!(f, "STRING"),

src/parser.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4365,6 +4365,7 @@ impl<'a> Parser<'a> {
43654365
// qualifier that we don't currently support. See
43664366
// parse_interval for a taste.
43674367
Keyword::INTERVAL => Ok(DataType::Interval),
4368+
Keyword::JSON => Ok(DataType::JSON),
43684369
Keyword::REGCLASS => Ok(DataType::Regclass),
43694370
Keyword::STRING => Ok(DataType::String),
43704371
Keyword::TEXT => Ok(DataType::Text),

tests/sqlparser_common.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3598,6 +3598,58 @@ fn parse_at_timezone() {
35983598
);
35993599
}
36003600

3601+
#[test]
3602+
fn parse_json_keyword() {
3603+
let sql = r#"SELECT JSON '{
3604+
"id": 10,
3605+
"type": "fruit",
3606+
"name": "apple",
3607+
"on_menu": true,
3608+
"recipes":
3609+
{
3610+
"salads":
3611+
[
3612+
{ "id": 2001, "type": "Walnut Apple Salad" },
3613+
{ "id": 2002, "type": "Apple Spinach Salad" }
3614+
],
3615+
"desserts":
3616+
[
3617+
{ "id": 3001, "type": "Apple Pie" },
3618+
{ "id": 3002, "type": "Apple Scones" },
3619+
{ "id": 3003, "type": "Apple Crumble" }
3620+
]
3621+
}
3622+
}'"#;
3623+
let select = verified_only_select(sql);
3624+
assert_eq!(
3625+
&Expr::TypedString {
3626+
data_type: DataType::JSON,
3627+
value: r#"{
3628+
"id": 10,
3629+
"type": "fruit",
3630+
"name": "apple",
3631+
"on_menu": true,
3632+
"recipes":
3633+
{
3634+
"salads":
3635+
[
3636+
{ "id": 2001, "type": "Walnut Apple Salad" },
3637+
{ "id": 2002, "type": "Apple Spinach Salad" }
3638+
],
3639+
"desserts":
3640+
[
3641+
{ "id": 3001, "type": "Apple Pie" },
3642+
{ "id": 3002, "type": "Apple Scones" },
3643+
{ "id": 3003, "type": "Apple Crumble" }
3644+
]
3645+
}
3646+
}"#
3647+
.into()
3648+
},
3649+
expr_from_projection(only(&select.projection)),
3650+
);
3651+
}
3652+
36013653
#[test]
36023654
fn parse_simple_math_expr_plus() {
36033655
let sql = "SELECT a + b, 2 + a, 2.5 + a, a_f + b_f, 2 + a_f, 2.5 + a_f FROM c";

0 commit comments

Comments
 (0)