Skip to content

Commit 76246ba

Browse files
DvirDukhanswilly22
authored andcommitted
added list/array support (#43)
* added list/array support * added node and edge parsing as scalars * array unit test * array parsing done in place * fixed PR comments
1 parent d65ec32 commit 76246ba

File tree

2 files changed

+68
-20
lines changed

2 files changed

+68
-20
lines changed

redisgraph/query_result.py

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,25 @@
22
from .edge import Edge
33
from prettytable import PrettyTable
44

5+
56
class ResultSetColumnTypes(object):
67
COLUMN_UNKNOWN = 0
78
COLUMN_SCALAR = 1
89
COLUMN_NODE = 2
910
COLUMN_RELATION = 3
1011

12+
1113
class ResultSetScalarTypes(object):
12-
PROPERTY_UNKNOWN = 0
13-
PROPERTY_NULL = 1
14-
PROPERTY_STRING = 2
15-
PROPERTY_INTEGER = 3
16-
PROPERTY_BOOLEAN = 4
17-
PROPERTY_DOUBLE = 5
14+
VALUE_UNKNOWN = 0
15+
VALUE_NULL = 1
16+
VALUE_STRING = 2
17+
VALUE_INTEGER = 3
18+
VALUE_BOOLEAN = 4
19+
VALUE_DOUBLE = 5
20+
VALUE_ARRAY = 6
21+
VALUE_EDGE = 7
22+
VALUE_NODE = 8
23+
1824

1925
class QueryResult(object):
2026
LABELS_ADDED = 'Labels added'
@@ -34,11 +40,11 @@ def __init__(self, graph, response):
3440
self.parse_statistics(response[0])
3541
else:
3642
self.parse_results(response)
37-
self.parse_statistics(response[-1]) # Last element.
43+
self.parse_statistics(response[-1]) # Last element.
3844

3945
def parse_results(self, raw_result_set):
4046
self.header = self.parse_header(raw_result_set)
41-
47+
4248
# Empty header.
4349
if len(self.header) == 0:
4450
return
@@ -48,7 +54,8 @@ def parse_results(self, raw_result_set):
4854
def parse_statistics(self, raw_statistics):
4955
self.statistics = {}
5056

51-
stats = [self.LABELS_ADDED, self.NODES_CREATED, self.PROPERTIES_SET, self.RELATIONSHIPS_CREATED, self.NODES_DELETED, self.RELATIONSHIPS_DELETED, self.INTERNAL_EXECUTION_TIME]
57+
stats = [self.LABELS_ADDED, self.NODES_CREATED, self.PROPERTIES_SET, self.RELATIONSHIPS_CREATED,
58+
self.NODES_DELETED, self.RELATIONSHIPS_DELETED, self.INTERNAL_EXECUTION_TIME]
5259
for s in stats:
5360
v = self._get_value(s, raw_statistics)
5461
if v is not None:
@@ -81,7 +88,7 @@ def parse_entity_properties(self, props):
8188
# [[name, value type, value] X N]
8289
properties = {}
8390
for prop in props:
84-
prop_name = self.graph.get_property(prop[0])
91+
prop_name = self.graph.get_property(prop[0])
8592
prop_value = self.parse_scalar(prop[1:])
8693
properties[prop_name] = prop_value
8794

@@ -118,16 +125,16 @@ def parse_scalar(self, cell):
118125
value = cell[1]
119126
scalar = None
120127

121-
if scalar_type == ResultSetScalarTypes.PROPERTY_NULL:
128+
if scalar_type == ResultSetScalarTypes.VALUE_NULL:
122129
scalar = None
123130

124-
elif scalar_type == ResultSetScalarTypes.PROPERTY_STRING:
131+
elif scalar_type == ResultSetScalarTypes.VALUE_STRING:
125132
scalar = str(value)
126-
127-
elif scalar_type == ResultSetScalarTypes.PROPERTY_INTEGER:
133+
134+
elif scalar_type == ResultSetScalarTypes.VALUE_INTEGER:
128135
scalar = int(value)
129136

130-
elif scalar_type == ResultSetScalarTypes.PROPERTY_BOOLEAN:
137+
elif scalar_type == ResultSetScalarTypes.VALUE_BOOLEAN:
131138
value = value.decode() if isinstance(value, bytes) else value
132139
if value == "true":
133140
scalar = True
@@ -136,10 +143,22 @@ def parse_scalar(self, cell):
136143
else:
137144
print("Unknown boolean type\n")
138145

139-
elif scalar_type == ResultSetScalarTypes.PROPERTY_DOUBLE:
146+
elif scalar_type == ResultSetScalarTypes.VALUE_DOUBLE:
140147
scalar = float(value)
141148

142-
elif scalar_type == ResultSetScalarTypes.PROPERTY_UNKNOWN:
149+
elif scalar_type == ResultSetScalarTypes.VALUE_ARRAY:
150+
# array variable is introduced only for readability
151+
scalar = array = value
152+
for i in range(len(array)):
153+
scalar[i] = self.parse_scalar(array[i])
154+
155+
elif scalar_type == ResultSetScalarTypes.VALUE_NODE:
156+
scalar = self.parse_node(value)
157+
158+
elif scalar_type == ResultSetScalarTypes.VALUE_EDGE:
159+
scalar = self.parse_edge(value)
160+
161+
elif scalar_type == ResultSetScalarTypes.VALUE_UNKNOWN:
143162
print("Unknown scalar type\n")
144163

145164
return scalar
@@ -150,6 +169,7 @@ def parse_scalar(self, cell):
150169
2. The row after that will contain the data returned, or 'No Data returned' if there is none.
151170
3. Prints the statistics of the query.
152171
"""
172+
153173
def pretty_print(self):
154174
if not self.is_empty():
155175
header = [col[1] for col in self.header]
@@ -172,7 +192,7 @@ def pretty_print(self):
172192
print(str(tbl) + '\n')
173193

174194
for stat in self.statistics:
175-
print("%s %s" %(stat, self.statistics[stat]))
195+
print("%s %s" % (stat, self.statistics[stat]))
176196

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

193213
@property
194-
def labels_added(self):
214+
def labels_added(self):
195215
return self._get_stat(self.LABELS_ADDED)
196216

197217
@property
@@ -217,4 +237,3 @@ def relationships_deleted(self):
217237
@property
218238
def run_time_ms(self):
219239
return self._get_stat(self.INTERNAL_EXECUTION_TIME)
220-

test.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,37 @@ def test_graph_creation(self):
3333
self.assertEqual(visit.properties, edge.properties)
3434
self.assertEqual(country, japan)
3535

36+
query = """RETURN [1, 2.3, "4", true, false, null]"""
37+
result = redis_graph.query(query)
38+
self.assertEqual([1, 2.3, "4", True, False, None], result.result_set[0][0])
39+
3640
# All done, remove graph.
3741
redis_graph.delete()
3842

43+
def test_array_functions(self):
44+
redis_graph = Graph('social', self.r)
45+
46+
query = """CREATE (p:person{name:'a',age:32, array:[0,1,2]})"""
47+
redis_graph.query(query)
48+
49+
query = """CREATE (p:person{name:'b',age:30, array:[3,4,5]})"""
50+
redis_graph.query(query)
51+
52+
query = """WITH [0,1,2] as x return x"""
53+
result = redis_graph.query(query)
54+
self.assertEqual([0, 1, 2], result.result_set[0][0])
55+
56+
query = """MATCH(n) return collect(n) as x"""
57+
result = redis_graph.query(query)
58+
59+
a = Node(label='person', properties={'name': 'a', 'age': 32, 'array': [0, 1, 2]})
60+
b = Node(label='person', properties={'name': 'b', 'age': 30, 'array': [3, 4, 5]})
61+
62+
self.assertEqual([a, b], result.result_set[0][0])
63+
64+
65+
# All done, remove graph.
66+
redis_graph.delete()
67+
3968
if __name__ == '__main__':
4069
unittest.main()

0 commit comments

Comments
 (0)