Skip to content

Commit

Permalink
Error if special numeric during copy to (#97)
Browse files Browse the repository at this point in the history
Do not allow copying numeric nan and +-infinity.
  • Loading branch information
aykut-bozkurt authored Jan 24, 2025
1 parent a123f52 commit e775b0e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/pgrx_tests/copy_type_roundtrip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,22 @@ mod tests {
test_table.assert_expected_and_result_rows();
}

#[pg_test]
#[should_panic = "Special numeric values like NaN, Inf, -Inf are not allowed"]
fn test_numeric_nan() {
let test_table = TestTable::<AnyNumeric>::new("numeric".into());
test_table.insert("INSERT INTO test_expected (a) VALUES ('NaN');");
test_table.assert_expected_and_result_rows();
}

#[pg_test]
#[should_panic = "Special numeric values like NaN, Inf, -Inf are not allowed"]
fn test_numeric_inf() {
let test_table = TestTable::<AnyNumeric>::new("numeric".into());
test_table.insert("INSERT INTO test_expected (a) VALUES ('-Infinity');");
test_table.assert_expected_and_result_rows();
}

#[pg_test]
fn test_geometry() {
// Skip the test if postgis extension is not available
Expand Down
16 changes: 16 additions & 0 deletions src/type_compat/pg_arrow_type_conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,23 @@ pub(crate) fn i64_to_timetz(i64_timetz: i64) -> TimeWithTimeZone {
.unwrap_or_else(|e| panic!("{}", e))
}

fn error_if_special_numeric(numeric: AnyNumeric) {
if ["NaN", "Infinity", "-Infinity"]
.iter()
.any(|&s| format!("{}", numeric) == s)
{
ereport!(
pgrx::PgLogLevel::ERROR,
pgrx::PgSqlErrorCode::ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE,
"Special numeric values like NaN, Inf, -Inf are not allowed for numeric type during copy to parquet",
"Use float types instead.",
);
}
}

pub(crate) fn numeric_to_i128(numeric: AnyNumeric, typmod: i32, col_name: &str) -> i128 {
error_if_special_numeric(numeric.clone());

let numeric_str = if is_unbounded_numeric_typmod(typmod) {
let rescaled_unbounded_numeric = rescale_unbounded_numeric_or_error(numeric, col_name);

Expand Down

0 comments on commit e775b0e

Please sign in to comment.