use std::sync::Arc;
use relate_core::{
expected_type_detail, integer_args, value_type_name, Error, ErrorKind, Result, Value,
FUNCTION_REQUIRES_ONE_ARGUMENT, LENGTH_REQUIRES_A_PATH,
};
use relate_ir::QuantifierKind;
use crate::overlay::ExecGraph;
use super::super::collection::{
collection_distinct, collection_extreme, collection_flatten, collection_has,
collection_index_of, collection_insert, collection_remove, collection_sort, collection_zip,
};
use super::super::{into_items, Extreme};
pub(super) fn bi_length(_graph: &ExecGraph<'_>, _n: &str, vals: Vec<Value>) -> Result<Value> {
unary_arg(vals, |value| match value {
Value::Path(p) => Ok(Value::Int(p.rels.len() as i64)),
_ => Err(Error::invalid_argument_type(LENGTH_REQUIRES_A_PATH)),
})
}
fn expected_type_error(expected: &str, value: &Value) -> Error {
Error::invalid_argument_type_expected(expected, &value_type_name(value))
}
fn unary_arg(vals: Vec<Value>, f: impl FnOnce(Value) -> Result<Value>) -> Result<Value> {
let mut vals = vals.into_iter();
match (vals.next(), vals.next()) {
(Some(Value::Null), None) => Ok(Value::Null),
(Some(value), None) => f(value),
_ => Err(Error::invalid_number_of_arguments(
FUNCTION_REQUIRES_ONE_ARGUMENT,
)),
}
}
pub(super) fn bi_size(_graph: &ExecGraph<'_>, _n: &str, vals: Vec<Value>) -> Result<Value> {
unary_arg(vals, |value| match value {
Value::List(xs) => Ok(Value::Int(xs.len() as i64)),
Value::String(s) => Ok(Value::Int(s.chars().count() as i64)),
Value::Vector { values, .. } => Ok(Value::Int(values.len() as i64)),
Value::Path(_) => Err(Error::invalid_argument_value("size() on paths")),
other => {
Err(Error::invalid_argument_value(expected_type_detail(
"LIST<ANY>, STRING or VECTOR",
&value_type_name(&other),
)))
}
})
}
pub(super) fn bi_isempty(_graph: &ExecGraph<'_>, _n: &str, vals: Vec<Value>) -> Result<Value> {
unary_arg(vals, |value| match value {
Value::List(xs) => Ok(Value::Bool(xs.is_empty())),
Value::Map(m) => Ok(Value::Bool(m.is_empty())),
Value::String(s) => Ok(Value::Bool(s.is_empty())),
other => Err(expected_type_error("STRING, LIST or MAP", &other)),
})
}
pub(super) fn bi_head(_graph: &ExecGraph<'_>, _n: &str, vals: Vec<Value>) -> Result<Value> {
unary_arg(vals, |value| match value {
Value::List(xs) => Ok(xs.first().cloned().unwrap_or(Value::Null)),
other => Err(expected_type_error("LIST<ANY>", &other)),
})
}
pub(super) fn bi_last(_graph: &ExecGraph<'_>, _n: &str, vals: Vec<Value>) -> Result<Value> {
unary_arg(vals, |value| match value {
Value::List(xs) => Ok(xs.last().cloned().unwrap_or(Value::Null)),
other => Err(expected_type_error("LIST<ANY>", &other)),
})
}
pub(super) fn bi_tail(_graph: &ExecGraph<'_>, _n: &str, vals: Vec<Value>) -> Result<Value> {
unary_arg(vals, |value| match value {
Value::List(xs) => Ok(Value::List(
into_items(xs).skip(1).collect::<Vec<_>>().into(),
)),
other => Err(expected_type_error("LIST<ANY>", &other)),
})
}
pub(super) fn bi_reverse(_graph: &ExecGraph<'_>, _n: &str, vals: Vec<Value>) -> Result<Value> {
unary_arg(vals, |value| match value {
Value::List(xs) => {
let mut o = Arc::unwrap_or_clone(xs);
o.reverse();
Ok(Value::List(o.into()))
}
Value::String(s) => Ok(Value::String(s.chars().rev().collect::<String>().into())),
other => Err(expected_type_error("STRING or LIST<ANY>", &other)),
})
}
pub(super) fn bi_cardinality(_graph: &ExecGraph<'_>, _n: &str, vals: Vec<Value>) -> Result<Value> {
unary_arg(vals, |value| match value {
Value::List(xs) => Ok(Value::Int(xs.len() as i64)),
Value::Map(m) => Ok(Value::Int(m.len() as i64)),
Value::Path(p) => Ok(Value::Int((p.nodes.len() + p.rels.len()) as i64)),
_ => Err(Error::ty("CARDINALITY() requires a list, path, or map")),
})
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct RangePlan {
start: i64,
step: i64,
size: i64,
}
#[derive(Clone, Debug)]
pub(crate) struct RangeValues {
next: i64,
step: i64,
remaining: i64,
}
impl RangePlan {
pub(crate) fn len(&self) -> Result<usize> {
usize::try_from(self.size).map_err(|_| Error::ty("integer out of range"))
}
pub(crate) fn values(self) -> RangeValues {
RangeValues {
next: self.start,
step: self.step,
remaining: self.size,
}
}
}
impl RangeValues {
pub(crate) fn remaining(&self) -> i64 {
self.remaining
}
}
impl Iterator for RangeValues {
type Item = Result<Value>;
fn next(&mut self) -> Option<Self::Item> {
if self.remaining <= 0 {
return None;
}
let value = Value::Int(self.next);
self.remaining -= 1;
if self.remaining > 0 {
match self.next.checked_add(self.step) {
Some(next) => self.next = next,
None => {
self.remaining = 0;
return Some(Err(Error::ty("integer out of range")));
}
}
}
Some(Ok(value))
}
}
pub(crate) fn range_plan(vals: &[Value]) -> Result<RangePlan> {
let int_arg_or_type_error = |v: Option<&Value>, detail: &str| {
v.and_then(super::super::numeric::int_arg)
.ok_or_else(|| Error::invalid_argument_type(detail))
};
let start = int_arg_or_type_error(vals.first(), integer_args::RANGE_START.type_error)?;
let end = int_arg_or_type_error(vals.get(1), integer_args::RANGE_END.type_error)?;
let step = if vals.len() >= 3 {
int_arg_or_type_error(vals.get(2), integer_args::RANGE_STEP.type_error)?
} else {
1
};
if step == 0 {
return Err(Error::argument_value_kind(
ErrorKind::NumberOutOfRange,
"range() step cannot be 0",
));
}
let diff = end
.checked_sub(start)
.ok_or_else(|| Error::ty("integer out of range"))?;
let size = if (diff > 0 && step < 0) || (diff < 0 && step > 0) {
0
} else {
diff.checked_div(step)
.and_then(|q| q.checked_add(1))
.ok_or_else(|| Error::ty("integer out of range"))?
};
Ok(RangePlan { start, step, size })
}
pub(super) fn bi_range(graph: &ExecGraph<'_>, _n: &str, vals: Vec<Value>) -> Result<Value> {
let plan = range_plan(&vals)?;
let xs = if plan.size > 0 {
let len = plan.len()?;
graph.charge_value_buffer(len)?;
let mut xs = Vec::with_capacity(len);
for value in plan.values() {
graph.cancel_tick()?;
xs.push(value?);
}
xs
} else {
Vec::new()
};
Ok(Value::List(xs.into()))
}
pub(super) fn bi_coll_flatten(graph: &ExecGraph<'_>, _n: &str, vals: Vec<Value>) -> Result<Value> {
collection_flatten(graph, &vals)
}
pub(super) fn bi_coll_sort(_graph: &ExecGraph<'_>, _n: &str, vals: Vec<Value>) -> Result<Value> {
unary_arg(vals, collection_sort)
}
pub(super) fn bi_coll_indexof(_graph: &ExecGraph<'_>, _n: &str, vals: Vec<Value>) -> Result<Value> {
collection_index_of(&vals)
}
pub(super) fn bi_coll_insert(_graph: &ExecGraph<'_>, _n: &str, vals: Vec<Value>) -> Result<Value> {
collection_insert(vals)
}
pub(super) fn bi_coll_remove(_graph: &ExecGraph<'_>, _n: &str, vals: Vec<Value>) -> Result<Value> {
collection_remove(vals)
}
pub(super) fn bi_coll_max(_graph: &ExecGraph<'_>, _n: &str, vals: Vec<Value>) -> Result<Value> {
unary_arg(vals, |value| collection_extreme(value, Extreme::Max))
}
pub(super) fn bi_coll_min(_graph: &ExecGraph<'_>, _n: &str, vals: Vec<Value>) -> Result<Value> {
unary_arg(vals, |value| collection_extreme(value, Extreme::Min))
}
pub(super) fn bi_coll_distinct(
_graph: &ExecGraph<'_>,
_n: &str,
vals: Vec<Value>,
) -> Result<Value> {
unary_arg(vals, collection_distinct)
}
pub(super) fn bi_coll_zip(graph: &ExecGraph<'_>, _n: &str, vals: Vec<Value>) -> Result<Value> {
collection_zip(graph, &vals)
}
pub(super) fn bi_coll_has_any_of(
_graph: &ExecGraph<'_>,
_n: &str,
vals: Vec<Value>,
) -> Result<Value> {
collection_has(QuantifierKind::Any, &vals)
}
pub(super) fn bi_coll_has_all_of(
_graph: &ExecGraph<'_>,
_n: &str,
vals: Vec<Value>,
) -> Result<Value> {
collection_has(QuantifierKind::All, &vals)
}
pub(super) fn bi_coll_has_none_of(
_graph: &ExecGraph<'_>,
_n: &str,
vals: Vec<Value>,
) -> Result<Value> {
collection_has(QuantifierKind::None, &vals)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::graph::Graph;
use crate::test_support::ints;
#[test]
fn tail_drops_the_head_of_a_shared_or_unique_list() {
let g = Graph::default();
let graph = ExecGraph::read(&g);
let tail = |v: Value| bi_tail(&graph, "tail", vec![v]).unwrap();
assert_eq!(tail(ints(&[1, 2, 3])), ints(&[2, 3]));
let input = ints(&[1, 2, 3]);
assert_eq!(tail(input.clone()), ints(&[2, 3]));
assert_eq!(input, ints(&[1, 2, 3]));
let empty = ints(&[]);
assert_eq!(tail(empty.clone()), ints(&[]));
assert_eq!(tail(empty), ints(&[]));
assert_eq!(tail(Value::Null), Value::Null);
}
fn assert_invalid_argument(error: Error, expected: &str) {
match error {
Error::InvalidArgumentType(detail) => {
assert!(
detail.contains(expected),
"detail should name the accepted type: {detail}"
)
}
other => panic!("expected InvalidArgumentType, got {other:?}"),
}
}
fn assert_invalid_argument_value(error: Error, expected: &str) {
match error {
Error::Type(detail) => assert!(
detail.contains(expected),
"detail should name the accepted type: {detail}"
),
other => panic!("expected the InvalidArgumentValue type error, got {other:?}"),
}
}
fn assert_one_argument_error(error: Error) {
assert_eq!(
error,
Error::invalid_number_of_arguments(FUNCTION_REQUIRES_ONE_ARGUMENT),
"zero or extra arguments is the one-argument arity rejection"
);
}
#[test]
fn head_last_tail_and_reverse_reject_wrong_argument_types() {
let g = Graph::default();
let graph = ExecGraph::read(&g);
for (name, builtin, nonempty, empty) in [
(
"head",
bi_head as super::super::registry::Builtin,
Value::Int(1),
Value::Null,
),
(
"last",
bi_last as super::super::registry::Builtin,
Value::Int(3),
Value::Null,
),
(
"tail",
bi_tail as super::super::registry::Builtin,
ints(&[2, 3]),
ints(&[]),
),
] {
assert_eq!(
builtin(&graph, name, vec![ints(&[1, 2, 3])]).unwrap(),
nonempty
);
assert_eq!(builtin(&graph, name, vec![ints(&[])]).unwrap(), empty);
assert_eq!(
builtin(&graph, name, vec![Value::Null]).unwrap(),
Value::Null
);
assert_one_argument_error(builtin(&graph, name, vec![]).unwrap_err());
assert_one_argument_error(
builtin(&graph, name, vec![ints(&[1, 2]), Value::Null]).unwrap_err(),
);
for bad in [
Value::Int(1),
Value::Float(1.5),
Value::Bool(true),
Value::String("abc".into()),
Value::Map(std::collections::BTreeMap::new().into()),
Value::Vector {
coord: relate_core::VectorCoordKind::Float64,
values: vec![Value::Float(1.0)].into(),
},
] {
let error = builtin(&graph, name, vec![bad]).unwrap_err();
assert_invalid_argument(error, "LIST<ANY>");
}
}
assert_eq!(
bi_reverse(&graph, "reverse", vec![Value::Null]).unwrap(),
Value::Null
);
assert_one_argument_error(
bi_reverse(&graph, "reverse", vec![ints(&[1]), ints(&[2])]).unwrap_err(),
);
assert_eq!(
bi_reverse(&graph, "reverse", vec![ints(&[1, 2, 3])]).unwrap(),
ints(&[3, 2, 1])
);
assert_eq!(
bi_reverse(&graph, "reverse", vec![Value::String("abc".into())]).unwrap(),
Value::String("cba".into())
);
for bad in [
Value::Int(1),
Value::Bool(true),
Value::Map(std::collections::BTreeMap::new().into()),
Value::Vector {
coord: relate_core::VectorCoordKind::Float64,
values: vec![Value::Float(1.0)].into(),
},
] {
let error = bi_reverse(&graph, "reverse", vec![bad]).unwrap_err();
assert_invalid_argument(error, "STRING or LIST<ANY>");
}
}
#[test]
fn size_rejects_maps_and_other_unsized_types() {
let g = Graph::default();
let graph = ExecGraph::read(&g);
assert_eq!(
bi_size(&graph, "size", vec![ints(&[1, 2, 3])]).unwrap(),
Value::Int(3)
);
assert_one_argument_error(bi_size(&graph, "size", vec![]).unwrap_err());
assert_one_argument_error(
bi_size(&graph, "size", vec![ints(&[1]), ints(&[2])]).unwrap_err(),
);
assert_eq!(
bi_size(&graph, "size", vec![Value::String("héllo".into())]).unwrap(),
Value::Int(5)
);
assert_eq!(
bi_size(
&graph,
"size",
vec![Value::Vector {
coord: relate_core::VectorCoordKind::Float64,
values: vec![Value::Float(1.0), Value::Float(2.0)].into(),
}]
)
.unwrap(),
Value::Int(2)
);
assert_eq!(
bi_size(&graph, "size", vec![Value::Null]).unwrap(),
Value::Null
);
for bad in [
Value::Map(std::collections::BTreeMap::new().into()),
Value::Int(1),
Value::Bool(true),
] {
let error = bi_size(&graph, "size", vec![bad]).unwrap_err();
assert_invalid_argument_value(error, "LIST<ANY>, STRING or VECTOR");
}
}
#[test]
fn coll_zip_rejects_non_list_arguments() {
let g = Graph::default();
let graph = ExecGraph::read(&g);
for args in [
vec![Value::Int(1), ints(&[1])],
vec![ints(&[1]), Value::Int(1)],
vec![ints(&[1]), Value::String("x".into())],
] {
let error = bi_coll_zip(&graph, "coll.zip", args).unwrap_err();
assert_invalid_argument_value(error, "coll.zip() requires a list");
}
assert_eq!(
bi_coll_zip(&graph, "coll.zip", vec![Value::Null, ints(&[1])]).unwrap(),
Value::Null
);
assert_eq!(
bi_coll_zip(&graph, "coll.zip", vec![ints(&[1]), Value::Null]).unwrap(),
Value::Null
);
assert_eq!(
bi_coll_zip(&graph, "coll.zip", vec![ints(&[1, 2]), ints(&[3, 4])]).unwrap(),
Value::List(vec![ints(&[1, 3]), ints(&[2, 4])].into())
);
}
#[test]
fn one_argument_coll_builtins_reject_extra_arguments_and_non_lists() {
let g = Graph::default();
let graph = ExecGraph::read(&g);
for (name, builtin) in [
("coll.sort", bi_coll_sort as super::super::registry::Builtin),
("coll.min", bi_coll_min as super::super::registry::Builtin),
("coll.max", bi_coll_max as super::super::registry::Builtin),
(
"coll.distinct",
bi_coll_distinct as super::super::registry::Builtin,
),
] {
assert_one_argument_error(
builtin(&graph, name, vec![ints(&[1, 2]), ints(&[3])]).unwrap_err(),
);
assert_eq!(
builtin(&graph, name, vec![Value::Null]).unwrap(),
Value::Null
);
let error = builtin(&graph, name, vec![Value::Int(1)]).unwrap_err();
assert_invalid_argument_value(error, &format!("{name}() requires a list"));
}
}
#[test]
fn length_and_cardinality_use_the_unary_arity_contract() {
let g = Graph::default();
let graph = ExecGraph::read(&g);
for (name, builtin) in [
("length", bi_length as super::super::registry::Builtin),
(
"cardinality",
bi_cardinality as super::super::registry::Builtin,
),
] {
assert_one_argument_error(builtin(&graph, name, vec![]).unwrap_err());
assert_one_argument_error(
builtin(&graph, name, vec![ints(&[1]), ints(&[2])]).unwrap_err(),
);
assert_eq!(
builtin(&graph, name, vec![Value::Null]).unwrap(),
Value::Null
);
}
assert_eq!(
bi_cardinality(&graph, "cardinality", vec![ints(&[1, 2])]).unwrap(),
Value::Int(2)
);
assert!(bi_length(&graph, "length", vec![ints(&[1, 2])]).is_err());
}
#[test]
fn coll_min_and_max_rank_across_types_like_order_by() {
let g = Graph::default();
let graph = ExecGraph::read(&g);
let mixed = Value::List(
vec![
Value::Int(7),
Value::Int(2),
ints(&[2]),
Value::Int(2),
Value::Int(5),
Value::String("a".into()),
]
.into(),
);
assert_eq!(
bi_coll_min(&graph, "coll.min", vec![mixed.clone()]).unwrap(),
ints(&[2])
);
assert_eq!(
bi_coll_max(&graph, "coll.max", vec![mixed]).unwrap(),
Value::Int(7)
);
let ranked = Value::List(
vec![
Value::String("a".into()),
ints(&[1]),
Value::Int(3),
Value::Bool(true),
]
.into(),
);
assert_eq!(
bi_coll_min(&graph, "coll.min", vec![ranked.clone()]).unwrap(),
ints(&[1])
);
assert_eq!(
bi_coll_max(&graph, "coll.max", vec![ranked]).unwrap(),
Value::Int(3)
);
}
#[test]
fn coll_max_poisons_on_null_while_coll_min_skips() {
let g = Graph::default();
let graph = ExecGraph::read(&g);
let with_null = Value::List(
vec![
Value::String("hello".into()),
Value::Int(1),
Value::Bool(true),
Value::Null,
]
.into(),
);
assert_eq!(
bi_coll_max(&graph, "coll.max", vec![with_null.clone()]).unwrap(),
Value::Null
);
assert_eq!(
bi_coll_min(&graph, "coll.min", vec![with_null]).unwrap(),
Value::String("hello".into())
);
let nums = Value::List(vec![Value::Int(1), Value::Null, Value::Int(2)].into());
assert_eq!(
bi_coll_max(&graph, "coll.max", vec![nums.clone()]).unwrap(),
Value::Null
);
assert_eq!(
bi_coll_min(&graph, "coll.min", vec![nums]).unwrap(),
Value::Int(1)
);
assert_eq!(
bi_coll_max(&graph, "coll.max", vec![Value::Null]).unwrap(),
Value::Null
);
}
#[test]
fn isempty_reads_list_map_and_string_emptiness() {
let g = Graph::default();
let graph = ExecGraph::read(&g);
let isempty = |v: Value| bi_isempty(&graph, "isEmpty", vec![v]).unwrap();
assert_one_argument_error(bi_isempty(&graph, "isEmpty", vec![]).unwrap_err());
assert_one_argument_error(
bi_isempty(&graph, "isEmpty", vec![ints(&[]), ints(&[])]).unwrap_err(),
);
let map = |entries: &[(&str, Value)]| {
Value::Map(
entries
.iter()
.map(|(k, v)| ((*k).to_string(), v.clone()))
.collect::<std::collections::BTreeMap<_, _>>()
.into(),
)
};
assert_eq!(isempty(Value::Null), Value::Null);
assert_eq!(isempty(ints(&[])), Value::Bool(true));
assert_eq!(isempty(ints(&[1])), Value::Bool(false));
assert_eq!(isempty(map(&[])), Value::Bool(true));
assert_eq!(isempty(map(&[("a", Value::Int(1))])), Value::Bool(false));
assert_eq!(isempty(Value::String("".into())), Value::Bool(true));
assert_eq!(isempty(Value::String("hello".into())), Value::Bool(false));
let error = bi_isempty(&graph, "isEmpty", vec![Value::Int(1)]).unwrap_err();
assert_invalid_argument(error, "STRING, LIST or MAP");
}
#[test]
fn expression_budget_refuses_flatten_and_zip_allocations() {
let mut g = Graph::default();
g.configure_expression_memory_limit(Some(1));
let graph = ExecGraph::read(&g);
let list = |xs: Vec<Value>| Value::List(xs.into());
let nested = list(vec![Value::Int(1), list(vec![Value::Int(2)])]);
let error = bi_coll_flatten(&graph, "coll.flatten", vec![nested]).unwrap_err();
assert!(
matches!(
error,
Error::Capacity {
limit: 1,
kind: relate_core::CapacityKind::Expression,
..
}
),
"{error}"
);
let a = list(vec![Value::Int(1), Value::Int(2)]);
let b = list(vec![Value::Int(3)]);
let error = bi_coll_zip(&graph, "coll.zip", vec![a, b]).unwrap_err();
assert!(
matches!(
error,
Error::Capacity {
limit: 1,
kind: relate_core::CapacityKind::Expression,
..
}
),
"{error}"
);
}
}