forked from lance-format/lance
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_file.py
More file actions
219 lines (178 loc) · 6.31 KB
/
test_file.py
File metadata and controls
219 lines (178 loc) · 6.31 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
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright The Lance Authors
from pathlib import Path
import pyarrow as pa
import pytest
from lance.file import LanceFileReader, LanceFileWriter
NUM_ROWS = 10_000_000
ROWS_TO_SAMPLE = 10
@pytest.mark.parametrize(
"version",
["2.0", "2.1"],
ids=["2_0", "2_1"],
)
@pytest.mark.benchmark(group="scan_single_column")
def test_scan_integer(tmp_path: Path, benchmark, version):
schema = pa.schema([pa.field("values", pa.uint64(), True)])
def gen_data():
remaining = NUM_ROWS
offset = 0
while remaining > 0:
to_take = min(remaining, 10000)
values = pa.array(range(offset, offset + to_take))
batch = pa.table({"values": values}).to_batches()[0]
yield batch
remaining -= to_take
offset += to_take
with LanceFileWriter(
str(tmp_path / "file.lance"), schema, version=version
) as writer:
for batch in gen_data():
writer.write_batch(batch)
def read_all():
reader = LanceFileReader(str(tmp_path / "file.lance"))
return reader.read_all(batch_size=16 * 1024).to_table()
result = benchmark.pedantic(read_all, rounds=1, iterations=1)
assert result.num_rows == NUM_ROWS
@pytest.mark.parametrize(
"version",
["2.0", "2.1"],
ids=["2_0", "2_1"],
)
@pytest.mark.benchmark(group="scan_single_column")
def test_scan_nullable_integer(tmp_path: Path, benchmark, version):
schema = pa.schema([pa.field("values", pa.uint64(), True)])
def gen_data():
remaining = NUM_ROWS
offset = 0
while remaining > 0:
to_take = min(remaining, 10000)
values = pa.array(
[None if i % 2 == 0 else i for i in range(offset, offset + to_take)]
)
batch = pa.table({"values": values}).to_batches()[0]
yield batch
remaining -= to_take
offset += to_take
with LanceFileWriter(
str(tmp_path / "file.lance"), schema, version=version
) as writer:
for batch in gen_data():
writer.write_batch(batch)
def read_all():
reader = LanceFileReader(str(tmp_path / "file.lance"))
return reader.read_all(batch_size=16 * 1024).to_table()
result = benchmark.pedantic(read_all, rounds=1, iterations=1)
assert result.num_rows == NUM_ROWS
@pytest.mark.benchmark(group="scan_single_column")
def test_scan_nested_integer(tmp_path: Path, benchmark):
def get_val(i: int):
if i % 4 == 0:
return None
elif i % 4 == 1:
return {"outer": None}
elif i % 4 == 2:
return {"outer": {"inner": None}}
else:
return {"outer": {"inner": i}}
dtype = pa.struct(
[pa.field("outer", pa.struct([pa.field("inner", pa.uint64(), True)]), True)]
)
schema = pa.schema(
[
pa.field(
"values",
dtype,
True,
)
]
)
def gen_data():
remaining = NUM_ROWS
offset = 0
while remaining > 0:
to_take = min(remaining, 10000)
values = pa.array([get_val(i) for i in range(offset, offset + to_take)])
batch = pa.table({"values": values}).to_batches()[0]
yield batch
remaining -= to_take
offset += to_take
with LanceFileWriter(str(tmp_path / "file.lance"), schema, version="2.1") as writer:
for batch in gen_data():
writer.write_batch(batch)
def read_all():
reader = LanceFileReader(str(tmp_path / "file.lance"))
return reader.read_all(batch_size=16 * 1024).to_table()
result = benchmark.pedantic(read_all, rounds=1, iterations=1)
assert result.num_rows == NUM_ROWS
@pytest.mark.parametrize(
"version",
["2.0", "2.1"],
ids=["2_0", "2_1"],
)
@pytest.mark.benchmark(group="sample_single_column")
def test_sample_integer(tmp_path: Path, benchmark, version):
schema = pa.schema([pa.field("values", pa.uint64(), True)])
def gen_data():
remaining = NUM_ROWS
offset = 0
while remaining > 0:
to_take = min(remaining, 10000)
values = pa.array(range(offset, offset + to_take))
batch = pa.table({"values": values}).to_batches()[0]
yield batch
remaining -= to_take
offset += to_take
with LanceFileWriter(
str(tmp_path / "file.lance"), schema, version=version
) as writer:
for batch in gen_data():
writer.write_batch(batch)
reader = LanceFileReader(str(tmp_path / "file.lance"))
indices = list(range(0, NUM_ROWS, NUM_ROWS // ROWS_TO_SAMPLE))
def sample():
return reader.take_rows(indices).to_table()
result = benchmark.pedantic(sample, rounds=30, iterations=1)
assert result.num_rows == NUM_ROWS
@pytest.mark.benchmark(group="sample_single_column")
def test_sample_nested_integer(tmp_path: Path, benchmark):
def get_val(i: int):
if i % 4 == 0:
return None
elif i % 4 == 1:
return {"outer": None}
elif i % 4 == 2:
return {"outer": {"inner": None}}
else:
return {"outer": {"inner": i}}
dtype = pa.struct(
[pa.field("outer", pa.struct([pa.field("inner", pa.uint64(), True)]), True)]
)
schema = pa.schema(
[
pa.field(
"values",
dtype,
True,
)
]
)
def gen_data():
remaining = NUM_ROWS
offset = 0
while remaining > 0:
to_take = min(remaining, 10000)
values = pa.array([get_val(i) for i in range(offset, offset + to_take)])
batch = pa.table({"values": values}).to_batches()[0]
yield batch
remaining -= to_take
offset += to_take
with LanceFileWriter(str(tmp_path / "file.lance"), schema, version="2.1") as writer:
for batch in gen_data():
writer.write_batch(batch)
reader = LanceFileReader(str(tmp_path / "file.lance"))
indices = list(range(0, NUM_ROWS, NUM_ROWS // ROWS_TO_SAMPLE))
def sample():
return reader.take_rows(indices).to_table()
result = benchmark.pedantic(sample, rounds=30, iterations=1)
assert result.num_rows == NUM_ROWS