File: test_2898_to_parquet_dataset.py

package info (click to toggle)
python-awkward 2.6.5-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 23,088 kB
  • sloc: python: 148,689; cpp: 33,562; sh: 432; makefile: 21; javascript: 8
file content (140 lines) | stat: -rw-r--r-- 4,399 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
from __future__ import annotations

import os

import pytest

import awkward as ak

pyarrow = pytest.importorskip("pyarrow")


def simple_test(tmp_path):
    array = ak.Array([[1.1, 2.2, 3.3], [], [4.4, 5.5]])
    array1 = ak.Array([[1.1, 2.2, 3.3, 4.4], [4.0], [4.4, 5.5]])
    array2 = ak.Array([[1.0, 3.0, 3.3, 4.4], [4.0], [4.4, 10.0], [11.11]])
    ak.to_parquet(
        array, os.path.join(tmp_path, "arr1.parquet"), parquet_compliant_nested=True
    )
    ak.to_parquet(
        array1, os.path.join(tmp_path, "arr2.parquet"), parquet_compliant_nested=True
    )
    ak.to_parquet(
        array2, os.path.join(tmp_path, "arr3.parquet"), parquet_compliant_nested=True
    )

    ak.to_parquet_dataset(tmp_path, filenames="arr[1-3].parquet")
    assert os.path.exists(os.path.join(tmp_path, "_common_metadata"))
    assert os.path.exists(os.path.join(tmp_path, "_metadata"))

    with_metadata = ak.from_parquet(tmp_path)
    print(with_metadata.to_list())
    assert with_metadata.tolist() == [
        [1.1, 2.2, 3.3],
        [],
        [4.4, 5.5],
        [1.1, 2.2, 3.3, 4.4],
        [4.0],
        [4.4, 5.5],
        [1.0, 3.0, 3.3, 4.4],
        [4.0],
        [4.4, 10.0],
        [11.11],
    ]


def complex_test(tmp_path):
    array1 = ak.Array(
        [{"x": 1.1, "y": [1]}, {"x": 2.2, "y": [1, 2]}, {"x": 3.3, "y": [1, 2, 3]}]
    )
    array2 = ak.Array([{"x": 1.8, "y": [3, 5, 6]}])
    array3 = ak.Array([{"x": 4.4, "y": [1, 2, 3, 4]}, {"x": 5.5, "y": [1, 2, 3, 4, 5]}])
    ak.to_parquet(
        array1, os.path.join(tmp_path, "arr1.parquet"), parquet_compliant_nested=True
    )
    ak.to_parquet(
        array2, os.path.join(tmp_path, "arr2.parquet"), parquet_compliant_nested=True
    )
    ak.to_parquet(
        array3, os.path.join(tmp_path, "arr3.parquet"), parquet_compliant_nested=True
    )

    ak.to_parquet_dataset(tmp_path)

    assert os.path.exists(os.path.join(tmp_path, "_common_metadata"))
    assert os.path.exists(os.path.join(tmp_path, "_metadata"))

    with_metadata = ak.from_parquet(tmp_path)
    assert with_metadata.tolist() == [
        {"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": [1, 2, 3, 4, 5]},
    ]


def test_filenames(tmp_path):
    array = ak.Array(
        [{"x": 1.1, "y": [1]}, {"x": 2.2, "y": [1, 2]}, {"x": 3.3, "y": [1, 2, 3]}]
    )
    array1 = ak.Array([{"x": 1.8, "y": [3, 5, 6]}])
    array2 = ak.Array([{"x": 4.4, "y": [1, 2, 3, 4]}, {"x": 5.5, "y": [1, 2, 3, 4, 5]}])
    ak.to_parquet(
        array, os.path.join(tmp_path, "arr1.parquet"), parquet_compliant_nested=True
    )
    ak.to_parquet(
        array1, os.path.join(tmp_path, "arr2.parquet"), parquet_compliant_nested=True
    )
    ak.to_parquet(
        array2, os.path.join(tmp_path, "arr3.parquet"), parquet_compliant_nested=True
    )

    ak.to_parquet_dataset(
        tmp_path, filenames=["arr1.parquet", "arr2.parquet", "arr3.parquet"]
    )

    assert os.path.exists(os.path.join(tmp_path, "_common_metadata"))
    assert os.path.exists(os.path.join(tmp_path, "_metadata"))

    with_metadata = ak.from_parquet(tmp_path)
    assert with_metadata.tolist() == [
        {"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": [1, 2, 3, 4, 5]},
    ]


def test_wildcard(tmp_path):
    array = ak.Array([[1.1, 2.2, 3.3], [], [4.4, 5.5]])
    array1 = ak.Array([[1.1, 2.2, 3.3, 4.4], [4.0], [4.4, 5.5]])
    array2 = ak.Array([[1.0, 3.0, 3.3, 4.4], [4.0], [4.4, 10.0], [11.11]])
    ak.to_parquet(
        array, os.path.join(tmp_path, "arr1.parquet"), parquet_compliant_nested=True
    )
    ak.to_parquet(
        array1, os.path.join(tmp_path, "arr2.parquet"), parquet_compliant_nested=True
    )
    ak.to_parquet(
        array2, os.path.join(tmp_path, "arr3.parquet"), parquet_compliant_nested=True
    )

    ak.to_parquet_dataset(tmp_path, filenames="arr?.parquet")

    with_metadata = ak.from_parquet(tmp_path)
    assert with_metadata.tolist() == [
        [1.1, 2.2, 3.3],
        [],
        [4.4, 5.5],
        [1.1, 2.2, 3.3, 4.4],
        [4.0],
        [4.4, 5.5],
        [1.0, 3.0, 3.3, 4.4],
        [4.0],
        [4.4, 10.0],
        [11.11],
    ]