feat: formalize MATCH-CREATE combinations with comprehensive tests (issue #23)#41
Conversation
…ssue #23) ## Summary Adds grammar support and comprehensive testing for MATCH-CREATE combinations, enabling users to connect existing nodes to new nodes and build graph expansions. ## Grammar Changes (cypher.lark) Added MATCH + CREATE pattern to write_query: - match_clause where_clause? create_clause+ set_clause? return_clause? order_by_clause? skip_clause? limit_clause? This enables queries like: - MATCH (a) CREATE (a)-[:REL]->(b) - MATCH (a), (b) CREATE (a)-[:REL]->(b) - MATCH (a) WHERE a.prop > 10 CREATE (a)-[:REL]->(b) ## Testing Added 20 comprehensive tests in test_match_create.py: ### Basic Combinations (3 tests): - test_match_one_create_connected_node - test_match_two_nodes_create_relationship - test_match_pattern_create_expansion ### Multiple Matches (3 tests): - test_match_multiple_create_for_each (cardinality: 3 matches = 3 creates) - test_match_with_where_filters_creates - test_cartesian_product_creates ### No Matches (2 tests): - test_no_match_no_create (no matches = no creates) - test_optional_match_creates_with_null ### Complex Patterns (4 tests): - test_chain_multiple_creates - test_match_relationship_create_from_dest - test_create_references_match_variable_multiple_times - test_match_multiple_patterns_create_references_both ### With Other Clauses (3 tests): - test_match_create_set (MATCH-CREATE-SET) - test_match_create_return_all_columns - test_match_create_with_order_limit (with ORDER BY, LIMIT) ### Edge Cases (5 tests): - test_create_node_with_properties_from_matched - test_create_multiple_relationships_same_nodes - test_match_create_with_null_property - test_match_create_multiple_new_nodes_per_match - test_match_with_aggregation_then_create ## Use Cases ### Connect Existing to New Nodes ```cypher MATCH (a:Person {name: 'Alice'}) CREATE (a)-[:KNOWS]->(b:Person {name: 'Bob'}) RETURN a, b ``` ### Create Relationships Between Existing Nodes ```cypher MATCH (src:City {name: 'NYC'}), (dst:City {name: 'LA'}) CREATE (src)-[r:FLIGHT {duration: 6}]->(dst) RETURN r ``` ### Multiple Creates Per Match ```cypher MATCH (p:Person) CREATE (p)-[:POSTED]->(post:Post {content: 'Hello'}) RETURN p, post -- Creates one post per person ``` ### With Filtering ```cypher MATCH (p:Person) WHERE p.age > 30 CREATE (p)-[:ATTENDED]->(e:Event {name: 'Conference'}) RETURN p ``` ### Chain Multiple Creates ```cypher MATCH (a:Person {name: 'Alice'}) CREATE (a)-[:WROTE]->(b:Book {title: 'GraphDB 101'}) CREATE (a)-[:WROTE]->(c:Book {title: 'Cypher Guide'}) RETURN a, b, c ``` ## Key Features 1. **Cardinality Preservation**: N matches = N creates 2. **Variable Binding**: MATCH variables accessible in CREATE 3. **No Match = No Create**: Empty MATCH produces no CREATE 4. **Property Access**: CREATE can reference MATCH node properties 5. **Multiple Creates**: Chain multiple CREATE clauses after MATCH 6. **Filtering**: WHERE clause filters which matches get creates 7. **Full Pipeline**: Supports SET, RETURN, ORDER BY, SKIP, LIMIT ## Test Results ``` ✅ 707 tests passed (+20 new MATCH-CREATE tests) ✅ 14 skipped ✅ Coverage: 94.82% (exceeds threshold) ✅ All pre-push checks passed: - Formatting ✅ - Linting ✅ - Type checking ✅ - Coverage ✅ ``` ## Impact - **New Pattern**: MATCH + CREATE now officially supported - **Use Cases**: Graph expansion, relationship creation, batch operations - **Breaking Changes**: None - pure additive feature - **TCK Impact**: Likely improves OpenCypher compliance - **Documentation**: 20 tests serve as comprehensive examples ## Examples Verified All these patterns now work and are tested: - ✅ MATCH (a) CREATE (a)-[:REL]->(b) - ✅ MATCH (a), (b) CREATE (a)-[:REL]->(b) - ✅ MATCH (a) WHERE ... CREATE (a)-[:REL]->(b) - ✅ MATCH (a) CREATE ... CREATE ... (multiple creates) - ✅ MATCH (a) CREATE ... SET ... RETURN ... - ✅ MATCH (a) CREATE ... RETURN ... ORDER BY ... LIMIT ... ## Related - Implements #23 - Part of v0.2.0 feature set (Phase 3: Complex Features) - Estimated 3-4 hours (actual: ~2 hours - faster since executor already worked) ## Checklist - [x] Grammar updated for MATCH + CREATE pattern - [x] 20 comprehensive tests added (all passing) - [x] Tests verify cardinality (N matches = N creates) - [x] Tests verify variable binding from MATCH to CREATE - [x] Tests verify empty MATCH = no CREATE - [x] Tests cover edge cases and complex patterns - [x] All 707 tests passing - [x] Coverage maintained (94.82%) - [x] Pre-push checks passed Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
WalkthroughThe changes extend the Cypher grammar to formally support MATCH...CREATE write operations, allowing queries that match existing nodes and create new nodes or relationships. A comprehensive integration test suite validates this functionality across various scenarios including multiple matches, cartesian products, null handling, and interactions with SET, RETURN, ORDER BY, and LIMIT clauses. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related issues
Suggested labels
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #41 +/- ##
=======================================
Coverage 92.59% 92.59%
=======================================
Files 15 15
Lines 1810 1810
Branches 446 446
=======================================
Hits 1676 1676
Misses 50 50
Partials 84 84
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report in Codecov by Sentry.
|
Summary
Implements #23 - Formalizes MATCH-CREATE combinations with grammar support and 20 comprehensive tests, enabling users to connect existing nodes to new nodes and build dynamic graph expansions.
What Changed
Grammar Enhancement (
cypher.lark)Added MATCH + CREATE pattern to
write_query:This single grammar change unlocks powerful MATCH-CREATE combinations.
Testing (
test_match_create.py)20 comprehensive tests covering all scenarios:
Test Coverage
✅ Basic Combinations (3 tests)
✅ Multiple Matches (3 tests)
✅ No Matches (2 tests)
✅ Complex Patterns (4 tests)
✅ With Other Clauses (3 tests)
✅ Edge Cases (5 tests)
Examples
Connect Existing to New Node
Create Relationship Between Existing Nodes
Batch Operations
With Filtering
Chain Multiple Creates
Copy Properties
Complex Pattern Expansion
Key Features
1. Cardinality Preservation
2. Variable Binding
CREATE (b {prop: a.value})3. Empty Match Handling
4. Full Pipeline Support
5. Multiple Creates
Test Results
Implementation Notes
Why This Was Fast
The issue description was correct - the executor already worked! We only needed:
The planner and executor handled everything else automatically because:
_execute_create()already uses context bindingsWhat Was Tested
Every test verifies:
Use Cases Unlocked
Pattern Verification
All these patterns now work and are tested:
MATCH (a) CREATE (a)-[:R]->(b)MATCH (a), (b) CREATE (a)-[:R]->(b)MATCH (a) WHERE ... CREATE ...MATCH (a)-[r]->(b) CREATE ...MATCH (a) CREATE ... CREATE ...MATCH (a) CREATE ... SET ...MATCH (a) CREATE ... RETURN ... ORDER BY ...Impact
Future Enhancements
Potential follow-ups (not in this PR):
Related
Checklist
🤖 Generated with Claude Code
Summary by CodeRabbit
New Features
Tests