use std::cmp::Ordering;
use super::*;
fn cypher_eq_pairs<'a>(pairs: impl Iterator<Item = (&'a Value, &'a Value)>, depth: u32) -> Value {
let mut saw_null = false;
for (x, y) in pairs {
match x.cypher_eq_at(y, depth) {
Value::Bool(false) => return Value::Bool(false),
Value::Null => saw_null = true,
_ => {}
}
}
if saw_null {
Value::Null
} else {
Value::Bool(true)
}
}
fn lexicographic<A, B, C>(
a: impl Iterator<Item = A>,
b: impl Iterator<Item = B>,
equal: C,
tie: C,
mut cmp: impl FnMut(A, B) -> C,
) -> C
where
C: PartialEq,
{
for (x, y) in a.zip(b) {
let result = cmp(x, y);
if result != equal {
return result;
}
}
tie
}
fn elementwise_eq(a: &[Value], b: &[Value], depth: u32) -> bool {
a.len() == b.len()
&& a.iter()
.zip(b.iter())
.all(|(x, y)| x.eq_non_null_at(y, depth))
}
fn compare_maps(
a: &std::collections::BTreeMap<String, Value>,
b: &std::collections::BTreeMap<String, Value>,
depth: u32,
) -> Option<Ordering> {
match a.len().cmp(&b.len()) {
Ordering::Equal => {}
other => return Some(other),
}
let by_key = lexicographic(
a.keys(),
b.keys(),
Ordering::Equal,
Ordering::Equal,
|ka: &String, kb: &String| ka.cmp(kb),
);
if by_key != Ordering::Equal {
return Some(by_key);
}
lexicographic(
a.values(),
b.values(),
Some(Ordering::Equal),
Some(Ordering::Equal),
|x, y| x.compare_at(y, depth),
)
}
impl Value {
pub fn cypher_eq(&self, other: &Value) -> Value {
self.cypher_eq_at(other, 0)
}
fn cypher_eq_at(&self, other: &Value, depth: u32) -> Value {
if self.is_null() || other.is_null() {
return Value::Null;
}
match (self, other) {
(Value::List(a), Value::List(b)) if descends(depth) => {
if a.len() != b.len() {
return Value::Bool(false);
}
cypher_eq_pairs(a.iter().zip(b.iter()), depth + 1)
}
(Value::Map(a), Value::Map(b)) if descends(depth) => {
if a.len() != b.len() || a.keys().any(|k| !b.contains_key(k)) {
return Value::Bool(false);
}
cypher_eq_pairs(
a.iter().map(|(k, v)| (v, b.get(k).expect("key checked"))),
depth + 1,
)
}
(Value::List(_), Value::List(_)) | (Value::Map(_), Value::Map(_)) => Value::Bool(false),
_ => Value::Bool(self.eq_non_null_at(other, depth)),
}
}
pub fn eq_non_null(&self, other: &Value) -> bool {
self.eq_non_null_at(other, 0)
}
fn eq_non_null_at(&self, other: &Value, depth: u32) -> bool {
match (self, other) {
(Value::Bool(a), Value::Bool(b)) => a == b,
(Value::Int(a), Value::Int(b)) => a == b,
(Value::Float(a), Value::Float(b)) => a == b,
(Value::Int(a), Value::Float(b)) => relate_model::canonical_key::eq_i64_f64(*a, *b),
(Value::Float(a), Value::Int(b)) => relate_model::canonical_key::eq_i64_f64(*b, *a),
(Value::String(a), Value::String(b)) => a == b,
(Value::Bytes(a), Value::Bytes(b)) => a == b,
(Value::Uuid(a), Value::Uuid(b)) => a == b,
(Value::List(a), Value::List(b)) if descends(depth) => elementwise_eq(a, b, depth + 1),
(Value::Map(a), Value::Map(b)) if descends(depth) => {
a.len() == b.len()
&& a.iter()
.all(|(k, v)| b.get(k).is_some_and(|o| v.eq_non_null_at(o, depth + 1)))
}
(Value::Node(a), Value::Node(b)) => a.id == b.id,
(Value::Relationship(a), Value::Relationship(b)) => a.id == b.id,
(Value::Path(a), Value::Path(b)) => a == b,
(Value::Temporal(a), Value::Temporal(b)) => a.equals(b),
(Value::Point(a), Value::Point(b)) => a == b,
(
Value::Vector {
coord: ac,
values: av,
},
Value::Vector {
coord: bc,
values: bv,
},
) if descends(depth) => {
ac == bc && elementwise_eq(av, bv, depth + 1)
}
_ => false,
}
}
pub fn compare(&self, other: &Value) -> Option<Ordering> {
self.compare_at(other, 0)
}
fn compare_at(&self, other: &Value, depth: u32) -> Option<Ordering> {
if self.is_null() || other.is_null() {
return None;
}
match (self, other) {
(Value::Int(a), Value::Int(b)) => Some(a.cmp(b)),
(Value::Float(a), Value::Float(b)) => a.partial_cmp(b),
(Value::Int(a), Value::Float(b)) => relate_model::canonical_key::cmp_i64_f64(*a, *b),
(Value::Float(a), Value::Int(b)) => {
relate_model::canonical_key::cmp_i64_f64(*b, *a).map(Ordering::reverse)
}
(Value::String(a), Value::String(b)) => Some(a.cmp(b)),
(Value::Bytes(a), Value::Bytes(b)) => Some(a.cmp(b)),
(Value::Bool(a), Value::Bool(b)) => Some(a.cmp(b)),
(Value::Uuid(a), Value::Uuid(b)) => (*a == *b).then_some(Ordering::Equal),
(Value::Temporal(a), Value::Temporal(b)) => a.compare(b),
(Value::List(a), Value::List(b)) if descends(depth) => lexicographic(
a.iter(),
b.iter(),
Some(Ordering::Equal),
Some(a.len().cmp(&b.len())),
|x, y| x.compare_at(y, depth + 1),
),
(Value::Map(a), Value::Map(b)) if descends(depth) => compare_maps(a, b, depth + 1),
_ => None,
}
}
pub fn order_by_cmp(&self, other: &Value) -> Ordering {
self.order_by_cmp_at(other, 0)
}
fn order_by_cmp_at(&self, other: &Value, depth: u32) -> Ordering {
let ra = self.order_type_rank();
let rb = other.order_type_rank();
if ra != rb {
return ra.cmp(&rb);
}
match (self, other) {
(Value::Null, Value::Null) => Ordering::Equal,
(Value::Uuid(a), Value::Uuid(b)) => a.cmp(b),
(Value::Temporal(a), Value::Temporal(b)) => a.total_cmp(b),
(Value::List(a), Value::List(b)) if descends(depth) => lexicographic(
a.iter(),
b.iter(),
Ordering::Equal,
a.len().cmp(&b.len()),
|x, y| x.order_by_cmp_at(y, depth + 1),
),
(
Value::Vector {
coord: ac,
values: av,
},
Value::Vector {
coord: bc,
values: bv,
},
) if descends(depth) => {
let type_order = ac
.order_rank()
.cmp(&bc.order_rank())
.then_with(|| av.len().cmp(&bv.len()));
if type_order != Ordering::Equal {
return type_order;
}
lexicographic(
av.iter(),
bv.iter(),
Ordering::Equal,
Ordering::Equal,
|x, y| x.order_by_cmp_at(y, depth + 1),
)
}
_ => self.compare_at(other, depth).unwrap_or(Ordering::Equal),
}
}
fn order_type_rank(&self) -> u8 {
match self {
Value::Map(_) => 0,
Value::Node(_) => 1,
Value::Relationship(_) => 2,
Value::List(_) => 3,
Value::Bytes(_) => 4,
Value::Path(_) => 5,
Value::Vector { .. } => 6,
Value::Point(_) => 7,
Value::Temporal(t) => 8 + t.kind() as u8,
Value::String(_) => 14,
Value::Bool(_) => 15,
Value::Uuid(_) => 16,
Value::Int(_) => 17,
Value::Float(f) if f.is_nan() => 19,
Value::Float(_) => 17,
Value::Null => 20,
}
}
}