Skip to content

Commit

Permalink
Add conformance tests, support integer quantiles (0 and 1)
Browse files Browse the repository at this point in the history
  • Loading branch information
pomo-mondreganto committed Sep 9, 2024
1 parent c2e1e9e commit a2cceac
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
14 changes: 14 additions & 0 deletions crates/polars-sql/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1273,6 +1273,13 @@ impl SQLFunctionVisitor<'_> {
polars_bail!(SQLSyntax: "QUANTILE_DISC value must be between 0 and 1 ({})", args[1])
}
},
Expr::Literal(LiteralValue::Int(n)) => {
if (0..=1).contains(&n) {
Expr::from(n as f64)
} else {
polars_bail!(SQLSyntax: "QUANTILE_DISC value must be between 0 and 1 ({})", args[1])
}
},
_ => polars_bail!(SQLSyntax: "invalid value for QUANTILE_DISC ({})", args[1])
};
Ok(e.quantile(value, QuantileInterpolOptions::Lower))
Expand All @@ -1292,6 +1299,13 @@ impl SQLFunctionVisitor<'_> {
polars_bail!(SQLSyntax: "QUANTILE_CONT value must be between 0 and 1 ({})", args[1])
}
},
Expr::Literal(LiteralValue::Int(n)) => {
if (0..=1).contains(&n) {
Expr::from(n as f64)
} else {
polars_bail!(SQLSyntax: "QUANTILE_CONT value must be between 0 and 1 ({})", args[1])
}
},
_ => polars_bail!(SQLSyntax: "invalid value for QUANTILE_CONT ({})", args[1])
};
Ok(e.quantile(value, QuantileInterpolOptions::Linear))
Expand Down
38 changes: 36 additions & 2 deletions crates/polars-sql/tests/functions_aggregate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,38 @@ fn test_quantile_disc() {
let sql_expr = format!("QUANTILE_DISC(Sales, {})", q);
let (expected, actual) = create_expected(expr, &sql_expr);

assert!(expected.equals(&actual))
assert!(
expected.equals(&actual),
"q: {q}: expected {expected:?}, got {actual:?}"
)
}
}

#[test]
fn test_quantile_disc_conformance() {
for (q, expected) in [
(0., 1000),
(0.1, 1000),
(0.2, 2000),
(0.3, 2000),
(0.4, 3000),
(0.5, 3000),
(0.6, 4000),
(0.7, 4000),
(0.8, 5000),
(0.9, 5000),
(1., 6000),
] {
let expr = lit(expected);

let sql_expr = format!("QUANTILE_DISC(Sales, {q})");

let (expected, actual) = create_expected(expr, &sql_expr);

assert!(
expected.equals(&actual),
"q: {q}: expected {expected:?}, got {actual:?}"
);
}
}

Expand All @@ -69,6 +100,9 @@ fn test_quantile_cont() {
let sql_expr = format!("QUANTILE_CONT(Sales, {})", q);
let (expected, actual) = create_expected(expr, &sql_expr);

assert!(expected.equals(&actual))
assert!(
expected.equals(&actual),
"q: {q}: expected {expected:?}, got {actual:?}"
)
}
}

0 comments on commit a2cceac

Please sign in to comment.