mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-10-28 06:36:18 +00:00
refactor: separate a json2 extension type (#8745)
Signed-off-by: luofucong <luofc@foxmail.com>
This commit is contained in:
@@ -404,7 +404,6 @@ impl Function for JsonGetWithType {
|
||||
let result = match arg0.data_type() {
|
||||
DataType::Binary | DataType::LargeBinary | DataType::BinaryView => {
|
||||
let arg0 = compute::cast(&arg0, &DataType::BinaryView)?;
|
||||
let jsons = arg0.as_binary_view();
|
||||
|
||||
if args.arg_fields.first().is_some_and(is_json2_extension_type) {
|
||||
// Query concretization projects nested JSON2 paths as Struct arrays. A binary
|
||||
@@ -414,6 +413,7 @@ impl Function for JsonGetWithType {
|
||||
.project_to(&with_type)
|
||||
.map_err(|e| exec_datafusion_err!("{e:?}"))?
|
||||
} else {
|
||||
let jsons = arg0.as_binary_view();
|
||||
let mut builder = result_builder(len, &with_type)?;
|
||||
jsonb_get(jsons, path, builder.as_mut())?;
|
||||
builder.build()
|
||||
|
||||
@@ -45,7 +45,7 @@ use datatypes::arrow::util::display::{
|
||||
ArrayFormatter, ArrayFormatterFactory, DisplayIndex, FormatOptions, FormatResult,
|
||||
};
|
||||
use datatypes::arrow::util::pretty::pretty_format_batches_with_options;
|
||||
use datatypes::extension::json::is_json_extension_type;
|
||||
use datatypes::extension::json::is_any_json_extension_type;
|
||||
use datatypes::prelude::{ConcreteDataType, DataType, VectorRef};
|
||||
use datatypes::schema::{ColumnSchema, Schema, SchemaRef};
|
||||
use datatypes::types::{JsonFormat, StructField, StructType, jsonb_to_string};
|
||||
@@ -455,7 +455,7 @@ impl ArrayFormatterFactory for BinaryFormatterFactory {
|
||||
Ok(Some(ArrayFormatter::new(
|
||||
Box::new(BinaryFormatter {
|
||||
array,
|
||||
is_json: field.is_some_and(is_json_extension_type),
|
||||
is_json: field.is_some_and(is_any_json_extension_type),
|
||||
default: ArrayFormatter::try_new(array, options)?,
|
||||
null: options.null(),
|
||||
}),
|
||||
@@ -485,7 +485,7 @@ impl DisplayIndex for BinaryFormatter<'_> {
|
||||
ArrowDataType::Binary => self.array.as_binary::<i32>().value(idx),
|
||||
ArrowDataType::LargeBinary => self.array.as_binary::<i64>().value(idx),
|
||||
ArrowDataType::BinaryView => self.array.as_binary_view().value(idx),
|
||||
_ => unreachable!(),
|
||||
_ => return Ok(self.default.value(idx).write(f)?),
|
||||
};
|
||||
let value =
|
||||
jsonb_to_string(bytes).map_err(|e| ArrowError::ExternalError(Box::new(e)))?;
|
||||
|
||||
@@ -378,7 +378,7 @@ mod tests {
|
||||
use datatypes::arrow::array::{AsArray, StringArray, StringViewArray, UInt32Array};
|
||||
use datatypes::arrow::datatypes::{DataType, Field, Schema as ArrowSchema, UInt32Type};
|
||||
use datatypes::data_type::ConcreteDataType;
|
||||
use datatypes::extension::json::{JsonExtensionType, JsonMetadata};
|
||||
use datatypes::extension::json::JsonExtensionType;
|
||||
use datatypes::schema::{ColumnSchema, Schema};
|
||||
use datatypes::vectors::{BinaryVector, StringVector, UInt32Vector};
|
||||
|
||||
@@ -594,8 +594,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_legacy_json_with_extension_does_not_align_as_structured_json() {
|
||||
let field = Field::new("j", DataType::Binary, true)
|
||||
.with_extension_type(JsonExtensionType::new(Arc::new(JsonMetadata::default())));
|
||||
let field = Field::new("j", DataType::Binary, true).with_extension_type(JsonExtensionType);
|
||||
let arrow_schema = Arc::new(ArrowSchema::new(vec![field]));
|
||||
let schema = Arc::new(Schema::try_from(arrow_schema).unwrap());
|
||||
let columns: Vec<VectorRef> = vec![Arc::new(BinaryVector::from(vec![Some(
|
||||
|
||||
@@ -17,7 +17,7 @@ use std::str::FromStr;
|
||||
use arrow_schema::extension::ExtensionType;
|
||||
use common_time::Timestamp;
|
||||
use common_time::timezone::Timezone;
|
||||
use datatypes::extension::json::JsonExtensionType;
|
||||
use datatypes::extension::json::{Json2ExtensionType, parse_legacy_json2_settings};
|
||||
use datatypes::prelude::ConcreteDataType;
|
||||
use datatypes::schema::{ColumnDefaultConstraint, ColumnSchema};
|
||||
use datatypes::types::{JsonFormat, parse_string_to_jsonb, parse_string_to_vector_type_value};
|
||||
@@ -307,13 +307,20 @@ pub(crate) fn parse_string_to_value(
|
||||
Ok(Value::Binary(v.into()))
|
||||
}
|
||||
JsonFormat::Json2(_) => {
|
||||
let extension_type: Option<JsonExtensionType> =
|
||||
column_schema.extension_type().context(DatatypeSnafu)?;
|
||||
let json_settings = extension_type
|
||||
.and_then(|x| x.metadata().json_settings.clone())
|
||||
.unwrap_or_default();
|
||||
let v = serde_json::from_str(&s).context(DeserializeSnafu { json: s })?;
|
||||
json_settings.encode(v).context(DatatypeSnafu)
|
||||
|
||||
if let Some(extension) = column_schema
|
||||
.extension_type::<Json2ExtensionType>()
|
||||
.context(DatatypeSnafu)?
|
||||
{
|
||||
extension.metadata().json_settings().encode(v)
|
||||
} else {
|
||||
parse_legacy_json2_settings(column_schema.metadata())
|
||||
.context(DatatypeSnafu)?
|
||||
.unwrap_or_default()
|
||||
.encode(v)
|
||||
}
|
||||
.context(DatatypeSnafu)
|
||||
}
|
||||
},
|
||||
ConcreteDataType::Vector(d) => {
|
||||
|
||||
Reference in New Issue
Block a user