Skip to content

Commit

Permalink
Merge pull request #4534 from b41sh/func-parse-json
Browse files Browse the repository at this point in the history
support PARSE_JSON / TRY_PARSE_JSON function
  • Loading branch information
BohuTANG authored Mar 23, 2022
2 parents 203fba8 + 0dfdc7c commit d8f27da
Show file tree
Hide file tree
Showing 14 changed files with 755 additions and 10 deletions.
23 changes: 23 additions & 0 deletions common/datavalues/src/columns/series.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,29 @@ impl SeriesFrom<Vec<JsonValue>, Vec<JsonValue>> for Series {
}
}

impl SeriesFrom<Vec<Option<JsonValue>>, Vec<Option<JsonValue>>> for Series {
fn from_data(v: Vec<Option<JsonValue>>) -> ColumnRef {
type Builder = <<JsonValue as Scalar>::ColumnType as ScalarColumn>::Builder;
let mut builder = Builder::with_capacity(v.len());
let mut bitmap = MutableBitmap::with_capacity(v.len());

for item in v {
match item {
Some(v) => {
bitmap.push(true);
builder.push(&v);
}
None => {
bitmap.push(false);
builder.push(&JsonValue::default());
}
}
}
let column = builder.finish();
Arc::new(NullableColumn::new(Arc::new(column), bitmap.into()))
}
}

macro_rules! impl_from_option_iterator {
([], $( { $S: ident} ),*) => {
$(
Expand Down
6 changes: 3 additions & 3 deletions common/datavalues/src/types/serializations/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ pub struct VariantSerializer {}
impl TypeSerializer for VariantSerializer {
fn serialize_value(&self, value: &DataValue) -> Result<String> {
if let DataValue::Json(v) = value {
Ok(format!("{:#}", v))
Ok(v.to_string())
} else {
Err(ErrorCode::BadBytes("Incorrect Variant value"))
}
}

fn serialize_column(&self, column: &ColumnRef) -> Result<Vec<String>> {
let column: &JsonColumn = Series::check_get(column)?;
let result: Vec<String> = column.iter().map(|v| format!("{:#}", v)).collect();
let result: Vec<String> = column.iter().map(|v| v.to_string()).collect();
Ok(result)
}

Expand All @@ -49,7 +49,7 @@ impl TypeSerializer for VariantSerializer {
column: &ColumnRef,
) -> Result<opensrv_clickhouse::types::column::ArcColumnData> {
let column: &JsonColumn = Series::check_get(column)?;
let values: Vec<String> = column.iter().map(|v| format!("{:#}", v)).collect();
let values: Vec<String> = column.iter().map(|v| v.to_string()).collect();

Ok(Vec::column_from::<ArcColumnWrapper>(values))
}
Expand Down
11 changes: 4 additions & 7 deletions common/functions/src/scalars/expressions/cast_with_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ pub fn cast_to_variant(
column: &ColumnRef,
from_type: &DataTypePtr,
) -> Result<(ColumnRef, Option<Bitmap>)> {
if from_type.data_type_id() == TypeID::Variant {
return Ok((column.clone(), None));
}

let column = Series::remove_nullable(column);
let size = column.len();
let mut builder = ColumnBuilder::<JsonValue>::with_capacity(size);
Expand All @@ -194,13 +198,6 @@ pub fn cast_to_variant(
return Ok((builder.build(size), None));
}, {
match from_type.data_type_id() {
TypeID::Null => {
for _ in 0..size {
let v = JsonValue::Null;
builder.append(&v);
}
return Ok((builder.build(size), None));
}
TypeID::Boolean => {
let c: &BooleanColumn = Series::check_get(&column)?;
for v in c.iter() {
Expand Down
2 changes: 2 additions & 0 deletions common/functions/src/scalars/function_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use super::LogicFunction;
use super::MathsFunction;
use super::NullableFunction;
use super::OtherFunction;
use super::SemiStructuredFunction;
use super::StringFunction;
use super::ToCastFunction;
use super::TupleClassFunction;
Expand Down Expand Up @@ -175,6 +176,7 @@ static FUNCTION_FACTORY: Lazy<Arc<FunctionFactory>> = Lazy::new(|| {
TupleClassFunction::register(&mut function_factory);
ComparisonFunction::register(&mut function_factory);
UdfFunction::register(&mut function_factory);
SemiStructuredFunction::register(&mut function_factory);
StringFunction::register(&mut function_factory);
HashesFunction::register(&mut function_factory);
ConditionalFunction::register(&mut function_factory);
Expand Down
2 changes: 2 additions & 0 deletions common/functions/src/scalars/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ mod logics;
mod maths;
mod nullables;
mod others;
mod semi_structureds;
mod strings;
mod tuples;
mod udfs;
Expand All @@ -47,6 +48,7 @@ pub use logics::*;
pub use maths::*;
pub use nullables::*;
pub use others::*;
pub use semi_structureds::*;
pub use strings::*;
pub use tuples::*;
pub use udfs::*;
Expand Down
20 changes: 20 additions & 0 deletions common/functions/src/scalars/semi_structureds/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2022 Datafuse Labs.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

mod parse_json;
mod semi_structured;

pub use parse_json::ParseJsonFunction;
pub use parse_json::TryParseJsonFunction;
pub use semi_structured::SemiStructuredFunction;
Loading

1 comment on commit d8f27da

@vercel
Copy link

@vercel vercel bot commented on d8f27da Mar 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.