Skip to content

Commit 0e68f0b

Browse files
committed
fix #79 fix explain string and add support for params
1 parent db1f290 commit 0e68f0b

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

redisgraph/graph.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,21 +124,22 @@ def query(self, q, params=None):
124124
if params is not None:
125125
q = self.build_params_header(params) + q
126126

127-
statistics = None
128-
result_set = None
129-
130127
response = self.redis_con.execute_command("GRAPH.QUERY", self.name, q, "--compact")
131128
return QueryResult(self, response)
132129

133130
def _execution_plan_to_string(self, plan):
134-
return b"\n".join(plan)
131+
return "\n".join(plan)
135132

136-
def execution_plan(self, query):
133+
def execution_plan(self, query, params=None):
137134
"""
138135
Get the execution plan for given query,
139136
GRAPH.EXPLAIN returns an array of operations.
140137
"""
141-
plan = self.redis_con.execute_command("GRAPH.EXPLAIN", self.name, query)
138+
139+
if params is not None:
140+
query = self.build_params_header(params) + query
141+
142+
plan = self.redis_con.execute_command("GRAPH.EXPLAIN", self.name, query, query)
142143
return self._execution_plan_to_string(plan)
143144

144145
def delete(self):

test.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,18 +191,32 @@ def test_cached_execution(self):
191191
redis_graph = Graph('cached', self.r)
192192
redis_graph.query("CREATE ()")
193193

194-
uncached_result = redis_graph.query("MATCH (n) RETURN n")
194+
uncached_result = redis_graph.query("MATCH (n) RETURN n, $param", {'param': [0]})
195195
self.assertFalse(uncached_result.cached_execution)
196196

197197
# loop to make sure the query is cached on each thread on server
198198
for x in range(0, 32):
199-
cached_result = redis_graph.query("MATCH (n) RETURN n")
199+
cached_result = redis_graph.query("MATCH (n) RETURN n, $param", {'param': [0]})
200200
self.assertEqual(uncached_result.result_set, cached_result.result_set)
201201

202202
# should be cached on all threads by now
203203
self.assertTrue(cached_result.cached_execution)
204204

205205
redis_graph.delete()
206+
207+
208+
def test_execution_plan(self):
209+
redis_graph = Graph('execution_plan', self.r)
210+
create_query = """CREATE (:Rider {name:'Valentino Rossi'})-[:rides]->(:Team {name:'Yamaha'}),
211+
(:Rider {name:'Dani Pedrosa'})-[:rides]->(:Team {name:'Honda'}),
212+
(:Rider {name:'Andrea Dovizioso'})-[:rides]->(:Team {name:'Ducati'})"""
213+
redis_graph.query(create_query)
214+
215+
result = redis_graph.execution_plan("MATCH (r:Rider)-[:rides]->(t:Team) WHERE t.name = $name RETURN r.name, t.name, $params", {'name': 'Yehuda'})
216+
expected = "Results\n Project\n Conditional Traverse | (t:Team)->(r:Rider)\n Filter\n Node By Label Scan | (t:Team)"
217+
self.assertEqual(result, expected)
218+
219+
redis_graph.delete()
206220

207221
if __name__ == '__main__':
208222
unittest.main()

0 commit comments

Comments
 (0)