Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(query): refactor json functions #16840

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ backtrace = { git = "https://github.com/rust-lang/backtrace-rs.git", rev = "7226
color-eyre = { git = "https://github.com/eyre-rs/eyre.git", rev = "e5d92c3" }
deltalake = { git = "https://github.com/delta-io/delta-rs", rev = "3038c145" }
ethnum = { git = "https://github.com/datafuse-extras/ethnum-rs", rev = "4cb05f1" }
jsonb = { git = "https://github.com/databendlabs/jsonb", rev = "ada713c" }
jsonb = { git = "https://github.com/b41sh/jsonb", rev = "7ba454e7f6cc93b61c6d065468e3480998c88888" }
openai_api_rust = { git = "https://github.com/datafuse-extras/openai-api", rev = "819a0ed" }
openraft = { git = "https://github.com/databendlabs/openraft", tag = "v0.10.0-alpha.7" }
orc-rust = { git = "https://github.com/datafusion-contrib/orc-rust", rev = "dfb1ede" }
Expand Down
4 changes: 3 additions & 1 deletion src/query/expression/src/utils/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use geozero::GeozeroGeometry;
use geozero::ToGeos;
use geozero::ToWkt;
use itertools::Itertools;
use jsonb::RawJsonb;
use num_traits::FromPrimitive;
use rust_decimal::Decimal;
use rust_decimal::RoundingStrategy;
Expand Down Expand Up @@ -259,7 +260,8 @@ impl<'a> Display for ScalarRef<'a> {
write!(f, ")")
}
ScalarRef::Variant(s) => {
let value = jsonb::to_string(s);
let raw_jsonb = RawJsonb(s);
let value = raw_jsonb.to_string();
write!(f, "'{value}'")
}
ScalarRef::Geometry(s) => {
Expand Down
5 changes: 3 additions & 2 deletions src/query/expression/src/utils/variant_transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use databend_common_exception::ErrorCode;
use databend_common_exception::Result;
use jsonb::parse_value;
use jsonb::to_string;
use jsonb::RawJsonb;

use crate::types::AnyType;
use crate::types::DataType;
Expand Down Expand Up @@ -94,7 +94,8 @@ fn transform_scalar(scalar: ScalarRef<'_>, decode: bool) -> Result<Scalar> {
}
ScalarRef::Variant(data) => {
if decode {
Scalar::Variant(to_string(data).into_bytes())
let raw_jsonb = RawJsonb(data);
Scalar::Variant(raw_jsonb.to_string().into_bytes())
} else {
let value = parse_value(data).map_err(|err| {
ErrorCode::UDFDataError(format!("parse json value error: {err}"))
Expand Down
3 changes: 2 additions & 1 deletion src/query/formats/src/field_encoder/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use databend_common_io::constants::NULL_BYTES_LOWER;
use databend_common_io::constants::TRUE_BYTES_LOWER;
use geozero::wkb::Ewkb;
use geozero::ToJson;
use jsonb::RawJsonb;

use crate::field_encoder::helpers::write_json_string;
use crate::field_encoder::FieldEncoderValues;
Expand Down Expand Up @@ -77,7 +78,7 @@ impl FieldEncoderJSON {

Column::Variant(c) => {
let v = unsafe { c.index_unchecked(row_index) };
out_buf.extend_from_slice(jsonb::to_string(v).as_bytes());
out_buf.extend_from_slice(RawJsonb(v).to_string().as_bytes());
}
Column::Geometry(c) => {
let v = unsafe { c.index_unchecked(row_index) };
Expand Down
3 changes: 2 additions & 1 deletion src/query/formats/src/field_encoder/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ use geozero::ToGeos;
use geozero::ToJson;
use geozero::ToWkb;
use geozero::ToWkt;
use jsonb::RawJsonb;
use lexical_core::ToLexical;
use micromarshal::Marshal;
use micromarshal::Unmarshal;
Expand Down Expand Up @@ -296,7 +297,7 @@ impl FieldEncoderValues {
in_nested: bool,
) {
let v = unsafe { column.index_unchecked(row_index) };
let s = jsonb::to_string(v);
let s = RawJsonb(v).to_string();
self.write_string_inner(s.as_bytes(), out_buf, in_nested);
}

Expand Down
25 changes: 19 additions & 6 deletions src/query/functions/src/scalars/comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ use databend_common_expression::LikePattern;
use databend_common_expression::ScalarRef;
use databend_common_expression::SimpleDomainCmp;
use databend_common_expression::ValueRef;
use jsonb::RawJsonb;
use regex::Regex;

use crate::scalars::decimal::register_decimal_compare_op;
Expand Down Expand Up @@ -509,14 +510,26 @@ fn register_like(registry: &mut FunctionRegistry) {
| LikePattern::StartOfPercent(_)
| LikePattern::EndOfPercent(_)
| LikePattern::Constant(_) => {
if let Some(s) = jsonb::as_str(val) {
pattern_type.compare(s.as_bytes())
} else {
false
let raw_jsonb = RawJsonb(val);
match raw_jsonb.as_str() {
Ok(Some(s)) => pattern_type.compare(s.as_bytes()),
Ok(None) => false,
Err(_) => {
let s = raw_jsonb.to_string();
pattern_type.compare(s.as_bytes())
}
}
}
_ => {
let raw_jsonb = RawJsonb(val);
match raw_jsonb.traverse_check_string(|v| pattern_type.compare(v)) {
Ok(res) => res,
Err(_) => {
let s = raw_jsonb.to_string();
pattern_type.compare(s.as_bytes())
}
}
}

_ => jsonb::traverse_check_string(val, |v| pattern_type.compare(v)),
}),
);

Expand Down
5 changes: 3 additions & 2 deletions src/query/functions/src/scalars/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ use geozero::ToJson;
use geozero::ToWkb;
use geozero::ToWkt;
use jsonb::parse_value;
use jsonb::to_string;
use jsonb::RawJsonb;
use num_traits::AsPrimitive;
use proj4rs::transform::transform;
use proj4rs::Proj;
Expand Down Expand Up @@ -1695,7 +1695,8 @@ fn json_to_geometry_impl(
binary: &[u8],
srid: Option<i32>,
) -> databend_common_exception::Result<Vec<u8>> {
let s = to_string(binary);
let raw_jsonb = RawJsonb(binary);
let s = raw_jsonb.to_string();
let json = GeoJson(s.as_str());
match json.to_ewkb(CoordDimensions::xy(), srid) {
Ok(data) => Ok(data),
Expand Down
Loading
Loading