Skip to content

fix #79 fix explain string and add support for params #80

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 2 commits into from
Jul 6, 2020
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
13 changes: 7 additions & 6 deletions redisgraph/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
18 changes: 16 additions & 2 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()