Skip to content

Commit

Permalink
refactor: remove JsonbValue and JsonVector
Browse files Browse the repository at this point in the history
  • Loading branch information
CookiePieWw committed Aug 26, 2024
1 parent f910b37 commit c334919
Show file tree
Hide file tree
Showing 15 changed files with 49 additions and 455 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ greptime-proto = { git = "https://github.com/CookiePieWw/greptime-proto.git", re
humantime = "2.1"
humantime-serde = "1.1"
itertools = "0.10"
jsonb = "0.4.1"
jsonb = "0.4"
lazy_static = "1.4"
meter-core = { git = "https://github.com/GreptimeTeam/greptime-meter.git", rev = "80eb97c24c88af4dd9a86f8bbaf50e741d4eb8cd" }
mockall = "0.11.4"
Expand Down
8 changes: 4 additions & 4 deletions src/api/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use datatypes::scalars::ScalarVector;
use datatypes::types::{
Int16Type, Int8Type, IntervalType, TimeType, TimestampType, UInt16Type, UInt8Type,
};
use datatypes::value::{JsonbValueRef, OrderedF32, OrderedF64, Value};
use datatypes::value::{OrderedF32, OrderedF64, Value};
use datatypes::vectors::{
BinaryVector, BooleanVector, DateTimeVector, DateVector, Decimal128Vector, Float32Vector,
Float64Vector, Int32Vector, Int64Vector, IntervalDayTimeVector, IntervalMonthDayNanoVector,
Expand Down Expand Up @@ -575,7 +575,7 @@ pub fn pb_value_to_value_ref<'a>(
))
}
}
ValueData::JsonValue(v) => ValueRef::Json(JsonbValueRef::new(v)),
ValueData::JsonValue(v) => ValueRef::Json(v),
}
}

Expand Down Expand Up @@ -938,7 +938,7 @@ pub fn to_proto_value(value: Value) -> Option<v1::Value> {
value_data: Some(ValueData::Decimal128Value(convert_to_pb_decimal128(v))),
},
Value::Json(v) => v1::Value {
value_data: Some(ValueData::JsonValue(v.value().to_vec())),
value_data: Some(ValueData::JsonValue(v.to_vec())),
},
Value::List(_) | Value::Duration(_) => return None,
};
Expand Down Expand Up @@ -1036,7 +1036,7 @@ pub fn value_to_grpc_value(value: Value) -> GrpcValue {
}
}),
Value::Decimal128(v) => Some(ValueData::Decimal128Value(convert_to_pb_decimal128(v))),
Value::Json(v) => Some(ValueData::JsonValue(v.value().to_vec())),
Value::Json(v) => Some(ValueData::JsonValue(v.to_vec())),
Value::List(_) | Value::Duration(_) => unreachable!(),
},
}
Expand Down
2 changes: 1 addition & 1 deletion src/common/function/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ common-time.workspace = true
common-version.workspace = true
datafusion.workspace = true
datatypes.workspace = true
jsonb.workspace = true
num = "0.4"
num-traits = "0.2"
once_cell.workspace = true
Expand All @@ -35,7 +36,6 @@ sql.workspace = true
statrs = "0.16"
store-api.workspace = true
table.workspace = true
jsonb.workspace = true

[dev-dependencies]
ron = "0.7"
Expand Down
39 changes: 3 additions & 36 deletions src/common/function/src/scalars/json/get_by_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,35 +57,7 @@ impl Function for GetByPathFunction {
let mut results = StringVectorBuilder::with_capacity(size);

match datatype {
ConcreteDataType::Json(_) => {
for i in 0..size {
let json = jsons.get_ref(i);
let json = json.as_json().unwrap();
let path = paths.get_ref(i);
let path = path.as_string().unwrap();
let result = match (json, path) {
(Some(json), Some(path)) => {
let json_path =
jsonb::jsonpath::parse_json_path(path.as_bytes()).unwrap();
let mut sub_jsonb = Vec::new();
let mut sub_offsets = Vec::new();
match jsonb::get_by_path(
json.value(),
json_path,
&mut sub_jsonb,
&mut sub_offsets,
) {
Ok(_) => Some(jsonb::to_string(&sub_jsonb)),
Err(_) => None,
}
}
_ => None,
};

results.push(result.as_deref());
}
}
ConcreteDataType::Binary(_) => {
ConcreteDataType::Binary(_) | ConcreteDataType::Json(_) => {
for i in 0..size {
let json = jsons.get_ref(i);
let json = json.as_binary().unwrap();
Expand Down Expand Up @@ -138,8 +110,7 @@ mod tests {

use common_query::prelude::TypeSignature;
use datatypes::scalars::ScalarVector;
use datatypes::value::JsonbValue;
use datatypes::vectors::{JsonVector, StringVector};
use datatypes::vectors::{BinaryVector, StringVector};

use super::*;

Expand Down Expand Up @@ -181,11 +152,7 @@ mod tests {
})
.collect::<Vec<_>>();

let json_values = jsonbs
.iter()
.map(|j| JsonbValue::new(j.clone()))
.collect::<Vec<_>>();
let json_vector = JsonVector::from_vec(json_values);
let json_vector = BinaryVector::from_vec(jsonbs);
let path_vector = StringVector::from_vec(paths);
let args: Vec<VectorRef> = vec![Arc::new(json_vector), Arc::new(path_vector)];
let vector = get_by_path.eval(FunctionContext::default(), &args).unwrap();
Expand Down
27 changes: 3 additions & 24 deletions src/datatypes/src/scalars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ use crate::types::{
Float32Type, Float64Type, Int16Type, Int32Type, Int64Type, Int8Type, UInt16Type, UInt32Type,
UInt64Type, UInt8Type,
};
use crate::value::{JsonbValue, JsonbValueRef, ListValue, ListValueRef, Value};
use crate::value::{ListValue, ListValueRef, Value};
use crate::vectors::{
BinaryVector, BooleanVector, DateTimeVector, DateVector, Decimal128Vector, JsonVector,
ListVector, MutableVector, PrimitiveVector, StringVector, Vector,
BinaryVector, BooleanVector, DateTimeVector, DateVector, Decimal128Vector, ListVector,
MutableVector, PrimitiveVector, StringVector, Vector,
};

fn get_iter_capacity<T, I: Iterator<Item = T>>(iter: &I) -> usize {
Expand Down Expand Up @@ -357,27 +357,6 @@ impl<'a> ScalarRef<'a> for ListValueRef<'a> {
}
}

impl Scalar for JsonbValue {
type VectorType = JsonVector;
type RefType<'a> = JsonbValueRef<'a>;

fn as_scalar_ref(&self) -> Self::RefType<'_> {
self.as_jsonb_ref()
}

fn upcast_gat<'short, 'long: 'short>(long: Self::RefType<'long>) -> Self::RefType<'short> {
long
}
}

impl<'a> ScalarRef<'a> for JsonbValueRef<'a> {
type ScalarType = JsonbValue;

fn to_owned_scalar(&self) -> Self::ScalarType {
Self::ScalarType::new(self.value().to_vec())
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
9 changes: 5 additions & 4 deletions src/datatypes/src/types/json_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
use std::sync::Arc;

use arrow::datatypes::DataType as ArrowDataType;
use common_base::bytes::Bytes;
use serde::{Deserialize, Serialize};

use crate::data_type::{DataType, DataTypeRef};
use crate::scalars::ScalarVectorBuilder;
use crate::type_id::LogicalTypeId;
use crate::value::{JsonbValue, Value};
use crate::vectors::{JsonVectorBuilder, MutableVector};
use crate::value::Value;
use crate::vectors::{BinaryVectorBuilder, MutableVector};

#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub struct JsonType;
Expand All @@ -42,15 +43,15 @@ impl DataType for JsonType {
}

fn default_value(&self) -> Value {
Value::Json(JsonbValue::default())
Bytes::default().into()
}

fn as_arrow_type(&self) -> ArrowDataType {
ArrowDataType::Binary
}

fn create_mutable_vector(&self, capacity: usize) -> Box<dyn MutableVector> {
Box::new(JsonVectorBuilder::with_capacity(capacity))
Box::new(BinaryVectorBuilder::with_capacity(capacity))
}

fn try_cast(&self, from: Value) -> Option<Value> {
Expand Down
2 changes: 1 addition & 1 deletion src/datatypes/src/types/string_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl DataType for StringType {
Value::Interval(v) => Some(Value::String(StringBytes::from(v.to_iso8601_string()))),
Value::Duration(v) => Some(Value::String(StringBytes::from(v.to_string()))),
Value::Decimal128(v) => Some(Value::String(StringBytes::from(v.to_string()))),
Value::Json(v) => Some(Value::String(StringBytes::from(v.to_json_string()))),
Value::Json(v) => Some(Value::String(StringBytes::from(jsonb::to_string(&v)))),

// StringBytes is only support for utf-8, Value::Binary is not allowed.
Value::Binary(_) | Value::List(_) => None,
Expand Down
Loading

0 comments on commit c334919

Please sign in to comment.