Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions engine/src/ast/field_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -799,9 +799,9 @@ mod tests {
use crate::ast::logical_expr::LogicalExpr;
use crate::execution_context::ExecutionContext;
use crate::functions::{
FunctionArgKind, FunctionArgs, FunctionDefinition, FunctionDefinitionContext,
FunctionParam, FunctionParamError, SimpleFunctionDefinition, SimpleFunctionImpl,
SimpleFunctionOptParam, SimpleFunctionParam,
CompiledFunction, FunctionArgKind, FunctionArgs, FunctionDefinition,
FunctionDefinitionContext, FunctionParam, FunctionParamError, SimpleFunctionDefinition,
SimpleFunctionImpl, SimpleFunctionOptParam, SimpleFunctionParam,
};
use crate::lhs_types::{Array, Map};
use crate::list_matcher::{ListDefinition, ListMatcher};
Expand Down Expand Up @@ -908,12 +908,7 @@ mod tests {
&self,
_: &mut dyn ExactSizeIterator<Item = FunctionParam<'_>>,
_: Option<FunctionDefinitionContext>,
) -> Box<
dyn for<'i, 'a> Fn(FunctionArgs<'i, 'a>) -> Option<LhsValue<'a>>
+ Sync
+ Send
+ 'static,
> {
) -> CompiledFunction {
Box::new(|args| {
let value_array = Array::try_from(args.next().unwrap().unwrap()).unwrap();
let keep_array = Array::try_from(args.next().unwrap().unwrap()).unwrap();
Expand Down
8 changes: 3 additions & 5 deletions engine/src/ast/function_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::ast::logical_expr::{LogicalExpr, UnaryOp};
use crate::compiler::Compiler;
use crate::filter::{CompiledExpr, CompiledValueExpr, CompiledValueResult};
use crate::functions::{
ExactSizeChain, FunctionArgs, FunctionDefinition, FunctionDefinitionContext, FunctionParam,
CompiledFunction, ExactSizeChain, FunctionDefinition, FunctionDefinitionContext, FunctionParam,
FunctionParamError,
};
use crate::lex::{Lex, LexError, LexErrorKind, LexResult, LexWith, expect, skip_space, span};
Expand Down Expand Up @@ -272,11 +272,9 @@ impl ValueExpr for FunctionCallExpr {
let first = args.remove(0);

#[inline(always)]
fn compute<'s, 'a, I: ExactSizeIterator<Item = CompiledValueResult<'a>>>(
fn compute<'a, I: ExactSizeIterator<Item = CompiledValueResult<'a>>>(
first: CompiledValueResult<'a>,
call: &(
dyn for<'b> Fn(FunctionArgs<'_, 'b>) -> Option<LhsValue<'b>> + Sync + Send + 's
),
call: &CompiledFunction,
return_type: Type,
f: impl Fn(LhsValue<'a>) -> I,
) -> CompiledValueResult<'a> {
Expand Down
12 changes: 5 additions & 7 deletions engine/src/functions/all.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use crate::{
FunctionArgKind, FunctionArgs, FunctionDefinition, FunctionDefinitionContext, FunctionParam,
FunctionParamError, GetType, LhsValue, ParserSettings, Type,
CompiledFunction, FunctionArgKind, FunctionArgs, FunctionDefinition, FunctionDefinitionContext,
FunctionParam, FunctionParamError, GetType, LhsValue, ParserSettings, Type,
};
use std::iter::once;

#[inline]
fn all_impl<'a>(args: FunctionArgs<'_, 'a>) -> Option<LhsValue<'a>> {
let arg = args.next().expect("expected 1 argument, got 0");
if args.next().is_some() {
Expand Down Expand Up @@ -58,12 +57,11 @@ impl FunctionDefinition for AllFunction {
(1, Some(0))
}

fn compile<'s>(
&'s self,
fn compile(
&self,
_: &mut dyn ExactSizeIterator<Item = FunctionParam<'_>>,
_: Option<FunctionDefinitionContext>,
) -> Box<dyn for<'i, 'a> Fn(FunctionArgs<'i, 'a>) -> Option<LhsValue<'a>> + Sync + Send + 'static>
{
) -> CompiledFunction {
Box::new(all_impl)
}
}
Expand Down
12 changes: 5 additions & 7 deletions engine/src/functions/any.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use crate::{
FunctionArgKind, FunctionArgs, FunctionDefinition, FunctionDefinitionContext, FunctionParam,
FunctionParamError, GetType, LhsValue, ParserSettings, Type,
CompiledFunction, FunctionArgKind, FunctionArgs, FunctionDefinition, FunctionDefinitionContext,
FunctionParam, FunctionParamError, GetType, LhsValue, ParserSettings, Type,
};
use std::iter::once;

#[inline]
fn any_impl<'a>(args: FunctionArgs<'_, 'a>) -> Option<LhsValue<'a>> {
let arg = args.next().expect("expected 1 argument, got 0");
if args.next().is_some() {
Expand Down Expand Up @@ -58,12 +57,11 @@ impl FunctionDefinition for AnyFunction {
(1, Some(0))
}

fn compile<'s>(
&'s self,
fn compile(
&self,
_: &mut dyn ExactSizeIterator<Item = FunctionParam<'_>>,
_: Option<FunctionDefinitionContext>,
) -> Box<dyn for<'i, 'a> Fn(FunctionArgs<'i, 'a>) -> Option<LhsValue<'a>> + Sync + Send + 'static>
{
) -> CompiledFunction {
Box::new(any_impl)
}
}
Expand Down
60 changes: 32 additions & 28 deletions engine/src/functions/concat.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{
Array, Bytes, ExpectedType, FunctionArgs, FunctionDefinition, FunctionDefinitionContext,
FunctionParam, FunctionParamError, GetType, LhsValue, ParserSettings, Type,
Array, Bytes, CompiledFunction, ExpectedType, FunctionArgs, FunctionDefinition,
FunctionDefinitionContext, FunctionParam, FunctionParamError, GetType, LhsValue,
ParserSettings, Type,
};
use std::iter::once;

Expand All @@ -19,6 +20,7 @@ impl ConcatFunction {
}
}

#[inline]
fn concat_array<'a>(accumulator: Array<'a>, args: FunctionArgs<'_, 'a>) -> Array<'a> {
let mut args = args.flat_map(|arg| arg.ok());
let Some(first) = args.next() else {
Expand All @@ -43,6 +45,7 @@ fn concat_array<'a>(accumulator: Array<'a>, args: FunctionArgs<'_, 'a>) -> Array
Array::try_from_vec(val_type, vec).unwrap()
}

#[inline]
fn concat_bytes<'a>(mut accumulator: Vec<u8>, args: FunctionArgs<'_, 'a>) -> Bytes<'a> {
for arg in args {
match arg {
Expand All @@ -54,6 +57,25 @@ fn concat_bytes<'a>(mut accumulator: Vec<u8>, args: FunctionArgs<'_, 'a>) -> Byt
accumulator.into()
}

fn concat_impl<'a>(args: FunctionArgs<'_, 'a>) -> Option<LhsValue<'a>> {
while let Some(arg) = args.next() {
match arg {
Ok(LhsValue::Array(array)) => {
return Some(LhsValue::Array(concat_array(array, args)));
}
Ok(LhsValue::Bytes(bytes)) => {
return Some(LhsValue::Bytes(concat_bytes(
bytes.into_owned().into(),
args,
)));
}
Err(_) => (),
_ => unreachable!(),
}
}
None
}

pub(crate) const EXPECTED_TYPES: [ExpectedType; 2] =
[ExpectedType::Array, ExpectedType::Type(Type::Bytes)];

Expand Down Expand Up @@ -90,30 +112,12 @@ impl FunctionDefinition for ConcatFunction {
(2, None)
}

fn compile<'s>(
&'s self,
fn compile(
&self,
_: &mut dyn ExactSizeIterator<Item = FunctionParam<'_>>,
_: Option<FunctionDefinitionContext>,
) -> Box<dyn for<'i, 'a> Fn(FunctionArgs<'i, 'a>) -> Option<LhsValue<'a>> + Sync + Send + 'static>
{
Box::new(|args| {
while let Some(arg) = args.next() {
match arg {
Ok(LhsValue::Array(array)) => {
return Some(LhsValue::Array(concat_array(array, args)));
}
Ok(LhsValue::Bytes(bytes)) => {
return Some(LhsValue::Bytes(concat_bytes(
bytes.into_owned().into(),
args,
)));
}
Err(_) => (),
_ => unreachable!(),
}
}
None
})
) -> CompiledFunction {
Box::new(concat_impl)
}
}

Expand All @@ -133,7 +137,7 @@ mod tests {
.into_iter();
assert_eq!(
Some(LhsValue::Bytes(Bytes::Borrowed(b"helloworld"))),
CONCAT_FN.compile(&mut std::iter::empty(), None)(&mut args)
concat_impl(&mut args)
);
}

Expand All @@ -148,7 +152,7 @@ mod tests {
.into_iter();
assert_eq!(
Some(LhsValue::Bytes(Bytes::Borrowed(b"helloworldhello2world2"))),
CONCAT_FN.compile(&mut std::iter::empty(), None)(&mut args)
concat_impl(&mut args)
);
}

Expand All @@ -159,15 +163,15 @@ mod tests {
let mut args = vec![Ok(arg1), Ok(arg2)].into_iter();
assert_eq!(
Some(LhsValue::Array(Array::from_iter([1, 2, 3, 4, 5, 6]))),
CONCAT_FN.compile(&mut std::iter::empty(), None)(&mut args)
concat_impl(&mut args)
);
}

#[test]
#[should_panic]
fn test_concat_function_bad_arg_type() {
let mut args = vec![Ok(LhsValue::from(2))].into_iter();
CONCAT_FN.compile(&mut std::iter::empty(), None)(&mut args);
concat_impl(&mut args);
}

#[test]
Expand Down
12 changes: 9 additions & 3 deletions engine/src/functions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,13 @@ impl std::fmt::Debug for FunctionDefinitionContext {
}
}

/// A compiled function that can be called during filter execution.
///
/// Returned by [`FunctionDefinition::compile`] after a function call expression
/// has been validated and compiled.
pub type CompiledFunction =
Box<dyn for<'i, 'a> Fn(FunctionArgs<'i, 'a>) -> Option<LhsValue<'a>> + Sync + Send + 'static>;

/// Trait to implement function
pub trait FunctionDefinition: Debug + Send + Sync {
/// Custom context to store information during parsing
Expand Down Expand Up @@ -404,7 +411,7 @@ pub trait FunctionDefinition: Debug + Send + Sync {
&self,
params: &mut dyn ExactSizeIterator<Item = FunctionParam<'_>>,
ctx: Option<FunctionDefinitionContext>,
) -> Box<dyn for<'i, 'a> Fn(FunctionArgs<'i, 'a>) -> Option<LhsValue<'a>> + Sync + Send + 'static>;
) -> CompiledFunction;
}

// Simple function APIs
Expand Down Expand Up @@ -530,8 +537,7 @@ impl FunctionDefinition for SimpleFunctionDefinition {
&self,
params: &mut dyn ExactSizeIterator<Item = FunctionParam<'_>>,
_: Option<FunctionDefinitionContext>,
) -> Box<dyn for<'i, 'a> Fn(FunctionArgs<'i, 'a>) -> Option<LhsValue<'a>> + Sync + Send + 'static>
{
) -> CompiledFunction {
let params_count = params.len();
let opt_params = &self.opt_params[(params_count - self.params.len())..];
let implementation = self.implementation;
Expand Down
8 changes: 4 additions & 4 deletions engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ pub use self::filter::{
CompiledExpr, CompiledOneExpr, CompiledValueExpr, CompiledVecExpr, Filter, FilterValue,
};
pub use self::functions::{
AllFunction, AnyFunction, ConcatFunction, FunctionArgInvalidConstantError, FunctionArgKind,
FunctionArgKindMismatchError, FunctionArgs, FunctionDefinition, FunctionDefinitionContext,
FunctionParam, FunctionParamError, SimpleFunctionArgKind, SimpleFunctionDefinition,
SimpleFunctionImpl, SimpleFunctionOptParam, SimpleFunctionParam,
AllFunction, AnyFunction, CompiledFunction, ConcatFunction, FunctionArgInvalidConstantError,
FunctionArgKind, FunctionArgKindMismatchError, FunctionArgs, FunctionDefinition,
FunctionDefinitionContext, FunctionParam, FunctionParamError, SimpleFunctionArgKind,
SimpleFunctionDefinition, SimpleFunctionImpl, SimpleFunctionOptParam, SimpleFunctionParam,
};
pub use self::lex::LexErrorKind;
pub use self::lhs_types::{Array, Bytes, Map, MapIter, TypedArray, TypedMap};
Expand Down
Loading