Skip to content

Commit

Permalink
Add precision != 0 check when making decimal type (#3818)
Browse files Browse the repository at this point in the history
* add precision check for zero

Signed-off-by: remzi <[email protected]>

* update error msg

Signed-off-by: remzi <[email protected]>

Signed-off-by: remzi <[email protected]>
  • Loading branch information
HaoYang670 authored Oct 13, 2022
1 parent c989b8e commit 051f243
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
35 changes: 28 additions & 7 deletions datafusion/sql/src/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2862,13 +2862,34 @@ mod tests {
}

#[test]
fn test_int_decimal_scale_larger_precision() {
let sql = "SELECT CAST(10 AS DECIMAL(5, 10))";
let err = logical_plan(sql).expect_err("query should have failed");
assert_eq!(
r##"Internal("For decimal(precision, scale) precision must be less than or equal to 38 and scale can't be greater than precision. Got (5, 10)")"##,
format!("{:?}", err)
);
fn cast_to_invalid_decimal_type() {
// precision == 0
{
let sql = "SELECT CAST(10 AS DECIMAL(0))";
let err = logical_plan(sql).expect_err("query should have failed");
assert_eq!(
r##"Internal("Decimal(precision = 0, scale = 0) should satisty `0 < precision <= 38`, and `scale <= precision`.")"##,
format!("{:?}", err)
);
}
// precision > 38
{
let sql = "SELECT CAST(10 AS DECIMAL(39))";
let err = logical_plan(sql).expect_err("query should have failed");
assert_eq!(
r##"Internal("Decimal(precision = 39, scale = 0) should satisty `0 < precision <= 38`, and `scale <= precision`.")"##,
format!("{:?}", err)
);
}
// precision < scale
{
let sql = "SELECT CAST(10 AS DECIMAL(5, 10))";
let err = logical_plan(sql).expect_err("query should have failed");
assert_eq!(
r##"Internal("Decimal(precision = 5, scale = 10) should satisty `0 < precision <= 38`, and `scale <= precision`.")"##,
format!("{:?}", err)
);
}
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions datafusion/sql/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,9 +508,9 @@ pub(crate) fn make_decimal_type(
};

// Arrow decimal is i128 meaning 38 maximum decimal digits
if precision > DECIMAL128_MAX_PRECISION || scale > precision {
if precision == 0 || precision > DECIMAL128_MAX_PRECISION || scale > precision {
Err(DataFusionError::Internal(format!(
"For decimal(precision, scale) precision must be less than or equal to 38 and scale can't be greater than precision. Got ({}, {})",
"Decimal(precision = {}, scale = {}) should satisty `0 < precision <= 38`, and `scale <= precision`.",
precision, scale
)))
} else {
Expand Down

0 comments on commit 051f243

Please sign in to comment.