1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
//! The TinkerPop `Operator` token and its pure semantics over [`ElementProperty`] values.
//!
//! `Operator` is the binary function behind `withSack(init, Operator)`, `sack(Operator)`,
//! `fold(seed, Operator)` and `withSideEffect(key, init, Operator)`. This module holds only the
//! token and [`Operator::apply`]; the steps that use it come later.
use std::fmt;
use compact_str::CompactString;
use crate::error::{OperatorFailure, ValueError};
use crate::value::scalar::compare_scalars;
use crate::value::{ElementProperty, N, Number, PropertyMap, ValueMap};
/// TinkerPop's `Operator` enum: a binary function over two values.
///
/// Closed by TinkerPop's definition, but `#[non_exhaustive]` (a later TinkerPop release may add
/// one). In the Rhai DSL the tokens are `Operator.sum` / `Operator::sum` and so on.
///
/// Deliberate deviations from TinkerPop 3.7/3.8, all documented in the book page "Sack and
/// operators" and in `tinkerpop_deviations.md`:
///
/// - integers are `int64` only: an integer overflow of `sum`/`minus`/`mult` is an error
/// ([`OperatorFailure::Overflow`]) instead of promotion to a wider type, and `sumLong` does not
/// wrap around;
/// - `div` of two integers truncates toward zero (Java `/`); a float operand gives IEEE results;
/// - `addAll` of a list and a scalar appends the scalar (3.8.2 feature files; 3.7.2 throws).
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Operator {
/// `Operator.sum`: addition of two numbers.
///
/// Null rule inherited from TinkerPop's `NumberHelper`: if either operand is null the result
/// is the **left** operand, so `sum(null, 1)` is null and `sum(1, null)` is 1. The same rule
/// holds for `minus`, `mult` and `div`.
Sum,
/// `Operator.minus`: subtraction of two numbers (null rule as for [`Operator::Sum`]).
Minus,
/// `Operator.mult`: multiplication of two numbers (null rule as for [`Operator::Sum`]).
Mult,
/// `Operator.div`: division. Two integers divide as Java does (truncation toward zero; a zero
/// divisor or `i64::MIN / -1` is an error); with a float operand the division is IEEE (`inf`,
/// `NaN`, no error). Null rule as for [`Operator::Sum`].
Div,
/// `Operator.max`: the larger of two numbers (int and float widen to float) or of two values of
/// the same scalar kind (string, boolean, uuid). A null operand yields the other operand.
Max,
/// `Operator.min`: the smaller of two values, same rules as [`Operator::Max`].
Min,
/// `Operator.assign`: the right operand, even when it is null.
Assign,
/// `Operator.and`: logical and of two booleans. One null operand yields the other, two nulls
/// yield null.
And,
/// `Operator.or`: logical or of two booleans (null rule as for [`Operator::And`]).
Or,
/// `Operator.addAll`: list + list concatenates, map + map is putAll (the right value wins, the
/// left key order is kept), list + scalar appends the scalar.
AddAll,
/// `Operator.sumLong`: addition of two `int64` values; overflow is an error (3.7.2 wraps).
SumLong,
}
impl Operator {
/// The TinkerPop spelling of the operator (`"sum"`, ..., `"addAll"`, `"sumLong"`).
pub fn name(self) -> &'static str {
match self {
Operator::Sum => "sum",
Operator::Minus => "minus",
Operator::Mult => "mult",
Operator::Div => "div",
Operator::Max => "max",
Operator::Min => "min",
Operator::Assign => "assign",
Operator::And => "and",
Operator::Or => "or",
Operator::AddAll => "addAll",
Operator::SumLong => "sumLong",
}
}
/// Applies the operator to two values and returns the result.
///
/// # Errors
///
/// [`ValueError::OperatorFailed`] when an integer operation overflows, an integer is divided by
/// zero, or the operands are not of the kind the operator accepts.
pub fn apply(
self,
left: &ElementProperty,
right: &ElementProperty,
) -> Result<ElementProperty, ValueError> {
self.apply_owned(left.clone(), right.clone())
}
/// Owning form of [`Operator::apply`]: `addAll` extends the left list in place, so folding a
/// growing list is linear, not quadratic.
pub(crate) fn apply_owned(
self,
left: ElementProperty,
right: ElementProperty,
) -> Result<ElementProperty, ValueError> {
match self {
Operator::Assign => Ok(right),
Operator::Sum => self.arithmetic(left, right),
Operator::Minus => self.arithmetic(left, right),
Operator::Mult => self.arithmetic(left, right),
Operator::Div => self.arithmetic(left, right),
Operator::Max | Operator::Min => self.extremum(left, right),
Operator::And | Operator::Or => self.logic(left, right),
Operator::AddAll => self.add_all(left, right),
Operator::SumLong => self.sum_long(left, right),
}
}
fn fail(
self,
kind: OperatorFailure,
left: &ElementProperty,
right: &ElementProperty,
) -> ValueError {
ValueError::OperatorFailed {
operator: self.name(),
kind,
left: describe(left),
right: describe(right),
}
}
fn incompatible(
self,
expected: &'static str,
left: &ElementProperty,
right: &ElementProperty,
) -> ValueError {
self.fail(
OperatorFailure::IncompatibleOperands { expected },
left,
right,
)
}
fn arithmetic(
self,
left: ElementProperty,
right: ElementProperty,
) -> Result<ElementProperty, ValueError> {
if matches!(left, ElementProperty::Null) || matches!(right, ElementProperty::Null) {
return Ok(left);
}
let (ElementProperty::Number(a), ElementProperty::Number(b)) = (&left, &right) else {
return Err(self.incompatible("number", &left, &right));
};
let result = match (&a.n, &b.n) {
(N::NegInt(a), N::NegInt(b)) => {
let (a, b) = (*a, *b);
let checked = match self {
Operator::Sum => a.checked_add(b),
Operator::Minus => a.checked_sub(b),
Operator::Mult => a.checked_mul(b),
_ => {
if b == 0 {
return Err(self.fail(OperatorFailure::DivisionByZero, &left, &right));
}
a.checked_div(b)
}
};
match checked {
Some(value) => Number::from(value),
None => return Err(self.fail(OperatorFailure::Overflow, &left, &right)),
}
}
(a, b) => {
let (a, b) = (as_f64(a), as_f64(b));
Number::from(match self {
Operator::Sum => a + b,
Operator::Minus => a - b,
Operator::Mult => a * b,
_ => a / b,
})
}
};
Ok(ElementProperty::Number(result))
}
fn extremum(
self,
left: ElementProperty,
right: ElementProperty,
) -> Result<ElementProperty, ValueError> {
if matches!(left, ElementProperty::Null) {
return Ok(right);
}
if matches!(right, ElementProperty::Null) {
return Ok(left);
}
let want_max = self == Operator::Max;
if let (ElementProperty::Number(a), ElementProperty::Number(b)) = (&left, &right) {
return Ok(ElementProperty::Number(match (&a.n, &b.n) {
(N::NegInt(x), N::NegInt(y)) => {
Number::from(if want_max { *x.max(y) } else { *x.min(y) })
}
(x, y) => {
let (x, y) = (as_f64(x), as_f64(y));
// Java's Math.min/max: NaN wins.
Number::from(if x.is_nan() || y.is_nan() {
f64::NAN
} else if want_max {
x.max(y)
} else {
x.min(y)
})
}
}));
}
match compare_scalars(&left, &right) {
Some(ordering) => {
let left_wins = if want_max {
ordering.is_ge()
} else {
ordering.is_le()
};
Ok(if left_wins { left } else { right })
}
None => Err(self.incompatible(
"two numbers or two values of one scalar kind",
&left,
&right,
)),
}
}
fn logic(
self,
left: ElementProperty,
right: ElementProperty,
) -> Result<ElementProperty, ValueError> {
match (&left, &right) {
(ElementProperty::Null, _) => {
if matches!(right, ElementProperty::Null | ElementProperty::Boolean(_)) {
Ok(right)
} else {
Err(self.incompatible("boolean", &left, &right))
}
}
(_, ElementProperty::Null) => {
if matches!(left, ElementProperty::Boolean(_)) {
Ok(left)
} else {
Err(self.incompatible("boolean", &left, &right))
}
}
(ElementProperty::Boolean(a), ElementProperty::Boolean(b)) => {
Ok(ElementProperty::Boolean(if self == Operator::And {
*a && *b
} else {
*a || *b
}))
}
_ => Err(self.incompatible("boolean", &left, &right)),
}
}
fn sum_long(
self,
left: ElementProperty,
right: ElementProperty,
) -> Result<ElementProperty, ValueError> {
if let (ElementProperty::Number(a), ElementProperty::Number(b)) = (&left, &right)
&& let (N::NegInt(a), N::NegInt(b)) = (&a.n, &b.n)
{
return match a.checked_add(*b) {
Some(sum) => Ok(ElementProperty::Number(Number::from(sum))),
None => Err(self.fail(OperatorFailure::Overflow, &left, &right)),
};
}
Err(self.incompatible("two int64 values", &left, &right))
}
fn add_all(
self,
left: ElementProperty,
right: ElementProperty,
) -> Result<ElementProperty, ValueError> {
match (left, right) {
(ElementProperty::Array(mut items), ElementProperty::Array(more)) => {
items.extend(more);
Ok(ElementProperty::Array(items))
}
(ElementProperty::Array(mut items), right) if is_scalar(&right) => {
items.push(right);
Ok(ElementProperty::Array(items))
}
(ElementProperty::Object(mut target), ElementProperty::Object(more)) => {
for (key, value) in *more {
target.insert(key, value);
}
Ok(ElementProperty::Object(target))
}
(
left @ (ElementProperty::Object(_) | ElementProperty::Map(_)),
right @ (ElementProperty::Object(_) | ElementProperty::Map(_)),
) => {
let mut target = into_value_map(left);
for (key, value) in into_value_map(right) {
target.insert(key, value);
}
Ok(target.into_property())
}
(left, right) => {
Err(self.incompatible("two lists, two maps, or a list and a value", &left, &right))
}
}
}
}
impl fmt::Display for Operator {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.name())
}
}
fn as_f64(n: &N) -> f64 {
match n {
N::NegInt(value) => *value as f64,
N::Float(value) => **value,
}
}
fn is_scalar(value: &ElementProperty) -> bool {
matches!(
value,
ElementProperty::Null
| ElementProperty::String(_)
| ElementProperty::Number(_)
| ElementProperty::Boolean(_)
| ElementProperty::Uuid(_)
)
}
fn into_value_map(value: ElementProperty) -> ValueMap {
match value {
ElementProperty::Object(object) => {
let object: PropertyMap = *object;
object
.into_iter()
.map(|(key, value): (CompactString, ElementProperty)| {
(ElementProperty::String(key), value)
})
.collect()
}
ElementProperty::Map(map) => *map,
_ => ValueMap::new(),
}
}
/// `value (kind)` for a diagnostic message.
pub(crate) fn describe(value: &ElementProperty) -> String {
let kind = match value {
ElementProperty::Null => "null",
ElementProperty::String(_) | ElementProperty::Compressed(_) => "string",
ElementProperty::Number(n) if n.is_i64() => "int64",
ElementProperty::Number(_) => "float64",
ElementProperty::Boolean(_) => "boolean",
ElementProperty::Uuid(_) => "uuid",
ElementProperty::Object(_) | ElementProperty::Map(_) => "map",
ElementProperty::Array(_) => "list",
ElementProperty::Vertex(_) => "vertex",
ElementProperty::Edge(_) => "edge",
};
let mut rendered = crate::value::literal::rhai_literal(value);
if rendered.chars().count() > 48 {
rendered = rendered.chars().take(48).collect::<String>() + "…";
}
format!("{rendered} ({kind})")
}
#[cfg(test)]
mod tests {
use super::Operator;
#[test]
fn display_is_the_tinkerpop_name() {
assert_eq!(Operator::AddAll.to_string(), "addAll");
assert_eq!(Operator::SumLong.to_string(), "sumLong");
}
}