Skip to content

Commit

Permalink
feedback amendment
Browse files Browse the repository at this point in the history
  • Loading branch information
taytzehao committed Sep 25, 2023
1 parent 302b6f7 commit 053b9b2
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 15 deletions.
3 changes: 2 additions & 1 deletion proto/expr.proto
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ message ExprNode {
HEX_TO_INT256 = 560;

// Jsonb functions
JSONB_CAT = 575;


// jsonb -> int, jsonb -> text, jsonb #> text[] that returns jsonb
JSONB_ACCESS_INNER = 600;
Expand All @@ -215,6 +215,7 @@ message ExprNode {
JSONB_TYPEOF = 602;
JSONB_ARRAY_LENGTH = 603;
IS_JSON = 604;
JSONB_CAT = 605;

// Non-pure functions below (> 1000)
// ------------------------
Expand Down
5 changes: 0 additions & 5 deletions src/common/src/types/jsonb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,6 @@ impl From<Value> for JsonbVal {
}
}

impl<'a> From<&'a Value> for JsonbRef<'a> {
fn from(value: &'a Value) -> Self {
JsonbRef(value)
}
}

impl<'a> JsonbRef<'a> {
pub fn memcmp_serialize(
Expand Down
28 changes: 24 additions & 4 deletions src/expr/src/vector_op/jsonb_concat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,21 @@ use serde_json::{json, Value};
/// SELECT '1'::jsonb || '2'::jsonb;
/// ----
/// [1, 2]
///
/// query T
/// SELECT '[1,2]'::jsonb || NULL::jsonb;
/// ----
/// NULL
///
/// query T
/// SELECT NULL::jsonb || '[1,2]'::jsonb;
/// ----
/// NULL
/// ```
#[function("jsonb_cat(jsonb, jsonb) -> jsonb")]
pub fn jsonb_cat(left: JsonbRef<'_>, right: JsonbRef<'_>) -> JsonbVal {
let left_val = left.value().clone();
let right_val = right.value().clone();
pub fn jsonb_cat(left: Option<JsonbRef<'_>>, right: Option<JsonbRef<'_>>) -> 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());
match (left_val, right_val) {
// left and right are object based.
// This would have left:{'a':1}, right:{'b':2} -> {'a':1,'b':2}
Expand All @@ -65,11 +75,21 @@ pub fn jsonb_cat(left: JsonbRef<'_>, right: JsonbRef<'_>) -> JsonbVal {
// 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)) => {
(Value::Array(mut left_arr), single_val)
| (single_val, Value::Array(mut left_arr))
if single_val != Value::Null => {
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)
}

// Both are non-array inputs.
// Both elements would be placed together in an array
// Eg left:1 right: 2 -> [1,2]
Expand Down
8 changes: 4 additions & 4 deletions src/frontend/src/binder/expr/binary_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ impl Binder {
ExprType::ConcatOp
}

(Some(DataType::Jsonb), Some(DataType::Jsonb)) => ExprType::JsonbCat,
(Some(DataType::Jsonb), Some(DataType::Jsonb))
| (Some(DataType::Jsonb), None)
| (None, Some(DataType::Jsonb))=> ExprType::JsonbCat,

// bytea (and varbit, tsvector, tsquery)
(Some(t @ DataType::Jsonb), None)
| (None, Some(t @ DataType::Jsonb))
| (Some(t @ DataType::Bytea), Some(DataType::Bytea))
(Some(t @ DataType::Bytea), Some(DataType::Bytea))
| (Some(t @ DataType::Bytea), None)
| (None, Some(t @ DataType::Bytea)) => {
return Err(ErrorCode::BindError(format!(
Expand Down
1 change: 0 additions & 1 deletion src/frontend/src/binder/expr/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,6 @@ impl Binder {
// int256
("hex_to_int256", raw_call(ExprType::HexToInt256)),
// jsonb
("jsonb_cat", raw_call(ExprType::JsonbCat)),
("jsonb_object_field", raw_call(ExprType::JsonbAccessInner)),
("jsonb_array_element", raw_call(ExprType::JsonbAccessInner)),
("jsonb_object_field_text", raw_call(ExprType::JsonbAccessStr)),
Expand Down

0 comments on commit 053b9b2

Please sign in to comment.