rename functions

This commit is contained in:
Mikaël Francoeur
2025-08-15 14:50:37 -04:00
parent 4a986080d2
commit 2ee0132afe
13 changed files with 307 additions and 328 deletions

View File

@@ -896,7 +896,7 @@ impl Jsonb {
Ok((header, offset))
}
pub fn is_valid(&self) -> Result<ElementType> {
pub fn element_type(&self) -> Result<ElementType> {
match self.read_header(0) {
Ok((header, offset)) => {
if self.data.get(offset..offset + header.1).is_some() {
@@ -2746,7 +2746,7 @@ impl Jsonb {
Ok(pos)
}
// Primitive implementation could be optimized.
// TODO Primitive implementation could be optimized.
pub fn patch(&mut self, patch: &Jsonb) -> Result<()> {
let (patch_header, _) = patch.read_header(0)?;
@@ -3578,14 +3578,14 @@ world""#,
fn test_jsonb_is_valid() {
// Valid JSONB
let jsonb = Jsonb::from_str(r#"{"test":"value"}"#).unwrap();
assert!(jsonb.is_valid().is_ok());
assert!(jsonb.element_type().is_ok());
// Invalid JSONB (manually corrupted)
let mut invalid = jsonb.data.clone();
if !invalid.is_empty() {
invalid[0] = 0xFF; // Invalid element type
let jsonb = Jsonb::new(0, Some(&invalid));
assert!(jsonb.is_valid().is_err());
assert!(jsonb.element_type().is_err());
}
}

View File

@@ -51,7 +51,7 @@ pub fn get_json(json_value: &Value, indent: Option<&str>) -> crate::Result<Value
}
Value::Blob(b) => {
let jsonbin = Jsonb::new(b.len(), Some(b));
jsonbin.is_valid()?;
jsonbin.element_type()?;
Ok(Value::Text(Text {
value: jsonbin.to_string()?.into_bytes(),
subtype: TextSubtype::Json,
@@ -63,7 +63,7 @@ pub fn get_json(json_value: &Value, indent: Option<&str>) -> crate::Result<Value
let json = match indent {
Some(indent) => Value::Text(Text::json(json_val.to_string_pretty(Some(indent))?)),
None => {
let element_type = json_val.is_valid()?;
let element_type = json_val.element_type()?;
json_string_to_db_type(json_val, element_type, OutputVariant::ElementType)?
}
};
@@ -93,7 +93,7 @@ pub fn convert_dbtype_to_raw_jsonb(data: &Value) -> crate::Result<Vec<u8>> {
pub fn json_from_raw_bytes_agg(data: &[u8], raw: bool) -> crate::Result<Value> {
let mut json = Jsonb::from_raw_data(data);
let el_type = json.is_valid()?;
let el_type = json.element_type()?;
json.finalize_unsafe(el_type)?;
if raw {
json_string_to_db_type(json, el_type, OutputVariant::Binary)
@@ -144,7 +144,7 @@ pub fn convert_ref_dbtype_to_jsonb(val: &RefValue, strict: Conv) -> crate::Resul
}
RefValue::Blob(blob) => {
let json = Jsonb::from_raw_data(blob.to_slice());
json.is_valid()?;
json.element_type()?;
Ok(json)
}
RefValue::Null => Ok(Jsonb::from_raw_data(
@@ -168,10 +168,10 @@ pub fn json_array(values: &[Register]) -> crate::Result<Value> {
let mut json = Jsonb::make_empty_array(values.len());
for value in values.iter() {
if matches!(value.get_owned_value(), Value::Blob(_)) {
if matches!(value.get_value(), Value::Blob(_)) {
crate::bail_constraint_error!("JSON cannot hold BLOB values")
}
let value = convert_dbtype_to_jsonb(value.get_owned_value(), Conv::NotStrict)?;
let value = convert_dbtype_to_jsonb(value.get_value(), Conv::NotStrict)?;
json.append_jsonb_to_end(value.data());
}
json.finalize_unsafe(ElementType::ARRAY)?;
@@ -183,10 +183,10 @@ pub fn jsonb_array(values: &[Register]) -> crate::Result<Value> {
let mut json = Jsonb::make_empty_array(values.len());
for value in values.iter() {
if matches!(value.get_owned_value(), Value::Blob(_)) {
if matches!(value.get_value(), Value::Blob(_)) {
crate::bail_constraint_error!("JSON cannot hold BLOB values")
}
let value = convert_dbtype_to_jsonb(value.get_owned_value(), Conv::NotStrict)?;
let value = convert_dbtype_to_jsonb(value.get_value(), Conv::NotStrict)?;
json.append_jsonb_to_end(value.data());
}
json.finalize_unsafe(ElementType::ARRAY)?;
@@ -207,7 +207,7 @@ pub fn json_array_length(
return Ok(Value::Integer(len as i64));
}
let path = json_path_from_owned_value(path.expect("We already checked none"), true)?;
let path = json_path_from_db_value(path.expect("We already checked none"), true)?;
if let Some(path) = path {
let mut op = SearchOperation::new(json.len() / 2);
@@ -225,20 +225,20 @@ pub fn json_set(args: &[Register], json_cache: &JsonCacheCell) -> crate::Result<
}
let make_jsonb_fn = curry_convert_dbtype_to_jsonb(Conv::Strict);
let mut json = json_cache.get_or_insert_with(args[0].get_owned_value(), make_jsonb_fn)?;
let mut json = json_cache.get_or_insert_with(args[0].get_value(), make_jsonb_fn)?;
let other = args[1..].chunks_exact(2);
for chunk in other {
let path = json_path_from_owned_value(chunk[0].get_owned_value(), true)?;
let path = json_path_from_db_value(chunk[0].get_value(), true)?;
let value = convert_dbtype_to_jsonb(chunk[1].get_owned_value(), Conv::NotStrict)?;
let value = convert_dbtype_to_jsonb(chunk[1].get_value(), Conv::NotStrict)?;
let mut op = SetOperation::new(value);
if let Some(path) = path {
let _ = json.operate_on_path(&path, &mut op);
}
}
let el_type = json.is_valid()?;
let el_type = json.element_type()?;
json_string_to_db_type(json, el_type, OutputVariant::String)
}
@@ -249,20 +249,20 @@ pub fn jsonb_set(args: &[Register], json_cache: &JsonCacheCell) -> crate::Result
}
let make_jsonb_fn = curry_convert_dbtype_to_jsonb(Conv::Strict);
let mut json = json_cache.get_or_insert_with(args[0].get_owned_value(), make_jsonb_fn)?;
let mut json = json_cache.get_or_insert_with(args[0].get_value(), make_jsonb_fn)?;
let other = args[1..].chunks_exact(2);
for chunk in other {
let path = json_path_from_owned_value(chunk[0].get_owned_value(), true)?;
let path = json_path_from_db_value(chunk[0].get_value(), true)?;
let value = convert_dbtype_to_jsonb(chunk[1].get_owned_value(), Conv::NotStrict)?;
let value = convert_dbtype_to_jsonb(chunk[1].get_value(), Conv::NotStrict)?;
let mut op = SetOperation::new(value);
if let Some(path) = path {
let _ = json.operate_on_path(&path, &mut op);
}
}
let el_type = json.is_valid()?;
let el_type = json.element_type()?;
json_string_to_db_type(json, el_type, OutputVariant::Binary)
}
@@ -278,7 +278,7 @@ pub fn json_arrow_extract(
return Ok(Value::Null);
}
if let Some(path) = json_path_from_owned_value(path, false)? {
if let Some(path) = json_path_from_db_value(path, false)? {
let make_jsonb_fn = curry_convert_dbtype_to_jsonb(Conv::Strict);
let mut json = json_cache.get_or_insert_with(value, make_jsonb_fn)?;
let mut op = SearchOperation::new(json.len());
@@ -304,13 +304,13 @@ pub fn json_arrow_shift_extract(
if let Value::Null = value {
return Ok(Value::Null);
}
if let Some(path) = json_path_from_owned_value(path, false)? {
if let Some(path) = json_path_from_db_value(path, false)? {
let make_jsonb_fn = curry_convert_dbtype_to_jsonb(Conv::Strict);
let mut json = json_cache.get_or_insert_with(value, make_jsonb_fn)?;
let mut op = SearchOperation::new(json.len());
let res = json.operate_on_path(&path, &mut op);
let extracted = op.result();
let element_type = match extracted.is_valid() {
let element_type = match extracted.element_type() {
Err(_) => return Ok(Value::Null),
Ok(el) => el,
};
@@ -377,13 +377,13 @@ pub fn jsonb_extract(
fn jsonb_extract_internal(value: Jsonb, paths: &[Register]) -> crate::Result<(Jsonb, ElementType)> {
let null = Jsonb::from_raw_data(JsonbHeader::make_null().into_bytes().as_bytes());
if paths.len() == 1 {
if let Some(path) = json_path_from_owned_value(paths[0].get_owned_value(), true)? {
if let Some(path) = json_path_from_db_value(paths[0].get_value(), true)? {
let mut json = value;
let mut op = SearchOperation::new(json.len());
let res = json.operate_on_path(&path, &mut op);
let extracted = op.result();
let element_type = match extracted.is_valid() {
let element_type = match extracted.element_type() {
Err(_) => return Ok((null, ElementType::NULL)),
Ok(el) => el,
};
@@ -403,7 +403,7 @@ fn jsonb_extract_internal(value: Jsonb, paths: &[Register]) -> crate::Result<(Js
// TODO: make an op to avoid creating new json for every path element
let paths = paths
.iter()
.map(|p| json_path_from_owned_value(p.get_owned_value(), true));
.map(|p| json_path_from_db_value(p.get_value(), true));
for path in paths {
if let Some(path) = path? {
let mut op = SearchOperation::new(json.len());
@@ -487,11 +487,11 @@ pub fn json_type(value: &Value, path: Option<&Value>) -> crate::Result<Value> {
}
if path.is_none() {
let json = convert_dbtype_to_jsonb(value, Conv::Strict)?;
let element_type = json.is_valid()?;
let element_type = json.element_type()?;
return Ok(Value::Text(Text::json(element_type.into())));
}
if let Some(path) = json_path_from_owned_value(path.unwrap(), true)? {
if let Some(path) = json_path_from_db_value(path.unwrap(), true)? {
let mut json = convert_dbtype_to_jsonb(value, Conv::Strict)?;
if let Ok(mut path) = json.navigate_path(&path, PathOperationMode::ReplaceExisting) {
@@ -510,7 +510,7 @@ pub fn json_type(value: &Value, path: Option<&Value>) -> crate::Result<Value> {
}
}
fn json_path_from_owned_value(path: &Value, strict: bool) -> crate::Result<Option<JsonPath>> {
fn json_path_from_db_value(path: &Value, strict: bool) -> crate::Result<Option<JsonPath>> {
let json_path = if strict {
match path {
Value::Text(t) => json_path(t.as_str())?,
@@ -584,12 +584,12 @@ pub fn json_object(values: &[Register]) -> crate::Result<Value> {
let mut json = Jsonb::make_empty_obj(values.len() * 50);
for chunk in values.chunks_exact(2) {
if chunk[0].get_owned_value().value_type() != ValueType::Text {
if chunk[0].get_value().value_type() != ValueType::Text {
bail_constraint_error!("json_object() labels must be TEXT")
}
let key = convert_dbtype_to_jsonb(chunk[0].get_owned_value(), Conv::ToString)?;
let key = convert_dbtype_to_jsonb(chunk[0].get_value(), Conv::ToString)?;
json.append_jsonb_to_end(key.data());
let value = convert_dbtype_to_jsonb(chunk[1].get_owned_value(), Conv::NotStrict)?;
let value = convert_dbtype_to_jsonb(chunk[1].get_value(), Conv::NotStrict)?;
json.append_jsonb_to_end(value.data());
}
@@ -605,12 +605,12 @@ pub fn jsonb_object(values: &[Register]) -> crate::Result<Value> {
let mut json = Jsonb::make_empty_obj(values.len() * 50);
for chunk in values.chunks_exact(2) {
if chunk[0].get_owned_value().value_type() != ValueType::Text {
if chunk[0].get_value().value_type() != ValueType::Text {
bail_constraint_error!("json_object() labels must be TEXT")
}
let key = convert_dbtype_to_jsonb(chunk[0].get_owned_value(), Conv::ToString)?;
let key = convert_dbtype_to_jsonb(chunk[0].get_value(), Conv::ToString)?;
json.append_jsonb_to_end(key.data());
let value = convert_dbtype_to_jsonb(chunk[1].get_owned_value(), Conv::NotStrict)?;
let value = convert_dbtype_to_jsonb(chunk[1].get_value(), Conv::NotStrict)?;
json.append_jsonb_to_end(value.data());
}
@@ -1174,10 +1174,10 @@ mod tests {
}
#[test]
fn test_json_path_from_owned_value_root_strict() {
fn test_json_path_from_db_value_root_strict() {
let path = Value::Text(Text::new("$"));
let result = json_path_from_owned_value(&path, true);
let result = json_path_from_db_value(&path, true);
assert!(result.is_ok());
let result = result.unwrap();
@@ -1191,10 +1191,10 @@ mod tests {
}
#[test]
fn test_json_path_from_owned_value_root_non_strict() {
fn test_json_path_from_db_value_root_non_strict() {
let path = Value::Text(Text::new("$"));
let result = json_path_from_owned_value(&path, false);
let result = json_path_from_db_value(&path, false);
assert!(result.is_ok());
let result = result.unwrap();
@@ -1208,17 +1208,17 @@ mod tests {
}
#[test]
fn test_json_path_from_owned_value_named_strict() {
fn test_json_path_from_db_value_named_strict() {
let path = Value::Text(Text::new("field"));
assert!(json_path_from_owned_value(&path, true).is_err());
assert!(json_path_from_db_value(&path, true).is_err());
}
#[test]
fn test_json_path_from_owned_value_named_non_strict() {
fn test_json_path_from_db_value_named_non_strict() {
let path = Value::Text(Text::new("field"));
let result = json_path_from_owned_value(&path, false);
let result = json_path_from_db_value(&path, false);
assert!(result.is_ok());
let result = result.unwrap();
@@ -1232,16 +1232,16 @@ mod tests {
}
#[test]
fn test_json_path_from_owned_value_integer_strict() {
fn test_json_path_from_db_value_integer_strict() {
let path = Value::Integer(3);
assert!(json_path_from_owned_value(&path, true).is_err());
assert!(json_path_from_db_value(&path, true).is_err());
}
#[test]
fn test_json_path_from_owned_value_integer_non_strict() {
fn test_json_path_from_db_value_integer_non_strict() {
let path = Value::Integer(3);
let result = json_path_from_owned_value(&path, false);
let result = json_path_from_db_value(&path, false);
assert!(result.is_ok());
let result = result.unwrap();
@@ -1255,10 +1255,10 @@ mod tests {
}
#[test]
fn test_json_path_from_owned_value_null_strict() {
fn test_json_path_from_db_value_null_strict() {
let path = Value::Null;
let result = json_path_from_owned_value(&path, true);
let result = json_path_from_db_value(&path, true);
assert!(result.is_ok());
let result = result.unwrap();
@@ -1266,10 +1266,10 @@ mod tests {
}
#[test]
fn test_json_path_from_owned_value_null_non_strict() {
fn test_json_path_from_db_value_null_non_strict() {
let path = Value::Null;
let result = json_path_from_owned_value(&path, false);
let result = json_path_from_db_value(&path, false);
assert!(result.is_ok());
let result = result.unwrap();
@@ -1277,17 +1277,17 @@ mod tests {
}
#[test]
fn test_json_path_from_owned_value_float_strict() {
fn test_json_path_from_db_value_float_strict() {
let path = Value::Float(1.23);
assert!(json_path_from_owned_value(&path, true).is_err());
assert!(json_path_from_db_value(&path, true).is_err());
}
#[test]
fn test_json_path_from_owned_value_float_non_strict() {
fn test_json_path_from_db_value_float_non_strict() {
let path = Value::Float(1.23);
let result = json_path_from_owned_value(&path, false);
let result = json_path_from_db_value(&path, false);
assert!(result.is_ok());
let result = result.unwrap();

View File

@@ -1,7 +1,7 @@
use crate::{types::Value, vdbe::Register};
use super::{
convert_dbtype_to_jsonb, curry_convert_dbtype_to_jsonb, json_path_from_owned_value,
convert_dbtype_to_jsonb, curry_convert_dbtype_to_jsonb, json_path_from_db_value,
json_string_to_db_type,
jsonb::{DeleteOperation, InsertOperation, ReplaceOperation},
Conv, JsonCacheCell, OutputVariant,
@@ -25,7 +25,7 @@ pub fn json_patch(target: &Value, patch: &Value, cache: &JsonCacheCell) -> crate
target.patch(&patch)?;
let element_type = target.is_valid()?;
let element_type = target.element_type()?;
json_string_to_db_type(target, element_type, OutputVariant::ElementType)
}
@@ -43,7 +43,7 @@ pub fn jsonb_patch(target: &Value, patch: &Value, cache: &JsonCacheCell) -> crat
target.patch(&patch)?;
let element_type = target.is_valid()?;
let element_type = target.element_type()?;
json_string_to_db_type(target, element_type, OutputVariant::Binary)
}
@@ -54,15 +54,15 @@ pub fn json_remove(args: &[Register], json_cache: &JsonCacheCell) -> crate::Resu
}
let make_jsonb_fn = curry_convert_dbtype_to_jsonb(Conv::Strict);
let mut json = json_cache.get_or_insert_with(args[0].get_owned_value(), make_jsonb_fn)?;
let mut json = json_cache.get_or_insert_with(args[0].get_value(), make_jsonb_fn)?;
for arg in &args[1..] {
if let Some(path) = json_path_from_owned_value(arg.get_owned_value(), true)? {
if let Some(path) = json_path_from_db_value(arg.get_value(), true)? {
let mut op = DeleteOperation::new();
let _ = json.operate_on_path(&path, &mut op);
}
}
let el_type = json.is_valid()?;
let el_type = json.element_type()?;
json_string_to_db_type(json, el_type, OutputVariant::String)
}
@@ -73,9 +73,9 @@ pub fn jsonb_remove(args: &[Register], json_cache: &JsonCacheCell) -> crate::Res
}
let make_jsonb_fn = curry_convert_dbtype_to_jsonb(Conv::Strict);
let mut json = json_cache.get_or_insert_with(args[0].get_owned_value(), make_jsonb_fn)?;
let mut json = json_cache.get_or_insert_with(args[0].get_value(), make_jsonb_fn)?;
for arg in &args[1..] {
if let Some(path) = json_path_from_owned_value(arg.get_owned_value(), true)? {
if let Some(path) = json_path_from_db_value(arg.get_value(), true)? {
let mut op = DeleteOperation::new();
let _ = json.operate_on_path(&path, &mut op);
}
@@ -90,12 +90,12 @@ pub fn json_replace(args: &[Register], json_cache: &JsonCacheCell) -> crate::Res
}
let make_jsonb_fn = curry_convert_dbtype_to_jsonb(Conv::Strict);
let mut json = json_cache.get_or_insert_with(args[0].get_owned_value(), make_jsonb_fn)?;
let mut json = json_cache.get_or_insert_with(args[0].get_value(), make_jsonb_fn)?;
let other = args[1..].chunks_exact(2);
for chunk in other {
let path = json_path_from_owned_value(chunk[0].get_owned_value(), true)?;
let path = json_path_from_db_value(chunk[0].get_value(), true)?;
let value = convert_dbtype_to_jsonb(chunk[1].get_owned_value(), Conv::NotStrict)?;
let value = convert_dbtype_to_jsonb(chunk[1].get_value(), Conv::NotStrict)?;
if let Some(path) = path {
let mut op = ReplaceOperation::new(value);
@@ -103,7 +103,7 @@ pub fn json_replace(args: &[Register], json_cache: &JsonCacheCell) -> crate::Res
}
}
let el_type = json.is_valid()?;
let el_type = json.element_type()?;
json_string_to_db_type(json, el_type, super::OutputVariant::String)
}
@@ -114,11 +114,11 @@ pub fn jsonb_replace(args: &[Register], json_cache: &JsonCacheCell) -> crate::Re
}
let make_jsonb_fn = curry_convert_dbtype_to_jsonb(Conv::Strict);
let mut json = json_cache.get_or_insert_with(args[0].get_owned_value(), make_jsonb_fn)?;
let mut json = json_cache.get_or_insert_with(args[0].get_value(), make_jsonb_fn)?;
let other = args[1..].chunks_exact(2);
for chunk in other {
let path = json_path_from_owned_value(chunk[0].get_owned_value(), true)?;
let value = convert_dbtype_to_jsonb(chunk[1].get_owned_value(), Conv::NotStrict)?;
let path = json_path_from_db_value(chunk[0].get_value(), true)?;
let value = convert_dbtype_to_jsonb(chunk[1].get_value(), Conv::NotStrict)?;
if let Some(path) = path {
let mut op = ReplaceOperation::new(value);
@@ -126,7 +126,7 @@ pub fn jsonb_replace(args: &[Register], json_cache: &JsonCacheCell) -> crate::Re
}
}
let el_type = json.is_valid()?;
let el_type = json.element_type()?;
json_string_to_db_type(json, el_type, OutputVariant::Binary)
}
@@ -137,11 +137,11 @@ pub fn json_insert(args: &[Register], json_cache: &JsonCacheCell) -> crate::Resu
}
let make_jsonb_fn = curry_convert_dbtype_to_jsonb(Conv::Strict);
let mut json = json_cache.get_or_insert_with(args[0].get_owned_value(), make_jsonb_fn)?;
let mut json = json_cache.get_or_insert_with(args[0].get_value(), make_jsonb_fn)?;
let other = args[1..].chunks_exact(2);
for chunk in other {
let path = json_path_from_owned_value(chunk[0].get_owned_value(), true)?;
let value = convert_dbtype_to_jsonb(chunk[1].get_owned_value(), Conv::NotStrict)?;
let path = json_path_from_db_value(chunk[0].get_value(), true)?;
let value = convert_dbtype_to_jsonb(chunk[1].get_value(), Conv::NotStrict)?;
if let Some(path) = path {
let mut op = InsertOperation::new(value);
@@ -149,7 +149,7 @@ pub fn json_insert(args: &[Register], json_cache: &JsonCacheCell) -> crate::Resu
}
}
let el_type = json.is_valid()?;
let el_type = json.element_type()?;
json_string_to_db_type(json, el_type, OutputVariant::String)
}
@@ -160,11 +160,11 @@ pub fn jsonb_insert(args: &[Register], json_cache: &JsonCacheCell) -> crate::Res
}
let make_jsonb_fn = curry_convert_dbtype_to_jsonb(Conv::Strict);
let mut json = json_cache.get_or_insert_with(args[0].get_owned_value(), make_jsonb_fn)?;
let mut json = json_cache.get_or_insert_with(args[0].get_value(), make_jsonb_fn)?;
let other = args[1..].chunks_exact(2);
for chunk in other {
let path = json_path_from_owned_value(chunk[0].get_owned_value(), true)?;
let value = convert_dbtype_to_jsonb(chunk[1].get_owned_value(), Conv::NotStrict)?;
let path = json_path_from_db_value(chunk[0].get_value(), true)?;
let value = convert_dbtype_to_jsonb(chunk[1].get_value(), Conv::NotStrict)?;
if let Some(path) = path {
let mut op = InsertOperation::new(value);
@@ -172,7 +172,7 @@ pub fn jsonb_insert(args: &[Register], json_cache: &JsonCacheCell) -> crate::Res
}
}
let el_type = json.is_valid()?;
let el_type = json.element_type()?;
json_string_to_db_type(json, el_type, OutputVariant::Binary)
}