diff --git a/redisgraph/graph.py b/redisgraph/graph.py index b05a2a7..e3d5f02 100644 --- a/redisgraph/graph.py +++ b/redisgraph/graph.py @@ -124,21 +124,22 @@ def query(self, q, params=None): if params is not None: q = self.build_params_header(params) + q - statistics = None - result_set = None - response = self.redis_con.execute_command("GRAPH.QUERY", self.name, q, "--compact") return QueryResult(self, response) def _execution_plan_to_string(self, plan): - return b"\n".join(plan) + return "\n".join(plan) - def execution_plan(self, query): + def execution_plan(self, query, params=None): """ Get the execution plan for given query, GRAPH.EXPLAIN returns an array of operations. """ - plan = self.redis_con.execute_command("GRAPH.EXPLAIN", self.name, query) + + if params is not None: + query = self.build_params_header(params) + query + + plan = self.redis_con.execute_command("GRAPH.EXPLAIN", self.name, query, query) return self._execution_plan_to_string(plan) def delete(self): diff --git a/test.py b/test.py index 00c051e..58f0bfd 100644 --- a/test.py +++ b/test.py @@ -191,18 +191,32 @@ def test_cached_execution(self): redis_graph = Graph('cached', self.r) redis_graph.query("CREATE ()") - uncached_result = redis_graph.query("MATCH (n) RETURN n") + uncached_result = redis_graph.query("MATCH (n) RETURN n, $param", {'param': [0]}) self.assertFalse(uncached_result.cached_execution) # loop to make sure the query is cached on each thread on server for x in range(0, 64): - cached_result = redis_graph.query("MATCH (n) RETURN n") + cached_result = redis_graph.query("MATCH (n) RETURN n, $param", {'param': [0]}) self.assertEqual(uncached_result.result_set, cached_result.result_set) # should be cached on all threads by now self.assertTrue(cached_result.cached_execution) 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() if __name__ == '__main__': unittest.main()