Skip to content

bring back execution plan and introduce explain #153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 6, 2021
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
15 changes: 15 additions & 0 deletions redisgraph/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,21 @@ def query(self, q, params=None, timeout=None, read_only=False):
return self.query(q, params, timeout, read_only)

def execution_plan(self, query, params=None):
"""
Get the execution plan for given query,
GRAPH.EXPLAIN returns an array of operations.

Args:
query: the query that will be executed
params: query parameters
"""
if params is not None:
query = self._build_params_header(params) + query

plan = self.redis_con.execute_command("GRAPH.EXPLAIN", self.name, query)
return "\n".join(plan)

def explain(self, query, params=None):
"""
Get the execution plan for given query,
GRAPH.EXPLAIN returns ExecutionPlan object.
Expand Down
27 changes: 20 additions & 7 deletions tests/functional/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,19 @@ def test_cached_execution(self):
redis_graph.delete()

def test_execution_plan(self):
redis_graph = Graph('execution_plan', self.r)
create_query = """CREATE (:Rider {name:'Valentino Rossi'})-[:rides]->(:Team {name:'Yamaha'}),
(:Rider {name:'Dani Pedrosa'})-[:rides]->(:Team {name:'Honda'}),
(:Rider {name:'Andrea Dovizioso'})-[:rides]->(:Team {name:'Ducati'})"""
redis_graph.query(create_query)

result = redis_graph.execution_plan("MATCH (r:Rider)-[:rides]->(t:Team) WHERE t.name = $name RETURN r.name, t.name, $params", {'name': 'Yehuda'})
expected = "Results\n Project\n Conditional Traverse | (t:Team)->(r:Rider)\n Filter\n Node By Label Scan | (t:Team)"
self.assertEqual(result, expected)

redis_graph.delete()

def test_explain(self):
redis_graph = Graph('execution_plan', self.r)
# graph creation / population
create_query = """CREATE
Expand All @@ -253,13 +266,13 @@ def test_execution_plan(self):
(:Rider {name:'Andrea Dovizioso'})-[:rides]->(:Team {name:'Ducati'})"""
redis_graph.query(create_query)

result = redis_graph.execution_plan("""MATCH (r:Rider)-[:rides]->(t:Team)
WHERE t.name = $name
RETURN r.name, t.name
UNION
MATCH (r:Rider)-[:rides]->(t:Team)
WHERE t.name = $name
RETURN r.name, t.name""", {'name': 'Yamaha'})
result = redis_graph.explain("""MATCH (r:Rider)-[:rides]->(t:Team)
WHERE t.name = $name
RETURN r.name, t.name
UNION
MATCH (r:Rider)-[:rides]->(t:Team)
WHERE t.name = $name
RETURN r.name, t.name""", {'name': 'Yamaha'})
expected = '''\
Results
Distinct
Expand Down