diff --git a/src/expr/src/vector_op/jsonb_concat.rs b/src/expr/src/vector_op/jsonb_concat.rs index 1d910aa2b28a0..80e7bf7b5e5bf 100644 --- a/src/expr/src/vector_op/jsonb_concat.rs +++ b/src/expr/src/vector_op/jsonb_concat.rs @@ -43,19 +43,24 @@ use serde_json::{json, Value}; /// [1, 2] /// /// query T -/// SELECT '[1,2]'::jsonb || NULL::jsonb; +/// SELECT '[1,2]'::jsonb || 'null'::jsonb; /// ---- -/// null +/// [1, 2, null] /// /// query T -/// SELECT NULL::jsonb || '[1,2]'::jsonb; +/// SELECT 'null'::jsonb || '[1,2]'::jsonb; /// ---- -/// null +/// [null, 1, 2] +/// +/// query T +/// SELECT 'null'::jsonb || '1'::jsonb; +/// ---- +/// [null, 1] /// ``` #[function("jsonb_cat(jsonb, jsonb) -> jsonb")] -pub fn jsonb_cat(left: Option>, right: Option>) -> JsonbVal { - let left_val = left.map_or(Value::Null, |v| v.value().clone()); - let right_val = right.map_or(Value::Null, |v| v.value().clone()); +pub fn jsonb_cat(left: JsonbRef<'_>, right: JsonbRef<'_>) -> JsonbVal { + let left_val = left.value().clone(); + let right_val = right.value().clone(); match (left_val, right_val) { // left and right are object based. // This would have left:{'a':1}, right:{'b':2} -> {'a':1,'b':2} @@ -75,17 +80,18 @@ pub fn jsonb_cat(left: Option>, right: Option>) -> Jso // One operand is an array, and the other is a single element. // This would insert the non-array value as another element into the array // Eg left:[1,2] right: {'a':1} -> [1,2,{'a':1}] - (Value::Array(mut left_arr), single_val) | (single_val, Value::Array(mut left_arr)) - if single_val != Value::Null => - { + (Value::Array(mut left_arr), single_val) => { left_arr.push(single_val); JsonbVal::from(Value::Array(left_arr)) } - // Either left/right is None - // This return a None value - // This would have left:[1,2], right:None -> None - (Value::Null, _) | (_, Value::Null) => JsonbVal::from(Value::Null), + // One operand is an array, and the other is a single element. + // This would insert the non-array value as another element into the array + // Eg left:{'a':1} right:[1,2] -> [{'a':1},1,2] + (single_val, Value::Array(mut right_arr)) => { + right_arr.insert(0,single_val); + JsonbVal::from(Value::Array(right_arr)) + } // Both are non-array inputs. // Both elements would be placed together in an array