forked from lance-format/lance
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_blob.py
More file actions
334 lines (276 loc) · 9.58 KB
/
test_blob.py
File metadata and controls
334 lines (276 loc) · 9.58 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
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright The Lance Authors
import lance
import pyarrow as pa
import pytest
from lance import BlobColumn
def test_blob_read_from_binary():
values = [b"foo", b"bar", b"baz"]
data = pa.table(
{
"bin": pa.array(values, type=pa.binary()),
"largebin": pa.array(values, type=pa.large_binary()),
}
)
for col_name in ["bin", "largebin"]:
blobs = BlobColumn(data.column(col_name))
for i, f in enumerate(blobs):
assert f.read() in values[i]
def test_blob_reject_invalid_col():
values = pa.array([1, 2, 3])
with pytest.raises(ValueError, match="Expected a binary array"):
BlobColumn(values)
def test_blob_descriptions(tmp_path):
values = pa.array([b"foo", b"bar", b"baz"], pa.large_binary())
table = pa.table(
[values],
schema=pa.schema(
[
pa.field(
"blobs", pa.large_binary(), metadata={"lance-encoding:blob": "true"}
)
]
),
)
ds = lance.write_dataset(table, tmp_path / "test_ds")
# These positions may be surprising but lance pads buffers to 64-byte boundaries
expected_positions = pa.array([0, 64, 128], pa.uint64())
expected_sizes = pa.array([3, 3, 3], pa.uint64())
descriptions = ds.to_table().column("blobs").chunk(0)
assert descriptions.field(0) == expected_positions
assert descriptions.field(1) == expected_sizes
def test_scan_blob_as_binary(tmp_path):
values = [b"foo", b"bar", b"baz"]
arr = pa.array(values, pa.large_binary())
table = pa.table(
[arr],
schema=pa.schema(
[
pa.field(
"blobs", pa.large_binary(), metadata={"lance-encoding:blob": "true"}
)
]
),
)
ds = lance.write_dataset(table, tmp_path / "test_ds")
tbl = ds.scanner(columns=["blobs"], blob_handling="all_binary").to_table()
assert tbl.column("blobs").to_pylist() == values
def test_fragment_scan_blob_as_binary(tmp_path):
values = [b"foo", b"bar", b"baz"]
arr = pa.array(values, pa.large_binary())
table = pa.table(
[arr],
schema=pa.schema(
[
pa.field(
"blobs", pa.large_binary(), metadata={"lance-encoding:blob": "true"}
)
]
),
)
ds = lance.write_dataset(table, tmp_path / "test_ds")
fragment = ds.get_fragments()[0]
tbl = fragment.scanner(columns=["blobs"], blob_handling="all_binary").to_table()
assert tbl.column("blobs").to_pylist() == values
tbl = fragment.to_table(columns=["blobs"], blob_handling="all_binary")
assert tbl.column("blobs").to_pylist() == values
@pytest.fixture
def dataset_with_blobs(tmp_path):
values = pa.array([b"foo", b"bar", b"baz"], pa.large_binary())
idx = pa.array([0, 1, 2], pa.uint64())
table = pa.table(
[values, idx],
schema=pa.schema(
[
pa.field(
"blobs", pa.large_binary(), metadata={"lance-encoding:blob": "true"}
),
pa.field("idx", pa.uint64()),
]
),
)
ds = lance.write_dataset(table, tmp_path / "test_ds")
values = pa.array([b"qux", b"quux", b"corge"], pa.large_binary())
idx = pa.array([3, 4, 5], pa.uint64())
table = pa.table(
[values, idx],
schema=pa.schema(
[
pa.field(
"blobs", pa.large_binary(), metadata={"lance-encoding:blob": "true"}
),
pa.field("idx", pa.uint64()),
]
),
)
ds.insert(table)
return ds
def test_blob_files(dataset_with_blobs):
row_ids = (
dataset_with_blobs.to_table(columns=[], with_row_id=True)
.column("_rowid")
.to_pylist()
)
blobs = dataset_with_blobs.take_blobs("blobs", ids=row_ids)
for expected in [b"foo", b"bar", b"baz"]:
with blobs.pop(0) as f:
assert f.read() == expected
def test_blob_files_by_address(dataset_with_blobs):
addresses = (
dataset_with_blobs.to_table(columns=[], with_row_address=True)
.column("_rowaddr")
.to_pylist()
)
blobs = dataset_with_blobs.take_blobs("blobs", addresses=addresses)
for expected in [b"foo", b"bar", b"baz"]:
with blobs.pop(0) as f:
assert f.read() == expected
def test_blob_files_by_address_with_stable_row_ids(tmp_path):
table = pa.table(
{
"blobs": pa.array([b"foo"], pa.large_binary()),
"idx": pa.array([0], pa.uint64()),
},
schema=pa.schema(
[
pa.field(
"blobs", pa.large_binary(), metadata={"lance-encoding:blob": "true"}
),
pa.field("idx", pa.uint64()),
]
),
)
ds = lance.write_dataset(
table,
tmp_path / "test_ds",
enable_stable_row_ids=True,
)
ds.insert(
pa.table(
{
"blobs": pa.array([b"bar"], pa.large_binary()),
"idx": pa.array([1], pa.uint64()),
},
schema=table.schema,
)
)
t = ds.to_table(columns=["idx"], with_row_address=True)
row_idx = t.column("idx").to_pylist().index(1)
addr = t.column("_rowaddr").to_pylist()[row_idx]
blobs = ds.take_blobs("blobs", addresses=[addr])
assert len(blobs) == 1
with blobs[0] as f:
assert f.read() == b"bar"
def test_blob_by_indices(tmp_path, dataset_with_blobs):
indices = [0, 4]
blobs = dataset_with_blobs.take_blobs("blobs", indices=indices)
blobs2 = dataset_with_blobs.take_blobs("blobs", ids=[0, (1 << 32) + 1])
assert len(blobs) == len(blobs2)
for b1, b2 in zip(blobs, blobs2):
with b1 as f1, b2 as f2:
assert f1.read() == f2.read()
def test_blob_file_seek(tmp_path, dataset_with_blobs):
row_ids = (
dataset_with_blobs.to_table(columns=[], with_row_id=True)
.column("_rowid")
.to_pylist()
)
blobs = dataset_with_blobs.take_blobs("blobs", ids=row_ids)
with blobs[1] as f:
assert f.seek(1) == 1
assert f.read(1) == b"a"
def test_null_blobs(tmp_path):
table = pa.table(
{
"id": range(100),
"blob": pa.array([None] * 100, pa.large_binary()),
},
schema=pa.schema(
[
pa.field("id", pa.uint64()),
pa.field(
"blob", pa.large_binary(), metadata={"lance-encoding:blob": "true"}
),
]
),
)
ds = lance.write_dataset(table, tmp_path / "test_ds")
blobs = ds.take_blobs("blob", ids=range(100))
for blob in blobs:
assert blob.size() == 0
ds.insert(pa.table({"id": pa.array(range(100, 200), pa.uint64())}))
ds.add_columns(
pa.field(
"more_blob",
pa.large_binary(),
metadata={"lance-encoding:blob": "true"},
)
)
for blob_col in ["blob", "more_blob"]:
blobs = ds.take_blobs(blob_col, indices=range(100, 200))
for blob in blobs:
assert blob.size() == 0
blobs = ds.to_table(columns=[blob_col])
for blob in blobs.column(blob_col):
py_blob = blob.as_py()
# When we write blobs to a file we store the position as 1 and size as 0
# to avoid needing a validity buffer.
assert py_blob is None or py_blob == {
"position": 1,
"size": 0,
}
def test_blob_file_read_middle(tmp_path, dataset_with_blobs):
# This regresses an issue where we were not setting the cursor
# correctly after a call to `read` when the blob was not the
# first thing in the file.
row_ids = (
dataset_with_blobs.to_table(columns=[], with_row_id=True)
.column("_rowid")
.to_pylist()
)
blobs = dataset_with_blobs.take_blobs("blobs", ids=row_ids)
with blobs[1] as f:
assert f.read(1) == b"b"
assert f.read(1) == b"a"
assert f.read(1) == b"r"
def test_take_deleted_blob(tmp_path, dataset_with_blobs):
row_ids = (
dataset_with_blobs.to_table(columns=[], with_row_id=True)
.column("_rowid")
.to_pylist()
)
dataset_with_blobs.delete("idx = 1")
with pytest.raises(
NotImplementedError,
match="A take operation that includes row addresses must not target deleted",
):
dataset_with_blobs.take_blobs("blobs", ids=row_ids)
def test_scan_blob(tmp_path, dataset_with_blobs):
ds = dataset_with_blobs.scanner(filter="idx = 2").to_table()
assert ds.num_rows == 1
def test_blob_extension_write_inline(tmp_path):
table = pa.table({"blob": lance.blob_array([b"foo", b"bar"])})
ds = lance.write_dataset(
table,
tmp_path / "test_ds_v2",
data_storage_version="2.2",
)
desc = ds.to_table(columns=["blob"]).column("blob").chunk(0)
assert pa.types.is_struct(desc.type)
blobs = ds.take_blobs("blob", indices=[0, 1])
with blobs[0] as f:
assert f.read() == b"foo"
def test_blob_extension_write_external(tmp_path):
blob_path = tmp_path / "external_blob.bin"
blob_path.write_bytes(b"hello")
uri = blob_path.as_uri()
table = pa.table({"blob": lance.blob_array([uri])})
ds = lance.write_dataset(
table,
tmp_path / "test_ds_v2_external",
data_storage_version="2.2",
)
blob = ds.take_blobs("blob", indices=[0])[0]
assert blob.size() == 5
with blob as f:
assert f.read() == b"hello"