File tree 4 files changed +90
-5
lines changed
4 files changed +90
-5
lines changed Original file line number Diff line number Diff line change @@ -3359,12 +3359,18 @@ jsonb_concat(PG_FUNCTION_ARGS)
3359
3359
* it2 ;
3360
3360
3361
3361
/*
3362
- * If one of the jsonb is empty, just return other.
3362
+ * If one of the jsonb is empty, just return the other if it's not
3363
+ * scalar and both are of the same kind. If it's a scalar or they are
3364
+ * of different kinds we need to perform the concatenation even if one is
3365
+ * empty.
3363
3366
*/
3364
- if (JB_ROOT_COUNT (jb1 ) == 0 )
3365
- PG_RETURN_JSONB (jb2 );
3366
- else if (JB_ROOT_COUNT (jb2 ) == 0 )
3367
- PG_RETURN_JSONB (jb1 );
3367
+ if (JB_ROOT_IS_OBJECT (jb1 ) == JB_ROOT_IS_OBJECT (jb2 ))
3368
+ {
3369
+ if (JB_ROOT_COUNT (jb1 ) == 0 && !JB_ROOT_IS_SCALAR (jb2 ))
3370
+ PG_RETURN_JSONB (jb2 );
3371
+ else if (JB_ROOT_COUNT (jb2 ) == 0 && !JB_ROOT_IS_SCALAR (jb1 ))
3372
+ PG_RETURN_JSONB (jb1 );
3373
+ }
3368
3374
3369
3375
it1 = JsonbIteratorInit (& jb1 -> root );
3370
3376
it2 = JsonbIteratorInit (& jb2 -> root );
Original file line number Diff line number Diff line change @@ -2912,6 +2912,42 @@ select '"c"' || '["a", "b"]'::jsonb;
2912
2912
["c", "a", "b"]
2913
2913
(1 row)
2914
2914
2915
+ select '[]'::jsonb || '["a"]'::jsonb;
2916
+ ?column?
2917
+ ----------
2918
+ ["a"]
2919
+ (1 row)
2920
+
2921
+ select '[]'::jsonb || '"a"'::jsonb;
2922
+ ?column?
2923
+ ----------
2924
+ ["a"]
2925
+ (1 row)
2926
+
2927
+ select '"b"'::jsonb || '"a"'::jsonb;
2928
+ ?column?
2929
+ ------------
2930
+ ["b", "a"]
2931
+ (1 row)
2932
+
2933
+ select '{}'::jsonb || '{"a":"b"}'::jsonb;
2934
+ ?column?
2935
+ ------------
2936
+ {"a": "b"}
2937
+ (1 row)
2938
+
2939
+ select '[]'::jsonb || '{"a":"b"}'::jsonb;
2940
+ ?column?
2941
+ --------------
2942
+ [{"a": "b"}]
2943
+ (1 row)
2944
+
2945
+ select '{"a":"b"}'::jsonb || '[]'::jsonb;
2946
+ ?column?
2947
+ --------------
2948
+ [{"a": "b"}]
2949
+ (1 row)
2950
+
2915
2951
select '"a"'::jsonb || '{"a":1}';
2916
2952
ERROR: invalid concatenation of jsonb objects
2917
2953
select '{"a":1}' || '"a"'::jsonb;
Original file line number Diff line number Diff line change @@ -2912,6 +2912,42 @@ select '"c"' || '["a", "b"]'::jsonb;
2912
2912
["c", "a", "b"]
2913
2913
(1 row)
2914
2914
2915
+ select '[]'::jsonb || '["a"]'::jsonb;
2916
+ ?column?
2917
+ ----------
2918
+ ["a"]
2919
+ (1 row)
2920
+
2921
+ select '[]'::jsonb || '"a"'::jsonb;
2922
+ ?column?
2923
+ ----------
2924
+ ["a"]
2925
+ (1 row)
2926
+
2927
+ select '"b"'::jsonb || '"a"'::jsonb;
2928
+ ?column?
2929
+ ------------
2930
+ ["b", "a"]
2931
+ (1 row)
2932
+
2933
+ select '{}'::jsonb || '{"a":"b"}'::jsonb;
2934
+ ?column?
2935
+ ------------
2936
+ {"a": "b"}
2937
+ (1 row)
2938
+
2939
+ select '[]'::jsonb || '{"a":"b"}'::jsonb;
2940
+ ?column?
2941
+ --------------
2942
+ [{"a": "b"}]
2943
+ (1 row)
2944
+
2945
+ select '{"a":"b"}'::jsonb || '[]'::jsonb;
2946
+ ?column?
2947
+ --------------
2948
+ [{"a": "b"}]
2949
+ (1 row)
2950
+
2915
2951
select '"a"'::jsonb || '{"a":1}';
2916
2952
ERROR: invalid concatenation of jsonb objects
2917
2953
select '{"a":1}' || '"a"'::jsonb;
Original file line number Diff line number Diff line change @@ -718,6 +718,13 @@ select '["c"]' || '["a", "b"]'::jsonb;
718
718
select ' ["a", "b"]' ::jsonb || ' "c"' ;
719
719
select ' "c"' || ' ["a", "b"]' ::jsonb;
720
720
721
+ select ' []' ::jsonb || ' ["a"]' ::jsonb;
722
+ select ' []' ::jsonb || ' "a"' ::jsonb;
723
+ select ' "b"' ::jsonb || ' "a"' ::jsonb;
724
+ select ' {}' ::jsonb || ' {"a":"b"}' ::jsonb;
725
+ select ' []' ::jsonb || ' {"a":"b"}' ::jsonb;
726
+ select ' {"a":"b"}' ::jsonb || ' []' ::jsonb;
727
+
721
728
select ' "a"' ::jsonb || ' {"a":1}' ;
722
729
select ' {"a":1}' || ' "a"' ::jsonb;
723
730
You can’t perform that action at this time.
0 commit comments