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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/lance-csr/src/datafusion_planner/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ mod tests {
relationship_variable: None,
min_length: Some(1),
max_length: Some(2),
properties: Default::default(),
target_properties: HashMap::new(),
};

Expand Down
44 changes: 32 additions & 12 deletions crates/lance-csr/src/datafusion_planner/builder/expand_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,7 @@ impl DataFusionPlanner {
direction: &RelationshipDirection,
min_length: Option<u32>,
max_length: Option<u32>,
relationship_properties: &HashMap<String, crate::ast::PropertyValue>,
target_properties: &HashMap<String, crate::ast::PropertyValue>,
) -> Result<LogicalPlan> {
let min_hops = min_length.unwrap_or(1);
Expand Down Expand Up @@ -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()
Expand All @@ -736,6 +744,7 @@ impl DataFusionPlanner {
relationship_types,
direction,
hop_count,
relationship_properties,
target_properties,
)?;

Expand Down Expand Up @@ -804,6 +813,7 @@ impl DataFusionPlanner {
relationship_types: &[String],
direction: &RelationshipDirection,
hop_count: u32,
relationship_properties: &HashMap<String, crate::ast::PropertyValue>,
target_properties: &HashMap<String, crate::ast::PropertyValue>,
) -> Result<LogicalPlan> {
if hop_count == 0 {
Expand Down Expand Up @@ -888,6 +898,7 @@ impl DataFusionPlanner {
&current_target,
relationship_types,
direction,
relationship_properties,
props_to_apply,
)?;

Expand Down Expand Up @@ -1043,6 +1054,7 @@ impl DataFusionPlanner {
target_variable: &str,
relationship_types: &[String],
direction: &RelationshipDirection,
relationship_properties: &HashMap<String, crate::ast::PropertyValue>,
target_properties: &HashMap<String, crate::ast::PropertyValue>,
) -> Result<LogicalPlan> {
if relationship_types.is_empty() {
Expand All @@ -1065,7 +1077,7 @@ impl DataFusionPlanner {
relationship_types,
direction,
None,
&HashMap::new(),
relationship_properties,
target_properties,
)
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions crates/lance-csr/src/datafusion_planner/builder/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ mod tests {
relationship_variable: None,
min_length: Some(1),
max_length: Some(3),
properties: Default::default(),
target_properties: HashMap::new(),
};

Expand Down
2 changes: 2 additions & 0 deletions crates/lance-csr/src/datafusion_planner/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ impl DataFusionPlanner {
direction,
min_length,
max_length,
properties,
target_properties,
..
} => self.build_variable_length_expand(
Expand All @@ -108,6 +109,7 @@ impl DataFusionPlanner {
direction,
*min_length,
*max_length,
properties,
target_properties,
),
LogicalOperator::Join {
Expand Down
1 change: 1 addition & 0 deletions crates/lance-csr/src/datafusion_planner/scan_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ mod tests {
min_length: Some(2),
max_length: Some(2),
relationship_variable: None,
properties: Default::default(),
target_properties: Default::default(),
};

Expand Down
3 changes: 3 additions & 0 deletions crates/lance-csr/src/logical_plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ pub enum LogicalOperator {
min_length: Option<u32>,
/// Maximum number of hops (defaults to system max if None)
max_length: Option<u32>,
/// Property filters every relationship along the path must match
properties: HashMap<String, PropertyValue>,
/// Property filters to apply on target nodes
target_properties: HashMap<String, PropertyValue>,
},
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion docs/cypher.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
28 changes: 27 additions & 1 deletion python/python/tests/test_engine_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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}