use crate::Engine;
use crate::node::{CompiledNode, SYNTHETIC_ID, node_is_static};
use crate::opcode::OpCode;
use datavalue::OwnedDataValue;
pub(crate) fn fold(node: CompiledNode, engine: &Engine) -> (CompiledNode, bool) {
match &node {
CompiledNode::BuiltinOperator {
id, opcode, args, ..
} => {
if is_commutative(opcode) && args.len() >= 2 {
match try_partial_fold(*id, *opcode, args, engine) {
Some(new) => (new, true),
None => (node, false),
}
} else if *opcode == OpCode::Concat && args.len() >= 2 {
match try_fold_concat(*id, args) {
Some(new) => (new, true),
None => (node, false),
}
} else {
(node, false)
}
}
_ => (node, false),
}
}
fn is_commutative(opcode: &OpCode) -> bool {
matches!(opcode, OpCode::Add | OpCode::Multiply)
}
fn try_partial_fold(
outer_id: crate::node::NodeId,
opcode: OpCode,
args: &[CompiledNode],
engine: &Engine,
) -> Option<CompiledNode> {
let static_count = args.iter().filter(|a| node_is_static(a)).count();
if static_count < 2 || static_count == args.len() {
return None;
}
let mut static_args: Vec<CompiledNode> = Vec::with_capacity(static_count);
let mut dynamic_args: Vec<CompiledNode> = Vec::with_capacity(args.len() - static_count);
for arg in args {
if node_is_static(arg) {
static_args.push(arg.clone());
} else {
dynamic_args.push(arg.clone());
}
}
let static_node = CompiledNode::BuiltinOperator {
id: SYNTHETIC_ID,
opcode,
args: static_args.into_boxed_slice(),
predicate_hint: None,
iter_arg_kind: crate::operators::array::IterArgKind::General,
};
let folded_value = fold_static_node(&static_node, engine)?;
let mut new_args = Vec::with_capacity(1 + dynamic_args.len());
new_args.push(CompiledNode::synthetic_value(folded_value));
new_args.extend(dynamic_args);
Some(CompiledNode::BuiltinOperator {
id: outer_id,
opcode,
args: new_args.into_boxed_slice(),
predicate_hint: None,
iter_arg_kind: crate::operators::array::IterArgKind::General,
})
}
fn try_fold_concat(outer_id: crate::node::NodeId, args: &[CompiledNode]) -> Option<CompiledNode> {
let has_adjacent_strings = args.windows(2).any(|w| {
matches!(
(&w[0], &w[1]),
(
CompiledNode::Value {
value: OwnedDataValue::String(_),
..
},
CompiledNode::Value {
value: OwnedDataValue::String(_),
..
},
)
)
});
if !has_adjacent_strings {
return None;
}
let mut new_args: Vec<CompiledNode> = Vec::new();
let mut current_static_str: Option<String> = None;
let mut folded_any = false;
for arg in args {
if let CompiledNode::Value {
value: OwnedDataValue::String(s),
..
} = arg
{
match &mut current_static_str {
Some(accumulated) => {
accumulated.push_str(s);
folded_any = true;
}
None => {
current_static_str = Some(s.clone());
}
}
} else {
if let Some(s) = current_static_str.take() {
new_args.push(CompiledNode::synthetic_value(OwnedDataValue::String(s)));
}
new_args.push(arg.clone());
}
}
if let Some(s) = current_static_str.take() {
new_args.push(CompiledNode::synthetic_value(OwnedDataValue::String(s)));
}
if !folded_any {
return None;
}
if new_args.len() == 1 {
return Some(new_args.into_iter().next().unwrap());
}
Some(CompiledNode::BuiltinOperator {
id: outer_id,
opcode: OpCode::Concat,
args: new_args.into_boxed_slice(),
predicate_hint: None,
iter_arg_kind: crate::operators::array::IterArgKind::General,
})
}
pub(crate) fn fold_static_node(node: &CompiledNode, engine: &Engine) -> Option<OwnedDataValue> {
let arena = bumpalo::Bump::new();
let null_root: &crate::arena::DataValue<'_> = arena.alloc(crate::arena::DataValue::Null);
let mut ctx = crate::arena::ContextStack::new(null_root);
let av = engine.dispatch_node(node, &mut ctx, &arena).ok()?;
Some(av.to_owned())
}
#[cfg(test)]
mod tests {
use super::super::test_helpers::{builtin, val, var_node};
use super::*;
use datavalue::OwnedDataValue;
fn ov(s: &str) -> OwnedDataValue {
OwnedDataValue::from_json(s).unwrap()
}
#[test]
fn test_partial_fold_add() {
let engine = Engine::new();
let node = builtin(
OpCode::Add,
vec![val(ov("1")), val(ov("2")), var_node("x"), val(ov("3"))],
);
let (result, _changed) = fold(node, &engine);
if let CompiledNode::BuiltinOperator { args, .. } = &result {
assert_eq!(args.len(), 2);
if let CompiledNode::Value { value, .. } = &args[0] {
assert_eq!(value.as_i64(), Some(6));
} else {
panic!("expected folded value");
}
} else {
panic!("expected BuiltinOperator");
}
}
#[test]
fn test_fold_cat_adjacent() {
let engine = Engine::new();
let node = builtin(
OpCode::Concat,
vec![val(ov("\"hello \"")), val(ov("\"world\"")), var_node("x")],
);
let (result, _changed) = fold(node, &engine);
if let CompiledNode::BuiltinOperator { args, .. } = &result {
assert_eq!(args.len(), 2);
if let CompiledNode::Value { value, .. } = &args[0] {
assert_eq!(value.as_str(), Some("hello world"));
}
}
}
#[test]
fn numeric_strings_are_not_precoerced() {
let engine = Engine::new();
let node = builtin(OpCode::Add, vec![val(ov("\"5\"")), var_node("x")]);
let (result, changed) = fold(node, &engine);
assert!(!changed);
if let CompiledNode::BuiltinOperator { args, .. } = &result {
assert!(matches!(
&args[0],
CompiledNode::Value {
value: OwnedDataValue::String(_),
..
}
));
} else {
panic!("expected BuiltinOperator");
}
}
}