-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_scan_pyarrow.py
More file actions
335 lines (278 loc) · 11.1 KB
/
test_scan_pyarrow.py
File metadata and controls
335 lines (278 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import pyarrow as pa
import pytest
from type_aliases import ConnDB
def test_create_arrow_table_keeps_pyarrow_memory_alive(conn_db_empty: ConnDB) -> None:
conn, _ = conn_db_empty
def register_table() -> None:
table = pa.Table.from_arrays(
[
pa.array([1, 2, 3], type=pa.int64()),
pa.array(["alice", "bob", "carol"], type=pa.string()),
],
names=["id", "name"],
)
conn.create_arrow_table("people_arrow_gc", table)
register_table()
import gc
gc.collect()
result = conn.execute("MATCH (n:people_arrow_gc) RETURN n.id, n.name ORDER BY n.id")
assert result.get_next() == [1, "alice"]
assert result.get_next() == [2, "bob"]
assert result.get_next() == [3, "carol"]
assert not result.has_next()
conn.drop_arrow_table("people_arrow_gc")
def test_pyarrow_basic(conn_db_readonly: ConnDB) -> None:
conn, _ = conn_db_readonly
tab = pa.Table.from_arrays(
[
[1, 2, 3],
["a", "b", "c"],
[True, False, None],
],
names=["col1", "col2", "col3"],
)
result = conn.execute("LOAD FROM tab RETURN * ORDER BY col1")
assert result.get_next() == [1, "a", True]
assert result.get_next() == [2, "b", False]
assert result.get_next() == [3, "c", None]
def test_pyarrow_copy_from_parameterized_df(conn_db_readwrite: ConnDB) -> None:
conn, _ = conn_db_readwrite
def get_tab_func():
return pa.Table.from_arrays(
[
pa.array([1, 2, 3], type=pa.int32()),
pa.array(["a", "b", "c"], type=pa.string()),
pa.array([True, False, None], type=pa.bool_()),
],
names=["id", "A", "B"],
)
conn.execute(
"CREATE NODE TABLE pyarrowtab(id INT32, A STRING, B BOOL, PRIMARY KEY(id))"
)
conn.execute("COPY pyarrowtab FROM $tab", {"tab": get_tab_func()})
result = conn.execute(
"MATCH (t:pyarrowtab) RETURN t.id AS id, t.A AS A, t.B AS B ORDER BY t.id"
)
assert result.get_next() == [1, "a", True]
assert result.get_next() == [2, "b", False]
assert result.get_next() == [3, "c", None]
rels = pa.Table.from_arrays(
[pa.array([1, 2, 3], type=pa.int32()), pa.array([2, 3, 1], type=pa.int32())],
names=["from", "to"],
)
conn.execute("CREATE REL TABLE pyarrowrel(FROM pyarrowtab TO pyarrowtab)")
conn.execute("COPY pyarrowrel FROM $tab", {"tab": rels})
result = conn.execute(
"MATCH (a:pyarrowtab)-[:pyarrowrel]->(b:pyarrowtab) RETURN a.id, b.id ORDER BY a.id"
)
assert result.get_next() == [1, 2]
assert result.get_next() == [2, 3]
assert result.get_next() == [3, 1]
def test_create_arrow_table_from_pyarrow_table(conn_db_empty: ConnDB) -> None:
conn, _ = conn_db_empty
tab = pa.Table.from_arrays(
[
pa.array([1, 2, 3], type=pa.int64()),
pa.array(["alice", "bob", "carol"], type=pa.string()),
],
names=["id", "name"],
)
conn.create_arrow_table("people_arrow", tab)
result = conn.execute("MATCH (n:people_arrow) RETURN n.id, n.name ORDER BY n.id")
assert result.get_next() == [1, "alice"]
assert result.get_next() == [2, "bob"]
assert result.get_next() == [3, "carol"]
assert not result.has_next()
conn.drop_arrow_table("people_arrow")
def test_pyarrow_to_filtered_pyarrow_table(conn_db_empty: ConnDB) -> None:
conn, _ = conn_db_empty
tab = pa.Table.from_arrays(
[
pa.array([4, 1, 3, 2], type=pa.int64()),
pa.array(["bob", "alice", "bob", "carol"], type=pa.string()),
],
names=["id", "name"],
)
conn.create_arrow_table("people_arrow_filter", tab)
filtered = conn.execute(
"MATCH (n:people_arrow_filter) "
"WHERE n.name = 'bob' "
"RETURN n.id, n.name "
"ORDER BY n.id"
).get_as_arrow()
assert filtered.column("n.id").to_pylist() == [3, 4]
assert filtered.column("n.name").to_pylist() == ["bob", "bob"]
conn.drop_arrow_table("people_arrow_filter")
def test_pyarrow_copy_from_invalid_source(conn_db_readwrite: ConnDB) -> None:
conn, _ = conn_db_readwrite
conn.execute(
"CREATE NODE TABLE pyarrowtab(id INT32, A STRING, B BOOL, PRIMARY KEY(id))"
)
with pytest.raises(
RuntimeError,
match=r"Binder exception: Trying to scan from unsupported data type INT(8|64)\[\]. The only parameter types that can be scanned from are pandas/polars dataframes and pyarrow tables.",
):
conn.execute("COPY pyarrowtab FROM $tab", {"tab": [1, 2, 3]})
def test_pyarrow_copy_from(conn_db_readwrite: ConnDB) -> None:
conn, _ = conn_db_readwrite
tab = pa.Table.from_arrays(
[
pa.array([1, 2, 3], type=pa.int32()),
pa.array(["a", "b", "c"], type=pa.string()),
pa.array([True, False, None], type=pa.bool_()),
],
names=["id", "A", "B"],
)
conn.execute(
"CREATE NODE TABLE pyarrowtab(id INT32, A STRING, B BOOL, PRIMARY KEY(id))"
)
conn.execute("COPY pyarrowtab FROM tab")
result = conn.execute(
"MATCH (t:pyarrowtab) RETURN t.id AS id, t.A AS A, t.B AS B ORDER BY t.id"
)
assert result.get_next() == [1, "a", True]
assert result.get_next() == [2, "b", False]
assert result.get_next() == [3, "c", None]
rels = pa.Table.from_arrays(
[pa.array([1, 2, 3], type=pa.int32()), pa.array([2, 3, 1], type=pa.int32())],
names=["from", "to"],
)
conn.execute("CREATE REL TABLE pyarrowrel(FROM pyarrowtab TO pyarrowtab)")
conn.execute("COPY pyarrowrel FROM rels")
result = conn.execute(
"MATCH (a:pyarrowtab)-[:pyarrowrel]->(b:pyarrowtab) RETURN a.id, b.id ORDER BY a.id"
)
assert result.get_next() == [1, 2]
assert result.get_next() == [2, 3]
assert result.get_next() == [3, 1]
conn.execute("DROP TABLE pyarrowrel")
rels = pa.Table.from_arrays(
[
pa.array([1, 2, 3], type=pa.int32()),
pa.array([2, 3, 1], type=pa.int32()),
pa.array(["honk", "shoo", "mimimimimimimi"], type=pa.string()),
],
names=["foo", "bar", "spongebobmeboy"],
)
conn.execute(
"CREATE REL TABLE pyarrowrel(FROM pyarrowtab TO pyarrowtab, a_distraction STRING)"
)
conn.execute("COPY pyarrowrel FROM rels")
result = conn.execute(
"MATCH (a:pyarrowtab)-[r:pyarrowrel]->(b:pyarrowtab) RETURN a.id, r.a_distraction, b.id ORDER BY a.id"
)
assert result.get_next() == [1, "honk", 2]
assert result.get_next() == [2, "shoo", 3]
assert result.get_next() == [3, "mimimimimimimi", 1]
def test_pyarrow_scan_ignore_errors(conn_db_readwrite: ConnDB) -> None:
conn, _ = conn_db_readwrite
tab = pa.Table.from_arrays(
[
pa.array([1, 2, 3, 1], type=pa.int32()),
],
names=["id"],
)
conn.execute("CREATE NODE TABLE ids(id INT64, PRIMARY KEY(id))")
conn.execute("COPY ids FROM tab(IGNORE_ERRORS=true)")
people = conn.execute("MATCH (i:ids) RETURN i.id")
assert people.get_next() == [1]
assert people.get_next() == [2]
assert people.get_next() == [3]
assert not people.has_next()
warnings = conn.execute("CALL show_warnings() RETURN *")
assert warnings.get_next()[1].startswith("Found duplicated primary key value 1")
assert not warnings.has_next()
def test_pyarrow_scan_invalid_option(conn_db_readwrite: ConnDB) -> None:
conn, _ = conn_db_readwrite
tab = pa.Table.from_arrays(
[
pa.array([1, 2, 3], type=pa.int32()),
],
names=["id"],
)
conn.execute("CREATE NODE TABLE ids(id INT64, PRIMARY KEY(id))")
error_message = "INVALID_OPTION Option not recognized by pyArrow scanner."
with pytest.raises(RuntimeError, match=error_message):
conn.execute("COPY ids FROM tab(INVALID_OPTION=1)")
def test_copy_from_pyarrow_multi_pairs(conn_db_readwrite: ConnDB) -> None:
conn, _ = conn_db_readwrite
conn.execute("CREATE NODE TABLE prof(id INT64, PRIMARY KEY(id))")
conn.execute("CREATE (p:prof {id: 3});")
conn.execute("CREATE (p:prof {id: 4});")
conn.execute("CREATE NODE TABLE student(id INT64, PRIMARY KEY(id))")
conn.execute("CREATE (p:student {id: 2});")
conn.execute(
"CREATE REL TABLE teaches(from prof to prof, from prof to student, length int64)"
)
df = pa.Table.from_arrays(
[
pa.array([3], type=pa.int64()),
pa.array([4], type=pa.int64()),
pa.array([252], type=pa.int64()),
],
names=["from", "to", "length"],
)
conn.execute("COPY teaches from df (from = 'prof', to = 'prof');")
result = conn.execute("match (:prof)-[e:teaches]->(:prof) return e.*")
assert result.has_next()
assert result.get_next()[0] == 252
assert not result.has_next()
def test_create_arrow_rel_table_from_pyarrow_table_query_results(
conn_db_empty: ConnDB,
) -> None:
conn, _ = conn_db_empty
conn.execute("CREATE NODE TABLE person(id INT64, PRIMARY KEY(id))")
conn.execute("CREATE (:person {id: 1})")
conn.execute("CREATE (:person {id: 2})")
conn.execute("CREATE (:person {id: 3})")
rels = pa.Table.from_arrays(
[
pa.array([1, 2, 3], type=pa.int64()),
pa.array([2, 3, 1], type=pa.int64()),
],
names=["from", "to"],
)
conn.create_arrow_rel_table("knows_arrow_rel_reg", rels, "person", "person")
result = conn.execute(
"MATCH (a:person)-[r:knows_arrow_rel_reg]->(b:person) "
"RETURN a.id, b.id ORDER BY a.id"
)
rows = []
while result.has_next():
rows.append(result.get_next())
assert rows == [[1, 2], [2, 3], [3, 1]]
conn.drop_arrow_table("knows_arrow_rel_reg")
def test_arrow_node_and_arrow_rel_with_filtering_query(conn_db_empty: ConnDB) -> None:
conn, _ = conn_db_empty
people = pa.Table.from_arrays(
[
pa.array([1, 2, 3, 4], type=pa.int64()),
pa.array(["eng", "sales", "eng", "hr"], type=pa.string()),
],
names=["id", "department"],
)
conn.create_arrow_table("people_arrow_mix", people)
conn.execute("CREATE NODE TABLE person(id INT64, PRIMARY KEY(id))")
conn.execute("CREATE (:person {id: 1})")
conn.execute("CREATE (:person {id: 2})")
conn.execute("CREATE (:person {id: 3})")
rels = pa.Table.from_arrays(
[
pa.array([1, 2, 3], type=pa.int64()),
pa.array([2, 3, 1], type=pa.int64()),
pa.array([5, 6, 7], type=pa.int64()),
],
names=["from", "to", "weight"],
)
conn.create_arrow_rel_table("knows_arrow_mix", rels, "person", "person")
result = conn.execute(
"MATCH (x:people_arrow_mix), (a:person)-[r:knows_arrow_mix]->(b:person) "
"WHERE x.department='eng' AND a.id >= 2 "
"RETURN x.id, a.id, b.id, r.weight ORDER BY x.id, a.id"
)
rows = []
while result.has_next():
rows.append(result.get_next())
assert rows == [[1, 2, 3, 6], [1, 3, 1, 7], [3, 2, 3, 6], [3, 3, 1, 7]]
conn.drop_arrow_table("knows_arrow_mix")
conn.drop_arrow_table("people_arrow_mix")