Skip to content

added list/array support #43

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 5 commits into from
Sep 5, 2019
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
59 changes: 39 additions & 20 deletions redisgraph/query_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@
from .edge import Edge
from prettytable import PrettyTable


class ResultSetColumnTypes(object):
COLUMN_UNKNOWN = 0
COLUMN_SCALAR = 1
COLUMN_NODE = 2
COLUMN_RELATION = 3


class ResultSetScalarTypes(object):
PROPERTY_UNKNOWN = 0
PROPERTY_NULL = 1
PROPERTY_STRING = 2
PROPERTY_INTEGER = 3
PROPERTY_BOOLEAN = 4
PROPERTY_DOUBLE = 5
VALUE_UNKNOWN = 0
VALUE_NULL = 1
VALUE_STRING = 2
VALUE_INTEGER = 3
VALUE_BOOLEAN = 4
VALUE_DOUBLE = 5
VALUE_ARRAY = 6
VALUE_EDGE = 7
VALUE_NODE = 8


class QueryResult(object):
LABELS_ADDED = 'Labels added'
Expand All @@ -34,11 +40,11 @@ def __init__(self, graph, response):
self.parse_statistics(response[0])
else:
self.parse_results(response)
self.parse_statistics(response[-1]) # Last element.
self.parse_statistics(response[-1]) # Last element.

def parse_results(self, raw_result_set):
self.header = self.parse_header(raw_result_set)

# Empty header.
if len(self.header) == 0:
return
Expand All @@ -48,7 +54,8 @@ def parse_results(self, raw_result_set):
def parse_statistics(self, raw_statistics):
self.statistics = {}

stats = [self.LABELS_ADDED, self.NODES_CREATED, self.PROPERTIES_SET, self.RELATIONSHIPS_CREATED, self.NODES_DELETED, self.RELATIONSHIPS_DELETED, self.INTERNAL_EXECUTION_TIME]
stats = [self.LABELS_ADDED, self.NODES_CREATED, self.PROPERTIES_SET, self.RELATIONSHIPS_CREATED,
self.NODES_DELETED, self.RELATIONSHIPS_DELETED, self.INTERNAL_EXECUTION_TIME]
for s in stats:
v = self._get_value(s, raw_statistics)
if v is not None:
Expand Down Expand Up @@ -81,7 +88,7 @@ def parse_entity_properties(self, props):
# [[name, value type, value] X N]
properties = {}
for prop in props:
prop_name = self.graph.get_property(prop[0])
prop_name = self.graph.get_property(prop[0])
prop_value = self.parse_scalar(prop[1:])
properties[prop_name] = prop_value

Expand Down Expand Up @@ -118,16 +125,16 @@ def parse_scalar(self, cell):
value = cell[1]
scalar = None

if scalar_type == ResultSetScalarTypes.PROPERTY_NULL:
if scalar_type == ResultSetScalarTypes.VALUE_NULL:
scalar = None

elif scalar_type == ResultSetScalarTypes.PROPERTY_STRING:
elif scalar_type == ResultSetScalarTypes.VALUE_STRING:
scalar = str(value)
elif scalar_type == ResultSetScalarTypes.PROPERTY_INTEGER:

elif scalar_type == ResultSetScalarTypes.VALUE_INTEGER:
scalar = int(value)

elif scalar_type == ResultSetScalarTypes.PROPERTY_BOOLEAN:
elif scalar_type == ResultSetScalarTypes.VALUE_BOOLEAN:
value = value.decode() if isinstance(value, bytes) else value
if value == "true":
scalar = True
Expand All @@ -136,10 +143,22 @@ def parse_scalar(self, cell):
else:
print("Unknown boolean type\n")

elif scalar_type == ResultSetScalarTypes.PROPERTY_DOUBLE:
elif scalar_type == ResultSetScalarTypes.VALUE_DOUBLE:
scalar = float(value)

elif scalar_type == ResultSetScalarTypes.PROPERTY_UNKNOWN:
elif scalar_type == ResultSetScalarTypes.VALUE_ARRAY:
# array variable is introduced only for readability
scalar = array = value
for i in range(len(array)):
scalar[i] = self.parse_scalar(array[i])

elif scalar_type == ResultSetScalarTypes.VALUE_NODE:
scalar = self.parse_node(value)

elif scalar_type == ResultSetScalarTypes.VALUE_EDGE:
scalar = self.parse_edge(value)

elif scalar_type == ResultSetScalarTypes.VALUE_UNKNOWN:
print("Unknown scalar type\n")

return scalar
Expand All @@ -150,6 +169,7 @@ def parse_scalar(self, cell):
2. The row after that will contain the data returned, or 'No Data returned' if there is none.
3. Prints the statistics of the query.
"""

def pretty_print(self):
if not self.is_empty():
header = [col[1] for col in self.header]
Expand All @@ -172,7 +192,7 @@ def pretty_print(self):
print(str(tbl) + '\n')

for stat in self.statistics:
print("%s %s" %(stat, self.statistics[stat]))
print("%s %s" % (stat, self.statistics[stat]))

def is_empty(self):
return len(self.result_set) == 0
Expand All @@ -191,7 +211,7 @@ def _get_stat(self, stat):
return self.statistics[stat] if stat in self.statistics else 0

@property
def labels_added(self):
def labels_added(self):
return self._get_stat(self.LABELS_ADDED)

@property
Expand All @@ -217,4 +237,3 @@ def relationships_deleted(self):
@property
def run_time_ms(self):
return self._get_stat(self.INTERNAL_EXECUTION_TIME)

29 changes: 29 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,37 @@ def test_graph_creation(self):
self.assertEqual(visit.properties, edge.properties)
self.assertEqual(country, japan)

query = """RETURN [1, 2.3, "4", true, false, null]"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Include a sub-array within this array.

result = redis_graph.query(query)
self.assertEqual([1, 2.3, "4", True, False, None], result.result_set[0][0])

# All done, remove graph.
redis_graph.delete()

def test_array_functions(self):
redis_graph = Graph('social', self.r)

query = """CREATE (p:person{name:'a',age:32, array:[0,1,2]})"""
redis_graph.query(query)

query = """CREATE (p:person{name:'b',age:30, array:[3,4,5]})"""
redis_graph.query(query)

query = """WITH [0,1,2] as x return x"""
result = redis_graph.query(query)
self.assertEqual([0, 1, 2], result.result_set[0][0])

query = """MATCH(n) return collect(n) as x"""
result = redis_graph.query(query)

a = Node(label='person', properties={'name': 'a', 'age': 32, 'array': [0, 1, 2]})
b = Node(label='person', properties={'name': 'b', 'age': 30, 'array': [3, 4, 5]})

self.assertEqual([a, b], result.result_set[0][0])


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove extra line

# All done, remove graph.
redis_graph.delete()

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