mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-07-08 01:15:00 +00:00
Support json operators @>
<@
#-
@?
and @@
postgres supports a bunch more json operators. See https://www.postgresql.org/docs/15/functions-json.html Skipping operators starting with a question mark for now, since those are hard to distinguish from placeholders without more context.
This commit is contained in:
parent
fb02344131
commit
6d6eb4bc9b
4 changed files with 132 additions and 2 deletions
|
@ -1620,6 +1620,71 @@ fn test_json() {
|
|||
}),
|
||||
select.projection[0]
|
||||
);
|
||||
|
||||
let sql = "SELECT info FROM orders WHERE info @> '{\"a\": 1}'";
|
||||
let select = pg().verified_only_select(sql);
|
||||
assert_eq!(
|
||||
Expr::JsonAccess {
|
||||
left: Box::new(Expr::Identifier(Ident::new("info"))),
|
||||
operator: JsonOperator::AtArrow,
|
||||
right: Box::new(Expr::Value(Value::SingleQuotedString(
|
||||
"{\"a\": 1}".to_string()
|
||||
))),
|
||||
},
|
||||
select.selection.unwrap(),
|
||||
);
|
||||
|
||||
let sql = "SELECT info FROM orders WHERE '{\"a\": 1}' <@ info";
|
||||
let select = pg().verified_only_select(sql);
|
||||
assert_eq!(
|
||||
Expr::JsonAccess {
|
||||
left: Box::new(Expr::Value(Value::SingleQuotedString(
|
||||
"{\"a\": 1}".to_string()
|
||||
))),
|
||||
operator: JsonOperator::ArrowAt,
|
||||
right: Box::new(Expr::Identifier(Ident::new("info"))),
|
||||
},
|
||||
select.selection.unwrap(),
|
||||
);
|
||||
|
||||
let sql = "SELECT info #- ARRAY['a', 'b'] FROM orders";
|
||||
let select = pg().verified_only_select(sql);
|
||||
assert_eq!(
|
||||
SelectItem::UnnamedExpr(Expr::JsonAccess {
|
||||
left: Box::new(Expr::Identifier(Ident::from("info"))),
|
||||
operator: JsonOperator::HashMinus,
|
||||
right: Box::new(Expr::Array(Array {
|
||||
elem: vec![
|
||||
Expr::Value(Value::SingleQuotedString("a".to_string())),
|
||||
Expr::Value(Value::SingleQuotedString("b".to_string())),
|
||||
],
|
||||
named: true,
|
||||
})),
|
||||
}),
|
||||
select.projection[0],
|
||||
);
|
||||
|
||||
let sql = "SELECT info FROM orders WHERE info @? '$.a'";
|
||||
let select = pg().verified_only_select(sql);
|
||||
assert_eq!(
|
||||
Expr::JsonAccess {
|
||||
left: Box::new(Expr::Identifier(Ident::from("info"))),
|
||||
operator: JsonOperator::AtQuestion,
|
||||
right: Box::new(Expr::Value(Value::SingleQuotedString("$.a".to_string())),),
|
||||
},
|
||||
select.selection.unwrap(),
|
||||
);
|
||||
|
||||
let sql = "SELECT info FROM orders WHERE info @@ '$.a'";
|
||||
let select = pg().verified_only_select(sql);
|
||||
assert_eq!(
|
||||
Expr::JsonAccess {
|
||||
left: Box::new(Expr::Identifier(Ident::from("info"))),
|
||||
operator: JsonOperator::AtAt,
|
||||
right: Box::new(Expr::Value(Value::SingleQuotedString("$.a".to_string())),),
|
||||
},
|
||||
select.selection.unwrap(),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue