Skip to content

Commit fcdb680

Browse files
committed
Implemented greatest and least.
1 parent b4fc2c9 commit fcdb680

3 files changed

Lines changed: 28 additions & 2 deletions

File tree

Template.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,14 @@ From highest to lowest precedence:
233233
234234
If *value* equals to *p1*, then the expression's value is *r1*, etc.
235235

236-
* **@local := f()**
237-
238236
* **@local**
239237

240238
Gets the previous assigned local variable. If the variable was undefined, this will return NULL.
239+
240+
* **greatest(*x*, *y*, *z*)**
241+
242+
Returns the largest of all given values. NULL values are ignored.
243+
244+
* **least(*x*, *y*, *z*)**
245+
246+
Returns the smallest of all given values. NULL values are ignored.

src/eval.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,5 +405,22 @@ pub fn compile_function(name: Function, args: &[impl AsValue]) -> Result<Compile
405405
Compiled(C::Constant(Value::Null))
406406
})
407407
}
408+
409+
Function::Greatest | Function::Least => {
410+
let mut res = &Value::Null;
411+
for value in iter_args::<&Value, _>(name, args) {
412+
let value = value?;
413+
let should_replace = match value.sql_cmp(res, name)? {
414+
Some(Ordering::Greater) => name == Function::Greatest,
415+
Some(Ordering::Less) => name == Function::Least,
416+
None => res == &Value::Null,
417+
_ => false
418+
};
419+
if should_replace {
420+
res = value;
421+
}
422+
}
423+
Ok(Compiled(C::Constant(res.clone())))
424+
}
408425
}
409426
}

src/parser.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,9 @@ define_function! {
403403
RandLogNormal = "rand.log_normal",
404404
RandBool = "rand.bool",
405405

406+
Greatest = "greatest",
407+
Least = "least",
408+
406409
'sym_op:
407410
Eq = "=",
408411
Lt = "<",

0 commit comments

Comments
 (0)