mirror of
				https://github.com/postgres/postgres.git
				synced 2025-11-03 09:13:20 +03:00 
			
		
		
		
	Make edge-case behavior of jsonb_populate_record match json_populate_record
json_populate_record throws an error if asked to convert a JSON scalar or array into a composite type. jsonb_populate_record was returning a record full of NULL fields instead. It seems better to make it throw an error for this case as well. Nikita Glukhov Discussion: https://postgr.es/m/fbd1d566-bba0-a3de-d6d0-d3b1d7c24ff2@postgrespro.ru
This commit is contained in:
		@@ -2682,9 +2682,24 @@ JsValueToJsObject(JsValue *jsv, JsObject *jso)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
		if (jbv->type == jbvBinary &&
 | 
							if (jbv->type == jbvBinary &&
 | 
				
			||||||
			JsonContainerIsObject(jbv->val.binary.data))
 | 
								JsonContainerIsObject(jbv->val.binary.data))
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
			jso->val.jsonb_cont = jbv->val.binary.data;
 | 
								jso->val.jsonb_cont = jbv->val.binary.data;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
		else
 | 
							else
 | 
				
			||||||
			jso->val.jsonb_cont = NULL;
 | 
							{
 | 
				
			||||||
 | 
								bool		is_scalar;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								is_scalar = IsAJsonbScalar(jbv) ||
 | 
				
			||||||
 | 
									(jbv->type == jbvBinary &&
 | 
				
			||||||
 | 
									 JsonContainerIsScalar(jbv->val.binary.data));
 | 
				
			||||||
 | 
								ereport(ERROR,
 | 
				
			||||||
 | 
										(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 | 
				
			||||||
 | 
										 is_scalar
 | 
				
			||||||
 | 
										 ? errmsg("cannot call %s on a scalar",
 | 
				
			||||||
 | 
												  "populate_composite")
 | 
				
			||||||
 | 
										 : errmsg("cannot call %s on an array",
 | 
				
			||||||
 | 
												  "populate_composite")));
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2276,17 +2276,9 @@ SELECT jsa FROM jsonb_populate_record(NULL::jsbrec, '{"jsa": ["aaa", null, [1, 2
 | 
				
			|||||||
(1 row)
 | 
					(1 row)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
SELECT rec FROM jsonb_populate_record(NULL::jsbrec, '{"rec": 123}') q;
 | 
					SELECT rec FROM jsonb_populate_record(NULL::jsbrec, '{"rec": 123}') q;
 | 
				
			||||||
 rec  
 | 
					ERROR:  cannot call populate_composite on a scalar
 | 
				
			||||||
------
 | 
					 | 
				
			||||||
 (,,)
 | 
					 | 
				
			||||||
(1 row)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
SELECT rec FROM jsonb_populate_record(NULL::jsbrec, '{"rec": [1, 2]}') q;
 | 
					SELECT rec FROM jsonb_populate_record(NULL::jsbrec, '{"rec": [1, 2]}') q;
 | 
				
			||||||
 rec  
 | 
					ERROR:  cannot call populate_composite on an array
 | 
				
			||||||
------
 | 
					 | 
				
			||||||
 (,,)
 | 
					 | 
				
			||||||
(1 row)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
SELECT rec FROM jsonb_populate_record(NULL::jsbrec, '{"rec": {"a": "abc", "c": "01.02.2003", "x": 43.2}}') q;
 | 
					SELECT rec FROM jsonb_populate_record(NULL::jsbrec, '{"rec": {"a": "abc", "c": "01.02.2003", "x": 43.2}}') q;
 | 
				
			||||||
                rec                
 | 
					                rec                
 | 
				
			||||||
-----------------------------------
 | 
					-----------------------------------
 | 
				
			||||||
@@ -2303,11 +2295,7 @@ SELECT reca FROM jsonb_populate_record(NULL::jsbrec, '{"reca": 123}') q;
 | 
				
			|||||||
ERROR:  expected json array
 | 
					ERROR:  expected json array
 | 
				
			||||||
HINT:  see the value of key "reca"
 | 
					HINT:  see the value of key "reca"
 | 
				
			||||||
SELECT reca FROM jsonb_populate_record(NULL::jsbrec, '{"reca": [1, 2]}') q;
 | 
					SELECT reca FROM jsonb_populate_record(NULL::jsbrec, '{"reca": [1, 2]}') q;
 | 
				
			||||||
      reca       
 | 
					ERROR:  cannot call populate_composite on a scalar
 | 
				
			||||||
-----------------
 | 
					 | 
				
			||||||
 {"(,,)","(,,)"}
 | 
					 | 
				
			||||||
(1 row)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
SELECT reca FROM jsonb_populate_record(NULL::jsbrec, '{"reca": [{"a": "abc", "b": 456}, null, {"c": "01.02.2003", "x": 43.2}]}') q;
 | 
					SELECT reca FROM jsonb_populate_record(NULL::jsbrec, '{"reca": [{"a": "abc", "b": 456}, null, {"c": "01.02.2003", "x": 43.2}]}') q;
 | 
				
			||||||
                          reca                          
 | 
					                          reca                          
 | 
				
			||||||
--------------------------------------------------------
 | 
					--------------------------------------------------------
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user