diff --git a/crates/lance-csr/src/path_match.rs b/crates/lance-csr/src/path_match.rs index 99ab9d7..82749e5 100644 --- a/crates/lance-csr/src/path_match.rs +++ b/crates/lance-csr/src/path_match.rs @@ -370,7 +370,7 @@ struct Ready { node_keys: Int64Array, shortest: Option<(Neighbors, Neighbors)>, /// CSR route only: per step and hop, its filter and what that filter has checked. - hops: Vec>, + hops: Vec)>>, /// CSR route only: the inventory has no edges, so relationship values are built here. edge_type: arrow_schema::DataType, edge_fields: arrow_schema::Fields, @@ -708,15 +708,23 @@ impl Ready { let edge_fields = property_fields(spec.edges.iter().map(|s| s.schema.fields()))?; let node_type = inventory.nodes.data_type().clone(); let edge_type = entity_type(edge_fields.clone(), "relationship", Some(&node_type)); - let hops = hop_filters - .iter() - .map(|filters| { - filters - .iter() - .map(|f| (f.clone(), EdgeFilterState::default())) - .collect() - }) - .collect(); + // Hops of one step over the same table share a state: the two halves of an + // undirected step then count their checks together and read one mask. + let hops = match &spec.csr { + Some(csr) => csr + .steps + .iter() + .zip(hop_filters) + .map(|(step, filters)| { + let mut states: HashMap> = HashMap::new(); + step.iter() + .zip(filters) + .map(|(hop, f)| (f.clone(), states.entry(hop.kind).or_default().clone())) + .collect() + }) + .collect(), + None => Vec::new(), + }; let mask = |p: &Option>, batch: &RecordBatch| -> Result { match p { diff --git a/docs/internals/operators.md b/docs/internals/operators.md index 2de391b..bffd42e 100644 --- a/docs/internals/operators.md +++ b/docs/internals/operators.md @@ -288,7 +288,8 @@ A frame reads its node's neighbors and their edge rows from the CSR the first ti step reads the outgoing and incoming CSR separately, so each edge keeps its stored direction. The step's relationship equalities are compiled against each relationship table and checked by `EdgeFilterState`, the same rule `CsrExpandExec` uses: fetch the filter columns of just the edges reached and, on a Lance table, switch to one read of -the whole column once the edges checked pass 1/256 of the relationship's edges. An edge's identity is its CSR row, +the whole column once the edges checked pass 1/256 of the relationship's edges. Both halves of an undirected step +share one state, so they count their checks together and read the column once. An edge's identity is its CSR row, numbered per input batch so a trail can refuse to reuse one. Relationship records (properties, identity, start and end nodes) are built only for the edges of the paths in an output batch. This follows Kùzu's design, which evaluates relationship predicates inside recursive joins over CSR adjacency lists, so the work follows the frontier instead of