diff --git a/crates/lance-csr/src/datafusion_planner/analysis.rs b/crates/lance-csr/src/datafusion_planner/analysis.rs index 613b250e..f2bf6abd 100644 --- a/crates/lance-csr/src/datafusion_planner/analysis.rs +++ b/crates/lance-csr/src/datafusion_planner/analysis.rs @@ -457,6 +457,7 @@ mod tests { relationship_variable: None, min_length: Some(1), max_length: Some(2), + properties: Default::default(), target_properties: HashMap::new(), }; diff --git a/crates/lance-csr/src/datafusion_planner/builder/expand_ops.rs b/crates/lance-csr/src/datafusion_planner/builder/expand_ops.rs index 3394ff82..8d7ffd78 100644 --- a/crates/lance-csr/src/datafusion_planner/builder/expand_ops.rs +++ b/crates/lance-csr/src/datafusion_planner/builder/expand_ops.rs @@ -657,6 +657,7 @@ impl DataFusionPlanner { direction: &RelationshipDirection, min_length: Option, max_length: Option, + relationship_properties: &HashMap, target_properties: &HashMap, ) -> Result { let min_hops = min_length.unwrap_or(1); @@ -700,17 +701,24 @@ impl DataFusionPlanner { .map(|f| f.name().clone()), ); - if let Some(plan) = self.try_build_csr_traversal( - ctx, - input_plan.clone(), - source_variable, - target_variable, - relationship_types, - direction, - min_hops, - max_hops, - target_properties, - )? { + // The CSR walk cannot filter relationship properties, so a property map falls + // back to the fixed-length plans, which filter every hop. + let csr_plan = if relationship_properties.is_empty() { + self.try_build_csr_traversal( + ctx, + input_plan.clone(), + source_variable, + target_variable, + relationship_types, + direction, + min_hops, + max_hops, + target_properties, + )? + } else { + None + }; + if let Some(plan) = csr_plan { let projection: Vec<_> = plan .schema() .fields() @@ -736,6 +744,7 @@ impl DataFusionPlanner { relationship_types, direction, hop_count, + relationship_properties, target_properties, )?; @@ -804,6 +813,7 @@ impl DataFusionPlanner { relationship_types: &[String], direction: &RelationshipDirection, hop_count: u32, + relationship_properties: &HashMap, target_properties: &HashMap, ) -> Result { if hop_count == 0 { @@ -888,6 +898,7 @@ impl DataFusionPlanner { ¤t_target, relationship_types, direction, + relationship_properties, props_to_apply, )?; @@ -1043,6 +1054,7 @@ impl DataFusionPlanner { target_variable: &str, relationship_types: &[String], direction: &RelationshipDirection, + relationship_properties: &HashMap, target_properties: &HashMap, ) -> Result { if relationship_types.is_empty() { @@ -1065,7 +1077,7 @@ impl DataFusionPlanner { relationship_types, direction, None, - &HashMap::new(), + relationship_properties, target_properties, ) } @@ -1145,6 +1157,7 @@ mod tests { relationship_variable: Some("r".into()), min_length: Some(1), max_length: Some(2), + properties: Default::default(), target_properties: HashMap::new(), }; let project = LogicalOperator::Project { @@ -1181,6 +1194,7 @@ mod tests { relationship_variable: None, min_length: Some(1), max_length: Some(1), + properties: Default::default(), target_properties: HashMap::new(), }; let project = LogicalOperator::Project { @@ -1217,6 +1231,7 @@ mod tests { relationship_variable: None, min_length: Some(2), max_length: Some(3), + properties: Default::default(), target_properties: HashMap::new(), }; let project = LogicalOperator::Project { @@ -1267,6 +1282,7 @@ mod tests { relationship_variable: None, min_length: None, // Should default to 1 max_length: Some(3), + properties: Default::default(), target_properties: HashMap::new(), }; let project = LogicalOperator::Project { @@ -1316,6 +1332,7 @@ mod tests { relationship_variable: None, min_length: Some(2), max_length: None, // Should default to MAX_VARIABLE_LENGTH_HOPS (20) + properties: Default::default(), target_properties: HashMap::new(), }; let project = LogicalOperator::Project { @@ -1366,6 +1383,7 @@ mod tests { relationship_variable: None, min_length: Some(3), max_length: Some(2), // Invalid: min > max + properties: Default::default(), target_properties: HashMap::new(), }; let project = LogicalOperator::Project { @@ -1416,6 +1434,7 @@ mod tests { relationship_variable: None, min_length: Some(1), max_length: Some(25), // Exceeds MAX_VARIABLE_LENGTH_HOPS + properties: Default::default(), target_properties: HashMap::new(), }; let project = LogicalOperator::Project { @@ -1466,6 +1485,7 @@ mod tests { relationship_variable: None, min_length: Some(1), max_length: Some(2), + properties: Default::default(), target_properties: HashMap::new(), }; let filter = LogicalOperator::Filter { diff --git a/crates/lance-csr/src/datafusion_planner/builder/helpers.rs b/crates/lance-csr/src/datafusion_planner/builder/helpers.rs index ffe66c0c..d43be753 100644 --- a/crates/lance-csr/src/datafusion_planner/builder/helpers.rs +++ b/crates/lance-csr/src/datafusion_planner/builder/helpers.rs @@ -347,6 +347,7 @@ mod tests { relationship_variable: None, min_length: Some(1), max_length: Some(3), + properties: Default::default(), target_properties: HashMap::new(), }; diff --git a/crates/lance-csr/src/datafusion_planner/builder/mod.rs b/crates/lance-csr/src/datafusion_planner/builder/mod.rs index 93e677c0..5d9a0bee 100644 --- a/crates/lance-csr/src/datafusion_planner/builder/mod.rs +++ b/crates/lance-csr/src/datafusion_planner/builder/mod.rs @@ -97,6 +97,7 @@ impl DataFusionPlanner { direction, min_length, max_length, + properties, target_properties, .. } => self.build_variable_length_expand( @@ -108,6 +109,7 @@ impl DataFusionPlanner { direction, *min_length, *max_length, + properties, target_properties, ), LogicalOperator::Join { diff --git a/crates/lance-csr/src/datafusion_planner/scan_ops.rs b/crates/lance-csr/src/datafusion_planner/scan_ops.rs index 22c0c8a0..271d471d 100644 --- a/crates/lance-csr/src/datafusion_planner/scan_ops.rs +++ b/crates/lance-csr/src/datafusion_planner/scan_ops.rs @@ -407,6 +407,7 @@ mod tests { min_length: Some(2), max_length: Some(2), relationship_variable: None, + properties: Default::default(), target_properties: Default::default(), }; diff --git a/crates/lance-csr/src/logical_plan.rs b/crates/lance-csr/src/logical_plan.rs index fe57d64b..063453d6 100644 --- a/crates/lance-csr/src/logical_plan.rs +++ b/crates/lance-csr/src/logical_plan.rs @@ -100,6 +100,8 @@ pub enum LogicalOperator { min_length: Option, /// Maximum number of hops (defaults to system max if None) max_length: Option, + /// Property filters every relationship along the path must match + properties: HashMap, /// Property filters to apply on target nodes target_properties: HashMap, }, @@ -2003,6 +2005,7 @@ impl<'a> LogicalPlanner<'a> { relationship_variable: segment.relationship.variable.clone(), min_length: length_range.min, max_length: length_range.max, + properties: segment.relationship.properties.clone(), target_properties: segment.end_node.properties.clone(), }, None => LogicalOperator::Expand { diff --git a/docs/cypher.md b/docs/cypher.md index 91b1949f..14474453 100644 --- a/docs/cypher.md +++ b/docs/cypher.md @@ -368,7 +368,9 @@ RETURN length(p), nodes(p) - **Variable length** defaults to a minimum of 1 and a maximum of 20 hops; a maximum above 20 is rejected. `*0..` also matches the source itself, once per input row; `*0..0` matches only that identity. The start and end must have the same label and key for a zero-hop match. Eligible CSR paths use one iterative - expansion for the whole range; the join fallback uses one branch per length. + expansion for the whole range; the join fallback uses one branch per length. A property map on a + variable-length relationship, such as `[:KNOWS*1..3 {since: 2020}]`, must hold for every relationship + on the path; a zero-hop match has none to check. Such a path uses the join fallback. - **Variable-length alternation** follows each relationship type's configured endpoint labels. With suitable CSR and node indexes, a mixed path such as `[:hasType|isSubclassOf*1..3]` stays in one operator. Named paths also support typed variable-length alternation through their edge inventory. diff --git a/python/python/tests/test_engine_paths.py b/python/python/tests/test_engine_paths.py index bece713a..6fca1f57 100644 --- a/python/python/tests/test_engine_paths.py +++ b/python/python/tests/test_engine_paths.py @@ -9,7 +9,7 @@ import pyarrow as pa import pytest -from lance_csr import CypherEngine +from lance_csr import CypherEngine, GraphConfig from test_engine_csr import CONFIG, KNOWS, PEOPLE CASES = [ @@ -166,3 +166,29 @@ def test_shortest_path_null_checks_keep_compact_plan( plan = engine.explain(query) assert "ShortestPathExec" in plan assert "PathMatchExec" not in plan + + +@pytest.mark.parametrize("use_csr", [False, True]) +@pytest.mark.parametrize( + ("pattern", "expected"), + [ + # 2->3 has day 6, so the walk from 1 stops at 2. + ("(a:N {id: 1})-[:R*1..3 {day: 5}]->(b:N)", [2]), + ("(a:N {id: 4})<-[:R*1..3 {day: 5}]-(b:N)", [3]), + # Zero hops has no relationship to check, so 3 matches itself. + ("(a:N {id: 3})-[:R*0..3 {day: 5}]-(b:N)", [3, 4]), + ("p = (a:N {id: 1})-[:R*1..3 {day: 5}]->(b:N)", [2]), + ], +) +def test_variable_length_property_map_filters_every_hop(use_csr, pattern, expected): + config = ( + GraphConfig.builder() + .node_table("N", "id") + .rel_table("R", "N", "N", "src", "dst") + .build() + ) + nodes = pa.table({"id": [1, 2, 3, 4]}) + rels = pa.table({"src": [1, 2, 3], "dst": [2, 3, 4], "day": [5, 6, 5]}) + engine = CypherEngine(config, {"N": nodes, "R": rels}, use_csr=use_csr) + result = engine.execute(f"MATCH {pattern} RETURN b.id AS b ORDER BY b") + assert result.to_pydict() == {"b": expected}