forked from apache/arrow-rs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_sql.py
321 lines (281 loc) · 8.09 KB
/
test_sql.py
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
# -*- coding: utf-8 -*-
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import contextlib
import datetime
import decimal
import string
import pytest
import pyarrow as pa
import pytz
import arrow_pyarrow_integration_testing as rust
@contextlib.contextmanager
def no_pyarrow_leak():
# No leak of C++ memory
old_allocation = pa.total_allocated_bytes()
try:
yield
finally:
assert pa.total_allocated_bytes() == old_allocation
@pytest.fixture(autouse=True)
def assert_pyarrow_leak():
# automatically applied to all test cases
with no_pyarrow_leak():
yield
_supported_pyarrow_types = [
pa.null(),
pa.bool_(),
pa.int32(),
pa.time32("s"),
pa.time64("us"),
pa.date32(),
pa.timestamp("us"),
pa.timestamp("us", tz="UTC"),
pa.timestamp("us", tz="Europe/Paris"),
pa.duration("s"),
pa.duration("ms"),
pa.duration("us"),
pa.duration("ns"),
pa.float16(),
pa.float32(),
pa.float64(),
pa.decimal128(19, 4),
pa.string(),
pa.binary(),
pa.binary(10),
pa.large_string(),
pa.large_binary(),
pa.list_(pa.int32()),
pa.list_(pa.int32(), 2),
pa.large_list(pa.uint16()),
pa.struct(
[
pa.field("a", pa.int32()),
pa.field("b", pa.int8()),
pa.field("c", pa.string()),
]
),
pa.struct(
[
pa.field("a", pa.int32(), nullable=False),
pa.field("b", pa.int8(), nullable=False),
pa.field("c", pa.string()),
]
),
pa.dictionary(pa.int8(), pa.string()),
]
_unsupported_pyarrow_types = [
pa.decimal256(76, 38),
pa.map_(pa.string(), pa.int32()),
pa.union(
[pa.field("a", pa.binary(10)), pa.field("b", pa.string())],
mode=pa.lib.UnionMode_DENSE,
),
pa.union(
[pa.field("a", pa.binary(10)), pa.field("b", pa.string())],
mode=pa.lib.UnionMode_DENSE,
type_codes=[4, 8],
),
pa.union(
[pa.field("a", pa.binary(10)), pa.field("b", pa.string())],
mode=pa.lib.UnionMode_SPARSE,
),
pa.union(
[
pa.field("a", pa.binary(10), nullable=False),
pa.field("b", pa.string()),
],
mode=pa.lib.UnionMode_SPARSE,
),
]
@pytest.mark.parametrize("pyarrow_type", _supported_pyarrow_types, ids=str)
def test_type_roundtrip(pyarrow_type):
restored = rust.round_trip_type(pyarrow_type)
assert restored == pyarrow_type
assert restored is not pyarrow_type
@pytest.mark.parametrize("pyarrow_type", _unsupported_pyarrow_types, ids=str)
def test_type_roundtrip_raises(pyarrow_type):
with pytest.raises(pa.ArrowException):
rust.round_trip_type(pyarrow_type)
@pytest.mark.parametrize('pyarrow_type', _supported_pyarrow_types, ids=str)
def test_field_roundtrip(pyarrow_type):
pyarrow_field = pa.field("test", pyarrow_type, nullable=True)
field = rust.round_trip_field(pyarrow_field)
assert field == pyarrow_field
if pyarrow_type != pa.null():
# A null type field may not be non-nullable
pyarrow_field = pa.field("test", pyarrow_type, nullable=False)
field = rust.round_trip_field(pyarrow_field)
assert field == pyarrow_field
def test_schema_roundtrip():
pyarrow_fields = zip(string.ascii_lowercase, _supported_pyarrow_types)
pyarrow_schema = pa.schema(pyarrow_fields)
schema = rust.round_trip_schema(pyarrow_schema)
assert schema == pyarrow_schema
def test_primitive_python():
"""
Python -> Rust -> Python
"""
a = pa.array([1, 2, 3])
b = rust.double(a)
assert b == pa.array([2, 4, 6])
del a
del b
def test_primitive_rust():
"""
Rust -> Python -> Rust
"""
def double(array):
array = array.to_pylist()
return pa.array([x * 2 if x is not None else None for x in array])
is_correct = rust.double_py(double)
assert is_correct
def test_string_python():
"""
Python -> Rust -> Python
"""
a = pa.array(["a", None, "ccc"])
b = rust.substring(a, 1)
assert b == pa.array(["", None, "cc"])
del a
del b
def test_time32_python():
"""
Python -> Rust -> Python
"""
a = pa.array([None, 1, 2], pa.time32("s"))
b = rust.concatenate(a)
expected = pa.array([None, 1, 2] + [None, 1, 2], pa.time32("s"))
assert b == expected
del a
del b
del expected
def test_binary_array():
"""
Python -> Rust -> Python
"""
a = pa.array(["a", None, "bb", "ccc"], pa.binary())
b = rust.round_trip_array(a)
b.validate(full=True)
assert a.to_pylist() == b.to_pylist()
assert a.type == b.type
del a
del b
def test_fixed_len_binary_array():
"""
Python -> Rust -> Python
"""
a = pa.array(["aaa", None, "bbb", "ccc"], pa.binary(3))
b = rust.round_trip_array(a)
b.validate(full=True)
assert a.to_pylist() == b.to_pylist()
assert a.type == b.type
del a
del b
def test_list_array():
"""
Python -> Rust -> Python
"""
a = pa.array([[], None, [1, 2], [4, 5, 6]], pa.list_(pa.int64()))
b = rust.round_trip_array(a)
b.validate(full=True)
assert a.to_pylist() == b.to_pylist()
assert a.type == b.type
del a
del b
def test_fixed_len_list_array():
"""
Python -> Rust -> Python
"""
a = pa.array([[1, 2], None, [3, 4], [5, 6]], pa.list_(pa.int64(), 2))
b = rust.round_trip_array(a)
b.validate(full=True)
assert a.to_pylist() == b.to_pylist()
assert a.type == b.type
del a
del b
def test_timestamp_python():
"""
Python -> Rust -> Python
"""
data = [
None,
datetime.datetime(2021, 1, 1, 1, 1, 1, 1),
datetime.datetime(2020, 3, 9, 1, 1, 1, 1),
]
a = pa.array(data, pa.timestamp("us"))
b = rust.concatenate(a)
expected = pa.array(data + data, pa.timestamp("us"))
assert b == expected
del a
del b
del expected
def test_timestamp_tz_python():
"""
Python -> Rust -> Python
"""
tzinfo = pytz.timezone("America/New_York")
pyarrow_type = pa.timestamp("us", tz="America/New_York")
data = [
None,
datetime.datetime(2021, 1, 1, 1, 1, 1, 1, tzinfo=tzinfo),
datetime.datetime(2020, 3, 9, 1, 1, 1, 1, tzinfo=tzinfo),
]
a = pa.array(data, type=pyarrow_type)
b = rust.concatenate(a)
expected = pa.array(data * 2, type=pyarrow_type)
assert b == expected
del a
del b
del expected
def test_decimal_python():
"""
Python -> Rust -> Python
"""
data = [
round(decimal.Decimal(123.45), 2),
round(decimal.Decimal(-123.45), 2),
None
]
a = pa.array(data, pa.decimal128(6, 2))
b = rust.round_trip_array(a)
assert a == b
del a
del b
def test_dictionary_python():
"""
Python -> Rust -> Python
"""
a = pa.array(["a", None, "b", None, "a"], type=pa.dictionary(pa.int8(), pa.string()))
b = rust.round_trip_array(a)
assert a == b
del a
del b
def test_record_batch_reader():
"""
Python -> Rust -> Python
"""
schema = pa.schema([('ints', pa.list_(pa.int32()))], metadata={b'key1': b'value1'})
batches = [
pa.record_batch([[[1], [2, 42]]], schema),
pa.record_batch([[None, [], [5, 6]]], schema),
]
a = pa.RecordBatchReader.from_batches(schema, batches)
b = rust.round_trip_record_batch_reader(a)
assert b.schema == schema
got_batches = list(b)
assert got_batches == batches