From d069c29c0bc00a4089096c95ebc1e6212ab8a300 Mon Sep 17 00:00:00 2001 From: andylokandy Date: Fri, 25 Oct 2024 23:08:01 +0800 Subject: [PATCH 1/3] refactor: refine cast variant to map --- src/query/expression/src/evaluator.rs | 43 +++++++++------------------ 1 file changed, 14 insertions(+), 29 deletions(-) diff --git a/src/query/expression/src/evaluator.rs b/src/query/expression/src/evaluator.rs index 80ec365430af..8b2d17a6d69a 100644 --- a/src/query/expression/src/evaluator.rs +++ b/src/query/expression/src/evaluator.rs @@ -43,6 +43,7 @@ use crate::types::BooleanType; use crate::types::DataType; use crate::types::NullableType; use crate::types::NumberScalar; +use crate::types::StringType; use crate::types::VariantType; use crate::values::Column; use crate::values::ColumnBuilder; @@ -640,12 +641,12 @@ impl<'a> Evaluator<'a> { ])))) } Value::Column(Column::Variant(col)) => { - let mut key_builder = StringColumnBuilder::with_capacity(0, 0); + let mut key_builder = StringType::create_builder(col.len(), &[]); let mut value_builder = ArrayType::::create_builder(col.len(), &[]); - for (idx, x) in col.iter().enumerate() { - let obj = if validity.as_ref().map(|v| v.get_bit(idx)).unwrap_or(true) { + for x in col.iter() { + let obj = if validity.as_ref().map(|v| v.get_bit(0)).unwrap_or(true) { temp_obj = jsonb::from_slice(x).map_err(|e| { ErrorCode::BadArguments(format!( "Expect to be valid json, got err: {e:?}" @@ -662,38 +663,22 @@ impl<'a> Evaluator<'a> { v.write_to_vec(&mut value_builder.builder.data); value_builder.builder.commit_row(); } + value_builder.commit_row(); } - let key_column = Column::String(key_builder.build()); - let value_column = value_builder.build(); - let validity = validity.map(|validity| { - let mut inner_validity = MutableBitmap::with_capacity(col.len()); - for (index, offsets) in value_column.offsets.windows(2).enumerate() { - inner_validity.extend_constant( - (offsets[1] - offsets[0]) as usize, - validity.get_bit(index), - ); - } - inner_validity.into() - }); + let offsets = value_builder.offsets.into(); + let key_col = key_builder.build(); + let value_col = value_builder.builder.build(); - let new_value_column = self - .run_cast( - span, - &DataType::Variant, - &fields_dest_ty[1], - Value::Column(Column::Variant(value_column.values)), - validity, - options, - )? - .into_column() - .unwrap(); + let kv_col = Column::Tuple(vec![ + Column::String(key_col), + Column::Variant(value_col), + ]); - let kv_column = Column::Tuple(vec![key_column, new_value_column]); Ok(Value::Column(Column::Map(Box::new(ArrayColumn { - values: kv_column, - offsets: col.offsets, + values: kv_col, + offsets, })))) } other => unreachable!("source: {}", other), From f0ec49bfa1811bc74bf83f6097131d150c61ad0e Mon Sep 17 00:00:00 2001 From: andylokandy Date: Fri, 25 Oct 2024 23:15:11 +0800 Subject: [PATCH 2/3] fix --- src/query/expression/src/evaluator.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/query/expression/src/evaluator.rs b/src/query/expression/src/evaluator.rs index 8b2d17a6d69a..10e7bc81ddd8 100644 --- a/src/query/expression/src/evaluator.rs +++ b/src/query/expression/src/evaluator.rs @@ -645,8 +645,8 @@ impl<'a> Evaluator<'a> { let mut value_builder = ArrayType::::create_builder(col.len(), &[]); - for x in col.iter() { - let obj = if validity.as_ref().map(|v| v.get_bit(0)).unwrap_or(true) { + for (idx, x) in col.iter().enumerate() { + let obj = if validity.as_ref().map(|v| v.get_bit(idx)).unwrap_or(true) { temp_obj = jsonb::from_slice(x).map_err(|e| { ErrorCode::BadArguments(format!( "Expect to be valid json, got err: {e:?}" From 7d1b931e21379942dbab4caf95cf02a1396a2997 Mon Sep 17 00:00:00 2001 From: andylokandy Date: Sun, 27 Oct 2024 16:45:53 +0800 Subject: [PATCH 3/3] address comment --- src/query/expression/src/evaluator.rs | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/query/expression/src/evaluator.rs b/src/query/expression/src/evaluator.rs index 10e7bc81ddd8..ae815428cec1 100644 --- a/src/query/expression/src/evaluator.rs +++ b/src/query/expression/src/evaluator.rs @@ -667,18 +667,28 @@ impl<'a> Evaluator<'a> { value_builder.commit_row(); } - let offsets = value_builder.offsets.into(); - let key_col = key_builder.build(); - let value_col = value_builder.builder.build(); + let key_col = Column::String(key_builder.build()); + let value_col = Column::Array(Box::new(value_builder.build().upcast())); - let kv_col = Column::Tuple(vec![ - Column::String(key_col), - Column::Variant(value_col), - ]); + let value_col = self + .run_cast( + span, + &DataType::Array(Box::new(DataType::Variant)), + &fields_dest_ty[1], + Value::Column(value_col), + validity, + options, + )? + .into_column() + .unwrap() + .into_array() + .unwrap(); + + let kv_col = Column::Tuple(vec![key_col, value_col.values]); Ok(Value::Column(Column::Map(Box::new(ArrayColumn { values: kv_col, - offsets, + offsets: value_col.offsets, })))) } other => unreachable!("source: {}", other),