File: test_2968_to_parquet_row_groups.py

package info (click to toggle)
python-awkward 2.8.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,932 kB
  • sloc: python: 178,875; cpp: 33,828; sh: 432; makefile: 21; javascript: 8
file content (231 lines) | stat: -rw-r--r-- 5,582 bytes parent folder | download
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
from __future__ import annotations

import os

import pytest

import awkward as ak

pytest.importorskip("pyarrow.parquet")


def generator(n):
    arr = [
        ak.Array(
            [
                {"x": 1.1, "y": [1], "z": [9, 0, 10]},
                {"x": 2.2, "y": [1, 2], "z": [9, 11, 14]},
                {"x": 3.3, "y": [1, 2, 3], "z": [12, 14]},
            ]
        ),
        ak.Array(
            [
                {"x": 1.1, "y": [1], "z": [8]},
                {"x": 2.2, "y": [1, 2], "z": [6, 2, 10]},
                {"x": 3.3, "y": [1, 2, 3], "z": [9, 14]},
            ]
        ),
        ak.Array(
            [
                {"x": 1.1, "y": [1], "z": [9, 11, 14]},
                {"x": 4.4, "y": [1, 2, 3, 4], "z": [1, 14]},
                {"x": 5.5, "y": [1, 2, 3, 4, 5], "z": [9]},
            ]
        ),
        ak.Array(
            [
                {"x": 3.3, "y": [1, 4], "z": [9, 11, 14]},
                {"x": 4.4, "y": [1], "z": [1]},
                {"x": 5.5, "y": [1, 4, 5], "z": [9, 10]},
            ]
        ),
        ak.Array(
            [
                {"x": 66.0, "y": [1, 2, 4], "z": [9, 11, 14]},
                {"x": 4.4, "y": [1, 2], "z": [1, 14]},
                {"x": 5.5, "y": [1, 2, 3, 4, 5], "z": [9, 2]},
            ]
        ),
    ]

    for _ in range(n):
        yield arr[_]


def test_simple(tmp_path):
    filename = os.path.join(tmp_path, "whatever.parquet")

    arr = [
        ak.Array(
            [
                {"x": 1.1, "y": [1]},
                {"x": 2.2, "y": [1, 2]},
                {"x": 3.3, "y": [1, 2, 3]},
                {"x": 1.8, "y": [3, 5, 6]},
                {"x": 4.4, "y": [1, 2, 3, 4]},
                {"x": 5.5, "y": [5, 3, 2, 6]},
            ]
        ),
        ak.Array(
            [
                {"x": 1.1, "y": [1]},
                {"x": 2.2, "y": [1, 2]},
                {"x": 3.3, "y": [1, 2, 3]},
                {"x": 4.4, "y": [1, 2, 3, 4]},
                {"x": 1.8, "y": [3, 5, 6]},
                {"x": 5.5, "y": [1, 2, 3, 4, 5]},
            ]
        ),
        ak.Array(
            [
                {"x": 1.1, "y": [1]},
                {"x": 4.4, "y": [1, 2, 3, 4]},
                {"x": 5.5, "y": [1, 2, 3, 4, 5]},
                {"x": 2.2, "y": [1, 2]},
                {"x": 3.3, "y": [1, 2, 3]},
                {"x": 1.8, "y": [3, 5, 6]},
            ]
        ),
    ]

    check = ak.Array(
        [
            {"x": 1.1, "y": [1]},
            {"x": 2.2, "y": [1, 2]},
            {"x": 3.3, "y": [1, 2, 3]},
            {"x": 1.8, "y": [3, 5, 6]},
            {"x": 4.4, "y": [1, 2, 3, 4]},
            {"x": 5.5, "y": [5, 3, 2, 6]},
            {"x": 1.1, "y": [1]},
            {"x": 2.2, "y": [1, 2]},
            {"x": 3.3, "y": [1, 2, 3]},
            {"x": 4.4, "y": [1, 2, 3, 4]},
            {"x": 1.8, "y": [3, 5, 6]},
            {"x": 5.5, "y": [1, 2, 3, 4, 5]},
            {"x": 1.1, "y": [1]},
            {"x": 4.4, "y": [1, 2, 3, 4]},
            {"x": 5.5, "y": [1, 2, 3, 4, 5]},
            {"x": 2.2, "y": [1, 2]},
            {"x": 3.3, "y": [1, 2, 3]},
            {"x": 1.8, "y": [3, 5, 6]},
        ]
    )

    iterator = (i for i in arr)

    metadata = ak.to_parquet_row_groups(iterator, filename)
    assert len(arr) == metadata.num_row_groups

    data = ak.from_parquet(filename)

    assert data.tolist() == check.tolist()

    with pytest.raises(TypeError):
        ak.to_parquet_row_groups(check, filename)

    with pytest.raises(ValueError):
        ak.to_parquet_row_groups([], filename)


def test_general(tmp_path):
    filename = os.path.join(tmp_path, "whatever.parquet")
    iterator = generator(3)

    metadata = ak.to_parquet_row_groups(iterator, filename)
    assert metadata.num_row_groups == 3

    data = ak.from_parquet(filename)
    assert len(data) == 9

    assert data["x"].tolist() == [1.1, 2.2, 3.3, 1.1, 2.2, 3.3, 1.1, 4.4, 5.5]
    assert data["y"].tolist() == [
        [1],
        [1, 2],
        [1, 2, 3],
        [1],
        [1, 2],
        [1, 2, 3],
        [1],
        [1, 2, 3, 4],
        [1, 2, 3, 4, 5],
    ]
    assert data["z"].tolist() == [
        [9, 0, 10],
        [9, 11, 14],
        [12, 14],
        [8],
        [6, 2, 10],
        [9, 14],
        [9, 11, 14],
        [1, 14],
        [9],
    ]


def test_params(tmp_path):
    filename = os.path.join(tmp_path, "whatever.parquet")
    iterator = generator(5)

    metadata = ak.to_parquet_row_groups(
        iterator,
        filename,
        extensionarray=False,
        count_nulls=False,
        row_group_size=64 * 64 * 64,
    )
    assert metadata.num_row_groups == 5

    data = ak.from_parquet(filename)
    assert len(data) == 15

    assert data["x"].tolist() == [
        1.1,
        2.2,
        3.3,
        1.1,
        2.2,
        3.3,
        1.1,
        4.4,
        5.5,
        3.3,
        4.4,
        5.5,
        66.0,
        4.4,
        5.5,
    ]
    assert data["y"].tolist() == [
        [1],
        [1, 2],
        [1, 2, 3],
        [1],
        [1, 2],
        [1, 2, 3],
        [1],
        [1, 2, 3, 4],
        [1, 2, 3, 4, 5],
        [1, 4],
        [1],
        [1, 4, 5],
        [1, 2, 4],
        [1, 2],
        [1, 2, 3, 4, 5],
    ]
    assert data["z"].tolist() == [
        [9, 0, 10],
        [9, 11, 14],
        [12, 14],
        [8],
        [6, 2, 10],
        [9, 14],
        [9, 11, 14],
        [1, 14],
        [9],
        [9, 11, 14],
        [1],
        [9, 10],
        [9, 11, 14],
        [1, 14],
        [9, 2],
    ]