forked from lance-format/lance
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_dataset.py
More file actions
5259 lines (4214 loc) · 175 KB
/
test_dataset.py
File metadata and controls
5259 lines (4214 loc) · 175 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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright The Lance Authors
import base64
import contextlib
import os
import pickle
import platform
import random
import re
import time
import uuid
from datetime import date, datetime, timedelta
from pathlib import Path
from typing import List
from unittest import mock
import lance
import lance.fragment
import numpy as np
import pandas as pd
import pandas.testing as tm
import polars as pl
import pyarrow as pa
import pyarrow.compute as pc
import pyarrow.dataset as pa_ds
import pyarrow.parquet as pq
import pytest
from helper import ProgressForTest
from lance._dataset.sharded_batch_iterator import ShardedBatchIterator
from lance.commit import CommitConflictError
from lance.dataset import LANCE_COMMIT_MESSAGE_KEY, AutoCleanupConfig
from lance.debug import format_fragment
from lance.file import stable_version
from lance.schema import LanceSchema
from lance.util import validate_vector_index
# Various valid inputs for write_dataset
input_schema = pa.schema([pa.field("a", pa.float64()), pa.field("b", pa.int64())])
input_data = [
# (schema, data)
(None, pa.table({"a": [1.0, 2.0], "b": [20, 30]})),
(None, pa.record_batch([[1.0, 2.0], [20, 30]], names=["a", "b"])),
(None, pd.DataFrame({"a": [1.0, 2.0], "b": [20, 30]})),
(None, pl.DataFrame({"a": [1.0, 2.0], "b": [20, 30]})),
(
input_schema,
[pa.record_batch([pa.array([1.0, 2.0]), pa.array([20, 30])], names=["a", "b"])],
),
# Can provide an iterator with a schema that is different but cast-able
(
input_schema,
iter(
pa.table(
{
"a": [1.0, 2.0],
"b": pa.array([20, 30], pa.int32()),
}
).to_batches()
),
),
]
@pytest.mark.parametrize("schema,data", input_data, ids=type)
def test_input_data(tmp_path: Path, schema, data):
base_dir = tmp_path / "test"
dataset = lance.write_dataset(data, base_dir, schema=schema)
assert dataset.to_table() == input_data[0][1]
def test_roundtrip_types(tmp_path: Path):
table = pa.table(
{
"dict": pa.array(["a", "b", "a"], pa.dictionary(pa.int8(), pa.string())),
# PyArrow doesn't support creating large_string dictionaries easily.
"large_dict": pa.DictionaryArray.from_arrays(
pa.array([0, 1, 1], pa.int8()),
pa.array(["foo", "bar"], pa.large_string()),
),
"list": pa.array(
[["a", "b"], ["c", "d"], ["e", "f"]], pa.list_(pa.string())
),
"large_list": pa.array(
[["a", "b"], ["c", "d"], ["e", "f"]], pa.large_list(pa.string())
),
}
)
# TODO: V2 does not currently handle large_dict
dataset = lance.write_dataset(table, tmp_path, data_storage_version="legacy")
assert dataset.schema == table.schema
assert dataset.to_table() == table
def test_dataset_overwrite(tmp_path: Path):
table1 = pa.Table.from_pylist([{"a": 1, "b": 2}, {"a": 10, "b": 20}])
base_dir = tmp_path / "test"
lance.write_dataset(table1, base_dir)
table2 = pa.Table.from_pylist([{"s": "one"}, {"s": "two"}])
lance.write_dataset(table2, base_dir, mode="overwrite")
dataset = lance.dataset(base_dir)
assert dataset.to_table() == table2
ds_v1 = lance.dataset(base_dir, version=1)
assert ds_v1.to_table() == table1
def test_truncate_table(tmp_path: Path):
base_dir = tmp_path / "truncate"
table = pa.table(
{
"i": pa.array([1, 2, 3], pa.int32()),
"dict": pa.DictionaryArray.from_arrays(
pa.array([0, 1, 2], pa.uint16()), pa.array(["a", "b", "c"])
),
}
)
ds = lance.write_dataset(table, base_dir, data_storage_version="stable")
assert ds.count_rows() == 3
ds.truncate_table()
assert ds.count_rows() == 0
assert ds.schema == table.schema
def test_dataset_append(tmp_path: Path):
table = pa.Table.from_pydict({"colA": [1, 2, 3], "colB": [4, 5, 6]})
base_dir = tmp_path / "test"
# verify append works even if no dataset existed at the uri
lance.write_dataset(table, base_dir, mode="append")
dataset = lance.dataset(base_dir)
assert dataset.to_table() == table
# verify appending batches with a different schema doesn't work
table2 = pa.Table.from_pydict({"COLUMN-C": [1, 2, 3], "colB": [4, 5, 6]})
with pytest.raises(OSError):
lance.write_dataset(table2, dataset, mode="append")
# But we can append subschemas
table3 = pa.Table.from_pydict({"colA": [4, 5, 6]})
dataset.insert(table3) # Append is default
assert dataset.to_table() == pa.table(
{"colA": [1, 2, 3, 4, 5, 6], "colB": [4, 5, 6, None, None, None]}
)
def test_dataset_from_record_batch_iterable(tmp_path: Path):
base_dir = tmp_path / "test"
test_pylist = [{"colA": "Alice", "colB": 20}, {"colA": "Blob", "colB": 30}]
# split into two batches
batches = [
pa.RecordBatch.from_pylist([test_pylist[0]]),
pa.RecordBatch.from_pylist([test_pylist[1]]),
]
# define schema
schema = pa.schema(
[
pa.field("colA", pa.string()),
pa.field("colB", pa.int64()),
]
)
# write dataset with iterator
lance.write_dataset(iter(batches), base_dir, schema)
dataset = lance.dataset(base_dir)
# After combined into one batch, make sure it is the same as original pylist
assert list(dataset.to_batches())[0].to_pylist() == test_pylist
# write dataset with list
lance.write_dataset(batches, base_dir, schema, mode="overwrite")
# After combined into one batch, make sure it is the same as original pylist
assert list(dataset.to_batches())[0].to_pylist() == test_pylist
def test_to_batches_with_partial_last_batch(tmp_path: Path):
base_dir = tmp_path / "test_batches"
row_count_per_file = 32
batch_size = 5
# Generate 3 batches of 32 rows each (96 total)
pylist = [{"colA": f"Row{i}", "colB": i} for i in range(row_count_per_file * 3)]
batches = [
pa.RecordBatch.from_pylist(
pylist[i * row_count_per_file : (i + 1) * row_count_per_file]
)
for i in range(3)
]
# Write dataset
schema = pa.schema([pa.field("colA", pa.string()), pa.field("colB", pa.int64())])
lance.write_dataset(batches, base_dir, schema, max_rows_per_file=row_count_per_file)
dataset = lance.dataset(base_dir)
# Check batch sizes
# strict_batch_size = True, batch_size = 5(batch_size < row_count_per_file),
all_batches = list(
dataset.to_batches(batch_size=batch_size, strict_batch_size=True)
)
print(all_batches)
assert sum(b.num_rows for b in all_batches) == row_count_per_file * 3 # Total rows
assert all(b.num_rows == batch_size for b in all_batches[:-1]) # Full batches
assert all_batches[-1].num_rows == 1 # Final partial batch
# Verify data integrity
combined = [row for batch in all_batches for row in batch.to_pylist()]
assert combined == pylist
# strict_batch_size = True, batch_size = 5*10 (batch_size > row_count_per_file),
large_batch_size = 10 * batch_size
all_batches = list(
dataset.to_batches(batch_size=large_batch_size, strict_batch_size=True)
)
assert sum(b.num_rows for b in all_batches) == row_count_per_file * 3 # Total rows
assert all(b.num_rows == large_batch_size for b in all_batches[:-1]) # Full batches
assert all_batches[-1].num_rows == 46 # Final partial batch
# strict_batch_size = False
# fragment 32 rows --> [5,5,5,5,5,5,2]
all_batches = list(
dataset.to_batches(batch_size=batch_size, strict_batch_size=False)
)
assert sum(b.num_rows for b in all_batches) == row_count_per_file * 3 # Total rows
partial_batches = [b for b in all_batches if b.num_rows < batch_size]
partial_batch_count = 3
assert len(partial_batches) == partial_batch_count
assert all(b.num_rows == 2 for b in partial_batches[:-1])
# strict_batch_size = False, batch_size = 5*10 (batch_size > row_count_per_file),
all_batches = list(
dataset.to_batches(batch_size=large_batch_size, strict_batch_size=False)
)
assert sum(b.num_rows for b in all_batches) == row_count_per_file * 3 # Total rows
assert all(b.num_rows == 32 for b in all_batches) # Full batches
# 32 rows, 1 row per file
ds = lance.write_dataset(
pa.table({"a": range(32)}), base_dir, max_rows_per_file=1, mode="overwrite"
)
# 1 row per batch if strict_batch_size is False (regardless of batch_size)
for batch in ds.to_batches():
assert batch.num_rows == 1
for batch in ds.to_batches(batch_size=8):
assert batch.num_rows == 1
# We should get 8 rows per batch if strict_batch_size is True
for batch in ds.to_batches(batch_size=8, strict_batch_size=True):
assert batch.num_rows == 8
def test_schema_metadata(tmp_path: Path):
schema = pa.schema(
[
pa.field("a", pa.int64(), metadata={b"thisis": "a"}),
pa.field("b", pa.int64(), metadata={b"thisis": "b"}),
],
metadata={b"foo": b"bar", b"baz": b"qux"},
)
table = pa.Table.from_pydict({"a": range(100), "b": range(100)}, schema=schema)
ds = lance.write_dataset(table, tmp_path)
# Original schema
assert ds.schema.metadata == {b"foo": b"bar", b"baz": b"qux"}
assert ds.schema.field("a").metadata == {b"thisis": b"a"}
assert ds.schema.field("b").metadata == {b"thisis": b"b"}
# Replace schema metadata (using new unified API)
ds.update_schema_metadata({"foo": "baz"}, replace=True)
assert ds.schema.metadata == {b"foo": b"baz"}
assert ds.schema.field("a").metadata == {b"thisis": b"a"}
assert ds.schema.field("b").metadata == {b"thisis": b"b"}
# Replace field metadata (using new unified API)
# Use field path instead of field ID
ds.update_field_metadata({"a": {"thisis": "c"}}, replace=True)
assert ds.schema.field("a").metadata == {b"thisis": b"c"}
assert ds.schema.field("b").metadata == {b"thisis": b"b"}
# Overwrite overwrites metadata
ds = lance.write_dataset(table, tmp_path, mode="overwrite")
assert ds.schema.metadata == {b"foo": b"bar", b"baz": b"qux"}
assert ds.schema.field("a").metadata == {b"thisis": b"a"}
assert ds.schema.field("b").metadata == {b"thisis": b"b"}
def test_versions(tmp_path: Path):
table1 = pa.Table.from_pylist([{"a": 1, "b": 2}, {"a": 10, "b": 20}])
base_dir = tmp_path / "test"
lance.write_dataset(table1, base_dir)
assert len(lance.dataset(base_dir).versions()) == 1
table2 = pa.Table.from_pylist([{"s": "one"}, {"s": "two"}])
time.sleep(1)
lance.write_dataset(table2, base_dir, mode="overwrite")
assert len(lance.dataset(base_dir).versions()) == 2
v1, v2 = lance.dataset(base_dir).versions()
assert v1["version"] == 1
assert v2["version"] == 2
assert isinstance(v1["timestamp"], datetime)
assert isinstance(v2["timestamp"], datetime)
assert v1["timestamp"] < v2["timestamp"]
assert isinstance(v1["metadata"], dict)
assert isinstance(v2["metadata"], dict)
def test_version_id(tmp_path: Path):
table1 = pa.Table.from_pylist([{"a": 1, "b": 2}, {"a": 10, "b": 20}])
base_dir = tmp_path / "test"
original_ds = lance.write_dataset(table1, base_dir)
assert original_ds.version == 1
assert original_ds.latest_version == 1
table2 = pa.Table.from_pylist([{"s": "one"}, {"s": "two"}])
time.sleep(1)
updated_ds = lance.write_dataset(table2, base_dir, mode="overwrite")
assert original_ds.version == 1
assert original_ds.latest_version == 2
assert updated_ds.version == 2
assert updated_ds.latest_version == 2
def test_checkout(tmp_path: Path):
tab = pa.table({"a": range(3)})
ds1 = lance.write_dataset(tab, tmp_path)
ds1.delete("a = 1")
ds2 = ds1.checkout_version(1)
assert ds2.version == 1
assert ds2.to_table() == tab
assert ds1.version == 2
assert ds1.to_table() == pa.table({"a": [0, 2]})
ds1.delete("a = 2")
assert ds1.count_rows() == 1
assert ds2.checkout_version(ds2.latest_version).version == ds1.version
def test_asof_checkout(tmp_path: Path):
table = pa.Table.from_pydict({"colA": [1, 2, 3], "colB": [4, 5, 6]})
base_dir = tmp_path / "test"
lance.write_dataset(table, base_dir)
assert len(lance.dataset(base_dir).versions()) == 1
time.sleep(0.1)
ts_1 = datetime.now()
time.sleep(0.1)
lance.write_dataset(table, base_dir, mode="append")
assert len(lance.dataset(base_dir).versions()) == 2
time.sleep(0.1)
ts_2 = datetime.now()
time.sleep(0.1)
lance.write_dataset(table, base_dir, mode="append")
assert len(lance.dataset(base_dir).versions()) == 3
time.sleep(0.1)
ts_3 = datetime.now()
# check that only the first batch is present
ds = lance.dataset(base_dir, asof=ts_1)
assert ds.version == 1
assert len(ds.to_table()) == 3
# check that the first and second batch are present
ds = lance.dataset(base_dir, asof=ts_2)
assert ds.version == 2
assert len(ds.to_table()) == 6
# check that all batches are present
ds = lance.dataset(base_dir, asof=ts_3)
assert ds.version == 3
assert len(ds.to_table()) == 9
def test_enable_stable_row_ids(tmp_path: Path):
table = pa.Table.from_pylist(
[{"name": "Alice", "age": 20}, {"name": "Bob", "age": 30}]
)
lance.write_dataset(table, tmp_path, enable_stable_row_ids=True)
ds = lance.write_dataset(table, tmp_path, enable_stable_row_ids=True, mode="append")
table_before = ds.scanner(with_row_id=True, with_row_address=True).to_table()
assert len(table_before) == 4
assert table_before["_rowid"][0].as_py() == 0
assert table_before["_rowid"][1].as_py() == 1
assert table_before["_rowid"][2].as_py() == 2
assert table_before["_rowid"][3].as_py() == 3
assert table_before["_rowaddr"][0].as_py() == 0
assert table_before["_rowaddr"][1].as_py() == 1
assert table_before["_rowaddr"][2].as_py() == (1 << 32) + 0
assert table_before["_rowaddr"][3].as_py() == (1 << 32) + 1
ds.optimize.compact_files()
table_after = ds.scanner(with_row_id=True, with_row_address=True).to_table()
assert len(table_after) == 4
assert table_after["_rowid"][0].as_py() == 0
assert table_after["_rowid"][1].as_py() == 1
assert table_after["_rowid"][2].as_py() == 2
assert table_after["_rowid"][3].as_py() == 3
assert table_after["_rowaddr"][0].as_py() == (2 << 32) + 0
assert table_after["_rowaddr"][1].as_py() == (2 << 32) + 1
assert table_after["_rowaddr"][2].as_py() == (2 << 32) + 2
assert table_after["_rowaddr"][3].as_py() == (2 << 32) + 3
def test_v2_manifest_paths(tmp_path: Path):
lance.write_dataset(
pa.table({"a": range(100)}), tmp_path, enable_v2_manifest_paths=True
)
manifest_path = os.listdir(tmp_path / "_versions")
assert len(manifest_path) == 1
assert re.match(r"\d{20}\.manifest", manifest_path[0])
def test_default_v2_manifest_paths(tmp_path: Path):
lance.write_dataset(pa.table({"a": range(100)}), tmp_path)
manifest_path = os.listdir(tmp_path / "_versions")
assert len(manifest_path) == 1
assert re.match(r"\d{20}\.manifest", manifest_path[0])
def test_v2_manifest_paths_migration(tmp_path: Path):
# Create a dataset with v1 manifest paths
lance.write_dataset(
pa.table({"a": range(100)}), tmp_path, enable_v2_manifest_paths=False
)
manifest_path = os.listdir(tmp_path / "_versions")
assert manifest_path == ["1.manifest"]
# Migrate to v2 manifest paths
lance.dataset(tmp_path).migrate_manifest_paths_v2()
manifest_path = os.listdir(tmp_path / "_versions")
assert len(manifest_path) == 1
assert re.match(r"\d{20}\.manifest", manifest_path[0])
def test_tag(tmp_path: Path):
table = pa.Table.from_pydict({"colA": [1, 2, 3], "colB": [4, 5, 6]})
base_dir = tmp_path / "test"
lance.write_dataset(table, base_dir)
ds = lance.write_dataset(table, base_dir, mode="append")
assert len(ds.tags.list()) == 0
with pytest.raises(ValueError):
ds.tags.create("tag1", 3)
with pytest.raises(ValueError):
ds.tags.delete("tag1")
ds.tags.create("tag1", 1)
assert len(ds.tags.list()) == 1
with pytest.raises(ValueError):
ds.tags.create("tag1", 1)
ds.tags.delete("tag1")
ds.tags.create("tag1", 1)
ds.tags.create("tag2", 1)
assert len(ds.tags.list()) == 2
with pytest.raises(OSError):
ds.checkout_version("tag3")
assert ds.checkout_version("tag1").version == 1
ds = lance.dataset(base_dir, "tag1")
assert ds.version == 1
with pytest.raises(ValueError):
lance.dataset(base_dir, "missing-tag")
# test tag update
with pytest.raises(
ValueError, match="Version not found error: version main:3 does not exist"
):
ds.tags.update("tag1", 3)
with pytest.raises(
ValueError, match="Ref not found error: tag tag3 does not exist"
):
ds.tags.update("tag3", 1)
ds.tags.update("tag1", 2)
ds = lance.dataset(base_dir, "tag1")
assert ds.version == 2
ds.tags.update("tag1", 1)
ds = lance.dataset(base_dir, "tag1")
assert ds.version == 1
version = ds.tags.get_version("tag1")
assert version == 1
ds.create_branch("branch", "tag1")
ds.tags.create("tag3", ("branch", None))
target_tag = ds.tags.list().get("tag3")
assert ds.tags.get_version("tag3") == 1
assert len(ds.tags.list()) == 3
assert target_tag is not None
assert target_tag["version"] == 1
assert target_tag["branch"] == "branch"
ds.tags.update("tag3", (None, 2))
target_tag = ds.tags.list()["tag3"]
assert ds.tags.get_version("tag3") == 2
assert target_tag is not None
assert target_tag["version"] == 2
assert target_tag["branch"] is None
ds.create_branch("branch2", 2)
ds.tags.update("tag3", ("branch2", 2))
target_tag = ds.tags.list()["tag3"]
assert ds.tags.get_version("tag3") == 2
assert target_tag is not None
assert target_tag["version"] == 2
assert target_tag["branch"] == "branch2"
ds.tags.delete("tag3")
assert len(ds.tags.list()) == 2
def test_tag_order(tmp_path: Path):
table = pa.Table.from_pydict({"colA": [1, 2, 3], "colB": [4, 5, 6]})
base_dir = tmp_path / "test"
for i in range(3):
mode = "append" if i > 0 else "create"
ds = lance.write_dataset(table, base_dir, mode=mode)
expected_tags = {"tag3": 3, "tag2": 2, "tag1": 1}
for name, version in expected_tags.items():
ds.tags.create(name, version)
tags_asc = ds.tags.list_ordered(order="asc")
assert len(tags_asc) == 3
tag_names_asc = [t[0] for t in tags_asc]
assert tag_names_asc == sorted(expected_tags.keys()), (
f"Unexpected ascending order: {tag_names_asc}"
)
# Test descending order (default)
tags_desc = ds.tags.list_ordered(order="desc")
assert len(tags_desc) == 3
tag_names_desc = [t[0] for t in tags_desc]
assert tag_names_desc == list(expected_tags.keys()), (
f"Unexpected descending order: {tag_names_desc}"
)
# Test without parameter (should default to descending)
tags_default = ds.tags.list_ordered()
assert tags_default == tags_desc, "Default order should match descending order"
def test_sample(tmp_path: Path):
table1 = pa.Table.from_pydict({"x": [0, 10, 20, 30, 40, 50], "y": range(6)})
base_dir = tmp_path / "test"
lance.write_dataset(table1, base_dir)
dataset = lance.dataset(base_dir)
sampled = dataset.sample(3)
assert sampled.num_rows == 3
assert sampled.num_columns == 2
sampled = dataset.sample(4, columns=["x"])
assert sampled.num_rows == 4
assert sampled.num_columns == 1
for row in sampled.column(0).chunk(0):
assert row.as_py() % 10 == 0
def test_take(tmp_path: Path):
table1 = pa.Table.from_pylist([{"a": 1, "b": 2}, {"a": 10, "b": 20}])
base_dir = tmp_path / "test"
lance.write_dataset(table1, base_dir)
dataset = lance.dataset(base_dir)
table2 = dataset.take([0, 1])
assert isinstance(table2, pa.Table)
assert table2 == table1
def test_take_rowid_rowaddr(tmp_path: Path):
sample_size = 10
table1 = pa.table({"a": range(1000), "b": range(1000)})
base_dir = tmp_path / "test_take_rowid_rowaddr"
lance.write_dataset(
table1, base_dir, enable_stable_row_ids=False, max_rows_per_file=50
)
dataset = lance.dataset(base_dir)
total_rows = len(dataset)
sampled_indices = random.sample(range(total_rows), min(sample_size, total_rows))
sample_dataset = dataset.take(sampled_indices, columns=["_rowid"])
assert sample_dataset.num_rows == 10
assert sample_dataset.num_columns == 1
sample_dataset = dataset.take(sampled_indices, columns=["_rowid", "_rowid"])
assert sample_dataset.num_rows == 10
assert sample_dataset.num_columns == 2
sample_dataset = dataset.take([1, 2, 3, 4, 5, 6, 7, 8, 9, 100], columns=["_rowid"])
assert sample_dataset.num_rows == 10
assert sample_dataset.num_columns == 1
sample_dataset = dataset.take(sampled_indices, columns=["_rowaddr"])
assert sample_dataset.num_rows == 10
assert sample_dataset.num_columns == 1
sample_dataset = dataset.take(
[1, 2, 3, 4, 5, 6, 7, 8, 9, 100], columns=["_rowaddr"]
)
assert sample_dataset.num_rows == 10
assert sample_dataset.num_columns == 1
sample_dataset = dataset.take(sampled_indices, columns=["_rowaddr", "_rowid"])
assert sample_dataset.num_rows == 10
assert sample_dataset.num_columns == 2
sample_dataset = dataset.take(
[1, 2, 3, 4, 5, 6, 7, 8, 9, 100], columns=["_rowaddr", "_rowid"]
)
assert sample_dataset.num_rows == 10
assert sample_dataset.num_columns == 2
sample_dataset = dataset.take(sampled_indices, columns=["_rowid", "_rowaddr"])
assert sample_dataset.num_rows == 10
assert sample_dataset.num_columns == 2
sample_dataset = dataset.take(
[1, 2, 3, 4, 5, 6, 7, 8, 9, 100], columns=["_rowid", "_rowaddr"]
)
assert sample_dataset.num_rows == 10
assert sample_dataset.num_columns == 2
sample_dataset = dataset.take(sampled_indices, columns=["a", "_rowid", "_rowaddr"])
assert sample_dataset.num_rows == 10
assert sample_dataset.num_columns == 3
sample_dataset = dataset.take(
[1, 2, 3, 4, 5, 6, 7, 8, 9, 100], columns=["a", "_rowid", "_rowaddr"]
)
assert sample_dataset.num_rows == 10
assert sample_dataset.num_columns == 3
sample_dataset = dataset.take(sampled_indices, columns=["_rowid", "_rowaddr", "b"])
assert sample_dataset.num_rows == 10
assert sample_dataset.num_columns == 3
sample_dataset = dataset.take(
[1, 2, 3, 4, 5, 6, 7, 8, 9, 100], columns=["_rowid", "_rowaddr", "b"]
)
assert sample_dataset.num_rows == 10
assert sample_dataset.num_columns == 3
sample_dataset = dataset.take(sampled_indices, columns=["a", "b"])
assert sample_dataset.num_rows == 10
assert sample_dataset.num_columns == 2
sample_dataset = dataset.take([1, 2, 3, 4, 5, 6, 7, 8, 9, 100], columns=["a", "b"])
assert sample_dataset.num_rows == 10
assert sample_dataset.num_columns == 2
@pytest.mark.parametrize(
"column_name",
[
"_rowid",
"_rowaddr",
"_rowoffset",
"_row_created_at_version",
"_row_last_updated_at_version",
],
)
def test_take_system_columns_values(tmp_path: Path, column_name: str):
"""Test that system columns return correct values in take."""
table = pa.table({"a": range(100), "b": range(100, 200)})
base_dir = tmp_path / "test_take_system_columns_values"
# Use max_rows_per_file to create multiple fragments
lance.write_dataset(table, base_dir, max_rows_per_file=25)
dataset = lance.dataset(base_dir)
indices = [0, 5, 10, 50, 99]
result = dataset.take(indices, columns=[column_name, "a"])
assert result.num_rows == len(indices)
assert result.schema.names == [column_name, "a"]
col_values = result.column(column_name).to_pylist()
a_values = result.column("a").to_pylist()
# Verify column type is UInt64
assert result.column(column_name).type == pa.uint64()
# Verify data column values
assert a_values == indices
# Verify system column values based on column type
if column_name == "_rowid":
# Without stable row IDs, _rowid equals _rowaddr (not the index).
# Row address = (fragment_id << 32) | row_offset_within_fragment
# With max_rows_per_file=25: frag0=0-24, frag1=25-49, frag2=50-74, frag3=75-99
expected_rowids = [
(0 << 32) | 0, # index 0: fragment 0, offset 0
(0 << 32) | 5, # index 5: fragment 0, offset 5
(0 << 32) | 10, # index 10: fragment 0, offset 10
(2 << 32) | 0, # index 50: fragment 2, offset 0
(3 << 32) | 24, # index 99: fragment 3, offset 24
]
assert col_values == expected_rowids
elif column_name in ("_row_created_at_version", "_row_last_updated_at_version"):
# All rows created/updated at version 1
assert col_values == [1] * len(indices)
# _rowaddr and _rowoffset values depend on fragment layout
def test_take_system_columns_column_ordering(tmp_path: Path):
"""Test that column ordering is preserved when using system columns."""
table = pa.table({"a": range(50), "b": range(50, 100)})
base_dir = tmp_path / "test_take_column_ordering"
lance.write_dataset(table, base_dir)
dataset = lance.dataset(base_dir)
indices = [0, 1, 2]
# Test different orderings with all system columns
result = dataset.take(indices, columns=["_rowid", "a", "_rowaddr"])
assert result.schema.names == ["_rowid", "a", "_rowaddr"]
result = dataset.take(indices, columns=["a", "_rowaddr", "_rowid"])
assert result.schema.names == ["a", "_rowaddr", "_rowid"]
result = dataset.take(indices, columns=["_rowaddr", "_rowid", "b", "a"])
assert result.schema.names == ["_rowaddr", "_rowid", "b", "a"]
# Test with version columns
result = dataset.take(
indices,
columns=[
"_row_created_at_version",
"a",
"_row_last_updated_at_version",
"_rowid",
],
)
assert result.schema.names == [
"_row_created_at_version",
"a",
"_row_last_updated_at_version",
"_rowid",
]
# Test with all system columns in mixed order
result = dataset.take(
indices,
columns=[
"_rowoffset",
"_row_last_updated_at_version",
"b",
"_rowaddr",
"_row_created_at_version",
"a",
"_rowid",
],
)
assert result.schema.names == [
"_rowoffset",
"_row_last_updated_at_version",
"b",
"_rowaddr",
"_row_created_at_version",
"a",
"_rowid",
]
def test_take_version_system_columns(tmp_path: Path):
"""Test _row_created_at_version and _row_last_updated_at_version columns."""
table = pa.table({"a": range(50)})
base_dir = tmp_path / "test_take_version_columns"
lance.write_dataset(table, base_dir, enable_stable_row_ids=True)
dataset = lance.dataset(base_dir)
# Initial version is 1
initial_version = dataset.version
indices = [0, 10, 25]
result = dataset.take(
indices,
columns=["a", "_row_created_at_version", "_row_last_updated_at_version"],
)
assert result.num_rows == 3
created_at = result.column("_row_created_at_version").to_pylist()
updated_at = result.column("_row_last_updated_at_version").to_pylist()
# All rows were created and last updated at the initial version
assert created_at == [initial_version] * 3
assert updated_at == [initial_version] * 3
# Now update some rows by overwriting
table2 = pa.table({"a": range(50, 100)})
lance.write_dataset(table2, base_dir, mode="append")
dataset = lance.dataset(base_dir)
# New rows should have version 2
result = dataset.take([50, 60], columns=["_row_created_at_version"])
created_at = result.column("_row_created_at_version").to_pylist()
assert created_at == [dataset.version] * 2
@pytest.mark.parametrize("indices", [[], [1, 1], [1, 1, 20, 20, 21], [21, 0, 21, 1, 0]])
def test_take_duplicate_index(tmp_path: Path, indices: List[int]):
table = pa.table({"x": range(24)})
dataset = lance.write_dataset(
table, tmp_path, max_rows_per_group=3, max_rows_per_file=9
)
expected = table.take(pa.array(indices, pa.int64()))
assert dataset.take(indices) == expected
def test_take_with_columns(tmp_path: Path):
table1 = pa.Table.from_pylist([{"a": 1, "b": 2}, {"a": 10, "b": 20}])
base_dir = tmp_path / "test"
lance.write_dataset(table1, base_dir)
dataset = lance.dataset(base_dir)
table2 = dataset.take([0], columns=["b"])
assert table2 == pa.Table.from_pylist([{"b": 2}])
def test_take_with_projection(tmp_path: Path):
table1 = pa.Table.from_pylist([{"a": 1, "b": "x"}, {"a": 2, "b": "y"}])
base_dir = tmp_path / "test"
lance.write_dataset(table1, base_dir)
dataset = lance.dataset(base_dir)
table2 = dataset.take([0], columns={"a2": "a*2", "bup": "UPPER(b)"})
assert table2 == pa.Table.from_pylist([{"a2": 2, "bup": "X"}])
table3 = dataset._take_rows([0], columns={"a2": "a*2", "bup": "UPPER(b)"})
assert table3 == table2
def test_filter(tmp_path: Path):
table = pa.Table.from_pydict({"a": range(100), "b": range(100)})
base_dir = tmp_path / "test"
lance.write_dataset(table, base_dir)
dataset = lance.dataset(base_dir)
actual_tab = dataset.to_table(columns=["a"], filter=(pa.compute.field("b") > 50))
assert actual_tab == pa.Table.from_pydict({"a": range(51, 100)})
def test_filter_meta_columns(tmp_path: Path):
table = pa.Table.from_pydict({"a": range(100), "b": range(100)})
base_dir = tmp_path / "test"
ds = lance.write_dataset(table, base_dir)
rowids = ds.to_table(with_row_id=True, columns=[])
some_row_id = random.sample(rowids.column(0).to_pylist(), 1)[0]
filtered = ds.to_table(filter=f"_rowid = {some_row_id}", with_row_id=True)
assert len(filtered) == 1
rowaddrs = ds.to_table(with_row_address=True, columns=[])
some_row_addr = random.sample(rowaddrs.column(0).to_pylist(), 1)[0]
filtered = ds.to_table(filter=f"_rowaddr = {some_row_addr}", with_row_address=True)
assert len(filtered) == 1
@pytest.mark.parametrize("data_storage_version", ["legacy", "stable"])
def test_limit_offset(tmp_path: Path, data_storage_version: str):
table = pa.Table.from_pydict({"a": range(100), "b": range(100)})
base_dir = tmp_path / "test"
lance.write_dataset(
table, base_dir, data_storage_version=data_storage_version, max_rows_per_file=10
)
dataset = lance.dataset(base_dir)
# test just limit
assert dataset.to_table(limit=10) == table.slice(0, 10)
assert dataset.to_table(limit=100) == table.slice(0, 100)
# test just offset
assert dataset.to_table(offset=0) == table.slice(0, 100)
assert dataset.to_table(offset=10) == table.slice(10, 90)
# test both
assert dataset.to_table(offset=10, limit=10) == table.slice(10, 10)
# Slicing in the middle of fragments
assert dataset.to_table(offset=5, limit=20) == table.slice(5, 20)
# Slicing within a single fragment
assert dataset.to_table(offset=5, limit=3) == table.slice(5, 3)
# Skipping entire fragments
assert dataset.to_table(offset=50, limit=25) == table.slice(50, 25)
# Limit past the end
assert dataset.to_table(limit=101) == table.slice(0, 100)
# Limit with offset past the end
assert dataset.to_table(offset=50, limit=51) == table.slice(50, 50)
# Offset past the end
assert dataset.to_table(offset=100) == table.slice(100, 0) # Empty table
assert dataset.to_table(offset=101) == table.slice(100, 0) # Empty table
# Offset with limit past the end
assert dataset.to_table(offset=100, limit=1) == table.slice(100, 0) # Empty table
assert dataset.to_table(offset=101, limit=1) == table.slice(100, 0) # Empty table
# Invalid limit / offset
with pytest.raises(ValueError, match="Offset must be non-negative"):
assert dataset.to_table(offset=-1, limit=10) == table.slice(50, 50)
with pytest.raises(ValueError, match="Limit must be non-negative"):
assert dataset.to_table(offset=10, limit=-1) == table.slice(50, 50)
full_ds_version = dataset.version
dataset.delete("a % 2 = 0")
filt_table = table.filter((pa.compute.bit_wise_and(pa.compute.field("a"), 1)) != 0)
assert (
dataset.to_table(offset=10).combine_chunks()
== filt_table.slice(10).combine_chunks()
)
dataset = dataset.checkout_version(full_ds_version)
dataset.restore()
dataset.delete("a > 2 AND a < 7")
filt_table = table.slice(7, 1)
assert dataset.to_table(offset=3, limit=1) == filt_table
def test_relative_paths(tmp_path: Path):
# relative paths get coerced to the full absolute path
current_dir = os.getcwd()
table = pa.Table.from_pydict({"a": range(100), "b": range(100)})
rel_uri = "test.lance"
try:
os.chdir(tmp_path)
lance.write_dataset(table, rel_uri)
# relative path works in the current dir
ds = lance.dataset(rel_uri)
assert ds.to_table() == table
finally:
os.chdir(current_dir)
# relative path doesn't work in the context of a different dir
with pytest.raises(ValueError):
ds = lance.dataset(rel_uri)
# relative path gets resolved to the right absolute path
ds = lance.dataset(tmp_path / rel_uri)
assert ds.to_table() == table
@pytest.mark.skipif(
(platform.system() == "Windows"),
reason="mocking user's home folder it not working",
)
def test_tilde_paths(tmp_path: Path):
# tilde paths get resolved to the right absolute path
tilde_uri = "~/test.lance"
table = pa.Table.from_pydict({"a": range(100), "b": range(100)})
with mock.patch.dict(
os.environ, {"HOME": str(tmp_path), "USERPROFILE": str(tmp_path)}
):
# NOTE: the resolution logic is a bit finicky
# link 1 - https://docs.rs/dirs/4.0.0/dirs/fn.home_dir.html