Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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