Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support default precision and scale toCAST <EXPR> AS DECIMAL #2680

Merged
merged 4 commits into from
Jun 14, 2022
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
28 changes: 28 additions & 0 deletions datafusion/sql/src/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2678,6 +2678,34 @@ mod tests {
);
}

#[test]
fn test_int_decimal_default() {
quick_test(
"SELECT CAST(10 AS DECIMAL)",
gandronchik marked this conversation as resolved.
Show resolved Hide resolved
"Projection: CAST(Int64(10) AS Decimal(38, 10))\
\n EmptyRelation",
);
}

#[test]
fn test_int_decimal_no_scale() {
quick_test(
"SELECT CAST(10 AS DECIMAL(5))",
"Projection: CAST(Int64(10) AS Decimal(5, 0))\
\n EmptyRelation",
);
}

#[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)
);
}

#[test]
fn select_column_does_not_exist() {
let sql = "SELECT doesnotexist FROM person";
Expand Down
36 changes: 20 additions & 16 deletions datafusion/sql/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

//! SQL Utility Functions

use arrow::datatypes::{DataType, DECIMAL_MAX_PRECISION};
use arrow::datatypes::{DataType, DECIMAL_DEFAULT_SCALE, DECIMAL_MAX_PRECISION};
use sqlparser::ast::Ident;

use datafusion_common::{DataFusionError, Result, ScalarValue};
Expand Down Expand Up @@ -447,22 +447,26 @@ pub(crate) fn make_decimal_type(
precision: Option<u64>,
scale: Option<u64>,
) -> Result<DataType> {
match (precision, scale) {
(None, _) | (_, None) => Err(DataFusionError::Internal(format!(
"Decimal(precision, scale) must both be specified, got ({:?}, {:?})",
precision, scale
))),
(Some(p), Some(s)) => {
// Arrow decimal is i128 meaning 38 maximum decimal digits
if (p as usize) > DECIMAL_MAX_PRECISION || s > p {
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 ({}, {})",
p, s
)))
} else {
Ok(DataType::Decimal(p as usize, s as usize))
}
// postgres like behavior
let (precision, scale) = match (precision, scale) {
(Some(p), Some(s)) => (p as usize, s as usize),
(Some(p), None) => (p as usize, 0),
(None, Some(_)) => {
return Err(DataFusionError::Internal(
"Cannot specify only scale for decimal data type".to_string(),
))
}
(None, None) => (DECIMAL_MAX_PRECISION, DECIMAL_DEFAULT_SCALE),
};

// Arrow decimal is i128 meaning 38 maximum decimal digits
if precision > DECIMAL_MAX_PRECISION || scale > precision {
return 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 ({}, {})",
precision, scale
)));
} else {
Ok(DataType::Decimal(precision, scale))
}
}

Expand Down