Cypher to GFQL Python & Wire Protocol Mapping#
Translate existing Cypher workloads to GPU-accelerated GFQL with minimal code changes.
Introduction#
This specification shows how to translate Cypher queries to both GFQL Python code and :ref:Wire Protocol <gfql-spec-wire-protocol> JSON, enabling migration from Cypher-based systems, LLM pipelines (text → Cypher → GFQL), language-agnostic API integration, and secure query generation without code execution.
What Maps 1-to-1#
When translating from Cypher, you’ll encounter three scenarios:
1. Direct Translation - Most pattern matching maps cleanly to pure GFQL 2. Hybrid Approach - Post-processing operations (RETURN clauses with aggregations) use df.groupby/agg 3. GFQL Advantages - Some capabilities go beyond what Cypher offers
Direct Translations#
Graph patterns:
(a)-[r]->(b)→ chain operationsProperty filters: WHERE clauses embed into operations
Path traversals: Variable-length paths use
hopsparameterPattern composition: Multiple patterns become sequential operations
When You Need DataFrames#
Aggregations: COUNT, SUM, AVG → pandas operations
Projections: RETURN specific columns → DataFrame selection
Sorting/limiting: ORDER BY, LIMIT → DataFrame methods
Joins: Multiple disconnected patterns → pandas merge
GFQL-Only Super-Powers#
Edge properties: Query edges as first-class entities
Dataframe-native: Zero-cost transitions between graph and tabular operations
GPU acceleration: Parallel execution on NVIDIA hardware
Heterogeneous graphs: No schema constraints on types or properties
Integrated visualization: Layouts like
group_in_a_box_layoutfor community visualizationAlgorithm chaining: Combine community detection with layout algorithms
Quick Example#
Cypher:
MATCH (p:Person)-[r:FOLLOWS]->(q:Person)
WHERE p.age > 30
Python:
g.gfql([
n({"type": "Person", "age": gt(30)}, name="p"),
e_forward({"type": "FOLLOWS"}, name="r"),
n({"type": "Person"}, name="q")
])
Wire Protocol:
{"type": "Chain", "chain": [
{"type": "Node", "filter_dict": {"type": "Person", "age": {"type": "GT", "val": 30}}, "name": "p"},
{"type": "Edge", "direction": "forward", "edge_match": {"type": "FOLLOWS"}, "name": "r"},
{"type": "Node", "filter_dict": {"type": "Person"}, "name": "q"}
]}
Translation Tables#
Node Patterns#
Cypher |
Python |
Wire Protocol |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
Edge Patterns#
Cypher |
Python |
Wire Protocol (compact) |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Predicates#
Cypher |
Python |
Wire Protocol |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Complete Examples#
Friend of Friend#
Cypher:
MATCH (u:User {name: 'Alice'})-[:FRIEND*2]->(fof:User)
WHERE fof.active = true
Python:
g.gfql([
n({"type": "User", "name": "Alice"}),
e_forward({"type": "FRIEND"}, min_hops=2, max_hops=2),
n({"type": "User", "active": True}, name="fof")
])
Wire Protocol:
{"type": "Chain", "chain": [
{"type": "Node", "filter_dict": {"type": "User", "name": "Alice"}},
{"type": "Edge", "direction": "forward", "edge_match": {"type": "FRIEND"}, "min_hops": 2, "max_hops": 2},
{"type": "Node", "filter_dict": {"type": "User", "active": true}, "name": "fof"}
]}
Fraud Detection#
Cypher:
MATCH (a:Account)-[t:TRANSFER]->(b:Account)
WHERE t.amount > 10000 AND t.date > date('2024-01-01')
Python:
g.gfql([
n({"type": "Account"}),
e_forward({
"type": "TRANSFER",
"amount": gt(10000),
"date": gt(date(2024,1,1))
}, name="t"),
n({"type": "Account"})
])
Wire Protocol:
{"type": "Chain", "chain": [
{"type": "Node", "filter_dict": {"type": "Account"}},
{"type": "Edge", "direction": "forward", "edge_match": {
"type": "TRANSFER",
"amount": {"type": "GT", "val": 10000},
"date": {"type": "GT", "val": {"type": "date", "value": "2024-01-01"}}
}, "name": "t"},
{"type": "Node", "filter_dict": {"type": "Account"}}
]}
Complex Aggregation Example#
Cypher:
MATCH (u:User)-[t:TRANSACTION]->(m:Merchant)
WHERE t.date > date('2024-01-01')
RETURN m.category, count(*) as cnt, sum(t.amount) as total
ORDER BY total DESC
LIMIT 10
Python:
# Step 1: Graph pattern
result = g.gfql([
n({"type": "User"}),
e_forward({"type": "TRANSACTION", "date": gt(date(2024,1,1))}, name="trans"),
n({"type": "Merchant"})
])
# Step 2: DataFrame operations
trans_df = result._edges[result._edges["trans"]]
merchant_df = result._nodes
analysis = (trans_df
.merge(merchant_df, left_on=g._destination, right_on=g._node)
.groupby('category')
.agg(cnt=('amount', 'count'), total=('amount', 'sum'))
.nlargest(10, 'total'))
Note: Wire protocol returns the filtered graph; aggregations require client-side processing.
DataFrame Operations Mapping#
Cypher Feature |
Python DataFrame Operation |
Notes |
|---|---|---|
|
|
Column selection |
|
|
Remove duplicates |
|
|
Sort results |
|
|
Limit rows |
|
|
Count rows |
|
|
Aggregation |
|
|
Collect to list |
Named patterns |
|
Boolean column filtering |
Key Differences#
Feature |
Python |
Wire Protocol |
|---|---|---|
Temporal values |
|
|
Direct equality |
|
|
Comparisons |
|
|
Collections |
|
|
Not Supported#
OPTIONAL MATCH- No equivalent (would need outer joins)CREATE,DELETE,SET- GFQL is read-onlyWITHclauses - Requires intermediate variablesMultiple
MATCHpatterns - Use separate chains or joins
Best Practices#
Direct Translation First: Try pure GFQL before adding DataFrame operations
Use Named Patterns: Label important results with
name=for easy accessFilter Early: Apply selective node filters before traversing edges
Type Consistency: Ensure wire protocol types match expected column types
Validate JSON: Test wire protocol against schema before sending
LLM Integration Guide#
When building translators:
Given Cypher: {cypher_query}
Generate both:
1. Python: Human-readable GFQL code
2. Wire Protocol: JSON for API calls
Rules:
- (n:Label) → Python: n({"type": "Label"}) → JSON: {"type": "Node", "filter_dict": {"type": "Label"}}
- WHERE → Embed as predicates in both formats
- Aggregations → Note as requiring DataFrame post-processing
See Also#
GFQL Wire Protocol Specification - Full wire protocol specification
GFQL Language Specification - Language specification
GFQL Python Embedding - Python implementation details