Skip to content

Commit

Permalink
Add SQL planner support for calling round function with two argumen…
Browse files Browse the repository at this point in the history
…ts (#2503)

* test

* specify correct signature for ROUND function
  • Loading branch information
andygrove authored May 10, 2022
1 parent 06fa7cc commit 8a29ed5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
12 changes: 12 additions & 0 deletions datafusion/core/src/sql/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4355,6 +4355,10 @@ mod tests {
Field::new("j3_id", DataType::Int32, false),
Field::new("j3_string", DataType::Utf8, false),
])),
"test_decimal" => Ok(Schema::new(vec![
Field::new("id", DataType::Int32, false),
Field::new("price", DataType::Decimal(10, 2), false),
])),
"person" => Ok(Schema::new(vec![
Field::new("id", DataType::UInt32, false),
Field::new("first_name", DataType::Utf8, false),
Expand Down Expand Up @@ -4700,6 +4704,14 @@ mod tests {
quick_test(sql, expected);
}

#[tokio::test]
async fn round_decimal() {
let sql = "SELECT round(price/3, 2) FROM test_decimal";
let expected = "Projection: round(#test_decimal.price / Int64(3), Int64(2))\
\n TableScan: test_decimal projection=None";
quick_test(sql, expected);
}

#[ignore] // see https://github.com/apache/arrow-datafusion/issues/2469
#[tokio::test]
async fn aggregate_with_grouping_sets() {
Expand Down
9 changes: 9 additions & 0 deletions datafusion/expr/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,15 @@ pub fn signature(fun: &BuiltinScalarFunction) -> Signature {
],
fun.volatility(),
),
BuiltinScalarFunction::Round => Signature::one_of(
vec![
TypeSignature::Exact(vec![DataType::Float64, DataType::Int64]),
TypeSignature::Exact(vec![DataType::Float32, DataType::Int64]),
TypeSignature::Exact(vec![DataType::Float64]),
TypeSignature::Exact(vec![DataType::Float32]),
],
fun.volatility(),
),
// math expressions expect 1 argument of type f64 or f32
// priority is given to f64 because e.g. `sqrt(1i32)` is in IR (real numbers) and thus we
// return the best approximation for it (in f64).
Expand Down

0 comments on commit 8a29ed5

Please sign in to comment.