Skip to content

Commit e775b0e

Browse files
Error if special numeric during copy to (#97)
Do not allow copying numeric nan and +-infinity.
1 parent a123f52 commit e775b0e

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/pgrx_tests/copy_type_roundtrip.rs

+16
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,22 @@ mod tests {
987987
test_table.assert_expected_and_result_rows();
988988
}
989989

990+
#[pg_test]
991+
#[should_panic = "Special numeric values like NaN, Inf, -Inf are not allowed"]
992+
fn test_numeric_nan() {
993+
let test_table = TestTable::<AnyNumeric>::new("numeric".into());
994+
test_table.insert("INSERT INTO test_expected (a) VALUES ('NaN');");
995+
test_table.assert_expected_and_result_rows();
996+
}
997+
998+
#[pg_test]
999+
#[should_panic = "Special numeric values like NaN, Inf, -Inf are not allowed"]
1000+
fn test_numeric_inf() {
1001+
let test_table = TestTable::<AnyNumeric>::new("numeric".into());
1002+
test_table.insert("INSERT INTO test_expected (a) VALUES ('-Infinity');");
1003+
test_table.assert_expected_and_result_rows();
1004+
}
1005+
9901006
#[pg_test]
9911007
fn test_geometry() {
9921008
// Skip the test if postgis extension is not available

src/type_compat/pg_arrow_type_conversions.rs

+16
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,23 @@ pub(crate) fn i64_to_timetz(i64_timetz: i64) -> TimeWithTimeZone {
173173
.unwrap_or_else(|e| panic!("{}", e))
174174
}
175175

176+
fn error_if_special_numeric(numeric: AnyNumeric) {
177+
if ["NaN", "Infinity", "-Infinity"]
178+
.iter()
179+
.any(|&s| format!("{}", numeric) == s)
180+
{
181+
ereport!(
182+
pgrx::PgLogLevel::ERROR,
183+
pgrx::PgSqlErrorCode::ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE,
184+
"Special numeric values like NaN, Inf, -Inf are not allowed for numeric type during copy to parquet",
185+
"Use float types instead.",
186+
);
187+
}
188+
}
189+
176190
pub(crate) fn numeric_to_i128(numeric: AnyNumeric, typmod: i32, col_name: &str) -> i128 {
191+
error_if_special_numeric(numeric.clone());
192+
177193
let numeric_str = if is_unbounded_numeric_typmod(typmod) {
178194
let rescaled_unbounded_numeric = rescale_unbounded_numeric_or_error(numeric, col_name);
179195

0 commit comments

Comments
 (0)