2022-05-14 12:35:08 +00:00
|
|
|
use crate::ctx::Context;
|
2023-07-06 14:57:42 +00:00
|
|
|
use crate::dbs::{Options, Transaction};
|
|
|
|
use crate::doc::CursorDoc;
|
2021-03-29 15:43:37 +00:00
|
|
|
use crate::err::Error;
|
|
|
|
use crate::fnc;
|
2020-06-29 15:36:01 +00:00
|
|
|
use crate::sql::comment::mightbespace;
|
2023-03-25 19:44:03 +00:00
|
|
|
use crate::sql::common::val_char;
|
2023-05-10 02:08:09 +00:00
|
|
|
use crate::sql::common::{closeparentheses, commas, openparentheses};
|
2022-01-16 20:31:50 +00:00
|
|
|
use crate::sql::error::IResult;
|
2022-10-04 21:51:18 +00:00
|
|
|
use crate::sql::fmt::Fmt;
|
2023-03-25 19:44:03 +00:00
|
|
|
use crate::sql::idiom::Idiom;
|
2022-06-27 15:57:11 +00:00
|
|
|
use crate::sql::script::{script as func, Script};
|
2023-06-06 06:12:59 +00:00
|
|
|
use crate::sql::value::{value, Value};
|
2023-03-25 19:44:03 +00:00
|
|
|
use async_recursion::async_recursion;
|
2023-02-12 12:18:47 +00:00
|
|
|
use futures::future::try_join_all;
|
2020-06-29 15:36:01 +00:00
|
|
|
use nom::branch::alt;
|
|
|
|
use nom::bytes::complete::tag;
|
2023-03-25 19:44:03 +00:00
|
|
|
use nom::bytes::complete::take_while1;
|
2022-03-16 23:52:25 +00:00
|
|
|
use nom::character::complete::char;
|
2023-04-06 08:33:52 +00:00
|
|
|
use nom::combinator::recognize;
|
2021-03-29 15:43:37 +00:00
|
|
|
use nom::multi::separated_list0;
|
2023-04-06 08:33:52 +00:00
|
|
|
use nom::multi::separated_list1;
|
2023-04-17 12:09:56 +00:00
|
|
|
use nom::sequence::preceded;
|
2020-06-29 15:36:01 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2021-03-29 15:43:37 +00:00
|
|
|
use std::cmp::Ordering;
|
2020-06-29 15:36:01 +00:00
|
|
|
use std::fmt;
|
|
|
|
|
2023-03-30 10:41:44 +00:00
|
|
|
pub(crate) const TOKEN: &str = "$surrealdb::private::sql::Function";
|
|
|
|
|
2023-04-29 20:50:25 +00:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Hash)]
|
|
|
|
#[serde(rename = "$surrealdb::private::sql::Function")]
|
2021-03-29 15:43:37 +00:00
|
|
|
pub enum Function {
|
2022-01-13 17:36:41 +00:00
|
|
|
Normal(String, Vec<Value>),
|
2023-03-25 19:44:03 +00:00
|
|
|
Custom(String, Vec<Value>),
|
2022-06-27 15:57:11 +00:00
|
|
|
Script(Script, Vec<Value>),
|
2023-04-29 20:50:25 +00:00
|
|
|
// Add new variants here
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialOrd for Function {
|
|
|
|
#[inline]
|
|
|
|
fn partial_cmp(&self, _: &Self) -> Option<Ordering> {
|
2022-03-23 11:56:39 +00:00
|
|
|
None
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
2022-03-25 18:43:22 +00:00
|
|
|
impl Function {
|
2022-11-23 09:42:59 +00:00
|
|
|
/// Get function name if applicable
|
2022-07-16 22:23:13 +00:00
|
|
|
pub fn name(&self) -> &str {
|
|
|
|
match self {
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::Normal(n, _) => n.as_str(),
|
2023-03-25 19:44:03 +00:00
|
|
|
Self::Custom(n, _) => n.as_str(),
|
2022-07-16 22:23:13 +00:00
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
2022-11-23 09:42:59 +00:00
|
|
|
/// Get function arguments if applicable
|
2022-03-25 18:43:22 +00:00
|
|
|
pub fn args(&self) -> &[Value] {
|
|
|
|
match self {
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::Normal(_, a) => a,
|
2023-03-25 19:44:03 +00:00
|
|
|
Self::Custom(_, a) => a,
|
2022-03-25 18:43:22 +00:00
|
|
|
_ => &[],
|
|
|
|
}
|
|
|
|
}
|
2023-03-25 19:44:03 +00:00
|
|
|
/// Convert function call to a field name
|
|
|
|
pub fn to_idiom(&self) -> Idiom {
|
|
|
|
match self {
|
|
|
|
Self::Script(_, _) => "function".to_string().into(),
|
|
|
|
Self::Normal(f, _) => f.to_owned().into(),
|
|
|
|
Self::Custom(f, _) => format!("fn::{f}").into(),
|
|
|
|
}
|
|
|
|
}
|
2022-11-23 09:42:59 +00:00
|
|
|
/// Convert this function to an aggregate
|
2022-10-04 21:51:18 +00:00
|
|
|
pub fn aggregate(&self, val: Value) -> Self {
|
2022-03-25 18:43:22 +00:00
|
|
|
match self {
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::Normal(n, a) => {
|
2022-03-25 18:43:22 +00:00
|
|
|
let mut a = a.to_owned();
|
|
|
|
match a.len() {
|
|
|
|
0 => a.insert(0, val),
|
|
|
|
_ => {
|
|
|
|
a.remove(0);
|
|
|
|
a.insert(0, val);
|
|
|
|
}
|
|
|
|
}
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::Normal(n.to_owned(), a)
|
2022-03-25 18:43:22 +00:00
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
2023-04-13 19:31:22 +00:00
|
|
|
/// Check if this function is a custom function
|
|
|
|
pub fn is_custom(&self) -> bool {
|
|
|
|
matches!(self, Self::Custom(_, _))
|
|
|
|
}
|
2022-11-23 09:42:59 +00:00
|
|
|
/// Check if this function is a rolling function
|
2022-03-25 18:43:22 +00:00
|
|
|
pub fn is_rolling(&self) -> bool {
|
|
|
|
match self {
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::Normal(f, _) if f == "count" => true,
|
|
|
|
Self::Normal(f, _) if f == "math::max" => true,
|
|
|
|
Self::Normal(f, _) if f == "math::mean" => true,
|
|
|
|
Self::Normal(f, _) if f == "math::min" => true,
|
|
|
|
Self::Normal(f, _) if f == "math::sum" => true,
|
2022-03-25 18:43:22 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2022-11-23 09:42:59 +00:00
|
|
|
/// Check if this function is a grouping function
|
2022-03-25 18:43:22 +00:00
|
|
|
pub fn is_aggregate(&self) -> bool {
|
|
|
|
match self {
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::Normal(f, _) if f == "array::distinct" => true,
|
2023-01-07 19:40:56 +00:00
|
|
|
Self::Normal(f, _) if f == "array::group" => true,
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::Normal(f, _) if f == "count" => true,
|
|
|
|
Self::Normal(f, _) if f == "math::bottom" => true,
|
|
|
|
Self::Normal(f, _) if f == "math::interquartile" => true,
|
|
|
|
Self::Normal(f, _) if f == "math::max" => true,
|
|
|
|
Self::Normal(f, _) if f == "math::mean" => true,
|
|
|
|
Self::Normal(f, _) if f == "math::median" => true,
|
|
|
|
Self::Normal(f, _) if f == "math::midhinge" => true,
|
|
|
|
Self::Normal(f, _) if f == "math::min" => true,
|
|
|
|
Self::Normal(f, _) if f == "math::mode" => true,
|
|
|
|
Self::Normal(f, _) if f == "math::nearestrank" => true,
|
|
|
|
Self::Normal(f, _) if f == "math::percentile" => true,
|
|
|
|
Self::Normal(f, _) if f == "math::sample" => true,
|
|
|
|
Self::Normal(f, _) if f == "math::spread" => true,
|
|
|
|
Self::Normal(f, _) if f == "math::stddev" => true,
|
|
|
|
Self::Normal(f, _) if f == "math::sum" => true,
|
|
|
|
Self::Normal(f, _) if f == "math::top" => true,
|
|
|
|
Self::Normal(f, _) if f == "math::trimean" => true,
|
|
|
|
Self::Normal(f, _) if f == "math::variance" => true,
|
2022-03-25 18:43:22 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-14 08:12:56 +00:00
|
|
|
impl Function {
|
2023-05-09 22:17:29 +00:00
|
|
|
/// Process this type returning a computed simple Value
|
2023-03-25 19:44:03 +00:00
|
|
|
#[cfg_attr(not(target_arch = "wasm32"), async_recursion)]
|
|
|
|
#[cfg_attr(target_arch = "wasm32", async_recursion(?Send))]
|
2023-07-06 14:57:42 +00:00
|
|
|
pub(crate) async fn compute(
|
|
|
|
&self,
|
|
|
|
ctx: &Context<'_>,
|
|
|
|
opt: &Options,
|
|
|
|
txn: &Transaction,
|
|
|
|
doc: Option<&'async_recursion CursorDoc<'_>>,
|
|
|
|
) -> Result<Value, Error> {
|
2022-10-06 16:35:03 +00:00
|
|
|
// Prevent long function chains
|
|
|
|
let opt = &opt.dive(1)?;
|
2022-12-22 08:33:57 +00:00
|
|
|
// Ensure futures are run
|
2023-07-05 21:26:13 +00:00
|
|
|
let opt = &opt.new_with_futures(true);
|
2022-10-06 16:35:03 +00:00
|
|
|
// Process the function type
|
2021-03-29 15:43:37 +00:00
|
|
|
match self {
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::Normal(s, x) => {
|
2023-02-12 12:18:47 +00:00
|
|
|
// Compute the function arguments
|
2023-07-06 14:57:42 +00:00
|
|
|
let a = try_join_all(x.iter().map(|v| v.compute(ctx, opt, txn, doc))).await?;
|
2023-02-12 12:18:47 +00:00
|
|
|
// Run the normal function
|
2023-07-06 14:57:42 +00:00
|
|
|
fnc::run(ctx, txn, doc, s, a).await
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
2023-03-25 19:44:03 +00:00
|
|
|
Self::Custom(s, x) => {
|
|
|
|
// Get the function definition
|
|
|
|
let val = {
|
|
|
|
// Claim transaction
|
2023-06-19 18:41:13 +00:00
|
|
|
let mut run = txn.lock().await;
|
2023-03-25 19:44:03 +00:00
|
|
|
// Get the function definition
|
|
|
|
run.get_fc(opt.ns(), opt.db(), s).await?
|
|
|
|
};
|
|
|
|
// Check the function arguments
|
|
|
|
if x.len() != val.args.len() {
|
|
|
|
return Err(Error::InvalidArguments {
|
|
|
|
name: format!("fn::{}", val.name),
|
|
|
|
message: match val.args.len() {
|
|
|
|
1 => String::from("The function expects 1 argument."),
|
|
|
|
l => format!("The function expects {l} arguments."),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// Compute the function arguments
|
2023-07-06 14:57:42 +00:00
|
|
|
let a = try_join_all(x.iter().map(|v| v.compute(ctx, opt, txn, doc))).await?;
|
2023-03-25 19:44:03 +00:00
|
|
|
// Duplicate context
|
|
|
|
let mut ctx = Context::new(ctx);
|
|
|
|
// Process the function arguments
|
|
|
|
for (val, (name, kind)) in a.into_iter().zip(val.args) {
|
2023-06-06 06:12:59 +00:00
|
|
|
ctx.add_value(name.to_raw(), val.coerce_to(&kind)?);
|
2023-03-25 19:44:03 +00:00
|
|
|
}
|
|
|
|
// Run the custom function
|
2023-07-06 14:57:42 +00:00
|
|
|
val.block.compute(&ctx, opt, txn, doc).await
|
2023-03-25 19:44:03 +00:00
|
|
|
}
|
2022-07-03 09:43:23 +00:00
|
|
|
#[allow(unused_variables)]
|
2022-10-04 21:51:18 +00:00
|
|
|
Self::Script(s, x) => {
|
2022-07-03 09:43:23 +00:00
|
|
|
#[cfg(feature = "scripting")]
|
|
|
|
{
|
2023-02-12 12:18:47 +00:00
|
|
|
// Compute the function arguments
|
2023-07-06 14:57:42 +00:00
|
|
|
let a = try_join_all(x.iter().map(|v| v.compute(ctx, opt, txn, doc))).await?;
|
2023-02-12 12:18:47 +00:00
|
|
|
// Run the script function
|
2023-07-06 14:57:42 +00:00
|
|
|
fnc::script::run(ctx, opt, txn, doc, s, a).await
|
2022-07-03 09:43:23 +00:00
|
|
|
}
|
|
|
|
#[cfg(not(feature = "scripting"))]
|
|
|
|
{
|
2022-06-27 15:57:11 +00:00
|
|
|
Err(Error::InvalidScript {
|
|
|
|
message: String::from("Embedded functions are not enabled."),
|
|
|
|
})
|
2022-05-20 21:16:25 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-14 08:12:56 +00:00
|
|
|
impl fmt::Display for Function {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match self {
|
2023-03-25 19:44:03 +00:00
|
|
|
Self::Normal(s, e) => write!(f, "{s}({})", Fmt::comma_separated(e)),
|
|
|
|
Self::Custom(s, e) => write!(f, "fn::{s}({})", Fmt::comma_separated(e)),
|
|
|
|
Self::Script(s, e) => write!(f, "function({}) {{{s}}}", Fmt::comma_separated(e)),
|
2022-01-14 08:12:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-29 15:36:01 +00:00
|
|
|
pub fn function(i: &str) -> IResult<&str, Function> {
|
2023-06-06 06:12:59 +00:00
|
|
|
alt((normal, custom, script))(i)
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
|
|
|
|
2023-03-31 15:42:29 +00:00
|
|
|
pub fn normal(i: &str) -> IResult<&str, Function> {
|
2022-06-27 15:57:11 +00:00
|
|
|
let (i, s) = function_names(i)?;
|
2023-05-10 02:08:09 +00:00
|
|
|
let (i, _) = openparentheses(i)?;
|
2022-06-27 15:57:11 +00:00
|
|
|
let (i, a) = separated_list0(commas, value)(i)?;
|
2023-05-10 02:08:09 +00:00
|
|
|
let (i, _) = closeparentheses(i)?;
|
2022-06-27 15:57:11 +00:00
|
|
|
Ok((i, Function::Normal(s.to_string(), a)))
|
|
|
|
}
|
|
|
|
|
2023-03-31 15:42:29 +00:00
|
|
|
pub fn custom(i: &str) -> IResult<&str, Function> {
|
2023-03-25 19:44:03 +00:00
|
|
|
let (i, _) = tag("fn::")(i)?;
|
2023-04-06 08:33:52 +00:00
|
|
|
let (i, s) = recognize(separated_list1(tag("::"), take_while1(val_char)))(i)?;
|
2023-03-25 19:44:03 +00:00
|
|
|
let (i, _) = char('(')(i)?;
|
|
|
|
let (i, _) = mightbespace(i)?;
|
|
|
|
let (i, a) = separated_list0(commas, value)(i)?;
|
|
|
|
let (i, _) = mightbespace(i)?;
|
|
|
|
let (i, _) = char(')')(i)?;
|
|
|
|
Ok((i, Function::Custom(s.to_string(), a)))
|
|
|
|
}
|
|
|
|
|
2022-06-27 15:57:11 +00:00
|
|
|
fn script(i: &str) -> IResult<&str, Function> {
|
2023-03-30 14:19:18 +00:00
|
|
|
let (i, _) = tag("function")(i)?;
|
2023-05-10 02:08:09 +00:00
|
|
|
let (i, _) = openparentheses(i)?;
|
2022-12-17 16:23:20 +00:00
|
|
|
let (i, _) = mightbespace(i)?;
|
2022-06-27 15:57:11 +00:00
|
|
|
let (i, a) = separated_list0(commas, value)(i)?;
|
2023-05-10 02:08:09 +00:00
|
|
|
let (i, _) = closeparentheses(i)?;
|
2022-01-13 17:36:41 +00:00
|
|
|
let (i, _) = mightbespace(i)?;
|
2022-06-27 15:57:11 +00:00
|
|
|
let (i, _) = char('{')(i)?;
|
|
|
|
let (i, v) = func(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char('}')(i)?;
|
2022-06-27 15:57:11 +00:00
|
|
|
Ok((i, Function::Script(v, a)))
|
2022-01-13 17:36:41 +00:00
|
|
|
}
|
|
|
|
|
2023-05-04 23:05:51 +00:00
|
|
|
pub(crate) fn function_names(i: &str) -> IResult<&str, &str> {
|
2023-04-17 12:09:56 +00:00
|
|
|
recognize(alt((
|
2023-07-14 18:00:07 +00:00
|
|
|
alt((
|
|
|
|
preceded(tag("array::"), function_array),
|
|
|
|
preceded(tag("bytes::"), function_bytes),
|
|
|
|
preceded(tag("crypto::"), function_crypto),
|
|
|
|
preceded(tag("duration::"), function_duration),
|
|
|
|
preceded(tag("encoding::"), function_encoding),
|
|
|
|
preceded(tag("geo::"), function_geo),
|
|
|
|
preceded(tag("http::"), function_http),
|
|
|
|
preceded(tag("is::"), function_is),
|
|
|
|
preceded(tag("math::"), function_math),
|
|
|
|
preceded(tag("meta::"), function_meta),
|
|
|
|
preceded(tag("parse::"), function_parse),
|
|
|
|
preceded(tag("rand::"), function_rand),
|
|
|
|
preceded(tag("search::"), function_search),
|
|
|
|
preceded(tag("session::"), function_session),
|
|
|
|
preceded(tag("string::"), function_string),
|
|
|
|
preceded(tag("time::"), function_time),
|
|
|
|
preceded(tag("type::"), function_type),
|
|
|
|
preceded(tag("vector::"), function_vector),
|
|
|
|
)),
|
|
|
|
alt((tag("count"), tag("not"), tag("rand"), tag("sleep"))),
|
2023-04-17 12:09:56 +00:00
|
|
|
)))(i)
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 15:43:37 +00:00
|
|
|
fn function_array(i: &str) -> IResult<&str, &str> {
|
2020-06-29 15:36:01 +00:00
|
|
|
alt((
|
2023-03-25 22:35:55 +00:00
|
|
|
alt((
|
2023-04-17 12:09:56 +00:00
|
|
|
tag("add"),
|
|
|
|
tag("all"),
|
|
|
|
tag("any"),
|
|
|
|
tag("append"),
|
2023-07-10 08:15:18 +00:00
|
|
|
tag("boolean_and"),
|
|
|
|
tag("boolean_not"),
|
|
|
|
tag("boolean_or"),
|
|
|
|
tag("boolean_xor"),
|
|
|
|
tag("clump"),
|
2023-04-17 12:09:56 +00:00
|
|
|
tag("combine"),
|
|
|
|
tag("complement"),
|
|
|
|
tag("concat"),
|
|
|
|
tag("difference"),
|
|
|
|
tag("distinct"),
|
2023-07-10 08:15:18 +00:00
|
|
|
tag("filter_index"),
|
|
|
|
tag("find_index"),
|
2023-04-17 12:09:56 +00:00
|
|
|
tag("flatten"),
|
|
|
|
tag("group"),
|
|
|
|
tag("insert"),
|
2023-03-25 22:35:55 +00:00
|
|
|
)),
|
|
|
|
alt((
|
2023-04-17 12:09:56 +00:00
|
|
|
tag("intersect"),
|
2023-05-04 20:38:02 +00:00
|
|
|
tag("join"),
|
2023-04-17 12:09:56 +00:00
|
|
|
tag("len"),
|
2023-07-10 08:15:18 +00:00
|
|
|
tag("logical_and"),
|
|
|
|
tag("logical_or"),
|
|
|
|
tag("logical_xor"),
|
|
|
|
tag("matches"),
|
2023-04-17 12:09:56 +00:00
|
|
|
tag("max"),
|
|
|
|
tag("min"),
|
|
|
|
tag("pop"),
|
|
|
|
tag("prepend"),
|
|
|
|
tag("push"),
|
2023-07-10 08:15:18 +00:00
|
|
|
)),
|
|
|
|
alt((
|
2023-04-17 12:09:56 +00:00
|
|
|
tag("remove"),
|
|
|
|
tag("reverse"),
|
2023-04-25 21:48:09 +00:00
|
|
|
tag("slice"),
|
2023-04-17 12:09:56 +00:00
|
|
|
tag("sort::asc"),
|
|
|
|
tag("sort::desc"),
|
|
|
|
tag("sort"),
|
2023-07-10 08:15:18 +00:00
|
|
|
tag("transpose"),
|
2023-04-17 12:09:56 +00:00
|
|
|
tag("union"),
|
2023-03-25 22:35:55 +00:00
|
|
|
)),
|
2020-06-29 15:36:01 +00:00
|
|
|
))(i)
|
|
|
|
}
|
|
|
|
|
2023-05-09 20:43:16 +00:00
|
|
|
fn function_bytes(i: &str) -> IResult<&str, &str> {
|
|
|
|
alt((tag("len"),))(i)
|
|
|
|
}
|
|
|
|
|
2022-01-13 17:36:41 +00:00
|
|
|
fn function_crypto(i: &str) -> IResult<&str, &str> {
|
2020-06-29 15:36:01 +00:00
|
|
|
alt((
|
2023-04-17 12:09:56 +00:00
|
|
|
preceded(tag("argon2::"), alt((tag("compare"), tag("generate")))),
|
|
|
|
preceded(tag("bcrypt::"), alt((tag("compare"), tag("generate")))),
|
|
|
|
preceded(tag("pbkdf2::"), alt((tag("compare"), tag("generate")))),
|
|
|
|
preceded(tag("scrypt::"), alt((tag("compare"), tag("generate")))),
|
|
|
|
tag("md5"),
|
|
|
|
tag("sha1"),
|
|
|
|
tag("sha256"),
|
|
|
|
tag("sha512"),
|
2020-06-29 15:36:01 +00:00
|
|
|
))(i)
|
|
|
|
}
|
|
|
|
|
2022-09-22 23:48:49 +00:00
|
|
|
fn function_duration(i: &str) -> IResult<&str, &str> {
|
2023-04-25 10:13:04 +00:00
|
|
|
alt((
|
|
|
|
tag("days"),
|
|
|
|
tag("hours"),
|
|
|
|
tag("micros"),
|
|
|
|
tag("millis"),
|
|
|
|
tag("mins"),
|
|
|
|
tag("nanos"),
|
|
|
|
tag("secs"),
|
|
|
|
tag("weeks"),
|
|
|
|
tag("years"),
|
|
|
|
preceded(
|
2023-04-29 19:57:10 +00:00
|
|
|
tag("from::"),
|
2023-04-25 10:13:04 +00:00
|
|
|
alt((
|
|
|
|
tag("days"),
|
|
|
|
tag("hours"),
|
|
|
|
tag("micros"),
|
|
|
|
tag("millis"),
|
|
|
|
tag("mins"),
|
|
|
|
tag("nanos"),
|
|
|
|
tag("secs"),
|
|
|
|
tag("weeks"),
|
|
|
|
)),
|
|
|
|
),
|
|
|
|
))(i)
|
2022-09-22 23:48:49 +00:00
|
|
|
}
|
|
|
|
|
2023-05-09 20:43:16 +00:00
|
|
|
fn function_encoding(i: &str) -> IResult<&str, &str> {
|
|
|
|
alt((preceded(tag("base64::"), alt((tag("decode"), tag("encode")))),))(i)
|
|
|
|
}
|
|
|
|
|
2021-03-29 15:43:37 +00:00
|
|
|
fn function_geo(i: &str) -> IResult<&str, &str> {
|
2020-06-29 15:36:01 +00:00
|
|
|
alt((
|
2023-04-17 12:09:56 +00:00
|
|
|
tag("area"),
|
|
|
|
tag("bearing"),
|
|
|
|
tag("centroid"),
|
|
|
|
tag("distance"),
|
|
|
|
preceded(tag("hash::"), alt((tag("decode"), tag("encode")))),
|
2020-06-29 15:36:01 +00:00
|
|
|
))(i)
|
|
|
|
}
|
|
|
|
|
2021-03-29 15:43:37 +00:00
|
|
|
fn function_http(i: &str) -> IResult<&str, &str> {
|
2023-04-17 12:09:56 +00:00
|
|
|
alt((tag("head"), tag("get"), tag("put"), tag("post"), tag("patch"), tag("delete")))(i)
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 15:43:37 +00:00
|
|
|
fn function_is(i: &str) -> IResult<&str, &str> {
|
2020-06-29 15:36:01 +00:00
|
|
|
alt((
|
2023-04-17 12:09:56 +00:00
|
|
|
tag("alphanum"),
|
|
|
|
tag("alpha"),
|
|
|
|
tag("ascii"),
|
|
|
|
tag("datetime"),
|
|
|
|
tag("domain"),
|
|
|
|
tag("email"),
|
|
|
|
tag("hexadecimal"),
|
|
|
|
tag("latitude"),
|
|
|
|
tag("longitude"),
|
|
|
|
tag("numeric"),
|
|
|
|
tag("semver"),
|
|
|
|
tag("url"),
|
|
|
|
tag("uuid"),
|
2020-06-29 15:36:01 +00:00
|
|
|
))(i)
|
|
|
|
}
|
|
|
|
|
2021-03-29 15:43:37 +00:00
|
|
|
fn function_math(i: &str) -> IResult<&str, &str> {
|
2020-06-29 15:36:01 +00:00
|
|
|
alt((
|
|
|
|
alt((
|
2023-04-17 12:09:56 +00:00
|
|
|
tag("abs"),
|
|
|
|
tag("bottom"),
|
|
|
|
tag("ceil"),
|
|
|
|
tag("fixed"),
|
|
|
|
tag("floor"),
|
|
|
|
tag("interquartile"),
|
|
|
|
tag("max"),
|
|
|
|
tag("mean"),
|
|
|
|
tag("median"),
|
|
|
|
tag("midhinge"),
|
|
|
|
tag("min"),
|
|
|
|
tag("mode"),
|
2020-06-29 15:36:01 +00:00
|
|
|
)),
|
|
|
|
alt((
|
2023-04-17 12:09:56 +00:00
|
|
|
tag("nearestrank"),
|
|
|
|
tag("percentile"),
|
|
|
|
tag("pow"),
|
|
|
|
tag("product"),
|
|
|
|
tag("round"),
|
|
|
|
tag("spread"),
|
|
|
|
tag("sqrt"),
|
|
|
|
tag("stddev"),
|
|
|
|
tag("sum"),
|
|
|
|
tag("top"),
|
|
|
|
tag("trimean"),
|
|
|
|
tag("variance"),
|
2020-06-29 15:36:01 +00:00
|
|
|
)),
|
|
|
|
))(i)
|
|
|
|
}
|
|
|
|
|
2022-09-19 02:56:59 +00:00
|
|
|
fn function_meta(i: &str) -> IResult<&str, &str> {
|
2023-04-17 12:09:56 +00:00
|
|
|
alt((tag("id"), tag("table"), tag("tb")))(i)
|
2023-03-10 15:24:27 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 15:43:37 +00:00
|
|
|
fn function_parse(i: &str) -> IResult<&str, &str> {
|
2020-06-29 15:36:01 +00:00
|
|
|
alt((
|
2023-04-17 12:09:56 +00:00
|
|
|
preceded(tag("email::"), alt((tag("host"), tag("user")))),
|
|
|
|
preceded(
|
|
|
|
tag("url::"),
|
|
|
|
alt((
|
|
|
|
tag("domain"),
|
|
|
|
tag("fragment"),
|
|
|
|
tag("host"),
|
|
|
|
tag("path"),
|
|
|
|
tag("port"),
|
|
|
|
tag("query"),
|
|
|
|
tag("scheme"),
|
|
|
|
)),
|
|
|
|
),
|
2020-06-29 15:36:01 +00:00
|
|
|
))(i)
|
|
|
|
}
|
|
|
|
|
2021-03-29 15:43:37 +00:00
|
|
|
fn function_rand(i: &str) -> IResult<&str, &str> {
|
2020-06-29 15:36:01 +00:00
|
|
|
alt((
|
2023-04-17 12:09:56 +00:00
|
|
|
tag("bool"),
|
|
|
|
tag("enum"),
|
|
|
|
tag("float"),
|
|
|
|
tag("guid"),
|
|
|
|
tag("int"),
|
|
|
|
tag("string"),
|
|
|
|
tag("time"),
|
|
|
|
tag("ulid"),
|
|
|
|
tag("uuid::v4"),
|
|
|
|
tag("uuid::v7"),
|
|
|
|
tag("uuid"),
|
2020-06-29 15:36:01 +00:00
|
|
|
))(i)
|
|
|
|
}
|
|
|
|
|
2023-06-21 18:31:15 +00:00
|
|
|
fn function_search(i: &str) -> IResult<&str, &str> {
|
2023-06-23 20:26:19 +00:00
|
|
|
alt((tag("score"), tag("highlight"), tag("offsets")))(i)
|
2023-06-21 18:31:15 +00:00
|
|
|
}
|
|
|
|
|
2022-08-17 14:09:08 +00:00
|
|
|
fn function_session(i: &str) -> IResult<&str, &str> {
|
|
|
|
alt((
|
2023-04-17 12:09:56 +00:00
|
|
|
tag("db"),
|
|
|
|
tag("id"),
|
|
|
|
tag("ip"),
|
|
|
|
tag("ns"),
|
|
|
|
tag("origin"),
|
|
|
|
tag("sc"),
|
|
|
|
tag("sd"),
|
|
|
|
tag("token"),
|
2022-08-17 14:09:08 +00:00
|
|
|
))(i)
|
|
|
|
}
|
|
|
|
|
2021-03-29 15:43:37 +00:00
|
|
|
fn function_string(i: &str) -> IResult<&str, &str> {
|
2020-06-29 15:36:01 +00:00
|
|
|
alt((
|
2023-04-17 12:09:56 +00:00
|
|
|
tag("concat"),
|
2023-05-03 10:40:17 +00:00
|
|
|
tag("contains"),
|
2023-04-17 12:09:56 +00:00
|
|
|
tag("endsWith"),
|
|
|
|
tag("join"),
|
|
|
|
tag("len"),
|
|
|
|
tag("lowercase"),
|
|
|
|
tag("repeat"),
|
|
|
|
tag("replace"),
|
|
|
|
tag("reverse"),
|
|
|
|
tag("slice"),
|
|
|
|
tag("slug"),
|
|
|
|
tag("split"),
|
|
|
|
tag("startsWith"),
|
|
|
|
tag("trim"),
|
|
|
|
tag("uppercase"),
|
|
|
|
tag("words"),
|
2023-07-14 20:37:52 +00:00
|
|
|
preceded(tag("distance::"), alt((tag("hamming"), tag("levenshtein")))),
|
|
|
|
preceded(tag("similarity::"), alt((tag("fuzzy"), tag("jaro"), tag("smithwaterman")))),
|
2020-06-29 15:36:01 +00:00
|
|
|
))(i)
|
|
|
|
}
|
|
|
|
|
2021-03-29 15:43:37 +00:00
|
|
|
fn function_time(i: &str) -> IResult<&str, &str> {
|
2020-06-29 15:36:01 +00:00
|
|
|
alt((
|
2023-05-11 18:46:36 +00:00
|
|
|
tag("ceil"),
|
2023-04-17 12:09:56 +00:00
|
|
|
tag("day"),
|
|
|
|
tag("floor"),
|
|
|
|
tag("format"),
|
|
|
|
tag("group"),
|
|
|
|
tag("hour"),
|
|
|
|
tag("minute"),
|
|
|
|
tag("month"),
|
|
|
|
tag("nano"),
|
|
|
|
tag("now"),
|
|
|
|
tag("round"),
|
|
|
|
tag("second"),
|
|
|
|
tag("timezone"),
|
|
|
|
tag("unix"),
|
|
|
|
tag("wday"),
|
|
|
|
tag("week"),
|
|
|
|
tag("yday"),
|
|
|
|
tag("year"),
|
2023-04-29 19:57:10 +00:00
|
|
|
preceded(tag("from::"), alt((tag("micros"), tag("millis"), tag("secs"), tag("unix")))),
|
2020-06-29 15:36:01 +00:00
|
|
|
))(i)
|
|
|
|
}
|
|
|
|
|
2021-03-29 15:43:37 +00:00
|
|
|
fn function_type(i: &str) -> IResult<&str, &str> {
|
2020-06-29 15:36:01 +00:00
|
|
|
alt((
|
2023-04-17 12:09:56 +00:00
|
|
|
tag("bool"),
|
|
|
|
tag("datetime"),
|
|
|
|
tag("decimal"),
|
|
|
|
tag("duration"),
|
|
|
|
tag("float"),
|
|
|
|
tag("int"),
|
|
|
|
tag("number"),
|
|
|
|
tag("point"),
|
|
|
|
tag("string"),
|
|
|
|
tag("table"),
|
|
|
|
tag("thing"),
|
2020-06-29 15:36:01 +00:00
|
|
|
))(i)
|
|
|
|
}
|
|
|
|
|
2023-07-14 18:00:07 +00:00
|
|
|
fn function_vector(i: &str) -> IResult<&str, &str> {
|
|
|
|
alt((
|
2023-07-16 13:04:22 +00:00
|
|
|
tag("add"),
|
|
|
|
tag("angle"),
|
|
|
|
tag("divide"),
|
|
|
|
tag("cross"),
|
|
|
|
tag("dot"),
|
2023-07-14 18:00:07 +00:00
|
|
|
tag("magnitude"),
|
2023-07-16 13:04:22 +00:00
|
|
|
tag("multiply"),
|
|
|
|
tag("normalize"),
|
|
|
|
tag("project"),
|
|
|
|
tag("subtract"),
|
2023-07-14 18:00:07 +00:00
|
|
|
preceded(
|
|
|
|
tag("distance::"),
|
|
|
|
alt((
|
|
|
|
tag("chebyshev"),
|
|
|
|
tag("euclidean"),
|
|
|
|
tag("hamming"),
|
|
|
|
tag("mahalanobis"),
|
|
|
|
tag("manhattan"),
|
|
|
|
tag("minkowski"),
|
|
|
|
)),
|
|
|
|
),
|
|
|
|
preceded(
|
|
|
|
tag("similarity::"),
|
|
|
|
alt((tag("cosine"), tag("jaccard"), tag("pearson"), tag("spearman"))),
|
|
|
|
),
|
|
|
|
))(i)
|
|
|
|
}
|
|
|
|
|
2020-06-29 15:36:01 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use super::*;
|
2022-01-13 17:36:41 +00:00
|
|
|
use crate::sql::test::Parse;
|
2020-06-29 15:36:01 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn function_single() {
|
|
|
|
let sql = "count()";
|
|
|
|
let res = function(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("count()", format!("{}", out));
|
2021-03-29 15:43:37 +00:00
|
|
|
assert_eq!(out, Function::Normal(String::from("count"), vec![]));
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
2022-12-30 09:34:46 +00:00
|
|
|
#[test]
|
|
|
|
fn function_single_not() {
|
2023-06-09 15:23:30 +00:00
|
|
|
let sql = "not(10)";
|
2022-12-30 09:34:46 +00:00
|
|
|
let res = function(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
2023-06-09 15:23:30 +00:00
|
|
|
assert_eq!("not(10)", format!("{}", out));
|
|
|
|
assert_eq!(out, Function::Normal("not".to_owned(), vec![10.into()]));
|
2022-12-30 09:34:46 +00:00
|
|
|
}
|
|
|
|
|
2020-06-29 15:36:01 +00:00
|
|
|
#[test]
|
|
|
|
fn function_module() {
|
2022-01-13 17:36:41 +00:00
|
|
|
let sql = "rand::uuid()";
|
2020-06-29 15:36:01 +00:00
|
|
|
let res = function(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
2022-01-13 17:36:41 +00:00
|
|
|
assert_eq!("rand::uuid()", format!("{}", out));
|
|
|
|
assert_eq!(out, Function::Normal(String::from("rand::uuid"), vec![]));
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn function_arguments() {
|
|
|
|
let sql = "is::numeric(null)";
|
|
|
|
let res = function(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("is::numeric(NULL)", format!("{}", out));
|
2022-01-13 17:36:41 +00:00
|
|
|
assert_eq!(out, Function::Normal(String::from("is::numeric"), vec![Value::Null]));
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
2022-05-20 21:16:25 +00:00
|
|
|
#[test]
|
|
|
|
fn function_script_expression() {
|
2022-07-23 18:53:49 +00:00
|
|
|
let sql = "function() { return this.tags.filter(t => { return t.length > 3; }); }";
|
2022-05-20 21:16:25 +00:00
|
|
|
let res = function(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!(
|
2022-07-23 18:53:49 +00:00
|
|
|
"function() { return this.tags.filter(t => { return t.length > 3; }); }",
|
2022-05-20 21:16:25 +00:00
|
|
|
format!("{}", out)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
out,
|
2022-06-27 15:57:11 +00:00
|
|
|
Function::Script(
|
|
|
|
Script::parse(" return this.tags.filter(t => { return t.length > 3; }); "),
|
|
|
|
vec![]
|
|
|
|
)
|
2022-05-20 21:16:25 +00:00
|
|
|
);
|
|
|
|
}
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|