-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcypher_examples.py
More file actions
190 lines (165 loc) · 4.54 KB
/
cypher_examples.py
File metadata and controls
190 lines (165 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
"""
Examples demonstrating the Cypher parser capabilities
Run these to see how Cypher queries are translated to PostgreSQL SQL
"""
from graphiti_postgres.cypher import CypherParser, SQLGenerator
def print_translation(title: str, cypher: str, params: dict = None):
"""Helper to show Cypher to SQL translation"""
print(f"\n{'='*80}")
print(f"Example: {title}")
print(f"{'='*80}")
print(f"\nCypher Query:")
print(cypher)
parser = CypherParser()
generator = SQLGenerator(group_id='example_group')
try:
ast = parser.parse(cypher)
sql, sql_params = generator.generate(ast, params or {})
print(f"\nGenerated SQL:")
print(sql)
print(f"\nParameters: {sql_params}")
except Exception as e:
print(f"\nError: {e}")
def main():
"""Run all examples"""
# Example 1: Simple MATCH
print_translation(
"Simple Node Match",
"MATCH (n:Person) RETURN n.name, n.age"
)
# Example 2: Relationship traversal
print_translation(
"Relationship Traversal",
"""
MATCH (a:Person)-[r:KNOWS]->(b:Person)
WHERE a.age > 25
RETURN a.name AS person, b.name AS friend
"""
)
# Example 3: Variable-length path
print_translation(
"Variable-Length Path (Friends of Friends)",
"""
MATCH (a:Person)-[:KNOWS*1..3]->(b:Person)
WHERE a.name = 'Alice'
RETURN DISTINCT b.name AS connection
"""
)
# Example 4: OPTIONAL MATCH
print_translation(
"Optional Relationships",
"""
MATCH (p:Person)
OPTIONAL MATCH (p)-[:LIKES]->(m:Movie)
RETURN p.name, m.title
"""
)
# Example 5: WITH clause (query chaining)
print_translation(
"Query Chaining with WITH",
"""
MATCH (p:Person)-[:LIVES_IN]->(c:City)
WITH c.name AS city, COUNT(p) AS population
WHERE population > 1000
RETURN city, population
ORDER BY population DESC
LIMIT 10
"""
)
# Example 6: Aggregation
print_translation(
"Aggregation and Grouping",
"""
MATCH (p:Person)-[:WORKS_AT]->(c:Company)
RETURN c.name AS company, COUNT(p) AS employeeCount, AVG(p.salary) AS avgSalary
ORDER BY employeeCount DESC
"""
)
# Example 7: Complex WHERE conditions
print_translation(
"Complex Filtering",
"""
MATCH (p:Person)
WHERE p.age >= 18 AND p.age <= 65
AND (p.city = 'NYC' OR p.city = 'SF')
AND p.email IS NOT NULL
RETURN p.name, p.age, p.city
"""
)
# Example 8: CASE expression
print_translation(
"CASE Expression",
"""
MATCH (p:Person)
RETURN p.name,
CASE
WHEN p.age < 18 THEN 'minor'
WHEN p.age < 65 THEN 'adult'
ELSE 'senior'
END AS ageGroup
"""
)
# Example 9: CREATE
print_translation(
"Create New Node",
"""
CREATE (p:Person {name: 'Bob', age: 30, city: 'NYC'})
"""
)
# Example 10: MERGE (Upsert)
print_translation(
"Merge (Upsert) Node",
"""
MERGE (p:Person {email: '[email protected]'})
SET p.name = 'Alice', p.lastSeen = timestamp()
"""
)
# Example 11: Parameters
print_translation(
"Using Parameters",
"""
MATCH (p:Person {id: $personId})
WHERE p.age > $minAge
RETURN p
""",
params={'personId': 123, 'minAge': 25}
)
# Example 12: UNION
print_translation(
"UNION Query",
"""
MATCH (p:Person) RETURN p.name AS name
UNION
MATCH (c:Company) RETURN c.name AS name
"""
)
# Example 13: String operations
print_translation(
"String Matching",
"""
MATCH (p:Person)
WHERE p.name STARTS WITH 'A'
AND p.email CONTAINS '@example.com'
RETURN p.name
"""
)
# Example 14: List operations
print_translation(
"List Membership",
"""
MATCH (p:Person)
WHERE p.city IN ['NYC', 'SF', 'LA']
RETURN p.name, p.city
"""
)
# Example 15: Multiple relationship types
print_translation(
"Multiple Relationship Types",
"""
MATCH (p:Person)-[r:KNOWS|:WORKS_WITH]->(other:Person)
WHERE p.name = 'Alice'
RETURN other.name, type(r) AS relationshipType
"""
)
if __name__ == '__main__':
main()