correctly handle edge cases

This commit is contained in:
Sachin Singh 2025-04-11 08:34:29 +05:30
parent 5ffdd42f12
commit 01fa02364d
3 changed files with 14 additions and 16 deletions

View file

@ -1602,29 +1602,34 @@ pub fn translate_expr(
let args = if let Some(args) = args {
if args.len() != 2 {
crate::bail_parse_error!(
"likelihood function must have exactly 2 arguments",
"likelihood() function must have exactly 2 arguments",
);
}
args
} else {
crate::bail_parse_error!("likelihood function with no arguments",);
crate::bail_parse_error!("likelihood() function with no arguments",);
};
if let ast::Expr::Literal(ast::Literal::Numeric(ref value)) = args[1] {
if let Ok(probability) = value.parse::<f64>() {
if probability < 0.0 || probability > 1.0 {
if !(0.0..=1.0).contains(&probability) {
crate::bail_parse_error!(
"likelihood second argument must be between 0.0 and 1.0",
"second argument of likelihood() must be between 0.0 and 1.0",
);
}
if !value.contains('.') {
crate::bail_parse_error!(
"second argument of likelihood() must be a floating point number with decimal point",
);
}
} else {
crate::bail_parse_error!(
"likelihood second argument must be a floating point constant",
"second argument of likelihood() must be a floating point constant",
);
}
} else {
crate::bail_parse_error!(
"likelihood second argument must be a numeric literal",
"second argument of likelihood() must be a numeric literal",
);
}

View file

@ -7188,7 +7188,7 @@ mod tests {
assert_eq!(exec_likelihood(&value, &prob), value);
let value = OwnedValue::Integer(100);
let prob = OwnedValue::Float(0.0625);
let prob = OwnedValue::Float(1.0);
assert_eq!(exec_likelihood(&value, &prob), value);
let value = OwnedValue::Float(12.34);
@ -7203,9 +7203,6 @@ mod tests {
let prob = OwnedValue::Float(0.5);
assert_eq!(exec_likelihood(&value, &prob), value);
let prob = OwnedValue::Integer(1);
assert_eq!(exec_likelihood(&value, &prob), value);
let prob = OwnedValue::build_text("0.5");
assert_eq!(exec_likelihood(&value, &prob), value);

View file

@ -224,7 +224,7 @@ do_execsql_test likelihood-integer {
} {100}
do_execsql_test likelihood-integer-probability-1 {
SELECT likelihood(42, 1);
SELECT likelihood(42, 1.0);
} {42}
do_execsql_test likelihood-decimal {
@ -240,11 +240,7 @@ do_execsql_test likelihood-blob {
} {01020304}
do_execsql_test likelihood-zero-probability {
SELECT likelihood(999, 0);
} {999}
do_execsql_test likelihood-extreme-probability {
SELECT likelihood(999, 1);
SELECT likelihood(999, 0.0);
} {999}
do_execsql_test unhex-str-ab {