Skip to content

Commit

Permalink
fix some corner cases
Browse files Browse the repository at this point in the history
Signed-off-by: Runji Wang <[email protected]>
  • Loading branch information
wangrunji0408 committed Oct 30, 2023
1 parent 6d34335 commit d9f1758
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/common/src/types/jsonb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,14 @@ impl<'a> JsonbRef<'a> {
self.0.as_null().is_some()
}

/// Returns true if this is a jsonb null, boolean, number or string.
pub fn is_scalar(&self) -> bool {
matches!(
self.0,
ValueRef::Null | ValueRef::Bool(_) | ValueRef::Number(_) | ValueRef::String(_)
)
}

/// Returns true if this is a jsonb array.
pub fn is_array(&self) -> bool {
matches!(self.0, ValueRef::Array(_))
Expand Down
29 changes: 29 additions & 0 deletions src/expr/impl/src/scalar/jsonb_delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,30 @@ fn jsonb_remove_index(v: JsonbRef<'_>, index: i32) -> Result<JsonbVal> {
/// SELECT '{"a": 1}'::jsonb #- '{}';
/// ----
/// {"a": 1}
///
/// # Edge case: Empty array
/// query T
/// SELECT '[]'::jsonb #- '{a}';
/// ----
/// []
///
/// # Edge case: Empty object
/// query T
/// SELECT '{}'::jsonb #- '{null}';
/// ----
/// {}
///
/// query error cannot delete path in scalar
/// SELECT '1'::jsonb #- '{}';
/// ```
#[function("jsonb_delete_path(jsonb, varchar[]) -> jsonb")]
fn jsonb_delete_path(v: JsonbRef<'_>, path: ListRef<'_>) -> Result<JsonbVal> {
if v.is_scalar() {
return Err(ExprError::InvalidParam {
name: "jsonb",
reason: "cannot delete path in scalar".into(),
});
}
if path.is_empty() {
return Ok(JsonbVal::from(v));
}
Expand All @@ -253,6 +274,10 @@ fn jsonbb_remove_path(
) -> Result<()> {
match jsonb {
ValueRef::Object(obj) => {
if obj.is_empty() {
builder.add_value(jsonb);
return Ok(());
}
let key = path
.get(i)
.unwrap()
Expand Down Expand Up @@ -282,6 +307,10 @@ fn jsonbb_remove_path(
Ok(())
}
ValueRef::Array(array) => {
if array.is_empty() {
builder.add_value(jsonb);
return Ok(());
}
let key = path
.get(i)
.unwrap()
Expand Down

0 comments on commit d9f1758

Please sign in to comment.