mirror of
				https://github.com/postgres/postgres.git
				synced 2025-10-31 00:03:57 -04:00 
			
		
		
		
	Add numeric_int8_opt_error() to optionally suppress errors
This matches the existing numeric_int4_opt_error() (see commit 16d489b0fe). It will be used by a future JSON-related patch, which wants to report errors in its own way and thus does not want the internal functions to throw any error. Author: Jeevan Chalke <jeevan.chalke@enterprisedb.com> Discussion: https://www.postgresql.org/message-id/flat/CAM2+6=XjTyqrrqHAOj80r0wVQxJSxc0iyib9bPC55uFO9VKatg@mail.gmail.com
This commit is contained in:
		
							parent
							
								
									d4e66a39eb
								
							
						
					
					
						commit
						c1b9e1e56d
					
				| @ -4430,35 +4430,62 @@ int8_numeric(PG_FUNCTION_ARGS) | |||||||
| 	PG_RETURN_NUMERIC(int64_to_numeric(val)); | 	PG_RETURN_NUMERIC(int64_to_numeric(val)); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | int64 | ||||||
|  | numeric_int8_opt_error(Numeric num, bool *have_error) | ||||||
|  | { | ||||||
|  | 	NumericVar	x; | ||||||
|  | 	int64		result; | ||||||
|  | 
 | ||||||
|  | 	if (have_error) | ||||||
|  | 		*have_error = false; | ||||||
|  | 
 | ||||||
|  | 	if (NUMERIC_IS_SPECIAL(num)) | ||||||
|  | 	{ | ||||||
|  | 		if (have_error) | ||||||
|  | 		{ | ||||||
|  | 			*have_error = true; | ||||||
|  | 			return 0; | ||||||
|  | 		} | ||||||
|  | 		else | ||||||
|  | 		{ | ||||||
|  | 			if (NUMERIC_IS_NAN(num)) | ||||||
|  | 				ereport(ERROR, | ||||||
|  | 						(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), | ||||||
|  | 						 errmsg("cannot convert NaN to %s", "bigint"))); | ||||||
|  | 			else | ||||||
|  | 				ereport(ERROR, | ||||||
|  | 						(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), | ||||||
|  | 						 errmsg("cannot convert infinity to %s", "bigint"))); | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	/* Convert to variable format, then convert to int8 */ | ||||||
|  | 	init_var_from_num(num, &x); | ||||||
|  | 
 | ||||||
|  | 	if (!numericvar_to_int64(&x, &result)) | ||||||
|  | 	{ | ||||||
|  | 		if (have_error) | ||||||
|  | 		{ | ||||||
|  | 			*have_error = true; | ||||||
|  | 			return 0; | ||||||
|  | 		} | ||||||
|  | 		else | ||||||
|  | 		{ | ||||||
|  | 			ereport(ERROR, | ||||||
|  | 					(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), | ||||||
|  | 					 errmsg("bigint out of range"))); | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	return result; | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
| Datum | Datum | ||||||
| numeric_int8(PG_FUNCTION_ARGS) | numeric_int8(PG_FUNCTION_ARGS) | ||||||
| { | { | ||||||
| 	Numeric		num = PG_GETARG_NUMERIC(0); | 	Numeric		num = PG_GETARG_NUMERIC(0); | ||||||
| 	NumericVar	x; |  | ||||||
| 	int64		result; |  | ||||||
| 
 | 
 | ||||||
| 	if (NUMERIC_IS_SPECIAL(num)) | 	PG_RETURN_INT64(numeric_int8_opt_error(num, NULL)); | ||||||
| 	{ |  | ||||||
| 		if (NUMERIC_IS_NAN(num)) |  | ||||||
| 			ereport(ERROR, |  | ||||||
| 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |  | ||||||
| 					 errmsg("cannot convert NaN to %s", "bigint"))); |  | ||||||
| 		else |  | ||||||
| 			ereport(ERROR, |  | ||||||
| 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |  | ||||||
| 					 errmsg("cannot convert infinity to %s", "bigint"))); |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	/* Convert to variable format and thence to int8 */ |  | ||||||
| 	init_var_from_num(num, &x); |  | ||||||
| 
 |  | ||||||
| 	if (!numericvar_to_int64(&x, &result)) |  | ||||||
| 		ereport(ERROR, |  | ||||||
| 				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), |  | ||||||
| 				 errmsg("bigint out of range"))); |  | ||||||
| 
 |  | ||||||
| 	PG_RETURN_INT64(result); |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -101,5 +101,6 @@ extern Numeric numeric_div_opt_error(Numeric num1, Numeric num2, | |||||||
| extern Numeric numeric_mod_opt_error(Numeric num1, Numeric num2, | extern Numeric numeric_mod_opt_error(Numeric num1, Numeric num2, | ||||||
| 									 bool *have_error); | 									 bool *have_error); | ||||||
| extern int32 numeric_int4_opt_error(Numeric num, bool *have_error); | extern int32 numeric_int4_opt_error(Numeric num, bool *have_error); | ||||||
|  | extern int64 numeric_int8_opt_error(Numeric num, bool *have_error); | ||||||
| 
 | 
 | ||||||
| #endif							/* _PG_NUMERIC_H_ */ | #endif							/* _PG_NUMERIC_H_ */ | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user