diff --git a/core/engine/src/compiled/mod.rs b/core/engine/src/compiled/mod.rs index c3e7944c..cdc51aa1 100644 --- a/core/engine/src/compiled/mod.rs +++ b/core/engine/src/compiled/mod.rs @@ -5,6 +5,8 @@ pub(crate) mod policy; pub(crate) mod row; pub(crate) mod call; pub(crate) mod traced; +mod state; +mod step; pub use columnar::{ColumnarOutput, OutputColumn, RecordsView}; mod schedule; mod table; @@ -15,105 +17,22 @@ mod schema; mod shred; mod leaf; -use crate::decision_graph::cleaner::VariableCleaner; -use crate::decision_graph::graph::{DecisionGraph, DecisionGraphResponse}; -use crate::decision_graph::schema_dict; -use crate::decision_graph::walker::{GraphWalker, StableDiDecisionGraph}; -use crate::loader::DynamicLoader; -use crate::model::{DecisionNodeKind, GraphContent}; -use ahash::HashMap; -use crate::nodes::custom::CustomNodeHandler; -use crate::nodes::function::v2::FunctionV2NodeHandler; -use crate::nodes::function::FunctionNodeHandler; -use crate::nodes::input::dates::DeclaredDates; -use crate::nodes::{NodeError, NodeResult}; -use crate::nodes::validator_cache::ValidatorCache; -use crate::nodes::variable_json::{Guards, VariableJson, VariableNode}; -use jsonschema::Validator; -use crate::nodes::input::InputNodeHandler; -use crate::nodes::output::OutputNodeHandler; -use crate::nodes::{NodeContextBase, NodeContextConfig, NodeHandlerExtensions}; -use crate::{DecisionGraphValidationError, EvaluationError, ZEN_CONFIG}; -use petgraph::prelude::NodeIndex; -use schedule::{End, Execute, Outcome, Replay, Segment}; +use crate::decision_graph::graph::DecisionGraphResponse; +use crate::decision_graph::walker::StableDiDecisionGraph; +use crate::model::GraphContent; +use crate::nodes::NodeHandlerExtensions; +use crate::{DecisionGraphValidationError, EvaluationError}; +use schedule::Segment; use std::cell::RefCell; use std::fmt::{Debug, Formatter}; -use std::sync::atomic::Ordering; use std::sync::{Arc, OnceLock}; use std::time::Instant; -use table::{Table, TableOut}; -use bound::{Bound, Scopes}; -use data::{Col, Data, Layer, Mask}; -use leaf::{Array, ColumnBuilder, Leaf}; -use std::rc::Rc; -use zen_types::symbol::Symbol; -use zen_expression::lane::{Binding, Columns}; -use zen_expression::lane::{LaneProgram, LaneRunner, Programs, SourceInfo}; -use zen_expression::{ExpressionKind, Scope, Variable}; -use row::Row; -use zen_types::decision::{ - DecisionNode, DecisionTableContent, ExpressionNodeContent, FunctionNodeContent, InputNodeContent, OutputNodeContent, SwitchStatementHitPolicy, - TransformAttributes, TransformExecutionMode, -}; - -enum Body { - Expression { - program: Box, - empty: Box<[bool]>, - entries: Box<[(Arc, Arc)]>, - fragments: Box<[Arc]>, - overlapping: bool, - walked: bool, - keys: Arc<[Arc]>, - }, - Table(Box), -} - -enum Hit { - Uniform(bool), - Rows(Vec), -} - -struct Transform { - input_field: Option<(LaneProgram, Arc)>, - base: Option>, - looped: bool, - output_path: Option>, - pass_through: bool, -} - -enum Kind { - Input, - Output, - Switch { - first: bool, - conditions: Box<[(Arc, Condition)]>, - }, - Node { - body: Body, - transform: Box, - }, - Host, -} - -enum Condition { - Always, - Never, - Program(Box), -} - -struct Step { - node: Arc, - kind: Kind, - nodes: bool, - statics: Option>, - root: bool, - schema: bool, - sites: Vec>>, - dynamic: bool, - eager: bool, - programs: OnceLock>, -} +use zen_expression::lane::Columns; +use zen_expression::lane::LaneRunner; +use zen_expression::Variable; +pub(crate) use state::{Nesting, Source, State}; +use state::NodeColumn; +use step::{Body, Condition, Hit, Kind, Step, Transform}; pub struct CompiledGraph { graph: StableDiDecisionGraph, @@ -189,126 +108,12 @@ impl CompiledPlan { } } -type StaticRead = (Arc, Arc, Arc); -type NodeColumn<'a> = (Arc, Leaf<'a>); - -pub(crate) struct State<'a> { - nodes: Vec>>>, - pub(crate) errors: Vec>>, - failed: usize, - results: Vec>, - pub(crate) endings: Vec<(Rc<[usize]>, Vec>)>, - nesting: Nesting<'a>, -} - -#[derive(Clone, Copy, Default)] -pub(crate) struct Nesting<'a> { - parents: Option<&'a [Option]>, - iteration: u8, -} - -pub(crate) enum Source<'a> { - Rows(&'a [Variable]), - Columns(Rc>), -} - -impl Step { - fn shared_programs(&self) -> Option<&Arc> { - self.eager.then(|| self.programs()) - } - - fn programs(&self) -> &Arc { - self.programs.get_or_init(|| { - let mut sources: Vec<(&str, ExpressionKind)> = Vec::new(); - match &self.node.kind { - DecisionNodeKind::ExpressionNode { content } => { - sources.extend( - content - .expressions - .iter() - .filter(|e| !e.key.is_empty() && !e.value.is_empty()) - .map(|e| (e.value.as_ref(), ExpressionKind::Standard)), - ); - sources.extend(content.transform_attributes.input_field.iter().map(|f| (f.as_ref(), ExpressionKind::Standard))); - } - DecisionNodeKind::DecisionTableNode { content } => { - for rule in content.rules.iter() { - for input in content.inputs.iter() { - if let Some(cell) = rule.get(&input.id).filter(|c| !c.is_empty()) { - let kind = match input.field.is_some() { - true => ExpressionKind::Unary, - false => ExpressionKind::Standard, - }; - sources.push((cell.as_ref(), kind)); - } - } - for output in content.outputs.iter() { - if let Some(cell) = rule.get(&output.id).filter(|c| !c.is_empty()) { - sources.push((cell.as_ref(), ExpressionKind::Standard)); - } - } - } - sources.extend(content.inputs.iter().filter_map(|i| i.field.as_deref()).map(|f| (f, ExpressionKind::Standard))); - sources.extend(content.transform_attributes.input_field.iter().map(|f| (f.as_ref(), ExpressionKind::Standard))); - } - DecisionNodeKind::SwitchNode { content } => { - sources.extend( - content - .statements - .iter() - .filter(|s| !s.condition.is_empty()) - .map(|s| (s.condition.as_ref(), ExpressionKind::Standard)), - ); - } - _ => {} - } - Arc::new(Programs::compile(sources)) - }) - } -} - -impl<'a> Source<'a> { - fn input(&self, rows: &Rc<[usize]>) -> Data<'a> { - match self { - Source::Rows(inputs) => Data::plain(rows.clone(), rows.iter().map(|&r| inputs[r].clone()).collect()), - Source::Columns(data) => Data::gather(std::slice::from_ref(data), rows), - } - } -} impl CompiledGraph { thread_local! { static RUNNER: RefCell = RefCell::new(LaneRunner::new()); } - fn compile(content: &GraphContent, eager: bool) -> Result { - let graph = DecisionGraph::build_graph(content) - .map_err(|e| Refusal::Invalid(Arc::from(e.to_string()), Invalid::Build(e)))?; - DecisionGraph::validate_graph(&graph) - .map_err(|e| Refusal::Invalid(Arc::from("graph fails validation"), Invalid::Validation(e)))?; - - let mut dictionaries = false; - let mut steps: Vec> = Vec::new(); - for nid in graph.node_indices() { - let node = graph[nid].clone(); - let step = Self::step(&node, &mut dictionaries, eager); - let index = nid.index(); - if steps.len() <= index { - steps.resize_with(index + 1, || None); - } - steps[index] = Some(step); - } - - let root = Arc::new(Replay::new(&graph).segment(&[])); - Ok(Self { - graph, - steps, - root, - dictionaries, - plan: OnceLock::new(), - }) - } - pub(crate) fn census() -> Vec<(String, u64, u64, u128)> { plan::census::Census::take() } @@ -334,531 +139,6 @@ impl CompiledGraph { } } - fn transform(attributes: &TransformAttributes) -> Result<(Transform, Vec>), String> { - let input_field = attributes - .input_field - .as_ref() - .map(|f| { - LaneProgram::standard(f) - .map(|p| (p, f.clone())) - .map_err(|e| e.to_string()) - }) - .transpose()?; - let looped = matches!(attributes.execution_mode, TransformExecutionMode::Loop); - let base = input_field - .as_ref() - .filter(|_| !looped) - .and_then(|(program, _)| program.path().map(Arc::from)); - Ok(( - Transform { - input_field, - base, - looped, - output_path: attributes.output_path.clone(), - pass_through: attributes.pass_through, - }, - attributes.input_field.iter().cloned().collect(), - )) - } - - fn expression_kind(content: &ExpressionNodeContent, sources: &mut Vec>) -> Result { - let entries: Box<[(Arc, Arc)]> = content - .expressions - .iter() - .filter(|e| !e.key.is_empty() && !e.value.is_empty()) - .map(|e| (e.key.clone(), e.value.clone())) - .collect(); - sources.extend(entries.iter().map(|(_, v)| v.clone())); - let opaque = entries.iter().any(|(_, v)| { - v.match_indices('$').any(|(at, _)| { - let rest = &v[at + 1..]; - !rest.starts_with("nodes") && !rest.starts_with('.') && !rest.starts_with('{') - }) - }); - let overlapping = entries.iter().any(|(key, _)| key.split('.').any(str::is_empty)) - || entries.iter().enumerate().any(|(i, (a, _))| { - entries.iter().skip(i + 1).any(|(b, _)| { - a == b - || a.strip_prefix(b.as_ref()).is_some_and(|r| r.starts_with('.')) - || b.strip_prefix(a.as_ref()).is_some_and(|r| r.starts_with('.')) - }) - }); - let walked = overlapping && entries.iter().any(|(_, source)| source.contains('$')); - let mut expanded: Vec<(Arc, Arc, Arc)> = Vec::with_capacity(entries.len()); - for (key, source) in entries.iter() { - let read = format!("$.{}", key.split('.').next().unwrap_or_default()); - let flat = !opaque && !overlapping && !entries.iter().any(|(_, v)| v.contains(read.as_str())); - match flat { - true => Self::expand(key, source, source, &mut expanded), - false => expanded.push((key.clone(), source.clone(), source.clone())), - } - } - let refs: Vec<(&str, &str)> = expanded.iter().map(|(k, _, v)| (k.as_ref(), v.as_ref())).collect(); - let program = LaneProgram::compile_many(&refs, true).map_err(|e| e.to_string())?; - let empty: Box<[bool]> = expanded.iter().map(|(_, _, fragment)| fragment.trim() == "{}").collect(); - let fragments: Box<[Arc]> = expanded.iter().map(|(_, _, fragment)| fragment.clone()).collect(); - let entries: Box<[(Arc, Arc)]> = - expanded.into_iter().map(|(key, source, _)| (key, source)).collect(); - let (transform, extra) = Self::transform(&content.transform_attributes)?; - sources.extend(extra); - Ok(Kind::Node { - transform: Box::new(transform), - body: Body::Expression { - program: Box::new(program), - keys: entries.iter().map(|(k, _)| k.clone()).collect(), - entries, - fragments, - empty, - overlapping, - walked, - }, - }) - } - - fn table_kind( - content: &DecisionTableContent, - sources: &mut Vec>, - cells: &mut Vec>, - ) -> Result { - for rule in content.rules.iter() { - for input in content.inputs.iter() { - if let Some(cell) = rule.get(&input.id) { - match input.field.is_some() { - true => cells.push(cell.clone()), - false => sources.push(cell.clone()), - } - } - } - for output in content.outputs.iter() { - sources.extend(rule.get(&output.id).cloned()); - } - } - sources.extend(content.inputs.iter().filter_map(|i| i.field.clone())); - let table = Table::compile(content)?; - let (transform, extra) = Self::transform(&content.transform_attributes)?; - sources.extend(extra); - Ok(Kind::Node { - body: Body::Table(Box::new(table)), - transform: Box::new(transform), - }) - } - - fn step(node: &Arc, dictionaries: &mut bool, eager: bool) -> Step { - let mut sources: Vec> = Vec::new(); - let mut cells: Vec> = Vec::new(); - let kind = match &node.kind { - DecisionNodeKind::InputNode { content } => { - *dictionaries |= content - .schema - .as_deref() - .is_some_and(schema_dict::schema_references_dictionary); - Kind::Input - } - DecisionNodeKind::OutputNode { content } => { - *dictionaries |= content - .schema - .as_deref() - .is_some_and(schema_dict::schema_references_dictionary); - Kind::Output - } - DecisionNodeKind::SwitchNode { content } => { - let conditions = content - .statements - .iter() - .map(|s| { - sources.push(s.condition.clone()); - let condition = match s.condition.is_empty() { - true => Condition::Always, - false => LaneProgram::standard(&s.condition) - .map(|p| Condition::Program(Box::new(p))) - .unwrap_or(Condition::Never), - }; - (s.id.clone(), condition) - }) - .collect(); - Kind::Switch { - first: matches!(content.hit_policy, SwitchStatementHitPolicy::First), - conditions, - } - } - DecisionNodeKind::ExpressionNode { .. } | DecisionNodeKind::DecisionTableNode { .. } if !eager => Kind::Host, - DecisionNodeKind::ExpressionNode { content } => { - Self::expression_kind(content, &mut sources).unwrap_or(Kind::Host) - } - DecisionNodeKind::DecisionTableNode { content } => { - Self::table_kind(content, &mut sources, &mut cells).unwrap_or(Kind::Host) - } - DecisionNodeKind::FunctionNode { .. } - | DecisionNodeKind::DecisionNode { .. } - | DecisionNodeKind::CustomNode { .. } => Kind::Host, - }; - let mut kind = kind; - if let Kind::Node { body, transform } = &mut kind { - if let Some(base) = transform.base.clone() { - let mappable = match &*body { - Body::Expression { program, .. } => program - .program() - .site_keys - .iter() - .flatten() - .all(|key| Self::rebased(&base, key).is_some()), - Body::Table(_) => false, - }; - if !mappable { - transform.base = None; - } - } - } - let mut programs: Vec<&LaneProgram> = Vec::new(); - match &kind { - Kind::Switch { conditions, .. } => programs.extend(conditions.iter().filter_map(|(_, c)| match c { - Condition::Program(p) => Some(p.as_ref()), - _ => None, - })), - Kind::Node { body, transform } => { - match body { - Body::Expression { program, .. } => programs.push(program), - Body::Table(table) => programs.extend(table.programs()), - } - programs.extend(transform.input_field.iter().map(|(p, _)| p)); - } - Kind::Input | Kind::Output | Kind::Host => {} - } - let base = match &kind { - Kind::Node { transform, .. } => transform.base.clone(), - _ => None, - }; - let input_sites = match &kind { - Kind::Node { transform, .. } => transform.input_field.iter().count(), - _ => 0, - }; - let body_programs = programs.len() - input_sites; - let programs = match base { - Some(_) => &programs[..body_programs], - None => &programs[..], - }; - let sites: Vec>> = programs - .iter() - .enumerate() - .flat_map(|(index, p)| { - let base = base.as_deref().filter(|_| index < body_programs); - p.program().site_keys.iter().map(move |k| { - k.as_deref().map(|key| match base { - Some(base) => Self::rebased(base, key).unwrap_or_else(|| Arc::from(key)), - None => Arc::from(key), - }) - }) - }) - .collect(); - let dynamic = programs - .iter() - .any(|p| p.program().writes_env || p.program().chain || p.program().opaque()); - let schema = match &node.kind { - DecisionNodeKind::InputNode { content } => content.schema.is_some(), - DecisionNodeKind::OutputNode { content } => content.schema.is_some(), - _ => false, - }; - Step { - node: node.clone(), - eager, - programs: OnceLock::new(), - statics: match &kind { - Kind::Host => None, - Kind::Node { body: Body::Table(_), .. } => None, - _ if sources.iter().any(|s| SourceInfo::reads_root(s)) => None, - _ => Self::statics(&sites), - }, - nodes: matches!(kind, Kind::Host) - || sources.iter().any(|s| SourceInfo::reads_nodes(s)) - || cells.iter().any(|s| SourceInfo::reads_nodes_unary(s)), - root: match &kind { - Kind::Node { - body: Body::Expression { program, .. }, - transform, - } => { - program.program().keys.iter().any(|k| k == "$" || k.starts_with("$.")) - || transform.input_field.as_ref().is_some_and(|(_, source)| SourceInfo::reads_root(source)) - } - _ => { - sources.iter().any(|s| SourceInfo::reads_root(s)) - || cells.iter().any(|s| SourceInfo::reads_root_unary(s)) - } - }, - schema, - sites, - dynamic, - kind, - } - } - - fn child(&self, segment: &Segment, outcome: Outcome) -> Arc { - let End::Switch { children, .. } = &segment.end else { - return self.root.clone(); - }; - if let Some(found) = children.read().ok().and_then(|c| c.get(&outcome).cloned()) { - return found; - } - let mut path = segment.path.to_vec(); - path.push(outcome.clone()); - let built = Arc::new(Replay::new(&self.graph).segment(&path)); - match children.write() { - Ok(mut map) => map.entry(outcome).or_insert(built).clone(), - Err(_) => built, - } - } - - fn expand(key: &Arc, origin: &Arc, source: &str, out: &mut Vec<(Arc, Arc, Arc)>) { - match LaneProgram::object_fields(source).filter(|fields| !fields.is_empty()) { - Some(fields) => { - for (field, fragment) in fields.into_iter().rev() { - Self::expand(&Arc::from(format!("{key}.{field}")), origin, &fragment, out); - } - } - None => out.push((key.clone(), origin.clone(), Arc::from(source))), - } - } - - fn rebased(base: &str, key: &str) -> Option> { - if key.starts_with("$nodes") { - return Some(Arc::from(key)); - } - match key.strip_prefix('$') { - Some("") => Some(Arc::from(base)), - Some(rest) if rest.starts_with('.') => Some(Arc::from(format!("{base}{rest}"))), - Some(_) => None, - None => Some(Arc::from(format!("{base}.{key}"))), - } - } - - fn statics(sites: &[Option>]) -> Option> { - if sites.iter().any(Option::is_none) { - return None; - } - let mut out: Vec = Vec::new(); - for key in sites.iter().flatten() { - if key.as_ref() == "$nodes" { - return None; - } - let Some(rest) = key.strip_prefix("$nodes.") else { - continue; - }; - let (name, path) = rest.split_once('.').unwrap_or((rest, "")); - if !out.iter().any(|(k, _, _)| k == key) { - out.push((key.clone(), Arc::from(name), Arc::from(path))); - } - } - Some(out) - } - - fn node_columns<'a>(&self, step: &Step, state: &State<'a>, visible: &[NodeIndex], rows: &Rc<[usize]>, with_nodes: bool) -> Vec> { - let Some(statics) = &step.statics else { - return Vec::new(); - }; - statics - .iter() - .map(|(key, name, path)| { - let source = visible - .iter() - .rev() - .filter(|_| with_nodes) - .find(|nid| self.step_at(**nid).is_some_and(|s| s.node.name.as_ref() == name.as_ref())) - .and_then(|nid| state.nodes.get(nid.index())) - .filter(|pieces| !pieces.is_empty()) - .map(|pieces| Data::gather(pieces, rows)); - let leaf = match source { - Some(data) if path.is_empty() => Leaf::Any(data.materialized()), - Some(data) => data.column_at(path), - None => Leaf::nulls(rows.len()), - }; - (key.clone(), leaf) - }) - .collect() - } - - fn step_at(&self, nid: NodeIndex) -> Option<&Step> { - self.steps.get(nid.index())?.as_ref() - } - - fn input_of<'a>(state: &State<'a>, parents: &[NodeIndex], rows: &Rc<[usize]>) -> Data<'a> { - let gathered: Vec> = parents - .iter() - .filter_map(|p| { - let pieces = state.nodes.get(p.index())?; - (!pieces.is_empty()).then(|| Data::gather(pieces, rows)) - }) - .collect(); - Data::merged(rows, gathered) - } - - fn nodes_of(&self, state: &State, visible: &[NodeIndex], rows: &Rc<[usize]>) -> Vec { - let columns: Vec<(Arc, Col)> = visible - .iter() - .filter_map(|nid| { - let step = self.step_at(*nid)?; - let pieces = state.nodes.get(nid.index()).filter(|pieces| !pieces.is_empty())?; - let column = Data::gather(pieces, rows).materialized(); - let column = match (&step.kind, state.nesting.parents) { - (Kind::Input, Some(parents)) => column - .iter() - .zip(rows.iter()) - .map(|(value, &row)| match parents.get(row).cloned().flatten() { - Some(parent) => { - let view = value.depth_clone(1); - view.dot_insert(Variable::nodes_key().as_ref(), parent); - view - } - None => value.clone(), - }) - .collect(), - _ => column, - }; - Some((step.node.name.clone(), column)) - }) - .collect(); - (0..rows.len()) - .map(|row| { - let object = Variable::empty_object(); - if let Variable::Object(map) = &object { - let mut map = map.borrow_mut(); - for (name, column) in &columns { - map.insert(name.as_ref().into(), column[row].clone()); - } - } - object - }) - .collect() - } - - fn scope(input: &Variable, nodes: Option<&Variable>) -> Scope { - let mut scope = Scope::new(input.clone()); - if let Some(nodes) = nodes { - scope.set_local(Variable::nodes_key(), nodes.clone()); - } - scope - } - - fn fault(state: &mut State, row: usize, error: NodeError) { - state.failed += 1; - state.errors[row] = Some(Box::new(EvaluationError::NodeError { - node_id: error.node_id, - source: error.source, - trace: None, - })); - } - - fn fail(state: &mut State, row: usize, node: &DecisionNode, message: String) { - state.failed += 1; - state.errors[row] = Some(Box::new(EvaluationError::NodeError { - node_id: node.id.clone(), - source: message.into(), - trace: None, - })); - } - - fn alive(state: &State, rows: &Rc<[usize]>) -> Rc<[usize]> { - if state.failed == 0 { - return rows.clone(); - } - match rows.iter().all(|&r| state.errors[r].is_none()) { - true => rows.clone(), - false => rows.iter().copied().filter(|&r| state.errors[r].is_none()).collect(), - } - } - - pub(crate) async fn run<'a>( - &self, - content: &Arc, - extensions: &NodeHandlerExtensions, - max_depth: u8, - source: &Source<'a>, - count: usize, - columnar: bool, - nesting: Nesting<'a>, - ) -> State<'a> { - let mut state = State { - nodes: vec![Vec::new(); self.steps.len()], - errors: (0..count).map(|_| None).collect(), - results: vec![None; count], - endings: Vec::new(), - failed: 0, - nesting, - }; - if nesting.iteration >= max_depth { - for error in state.errors.iter_mut() { - *error = Some(Box::new(EvaluationError::DepthLimitExceeded)); - } - return state; - } - let with_nodes = ZEN_CONFIG.nodes_in_context.load(Ordering::Relaxed); - let mut stack: Vec<(Arc, Rc<[usize]>)> = vec![(self.root.clone(), (0..count).collect())]; - while let Some((segment, mut rows)) = stack.pop() { - for event in segment.events.iter() { - rows = Self::alive(&state, &rows); - if rows.is_empty() { - break; - } - match self.step_at(event.node).filter(|step| Self::immediate(step)) { - Some(step) => self.execute_now(step, event, &rows, source, &mut state, with_nodes), - None => { - self.execute(event, &rows, source, &mut state, content, extensions, with_nodes, max_depth) - .await - } - } - } - rows = Self::alive(&state, &rows); - if rows.is_empty() { - continue; - } - match &segment.end { - End::Finish(ending) if columnar => { - let datas: Vec> = ending - .iter() - .filter_map(|nid| { - let pieces = state.nodes.get(nid.index())?; - (!pieces.is_empty()).then(|| Data::gather(pieces, &rows)) - }) - .collect(); - state.endings.push((rows.clone(), datas)); - } - End::Finish(ending) => { - let columns: Vec= ending - .iter() - .filter_map(|nid| { - let pieces = state.nodes.get(nid.index())?; - (!pieces.is_empty()).then(|| Data::gather(pieces, &rows).materialized()) - }) - .collect(); - for (i, &row) in rows.iter().enumerate() { - let result = match columns.as_slice() { - [single] => Self::ending(&single[i]), - _ => GraphWalker::merge_ending(columns.iter().map(|c| &c[i])), - }; - if state.nesting.parents.is_none() { - VariableCleaner::new().clean(&result); - } - state.results[row] = Some(result); - } - } - End::Switch { - node, - parents, - visible, - .. - } => { - let groups = self.switch(*node, parents, visible, &rows, &state, with_nodes); - for (outcome, members) in groups.into_iter().rev() { - let members: Rc<[usize]> = match members.len() == rows.len() { - true => rows.clone(), - false => members.into(), - }; - stack.push((self.child(&segment, outcome), members)); - } - } - } - } - state - } - pub(crate) async fn evaluate( &self, content: &Arc, @@ -907,1274 +187,4 @@ impl CompiledGraph { trace: None, }) } - - fn ending(value: &Variable) -> Variable { - if let Variable::Object(map) = value { - if Rc::strong_count(map) == 1 && map.borrow().shape().is_some() { - let nulls: Vec = map - .borrow() - .iter() - .filter(|(_, v)| matches!(v, Variable::Null)) - .map(|(k, _)| k.clone()) - .collect(); - let mut map = map.borrow_mut(); - for key in &nulls { - map.remove(key); - } - return value.clone(); - } - } - GraphWalker::merge_ending(std::iter::once(value)) - } - - fn node_inputs<'a>( - &self, - step: &Step, - state: &State<'a>, - visible: &[NodeIndex], - rows: &Rc<[usize]>, - with_nodes: bool, - columnar: bool, - ) -> (Vec>, Vec>) { - match (step.nodes, columnar && step.statics.is_some() && state.nesting.parents.is_none()) { - (false, _) => (Vec::new(), Vec::new()), - (true, true) => (Vec::new(), self.node_columns(step, state, visible, rows, with_nodes)), - (true, false) => match with_nodes { - true => (self.nodes_of(state, visible, rows).into_iter().map(Some).collect(), Vec::new()), - false => (Vec::new(), Vec::new()), - }, - } - } - - fn whole(step: &Step, data: &Data, rebase: Option<&str>) -> bool { - let leaves = data.leaves(); - if leaves.is_empty() { - return false; - } - (step.root && rebase.is_none()) - || step.dynamic - || leaves - .iter() - .any(|(k, _)| k.as_ref() == "$" || k.as_ref() == "$nodes" || k.starts_with("$nodes.")) - } - - fn partials<'a>(step: &Step, data: &Data<'a>) -> Vec> { - let mut out: Vec = Vec::new(); - for path in step.sites.iter().flatten() { - if data.binding(path).1 && !out.iter().any(|(p, _)| p == path) { - out.push((path.clone(), data.partial(path))); - } - } - out - } - - fn bound<'a, R>( - step: &Step, - data: &Data<'a>, - nodes: &[Option], - extra: &[NodeColumn<'a>], - rebase: Option<&str>, - f: impl FnOnce(&Bound) -> R, - ) -> R { - let with_nodes = |scope: Scope, row: usize| match nodes.get(row).cloned().flatten() { - None => scope, - Some(n) => { - let mut scope = scope; - scope.set_local(Variable::nodes_key(), n.clone()); - scope - } - }; - let root = |values: Col| -> Col { - match rebase { - None => values, - Some(base) => values - .iter() - .map(|v| Data::lookup(v, base).unwrap_or(Variable::Null)) - .collect(), - } - }; - if Self::whole(step, data, rebase) { - plan::census::Census::mark("whole"); - let whole = match rebase { - None => data.materialized(), - Some(base) => data.subtree(base), - }; - let scopes = whole - .iter() - .enumerate() - .map(|(row, v)| with_nodes(Scope::new(v.clone()), row)) - .collect(); - return f(&Bound::plain(scopes)); - } - let blank = nodes.iter().all(Option::is_none) - && matches!(&data.shape, data::Shape::Patched { base: None, .. }); - let scopes = match blank { - true => Bound::blank(data.len()), - false => Scopes::Owned( - root(data.base()) - .iter() - .enumerate() - .map(|(row, v)| with_nodes(Scope::new(v.clone()), row)) - .collect(), - ), - }; - let leaves = data.leaves(); - let mut slots: Vec> = vec![None; leaves.len()]; - let mut columns = Columns::new(data.len()); - for path in step.sites.iter().flatten() { - if let Binding::Column(index) = data.binding(path).0 { - if let (Some(slot @ None), Some((leaf, column))) = (slots.get_mut(index), leaves.get(index)) { - *slot = Some(columns.columns.len()); - columns = columns.column(leaf.as_ref(), column.column()); - } - } - } - let partials = Self::partials(step, data); - let offset = columns.columns.len(); - for (path, leaf) in extra.iter().chain(partials.iter()) { - columns = columns.column(path.as_ref(), leaf.column()); - } - let names: Vec> = extra.iter().chain(partials.iter()).map(|(p, _)| p.clone()).collect(); - let bound = Bound { - scopes, - columns, - bind: Box::new(move |key: &str| { - let rebased = rebase.and_then(|base| Self::rebased(base, key)); - let path = rebased.as_deref().unwrap_or(key); - if let Some(at) = names.iter().position(|p| p.as_ref() == path) { - return Binding::Column(offset + at); - } - match data.binding(path).0 { - Binding::Column(index) => match slots.get(index).copied().flatten() { - Some(slot) => Binding::Column(slot), - None => Binding::Row, - }, - other => other, - } - }), - }; - f(&bound) - } - - fn switch( - &self, - node: NodeIndex, - parents: &[NodeIndex], - visible: &[NodeIndex], - rows: &Rc<[usize]>, - state: &State<'_>, - with_nodes: bool, - ) -> Vec<(Outcome, Vec)> { - let Some(step @ Step { - kind: Kind::Switch { first, conditions }, - .. - }) = self.step_at(node) - else { - return vec![(Outcome::from(Vec::new()), rows.to_vec())]; - }; - let data = Self::input_of(state, parents, rows); - let (nodes, extra) = self.node_inputs(step, state, visible, rows, with_nodes, true); - let hits: Vec = Self::RUNNER.with_borrow_mut(|runner| { - Self::bound(step, &data, &nodes, &extra, None, |bound| { - conditions - .iter() - .map(|(_, condition)| match condition { - Condition::Program(program) => { - let p = program.program(); - let rows = bound.rows(); - let uniform = rows > 1 - && !p.opaque() - && p.stable() - && p.site_keys.iter().all(|key| key.as_deref().is_some_and(|key| (bound.bind)(key) == Binding::Absent)); - let mut outs = Vec::new(); - let subset = [0usize]; - bound.export(runner, program, uniform.then_some(&subset[..]), &mut outs, |_, _, _| {}); - match (outs.first_mut().map(|out| Leaf::typed(Array::from_output(out))), uniform) { - (Some(column), true) => Hit::Uniform(column.truthy(0)), - (Some(column), false) => Hit::Rows(column.truths(rows)), - (None, _) => Hit::Uniform(false), - } - } - Condition::Always => Hit::Uniform(true), - Condition::Never => Hit::Uniform(false), - }) - .collect() - }) - }); - let words = conditions.len().div_ceil(64).max(1); - let mut groups: Vec<(Vec, Vec)> = Vec::new(); - let mut key = vec![0u64; words]; - let uniform = hits.iter().all(|hit| matches!(hit, Hit::Uniform(_))); - let lanes = match uniform { - true => rows.len().min(1), - false => rows.len(), - }; - for (lane, &row) in rows.iter().enumerate().take(lanes) { - key.iter_mut().for_each(|w| *w = 0); - for (index, hit) in hits.iter().enumerate() { - let on = match hit { - Hit::Uniform(on) => *on, - Hit::Rows(truths) => truths[lane >> 6] >> (lane & 63) & 1 == 1, - }; - if on { - key[index / 64] |= 1 << (index % 64); - if *first { - break; - } - } - } - match groups.iter_mut().find(|(k, _)| *k == key) { - Some((_, members)) => members.push(row), - None => groups.push((key.clone(), vec![row])), - } - } - if let ([(_, members)], true) = (groups.as_mut_slice(), uniform) { - *members = rows.to_vec(); - } - groups - .into_iter() - .map(|(key, members)| { - let valid: Vec> = conditions - .iter() - .enumerate() - .filter(|(index, _)| key[index / 64] >> (index % 64) & 1 == 1) - .map(|(_, (id, _))| id.clone()) - .collect(); - (Outcome::from(valid), members) - }) - .collect() - } - - #[allow(clippy::too_many_arguments)] - async fn execute<'a>( - &self, - event: &Execute, - rows: &Rc<[usize]>, - source: &Source<'a>, - state: &mut State<'a>, - content: &Arc, - extensions: &NodeHandlerExtensions, - with_nodes: bool, - max_depth: u8, - ) { - let Some(step) = self.step_at(event.node) else { - return; - }; - let data = match step.kind { - Kind::Input => source.input(rows), - _ => Self::input_of(state, &event.parents, rows), - }; - let column_path = match &step.kind { - Kind::Node { body, transform } => { - (transform.input_field.is_none() || transform.base.is_some()) - && !transform.looped - && (rows.len() > 1 || matches!(source, Source::Columns(_))) - && !(transform.output_path.is_some() && matches!(body, Body::Expression { keys, .. } if keys.is_empty())) - } - _ => false, - }; - let (nodes, extra) = self.node_inputs(step, state, &event.visible, rows, with_nodes, column_path); - - if matches!(step.kind, Kind::Host) { - let materialized = data.materialized(); - let results = Self::hosted(step, &materialized, &nodes, extensions, max_depth, state.nesting.iteration, state.errors.len() > 1).await; - let mut values = Vec::with_capacity(rows.len()); - for (i, result) in results.into_iter().enumerate() { - match result { - Ok(v) => values.push(v), - Err(error) => { - Self::fault(state, rows[i], error); - values.push(Variable::Null); - } - } - } - state.nodes[event.node.index()].push(Rc::new(Data::plain(rows.clone(), values.into()))); - return; - } - - let (output, failures): (Data<'a>, Vec<(usize, String)>) = match &step.kind { - Kind::Switch { .. } | Kind::Host => (data, Vec::new()), - Kind::Output if !step.schema => (data, Vec::new()), - Kind::Input if matches!(source, Source::Columns(_)) && Self::columnar_input(step, content, extensions) => { - let failures = self.validate_input(step, &data, &nodes, content, extensions).await; - (data, failures) - } - Kind::Input if !step.schema && Self::unresolved(content, &step.node) => { - let values: Col = data - .materialized() - .iter() - .map(|value| DeclaredDates::prepare(value, None).unwrap_or_else(|| value.clone())) - .collect(); - (Data::plain(rows.clone(), values), Vec::new()) - } - Kind::Input | Kind::Output => { - let (values, failures) = self.bounded(step, &data.materialized(), &nodes, content, extensions).await; - (Data::plain(rows.clone(), values.into()), failures) - } - Kind::Node { .. } => self.node(step, rows, source, &data, &nodes, &extra), - }; - - for (i, message) in failures { - Self::fail(state, rows[i], &step.node, message); - } - state.nodes[event.node.index()].push(Rc::new(output)); - } - - async fn hosted( - step: &Step, - inputs: &[Variable], - nodes: &[Option], - extensions: &NodeHandlerExtensions, - max_depth: u8, - iteration: u8, - batched: bool, - ) -> Vec> { - if let DecisionNodeKind::DecisionNode { content } = &step.node.kind { - if let Some(results) = Self::nested(step, content, inputs, nodes, extensions, max_depth, iteration).await { - return results; - } - } - let single = iteration > 0 && inputs.len() == 1; - if let (DecisionNodeKind::FunctionNode { content: FunctionNodeContent::Version2(content) }, false) = (&step.node.kind, single) { - let config = NodeContextConfig { - max_depth, - trace: false, - ..Default::default() - }; - let inputs = inputs.iter().enumerate().map(|(i, value)| (value.clone(), nodes.get(i).cloned().flatten())).collect(); - return FunctionV2NodeHandler::batch(&step.node.id, content, extensions, &config, iteration, inputs).await; - } - let mut results = Vec::with_capacity(inputs.len()); - for (i, value) in inputs.iter().enumerate() { - let nodes = nodes.get(i).cloned().flatten(); - results.push(Self::host(step, value.clone(), nodes, extensions, max_depth, iteration, batched).await); - } - results - } - - async fn bounded( - &self, - step: &Step, - inputs: &[Variable], - nodes: &[Option], - content: &Arc, - extensions: &NodeHandlerExtensions, - ) -> (Vec, Vec<(usize, String)>) { - let validator = Self::validator(step, content, extensions); - let mut values = Vec::with_capacity(inputs.len()); - let mut failures = Vec::new(); - for (i, value) in inputs.iter().enumerate() { - let nodes = nodes.get(i).cloned().flatten(); - if let Some((validator, schema)) = &validator { - if validator.is_valid(VariableNode::new(value, &Guards::default())) { - values.push(match step.kind { - Kind::Input => DeclaredDates::prepare(value, Some(schema)).unwrap_or_else(|| value.clone()), - _ => value.clone(), - }); - continue; - } - } - match self.boundary(step, value.clone(), nodes, content, extensions).await { - Ok(v) => values.push(v), - Err(e) => { - failures.push((i, e)); - values.push(Variable::Null); - } - } - } - (values, failures) - } - - fn rowwise<'a>(&self, runner: &mut LaneRunner, step: &Step, data: Data<'a>, nodes: &[Option]) -> (Data<'a>, Vec<(usize, String)>) { - let rows: Rc<[usize]> = (0..data.len()).collect(); - let based = match &step.kind { - Kind::Node { transform, .. } if !transform.looped => transform.base.as_deref(), - _ => None, - }; - let foreign = matches!(&step.kind, Kind::Node { body: Body::Expression { walked: true, .. }, .. }) - || matches!(&data.shape, data::Shape::Plain(values) if values.iter().any(Self::foreign)) - || Self::foreign_base(&data, based); - if foreign { - plan::census::Census::mark("walked"); - let mut failures = Vec::new(); - let values: Col = data - .materialized() - .iter() - .enumerate() - .map(|(i, value)| match Self::walked(step, value.clone(), nodes.get(i).cloned().flatten(), true) { - Ok(output) => output, - Err(message) => { - failures.push((i, message)); - Variable::Null - } - }) - .collect(); - return (Data::plain(rows, values), failures); - } - self.node_with(runner, step, &rows, &Source::Rows(&[]), &data, nodes, &[]) - } - - fn node<'a>( - &self, - step: &Step, - rows: &Rc<[usize]>, - source: &Source<'a>, - data: &Data<'a>, - nodes: &[Option], - extra: &[NodeColumn<'a>], - ) -> (Data<'a>, Vec<(usize, String)>) { - Self::RUNNER.with_borrow_mut(|runner| self.node_with(runner, step, rows, source, data, nodes, extra)) - } - - #[allow(clippy::too_many_arguments)] - fn node_with<'a>( - &self, - runner: &mut LaneRunner, - step: &Step, - rows: &Rc<[usize]>, - source: &Source<'a>, - data: &Data<'a>, - nodes: &[Option], - extra: &[NodeColumn<'a>], - ) -> (Data<'a>, Vec<(usize, String)>) { - let Kind::Node { body, transform } = &step.kind else { - return (Data::plain(rows.clone(), Rc::from(Vec::new())), Vec::new()); - }; - { - let wide = rows.len() > 1 || matches!(source, Source::Columns(_)); - let rebase = match (&transform.input_field, &transform.base, transform.looped) { - (None, _, false) => Some(None), - (Some(_), Some(base), false) => Some(Some(base.as_ref())), - _ => None, - }; - let keyed = !matches!(body, Body::Expression { keys, .. } if keys.is_empty()); - let expression = matches!(body, Body::Expression { .. }); - let overlapping = matches!(body, Body::Expression { overlapping: true, .. }); - match (rebase, &transform.output_path, wide) { - (Some(rebase), None, true) if !overlapping => { - plan::census::Census::mark("layered"); - Self::bound(step, data, nodes, extra, rebase, |bound| { - Self::layered(runner, body, transform.pass_through, data, bound, None) - }) - } - (Some(rebase), Some(path), true) if keyed && expression && !overlapping => { - plan::census::Census::mark("layered-path"); - Self::bound(step, data, nodes, extra, rebase, |bound| { - Self::layered(runner, body, transform.pass_through, data, bound, Some(path)) - }) - } - (Some(None), Some(path), true) if keyed => { - plan::census::Census::mark("placed"); - Self::bound(step, data, nodes, extra, None, |bound| { - Self::placed(runner, body, path, transform.pass_through, data, bound) - }) - } - (None, Some(path), true) - if transform.looped - && !step.nodes - && transform.input_field.as_ref().is_some_and(|(program, _)| program.path().is_some()) => - { - plan::census::Census::mark("looped"); - Self::looped(runner, step, body, transform, path, data) - } - _ => { - plan::census::Census::mark("transformed"); - let materialized = data.materialized(); - let results = Self::transformed(runner, step, body, transform, &materialized, nodes); - let mut failures = Vec::new(); - let values: Col = results - .into_iter() - .enumerate() - .map(|(i, r)| match r { - Ok(v) => v, - Err(e) => { - failures.push((i, e)); - Variable::Null - } - }) - .collect(); - (Data::plain(rows.clone(), values), failures) - } - } - } - } - - fn foreign_base(data: &Data, rebase: Option<&str>) -> bool { - let Some(base) = rebase else { - return false; - }; - let column = data.column_at(base); - (0..data.len()).any(|row| Self::foreign(&column.get(row))) - } - - fn immediate(step: &Step) -> bool { - match &step.kind { - Kind::Switch { .. } | Kind::Node { .. } => true, - Kind::Output => !step.schema, - Kind::Input | Kind::Host => false, - } - } - - fn execute_now<'a>(&self, step: &Step, event: &Execute, rows: &Rc<[usize]>, source: &Source<'a>, state: &mut State<'a>, with_nodes: bool) { - let data = Self::input_of(state, &event.parents, rows); - let based = match &step.kind { - Kind::Node { transform, .. } if !transform.looped => transform.base.as_deref(), - _ => None, - }; - let foreign = matches!(&step.kind, Kind::Node { body: Body::Expression { walked: true, .. }, .. }) - || matches!(&data.shape, data::Shape::Plain(values) if values.iter().any(Self::foreign)) - || Self::foreign_base(&data, based); - let (output, failures) = match &step.kind { - Kind::Node { .. } if foreign => { - let values = data.materialized(); - let nodes: Vec> = match step.nodes && with_nodes { - true => self.nodes_of(state, &event.visible, rows).into_iter().map(Some).collect(), - false => Vec::new(), - }; - let mut failures = Vec::new(); - let outputs: Vec = values - .iter() - .enumerate() - .map(|(i, value)| match Self::walked(step, value.clone(), nodes.get(i).cloned().flatten(), true) { - Ok(output) => output, - Err(message) => { - failures.push((i, message)); - Variable::Null - } - }) - .collect(); - (Data::plain(rows.clone(), outputs.into()), failures) - } - Kind::Node { body, transform } => { - let column_path = (transform.input_field.is_none() || transform.base.is_some()) - && !transform.looped - && (rows.len() > 1 || matches!(source, Source::Columns(_))) - && !(transform.output_path.is_some() && matches!(body, Body::Expression { keys, .. } if keys.is_empty())); - let (nodes, extra) = self.node_inputs(step, state, &event.visible, rows, with_nodes, column_path); - self.node(step, rows, source, &data, &nodes, &extra) - } - _ => (data, Vec::new()), - }; - for (i, message) in failures { - Self::fail(state, rows[i], &step.node, message); - } - state.nodes[event.node.index()].push(Rc::new(output)); - } - - async fn validate_input( - &self, - step: &Step, - data: &Data<'_>, - nodes: &[Option], - content: &Arc, - extensions: &NodeHandlerExtensions, - ) -> Vec<(usize, String)> { - if let Some(message) = Self::failure(content, &step.node) { - return (0..data.len()).map(|i| (i, message.to_string())).collect(); - } - let mut failures = Vec::new(); - let Some((validator, schema)) = Self::validator(step, content, extensions) else { - return failures; - }; - let verdicts = schema::ObjectSchema::cached(&schema).and_then(|columns| columns.verdicts(data)); - let doubtful: Vec<(usize, Option, Option)> = match &verdicts { - Some(verdicts) => (0..data.len()) - .filter(|&i| !verdicts.sure[i]) - .map(|i| match verdicts.missing.get(i).copied().flatten() { - Some(key) => (i, None, Some(key)), - None => (i, Some(data.materialize_row(i)), None), - }) - .collect(), - None => data.materialized().iter().cloned().enumerate().map(|(i, value)| (i, Some(value), None)).collect(), - }; - let mut memo: Vec<(u64, Option)> = Vec::new(); - for (i, value, key) in doubtful { - if let Some((_, known)) = key.and_then(|key| memo.iter().find(|(k, _)| *k == key)) { - if let Some(e) = known { - failures.push((i, e.clone())); - } - continue; - } - let value = match value { - Some(value) if validator.is_valid(VariableNode::new(&value, &Guards::default())) => continue, - Some(value) => value, - None => data.materialize_row(i), - }; - let nodes = nodes.get(i).cloned().flatten(); - let failure = self.boundary(step, value, nodes, content, extensions).await.err(); - if let Some(key) = key.filter(|_| memo.len() < 64) { - memo.push((key, failure.clone())); - } - if let Some(e) = failure { - failures.push((i, e)); - } - } - failures - } - - fn placed<'a>( - runner: &mut LaneRunner, - body: &Body, - path: &Arc, - pass_through: bool, - input: &Data<'a>, - bound: &Bound, - ) -> (Data<'a>, Vec<(usize, String)>) { - let count = input.rows.len(); - let mut failures = Vec::new(); - let values: Vec = match body { - Body::Expression { program, entries, keys, .. } => { - let mut values = vec![Variable::Null; count]; - bound.many(runner, program, None, |row, result| match result { - Ok(outputs) => { - let object = Variable::empty_object(); - for (key, value) in keys.iter().zip(outputs) { - object.dot_insert(key, value); - } - values[row] = object; - } - Err((stage, _)) => failures.push(( - row, - format!( - r#"Failed to evaluate expression: "{}""#, - entries.get(stage).map(|(_, v)| v.as_ref()).unwrap_or_default() - ), - )), - }); - values - } - Body::Table(table) => table - .evaluate(runner, bound) - .into_iter() - .map(|out| table.value(out)) - .collect(), - }; - let layer = Layer::new(Arc::from([path.clone()]), vec![Leaf::Any(values.into())], None); - let data = match pass_through { - true => input.layered(layer), - false => Data::record(&input.rows, layer, None), - }; - (data, failures) - } - - fn layered<'a>( - runner: &mut LaneRunner, - body: &Body, - pass_through: bool, - input: &Data<'a>, - bound: &Bound, - prefix: Option<&Arc>, - ) -> (Data<'a>, Vec<(usize, String)>) { - let rows = input.rows.clone(); - let count = rows.len(); - let mut failures = Vec::new(); - match body { - Body::Expression { program, entries, keys, .. } => { - let mut outs = Vec::new(); - bound.export(runner, program, None, &mut outs, |row, stage, _| { - failures.push(( - row, - format!( - r#"Failed to evaluate expression: "{}""#, - entries.get(stage).map(|(_, v)| v.as_ref()).unwrap_or_default() - ), - )) - }); - let keys = match prefix { - None => keys.clone(), - Some(prefix) => keys.iter().map(|key| Arc::from(format!("{prefix}.{key}"))).collect(), - }; - let layer = Layer::new( - keys, - outs.iter_mut().map(|out| Leaf::typed(Array::from_output(out))).collect(), - None, - ); - let data = match pass_through { - true => input.layered(layer), - false => Data::record(&rows, layer, None), - }; - (data, failures) - } - Body::Table(table) if table.first_hit() => { - let (columns, present, unmatched) = table.columns(table.first(runner, bound)); - let layer = Layer::new(table.paths().clone(), columns, Some(present)); - let data = match pass_through { - true => input.layered(layer), - false => Data::record(&rows, layer, Some(unmatched.into())), - }; - (data, failures) - } - Body::Table(table) if table.collects() => { - let collected = table.first_collect(runner, bound); - let layer = Layer::new(collected.paths, collected.columns, Some(collected.present)); - let data = match pass_through { - true => input.layered(layer), - false => Data::record(&rows, layer, Some(collected.unmatched.into())), - }; - (data, failures) - } - Body::Table(table) => { - let outs = table.evaluate(runner, bound); - let first = outs.iter().all(|o| matches!(o, TableOut::Leaves(..) | TableOut::Value(Variable::Null))); - match (pass_through, first) { - (true, true) => { - let paths = table.paths(); - let mut columns: Vec> = (0..paths.len()).map(|_| vec![Variable::Null; count]).collect(); - let mut present: Vec> = (0..paths.len()).map(|_| vec![false; count]).collect(); - for (row, out) in outs.into_iter().enumerate() { - if let TableOut::Leaves(slots, values) = out { - for (&slot, value) in slots.iter().zip(values) { - columns[slot][row] = value; - present[slot][row] = true; - } - } - } - let layer = Layer::new( - paths.clone(), - columns.into_iter().map(|c| Leaf::Any(Col::from(c))).collect(), - Some( - present - .into_iter() - .map(|p| Mask::Bits(leaf::Bits::of(count, |row| p[row]).into())) - .collect(), - ), - ); - (input.layered(layer), failures) - } - (true, false) => { - let materialized = input.materialized(); - let values: Col = outs - .into_iter() - .enumerate() - .map(|(row, out)| match table.value(out) { - output @ Variable::Object(_) => materialized[row].clone().merge_clone(&output), - output @ Variable::Array(_) => output, - _ => materialized[row].clone(), - }) - .collect(); - (Data::plain(rows, values), failures) - } - (false, _) => ( - Data::plain(rows, outs.into_iter().map(|o| table.value(o)).collect()), - failures, - ), - } - } - } - } - - async fn host( - step: &Step, - input: Variable, - nodes: Option, - extensions: &NodeHandlerExtensions, - max_depth: u8, - iteration: u8, - batched: bool, - ) -> Result { - let node = &step.node; - let extensions = match &node.kind { - DecisionNodeKind::DecisionNode { .. } if batched => NodeHandlerExtensions { - function_runtime: Default::default(), - ..extensions.clone() - }, - _ => extensions.clone(), - }; - let base = NodeContextBase { - id: node.id.clone(), - name: node.name.clone(), - input, - nodes, - extensions, - iteration, - trace: None, - config: NodeContextConfig { - max_depth, - trace: false, - ..Default::default() - }, - }; - let response = match &node.kind { - DecisionNodeKind::FunctionNode { content } => { - DecisionGraph::handle(base, content.clone(), FunctionNodeHandler).await - } - DecisionNodeKind::DecisionNode { content } => { - let call = call::Call { - key: &content.key, - extensions: &base.extensions, - max_depth, - iteration, - }; - let row = Row { - id: &node.id, - input: base.input.clone(), - nodes: base.nodes.clone(), - programs: None, - trace: false, - }; - call.node(row, &content.transform_attributes).await - } - DecisionNodeKind::CustomNode { content } => { - DecisionGraph::handle(base, content.clone(), CustomNodeHandler).await - } - DecisionNodeKind::ExpressionNode { .. } | DecisionNodeKind::DecisionTableNode { .. } => { - let index = base.extensions.dt_indexes.as_ref().and_then(|indexes| indexes.get(&node.id)); - let row = Row { - id: &node.id, - input: base.input.clone(), - nodes: base.nodes.clone(), - programs: step.shared_programs(), - trace: false, - }; - row.node(node, index, true).await - } - _ => return Ok(base.input), - }; - response.map(|r| r.output) - } - - fn validator( - step: &Step, - content: &GraphContent, - extensions: &NodeHandlerExtensions, - ) -> Option<(Arc>, Arc)> { - let node = &step.node; - if Self::failure(content, node).is_some() { - return None; - } - let (schema, salt) = match content.resolved_schemas.as_ref().and_then(|r| r.get(&node.id)) { - Some((schema, salt)) => (schema.clone(), *salt), - None => match &node.kind { - DecisionNodeKind::InputNode { content } => (content.schema.clone()?, 0), - DecisionNodeKind::OutputNode { content } => (content.schema.clone()?, 0), - _ => return None, - }, - }; - let key = ValidatorCache::key(&node.id, &node.name, salt); - let validator = extensions.validator_cache().get_or_insert(key, &schema).ok()?; - Some((validator, schema)) - } - - fn failure<'c>(content: &'c GraphContent, node: &DecisionNode) -> Option<&'c Arc> { - content.schema_failures.as_ref()?.get(&node.id) - } - - pub(crate) async fn schemas(&self, content: &Arc, loader: &DynamicLoader) -> Arc { - if !self.dictionaries || content.resolved_schemas.is_some() { - return content.clone(); - } - let mut resolved = HashMap::default(); - let mut failures = HashMap::default(); - for node in &content.nodes { - let schema = match &node.kind { - DecisionNodeKind::InputNode { content } => content.schema.as_deref(), - DecisionNodeKind::OutputNode { content } => content.schema.as_deref(), - _ => continue, - }; - match content.validation_schema(loader, &node.id, schema).await { - Ok(Some(schema)) => { - resolved.insert(node.id.clone(), schema); - } - Ok(None) => {} - Err(message) => { - failures.insert(node.id.clone(), Arc::from(message)); - } - } - } - let mut owned = (**content).clone(); - owned.resolved_schemas = Some(Arc::new(resolved)); - owned.schema_failures = Some(Arc::new(failures)); - Arc::new(owned) - } - - fn columnar_input(step: &Step, content: &GraphContent, extensions: &NodeHandlerExtensions) -> bool { - match Self::validator(step, content, extensions) { - None => !step.schema && Self::unresolved(content, &step.node), - Some((_, schema)) => !DeclaredDates::mentions(&schema), - } - } - - fn unresolved(content: &GraphContent, node: &DecisionNode) -> bool { - content - .resolved_schemas - .as_ref() - .is_none_or(|resolved| !resolved.contains_key(&node.id)) - } - - async fn boundary( - &self, - step: &Step, - input: Variable, - nodes: Option, - content: &Arc, - extensions: &NodeHandlerExtensions, - ) -> Result { - let node = &step.node; - let base = NodeContextBase { - id: node.id.clone(), - name: node.name.clone(), - input, - nodes, - extensions: extensions.clone(), - iteration: 0, - trace: None, - config: NodeContextConfig::default(), - }; - self.edge(step, base, content) - .await - .map(|r| r.output) - .map_err(|e| e.source.to_string()) - } - - async fn edge(&self, step: &Step, mut base: NodeContextBase, content: &Arc) -> NodeResult { - let node = &step.node; - if let Some(message) = Self::failure(content, node) { - return base.error(message.to_string()); - } - let resolved = content - .resolved_schemas - .as_ref() - .and_then(|r| r.get(&node.id).cloned()); - if let Some((_, salt)) = &resolved { - base.config.validation_salt = *salt; - } - match &node.kind { - DecisionNodeKind::InputNode { content } => { - let content = match resolved { - Some((schema, _)) => InputNodeContent { schema: Some(schema) }, - None => content.clone(), - }; - DecisionGraph::handle(base, content, InputNodeHandler).await - } - DecisionNodeKind::OutputNode { content } => { - let content = match resolved { - Some((schema, _)) => OutputNodeContent { schema: Some(schema) }, - None => content.clone(), - }; - DecisionGraph::handle(base, content, OutputNodeHandler).await - } - _ => base.success(base.input.clone()), - } - } - - fn transformed( - runner: &mut LaneRunner, - step: &Step, - body: &Body, - transform: &Transform, - inputs: &[Variable], - nodes: &[Option], - ) -> Vec> { - let effective: Vec> = match &transform.input_field { - None => inputs.iter().cloned().map(Ok).collect(), - Some((program, _)) => { - let scopes: Vec = inputs - .iter() - .enumerate() - .map(|(row, i)| Self::scope(i, nodes.get(row).and_then(Option::as_ref))) - .collect(); - runner - .evaluate(program, &scopes) - .into_iter() - .map(|r| r.map_err(|e| format!("Failed to evaluate expression: {e}"))) - .collect() - } - }; - - let mut outputs: Vec> = match transform.looped { - false => { - let rows: Vec = (0..inputs.len()).filter(|&r| effective[r].is_ok()).collect(); - let body_inputs: Vec = rows - .iter() - .filter_map(|&r| effective[r].as_ref().ok().cloned()) - .collect(); - let body_nodes: Vec> = rows.iter().map(|&r| nodes.get(r).cloned().flatten()).collect(); - let mut produced = Self::items(runner, step, body, &body_inputs, &body_nodes).into_iter(); - effective - .into_iter() - .map(|e| e.and_then(|_| produced.next().unwrap_or_else(|| Ok(Variable::Null)))) - .collect() - } - true => { - let mut owners: Vec = Vec::new(); - let mut items: Vec = Vec::new(); - let mut item_nodes: Vec> = Vec::new(); - let mut shaped: Vec> = Vec::with_capacity(inputs.len()); - for (row, value) in effective.iter().enumerate() { - match value { - Err(e) => shaped.push(Err(e.clone())), - Ok(value) => match value.as_array() { - None => shaped.push(Err("Expected an array".to_string())), - Some(array) => { - let array = array.borrow(); - shaped.push(Ok(array.len())); - for item in array.iter() { - owners.push(row); - items.push(item.clone()); - item_nodes.push(nodes.get(row).cloned().flatten()); - } - } - }, - } - } - let mut produced = Self::items(runner, step, body, &items, &item_nodes).into_iter(); - let mut item_inputs = items.into_iter(); - shaped - .into_iter() - .map(|shape| { - let count = shape?; - let mut collected = Vec::with_capacity(count); - let mut failure: Option = None; - for _ in 0..count { - let output = produced.next().unwrap_or_else(|| Ok(Variable::Null)); - let item = item_inputs.next().unwrap_or(Variable::Null); - match (output, failure.is_some()) { - (_, true) => {} - (Err(e), false) => failure = Some(e), - (Ok(out), false) => collected.push(match transform.pass_through { - true => item.clone().merge_clone(&out), - false => out, - }), - } - } - match failure { - Some(e) => Err(e), - None => Ok(Variable::from_array(collected)), - } - }) - .collect() - } - }; - - for (row, output) in outputs.iter_mut().enumerate() { - let Ok(value) = output else { - continue; - }; - if let Some(path) = &transform.output_path { - let wrapped = Variable::empty_object(); - wrapped.dot_insert(path, value.clone()); - *value = wrapped; - } - if transform.pass_through { - let mut node_input = inputs[row].clone(); - *value = node_input.merge_clone(value); - } - } - outputs - } - - fn looped<'a>( - runner: &mut LaneRunner, - step: &Step, - body: &Body, - transform: &Transform, - path: &Arc, - data: &Data<'a>, - ) -> (Data<'a>, Vec<(usize, String)>) { - let source = transform - .input_field - .as_ref() - .and_then(|(program, _)| program.path()) - .unwrap_or_default(); - let lists = data.subtree(source); - let mut items: Vec = Vec::new(); - let mut spans: Vec> = Vec::with_capacity(lists.len()); - let mut failures = Vec::new(); - for (row, list) in lists.iter().enumerate() { - match list.as_array() { - Some(array) => { - let start = items.len(); - items.extend(array.borrow().iter().cloned()); - spans.push(Some((start, items.len()))); - } - None => { - failures.push((row, "Expected an array".to_string())); - spans.push(None); - } - } - } - let produced = Self::items(runner, step, body, &items, &[]); - let mut values = Vec::with_capacity(spans.len()); - for (row, span) in spans.into_iter().enumerate() { - let Some((start, end)) = span else { - values.push(Variable::Null); - continue; - }; - let mut collected = Vec::with_capacity(end - start); - let mut failure = None; - for (item, output) in items[start..end].iter().zip(&produced[start..end]) { - match output { - Ok(out) => collected.push(match transform.pass_through { - true => item.clone().merge_clone(out), - false => out.clone(), - }), - Err(e) => { - failure = Some(e.clone()); - break; - } - } - } - match failure { - Some(e) => { - failures.push((row, e)); - values.push(Variable::Null); - } - None => values.push(Variable::from_array(collected)), - } - } - let layer = Layer::new(Arc::from([path.clone()]), vec![Leaf::Any(values.into())], None); - let output = match transform.pass_through { - true => data.layered(layer), - false => Data::record(&data.rows, layer, None), - }; - (output, failures) - } - - fn items( - runner: &mut LaneRunner, - step: &Step, - body: &Body, - inputs: &[Variable], - nodes: &[Option], - ) -> Vec> { - if inputs.iter().any(Self::foreign) { - let kept: Vec = (0..inputs.len()).filter(|&i| !Self::foreign(&inputs[i])).collect(); - let kept_inputs: Vec = kept.iter().map(|&i| inputs[i].clone()).collect(); - let kept_nodes: Vec> = kept.iter().map(|&i| nodes.get(i).cloned().flatten()).collect(); - let mut produced = Self::items(runner, step, body, &kept_inputs, &kept_nodes).into_iter(); - return inputs - .iter() - .enumerate() - .map(|(i, input)| match Self::foreign(input) { - true => Self::walked(step, input.clone(), nodes.get(i).cloned().flatten(), false), - false => produced.next().unwrap_or(Ok(Variable::Null)), - }) - .collect(); - } - match Self::item_data(step, body, inputs, nodes) { - Some(data) => { - let (output, failures) = Self::item_run(runner, step, body, &data); - let values = output.materialized(); - let mut results: Vec> = values.iter().cloned().map(Ok).collect(); - for (item, message) in failures.into_iter().rev() { - if let Some(result) = results.get_mut(item) { - *result = Err(message); - } - } - results - } - None => Self::body(runner, body, inputs, nodes), - } - } - - fn foreign(value: &Variable) -> bool { - !matches!(value, Variable::Null | Variable::Object(_)) - } - - fn walked(step: &Step, input: Variable, nodes: Option, transforms: bool) -> Result { - let row = Row { - id: &step.node.id, - input, - nodes, - programs: step.shared_programs(), - trace: false, - }; - row.now(&step.node, None, transforms) - .map(|response| response.output) - .map_err(|error| error.source.to_string()) - } - - fn item_keys(step: &Step, body: &Body) -> Option>> { - if step.root || step.dynamic || matches!(&step.kind, Kind::Node { transform, .. } if transform.base.is_some()) { - return None; - } - let programs: Vec<&LaneProgram> = match body { - Body::Expression { program, .. } => vec![program], - Body::Table(table) => table.programs(), - }; - let mut keys: Vec> = Vec::new(); - for program in &programs { - let unary = matches!(program.kind(), zen_expression::ExpressionKind::Unary); - for key in program.program().site_keys.iter() { - let key = key.as_deref()?; - match key.starts_with('$') { - true if unary && (key == "$" || key.starts_with("$.")) => {} - true => return None, - false if !keys.iter().any(|k| k.as_ref() == key) => keys.push(Arc::from(key)), - false => {} - } - } - } - let overlapping = keys.iter().any(|a| { - keys.iter() - .any(|b| b.strip_prefix(a.as_ref()).is_some_and(|rest| rest.starts_with('.'))) - }); - (!overlapping).then_some(keys) - } - - fn item_run<'a>(runner: &mut LaneRunner, step: &Step, body: &Body, data: &Data<'a>) -> (Data<'a>, Vec<(usize, String)>) { - Self::bound(step, data, &[], &[], None, |bound| Self::layered(runner, body, false, data, bound, None)) - } - - fn item_data<'a>(step: &Step, body: &Body, inputs: &[Variable], nodes: &[Option]) -> Option> { - if inputs.len() < 2 || nodes.iter().any(Option::is_some) { - return None; - } - let keys = Self::item_keys(step, body)?; - let mut leaves = Vec::with_capacity(keys.len()); - let mut present = Vec::with_capacity(keys.len()); - for key in &keys { - let mut builder = ColumnBuilder::with_capacity(inputs.len()); - let mut mask = Vec::with_capacity(inputs.len()); - for item in inputs { - let value = Data::lookup(item, key); - mask.push(value.is_some()); - builder.push_variable(value.unwrap_or(Variable::Null)); - } - leaves.push(Leaf::typed(builder.finish())); - present.push(Mask::Bits(leaf::Bits::of(mask.len(), |row| mask[row]).into())); - } - let rows: Rc<[usize]> = (0..inputs.len()).collect(); - Some(Data::record(&rows, Layer::new(keys.into(), leaves, Some(present)), None)) - } - - fn body( - runner: &mut LaneRunner, - body: &Body, - inputs: &[Variable], - nodes: &[Option], - ) -> Vec> { - let scopes: Vec = inputs - .iter() - .enumerate() - .map(|(row, i)| Self::scope(i, nodes.get(row).and_then(Option::as_ref))) - .collect(); - match body { - Body::Expression { program, entries, .. } => { - let mut out: Vec> = vec![Ok(Variable::Null); scopes.len()]; - runner.evaluate_many(program, &scopes, None, |row, result| { - out[row] = match result { - Some(Ok(values)) => { - let object = Variable::empty_object(); - for ((key, _), value) in entries.iter().zip(values) { - object.dot_insert(key, value); - } - Ok(object) - } - Some(Err((stage, _))) => Err(format!( - r#"Failed to evaluate expression: "{}""#, - entries.get(stage).map(|(_, v)| v.as_ref()).unwrap_or_default() - )), - None => Ok(Variable::Null), - }; - }); - out - } - Body::Table(table) => table - .evaluate(runner, &Bound::plain(scopes)) - .into_iter() - .map(|o| Ok(table.value(o))) - .collect(), - } - } } diff --git a/core/engine/src/compiled/state/exec.rs b/core/engine/src/compiled/state/exec.rs new file mode 100644 index 00000000..bab26556 --- /dev/null +++ b/core/engine/src/compiled/state/exec.rs @@ -0,0 +1,332 @@ +use crate::model::{DecisionNodeKind, GraphContent}; +use crate::nodes::function::v2::FunctionV2NodeHandler; +use crate::nodes::input::dates::DeclaredDates; +use crate::nodes::NodeError; +use crate::nodes::variable_json::{Guards, VariableNode}; +use crate::nodes::{NodeContextConfig, NodeHandlerExtensions}; +use crate::compiled::schedule::Execute; +use std::sync::Arc; +use crate::compiled::data::{Col, Data}; +use std::rc::Rc; +use zen_expression::lane::LaneRunner; +use zen_expression::Variable; +use zen_types::decision::FunctionNodeContent; +use crate::compiled::{Body, CompiledGraph, Kind, NodeColumn, Source, State, Step, data, plan}; + +impl CompiledGraph { + #[allow(clippy::too_many_arguments)] + pub(in crate::compiled) async fn execute<'a>( + &self, + event: &Execute, + rows: &Rc<[usize]>, + source: &Source<'a>, + state: &mut State<'a>, + content: &Arc, + extensions: &NodeHandlerExtensions, + with_nodes: bool, + max_depth: u8, + ) { + let Some(step) = self.step_at(event.node) else { + return; + }; + let data = match step.kind { + Kind::Input => source.input(rows), + _ => Self::input_of(state, &event.parents, rows), + }; + let column_path = match &step.kind { + Kind::Node { body, transform } => { + (transform.input_field.is_none() || transform.base.is_some()) + && !transform.looped + && (rows.len() > 1 || matches!(source, Source::Columns(_))) + && !(transform.output_path.is_some() && matches!(body, Body::Expression { keys, .. } if keys.is_empty())) + } + _ => false, + }; + let (nodes, extra) = self.node_inputs(step, state, &event.visible, rows, with_nodes, column_path); + + if matches!(step.kind, Kind::Host) { + let materialized = data.materialized(); + let results = Self::hosted(step, &materialized, &nodes, extensions, max_depth, state.nesting.iteration, state.errors.len() > 1).await; + let mut values = Vec::with_capacity(rows.len()); + for (i, result) in results.into_iter().enumerate() { + match result { + Ok(v) => values.push(v), + Err(error) => { + Self::fault(state, rows[i], error); + values.push(Variable::Null); + } + } + } + state.nodes[event.node.index()].push(Rc::new(Data::plain(rows.clone(), values.into()))); + return; + } + + let (output, failures): (Data<'a>, Vec<(usize, String)>) = match &step.kind { + Kind::Switch { .. } | Kind::Host => (data, Vec::new()), + Kind::Output if !step.schema => (data, Vec::new()), + Kind::Input if matches!(source, Source::Columns(_)) && Self::columnar_input(step, content, extensions) => { + let failures = self.validate_input(step, &data, &nodes, content, extensions).await; + (data, failures) + } + Kind::Input if !step.schema && Self::unresolved(content, &step.node) => { + let values: Col = data + .materialized() + .iter() + .map(|value| DeclaredDates::prepare(value, None).unwrap_or_else(|| value.clone())) + .collect(); + (Data::plain(rows.clone(), values), Vec::new()) + } + Kind::Input | Kind::Output => { + let (values, failures) = self.bounded(step, &data.materialized(), &nodes, content, extensions).await; + (Data::plain(rows.clone(), values.into()), failures) + } + Kind::Node { .. } => self.node(step, rows, source, &data, &nodes, &extra), + }; + + for (i, message) in failures { + Self::fail(state, rows[i], &step.node, message); + } + state.nodes[event.node.index()].push(Rc::new(output)); + } + + pub(in crate::compiled) async fn hosted( + step: &Step, + inputs: &[Variable], + nodes: &[Option], + extensions: &NodeHandlerExtensions, + max_depth: u8, + iteration: u8, + batched: bool, + ) -> Vec> { + if let DecisionNodeKind::DecisionNode { content } = &step.node.kind { + if let Some(results) = Self::nested(step, content, inputs, nodes, extensions, max_depth, iteration).await { + return results; + } + } + let single = iteration > 0 && inputs.len() == 1; + if let (DecisionNodeKind::FunctionNode { content: FunctionNodeContent::Version2(content) }, false) = (&step.node.kind, single) { + let config = NodeContextConfig { + max_depth, + trace: false, + ..Default::default() + }; + let inputs = inputs.iter().enumerate().map(|(i, value)| (value.clone(), nodes.get(i).cloned().flatten())).collect(); + return FunctionV2NodeHandler::batch(&step.node.id, content, extensions, &config, iteration, inputs).await; + } + let mut results = Vec::with_capacity(inputs.len()); + for (i, value) in inputs.iter().enumerate() { + let nodes = nodes.get(i).cloned().flatten(); + results.push(Self::host(step, value.clone(), nodes, extensions, max_depth, iteration, batched).await); + } + results + } + + pub(in crate::compiled) async fn bounded( + &self, + step: &Step, + inputs: &[Variable], + nodes: &[Option], + content: &Arc, + extensions: &NodeHandlerExtensions, + ) -> (Vec, Vec<(usize, String)>) { + let validator = Self::validator(step, content, extensions); + let mut values = Vec::with_capacity(inputs.len()); + let mut failures = Vec::new(); + for (i, value) in inputs.iter().enumerate() { + let nodes = nodes.get(i).cloned().flatten(); + if let Some((validator, schema)) = &validator { + if validator.is_valid(VariableNode::new(value, &Guards::default())) { + values.push(match step.kind { + Kind::Input => DeclaredDates::prepare(value, Some(schema)).unwrap_or_else(|| value.clone()), + _ => value.clone(), + }); + continue; + } + } + match self.boundary(step, value.clone(), nodes, content, extensions).await { + Ok(v) => values.push(v), + Err(e) => { + failures.push((i, e)); + values.push(Variable::Null); + } + } + } + (values, failures) + } + + pub(in crate::compiled) fn rowwise<'a>(&self, runner: &mut LaneRunner, step: &Step, data: Data<'a>, nodes: &[Option]) -> (Data<'a>, Vec<(usize, String)>) { + let rows: Rc<[usize]> = (0..data.len()).collect(); + let based = match &step.kind { + Kind::Node { transform, .. } if !transform.looped => transform.base.as_deref(), + _ => None, + }; + let foreign = matches!(&step.kind, Kind::Node { body: Body::Expression { walked: true, .. }, .. }) + || matches!(&data.shape, data::Shape::Plain(values) if values.iter().any(Self::foreign)) + || Self::foreign_base(&data, based); + if foreign { + plan::census::Census::mark("walked"); + let mut failures = Vec::new(); + let values: Col = data + .materialized() + .iter() + .enumerate() + .map(|(i, value)| match Self::walked(step, value.clone(), nodes.get(i).cloned().flatten(), true) { + Ok(output) => output, + Err(message) => { + failures.push((i, message)); + Variable::Null + } + }) + .collect(); + return (Data::plain(rows, values), failures); + } + self.node_with(runner, step, &rows, &Source::Rows(&[]), &data, nodes, &[]) + } + + fn node<'a>( + &self, + step: &Step, + rows: &Rc<[usize]>, + source: &Source<'a>, + data: &Data<'a>, + nodes: &[Option], + extra: &[NodeColumn<'a>], + ) -> (Data<'a>, Vec<(usize, String)>) { + Self::RUNNER.with_borrow_mut(|runner| self.node_with(runner, step, rows, source, data, nodes, extra)) + } + + #[allow(clippy::too_many_arguments)] + fn node_with<'a>( + &self, + runner: &mut LaneRunner, + step: &Step, + rows: &Rc<[usize]>, + source: &Source<'a>, + data: &Data<'a>, + nodes: &[Option], + extra: &[NodeColumn<'a>], + ) -> (Data<'a>, Vec<(usize, String)>) { + let Kind::Node { body, transform } = &step.kind else { + return (Data::plain(rows.clone(), Rc::from(Vec::new())), Vec::new()); + }; + { + let wide = rows.len() > 1 || matches!(source, Source::Columns(_)); + let rebase = match (&transform.input_field, &transform.base, transform.looped) { + (None, _, false) => Some(None), + (Some(_), Some(base), false) => Some(Some(base.as_ref())), + _ => None, + }; + let keyed = !matches!(body, Body::Expression { keys, .. } if keys.is_empty()); + let expression = matches!(body, Body::Expression { .. }); + let overlapping = matches!(body, Body::Expression { overlapping: true, .. }); + match (rebase, &transform.output_path, wide) { + (Some(rebase), None, true) if !overlapping => { + plan::census::Census::mark("layered"); + Self::bound(step, data, nodes, extra, rebase, |bound| { + Self::layered(runner, body, transform.pass_through, data, bound, None) + }) + } + (Some(rebase), Some(path), true) if keyed && expression && !overlapping => { + plan::census::Census::mark("layered-path"); + Self::bound(step, data, nodes, extra, rebase, |bound| { + Self::layered(runner, body, transform.pass_through, data, bound, Some(path)) + }) + } + (Some(None), Some(path), true) if keyed => { + plan::census::Census::mark("placed"); + Self::bound(step, data, nodes, extra, None, |bound| { + Self::placed(runner, body, path, transform.pass_through, data, bound) + }) + } + (None, Some(path), true) + if transform.looped + && !step.nodes + && transform.input_field.as_ref().is_some_and(|(program, _)| program.path().is_some()) => + { + plan::census::Census::mark("looped"); + Self::looped(runner, step, body, transform, path, data) + } + _ => { + plan::census::Census::mark("transformed"); + let materialized = data.materialized(); + let results = Self::transformed(runner, step, body, transform, &materialized, nodes); + let mut failures = Vec::new(); + let values: Col = results + .into_iter() + .enumerate() + .map(|(i, r)| match r { + Ok(v) => v, + Err(e) => { + failures.push((i, e)); + Variable::Null + } + }) + .collect(); + (Data::plain(rows.clone(), values), failures) + } + } + } + } + + fn foreign_base(data: &Data, rebase: Option<&str>) -> bool { + let Some(base) = rebase else { + return false; + }; + let column = data.column_at(base); + (0..data.len()).any(|row| Self::foreign(&column.get(row))) + } + + pub(in crate::compiled) fn immediate(step: &Step) -> bool { + match &step.kind { + Kind::Switch { .. } | Kind::Node { .. } => true, + Kind::Output => !step.schema, + Kind::Input | Kind::Host => false, + } + } + + pub(in crate::compiled) fn execute_now<'a>(&self, step: &Step, event: &Execute, rows: &Rc<[usize]>, source: &Source<'a>, state: &mut State<'a>, with_nodes: bool) { + let data = Self::input_of(state, &event.parents, rows); + let based = match &step.kind { + Kind::Node { transform, .. } if !transform.looped => transform.base.as_deref(), + _ => None, + }; + let foreign = matches!(&step.kind, Kind::Node { body: Body::Expression { walked: true, .. }, .. }) + || matches!(&data.shape, data::Shape::Plain(values) if values.iter().any(Self::foreign)) + || Self::foreign_base(&data, based); + let (output, failures) = match &step.kind { + Kind::Node { .. } if foreign => { + let values = data.materialized(); + let nodes: Vec> = match step.nodes && with_nodes { + true => self.nodes_of(state, &event.visible, rows).into_iter().map(Some).collect(), + false => Vec::new(), + }; + let mut failures = Vec::new(); + let outputs: Vec = values + .iter() + .enumerate() + .map(|(i, value)| match Self::walked(step, value.clone(), nodes.get(i).cloned().flatten(), true) { + Ok(output) => output, + Err(message) => { + failures.push((i, message)); + Variable::Null + } + }) + .collect(); + (Data::plain(rows.clone(), outputs.into()), failures) + } + Kind::Node { body, transform } => { + let column_path = (transform.input_field.is_none() || transform.base.is_some()) + && !transform.looped + && (rows.len() > 1 || matches!(source, Source::Columns(_))) + && !(transform.output_path.is_some() && matches!(body, Body::Expression { keys, .. } if keys.is_empty())); + let (nodes, extra) = self.node_inputs(step, state, &event.visible, rows, with_nodes, column_path); + self.node(step, rows, source, &data, &nodes, &extra) + } + _ => (data, Vec::new()), + }; + for (i, message) in failures { + Self::fail(state, rows[i], &step.node, message); + } + state.nodes[event.node.index()].push(Rc::new(output)); + } +} diff --git a/core/engine/src/compiled/state/host.rs b/core/engine/src/compiled/state/host.rs new file mode 100644 index 00000000..5ca51182 --- /dev/null +++ b/core/engine/src/compiled/state/host.rs @@ -0,0 +1,424 @@ +use crate::decision_graph::graph::DecisionGraph; +use crate::loader::DynamicLoader; +use crate::model::{DecisionNodeKind, GraphContent}; +use ahash::HashMap; +use crate::nodes::custom::CustomNodeHandler; +use crate::nodes::function::FunctionNodeHandler; +use crate::nodes::input::dates::DeclaredDates; +use crate::nodes::{NodeError, NodeResult}; +use crate::nodes::validator_cache::ValidatorCache; +use crate::nodes::variable_json::{Guards, VariableJson, VariableNode}; +use jsonschema::Validator; +use crate::nodes::input::InputNodeHandler; +use crate::nodes::output::OutputNodeHandler; +use crate::nodes::{NodeContextBase, NodeContextConfig, NodeHandlerExtensions}; +use std::sync::Arc; +use crate::compiled::table::TableOut; +use crate::compiled::bound::Bound; +use crate::compiled::data::{Col, Data, Layer, Mask}; +use crate::compiled::leaf::{Array, Leaf}; +use zen_expression::lane::LaneRunner; +use zen_expression::Variable; +use crate::compiled::row::Row; +use zen_types::decision::{ + DecisionNode, InputNodeContent, OutputNodeContent, +}; +use crate::compiled::{Body, CompiledGraph, Step, call, leaf, schema}; + +impl CompiledGraph { + pub(in crate::compiled) async fn validate_input( + &self, + step: &Step, + data: &Data<'_>, + nodes: &[Option], + content: &Arc, + extensions: &NodeHandlerExtensions, + ) -> Vec<(usize, String)> { + if let Some(message) = Self::failure(content, &step.node) { + return (0..data.len()).map(|i| (i, message.to_string())).collect(); + } + let mut failures = Vec::new(); + let Some((validator, schema)) = Self::validator(step, content, extensions) else { + return failures; + }; + let verdicts = schema::ObjectSchema::cached(&schema).and_then(|columns| columns.verdicts(data)); + let doubtful: Vec<(usize, Option, Option)> = match &verdicts { + Some(verdicts) => (0..data.len()) + .filter(|&i| !verdicts.sure[i]) + .map(|i| match verdicts.missing.get(i).copied().flatten() { + Some(key) => (i, None, Some(key)), + None => (i, Some(data.materialize_row(i)), None), + }) + .collect(), + None => data.materialized().iter().cloned().enumerate().map(|(i, value)| (i, Some(value), None)).collect(), + }; + let mut memo: Vec<(u64, Option)> = Vec::new(); + for (i, value, key) in doubtful { + if let Some((_, known)) = key.and_then(|key| memo.iter().find(|(k, _)| *k == key)) { + if let Some(e) = known { + failures.push((i, e.clone())); + } + continue; + } + let value = match value { + Some(value) if validator.is_valid(VariableNode::new(&value, &Guards::default())) => continue, + Some(value) => value, + None => data.materialize_row(i), + }; + let nodes = nodes.get(i).cloned().flatten(); + let failure = self.boundary(step, value, nodes, content, extensions).await.err(); + if let Some(key) = key.filter(|_| memo.len() < 64) { + memo.push((key, failure.clone())); + } + if let Some(e) = failure { + failures.push((i, e)); + } + } + failures + } + + pub(in crate::compiled) fn placed<'a>( + runner: &mut LaneRunner, + body: &Body, + path: &Arc, + pass_through: bool, + input: &Data<'a>, + bound: &Bound, + ) -> (Data<'a>, Vec<(usize, String)>) { + let count = input.rows.len(); + let mut failures = Vec::new(); + let values: Vec = match body { + Body::Expression { program, entries, keys, .. } => { + let mut values = vec![Variable::Null; count]; + bound.many(runner, program, None, |row, result| match result { + Ok(outputs) => { + let object = Variable::empty_object(); + for (key, value) in keys.iter().zip(outputs) { + object.dot_insert(key, value); + } + values[row] = object; + } + Err((stage, _)) => failures.push(( + row, + format!( + r#"Failed to evaluate expression: "{}""#, + entries.get(stage).map(|(_, v)| v.as_ref()).unwrap_or_default() + ), + )), + }); + values + } + Body::Table(table) => table + .evaluate(runner, bound) + .into_iter() + .map(|out| table.value(out)) + .collect(), + }; + let layer = Layer::new(Arc::from([path.clone()]), vec![Leaf::Any(values.into())], None); + let data = match pass_through { + true => input.layered(layer), + false => Data::record(&input.rows, layer, None), + }; + (data, failures) + } + + pub(in crate::compiled) fn layered<'a>( + runner: &mut LaneRunner, + body: &Body, + pass_through: bool, + input: &Data<'a>, + bound: &Bound, + prefix: Option<&Arc>, + ) -> (Data<'a>, Vec<(usize, String)>) { + let rows = input.rows.clone(); + let count = rows.len(); + let mut failures = Vec::new(); + match body { + Body::Expression { program, entries, keys, .. } => { + let mut outs = Vec::new(); + bound.export(runner, program, None, &mut outs, |row, stage, _| { + failures.push(( + row, + format!( + r#"Failed to evaluate expression: "{}""#, + entries.get(stage).map(|(_, v)| v.as_ref()).unwrap_or_default() + ), + )) + }); + let keys = match prefix { + None => keys.clone(), + Some(prefix) => keys.iter().map(|key| Arc::from(format!("{prefix}.{key}"))).collect(), + }; + let layer = Layer::new( + keys, + outs.iter_mut().map(|out| Leaf::typed(Array::from_output(out))).collect(), + None, + ); + let data = match pass_through { + true => input.layered(layer), + false => Data::record(&rows, layer, None), + }; + (data, failures) + } + Body::Table(table) if table.first_hit() => { + let (columns, present, unmatched) = table.columns(table.first(runner, bound)); + let layer = Layer::new(table.paths().clone(), columns, Some(present)); + let data = match pass_through { + true => input.layered(layer), + false => Data::record(&rows, layer, Some(unmatched.into())), + }; + (data, failures) + } + Body::Table(table) if table.collects() => { + let collected = table.first_collect(runner, bound); + let layer = Layer::new(collected.paths, collected.columns, Some(collected.present)); + let data = match pass_through { + true => input.layered(layer), + false => Data::record(&rows, layer, Some(collected.unmatched.into())), + }; + (data, failures) + } + Body::Table(table) => { + let outs = table.evaluate(runner, bound); + let first = outs.iter().all(|o| matches!(o, TableOut::Leaves(..) | TableOut::Value(Variable::Null))); + match (pass_through, first) { + (true, true) => { + let paths = table.paths(); + let mut columns: Vec> = (0..paths.len()).map(|_| vec![Variable::Null; count]).collect(); + let mut present: Vec> = (0..paths.len()).map(|_| vec![false; count]).collect(); + for (row, out) in outs.into_iter().enumerate() { + if let TableOut::Leaves(slots, values) = out { + for (&slot, value) in slots.iter().zip(values) { + columns[slot][row] = value; + present[slot][row] = true; + } + } + } + let layer = Layer::new( + paths.clone(), + columns.into_iter().map(|c| Leaf::Any(Col::from(c))).collect(), + Some( + present + .into_iter() + .map(|p| Mask::Bits(leaf::Bits::of(count, |row| p[row]).into())) + .collect(), + ), + ); + (input.layered(layer), failures) + } + (true, false) => { + let materialized = input.materialized(); + let values: Col = outs + .into_iter() + .enumerate() + .map(|(row, out)| match table.value(out) { + output @ Variable::Object(_) => materialized[row].clone().merge_clone(&output), + output @ Variable::Array(_) => output, + _ => materialized[row].clone(), + }) + .collect(); + (Data::plain(rows, values), failures) + } + (false, _) => ( + Data::plain(rows, outs.into_iter().map(|o| table.value(o)).collect()), + failures, + ), + } + } + } + } + + pub(in crate::compiled) async fn host( + step: &Step, + input: Variable, + nodes: Option, + extensions: &NodeHandlerExtensions, + max_depth: u8, + iteration: u8, + batched: bool, + ) -> Result { + let node = &step.node; + let extensions = match &node.kind { + DecisionNodeKind::DecisionNode { .. } if batched => NodeHandlerExtensions { + function_runtime: Default::default(), + ..extensions.clone() + }, + _ => extensions.clone(), + }; + let base = NodeContextBase { + id: node.id.clone(), + name: node.name.clone(), + input, + nodes, + extensions, + iteration, + trace: None, + config: NodeContextConfig { + max_depth, + trace: false, + ..Default::default() + }, + }; + let response = match &node.kind { + DecisionNodeKind::FunctionNode { content } => { + DecisionGraph::handle(base, content.clone(), FunctionNodeHandler).await + } + DecisionNodeKind::DecisionNode { content } => { + let call = call::Call { + key: &content.key, + extensions: &base.extensions, + max_depth, + iteration, + }; + let row = Row { + id: &node.id, + input: base.input.clone(), + nodes: base.nodes.clone(), + programs: None, + trace: false, + }; + call.node(row, &content.transform_attributes).await + } + DecisionNodeKind::CustomNode { content } => { + DecisionGraph::handle(base, content.clone(), CustomNodeHandler).await + } + DecisionNodeKind::ExpressionNode { .. } | DecisionNodeKind::DecisionTableNode { .. } => { + let index = base.extensions.dt_indexes.as_ref().and_then(|indexes| indexes.get(&node.id)); + let row = Row { + id: &node.id, + input: base.input.clone(), + nodes: base.nodes.clone(), + programs: step.shared_programs(), + trace: false, + }; + row.node(node, index, true).await + } + _ => return Ok(base.input), + }; + response.map(|r| r.output) + } + + pub(in crate::compiled) fn validator( + step: &Step, + content: &GraphContent, + extensions: &NodeHandlerExtensions, + ) -> Option<(Arc>, Arc)> { + let node = &step.node; + if Self::failure(content, node).is_some() { + return None; + } + let (schema, salt) = match content.resolved_schemas.as_ref().and_then(|r| r.get(&node.id)) { + Some((schema, salt)) => (schema.clone(), *salt), + None => match &node.kind { + DecisionNodeKind::InputNode { content } => (content.schema.clone()?, 0), + DecisionNodeKind::OutputNode { content } => (content.schema.clone()?, 0), + _ => return None, + }, + }; + let key = ValidatorCache::key(&node.id, &node.name, salt); + let validator = extensions.validator_cache().get_or_insert(key, &schema).ok()?; + Some((validator, schema)) + } + + fn failure<'c>(content: &'c GraphContent, node: &DecisionNode) -> Option<&'c Arc> { + content.schema_failures.as_ref()?.get(&node.id) + } + + pub(crate) async fn schemas(&self, content: &Arc, loader: &DynamicLoader) -> Arc { + if !self.dictionaries || content.resolved_schemas.is_some() { + return content.clone(); + } + let mut resolved = HashMap::default(); + let mut failures = HashMap::default(); + for node in &content.nodes { + let schema = match &node.kind { + DecisionNodeKind::InputNode { content } => content.schema.as_deref(), + DecisionNodeKind::OutputNode { content } => content.schema.as_deref(), + _ => continue, + }; + match content.validation_schema(loader, &node.id, schema).await { + Ok(Some(schema)) => { + resolved.insert(node.id.clone(), schema); + } + Ok(None) => {} + Err(message) => { + failures.insert(node.id.clone(), Arc::from(message)); + } + } + } + let mut owned = (**content).clone(); + owned.resolved_schemas = Some(Arc::new(resolved)); + owned.schema_failures = Some(Arc::new(failures)); + Arc::new(owned) + } + + pub(in crate::compiled) fn columnar_input(step: &Step, content: &GraphContent, extensions: &NodeHandlerExtensions) -> bool { + match Self::validator(step, content, extensions) { + None => !step.schema && Self::unresolved(content, &step.node), + Some((_, schema)) => !DeclaredDates::mentions(&schema), + } + } + + pub(in crate::compiled) fn unresolved(content: &GraphContent, node: &DecisionNode) -> bool { + content + .resolved_schemas + .as_ref() + .is_none_or(|resolved| !resolved.contains_key(&node.id)) + } + + pub(in crate::compiled) async fn boundary( + &self, + step: &Step, + input: Variable, + nodes: Option, + content: &Arc, + extensions: &NodeHandlerExtensions, + ) -> Result { + let node = &step.node; + let base = NodeContextBase { + id: node.id.clone(), + name: node.name.clone(), + input, + nodes, + extensions: extensions.clone(), + iteration: 0, + trace: None, + config: NodeContextConfig::default(), + }; + self.edge(step, base, content) + .await + .map(|r| r.output) + .map_err(|e| e.source.to_string()) + } + + pub(in crate::compiled) async fn edge(&self, step: &Step, mut base: NodeContextBase, content: &Arc) -> NodeResult { + let node = &step.node; + if let Some(message) = Self::failure(content, node) { + return base.error(message.to_string()); + } + let resolved = content + .resolved_schemas + .as_ref() + .and_then(|r| r.get(&node.id).cloned()); + if let Some((_, salt)) = &resolved { + base.config.validation_salt = *salt; + } + match &node.kind { + DecisionNodeKind::InputNode { content } => { + let content = match resolved { + Some((schema, _)) => InputNodeContent { schema: Some(schema) }, + None => content.clone(), + }; + DecisionGraph::handle(base, content, InputNodeHandler).await + } + DecisionNodeKind::OutputNode { content } => { + let content = match resolved { + Some((schema, _)) => OutputNodeContent { schema: Some(schema) }, + None => content.clone(), + }; + DecisionGraph::handle(base, content, OutputNodeHandler).await + } + _ => base.success(base.input.clone()), + } + } +} diff --git a/core/engine/src/compiled/state/loops.rs b/core/engine/src/compiled/state/loops.rs new file mode 100644 index 00000000..375e83ea --- /dev/null +++ b/core/engine/src/compiled/state/loops.rs @@ -0,0 +1,332 @@ +use std::sync::Arc; +use crate::compiled::bound::Bound; +use crate::compiled::data::{Data, Layer, Mask}; +use crate::compiled::leaf::{ColumnBuilder, Leaf}; +use std::rc::Rc; +use zen_expression::lane::{LaneProgram, LaneRunner}; +use zen_expression::{Scope, Variable}; +use crate::compiled::row::Row; +use crate::compiled::{Body, CompiledGraph, Kind, Step, Transform, leaf}; + +impl CompiledGraph { + pub(in crate::compiled) fn transformed( + runner: &mut LaneRunner, + step: &Step, + body: &Body, + transform: &Transform, + inputs: &[Variable], + nodes: &[Option], + ) -> Vec> { + let effective: Vec> = match &transform.input_field { + None => inputs.iter().cloned().map(Ok).collect(), + Some((program, _)) => { + let scopes: Vec = inputs + .iter() + .enumerate() + .map(|(row, i)| Self::scope(i, nodes.get(row).and_then(Option::as_ref))) + .collect(); + runner + .evaluate(program, &scopes) + .into_iter() + .map(|r| r.map_err(|e| format!("Failed to evaluate expression: {e}"))) + .collect() + } + }; + + let mut outputs: Vec> = match transform.looped { + false => { + let rows: Vec = (0..inputs.len()).filter(|&r| effective[r].is_ok()).collect(); + let body_inputs: Vec = rows + .iter() + .filter_map(|&r| effective[r].as_ref().ok().cloned()) + .collect(); + let body_nodes: Vec> = rows.iter().map(|&r| nodes.get(r).cloned().flatten()).collect(); + let mut produced = Self::items(runner, step, body, &body_inputs, &body_nodes).into_iter(); + effective + .into_iter() + .map(|e| e.and_then(|_| produced.next().unwrap_or_else(|| Ok(Variable::Null)))) + .collect() + } + true => { + let mut owners: Vec = Vec::new(); + let mut items: Vec = Vec::new(); + let mut item_nodes: Vec> = Vec::new(); + let mut shaped: Vec> = Vec::with_capacity(inputs.len()); + for (row, value) in effective.iter().enumerate() { + match value { + Err(e) => shaped.push(Err(e.clone())), + Ok(value) => match value.as_array() { + None => shaped.push(Err("Expected an array".to_string())), + Some(array) => { + let array = array.borrow(); + shaped.push(Ok(array.len())); + for item in array.iter() { + owners.push(row); + items.push(item.clone()); + item_nodes.push(nodes.get(row).cloned().flatten()); + } + } + }, + } + } + let mut produced = Self::items(runner, step, body, &items, &item_nodes).into_iter(); + let mut item_inputs = items.into_iter(); + shaped + .into_iter() + .map(|shape| { + let count = shape?; + let mut collected = Vec::with_capacity(count); + let mut failure: Option = None; + for _ in 0..count { + let output = produced.next().unwrap_or_else(|| Ok(Variable::Null)); + let item = item_inputs.next().unwrap_or(Variable::Null); + match (output, failure.is_some()) { + (_, true) => {} + (Err(e), false) => failure = Some(e), + (Ok(out), false) => collected.push(match transform.pass_through { + true => item.clone().merge_clone(&out), + false => out, + }), + } + } + match failure { + Some(e) => Err(e), + None => Ok(Variable::from_array(collected)), + } + }) + .collect() + } + }; + + for (row, output) in outputs.iter_mut().enumerate() { + let Ok(value) = output else { + continue; + }; + if let Some(path) = &transform.output_path { + let wrapped = Variable::empty_object(); + wrapped.dot_insert(path, value.clone()); + *value = wrapped; + } + if transform.pass_through { + let mut node_input = inputs[row].clone(); + *value = node_input.merge_clone(value); + } + } + outputs + } + + pub(in crate::compiled) fn looped<'a>( + runner: &mut LaneRunner, + step: &Step, + body: &Body, + transform: &Transform, + path: &Arc, + data: &Data<'a>, + ) -> (Data<'a>, Vec<(usize, String)>) { + let source = transform + .input_field + .as_ref() + .and_then(|(program, _)| program.path()) + .unwrap_or_default(); + let lists = data.subtree(source); + let mut items: Vec = Vec::new(); + let mut spans: Vec> = Vec::with_capacity(lists.len()); + let mut failures = Vec::new(); + for (row, list) in lists.iter().enumerate() { + match list.as_array() { + Some(array) => { + let start = items.len(); + items.extend(array.borrow().iter().cloned()); + spans.push(Some((start, items.len()))); + } + None => { + failures.push((row, "Expected an array".to_string())); + spans.push(None); + } + } + } + let produced = Self::items(runner, step, body, &items, &[]); + let mut values = Vec::with_capacity(spans.len()); + for (row, span) in spans.into_iter().enumerate() { + let Some((start, end)) = span else { + values.push(Variable::Null); + continue; + }; + let mut collected = Vec::with_capacity(end - start); + let mut failure = None; + for (item, output) in items[start..end].iter().zip(&produced[start..end]) { + match output { + Ok(out) => collected.push(match transform.pass_through { + true => item.clone().merge_clone(out), + false => out.clone(), + }), + Err(e) => { + failure = Some(e.clone()); + break; + } + } + } + match failure { + Some(e) => { + failures.push((row, e)); + values.push(Variable::Null); + } + None => values.push(Variable::from_array(collected)), + } + } + let layer = Layer::new(Arc::from([path.clone()]), vec![Leaf::Any(values.into())], None); + let output = match transform.pass_through { + true => data.layered(layer), + false => Data::record(&data.rows, layer, None), + }; + (output, failures) + } + + pub(in crate::compiled) fn items( + runner: &mut LaneRunner, + step: &Step, + body: &Body, + inputs: &[Variable], + nodes: &[Option], + ) -> Vec> { + if inputs.iter().any(Self::foreign) { + let kept: Vec = (0..inputs.len()).filter(|&i| !Self::foreign(&inputs[i])).collect(); + let kept_inputs: Vec = kept.iter().map(|&i| inputs[i].clone()).collect(); + let kept_nodes: Vec> = kept.iter().map(|&i| nodes.get(i).cloned().flatten()).collect(); + let mut produced = Self::items(runner, step, body, &kept_inputs, &kept_nodes).into_iter(); + return inputs + .iter() + .enumerate() + .map(|(i, input)| match Self::foreign(input) { + true => Self::walked(step, input.clone(), nodes.get(i).cloned().flatten(), false), + false => produced.next().unwrap_or(Ok(Variable::Null)), + }) + .collect(); + } + match Self::item_data(step, body, inputs, nodes) { + Some(data) => { + let (output, failures) = Self::item_run(runner, step, body, &data); + let values = output.materialized(); + let mut results: Vec> = values.iter().cloned().map(Ok).collect(); + for (item, message) in failures.into_iter().rev() { + if let Some(result) = results.get_mut(item) { + *result = Err(message); + } + } + results + } + None => Self::body(runner, body, inputs, nodes), + } + } + + pub(in crate::compiled) fn foreign(value: &Variable) -> bool { + !matches!(value, Variable::Null | Variable::Object(_)) + } + + pub(in crate::compiled) fn walked(step: &Step, input: Variable, nodes: Option, transforms: bool) -> Result { + let row = Row { + id: &step.node.id, + input, + nodes, + programs: step.shared_programs(), + trace: false, + }; + row.now(&step.node, None, transforms) + .map(|response| response.output) + .map_err(|error| error.source.to_string()) + } + + pub(in crate::compiled) fn item_keys(step: &Step, body: &Body) -> Option>> { + if step.root || step.dynamic || matches!(&step.kind, Kind::Node { transform, .. } if transform.base.is_some()) { + return None; + } + let programs: Vec<&LaneProgram> = match body { + Body::Expression { program, .. } => vec![program], + Body::Table(table) => table.programs(), + }; + let mut keys: Vec> = Vec::new(); + for program in &programs { + let unary = matches!(program.kind(), zen_expression::ExpressionKind::Unary); + for key in program.program().site_keys.iter() { + let key = key.as_deref()?; + match key.starts_with('$') { + true if unary && (key == "$" || key.starts_with("$.")) => {} + true => return None, + false if !keys.iter().any(|k| k.as_ref() == key) => keys.push(Arc::from(key)), + false => {} + } + } + } + let overlapping = keys.iter().any(|a| { + keys.iter() + .any(|b| b.strip_prefix(a.as_ref()).is_some_and(|rest| rest.starts_with('.'))) + }); + (!overlapping).then_some(keys) + } + + pub(in crate::compiled) fn item_run<'a>(runner: &mut LaneRunner, step: &Step, body: &Body, data: &Data<'a>) -> (Data<'a>, Vec<(usize, String)>) { + Self::bound(step, data, &[], &[], None, |bound| Self::layered(runner, body, false, data, bound, None)) + } + + fn item_data<'a>(step: &Step, body: &Body, inputs: &[Variable], nodes: &[Option]) -> Option> { + if inputs.len() < 2 || nodes.iter().any(Option::is_some) { + return None; + } + let keys = Self::item_keys(step, body)?; + let mut leaves = Vec::with_capacity(keys.len()); + let mut present = Vec::with_capacity(keys.len()); + for key in &keys { + let mut builder = ColumnBuilder::with_capacity(inputs.len()); + let mut mask = Vec::with_capacity(inputs.len()); + for item in inputs { + let value = Data::lookup(item, key); + mask.push(value.is_some()); + builder.push_variable(value.unwrap_or(Variable::Null)); + } + leaves.push(Leaf::typed(builder.finish())); + present.push(Mask::Bits(leaf::Bits::of(mask.len(), |row| mask[row]).into())); + } + let rows: Rc<[usize]> = (0..inputs.len()).collect(); + Some(Data::record(&rows, Layer::new(keys.into(), leaves, Some(present)), None)) + } + + fn body( + runner: &mut LaneRunner, + body: &Body, + inputs: &[Variable], + nodes: &[Option], + ) -> Vec> { + let scopes: Vec = inputs + .iter() + .enumerate() + .map(|(row, i)| Self::scope(i, nodes.get(row).and_then(Option::as_ref))) + .collect(); + match body { + Body::Expression { program, entries, .. } => { + let mut out: Vec> = vec![Ok(Variable::Null); scopes.len()]; + runner.evaluate_many(program, &scopes, None, |row, result| { + out[row] = match result { + Some(Ok(values)) => { + let object = Variable::empty_object(); + for ((key, _), value) in entries.iter().zip(values) { + object.dot_insert(key, value); + } + Ok(object) + } + Some(Err((stage, _))) => Err(format!( + r#"Failed to evaluate expression: "{}""#, + entries.get(stage).map(|(_, v)| v.as_ref()).unwrap_or_default() + )), + None => Ok(Variable::Null), + }; + }); + out + } + Body::Table(table) => table + .evaluate(runner, &Bound::plain(scopes)) + .into_iter() + .map(|o| Ok(table.value(o))) + .collect(), + } + } +} diff --git a/core/engine/src/compiled/state/mod.rs b/core/engine/src/compiled/state/mod.rs new file mode 100644 index 00000000..eab73950 --- /dev/null +++ b/core/engine/src/compiled/state/mod.rs @@ -0,0 +1,518 @@ +mod exec; +mod host; +mod loops; + +use crate::decision_graph::cleaner::VariableCleaner; +use crate::decision_graph::walker::GraphWalker; +use crate::model::GraphContent; +use crate::nodes::NodeError; +use crate::nodes::NodeHandlerExtensions; +use crate::{EvaluationError, ZEN_CONFIG}; +use petgraph::prelude::NodeIndex; +use crate::compiled::schedule::{End, Outcome, Replay, Segment}; +use std::sync::atomic::Ordering; +use std::sync::Arc; +use crate::compiled::bound::{Bound, Scopes}; +use crate::compiled::data::{Col, Data}; +use crate::compiled::leaf::{Array, Leaf}; +use std::rc::Rc; +use zen_types::symbol::Symbol; +use zen_expression::lane::{Binding, Columns}; +use zen_expression::{Scope, Variable}; +use zen_types::decision::DecisionNode; +use crate::compiled::{CompiledGraph, Condition, Hit, Kind, Step, data, plan}; + +pub(super) type NodeColumn<'a> = (Arc, Leaf<'a>); + +pub(crate) struct State<'a> { + nodes: Vec>>>, + pub(crate) errors: Vec>>, + failed: usize, + pub(in crate::compiled) results: Vec>, + pub(crate) endings: Vec<(Rc<[usize]>, Vec>)>, + nesting: Nesting<'a>, +} + +#[derive(Clone, Copy, Default)] +pub(crate) struct Nesting<'a> { + pub(in crate::compiled) parents: Option<&'a [Option]>, + pub(in crate::compiled) iteration: u8, +} + +pub(crate) enum Source<'a> { + Rows(&'a [Variable]), + Columns(Rc>), +} + +impl<'a> Source<'a> { + fn input(&self, rows: &Rc<[usize]>) -> Data<'a> { + match self { + Source::Rows(inputs) => Data::plain(rows.clone(), rows.iter().map(|&r| inputs[r].clone()).collect()), + Source::Columns(data) => Data::gather(std::slice::from_ref(data), rows), + } + } +} + +impl CompiledGraph { + pub(in crate::compiled) fn child(&self, segment: &Segment, outcome: Outcome) -> Arc { + let End::Switch { children, .. } = &segment.end else { + return self.root.clone(); + }; + if let Some(found) = children.read().ok().and_then(|c| c.get(&outcome).cloned()) { + return found; + } + let mut path = segment.path.to_vec(); + path.push(outcome.clone()); + let built = Arc::new(Replay::new(&self.graph).segment(&path)); + match children.write() { + Ok(mut map) => map.entry(outcome).or_insert(built).clone(), + Err(_) => built, + } + } + + fn node_columns<'a>(&self, step: &Step, state: &State<'a>, visible: &[NodeIndex], rows: &Rc<[usize]>, with_nodes: bool) -> Vec> { + let Some(statics) = &step.statics else { + return Vec::new(); + }; + statics + .iter() + .map(|(key, name, path)| { + let source = visible + .iter() + .rev() + .filter(|_| with_nodes) + .find(|nid| self.step_at(**nid).is_some_and(|s| s.node.name.as_ref() == name.as_ref())) + .and_then(|nid| state.nodes.get(nid.index())) + .filter(|pieces| !pieces.is_empty()) + .map(|pieces| Data::gather(pieces, rows)); + let leaf = match source { + Some(data) if path.is_empty() => Leaf::Any(data.materialized()), + Some(data) => data.column_at(path), + None => Leaf::nulls(rows.len()), + }; + (key.clone(), leaf) + }) + .collect() + } + + pub(in crate::compiled) fn step_at(&self, nid: NodeIndex) -> Option<&Step> { + self.steps.get(nid.index())?.as_ref() + } + + fn input_of<'a>(state: &State<'a>, parents: &[NodeIndex], rows: &Rc<[usize]>) -> Data<'a> { + let gathered: Vec> = parents + .iter() + .filter_map(|p| { + let pieces = state.nodes.get(p.index())?; + (!pieces.is_empty()).then(|| Data::gather(pieces, rows)) + }) + .collect(); + Data::merged(rows, gathered) + } + + fn nodes_of(&self, state: &State, visible: &[NodeIndex], rows: &Rc<[usize]>) -> Vec { + let columns: Vec<(Arc, Col)> = visible + .iter() + .filter_map(|nid| { + let step = self.step_at(*nid)?; + let pieces = state.nodes.get(nid.index()).filter(|pieces| !pieces.is_empty())?; + let column = Data::gather(pieces, rows).materialized(); + let column = match (&step.kind, state.nesting.parents) { + (Kind::Input, Some(parents)) => column + .iter() + .zip(rows.iter()) + .map(|(value, &row)| match parents.get(row).cloned().flatten() { + Some(parent) => { + let view = value.depth_clone(1); + view.dot_insert(Variable::nodes_key().as_ref(), parent); + view + } + None => value.clone(), + }) + .collect(), + _ => column, + }; + Some((step.node.name.clone(), column)) + }) + .collect(); + (0..rows.len()) + .map(|row| { + let object = Variable::empty_object(); + if let Variable::Object(map) = &object { + let mut map = map.borrow_mut(); + for (name, column) in &columns { + map.insert(name.as_ref().into(), column[row].clone()); + } + } + object + }) + .collect() + } + + pub(in crate::compiled) fn scope(input: &Variable, nodes: Option<&Variable>) -> Scope { + let mut scope = Scope::new(input.clone()); + if let Some(nodes) = nodes { + scope.set_local(Variable::nodes_key(), nodes.clone()); + } + scope + } + + fn fault(state: &mut State, row: usize, error: NodeError) { + state.failed += 1; + state.errors[row] = Some(Box::new(EvaluationError::NodeError { + node_id: error.node_id, + source: error.source, + trace: None, + })); + } + + fn fail(state: &mut State, row: usize, node: &DecisionNode, message: String) { + state.failed += 1; + state.errors[row] = Some(Box::new(EvaluationError::NodeError { + node_id: node.id.clone(), + source: message.into(), + trace: None, + })); + } + + fn alive(state: &State, rows: &Rc<[usize]>) -> Rc<[usize]> { + if state.failed == 0 { + return rows.clone(); + } + match rows.iter().all(|&r| state.errors[r].is_none()) { + true => rows.clone(), + false => rows.iter().copied().filter(|&r| state.errors[r].is_none()).collect(), + } + } + + pub(crate) async fn run<'a>( + &self, + content: &Arc, + extensions: &NodeHandlerExtensions, + max_depth: u8, + source: &Source<'a>, + count: usize, + columnar: bool, + nesting: Nesting<'a>, + ) -> State<'a> { + let mut state = State { + nodes: vec![Vec::new(); self.steps.len()], + errors: (0..count).map(|_| None).collect(), + results: vec![None; count], + endings: Vec::new(), + failed: 0, + nesting, + }; + if nesting.iteration >= max_depth { + for error in state.errors.iter_mut() { + *error = Some(Box::new(EvaluationError::DepthLimitExceeded)); + } + return state; + } + let with_nodes = ZEN_CONFIG.nodes_in_context.load(Ordering::Relaxed); + let mut stack: Vec<(Arc, Rc<[usize]>)> = vec![(self.root.clone(), (0..count).collect())]; + while let Some((segment, mut rows)) = stack.pop() { + for event in segment.events.iter() { + rows = Self::alive(&state, &rows); + if rows.is_empty() { + break; + } + match self.step_at(event.node).filter(|step| Self::immediate(step)) { + Some(step) => self.execute_now(step, event, &rows, source, &mut state, with_nodes), + None => { + self.execute(event, &rows, source, &mut state, content, extensions, with_nodes, max_depth) + .await + } + } + } + rows = Self::alive(&state, &rows); + if rows.is_empty() { + continue; + } + match &segment.end { + End::Finish(ending) if columnar => { + let datas: Vec> = ending + .iter() + .filter_map(|nid| { + let pieces = state.nodes.get(nid.index())?; + (!pieces.is_empty()).then(|| Data::gather(pieces, &rows)) + }) + .collect(); + state.endings.push((rows.clone(), datas)); + } + End::Finish(ending) => { + let columns: Vec= ending + .iter() + .filter_map(|nid| { + let pieces = state.nodes.get(nid.index())?; + (!pieces.is_empty()).then(|| Data::gather(pieces, &rows).materialized()) + }) + .collect(); + for (i, &row) in rows.iter().enumerate() { + let result = match columns.as_slice() { + [single] => Self::ending(&single[i]), + _ => GraphWalker::merge_ending(columns.iter().map(|c| &c[i])), + }; + if state.nesting.parents.is_none() { + VariableCleaner::new().clean(&result); + } + state.results[row] = Some(result); + } + } + End::Switch { + node, + parents, + visible, + .. + } => { + let groups = self.switch(*node, parents, visible, &rows, &state, with_nodes); + for (outcome, members) in groups.into_iter().rev() { + let members: Rc<[usize]> = match members.len() == rows.len() { + true => rows.clone(), + false => members.into(), + }; + stack.push((self.child(&segment, outcome), members)); + } + } + } + } + state + } + + fn ending(value: &Variable) -> Variable { + if let Variable::Object(map) = value { + if Rc::strong_count(map) == 1 && map.borrow().shape().is_some() { + let nulls: Vec = map + .borrow() + .iter() + .filter(|(_, v)| matches!(v, Variable::Null)) + .map(|(k, _)| k.clone()) + .collect(); + let mut map = map.borrow_mut(); + for key in &nulls { + map.remove(key); + } + return value.clone(); + } + } + GraphWalker::merge_ending(std::iter::once(value)) + } + + fn node_inputs<'a>( + &self, + step: &Step, + state: &State<'a>, + visible: &[NodeIndex], + rows: &Rc<[usize]>, + with_nodes: bool, + columnar: bool, + ) -> (Vec>, Vec>) { + match (step.nodes, columnar && step.statics.is_some() && state.nesting.parents.is_none()) { + (false, _) => (Vec::new(), Vec::new()), + (true, true) => (Vec::new(), self.node_columns(step, state, visible, rows, with_nodes)), + (true, false) => match with_nodes { + true => (self.nodes_of(state, visible, rows).into_iter().map(Some).collect(), Vec::new()), + false => (Vec::new(), Vec::new()), + }, + } + } + + fn whole(step: &Step, data: &Data, rebase: Option<&str>) -> bool { + let leaves = data.leaves(); + if leaves.is_empty() { + return false; + } + (step.root && rebase.is_none()) + || step.dynamic + || leaves + .iter() + .any(|(k, _)| k.as_ref() == "$" || k.as_ref() == "$nodes" || k.starts_with("$nodes.")) + } + + fn partials<'a>(step: &Step, data: &Data<'a>) -> Vec> { + let mut out: Vec = Vec::new(); + for path in step.sites.iter().flatten() { + if data.binding(path).1 && !out.iter().any(|(p, _)| p == path) { + out.push((path.clone(), data.partial(path))); + } + } + out + } + + fn bound<'a, R>( + step: &Step, + data: &Data<'a>, + nodes: &[Option], + extra: &[NodeColumn<'a>], + rebase: Option<&str>, + f: impl FnOnce(&Bound) -> R, + ) -> R { + let with_nodes = |scope: Scope, row: usize| match nodes.get(row).cloned().flatten() { + None => scope, + Some(n) => { + let mut scope = scope; + scope.set_local(Variable::nodes_key(), n.clone()); + scope + } + }; + let root = |values: Col| -> Col { + match rebase { + None => values, + Some(base) => values + .iter() + .map(|v| Data::lookup(v, base).unwrap_or(Variable::Null)) + .collect(), + } + }; + if Self::whole(step, data, rebase) { + plan::census::Census::mark("whole"); + let whole = match rebase { + None => data.materialized(), + Some(base) => data.subtree(base), + }; + let scopes = whole + .iter() + .enumerate() + .map(|(row, v)| with_nodes(Scope::new(v.clone()), row)) + .collect(); + return f(&Bound::plain(scopes)); + } + let blank = nodes.iter().all(Option::is_none) + && matches!(&data.shape, data::Shape::Patched { base: None, .. }); + let scopes = match blank { + true => Bound::blank(data.len()), + false => Scopes::Owned( + root(data.base()) + .iter() + .enumerate() + .map(|(row, v)| with_nodes(Scope::new(v.clone()), row)) + .collect(), + ), + }; + let leaves = data.leaves(); + let mut slots: Vec> = vec![None; leaves.len()]; + let mut columns = Columns::new(data.len()); + for path in step.sites.iter().flatten() { + if let Binding::Column(index) = data.binding(path).0 { + if let (Some(slot @ None), Some((leaf, column))) = (slots.get_mut(index), leaves.get(index)) { + *slot = Some(columns.columns.len()); + columns = columns.column(leaf.as_ref(), column.column()); + } + } + } + let partials = Self::partials(step, data); + let offset = columns.columns.len(); + for (path, leaf) in extra.iter().chain(partials.iter()) { + columns = columns.column(path.as_ref(), leaf.column()); + } + let names: Vec> = extra.iter().chain(partials.iter()).map(|(p, _)| p.clone()).collect(); + let bound = Bound { + scopes, + columns, + bind: Box::new(move |key: &str| { + let rebased = rebase.and_then(|base| Self::rebased(base, key)); + let path = rebased.as_deref().unwrap_or(key); + if let Some(at) = names.iter().position(|p| p.as_ref() == path) { + return Binding::Column(offset + at); + } + match data.binding(path).0 { + Binding::Column(index) => match slots.get(index).copied().flatten() { + Some(slot) => Binding::Column(slot), + None => Binding::Row, + }, + other => other, + } + }), + }; + f(&bound) + } + + fn switch( + &self, + node: NodeIndex, + parents: &[NodeIndex], + visible: &[NodeIndex], + rows: &Rc<[usize]>, + state: &State<'_>, + with_nodes: bool, + ) -> Vec<(Outcome, Vec)> { + let Some(step @ Step { + kind: Kind::Switch { first, conditions }, + .. + }) = self.step_at(node) + else { + return vec![(Outcome::from(Vec::new()), rows.to_vec())]; + }; + let data = Self::input_of(state, parents, rows); + let (nodes, extra) = self.node_inputs(step, state, visible, rows, with_nodes, true); + let hits: Vec = Self::RUNNER.with_borrow_mut(|runner| { + Self::bound(step, &data, &nodes, &extra, None, |bound| { + conditions + .iter() + .map(|(_, condition)| match condition { + Condition::Program(program) => { + let p = program.program(); + let rows = bound.rows(); + let uniform = rows > 1 + && !p.opaque() + && p.stable() + && p.site_keys.iter().all(|key| key.as_deref().is_some_and(|key| (bound.bind)(key) == Binding::Absent)); + let mut outs = Vec::new(); + let subset = [0usize]; + bound.export(runner, program, uniform.then_some(&subset[..]), &mut outs, |_, _, _| {}); + match (outs.first_mut().map(|out| Leaf::typed(Array::from_output(out))), uniform) { + (Some(column), true) => Hit::Uniform(column.truthy(0)), + (Some(column), false) => Hit::Rows(column.truths(rows)), + (None, _) => Hit::Uniform(false), + } + } + Condition::Always => Hit::Uniform(true), + Condition::Never => Hit::Uniform(false), + }) + .collect() + }) + }); + let words = conditions.len().div_ceil(64).max(1); + let mut groups: Vec<(Vec, Vec)> = Vec::new(); + let mut key = vec![0u64; words]; + let uniform = hits.iter().all(|hit| matches!(hit, Hit::Uniform(_))); + let lanes = match uniform { + true => rows.len().min(1), + false => rows.len(), + }; + for (lane, &row) in rows.iter().enumerate().take(lanes) { + key.iter_mut().for_each(|w| *w = 0); + for (index, hit) in hits.iter().enumerate() { + let on = match hit { + Hit::Uniform(on) => *on, + Hit::Rows(truths) => truths[lane >> 6] >> (lane & 63) & 1 == 1, + }; + if on { + key[index / 64] |= 1 << (index % 64); + if *first { + break; + } + } + } + match groups.iter_mut().find(|(k, _)| *k == key) { + Some((_, members)) => members.push(row), + None => groups.push((key.clone(), vec![row])), + } + } + if let ([(_, members)], true) = (groups.as_mut_slice(), uniform) { + *members = rows.to_vec(); + } + groups + .into_iter() + .map(|(key, members)| { + let valid: Vec> = conditions + .iter() + .enumerate() + .filter(|(index, _)| key[index / 64] >> (index % 64) & 1 == 1) + .map(|(_, (id, _))| id.clone()) + .collect(); + (Outcome::from(valid), members) + }) + .collect() + } +} diff --git a/core/engine/src/compiled/step.rs b/core/engine/src/compiled/step.rs new file mode 100644 index 00000000..4ab9dc45 --- /dev/null +++ b/core/engine/src/compiled/step.rs @@ -0,0 +1,459 @@ +use crate::decision_graph::graph::DecisionGraph; +use crate::decision_graph::schema_dict; +use crate::model::{DecisionNodeKind, GraphContent}; +use crate::compiled::schedule::Replay; +use std::sync::{Arc, OnceLock}; +use crate::compiled::table::Table; +use zen_expression::lane::{LaneProgram, Programs, SourceInfo}; +use zen_expression::ExpressionKind; +use zen_types::decision::{ + DecisionNode, DecisionTableContent, ExpressionNodeContent, SwitchStatementHitPolicy, + TransformAttributes, TransformExecutionMode, +}; +use crate::compiled::{CompiledGraph, Refusal, Invalid}; + +pub(super) enum Body { + Expression { + program: Box, + empty: Box<[bool]>, + entries: Box<[(Arc, Arc)]>, + fragments: Box<[Arc]>, + overlapping: bool, + walked: bool, + keys: Arc<[Arc]>, + }, + Table(Box
), +} + +pub(super) enum Hit { + Uniform(bool), + Rows(Vec), +} + +pub(super) struct Transform { + pub(super) input_field: Option<(LaneProgram, Arc)>, + pub(super) base: Option>, + pub(super) looped: bool, + pub(super) output_path: Option>, + pub(super) pass_through: bool, +} + +pub(super) enum Kind { + Input, + Output, + Switch { + first: bool, + conditions: Box<[(Arc, Condition)]>, + }, + Node { + body: Body, + transform: Box, + }, + Host, +} + +pub(super) enum Condition { + Always, + Never, + Program(Box), +} + +pub(super) struct Step { + pub(super) node: Arc, + pub(super) kind: Kind, + pub(super) nodes: bool, + pub(super) statics: Option>, + pub(super) root: bool, + pub(super) schema: bool, + pub(super) sites: Vec>>, + pub(super) dynamic: bool, + eager: bool, + programs: OnceLock>, +} + +pub(super) type StaticRead = (Arc, Arc, Arc); + +impl Step { + pub(super) fn shared_programs(&self) -> Option<&Arc> { + self.eager.then(|| self.programs()) + } + + pub(super) fn programs(&self) -> &Arc { + self.programs.get_or_init(|| { + let mut sources: Vec<(&str, ExpressionKind)> = Vec::new(); + match &self.node.kind { + DecisionNodeKind::ExpressionNode { content } => { + sources.extend( + content + .expressions + .iter() + .filter(|e| !e.key.is_empty() && !e.value.is_empty()) + .map(|e| (e.value.as_ref(), ExpressionKind::Standard)), + ); + sources.extend(content.transform_attributes.input_field.iter().map(|f| (f.as_ref(), ExpressionKind::Standard))); + } + DecisionNodeKind::DecisionTableNode { content } => { + for rule in content.rules.iter() { + for input in content.inputs.iter() { + if let Some(cell) = rule.get(&input.id).filter(|c| !c.is_empty()) { + let kind = match input.field.is_some() { + true => ExpressionKind::Unary, + false => ExpressionKind::Standard, + }; + sources.push((cell.as_ref(), kind)); + } + } + for output in content.outputs.iter() { + if let Some(cell) = rule.get(&output.id).filter(|c| !c.is_empty()) { + sources.push((cell.as_ref(), ExpressionKind::Standard)); + } + } + } + sources.extend(content.inputs.iter().filter_map(|i| i.field.as_deref()).map(|f| (f, ExpressionKind::Standard))); + sources.extend(content.transform_attributes.input_field.iter().map(|f| (f.as_ref(), ExpressionKind::Standard))); + } + DecisionNodeKind::SwitchNode { content } => { + sources.extend( + content + .statements + .iter() + .filter(|s| !s.condition.is_empty()) + .map(|s| (s.condition.as_ref(), ExpressionKind::Standard)), + ); + } + _ => {} + } + Arc::new(Programs::compile(sources)) + }) + } +} + +impl CompiledGraph { + pub(super) fn compile(content: &GraphContent, eager: bool) -> Result { + let graph = DecisionGraph::build_graph(content) + .map_err(|e| Refusal::Invalid(Arc::from(e.to_string()), Invalid::Build(e)))?; + DecisionGraph::validate_graph(&graph) + .map_err(|e| Refusal::Invalid(Arc::from("graph fails validation"), Invalid::Validation(e)))?; + + let mut dictionaries = false; + let mut steps: Vec> = Vec::new(); + for nid in graph.node_indices() { + let node = graph[nid].clone(); + let step = Self::step(&node, &mut dictionaries, eager); + let index = nid.index(); + if steps.len() <= index { + steps.resize_with(index + 1, || None); + } + steps[index] = Some(step); + } + + let root = Arc::new(Replay::new(&graph).segment(&[])); + Ok(Self { + graph, + steps, + root, + dictionaries, + plan: OnceLock::new(), + }) + } + + fn transform(attributes: &TransformAttributes) -> Result<(Transform, Vec>), String> { + let input_field = attributes + .input_field + .as_ref() + .map(|f| { + LaneProgram::standard(f) + .map(|p| (p, f.clone())) + .map_err(|e| e.to_string()) + }) + .transpose()?; + let looped = matches!(attributes.execution_mode, TransformExecutionMode::Loop); + let base = input_field + .as_ref() + .filter(|_| !looped) + .and_then(|(program, _)| program.path().map(Arc::from)); + Ok(( + Transform { + input_field, + base, + looped, + output_path: attributes.output_path.clone(), + pass_through: attributes.pass_through, + }, + attributes.input_field.iter().cloned().collect(), + )) + } + + fn expression_kind(content: &ExpressionNodeContent, sources: &mut Vec>) -> Result { + let entries: Box<[(Arc, Arc)]> = content + .expressions + .iter() + .filter(|e| !e.key.is_empty() && !e.value.is_empty()) + .map(|e| (e.key.clone(), e.value.clone())) + .collect(); + sources.extend(entries.iter().map(|(_, v)| v.clone())); + let opaque = entries.iter().any(|(_, v)| { + v.match_indices('$').any(|(at, _)| { + let rest = &v[at + 1..]; + !rest.starts_with("nodes") && !rest.starts_with('.') && !rest.starts_with('{') + }) + }); + let overlapping = entries.iter().any(|(key, _)| key.split('.').any(str::is_empty)) + || entries.iter().enumerate().any(|(i, (a, _))| { + entries.iter().skip(i + 1).any(|(b, _)| { + a == b + || a.strip_prefix(b.as_ref()).is_some_and(|r| r.starts_with('.')) + || b.strip_prefix(a.as_ref()).is_some_and(|r| r.starts_with('.')) + }) + }); + let walked = overlapping && entries.iter().any(|(_, source)| source.contains('$')); + let mut expanded: Vec<(Arc, Arc, Arc)> = Vec::with_capacity(entries.len()); + for (key, source) in entries.iter() { + let read = format!("$.{}", key.split('.').next().unwrap_or_default()); + let flat = !opaque && !overlapping && !entries.iter().any(|(_, v)| v.contains(read.as_str())); + match flat { + true => Self::expand(key, source, source, &mut expanded), + false => expanded.push((key.clone(), source.clone(), source.clone())), + } + } + let refs: Vec<(&str, &str)> = expanded.iter().map(|(k, _, v)| (k.as_ref(), v.as_ref())).collect(); + let program = LaneProgram::compile_many(&refs, true).map_err(|e| e.to_string())?; + let empty: Box<[bool]> = expanded.iter().map(|(_, _, fragment)| fragment.trim() == "{}").collect(); + let fragments: Box<[Arc]> = expanded.iter().map(|(_, _, fragment)| fragment.clone()).collect(); + let entries: Box<[(Arc, Arc)]> = + expanded.into_iter().map(|(key, source, _)| (key, source)).collect(); + let (transform, extra) = Self::transform(&content.transform_attributes)?; + sources.extend(extra); + Ok(Kind::Node { + transform: Box::new(transform), + body: Body::Expression { + program: Box::new(program), + keys: entries.iter().map(|(k, _)| k.clone()).collect(), + entries, + fragments, + empty, + overlapping, + walked, + }, + }) + } + + fn table_kind( + content: &DecisionTableContent, + sources: &mut Vec>, + cells: &mut Vec>, + ) -> Result { + for rule in content.rules.iter() { + for input in content.inputs.iter() { + if let Some(cell) = rule.get(&input.id) { + match input.field.is_some() { + true => cells.push(cell.clone()), + false => sources.push(cell.clone()), + } + } + } + for output in content.outputs.iter() { + sources.extend(rule.get(&output.id).cloned()); + } + } + sources.extend(content.inputs.iter().filter_map(|i| i.field.clone())); + let table = Table::compile(content)?; + let (transform, extra) = Self::transform(&content.transform_attributes)?; + sources.extend(extra); + Ok(Kind::Node { + body: Body::Table(Box::new(table)), + transform: Box::new(transform), + }) + } + + fn step(node: &Arc, dictionaries: &mut bool, eager: bool) -> Step { + let mut sources: Vec> = Vec::new(); + let mut cells: Vec> = Vec::new(); + let kind = match &node.kind { + DecisionNodeKind::InputNode { content } => { + *dictionaries |= content + .schema + .as_deref() + .is_some_and(schema_dict::schema_references_dictionary); + Kind::Input + } + DecisionNodeKind::OutputNode { content } => { + *dictionaries |= content + .schema + .as_deref() + .is_some_and(schema_dict::schema_references_dictionary); + Kind::Output + } + DecisionNodeKind::SwitchNode { content } => { + let conditions = content + .statements + .iter() + .map(|s| { + sources.push(s.condition.clone()); + let condition = match s.condition.is_empty() { + true => Condition::Always, + false => LaneProgram::standard(&s.condition) + .map(|p| Condition::Program(Box::new(p))) + .unwrap_or(Condition::Never), + }; + (s.id.clone(), condition) + }) + .collect(); + Kind::Switch { + first: matches!(content.hit_policy, SwitchStatementHitPolicy::First), + conditions, + } + } + DecisionNodeKind::ExpressionNode { .. } | DecisionNodeKind::DecisionTableNode { .. } if !eager => Kind::Host, + DecisionNodeKind::ExpressionNode { content } => { + Self::expression_kind(content, &mut sources).unwrap_or(Kind::Host) + } + DecisionNodeKind::DecisionTableNode { content } => { + Self::table_kind(content, &mut sources, &mut cells).unwrap_or(Kind::Host) + } + DecisionNodeKind::FunctionNode { .. } + | DecisionNodeKind::DecisionNode { .. } + | DecisionNodeKind::CustomNode { .. } => Kind::Host, + }; + let mut kind = kind; + if let Kind::Node { body, transform } = &mut kind { + if let Some(base) = transform.base.clone() { + let mappable = match &*body { + Body::Expression { program, .. } => program + .program() + .site_keys + .iter() + .flatten() + .all(|key| Self::rebased(&base, key).is_some()), + Body::Table(_) => false, + }; + if !mappable { + transform.base = None; + } + } + } + let mut programs: Vec<&LaneProgram> = Vec::new(); + match &kind { + Kind::Switch { conditions, .. } => programs.extend(conditions.iter().filter_map(|(_, c)| match c { + Condition::Program(p) => Some(p.as_ref()), + _ => None, + })), + Kind::Node { body, transform } => { + match body { + Body::Expression { program, .. } => programs.push(program), + Body::Table(table) => programs.extend(table.programs()), + } + programs.extend(transform.input_field.iter().map(|(p, _)| p)); + } + Kind::Input | Kind::Output | Kind::Host => {} + } + let base = match &kind { + Kind::Node { transform, .. } => transform.base.clone(), + _ => None, + }; + let input_sites = match &kind { + Kind::Node { transform, .. } => transform.input_field.iter().count(), + _ => 0, + }; + let body_programs = programs.len() - input_sites; + let programs = match base { + Some(_) => &programs[..body_programs], + None => &programs[..], + }; + let sites: Vec>> = programs + .iter() + .enumerate() + .flat_map(|(index, p)| { + let base = base.as_deref().filter(|_| index < body_programs); + p.program().site_keys.iter().map(move |k| { + k.as_deref().map(|key| match base { + Some(base) => Self::rebased(base, key).unwrap_or_else(|| Arc::from(key)), + None => Arc::from(key), + }) + }) + }) + .collect(); + let dynamic = programs + .iter() + .any(|p| p.program().writes_env || p.program().chain || p.program().opaque()); + let schema = match &node.kind { + DecisionNodeKind::InputNode { content } => content.schema.is_some(), + DecisionNodeKind::OutputNode { content } => content.schema.is_some(), + _ => false, + }; + Step { + node: node.clone(), + eager, + programs: OnceLock::new(), + statics: match &kind { + Kind::Host => None, + Kind::Node { body: Body::Table(_), .. } => None, + _ if sources.iter().any(|s| SourceInfo::reads_root(s)) => None, + _ => Self::statics(&sites), + }, + nodes: matches!(kind, Kind::Host) + || sources.iter().any(|s| SourceInfo::reads_nodes(s)) + || cells.iter().any(|s| SourceInfo::reads_nodes_unary(s)), + root: match &kind { + Kind::Node { + body: Body::Expression { program, .. }, + transform, + } => { + program.program().keys.iter().any(|k| k == "$" || k.starts_with("$.")) + || transform.input_field.as_ref().is_some_and(|(_, source)| SourceInfo::reads_root(source)) + } + _ => { + sources.iter().any(|s| SourceInfo::reads_root(s)) + || cells.iter().any(|s| SourceInfo::reads_root_unary(s)) + } + }, + schema, + sites, + dynamic, + kind, + } + } + + fn expand(key: &Arc, origin: &Arc, source: &str, out: &mut Vec<(Arc, Arc, Arc)>) { + match LaneProgram::object_fields(source).filter(|fields| !fields.is_empty()) { + Some(fields) => { + for (field, fragment) in fields.into_iter().rev() { + Self::expand(&Arc::from(format!("{key}.{field}")), origin, &fragment, out); + } + } + None => out.push((key.clone(), origin.clone(), Arc::from(source))), + } + } + + pub(super) fn rebased(base: &str, key: &str) -> Option> { + if key.starts_with("$nodes") { + return Some(Arc::from(key)); + } + match key.strip_prefix('$') { + Some("") => Some(Arc::from(base)), + Some(rest) if rest.starts_with('.') => Some(Arc::from(format!("{base}{rest}"))), + Some(_) => None, + None => Some(Arc::from(format!("{base}.{key}"))), + } + } + + fn statics(sites: &[Option>]) -> Option> { + if sites.iter().any(Option::is_none) { + return None; + } + let mut out: Vec = Vec::new(); + for key in sites.iter().flatten() { + if key.as_ref() == "$nodes" { + return None; + } + let Some(rest) = key.strip_prefix("$nodes.") else { + continue; + }; + let (name, path) = rest.split_once('.').unwrap_or((rest, "")); + if !out.iter().any(|(k, _, _)| k == key) { + out.push((key.clone(), Arc::from(name), Arc::from(path))); + } + } + Some(out) + } +}