Skip to main content

akar_function/scalar/
comparison.rs

1use crate::registry::*;
2use akar_common::types::Value;
3
4// ==================== Comparison ====================
5
6pub(crate) fn evaluate_comparison(op: ComparisonOp, args: &[Value]) -> Result<Value, String> {
7    if args.len() < 2 && !matches!(op, ComparisonOp::IsNull | ComparisonOp::IsNotNull) {
8        return Err("Comparison requires 2 arguments".into());
9    }
10
11    match op {
12        ComparisonOp::Eq => Ok(Value::Bool(values_equal(&args[0], &args[1]))),
13        ComparisonOp::NotEq => Ok(Value::Bool(!values_equal(&args[0], &args[1]))),
14        ComparisonOp::Lt => Ok(Value::Bool(compare_values(&args[0], &args[1])?.is_lt())),
15        ComparisonOp::Lte => Ok(Value::Bool(!compare_values(&args[0], &args[1])?.is_gt())),
16        ComparisonOp::Gt => Ok(Value::Bool(compare_values(&args[0], &args[1])?.is_gt())),
17        ComparisonOp::Gte => Ok(Value::Bool(!compare_values(&args[0], &args[1])?.is_lt())),
18        ComparisonOp::IsNull => Ok(Value::Bool(matches!(args[0], Value::Null))),
19        ComparisonOp::IsNotNull => Ok(Value::Bool(!matches!(args[0], Value::Null))),
20    }
21}
22
23/// Exact cross-type numeric equality.
24///
25/// `Value::UInt64` and `Value::Int64` derive-distinct `PartialEq` instances
26/// (e.g. `UInt64(5) == Int64(5)` is `false`), so `WHERE uint64_col = 5` would
27/// silently return zero rows. Mixed integer operands are compared via `i128`
28/// promotion instead. Floats follow the NaN convention: NaN = NaN is true.
29fn values_equal(a: &Value, b: &Value) -> bool {
30    if a == b {
31        return true;
32    }
33    if let (Value::Double(x), Value::Double(y)) = (a, b) {
34        return x.is_nan() && y.is_nan();
35    }
36    if let (Value::Float(x), Value::Float(y)) = (a, b) {
37        return x.is_nan() && y.is_nan();
38    }
39    if let (Some(x), Some(y)) = (integer_to_i128(a), integer_to_i128(b)) {
40        return x == y;
41    }
42    false
43}
44
45/// Total-order comparison for floats with a NaN convention: NaN sorts greater
46/// than every finite value, and NaN == NaN.
47#[inline]
48pub(crate) fn double_cmp(a: f64, b: f64) -> std::cmp::Ordering {
49    if a.is_nan() {
50        if b.is_nan() {
51            std::cmp::Ordering::Equal
52        } else {
53            std::cmp::Ordering::Greater
54        }
55    } else if b.is_nan() {
56        std::cmp::Ordering::Less
57    } else {
58        a.partial_cmp(&b).unwrap_or(std::cmp::Ordering::Equal)
59    }
60}
61
62/// Widened representation of any integer `Value` variant (exact, no overflow).
63fn integer_to_i128(v: &Value) -> Option<i128> {
64    match v {
65        Value::Int64(x) => Some(*x as i128),
66        Value::Int32(x) => Some(*x as i128),
67        Value::Int16(x) => Some(*x as i128),
68        Value::Int8(x) => Some(*x as i128),
69        Value::UInt64(x) => Some(*x as i128),
70        Value::UInt32(x) => Some(*x as i128),
71        Value::UInt16(x) => Some(*x as i128),
72        Value::UInt8(x) => Some(*x as i128),
73        _ => None,
74    }
75}
76
77pub(crate) fn compare_values(a: &Value, b: &Value) -> Result<std::cmp::Ordering, String> {
78    match (a, b) {
79        (Value::Int64(x), Value::Int64(y)) => Ok(x.cmp(y)),
80        (Value::Int32(x), Value::Int32(y)) => Ok(x.cmp(y)),
81        (Value::Int16(x), Value::Int16(y)) => Ok(x.cmp(y)),
82        (Value::Int8(x), Value::Int8(y)) => Ok(x.cmp(y)),
83        (Value::UInt64(x), Value::UInt64(y)) => Ok(x.cmp(y)),
84        (Value::UInt32(x), Value::UInt32(y)) => Ok(x.cmp(y)),
85        (Value::UInt16(x), Value::UInt16(y)) => Ok(x.cmp(y)),
86        (Value::UInt8(x), Value::UInt8(y)) => Ok(x.cmp(y)),
87        (Value::Double(x), Value::Double(y)) => Ok(double_cmp(*x, *y)),
88        (Value::Float(x), Value::Float(y)) => Ok(double_cmp(*x as f64, *y as f64)),
89        (Value::String(x), Value::String(y)) => Ok(x.cmp(y)),
90        (Value::Bool(x), Value::Bool(y)) => Ok(x.cmp(y)),
91        (Value::Date(x), Value::Date(y)) => Ok(x.cmp(y)),
92        (Value::Timestamp(x), Value::Timestamp(y)) => Ok(x.cmp(y)),
93        // Cross-type numeric promotion (int ↔ float)
94        (Value::Int64(x), Value::Double(y)) => Ok(double_cmp(*x as f64, *y)),
95        (Value::Double(x), Value::Int64(y)) => Ok(double_cmp(*x, *y as f64)),
96        // Mixed signed/unsigned integer promotion (exact via i128). A UInt64
97        // column compared against an Int64 literal (e.g. `WHERE id > 5`) would
98        // otherwise hit the generic "Cannot compare types" error below.
99        _ => {
100            if let (Some(x), Some(y)) = (integer_to_i128(a), integer_to_i128(b)) {
101                Ok(x.cmp(&y))
102            } else {
103                Err("Cannot compare types".into())
104            }
105        }
106    }
107}