MCPcopy Create free account
hub / github.com/MaterializeInc/materialize / jsonb_get_path

Function jsonb_get_path

src/expr/src/scalar/func.rs:1701–1727  ·  view source on GitHub ↗
(mut json: JsonbRef<'a>, b: Array<'a>)

Source from the content-addressed store, hash-verified

1699
1700#[sqlfunc(sqlname = "#>", is_infix_op = true)]
1701fn jsonb_get_path<'a>(mut json: JsonbRef<'a>, b: Array<'a>) -> Option<JsonbRef<'a>> {
1702 let path = b.elements();
1703 for key in path.iter() {
1704 let key = match key {
1705 Datum::String(s) => s,
1706 Datum::Null => return None,
1707 _ => unreachable!("keys in jsonb_get_path known to be strings"),
1708 };
1709 let v = match json.into_datum() {
1710 Datum::Map(map) => map.iter().find(|(k, _)| key == *k).map(|(_k, v)| v),
1711 Datum::List(list) => {
1712 let i = strconv::parse_int64(key).ok()?;
1713 let i = if i >= 0 {
1714 usize::cast_from(i.unsigned_abs())
1715 } else {
1716 // index backwards from the end
1717 let i = usize::cast_from(i.unsigned_abs());
1718 (list.iter().count()).wrapping_sub(i)
1719 };
1720 list.iter().nth(i)
1721 }
1722 _ => return None,
1723 }?;
1724 json = JsonbRef::try_from_result(Ok::<_, ()>(v)).ok()?;
1725 }
1726 Some(json)
1727}
1728
1729#[sqlfunc(sqlname = "#>>", is_infix_op = true)]
1730fn jsonb_get_path_stringify<'a>(

Callers 1

jsonb_get_path_stringifyFunction · 0.85

Calls 8

parse_int64Function · 0.85
elementsMethod · 0.80
iterMethod · 0.45
into_datumMethod · 0.45
mapMethod · 0.45
findMethod · 0.45
okMethod · 0.45
countMethod · 0.45

Tested by

no test coverage detected