Skip to content

Commit

Permalink
fix: sql round without precision (#3863)
Browse files Browse the repository at this point in the history
  • Loading branch information
universalmind303 authored Feb 26, 2025
1 parent af2a841 commit c3591a2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/daft-sql/src/modules/numeric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,11 @@ fn to_expr(expr: &SQLNumericExpr, args: &[ExprRef]) -> SQLPlannerResult<ExprRef>
Ok(sign(args[0].clone()))
}
SQLNumericExpr::Round => {
ensure!(args.len() == 2, "round takes exactly two arguments");
let precision = match args[1].as_ref().as_literal() {
ensure!(
args.len() == 2 || args.len() == 1,
"round takes one or two arguments"
);
let precision = match args.get(1).and_then(|arg| arg.as_literal()) {
Some(LiteralValue::Int8(i)) => *i as i32,
Some(LiteralValue::UInt8(u)) => *u as i32,
Some(LiteralValue::Int16(i)) => *i as i32,
Expand All @@ -185,6 +188,7 @@ fn to_expr(expr: &SQLNumericExpr, args: &[ExprRef]) -> SQLPlannerResult<ExprRef>
Some(LiteralValue::UInt32(u)) => *u as i32,
Some(LiteralValue::Int64(i)) => *i as i32,
Some(LiteralValue::UInt64(u)) => *u as i32,
None => 0,
_ => invalid_operation_err!("round precision must be an integer"),
};
Ok(round(args[0].clone(), Some(precision)))
Expand Down
20 changes: 20 additions & 0 deletions tests/sql/test_exprs.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,23 @@ def test_coalesce():
).to_pydict()

assert actual == expected


@pytest.mark.parametrize(
"precision, value, expected",
[
(None, 3.14159, 3),
(None, 3, 3),
(1, 3.14159, 3.1),
(2, 3.14159, 3.14),
],
)
def test_round(precision, value, expected):
if precision is None:
query = f"select round({value})"
else:
query = f"select round({value}, {precision})"
actual = daft.sql(query).to_pydict()
expected = {"literal": [expected]}

assert actual == expected

0 comments on commit c3591a2

Please sign in to comment.